BUG_ON() Conversion in ipc/shm.c
[linux-2.6/suspend2-2.6.18.git] / net / netfilter / nf_conntrack_netlink.c
blob0e0e9d7b34c806d4ea1594f821cb4b2125fd248a
1 /* Connection tracking via netlink socket. Allows for user space
2 * protocol helpers and general trouble making from userspace.
4 * (C) 2001 by Jay Schulist <jschlst@samba.org>
5 * (C) 2002-2006 by Harald Welte <laforge@gnumonks.org>
6 * (C) 2003 by Patrick Mchardy <kaber@trash.net>
7 * (C) 2005-2006 by Pablo Neira Ayuso <pablo@eurodev.net>
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.
12 * Initial connection tracking via netlink development funded and
13 * generally made possible by Network Robots, Inc. (www.networkrobots.com)
15 * Further development of this code funded by Astaro AG (http://www.astaro.com)
17 * This software may be used and distributed according to the terms
18 * of the GNU General Public License, incorporated herein by reference.
20 * Derived from ip_conntrack_netlink.c: Port by Pablo Neira Ayuso (05/11/14)
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>
32 #include <linux/notifier.h>
34 #include <linux/netfilter.h>
35 #include <net/netfilter/nf_conntrack.h>
36 #include <net/netfilter/nf_conntrack_core.h>
37 #include <net/netfilter/nf_conntrack_helper.h>
38 #include <net/netfilter/nf_conntrack_l3proto.h>
39 #include <net/netfilter/nf_conntrack_protocol.h>
40 #include <linux/netfilter_ipv4/ip_nat_protocol.h>
42 #include <linux/netfilter/nfnetlink.h>
43 #include <linux/netfilter/nfnetlink_conntrack.h>
45 MODULE_LICENSE("GPL");
47 static char __initdata version[] = "0.93";
49 #if 0
50 #define DEBUGP printk
51 #else
52 #define DEBUGP(format, args...)
53 #endif
56 static inline int
57 ctnetlink_dump_tuples_proto(struct sk_buff *skb,
58 const struct nf_conntrack_tuple *tuple,
59 struct nf_conntrack_protocol *proto)
61 int ret = 0;
62 struct nfattr *nest_parms = NFA_NEST(skb, CTA_TUPLE_PROTO);
64 NFA_PUT(skb, CTA_PROTO_NUM, sizeof(u_int8_t), &tuple->dst.protonum);
66 if (likely(proto->tuple_to_nfattr))
67 ret = proto->tuple_to_nfattr(skb, tuple);
69 NFA_NEST_END(skb, nest_parms);
71 return ret;
73 nfattr_failure:
74 return -1;
77 static inline int
78 ctnetlink_dump_tuples_ip(struct sk_buff *skb,
79 const struct nf_conntrack_tuple *tuple,
80 struct nf_conntrack_l3proto *l3proto)
82 int ret = 0;
83 struct nfattr *nest_parms = NFA_NEST(skb, CTA_TUPLE_IP);
85 if (likely(l3proto->tuple_to_nfattr))
86 ret = l3proto->tuple_to_nfattr(skb, tuple);
88 NFA_NEST_END(skb, nest_parms);
90 return ret;
92 nfattr_failure:
93 return -1;
96 static inline int
97 ctnetlink_dump_tuples(struct sk_buff *skb,
98 const struct nf_conntrack_tuple *tuple)
100 int ret;
101 struct nf_conntrack_l3proto *l3proto;
102 struct nf_conntrack_protocol *proto;
104 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
105 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
106 nf_ct_l3proto_put(l3proto);
108 if (unlikely(ret < 0))
109 return ret;
111 proto = nf_ct_proto_find_get(tuple->src.l3num, tuple->dst.protonum);
112 ret = ctnetlink_dump_tuples_proto(skb, tuple, proto);
113 nf_ct_proto_put(proto);
115 return ret;
118 static inline int
119 ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
121 u_int32_t status = htonl((u_int32_t) ct->status);
122 NFA_PUT(skb, CTA_STATUS, sizeof(status), &status);
123 return 0;
125 nfattr_failure:
126 return -1;
129 static inline int
130 ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
132 long timeout_l = ct->timeout.expires - jiffies;
133 u_int32_t timeout;
135 if (timeout_l < 0)
136 timeout = 0;
137 else
138 timeout = htonl(timeout_l / HZ);
140 NFA_PUT(skb, CTA_TIMEOUT, sizeof(timeout), &timeout);
141 return 0;
143 nfattr_failure:
144 return -1;
147 static inline int
148 ctnetlink_dump_protoinfo(struct sk_buff *skb, const struct nf_conn *ct)
150 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);
151 struct nfattr *nest_proto;
152 int ret;
154 if (!proto->to_nfattr) {
155 nf_ct_proto_put(proto);
156 return 0;
159 nest_proto = NFA_NEST(skb, CTA_PROTOINFO);
161 ret = proto->to_nfattr(skb, nest_proto, ct);
163 nf_ct_proto_put(proto);
165 NFA_NEST_END(skb, nest_proto);
167 return ret;
169 nfattr_failure:
170 return -1;
173 static inline int
174 ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
176 struct nfattr *nest_helper;
177 const struct nf_conn_help *help = nfct_help(ct);
179 if (!help || !help->helper)
180 return 0;
182 nest_helper = NFA_NEST(skb, CTA_HELP);
183 NFA_PUT(skb, CTA_HELP_NAME, strlen(help->helper->name), help->helper->name);
185 if (help->helper->to_nfattr)
186 help->helper->to_nfattr(skb, ct);
188 NFA_NEST_END(skb, nest_helper);
190 return 0;
192 nfattr_failure:
193 return -1;
196 #ifdef CONFIG_NF_CT_ACCT
197 static inline int
198 ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct,
199 enum ip_conntrack_dir dir)
201 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
202 struct nfattr *nest_count = NFA_NEST(skb, type);
203 u_int32_t tmp;
205 tmp = htonl(ct->counters[dir].packets);
206 NFA_PUT(skb, CTA_COUNTERS32_PACKETS, sizeof(u_int32_t), &tmp);
208 tmp = htonl(ct->counters[dir].bytes);
209 NFA_PUT(skb, CTA_COUNTERS32_BYTES, sizeof(u_int32_t), &tmp);
211 NFA_NEST_END(skb, nest_count);
213 return 0;
215 nfattr_failure:
216 return -1;
218 #else
219 #define ctnetlink_dump_counters(a, b, c) (0)
220 #endif
222 #ifdef CONFIG_NF_CONNTRACK_MARK
223 static inline int
224 ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
226 u_int32_t mark = htonl(ct->mark);
228 NFA_PUT(skb, CTA_MARK, sizeof(u_int32_t), &mark);
229 return 0;
231 nfattr_failure:
232 return -1;
234 #else
235 #define ctnetlink_dump_mark(a, b) (0)
236 #endif
238 static inline int
239 ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
241 u_int32_t id = htonl(ct->id);
242 NFA_PUT(skb, CTA_ID, sizeof(u_int32_t), &id);
243 return 0;
245 nfattr_failure:
246 return -1;
249 static inline int
250 ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
252 u_int32_t use = htonl(atomic_read(&ct->ct_general.use));
254 NFA_PUT(skb, CTA_USE, sizeof(u_int32_t), &use);
255 return 0;
257 nfattr_failure:
258 return -1;
261 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
263 static int
264 ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
265 int event, int nowait,
266 const struct nf_conn *ct)
268 struct nlmsghdr *nlh;
269 struct nfgenmsg *nfmsg;
270 struct nfattr *nest_parms;
271 unsigned char *b;
273 b = skb->tail;
275 event |= NFNL_SUBSYS_CTNETLINK << 8;
276 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
277 nfmsg = NLMSG_DATA(nlh);
279 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
280 nfmsg->nfgen_family =
281 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
282 nfmsg->version = NFNETLINK_V0;
283 nfmsg->res_id = 0;
285 nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG);
286 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
287 goto nfattr_failure;
288 NFA_NEST_END(skb, nest_parms);
290 nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY);
291 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
292 goto nfattr_failure;
293 NFA_NEST_END(skb, nest_parms);
295 if (ctnetlink_dump_status(skb, ct) < 0 ||
296 ctnetlink_dump_timeout(skb, ct) < 0 ||
297 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
298 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
299 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
300 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
301 ctnetlink_dump_mark(skb, ct) < 0 ||
302 ctnetlink_dump_id(skb, ct) < 0 ||
303 ctnetlink_dump_use(skb, ct) < 0)
304 goto nfattr_failure;
306 nlh->nlmsg_len = skb->tail - b;
307 return skb->len;
309 nlmsg_failure:
310 nfattr_failure:
311 skb_trim(skb, b - skb->data);
312 return -1;
315 #ifdef CONFIG_NF_CONNTRACK_EVENTS
316 static int ctnetlink_conntrack_event(struct notifier_block *this,
317 unsigned long events, void *ptr)
319 struct nlmsghdr *nlh;
320 struct nfgenmsg *nfmsg;
321 struct nfattr *nest_parms;
322 struct nf_conn *ct = (struct nf_conn *)ptr;
323 struct sk_buff *skb;
324 unsigned int type;
325 unsigned char *b;
326 unsigned int flags = 0, group;
328 /* ignore our fake conntrack entry */
329 if (ct == &nf_conntrack_untracked)
330 return NOTIFY_DONE;
332 if (events & IPCT_DESTROY) {
333 type = IPCTNL_MSG_CT_DELETE;
334 group = NFNLGRP_CONNTRACK_DESTROY;
335 } else if (events & (IPCT_NEW | IPCT_RELATED)) {
336 type = IPCTNL_MSG_CT_NEW;
337 flags = NLM_F_CREATE|NLM_F_EXCL;
338 /* dump everything */
339 events = ~0UL;
340 group = NFNLGRP_CONNTRACK_NEW;
341 } else if (events & (IPCT_STATUS |
342 IPCT_PROTOINFO |
343 IPCT_HELPER |
344 IPCT_HELPINFO |
345 IPCT_NATINFO)) {
346 type = IPCTNL_MSG_CT_NEW;
347 group = NFNLGRP_CONNTRACK_UPDATE;
348 } else
349 return NOTIFY_DONE;
351 if (!nfnetlink_has_listeners(group))
352 return NOTIFY_DONE;
354 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
355 if (!skb)
356 return NOTIFY_DONE;
358 b = skb->tail;
360 type |= NFNL_SUBSYS_CTNETLINK << 8;
361 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
362 nfmsg = NLMSG_DATA(nlh);
364 nlh->nlmsg_flags = flags;
365 nfmsg->nfgen_family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
366 nfmsg->version = NFNETLINK_V0;
367 nfmsg->res_id = 0;
369 nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG);
370 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
371 goto nfattr_failure;
372 NFA_NEST_END(skb, nest_parms);
374 nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY);
375 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
376 goto nfattr_failure;
377 NFA_NEST_END(skb, nest_parms);
379 /* NAT stuff is now a status flag */
380 if ((events & IPCT_STATUS || events & IPCT_NATINFO)
381 && ctnetlink_dump_status(skb, ct) < 0)
382 goto nfattr_failure;
383 if (events & IPCT_REFRESH
384 && ctnetlink_dump_timeout(skb, ct) < 0)
385 goto nfattr_failure;
386 if (events & IPCT_PROTOINFO
387 && ctnetlink_dump_protoinfo(skb, ct) < 0)
388 goto nfattr_failure;
389 if (events & IPCT_HELPINFO
390 && ctnetlink_dump_helpinfo(skb, ct) < 0)
391 goto nfattr_failure;
393 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
394 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
395 goto nfattr_failure;
397 nlh->nlmsg_len = skb->tail - b;
398 nfnetlink_send(skb, 0, group, 0);
399 return NOTIFY_DONE;
401 nlmsg_failure:
402 nfattr_failure:
403 kfree_skb(skb);
404 return NOTIFY_DONE;
406 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
408 static int ctnetlink_done(struct netlink_callback *cb)
410 DEBUGP("entered %s\n", __FUNCTION__);
411 return 0;
414 #define L3PROTO(ct) ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num
416 static int
417 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
419 struct nf_conn *ct = NULL;
420 struct nf_conntrack_tuple_hash *h;
421 struct list_head *i;
422 u_int32_t *id = (u_int32_t *) &cb->args[1];
423 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
424 u_int8_t l3proto = nfmsg->nfgen_family;
426 DEBUGP("entered %s, last bucket=%lu id=%u\n", __FUNCTION__,
427 cb->args[0], *id);
429 read_lock_bh(&nf_conntrack_lock);
430 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++, *id = 0) {
431 list_for_each_prev(i, &nf_conntrack_hash[cb->args[0]]) {
432 h = (struct nf_conntrack_tuple_hash *) i;
433 if (DIRECTION(h) != IP_CT_DIR_ORIGINAL)
434 continue;
435 ct = nf_ct_tuplehash_to_ctrack(h);
436 /* Dump entries of a given L3 protocol number.
437 * If it is not specified, ie. l3proto == 0,
438 * then dump everything. */
439 if (l3proto && L3PROTO(ct) != l3proto)
440 continue;
441 if (ct->id <= *id)
442 continue;
443 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
444 cb->nlh->nlmsg_seq,
445 IPCTNL_MSG_CT_NEW,
446 1, ct) < 0)
447 goto out;
448 *id = ct->id;
451 out:
452 read_unlock_bh(&nf_conntrack_lock);
454 DEBUGP("leaving, last bucket=%lu id=%u\n", cb->args[0], *id);
456 return skb->len;
459 #ifdef CONFIG_NF_CT_ACCT
460 static int
461 ctnetlink_dump_table_w(struct sk_buff *skb, struct netlink_callback *cb)
463 struct nf_conn *ct = NULL;
464 struct nf_conntrack_tuple_hash *h;
465 struct list_head *i;
466 u_int32_t *id = (u_int32_t *) &cb->args[1];
467 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
468 u_int8_t l3proto = nfmsg->nfgen_family;
470 DEBUGP("entered %s, last bucket=%u id=%u\n", __FUNCTION__,
471 cb->args[0], *id);
473 write_lock_bh(&nf_conntrack_lock);
474 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++, *id = 0) {
475 list_for_each_prev(i, &nf_conntrack_hash[cb->args[0]]) {
476 h = (struct nf_conntrack_tuple_hash *) i;
477 if (DIRECTION(h) != IP_CT_DIR_ORIGINAL)
478 continue;
479 ct = nf_ct_tuplehash_to_ctrack(h);
480 if (l3proto && L3PROTO(ct) != l3proto)
481 continue;
482 if (ct->id <= *id)
483 continue;
484 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
485 cb->nlh->nlmsg_seq,
486 IPCTNL_MSG_CT_NEW,
487 1, ct) < 0)
488 goto out;
489 *id = ct->id;
491 memset(&ct->counters, 0, sizeof(ct->counters));
494 out:
495 write_unlock_bh(&nf_conntrack_lock);
497 DEBUGP("leaving, last bucket=%lu id=%u\n", cb->args[0], *id);
499 return skb->len;
501 #endif
503 static inline int
504 ctnetlink_parse_tuple_ip(struct nfattr *attr, struct nf_conntrack_tuple *tuple)
506 struct nfattr *tb[CTA_IP_MAX];
507 struct nf_conntrack_l3proto *l3proto;
508 int ret = 0;
510 DEBUGP("entered %s\n", __FUNCTION__);
512 nfattr_parse_nested(tb, CTA_IP_MAX, attr);
514 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
516 if (likely(l3proto->nfattr_to_tuple))
517 ret = l3proto->nfattr_to_tuple(tb, tuple);
519 nf_ct_l3proto_put(l3proto);
521 DEBUGP("leaving\n");
523 return ret;
526 static const size_t cta_min_proto[CTA_PROTO_MAX] = {
527 [CTA_PROTO_NUM-1] = sizeof(u_int8_t),
530 static inline int
531 ctnetlink_parse_tuple_proto(struct nfattr *attr,
532 struct nf_conntrack_tuple *tuple)
534 struct nfattr *tb[CTA_PROTO_MAX];
535 struct nf_conntrack_protocol *proto;
536 int ret = 0;
538 DEBUGP("entered %s\n", __FUNCTION__);
540 nfattr_parse_nested(tb, CTA_PROTO_MAX, attr);
542 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
543 return -EINVAL;
545 if (!tb[CTA_PROTO_NUM-1])
546 return -EINVAL;
547 tuple->dst.protonum = *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_NUM-1]);
549 proto = nf_ct_proto_find_get(tuple->src.l3num, tuple->dst.protonum);
551 if (likely(proto->nfattr_to_tuple))
552 ret = proto->nfattr_to_tuple(tb, tuple);
554 nf_ct_proto_put(proto);
556 return ret;
559 static inline int
560 ctnetlink_parse_tuple(struct nfattr *cda[], struct nf_conntrack_tuple *tuple,
561 enum ctattr_tuple type, u_int8_t l3num)
563 struct nfattr *tb[CTA_TUPLE_MAX];
564 int err;
566 DEBUGP("entered %s\n", __FUNCTION__);
568 memset(tuple, 0, sizeof(*tuple));
570 nfattr_parse_nested(tb, CTA_TUPLE_MAX, cda[type-1]);
572 if (!tb[CTA_TUPLE_IP-1])
573 return -EINVAL;
575 tuple->src.l3num = l3num;
577 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP-1], tuple);
578 if (err < 0)
579 return err;
581 if (!tb[CTA_TUPLE_PROTO-1])
582 return -EINVAL;
584 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO-1], tuple);
585 if (err < 0)
586 return err;
588 /* orig and expect tuples get DIR_ORIGINAL */
589 if (type == CTA_TUPLE_REPLY)
590 tuple->dst.dir = IP_CT_DIR_REPLY;
591 else
592 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
594 NF_CT_DUMP_TUPLE(tuple);
596 DEBUGP("leaving\n");
598 return 0;
601 #ifdef CONFIG_IP_NF_NAT_NEEDED
602 static const size_t cta_min_protonat[CTA_PROTONAT_MAX] = {
603 [CTA_PROTONAT_PORT_MIN-1] = sizeof(u_int16_t),
604 [CTA_PROTONAT_PORT_MAX-1] = sizeof(u_int16_t),
607 static int ctnetlink_parse_nat_proto(struct nfattr *attr,
608 const struct nf_conn *ct,
609 struct ip_nat_range *range)
611 struct nfattr *tb[CTA_PROTONAT_MAX];
612 struct ip_nat_protocol *npt;
614 DEBUGP("entered %s\n", __FUNCTION__);
616 nfattr_parse_nested(tb, CTA_PROTONAT_MAX, attr);
618 if (nfattr_bad_size(tb, CTA_PROTONAT_MAX, cta_min_protonat))
619 return -EINVAL;
621 npt = ip_nat_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
623 if (!npt->nfattr_to_range) {
624 ip_nat_proto_put(npt);
625 return 0;
628 /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
629 if (npt->nfattr_to_range(tb, range) > 0)
630 range->flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
632 ip_nat_proto_put(npt);
634 DEBUGP("leaving\n");
635 return 0;
638 static const size_t cta_min_nat[CTA_NAT_MAX] = {
639 [CTA_NAT_MINIP-1] = sizeof(u_int32_t),
640 [CTA_NAT_MAXIP-1] = sizeof(u_int32_t),
643 static inline int
644 ctnetlink_parse_nat(struct nfattr *cda[],
645 const struct nf_conn *ct, struct ip_nat_range *range)
647 struct nfattr *tb[CTA_NAT_MAX];
648 int err;
650 DEBUGP("entered %s\n", __FUNCTION__);
652 memset(range, 0, sizeof(*range));
654 nfattr_parse_nested(tb, CTA_NAT_MAX, cda[CTA_NAT-1]);
656 if (nfattr_bad_size(tb, CTA_NAT_MAX, cta_min_nat))
657 return -EINVAL;
659 if (tb[CTA_NAT_MINIP-1])
660 range->min_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MINIP-1]);
662 if (!tb[CTA_NAT_MAXIP-1])
663 range->max_ip = range->min_ip;
664 else
665 range->max_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MAXIP-1]);
667 if (range->min_ip)
668 range->flags |= IP_NAT_RANGE_MAP_IPS;
670 if (!tb[CTA_NAT_PROTO-1])
671 return 0;
673 err = ctnetlink_parse_nat_proto(tb[CTA_NAT_PROTO-1], ct, range);
674 if (err < 0)
675 return err;
677 DEBUGP("leaving\n");
678 return 0;
680 #endif
682 static inline int
683 ctnetlink_parse_help(struct nfattr *attr, char **helper_name)
685 struct nfattr *tb[CTA_HELP_MAX];
687 DEBUGP("entered %s\n", __FUNCTION__);
689 nfattr_parse_nested(tb, CTA_HELP_MAX, attr);
691 if (!tb[CTA_HELP_NAME-1])
692 return -EINVAL;
694 *helper_name = NFA_DATA(tb[CTA_HELP_NAME-1]);
696 return 0;
699 static const size_t cta_min[CTA_MAX] = {
700 [CTA_STATUS-1] = sizeof(u_int32_t),
701 [CTA_TIMEOUT-1] = sizeof(u_int32_t),
702 [CTA_MARK-1] = sizeof(u_int32_t),
703 [CTA_USE-1] = sizeof(u_int32_t),
704 [CTA_ID-1] = sizeof(u_int32_t)
707 static int
708 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
709 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
711 struct nf_conntrack_tuple_hash *h;
712 struct nf_conntrack_tuple tuple;
713 struct nf_conn *ct;
714 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
715 u_int8_t u3 = nfmsg->nfgen_family;
716 int err = 0;
718 DEBUGP("entered %s\n", __FUNCTION__);
720 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
721 return -EINVAL;
723 if (cda[CTA_TUPLE_ORIG-1])
724 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
725 else if (cda[CTA_TUPLE_REPLY-1])
726 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
727 else {
728 /* Flush the whole table */
729 nf_conntrack_flush();
730 return 0;
733 if (err < 0)
734 return err;
736 h = nf_conntrack_find_get(&tuple, NULL);
737 if (!h) {
738 DEBUGP("tuple not found in conntrack hash\n");
739 return -ENOENT;
742 ct = nf_ct_tuplehash_to_ctrack(h);
744 if (cda[CTA_ID-1]) {
745 u_int32_t id = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_ID-1]));
746 if (ct->id != id) {
747 nf_ct_put(ct);
748 return -ENOENT;
751 if (del_timer(&ct->timeout))
752 ct->timeout.function((unsigned long)ct);
754 nf_ct_put(ct);
755 DEBUGP("leaving\n");
757 return 0;
760 static int
761 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
762 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
764 struct nf_conntrack_tuple_hash *h;
765 struct nf_conntrack_tuple tuple;
766 struct nf_conn *ct;
767 struct sk_buff *skb2 = NULL;
768 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
769 u_int8_t u3 = nfmsg->nfgen_family;
770 int err = 0;
772 DEBUGP("entered %s\n", __FUNCTION__);
774 if (nlh->nlmsg_flags & NLM_F_DUMP) {
775 u32 rlen;
777 if (NFNL_MSG_TYPE(nlh->nlmsg_type) ==
778 IPCTNL_MSG_CT_GET_CTRZERO) {
779 #ifdef CONFIG_NF_CT_ACCT
780 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
781 ctnetlink_dump_table_w,
782 ctnetlink_done)) != 0)
783 return -EINVAL;
784 #else
785 return -ENOTSUPP;
786 #endif
787 } else {
788 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
789 ctnetlink_dump_table,
790 ctnetlink_done)) != 0)
791 return -EINVAL;
794 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
795 if (rlen > skb->len)
796 rlen = skb->len;
797 skb_pull(skb, rlen);
798 return 0;
801 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
802 return -EINVAL;
804 if (cda[CTA_TUPLE_ORIG-1])
805 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
806 else if (cda[CTA_TUPLE_REPLY-1])
807 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
808 else
809 return -EINVAL;
811 if (err < 0)
812 return err;
814 h = nf_conntrack_find_get(&tuple, NULL);
815 if (!h) {
816 DEBUGP("tuple not found in conntrack hash");
817 return -ENOENT;
819 DEBUGP("tuple found\n");
820 ct = nf_ct_tuplehash_to_ctrack(h);
822 err = -ENOMEM;
823 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
824 if (!skb2) {
825 nf_ct_put(ct);
826 return -ENOMEM;
828 NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
830 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
831 IPCTNL_MSG_CT_NEW, 1, ct);
832 nf_ct_put(ct);
833 if (err <= 0)
834 goto free;
836 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
837 if (err < 0)
838 goto out;
840 DEBUGP("leaving\n");
841 return 0;
843 free:
844 kfree_skb(skb2);
845 out:
846 return err;
849 static inline int
850 ctnetlink_change_status(struct nf_conn *ct, struct nfattr *cda[])
852 unsigned long d;
853 unsigned status = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_STATUS-1]));
854 d = ct->status ^ status;
856 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
857 /* unchangeable */
858 return -EINVAL;
860 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
861 /* SEEN_REPLY bit can only be set */
862 return -EINVAL;
865 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
866 /* ASSURED bit can only be set */
867 return -EINVAL;
869 if (cda[CTA_NAT-1]) {
870 #ifndef CONFIG_IP_NF_NAT_NEEDED
871 return -EINVAL;
872 #else
873 unsigned int hooknum;
874 struct ip_nat_range range;
876 if (ctnetlink_parse_nat(cda, ct, &range) < 0)
877 return -EINVAL;
879 DEBUGP("NAT: %u.%u.%u.%u-%u.%u.%u.%u:%u-%u\n",
880 NIPQUAD(range.min_ip), NIPQUAD(range.max_ip),
881 htons(range.min.all), htons(range.max.all));
883 /* This is tricky but it works. ip_nat_setup_info needs the
884 * hook number as parameter, so let's do the correct
885 * conversion and run away */
886 if (status & IPS_SRC_NAT_DONE)
887 hooknum = NF_IP_POST_ROUTING; /* IP_NAT_MANIP_SRC */
888 else if (status & IPS_DST_NAT_DONE)
889 hooknum = NF_IP_PRE_ROUTING; /* IP_NAT_MANIP_DST */
890 else
891 return -EINVAL; /* Missing NAT flags */
893 DEBUGP("NAT status: %lu\n",
894 status & (IPS_NAT_MASK | IPS_NAT_DONE_MASK));
896 if (ip_nat_initialized(ct, HOOK2MANIP(hooknum)))
897 return -EEXIST;
898 ip_nat_setup_info(ct, &range, hooknum);
900 DEBUGP("NAT status after setup_info: %lu\n",
901 ct->status & (IPS_NAT_MASK | IPS_NAT_DONE_MASK));
902 #endif
905 /* Be careful here, modifying NAT bits can screw up things,
906 * so don't let users modify them directly if they don't pass
907 * ip_nat_range. */
908 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
909 return 0;
913 static inline int
914 ctnetlink_change_helper(struct nf_conn *ct, struct nfattr *cda[])
916 struct nf_conntrack_helper *helper;
917 struct nf_conn_help *help = nfct_help(ct);
918 char *helpname;
919 int err;
921 DEBUGP("entered %s\n", __FUNCTION__);
923 if (!help) {
924 /* FIXME: we need to reallocate and rehash */
925 return -EBUSY;
928 /* don't change helper of sibling connections */
929 if (ct->master)
930 return -EINVAL;
932 err = ctnetlink_parse_help(cda[CTA_HELP-1], &helpname);
933 if (err < 0)
934 return err;
936 helper = __nf_conntrack_helper_find_byname(helpname);
937 if (!helper) {
938 if (!strcmp(helpname, ""))
939 helper = NULL;
940 else
941 return -EINVAL;
944 if (help->helper) {
945 if (!helper) {
946 /* we had a helper before ... */
947 nf_ct_remove_expectations(ct);
948 help->helper = NULL;
949 } else {
950 /* need to zero data of old helper */
951 memset(&help->help, 0, sizeof(help->help));
955 help->helper = helper;
957 return 0;
960 static inline int
961 ctnetlink_change_timeout(struct nf_conn *ct, struct nfattr *cda[])
963 u_int32_t timeout = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
965 if (!del_timer(&ct->timeout))
966 return -ETIME;
968 ct->timeout.expires = jiffies + timeout * HZ;
969 add_timer(&ct->timeout);
971 return 0;
974 static inline int
975 ctnetlink_change_protoinfo(struct nf_conn *ct, struct nfattr *cda[])
977 struct nfattr *tb[CTA_PROTOINFO_MAX], *attr = cda[CTA_PROTOINFO-1];
978 struct nf_conntrack_protocol *proto;
979 u_int16_t npt = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum;
980 u_int16_t l3num = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
981 int err = 0;
983 nfattr_parse_nested(tb, CTA_PROTOINFO_MAX, attr);
985 proto = nf_ct_proto_find_get(l3num, npt);
987 if (proto->from_nfattr)
988 err = proto->from_nfattr(tb, ct);
989 nf_ct_proto_put(proto);
991 return err;
994 static int
995 ctnetlink_change_conntrack(struct nf_conn *ct, struct nfattr *cda[])
997 int err;
999 DEBUGP("entered %s\n", __FUNCTION__);
1001 if (cda[CTA_HELP-1]) {
1002 err = ctnetlink_change_helper(ct, cda);
1003 if (err < 0)
1004 return err;
1007 if (cda[CTA_TIMEOUT-1]) {
1008 err = ctnetlink_change_timeout(ct, cda);
1009 if (err < 0)
1010 return err;
1013 if (cda[CTA_STATUS-1]) {
1014 err = ctnetlink_change_status(ct, cda);
1015 if (err < 0)
1016 return err;
1019 if (cda[CTA_PROTOINFO-1]) {
1020 err = ctnetlink_change_protoinfo(ct, cda);
1021 if (err < 0)
1022 return err;
1025 #if defined(CONFIG_IP_NF_CONNTRACK_MARK)
1026 if (cda[CTA_MARK-1])
1027 ct->mark = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_MARK-1]));
1028 #endif
1030 DEBUGP("all done\n");
1031 return 0;
1034 static int
1035 ctnetlink_create_conntrack(struct nfattr *cda[],
1036 struct nf_conntrack_tuple *otuple,
1037 struct nf_conntrack_tuple *rtuple)
1039 struct nf_conn *ct;
1040 int err = -EINVAL;
1042 DEBUGP("entered %s\n", __FUNCTION__);
1044 ct = nf_conntrack_alloc(otuple, rtuple);
1045 if (ct == NULL || IS_ERR(ct))
1046 return -ENOMEM;
1048 if (!cda[CTA_TIMEOUT-1])
1049 goto err;
1050 ct->timeout.expires = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
1052 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1053 ct->status |= IPS_CONFIRMED;
1055 err = ctnetlink_change_status(ct, cda);
1056 if (err < 0)
1057 goto err;
1059 if (cda[CTA_PROTOINFO-1]) {
1060 err = ctnetlink_change_protoinfo(ct, cda);
1061 if (err < 0)
1062 return err;
1065 #if defined(CONFIG_IP_NF_CONNTRACK_MARK)
1066 if (cda[CTA_MARK-1])
1067 ct->mark = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_MARK-1]));
1068 #endif
1070 add_timer(&ct->timeout);
1071 nf_conntrack_hash_insert(ct);
1073 DEBUGP("conntrack with id %u inserted\n", ct->id);
1074 return 0;
1076 err:
1077 nf_conntrack_free(ct);
1078 return err;
1081 static int
1082 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1083 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1085 struct nf_conntrack_tuple otuple, rtuple;
1086 struct nf_conntrack_tuple_hash *h = NULL;
1087 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1088 u_int8_t u3 = nfmsg->nfgen_family;
1089 int err = 0;
1091 DEBUGP("entered %s\n", __FUNCTION__);
1093 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
1094 return -EINVAL;
1096 if (cda[CTA_TUPLE_ORIG-1]) {
1097 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1098 if (err < 0)
1099 return err;
1102 if (cda[CTA_TUPLE_REPLY-1]) {
1103 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1104 if (err < 0)
1105 return err;
1108 write_lock_bh(&nf_conntrack_lock);
1109 if (cda[CTA_TUPLE_ORIG-1])
1110 h = __nf_conntrack_find(&otuple, NULL);
1111 else if (cda[CTA_TUPLE_REPLY-1])
1112 h = __nf_conntrack_find(&rtuple, NULL);
1114 if (h == NULL) {
1115 write_unlock_bh(&nf_conntrack_lock);
1116 DEBUGP("no such conntrack, create new\n");
1117 err = -ENOENT;
1118 if (nlh->nlmsg_flags & NLM_F_CREATE)
1119 err = ctnetlink_create_conntrack(cda, &otuple, &rtuple);
1120 return err;
1122 /* implicit 'else' */
1124 /* we only allow nat config for new conntracks */
1125 if (cda[CTA_NAT-1]) {
1126 err = -EINVAL;
1127 goto out_unlock;
1130 /* We manipulate the conntrack inside the global conntrack table lock,
1131 * so there's no need to increase the refcount */
1132 DEBUGP("conntrack found\n");
1133 err = -EEXIST;
1134 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1135 err = ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h), cda);
1137 out_unlock:
1138 write_unlock_bh(&nf_conntrack_lock);
1139 return err;
1142 /***********************************************************************
1143 * EXPECT
1144 ***********************************************************************/
1146 static inline int
1147 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1148 const struct nf_conntrack_tuple *tuple,
1149 enum ctattr_expect type)
1151 struct nfattr *nest_parms = NFA_NEST(skb, type);
1153 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1154 goto nfattr_failure;
1156 NFA_NEST_END(skb, nest_parms);
1158 return 0;
1160 nfattr_failure:
1161 return -1;
1164 static inline int
1165 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1166 const struct nf_conntrack_tuple *tuple,
1167 const struct nf_conntrack_tuple *mask)
1169 int ret;
1170 struct nf_conntrack_l3proto *l3proto;
1171 struct nf_conntrack_protocol *proto;
1172 struct nfattr *nest_parms = NFA_NEST(skb, CTA_EXPECT_MASK);
1174 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
1175 ret = ctnetlink_dump_tuples_ip(skb, mask, l3proto);
1176 nf_ct_l3proto_put(l3proto);
1178 if (unlikely(ret < 0))
1179 goto nfattr_failure;
1181 proto = nf_ct_proto_find_get(tuple->src.l3num, tuple->dst.protonum);
1182 ret = ctnetlink_dump_tuples_proto(skb, mask, proto);
1183 nf_ct_proto_put(proto);
1184 if (unlikely(ret < 0))
1185 goto nfattr_failure;
1187 NFA_NEST_END(skb, nest_parms);
1189 return 0;
1191 nfattr_failure:
1192 return -1;
1195 static inline int
1196 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1197 const struct nf_conntrack_expect *exp)
1199 struct nf_conn *master = exp->master;
1200 u_int32_t timeout = htonl((exp->timeout.expires - jiffies) / HZ);
1201 u_int32_t id = htonl(exp->id);
1203 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1204 goto nfattr_failure;
1205 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1206 goto nfattr_failure;
1207 if (ctnetlink_exp_dump_tuple(skb,
1208 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1209 CTA_EXPECT_MASTER) < 0)
1210 goto nfattr_failure;
1212 NFA_PUT(skb, CTA_EXPECT_TIMEOUT, sizeof(timeout), &timeout);
1213 NFA_PUT(skb, CTA_EXPECT_ID, sizeof(u_int32_t), &id);
1215 return 0;
1217 nfattr_failure:
1218 return -1;
1221 static int
1222 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1223 int event,
1224 int nowait,
1225 const struct nf_conntrack_expect *exp)
1227 struct nlmsghdr *nlh;
1228 struct nfgenmsg *nfmsg;
1229 unsigned char *b;
1231 b = skb->tail;
1233 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1234 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1235 nfmsg = NLMSG_DATA(nlh);
1237 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
1238 nfmsg->nfgen_family = exp->tuple.src.l3num;
1239 nfmsg->version = NFNETLINK_V0;
1240 nfmsg->res_id = 0;
1242 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1243 goto nfattr_failure;
1245 nlh->nlmsg_len = skb->tail - b;
1246 return skb->len;
1248 nlmsg_failure:
1249 nfattr_failure:
1250 skb_trim(skb, b - skb->data);
1251 return -1;
1254 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1255 static int ctnetlink_expect_event(struct notifier_block *this,
1256 unsigned long events, void *ptr)
1258 struct nlmsghdr *nlh;
1259 struct nfgenmsg *nfmsg;
1260 struct nf_conntrack_expect *exp = (struct nf_conntrack_expect *)ptr;
1261 struct sk_buff *skb;
1262 unsigned int type;
1263 unsigned char *b;
1264 int flags = 0;
1266 if (events & IPEXP_NEW) {
1267 type = IPCTNL_MSG_EXP_NEW;
1268 flags = NLM_F_CREATE|NLM_F_EXCL;
1269 } else
1270 return NOTIFY_DONE;
1272 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1273 if (!skb)
1274 return NOTIFY_DONE;
1276 b = skb->tail;
1278 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1279 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1280 nfmsg = NLMSG_DATA(nlh);
1282 nlh->nlmsg_flags = flags;
1283 nfmsg->nfgen_family = exp->tuple.src.l3num;
1284 nfmsg->version = NFNETLINK_V0;
1285 nfmsg->res_id = 0;
1287 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1288 goto nfattr_failure;
1290 nlh->nlmsg_len = skb->tail - b;
1291 nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
1292 return NOTIFY_DONE;
1294 nlmsg_failure:
1295 nfattr_failure:
1296 kfree_skb(skb);
1297 return NOTIFY_DONE;
1299 #endif
1301 static int
1302 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1304 struct nf_conntrack_expect *exp = NULL;
1305 struct list_head *i;
1306 u_int32_t *id = (u_int32_t *) &cb->args[0];
1307 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
1308 u_int8_t l3proto = nfmsg->nfgen_family;
1310 DEBUGP("entered %s, last id=%llu\n", __FUNCTION__, *id);
1312 read_lock_bh(&nf_conntrack_lock);
1313 list_for_each_prev(i, &nf_conntrack_expect_list) {
1314 exp = (struct nf_conntrack_expect *) i;
1315 if (l3proto && exp->tuple.src.l3num != l3proto)
1316 continue;
1317 if (exp->id <= *id)
1318 continue;
1319 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1320 cb->nlh->nlmsg_seq,
1321 IPCTNL_MSG_EXP_NEW,
1322 1, exp) < 0)
1323 goto out;
1324 *id = exp->id;
1326 out:
1327 read_unlock_bh(&nf_conntrack_lock);
1329 DEBUGP("leaving, last id=%llu\n", *id);
1331 return skb->len;
1334 static const size_t cta_min_exp[CTA_EXPECT_MAX] = {
1335 [CTA_EXPECT_TIMEOUT-1] = sizeof(u_int32_t),
1336 [CTA_EXPECT_ID-1] = sizeof(u_int32_t)
1339 static int
1340 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1341 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1343 struct nf_conntrack_tuple tuple;
1344 struct nf_conntrack_expect *exp;
1345 struct sk_buff *skb2;
1346 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1347 u_int8_t u3 = nfmsg->nfgen_family;
1348 int err = 0;
1350 DEBUGP("entered %s\n", __FUNCTION__);
1352 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1353 return -EINVAL;
1355 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1356 u32 rlen;
1358 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
1359 ctnetlink_exp_dump_table,
1360 ctnetlink_done)) != 0)
1361 return -EINVAL;
1362 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1363 if (rlen > skb->len)
1364 rlen = skb->len;
1365 skb_pull(skb, rlen);
1366 return 0;
1369 if (cda[CTA_EXPECT_MASTER-1])
1370 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1371 else
1372 return -EINVAL;
1374 if (err < 0)
1375 return err;
1377 exp = nf_conntrack_expect_find(&tuple);
1378 if (!exp)
1379 return -ENOENT;
1381 if (cda[CTA_EXPECT_ID-1]) {
1382 u_int32_t id = *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1383 if (exp->id != ntohl(id)) {
1384 nf_conntrack_expect_put(exp);
1385 return -ENOENT;
1389 err = -ENOMEM;
1390 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1391 if (!skb2)
1392 goto out;
1393 NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
1395 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1396 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1397 1, exp);
1398 if (err <= 0)
1399 goto free;
1401 nf_conntrack_expect_put(exp);
1403 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1405 free:
1406 kfree_skb(skb2);
1407 out:
1408 nf_conntrack_expect_put(exp);
1409 return err;
1412 static int
1413 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1414 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1416 struct nf_conntrack_expect *exp, *tmp;
1417 struct nf_conntrack_tuple tuple;
1418 struct nf_conntrack_helper *h;
1419 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1420 u_int8_t u3 = nfmsg->nfgen_family;
1421 int err;
1423 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1424 return -EINVAL;
1426 if (cda[CTA_EXPECT_TUPLE-1]) {
1427 /* delete a single expect by tuple */
1428 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1429 if (err < 0)
1430 return err;
1432 /* bump usage count to 2 */
1433 exp = nf_conntrack_expect_find(&tuple);
1434 if (!exp)
1435 return -ENOENT;
1437 if (cda[CTA_EXPECT_ID-1]) {
1438 u_int32_t id =
1439 *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1440 if (exp->id != ntohl(id)) {
1441 nf_conntrack_expect_put(exp);
1442 return -ENOENT;
1446 /* after list removal, usage count == 1 */
1447 nf_conntrack_unexpect_related(exp);
1448 /* have to put what we 'get' above.
1449 * after this line usage count == 0 */
1450 nf_conntrack_expect_put(exp);
1451 } else if (cda[CTA_EXPECT_HELP_NAME-1]) {
1452 char *name = NFA_DATA(cda[CTA_EXPECT_HELP_NAME-1]);
1454 /* delete all expectations for this helper */
1455 write_lock_bh(&nf_conntrack_lock);
1456 h = __nf_conntrack_helper_find_byname(name);
1457 if (!h) {
1458 write_unlock_bh(&nf_conntrack_lock);
1459 return -EINVAL;
1461 list_for_each_entry_safe(exp, tmp, &nf_conntrack_expect_list,
1462 list) {
1463 struct nf_conn_help *m_help = nfct_help(exp->master);
1464 if (m_help->helper == h
1465 && del_timer(&exp->timeout)) {
1466 nf_ct_unlink_expect(exp);
1467 nf_conntrack_expect_put(exp);
1470 write_unlock_bh(&nf_conntrack_lock);
1471 } else {
1472 /* This basically means we have to flush everything*/
1473 write_lock_bh(&nf_conntrack_lock);
1474 list_for_each_entry_safe(exp, tmp, &nf_conntrack_expect_list,
1475 list) {
1476 if (del_timer(&exp->timeout)) {
1477 nf_ct_unlink_expect(exp);
1478 nf_conntrack_expect_put(exp);
1481 write_unlock_bh(&nf_conntrack_lock);
1484 return 0;
1486 static int
1487 ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nfattr *cda[])
1489 return -EOPNOTSUPP;
1492 static int
1493 ctnetlink_create_expect(struct nfattr *cda[], u_int8_t u3)
1495 struct nf_conntrack_tuple tuple, mask, master_tuple;
1496 struct nf_conntrack_tuple_hash *h = NULL;
1497 struct nf_conntrack_expect *exp;
1498 struct nf_conn *ct;
1499 struct nf_conn_help *help;
1500 int err = 0;
1502 DEBUGP("entered %s\n", __FUNCTION__);
1504 /* caller guarantees that those three CTA_EXPECT_* exist */
1505 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1506 if (err < 0)
1507 return err;
1508 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1509 if (err < 0)
1510 return err;
1511 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1512 if (err < 0)
1513 return err;
1515 /* Look for master conntrack of this expectation */
1516 h = nf_conntrack_find_get(&master_tuple, NULL);
1517 if (!h)
1518 return -ENOENT;
1519 ct = nf_ct_tuplehash_to_ctrack(h);
1520 help = nfct_help(ct);
1522 if (!help || !help->helper) {
1523 /* such conntrack hasn't got any helper, abort */
1524 err = -EINVAL;
1525 goto out;
1528 exp = nf_conntrack_expect_alloc(ct);
1529 if (!exp) {
1530 err = -ENOMEM;
1531 goto out;
1534 exp->expectfn = NULL;
1535 exp->flags = 0;
1536 exp->master = ct;
1537 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
1538 memcpy(&exp->mask, &mask, sizeof(struct nf_conntrack_tuple));
1540 err = nf_conntrack_expect_related(exp);
1541 nf_conntrack_expect_put(exp);
1543 out:
1544 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1545 return err;
1548 static int
1549 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1550 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1552 struct nf_conntrack_tuple tuple;
1553 struct nf_conntrack_expect *exp;
1554 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1555 u_int8_t u3 = nfmsg->nfgen_family;
1556 int err = 0;
1558 DEBUGP("entered %s\n", __FUNCTION__);
1560 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1561 return -EINVAL;
1563 if (!cda[CTA_EXPECT_TUPLE-1]
1564 || !cda[CTA_EXPECT_MASK-1]
1565 || !cda[CTA_EXPECT_MASTER-1])
1566 return -EINVAL;
1568 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1569 if (err < 0)
1570 return err;
1572 write_lock_bh(&nf_conntrack_lock);
1573 exp = __nf_conntrack_expect_find(&tuple);
1575 if (!exp) {
1576 write_unlock_bh(&nf_conntrack_lock);
1577 err = -ENOENT;
1578 if (nlh->nlmsg_flags & NLM_F_CREATE)
1579 err = ctnetlink_create_expect(cda, u3);
1580 return err;
1583 err = -EEXIST;
1584 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1585 err = ctnetlink_change_expect(exp, cda);
1586 write_unlock_bh(&nf_conntrack_lock);
1588 DEBUGP("leaving\n");
1590 return err;
1593 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1594 static struct notifier_block ctnl_notifier = {
1595 .notifier_call = ctnetlink_conntrack_event,
1598 static struct notifier_block ctnl_notifier_exp = {
1599 .notifier_call = ctnetlink_expect_event,
1601 #endif
1603 static struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1604 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1605 .attr_count = CTA_MAX, },
1606 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1607 .attr_count = CTA_MAX, },
1608 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1609 .attr_count = CTA_MAX, },
1610 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1611 .attr_count = CTA_MAX, },
1614 static struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1615 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1616 .attr_count = CTA_EXPECT_MAX, },
1617 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1618 .attr_count = CTA_EXPECT_MAX, },
1619 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1620 .attr_count = CTA_EXPECT_MAX, },
1623 static struct nfnetlink_subsystem ctnl_subsys = {
1624 .name = "conntrack",
1625 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1626 .cb_count = IPCTNL_MSG_MAX,
1627 .cb = ctnl_cb,
1630 static struct nfnetlink_subsystem ctnl_exp_subsys = {
1631 .name = "conntrack_expect",
1632 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1633 .cb_count = IPCTNL_MSG_EXP_MAX,
1634 .cb = ctnl_exp_cb,
1637 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
1638 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
1640 static int __init ctnetlink_init(void)
1642 int ret;
1644 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1645 ret = nfnetlink_subsys_register(&ctnl_subsys);
1646 if (ret < 0) {
1647 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1648 goto err_out;
1651 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1652 if (ret < 0) {
1653 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1654 goto err_unreg_subsys;
1657 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1658 ret = nf_conntrack_register_notifier(&ctnl_notifier);
1659 if (ret < 0) {
1660 printk("ctnetlink_init: cannot register notifier.\n");
1661 goto err_unreg_exp_subsys;
1664 ret = nf_conntrack_expect_register_notifier(&ctnl_notifier_exp);
1665 if (ret < 0) {
1666 printk("ctnetlink_init: cannot expect register notifier.\n");
1667 goto err_unreg_notifier;
1669 #endif
1671 return 0;
1673 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1674 err_unreg_notifier:
1675 nf_conntrack_unregister_notifier(&ctnl_notifier);
1676 err_unreg_exp_subsys:
1677 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1678 #endif
1679 err_unreg_subsys:
1680 nfnetlink_subsys_unregister(&ctnl_subsys);
1681 err_out:
1682 return ret;
1685 static void __exit ctnetlink_exit(void)
1687 printk("ctnetlink: unregistering from nfnetlink.\n");
1689 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1690 nf_conntrack_unregister_notifier(&ctnl_notifier_exp);
1691 nf_conntrack_unregister_notifier(&ctnl_notifier);
1692 #endif
1694 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1695 nfnetlink_subsys_unregister(&ctnl_subsys);
1696 return;
1699 module_init(ctnetlink_init);
1700 module_exit(ctnetlink_exit);