[NETLINK]: Remove error pointer from netlink message handler
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / netfilter / nf_conntrack_netlink.c
blob443ba7753a333ac2eb06f410bc9c79bc8572c78a
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/interrupt.h>
33 #include <linux/notifier.h>
35 #include <linux/netfilter.h>
36 #include <net/netlink.h>
37 #include <net/netfilter/nf_conntrack.h>
38 #include <net/netfilter/nf_conntrack_core.h>
39 #include <net/netfilter/nf_conntrack_expect.h>
40 #include <net/netfilter/nf_conntrack_helper.h>
41 #include <net/netfilter/nf_conntrack_l3proto.h>
42 #include <net/netfilter/nf_conntrack_l4proto.h>
43 #include <net/netfilter/nf_conntrack_tuple.h>
44 #ifdef CONFIG_NF_NAT_NEEDED
45 #include <net/netfilter/nf_nat_core.h>
46 #include <net/netfilter/nf_nat_protocol.h>
47 #endif
49 #include <linux/netfilter/nfnetlink.h>
50 #include <linux/netfilter/nfnetlink_conntrack.h>
52 MODULE_LICENSE("GPL");
54 static char __initdata version[] = "0.93";
56 static inline int
57 ctnetlink_dump_tuples_proto(struct sk_buff *skb,
58 const struct nf_conntrack_tuple *tuple,
59 struct nf_conntrack_l4proto *l4proto)
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(l4proto->tuple_to_nfattr))
67 ret = l4proto->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_l4proto *l4proto;
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 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
112 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
113 nf_ct_l4proto_put(l4proto);
115 return ret;
118 static inline int
119 ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
121 __be32 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 __be32 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_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);
151 struct nfattr *nest_proto;
152 int ret;
154 if (!l4proto->to_nfattr) {
155 nf_ct_l4proto_put(l4proto);
156 return 0;
159 nest_proto = NFA_NEST(skb, CTA_PROTOINFO);
161 ret = l4proto->to_nfattr(skb, nest_proto, ct);
163 nf_ct_l4proto_put(l4proto);
165 NFA_NEST_END(skb, nest_proto);
167 return ret;
169 nfattr_failure:
170 nf_ct_l4proto_put(l4proto);
171 return -1;
174 static inline int
175 ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
177 struct nfattr *nest_helper;
178 const struct nf_conn_help *help = nfct_help(ct);
180 if (!help || !help->helper)
181 return 0;
183 nest_helper = NFA_NEST(skb, CTA_HELP);
184 NFA_PUT(skb, CTA_HELP_NAME, strlen(help->helper->name), help->helper->name);
186 if (help->helper->to_nfattr)
187 help->helper->to_nfattr(skb, ct);
189 NFA_NEST_END(skb, nest_helper);
191 return 0;
193 nfattr_failure:
194 return -1;
197 #ifdef CONFIG_NF_CT_ACCT
198 static inline int
199 ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct,
200 enum ip_conntrack_dir dir)
202 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
203 struct nfattr *nest_count = NFA_NEST(skb, type);
204 __be32 tmp;
206 tmp = htonl(ct->counters[dir].packets);
207 NFA_PUT(skb, CTA_COUNTERS32_PACKETS, sizeof(u_int32_t), &tmp);
209 tmp = htonl(ct->counters[dir].bytes);
210 NFA_PUT(skb, CTA_COUNTERS32_BYTES, sizeof(u_int32_t), &tmp);
212 NFA_NEST_END(skb, nest_count);
214 return 0;
216 nfattr_failure:
217 return -1;
219 #else
220 #define ctnetlink_dump_counters(a, b, c) (0)
221 #endif
223 #ifdef CONFIG_NF_CONNTRACK_MARK
224 static inline int
225 ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
227 __be32 mark = htonl(ct->mark);
229 NFA_PUT(skb, CTA_MARK, sizeof(u_int32_t), &mark);
230 return 0;
232 nfattr_failure:
233 return -1;
235 #else
236 #define ctnetlink_dump_mark(a, b) (0)
237 #endif
239 static inline int
240 ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
242 __be32 id = htonl(ct->id);
243 NFA_PUT(skb, CTA_ID, sizeof(u_int32_t), &id);
244 return 0;
246 nfattr_failure:
247 return -1;
250 static inline int
251 ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
253 __be32 use = htonl(atomic_read(&ct->ct_general.use));
255 NFA_PUT(skb, CTA_USE, sizeof(u_int32_t), &use);
256 return 0;
258 nfattr_failure:
259 return -1;
262 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
264 static int
265 ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
266 int event, int nowait,
267 const struct nf_conn *ct)
269 struct nlmsghdr *nlh;
270 struct nfgenmsg *nfmsg;
271 struct nfattr *nest_parms;
272 unsigned char *b = skb_tail_pointer(skb);
274 event |= NFNL_SUBSYS_CTNETLINK << 8;
275 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
276 nfmsg = NLMSG_DATA(nlh);
278 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
279 nfmsg->nfgen_family =
280 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
281 nfmsg->version = NFNETLINK_V0;
282 nfmsg->res_id = 0;
284 nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG);
285 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
286 goto nfattr_failure;
287 NFA_NEST_END(skb, nest_parms);
289 nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY);
290 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
291 goto nfattr_failure;
292 NFA_NEST_END(skb, nest_parms);
294 if (ctnetlink_dump_status(skb, ct) < 0 ||
295 ctnetlink_dump_timeout(skb, ct) < 0 ||
296 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
297 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
298 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
299 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
300 ctnetlink_dump_mark(skb, ct) < 0 ||
301 ctnetlink_dump_id(skb, ct) < 0 ||
302 ctnetlink_dump_use(skb, ct) < 0)
303 goto nfattr_failure;
305 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
306 return skb->len;
308 nlmsg_failure:
309 nfattr_failure:
310 nlmsg_trim(skb, b);
311 return -1;
314 #ifdef CONFIG_NF_CONNTRACK_EVENTS
315 static int ctnetlink_conntrack_event(struct notifier_block *this,
316 unsigned long events, void *ptr)
318 struct nlmsghdr *nlh;
319 struct nfgenmsg *nfmsg;
320 struct nfattr *nest_parms;
321 struct nf_conn *ct = (struct nf_conn *)ptr;
322 struct sk_buff *skb;
323 unsigned int type;
324 sk_buff_data_t b;
325 unsigned int flags = 0, group;
327 /* ignore our fake conntrack entry */
328 if (ct == &nf_conntrack_untracked)
329 return NOTIFY_DONE;
331 if (events & IPCT_DESTROY) {
332 type = IPCTNL_MSG_CT_DELETE;
333 group = NFNLGRP_CONNTRACK_DESTROY;
334 } else if (events & (IPCT_NEW | IPCT_RELATED)) {
335 type = IPCTNL_MSG_CT_NEW;
336 flags = NLM_F_CREATE|NLM_F_EXCL;
337 group = NFNLGRP_CONNTRACK_NEW;
338 } else if (events & (IPCT_STATUS | IPCT_PROTOINFO)) {
339 type = IPCTNL_MSG_CT_NEW;
340 group = NFNLGRP_CONNTRACK_UPDATE;
341 } else
342 return NOTIFY_DONE;
344 if (!nfnetlink_has_listeners(group))
345 return NOTIFY_DONE;
347 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
348 if (!skb)
349 return NOTIFY_DONE;
351 b = skb->tail;
353 type |= NFNL_SUBSYS_CTNETLINK << 8;
354 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
355 nfmsg = NLMSG_DATA(nlh);
357 nlh->nlmsg_flags = flags;
358 nfmsg->nfgen_family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
359 nfmsg->version = NFNETLINK_V0;
360 nfmsg->res_id = 0;
362 nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG);
363 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
364 goto nfattr_failure;
365 NFA_NEST_END(skb, nest_parms);
367 nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY);
368 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
369 goto nfattr_failure;
370 NFA_NEST_END(skb, nest_parms);
372 if (events & IPCT_DESTROY) {
373 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
374 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
375 goto nfattr_failure;
376 } else {
377 if (ctnetlink_dump_status(skb, ct) < 0)
378 goto nfattr_failure;
380 if (ctnetlink_dump_timeout(skb, ct) < 0)
381 goto nfattr_failure;
383 if (events & IPCT_PROTOINFO
384 && ctnetlink_dump_protoinfo(skb, ct) < 0)
385 goto nfattr_failure;
387 if ((events & IPCT_HELPER || nfct_help(ct))
388 && ctnetlink_dump_helpinfo(skb, ct) < 0)
389 goto nfattr_failure;
391 #ifdef CONFIG_NF_CONNTRACK_MARK
392 if ((events & IPCT_MARK || ct->mark)
393 && ctnetlink_dump_mark(skb, ct) < 0)
394 goto nfattr_failure;
395 #endif
397 if (events & IPCT_COUNTER_FILLING &&
398 (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
399 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0))
400 goto nfattr_failure;
403 nlh->nlmsg_len = skb->tail - b;
404 nfnetlink_send(skb, 0, group, 0);
405 return NOTIFY_DONE;
407 nlmsg_failure:
408 nfattr_failure:
409 kfree_skb(skb);
410 return NOTIFY_DONE;
412 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
414 static int ctnetlink_done(struct netlink_callback *cb)
416 if (cb->args[1])
417 nf_ct_put((struct nf_conn *)cb->args[1]);
418 return 0;
421 #define L3PROTO(ct) ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num
423 static int
424 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
426 struct nf_conn *ct, *last;
427 struct nf_conntrack_tuple_hash *h;
428 struct list_head *i;
429 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
430 u_int8_t l3proto = nfmsg->nfgen_family;
432 read_lock_bh(&nf_conntrack_lock);
433 last = (struct nf_conn *)cb->args[1];
434 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
435 restart:
436 list_for_each_prev(i, &nf_conntrack_hash[cb->args[0]]) {
437 h = (struct nf_conntrack_tuple_hash *) i;
438 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
439 continue;
440 ct = nf_ct_tuplehash_to_ctrack(h);
441 /* Dump entries of a given L3 protocol number.
442 * If it is not specified, ie. l3proto == 0,
443 * then dump everything. */
444 if (l3proto && L3PROTO(ct) != l3proto)
445 continue;
446 if (cb->args[1]) {
447 if (ct != last)
448 continue;
449 cb->args[1] = 0;
451 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
452 cb->nlh->nlmsg_seq,
453 IPCTNL_MSG_CT_NEW,
454 1, ct) < 0) {
455 nf_conntrack_get(&ct->ct_general);
456 cb->args[1] = (unsigned long)ct;
457 goto out;
459 #ifdef CONFIG_NF_CT_ACCT
460 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) ==
461 IPCTNL_MSG_CT_GET_CTRZERO)
462 memset(&ct->counters, 0, sizeof(ct->counters));
463 #endif
465 if (cb->args[1]) {
466 cb->args[1] = 0;
467 goto restart;
470 out:
471 read_unlock_bh(&nf_conntrack_lock);
472 if (last)
473 nf_ct_put(last);
475 return skb->len;
478 static inline int
479 ctnetlink_parse_tuple_ip(struct nfattr *attr, struct nf_conntrack_tuple *tuple)
481 struct nfattr *tb[CTA_IP_MAX];
482 struct nf_conntrack_l3proto *l3proto;
483 int ret = 0;
485 nfattr_parse_nested(tb, CTA_IP_MAX, attr);
487 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
489 if (likely(l3proto->nfattr_to_tuple))
490 ret = l3proto->nfattr_to_tuple(tb, tuple);
492 nf_ct_l3proto_put(l3proto);
494 return ret;
497 static const size_t cta_min_proto[CTA_PROTO_MAX] = {
498 [CTA_PROTO_NUM-1] = sizeof(u_int8_t),
501 static inline int
502 ctnetlink_parse_tuple_proto(struct nfattr *attr,
503 struct nf_conntrack_tuple *tuple)
505 struct nfattr *tb[CTA_PROTO_MAX];
506 struct nf_conntrack_l4proto *l4proto;
507 int ret = 0;
509 nfattr_parse_nested(tb, CTA_PROTO_MAX, attr);
511 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
512 return -EINVAL;
514 if (!tb[CTA_PROTO_NUM-1])
515 return -EINVAL;
516 tuple->dst.protonum = *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_NUM-1]);
518 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
520 if (likely(l4proto->nfattr_to_tuple))
521 ret = l4proto->nfattr_to_tuple(tb, tuple);
523 nf_ct_l4proto_put(l4proto);
525 return ret;
528 static inline int
529 ctnetlink_parse_tuple(struct nfattr *cda[], struct nf_conntrack_tuple *tuple,
530 enum ctattr_tuple type, u_int8_t l3num)
532 struct nfattr *tb[CTA_TUPLE_MAX];
533 int err;
535 memset(tuple, 0, sizeof(*tuple));
537 nfattr_parse_nested(tb, CTA_TUPLE_MAX, cda[type-1]);
539 if (!tb[CTA_TUPLE_IP-1])
540 return -EINVAL;
542 tuple->src.l3num = l3num;
544 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP-1], tuple);
545 if (err < 0)
546 return err;
548 if (!tb[CTA_TUPLE_PROTO-1])
549 return -EINVAL;
551 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO-1], tuple);
552 if (err < 0)
553 return err;
555 /* orig and expect tuples get DIR_ORIGINAL */
556 if (type == CTA_TUPLE_REPLY)
557 tuple->dst.dir = IP_CT_DIR_REPLY;
558 else
559 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
561 return 0;
564 #ifdef CONFIG_NF_NAT_NEEDED
565 static const size_t cta_min_protonat[CTA_PROTONAT_MAX] = {
566 [CTA_PROTONAT_PORT_MIN-1] = sizeof(u_int16_t),
567 [CTA_PROTONAT_PORT_MAX-1] = sizeof(u_int16_t),
570 static int nfnetlink_parse_nat_proto(struct nfattr *attr,
571 const struct nf_conn *ct,
572 struct nf_nat_range *range)
574 struct nfattr *tb[CTA_PROTONAT_MAX];
575 struct nf_nat_protocol *npt;
577 nfattr_parse_nested(tb, CTA_PROTONAT_MAX, attr);
579 if (nfattr_bad_size(tb, CTA_PROTONAT_MAX, cta_min_protonat))
580 return -EINVAL;
582 npt = nf_nat_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
584 if (!npt->nfattr_to_range) {
585 nf_nat_proto_put(npt);
586 return 0;
589 /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
590 if (npt->nfattr_to_range(tb, range) > 0)
591 range->flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
593 nf_nat_proto_put(npt);
595 return 0;
598 static const size_t cta_min_nat[CTA_NAT_MAX] = {
599 [CTA_NAT_MINIP-1] = sizeof(u_int32_t),
600 [CTA_NAT_MAXIP-1] = sizeof(u_int32_t),
603 static inline int
604 nfnetlink_parse_nat(struct nfattr *nat,
605 const struct nf_conn *ct, struct nf_nat_range *range)
607 struct nfattr *tb[CTA_NAT_MAX];
608 int err;
610 memset(range, 0, sizeof(*range));
612 nfattr_parse_nested(tb, CTA_NAT_MAX, nat);
614 if (nfattr_bad_size(tb, CTA_NAT_MAX, cta_min_nat))
615 return -EINVAL;
617 if (tb[CTA_NAT_MINIP-1])
618 range->min_ip = *(__be32 *)NFA_DATA(tb[CTA_NAT_MINIP-1]);
620 if (!tb[CTA_NAT_MAXIP-1])
621 range->max_ip = range->min_ip;
622 else
623 range->max_ip = *(__be32 *)NFA_DATA(tb[CTA_NAT_MAXIP-1]);
625 if (range->min_ip)
626 range->flags |= IP_NAT_RANGE_MAP_IPS;
628 if (!tb[CTA_NAT_PROTO-1])
629 return 0;
631 err = nfnetlink_parse_nat_proto(tb[CTA_NAT_PROTO-1], ct, range);
632 if (err < 0)
633 return err;
635 return 0;
637 #endif
639 static inline int
640 ctnetlink_parse_help(struct nfattr *attr, char **helper_name)
642 struct nfattr *tb[CTA_HELP_MAX];
644 nfattr_parse_nested(tb, CTA_HELP_MAX, attr);
646 if (!tb[CTA_HELP_NAME-1])
647 return -EINVAL;
649 *helper_name = NFA_DATA(tb[CTA_HELP_NAME-1]);
651 return 0;
654 static const size_t cta_min[CTA_MAX] = {
655 [CTA_STATUS-1] = sizeof(u_int32_t),
656 [CTA_TIMEOUT-1] = sizeof(u_int32_t),
657 [CTA_MARK-1] = sizeof(u_int32_t),
658 [CTA_USE-1] = sizeof(u_int32_t),
659 [CTA_ID-1] = sizeof(u_int32_t)
662 static int
663 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
664 struct nlmsghdr *nlh, struct nfattr *cda[])
666 struct nf_conntrack_tuple_hash *h;
667 struct nf_conntrack_tuple tuple;
668 struct nf_conn *ct;
669 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
670 u_int8_t u3 = nfmsg->nfgen_family;
671 int err = 0;
673 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
674 return -EINVAL;
676 if (cda[CTA_TUPLE_ORIG-1])
677 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
678 else if (cda[CTA_TUPLE_REPLY-1])
679 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
680 else {
681 /* Flush the whole table */
682 nf_conntrack_flush();
683 return 0;
686 if (err < 0)
687 return err;
689 h = nf_conntrack_find_get(&tuple, NULL);
690 if (!h)
691 return -ENOENT;
693 ct = nf_ct_tuplehash_to_ctrack(h);
695 if (cda[CTA_ID-1]) {
696 u_int32_t id = ntohl(*(__be32 *)NFA_DATA(cda[CTA_ID-1]));
697 if (ct->id != id) {
698 nf_ct_put(ct);
699 return -ENOENT;
702 if (del_timer(&ct->timeout))
703 ct->timeout.function((unsigned long)ct);
705 nf_ct_put(ct);
707 return 0;
710 static int
711 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
712 struct nlmsghdr *nlh, struct nfattr *cda[])
714 struct nf_conntrack_tuple_hash *h;
715 struct nf_conntrack_tuple tuple;
716 struct nf_conn *ct;
717 struct sk_buff *skb2 = NULL;
718 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
719 u_int8_t u3 = nfmsg->nfgen_family;
720 int err = 0;
722 if (nlh->nlmsg_flags & NLM_F_DUMP) {
723 #ifndef CONFIG_NF_CT_ACCT
724 if (NFNL_MSG_TYPE(nlh->nlmsg_type) == IPCTNL_MSG_CT_GET_CTRZERO)
725 return -ENOTSUPP;
726 #endif
727 err = netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
728 ctnetlink_done);
729 if (err == 0)
730 err = -EINTR;
731 return err;
734 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
735 return -EINVAL;
737 if (cda[CTA_TUPLE_ORIG-1])
738 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
739 else if (cda[CTA_TUPLE_REPLY-1])
740 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
741 else
742 return -EINVAL;
744 if (err < 0)
745 return err;
747 h = nf_conntrack_find_get(&tuple, NULL);
748 if (!h)
749 return -ENOENT;
751 ct = nf_ct_tuplehash_to_ctrack(h);
753 err = -ENOMEM;
754 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
755 if (!skb2) {
756 nf_ct_put(ct);
757 return -ENOMEM;
760 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
761 IPCTNL_MSG_CT_NEW, 1, ct);
762 nf_ct_put(ct);
763 if (err <= 0)
764 goto free;
766 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
767 if (err < 0)
768 goto out;
770 return 0;
772 free:
773 kfree_skb(skb2);
774 out:
775 return err;
778 static inline int
779 ctnetlink_change_status(struct nf_conn *ct, struct nfattr *cda[])
781 unsigned long d;
782 unsigned int status = ntohl(*(__be32 *)NFA_DATA(cda[CTA_STATUS-1]));
783 d = ct->status ^ status;
785 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
786 /* unchangeable */
787 return -EINVAL;
789 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
790 /* SEEN_REPLY bit can only be set */
791 return -EINVAL;
794 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
795 /* ASSURED bit can only be set */
796 return -EINVAL;
798 if (cda[CTA_NAT_SRC-1] || cda[CTA_NAT_DST-1]) {
799 #ifndef CONFIG_NF_NAT_NEEDED
800 return -EINVAL;
801 #else
802 struct nf_nat_range range;
804 if (cda[CTA_NAT_DST-1]) {
805 if (nfnetlink_parse_nat(cda[CTA_NAT_DST-1], ct,
806 &range) < 0)
807 return -EINVAL;
808 if (nf_nat_initialized(ct,
809 HOOK2MANIP(NF_IP_PRE_ROUTING)))
810 return -EEXIST;
811 nf_nat_setup_info(ct, &range, NF_IP_PRE_ROUTING);
813 if (cda[CTA_NAT_SRC-1]) {
814 if (nfnetlink_parse_nat(cda[CTA_NAT_SRC-1], ct,
815 &range) < 0)
816 return -EINVAL;
817 if (nf_nat_initialized(ct,
818 HOOK2MANIP(NF_IP_POST_ROUTING)))
819 return -EEXIST;
820 nf_nat_setup_info(ct, &range, NF_IP_POST_ROUTING);
822 #endif
825 /* Be careful here, modifying NAT bits can screw up things,
826 * so don't let users modify them directly if they don't pass
827 * nf_nat_range. */
828 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
829 return 0;
833 static inline int
834 ctnetlink_change_helper(struct nf_conn *ct, struct nfattr *cda[])
836 struct nf_conntrack_helper *helper;
837 struct nf_conn_help *help = nfct_help(ct);
838 char *helpname;
839 int err;
841 if (!help) {
842 /* FIXME: we need to reallocate and rehash */
843 return -EBUSY;
846 /* don't change helper of sibling connections */
847 if (ct->master)
848 return -EINVAL;
850 err = ctnetlink_parse_help(cda[CTA_HELP-1], &helpname);
851 if (err < 0)
852 return err;
854 helper = __nf_conntrack_helper_find_byname(helpname);
855 if (!helper) {
856 if (!strcmp(helpname, ""))
857 helper = NULL;
858 else
859 return -EINVAL;
862 if (help->helper) {
863 if (!helper) {
864 /* we had a helper before ... */
865 nf_ct_remove_expectations(ct);
866 help->helper = NULL;
867 } else {
868 /* need to zero data of old helper */
869 memset(&help->help, 0, sizeof(help->help));
873 help->helper = helper;
875 return 0;
878 static inline int
879 ctnetlink_change_timeout(struct nf_conn *ct, struct nfattr *cda[])
881 u_int32_t timeout = ntohl(*(__be32 *)NFA_DATA(cda[CTA_TIMEOUT-1]));
883 if (!del_timer(&ct->timeout))
884 return -ETIME;
886 ct->timeout.expires = jiffies + timeout * HZ;
887 add_timer(&ct->timeout);
889 return 0;
892 static inline int
893 ctnetlink_change_protoinfo(struct nf_conn *ct, struct nfattr *cda[])
895 struct nfattr *tb[CTA_PROTOINFO_MAX], *attr = cda[CTA_PROTOINFO-1];
896 struct nf_conntrack_l4proto *l4proto;
897 u_int16_t npt = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum;
898 u_int16_t l3num = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
899 int err = 0;
901 nfattr_parse_nested(tb, CTA_PROTOINFO_MAX, attr);
903 l4proto = nf_ct_l4proto_find_get(l3num, npt);
905 if (l4proto->from_nfattr)
906 err = l4proto->from_nfattr(tb, ct);
907 nf_ct_l4proto_put(l4proto);
909 return err;
912 static int
913 ctnetlink_change_conntrack(struct nf_conn *ct, struct nfattr *cda[])
915 int err;
917 if (cda[CTA_HELP-1]) {
918 err = ctnetlink_change_helper(ct, cda);
919 if (err < 0)
920 return err;
923 if (cda[CTA_TIMEOUT-1]) {
924 err = ctnetlink_change_timeout(ct, cda);
925 if (err < 0)
926 return err;
929 if (cda[CTA_STATUS-1]) {
930 err = ctnetlink_change_status(ct, cda);
931 if (err < 0)
932 return err;
935 if (cda[CTA_PROTOINFO-1]) {
936 err = ctnetlink_change_protoinfo(ct, cda);
937 if (err < 0)
938 return err;
941 #if defined(CONFIG_NF_CONNTRACK_MARK)
942 if (cda[CTA_MARK-1])
943 ct->mark = ntohl(*(__be32 *)NFA_DATA(cda[CTA_MARK-1]));
944 #endif
946 return 0;
949 static int
950 ctnetlink_create_conntrack(struct nfattr *cda[],
951 struct nf_conntrack_tuple *otuple,
952 struct nf_conntrack_tuple *rtuple)
954 struct nf_conn *ct;
955 int err = -EINVAL;
956 struct nf_conn_help *help;
958 ct = nf_conntrack_alloc(otuple, rtuple);
959 if (ct == NULL || IS_ERR(ct))
960 return -ENOMEM;
962 if (!cda[CTA_TIMEOUT-1])
963 goto err;
964 ct->timeout.expires = ntohl(*(__be32 *)NFA_DATA(cda[CTA_TIMEOUT-1]));
966 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
967 ct->status |= IPS_CONFIRMED;
969 if (cda[CTA_STATUS-1]) {
970 err = ctnetlink_change_status(ct, cda);
971 if (err < 0)
972 goto err;
975 if (cda[CTA_PROTOINFO-1]) {
976 err = ctnetlink_change_protoinfo(ct, cda);
977 if (err < 0)
978 goto err;
981 #if defined(CONFIG_NF_CONNTRACK_MARK)
982 if (cda[CTA_MARK-1])
983 ct->mark = ntohl(*(__be32 *)NFA_DATA(cda[CTA_MARK-1]));
984 #endif
986 help = nfct_help(ct);
987 if (help)
988 help->helper = nf_ct_helper_find_get(rtuple);
990 add_timer(&ct->timeout);
991 nf_conntrack_hash_insert(ct);
993 if (help && help->helper)
994 nf_ct_helper_put(help->helper);
996 return 0;
998 err:
999 nf_conntrack_free(ct);
1000 return err;
1003 static int
1004 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1005 struct nlmsghdr *nlh, struct nfattr *cda[])
1007 struct nf_conntrack_tuple otuple, rtuple;
1008 struct nf_conntrack_tuple_hash *h = NULL;
1009 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1010 u_int8_t u3 = nfmsg->nfgen_family;
1011 int err = 0;
1013 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
1014 return -EINVAL;
1016 if (cda[CTA_TUPLE_ORIG-1]) {
1017 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1018 if (err < 0)
1019 return err;
1022 if (cda[CTA_TUPLE_REPLY-1]) {
1023 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1024 if (err < 0)
1025 return err;
1028 write_lock_bh(&nf_conntrack_lock);
1029 if (cda[CTA_TUPLE_ORIG-1])
1030 h = __nf_conntrack_find(&otuple, NULL);
1031 else if (cda[CTA_TUPLE_REPLY-1])
1032 h = __nf_conntrack_find(&rtuple, NULL);
1034 if (h == NULL) {
1035 write_unlock_bh(&nf_conntrack_lock);
1036 err = -ENOENT;
1037 if (nlh->nlmsg_flags & NLM_F_CREATE)
1038 err = ctnetlink_create_conntrack(cda, &otuple, &rtuple);
1039 return err;
1041 /* implicit 'else' */
1043 /* we only allow nat config for new conntracks */
1044 if (cda[CTA_NAT_SRC-1] || cda[CTA_NAT_DST-1]) {
1045 err = -EINVAL;
1046 goto out_unlock;
1049 /* We manipulate the conntrack inside the global conntrack table lock,
1050 * so there's no need to increase the refcount */
1051 err = -EEXIST;
1052 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1053 err = ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h), cda);
1055 out_unlock:
1056 write_unlock_bh(&nf_conntrack_lock);
1057 return err;
1060 /***********************************************************************
1061 * EXPECT
1062 ***********************************************************************/
1064 static inline int
1065 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1066 const struct nf_conntrack_tuple *tuple,
1067 enum ctattr_expect type)
1069 struct nfattr *nest_parms = NFA_NEST(skb, type);
1071 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1072 goto nfattr_failure;
1074 NFA_NEST_END(skb, nest_parms);
1076 return 0;
1078 nfattr_failure:
1079 return -1;
1082 static inline int
1083 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1084 const struct nf_conntrack_tuple *tuple,
1085 const struct nf_conntrack_tuple *mask)
1087 int ret;
1088 struct nf_conntrack_l3proto *l3proto;
1089 struct nf_conntrack_l4proto *l4proto;
1090 struct nfattr *nest_parms = NFA_NEST(skb, CTA_EXPECT_MASK);
1092 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
1093 ret = ctnetlink_dump_tuples_ip(skb, mask, l3proto);
1094 nf_ct_l3proto_put(l3proto);
1096 if (unlikely(ret < 0))
1097 goto nfattr_failure;
1099 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
1100 ret = ctnetlink_dump_tuples_proto(skb, mask, l4proto);
1101 nf_ct_l4proto_put(l4proto);
1102 if (unlikely(ret < 0))
1103 goto nfattr_failure;
1105 NFA_NEST_END(skb, nest_parms);
1107 return 0;
1109 nfattr_failure:
1110 return -1;
1113 static inline int
1114 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1115 const struct nf_conntrack_expect *exp)
1117 struct nf_conn *master = exp->master;
1118 __be32 timeout = htonl((exp->timeout.expires - jiffies) / HZ);
1119 __be32 id = htonl(exp->id);
1121 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1122 goto nfattr_failure;
1123 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1124 goto nfattr_failure;
1125 if (ctnetlink_exp_dump_tuple(skb,
1126 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1127 CTA_EXPECT_MASTER) < 0)
1128 goto nfattr_failure;
1130 NFA_PUT(skb, CTA_EXPECT_TIMEOUT, sizeof(timeout), &timeout);
1131 NFA_PUT(skb, CTA_EXPECT_ID, sizeof(u_int32_t), &id);
1133 return 0;
1135 nfattr_failure:
1136 return -1;
1139 static int
1140 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1141 int event,
1142 int nowait,
1143 const struct nf_conntrack_expect *exp)
1145 struct nlmsghdr *nlh;
1146 struct nfgenmsg *nfmsg;
1147 unsigned char *b = skb_tail_pointer(skb);
1149 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1150 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1151 nfmsg = NLMSG_DATA(nlh);
1153 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
1154 nfmsg->nfgen_family = exp->tuple.src.l3num;
1155 nfmsg->version = NFNETLINK_V0;
1156 nfmsg->res_id = 0;
1158 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1159 goto nfattr_failure;
1161 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1162 return skb->len;
1164 nlmsg_failure:
1165 nfattr_failure:
1166 nlmsg_trim(skb, b);
1167 return -1;
1170 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1171 static int ctnetlink_expect_event(struct notifier_block *this,
1172 unsigned long events, void *ptr)
1174 struct nlmsghdr *nlh;
1175 struct nfgenmsg *nfmsg;
1176 struct nf_conntrack_expect *exp = (struct nf_conntrack_expect *)ptr;
1177 struct sk_buff *skb;
1178 unsigned int type;
1179 sk_buff_data_t b;
1180 int flags = 0;
1182 if (events & IPEXP_NEW) {
1183 type = IPCTNL_MSG_EXP_NEW;
1184 flags = NLM_F_CREATE|NLM_F_EXCL;
1185 } else
1186 return NOTIFY_DONE;
1188 if (!nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW))
1189 return NOTIFY_DONE;
1191 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1192 if (!skb)
1193 return NOTIFY_DONE;
1195 b = skb->tail;
1197 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1198 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1199 nfmsg = NLMSG_DATA(nlh);
1201 nlh->nlmsg_flags = flags;
1202 nfmsg->nfgen_family = exp->tuple.src.l3num;
1203 nfmsg->version = NFNETLINK_V0;
1204 nfmsg->res_id = 0;
1206 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1207 goto nfattr_failure;
1209 nlh->nlmsg_len = skb->tail - b;
1210 nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
1211 return NOTIFY_DONE;
1213 nlmsg_failure:
1214 nfattr_failure:
1215 kfree_skb(skb);
1216 return NOTIFY_DONE;
1218 #endif
1220 static int
1221 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1223 struct nf_conntrack_expect *exp = NULL;
1224 struct list_head *i;
1225 u_int32_t *id = (u_int32_t *) &cb->args[0];
1226 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
1227 u_int8_t l3proto = nfmsg->nfgen_family;
1229 read_lock_bh(&nf_conntrack_lock);
1230 list_for_each_prev(i, &nf_conntrack_expect_list) {
1231 exp = (struct nf_conntrack_expect *) i;
1232 if (l3proto && exp->tuple.src.l3num != l3proto)
1233 continue;
1234 if (exp->id <= *id)
1235 continue;
1236 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1237 cb->nlh->nlmsg_seq,
1238 IPCTNL_MSG_EXP_NEW,
1239 1, exp) < 0)
1240 goto out;
1241 *id = exp->id;
1243 out:
1244 read_unlock_bh(&nf_conntrack_lock);
1246 return skb->len;
1249 static const size_t cta_min_exp[CTA_EXPECT_MAX] = {
1250 [CTA_EXPECT_TIMEOUT-1] = sizeof(u_int32_t),
1251 [CTA_EXPECT_ID-1] = sizeof(u_int32_t)
1254 static int
1255 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1256 struct nlmsghdr *nlh, struct nfattr *cda[])
1258 struct nf_conntrack_tuple tuple;
1259 struct nf_conntrack_expect *exp;
1260 struct sk_buff *skb2;
1261 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1262 u_int8_t u3 = nfmsg->nfgen_family;
1263 int err = 0;
1265 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1266 return -EINVAL;
1268 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1269 err = netlink_dump_start(ctnl, skb, nlh,
1270 ctnetlink_exp_dump_table,
1271 ctnetlink_done);
1272 if (err == 0)
1273 err = -EINTR;
1274 return err;
1277 if (cda[CTA_EXPECT_MASTER-1])
1278 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1279 else
1280 return -EINVAL;
1282 if (err < 0)
1283 return err;
1285 exp = nf_conntrack_expect_find_get(&tuple);
1286 if (!exp)
1287 return -ENOENT;
1289 if (cda[CTA_EXPECT_ID-1]) {
1290 __be32 id = *(__be32 *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1291 if (exp->id != ntohl(id)) {
1292 nf_conntrack_expect_put(exp);
1293 return -ENOENT;
1297 err = -ENOMEM;
1298 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1299 if (!skb2)
1300 goto out;
1302 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1303 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1304 1, exp);
1305 if (err <= 0)
1306 goto free;
1308 nf_conntrack_expect_put(exp);
1310 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1312 free:
1313 kfree_skb(skb2);
1314 out:
1315 nf_conntrack_expect_put(exp);
1316 return err;
1319 static int
1320 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1321 struct nlmsghdr *nlh, struct nfattr *cda[])
1323 struct nf_conntrack_expect *exp, *tmp;
1324 struct nf_conntrack_tuple tuple;
1325 struct nf_conntrack_helper *h;
1326 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1327 u_int8_t u3 = nfmsg->nfgen_family;
1328 int err;
1330 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1331 return -EINVAL;
1333 if (cda[CTA_EXPECT_TUPLE-1]) {
1334 /* delete a single expect by tuple */
1335 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1336 if (err < 0)
1337 return err;
1339 /* bump usage count to 2 */
1340 exp = nf_conntrack_expect_find_get(&tuple);
1341 if (!exp)
1342 return -ENOENT;
1344 if (cda[CTA_EXPECT_ID-1]) {
1345 __be32 id = *(__be32 *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1346 if (exp->id != ntohl(id)) {
1347 nf_conntrack_expect_put(exp);
1348 return -ENOENT;
1352 /* after list removal, usage count == 1 */
1353 nf_conntrack_unexpect_related(exp);
1354 /* have to put what we 'get' above.
1355 * after this line usage count == 0 */
1356 nf_conntrack_expect_put(exp);
1357 } else if (cda[CTA_EXPECT_HELP_NAME-1]) {
1358 char *name = NFA_DATA(cda[CTA_EXPECT_HELP_NAME-1]);
1360 /* delete all expectations for this helper */
1361 write_lock_bh(&nf_conntrack_lock);
1362 h = __nf_conntrack_helper_find_byname(name);
1363 if (!h) {
1364 write_unlock_bh(&nf_conntrack_lock);
1365 return -EINVAL;
1367 list_for_each_entry_safe(exp, tmp, &nf_conntrack_expect_list,
1368 list) {
1369 struct nf_conn_help *m_help = nfct_help(exp->master);
1370 if (m_help->helper == h
1371 && del_timer(&exp->timeout)) {
1372 nf_ct_unlink_expect(exp);
1373 nf_conntrack_expect_put(exp);
1376 write_unlock_bh(&nf_conntrack_lock);
1377 } else {
1378 /* This basically means we have to flush everything*/
1379 write_lock_bh(&nf_conntrack_lock);
1380 list_for_each_entry_safe(exp, tmp, &nf_conntrack_expect_list,
1381 list) {
1382 if (del_timer(&exp->timeout)) {
1383 nf_ct_unlink_expect(exp);
1384 nf_conntrack_expect_put(exp);
1387 write_unlock_bh(&nf_conntrack_lock);
1390 return 0;
1392 static int
1393 ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nfattr *cda[])
1395 return -EOPNOTSUPP;
1398 static int
1399 ctnetlink_create_expect(struct nfattr *cda[], u_int8_t u3)
1401 struct nf_conntrack_tuple tuple, mask, master_tuple;
1402 struct nf_conntrack_tuple_hash *h = NULL;
1403 struct nf_conntrack_expect *exp;
1404 struct nf_conn *ct;
1405 struct nf_conn_help *help;
1406 int err = 0;
1408 /* caller guarantees that those three CTA_EXPECT_* exist */
1409 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1410 if (err < 0)
1411 return err;
1412 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1413 if (err < 0)
1414 return err;
1415 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1416 if (err < 0)
1417 return err;
1419 /* Look for master conntrack of this expectation */
1420 h = nf_conntrack_find_get(&master_tuple, NULL);
1421 if (!h)
1422 return -ENOENT;
1423 ct = nf_ct_tuplehash_to_ctrack(h);
1424 help = nfct_help(ct);
1426 if (!help || !help->helper) {
1427 /* such conntrack hasn't got any helper, abort */
1428 err = -EINVAL;
1429 goto out;
1432 exp = nf_conntrack_expect_alloc(ct);
1433 if (!exp) {
1434 err = -ENOMEM;
1435 goto out;
1438 exp->expectfn = NULL;
1439 exp->flags = 0;
1440 exp->master = ct;
1441 exp->helper = NULL;
1442 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
1443 memcpy(&exp->mask, &mask, sizeof(struct nf_conntrack_tuple));
1445 err = nf_conntrack_expect_related(exp);
1446 nf_conntrack_expect_put(exp);
1448 out:
1449 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1450 return err;
1453 static int
1454 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1455 struct nlmsghdr *nlh, struct nfattr *cda[])
1457 struct nf_conntrack_tuple tuple;
1458 struct nf_conntrack_expect *exp;
1459 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1460 u_int8_t u3 = nfmsg->nfgen_family;
1461 int err = 0;
1463 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1464 return -EINVAL;
1466 if (!cda[CTA_EXPECT_TUPLE-1]
1467 || !cda[CTA_EXPECT_MASK-1]
1468 || !cda[CTA_EXPECT_MASTER-1])
1469 return -EINVAL;
1471 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1472 if (err < 0)
1473 return err;
1475 write_lock_bh(&nf_conntrack_lock);
1476 exp = __nf_conntrack_expect_find(&tuple);
1478 if (!exp) {
1479 write_unlock_bh(&nf_conntrack_lock);
1480 err = -ENOENT;
1481 if (nlh->nlmsg_flags & NLM_F_CREATE)
1482 err = ctnetlink_create_expect(cda, u3);
1483 return err;
1486 err = -EEXIST;
1487 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1488 err = ctnetlink_change_expect(exp, cda);
1489 write_unlock_bh(&nf_conntrack_lock);
1491 return err;
1494 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1495 static struct notifier_block ctnl_notifier = {
1496 .notifier_call = ctnetlink_conntrack_event,
1499 static struct notifier_block ctnl_notifier_exp = {
1500 .notifier_call = ctnetlink_expect_event,
1502 #endif
1504 static struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1505 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1506 .attr_count = CTA_MAX, },
1507 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1508 .attr_count = CTA_MAX, },
1509 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1510 .attr_count = CTA_MAX, },
1511 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1512 .attr_count = CTA_MAX, },
1515 static struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1516 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1517 .attr_count = CTA_EXPECT_MAX, },
1518 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1519 .attr_count = CTA_EXPECT_MAX, },
1520 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1521 .attr_count = CTA_EXPECT_MAX, },
1524 static struct nfnetlink_subsystem ctnl_subsys = {
1525 .name = "conntrack",
1526 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1527 .cb_count = IPCTNL_MSG_MAX,
1528 .cb = ctnl_cb,
1531 static struct nfnetlink_subsystem ctnl_exp_subsys = {
1532 .name = "conntrack_expect",
1533 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1534 .cb_count = IPCTNL_MSG_EXP_MAX,
1535 .cb = ctnl_exp_cb,
1538 MODULE_ALIAS("ip_conntrack_netlink");
1539 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
1540 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
1542 static int __init ctnetlink_init(void)
1544 int ret;
1546 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1547 ret = nfnetlink_subsys_register(&ctnl_subsys);
1548 if (ret < 0) {
1549 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1550 goto err_out;
1553 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1554 if (ret < 0) {
1555 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1556 goto err_unreg_subsys;
1559 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1560 ret = nf_conntrack_register_notifier(&ctnl_notifier);
1561 if (ret < 0) {
1562 printk("ctnetlink_init: cannot register notifier.\n");
1563 goto err_unreg_exp_subsys;
1566 ret = nf_conntrack_expect_register_notifier(&ctnl_notifier_exp);
1567 if (ret < 0) {
1568 printk("ctnetlink_init: cannot expect register notifier.\n");
1569 goto err_unreg_notifier;
1571 #endif
1573 return 0;
1575 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1576 err_unreg_notifier:
1577 nf_conntrack_unregister_notifier(&ctnl_notifier);
1578 err_unreg_exp_subsys:
1579 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1580 #endif
1581 err_unreg_subsys:
1582 nfnetlink_subsys_unregister(&ctnl_subsys);
1583 err_out:
1584 return ret;
1587 static void __exit ctnetlink_exit(void)
1589 printk("ctnetlink: unregistering from nfnetlink.\n");
1591 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1592 nf_conntrack_expect_unregister_notifier(&ctnl_notifier_exp);
1593 nf_conntrack_unregister_notifier(&ctnl_notifier);
1594 #endif
1596 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1597 nfnetlink_subsys_unregister(&ctnl_subsys);
1598 return;
1601 module_init(ctnetlink_init);
1602 module_exit(ctnetlink_exit);