ia64/linux-2.6.18-xen.hg

view net/netfilter/nf_conntrack_netlink.c @ 871:9cbcc9008446

xen/x86: don't initialize cpu_data[]'s apicid field on generic code

Afaict, this is not only redundant with the intialization done in
drivers/xen/core/smpboot.c, but actually results - at least for
secondary CPUs - in the Xen-specific value written to be later
overwritten with whatever the generic code determines (with no
guarantee that the two values are identical).

Signed-off-by: Jan Beulich <jbeulich@novell.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu May 14 10:09:15 2009 +0100 (2009-05-14)
parents 831230e53067
children
line source
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>
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>
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 */
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/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>
43 #include <linux/netfilter/nfnetlink.h>
44 #include <linux/netfilter/nfnetlink_conntrack.h>
46 MODULE_LICENSE("GPL");
48 static char __initdata version[] = "0.93";
50 #if 0
51 #define DEBUGP printk
52 #else
53 #define DEBUGP(format, args...)
54 #endif
57 static inline int
58 ctnetlink_dump_tuples_proto(struct sk_buff *skb,
59 const struct nf_conntrack_tuple *tuple,
60 struct nf_conntrack_protocol *proto)
61 {
62 int ret = 0;
63 struct nfattr *nest_parms = NFA_NEST(skb, CTA_TUPLE_PROTO);
65 NFA_PUT(skb, CTA_PROTO_NUM, sizeof(u_int8_t), &tuple->dst.protonum);
67 if (likely(proto->tuple_to_nfattr))
68 ret = proto->tuple_to_nfattr(skb, tuple);
70 NFA_NEST_END(skb, nest_parms);
72 return ret;
74 nfattr_failure:
75 return -1;
76 }
78 static inline int
79 ctnetlink_dump_tuples_ip(struct sk_buff *skb,
80 const struct nf_conntrack_tuple *tuple,
81 struct nf_conntrack_l3proto *l3proto)
82 {
83 int ret = 0;
84 struct nfattr *nest_parms = NFA_NEST(skb, CTA_TUPLE_IP);
86 if (likely(l3proto->tuple_to_nfattr))
87 ret = l3proto->tuple_to_nfattr(skb, tuple);
89 NFA_NEST_END(skb, nest_parms);
91 return ret;
93 nfattr_failure:
94 return -1;
95 }
97 static inline int
98 ctnetlink_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;
105 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
106 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
107 nf_ct_l3proto_put(l3proto);
109 if (unlikely(ret < 0))
110 return ret;
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);
116 return ret;
117 }
119 static inline int
120 ctnetlink_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;
126 nfattr_failure:
127 return -1;
128 }
130 static inline int
131 ctnetlink_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;
136 if (timeout_l < 0)
137 timeout = 0;
138 else
139 timeout = htonl(timeout_l / HZ);
141 NFA_PUT(skb, CTA_TIMEOUT, sizeof(timeout), &timeout);
142 return 0;
144 nfattr_failure:
145 return -1;
146 }
148 static inline int
149 ctnetlink_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;
155 if (!proto->to_nfattr) {
156 nf_ct_proto_put(proto);
157 return 0;
158 }
160 nest_proto = NFA_NEST(skb, CTA_PROTOINFO);
162 ret = proto->to_nfattr(skb, nest_proto, ct);
164 nf_ct_proto_put(proto);
166 NFA_NEST_END(skb, nest_proto);
168 return ret;
170 nfattr_failure:
171 return -1;
172 }
174 static inline int
175 ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
176 {
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;
195 }
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)
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;
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;
218 }
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)
226 {
227 u_int32_t mark = htonl(ct->mark);
229 NFA_PUT(skb, CTA_MARK, sizeof(u_int32_t), &mark);
230 return 0;
232 nfattr_failure:
233 return -1;
234 }
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)
241 {
242 u_int32_t id = htonl(ct->id);
243 NFA_PUT(skb, CTA_ID, sizeof(u_int32_t), &id);
244 return 0;
246 nfattr_failure:
247 return -1;
248 }
250 static inline int
251 ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
252 {
253 u_int32_t 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;
260 }
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)
268 {
269 struct nlmsghdr *nlh;
270 struct nfgenmsg *nfmsg;
271 struct nfattr *nest_parms;
272 unsigned char *b;
274 b = skb->tail;
276 event |= NFNL_SUBSYS_CTNETLINK << 8;
277 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
278 nfmsg = NLMSG_DATA(nlh);
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;
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);
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);
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;
307 nlh->nlmsg_len = skb->tail - b;
308 return skb->len;
310 nlmsg_failure:
311 nfattr_failure:
312 skb_trim(skb, b - skb->data);
313 return -1;
314 }
316 #ifdef CONFIG_NF_CONNTRACK_EVENTS
317 static 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;
329 /* ignore our fake conntrack entry */
330 if (ct == &nf_conntrack_untracked)
331 return NOTIFY_DONE;
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;
342 } else if (events & (IPCT_STATUS |
343 IPCT_PROTOINFO |
344 IPCT_HELPER |
345 IPCT_HELPINFO |
346 IPCT_NATINFO)) {
347 type = IPCTNL_MSG_CT_NEW;
348 group = NFNLGRP_CONNTRACK_UPDATE;
349 } else
350 return NOTIFY_DONE;
352 if (!nfnetlink_has_listeners(group))
353 return NOTIFY_DONE;
355 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
356 if (!skb)
357 return NOTIFY_DONE;
359 b = skb->tail;
361 type |= NFNL_SUBSYS_CTNETLINK << 8;
362 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
363 nfmsg = NLMSG_DATA(nlh);
365 nlh->nlmsg_flags = flags;
366 nfmsg->nfgen_family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
367 nfmsg->version = NFNETLINK_V0;
368 nfmsg->res_id = 0;
370 nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG);
371 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
372 goto nfattr_failure;
373 NFA_NEST_END(skb, nest_parms);
375 nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY);
376 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
377 goto nfattr_failure;
378 NFA_NEST_END(skb, nest_parms);
380 /* NAT stuff is now a status flag */
381 if ((events & IPCT_STATUS || events & IPCT_NATINFO)
382 && ctnetlink_dump_status(skb, ct) < 0)
383 goto nfattr_failure;
384 if (events & IPCT_REFRESH
385 && ctnetlink_dump_timeout(skb, ct) < 0)
386 goto nfattr_failure;
387 if (events & IPCT_PROTOINFO
388 && ctnetlink_dump_protoinfo(skb, ct) < 0)
389 goto nfattr_failure;
390 if (events & IPCT_HELPINFO
391 && ctnetlink_dump_helpinfo(skb, ct) < 0)
392 goto nfattr_failure;
394 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
395 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
396 goto nfattr_failure;
398 nlh->nlmsg_len = skb->tail - b;
399 nfnetlink_send(skb, 0, group, 0);
400 return NOTIFY_DONE;
402 nlmsg_failure:
403 nfattr_failure:
404 kfree_skb(skb);
405 return NOTIFY_DONE;
406 }
407 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
409 static int ctnetlink_done(struct netlink_callback *cb)
410 {
411 if (cb->args[1])
412 nf_ct_put((struct nf_conn *)cb->args[1]);
413 DEBUGP("entered %s\n", __FUNCTION__);
414 return 0;
415 }
417 #define L3PROTO(ct) ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num
419 static int
420 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
421 {
422 struct nf_conn *ct, *last;
423 struct nf_conntrack_tuple_hash *h;
424 struct list_head *i;
425 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
426 u_int8_t l3proto = nfmsg->nfgen_family;
428 DEBUGP("entered %s, last bucket=%lu id=%u\n", __FUNCTION__,
429 cb->args[0], *id);
431 read_lock_bh(&nf_conntrack_lock);
432 last = (struct nf_conn *)cb->args[1];
433 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
434 restart:
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);
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;
445 if (cb->args[1]) {
446 if (ct != last)
447 continue;
448 cb->args[1] = 0;
449 }
450 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
451 cb->nlh->nlmsg_seq,
452 IPCTNL_MSG_CT_NEW,
453 1, ct) < 0) {
454 nf_conntrack_get(&ct->ct_general);
455 cb->args[1] = (unsigned long)ct;
456 goto out;
457 }
458 }
459 if (cb->args[1]) {
460 cb->args[1] = 0;
461 goto restart;
462 }
463 }
464 out:
465 read_unlock_bh(&nf_conntrack_lock);
466 if (last)
467 nf_ct_put(last);
469 DEBUGP("leaving, last bucket=%lu id=%u\n", cb->args[0], *id);
470 return skb->len;
471 }
473 #ifdef CONFIG_NF_CT_ACCT
474 static int
475 ctnetlink_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];
481 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
482 u_int8_t l3proto = nfmsg->nfgen_family;
484 DEBUGP("entered %s, last bucket=%u id=%u\n", __FUNCTION__,
485 cb->args[0], *id);
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);
494 if (l3proto && L3PROTO(ct) != l3proto)
495 continue;
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;
505 memset(&ct->counters, 0, sizeof(ct->counters));
506 }
507 }
508 out:
509 write_unlock_bh(&nf_conntrack_lock);
511 DEBUGP("leaving, last bucket=%lu id=%u\n", cb->args[0], *id);
513 return skb->len;
514 }
515 #endif
517 static inline int
518 ctnetlink_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;
524 DEBUGP("entered %s\n", __FUNCTION__);
526 nfattr_parse_nested(tb, CTA_IP_MAX, attr);
528 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
530 if (likely(l3proto->nfattr_to_tuple))
531 ret = l3proto->nfattr_to_tuple(tb, tuple);
533 nf_ct_l3proto_put(l3proto);
535 DEBUGP("leaving\n");
537 return ret;
538 }
540 static const size_t cta_min_proto[CTA_PROTO_MAX] = {
541 [CTA_PROTO_NUM-1] = sizeof(u_int8_t),
542 };
544 static inline int
545 ctnetlink_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;
552 DEBUGP("entered %s\n", __FUNCTION__);
554 nfattr_parse_nested(tb, CTA_PROTO_MAX, attr);
556 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
557 return -EINVAL;
559 if (!tb[CTA_PROTO_NUM-1])
560 return -EINVAL;
561 tuple->dst.protonum = *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_NUM-1]);
563 proto = nf_ct_proto_find_get(tuple->src.l3num, tuple->dst.protonum);
565 if (likely(proto->nfattr_to_tuple))
566 ret = proto->nfattr_to_tuple(tb, tuple);
568 nf_ct_proto_put(proto);
570 return ret;
571 }
573 static inline int
574 ctnetlink_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;
580 DEBUGP("entered %s\n", __FUNCTION__);
582 memset(tuple, 0, sizeof(*tuple));
584 nfattr_parse_nested(tb, CTA_TUPLE_MAX, cda[type-1]);
586 if (!tb[CTA_TUPLE_IP-1])
587 return -EINVAL;
589 tuple->src.l3num = l3num;
591 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP-1], tuple);
592 if (err < 0)
593 return err;
595 if (!tb[CTA_TUPLE_PROTO-1])
596 return -EINVAL;
598 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO-1], tuple);
599 if (err < 0)
600 return err;
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;
608 NF_CT_DUMP_TUPLE(tuple);
610 DEBUGP("leaving\n");
612 return 0;
613 }
615 #ifdef CONFIG_IP_NF_NAT_NEEDED
616 static 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 };
621 static 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;
628 DEBUGP("entered %s\n", __FUNCTION__);
630 nfattr_parse_nested(tb, CTA_PROTONAT_MAX, attr);
632 if (nfattr_bad_size(tb, CTA_PROTONAT_MAX, cta_min_protonat))
633 return -EINVAL;
635 npt = ip_nat_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
637 if (!npt->nfattr_to_range) {
638 ip_nat_proto_put(npt);
639 return 0;
640 }
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;
646 ip_nat_proto_put(npt);
648 DEBUGP("leaving\n");
649 return 0;
650 }
652 static 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 };
657 static inline int
658 ctnetlink_parse_nat(struct nfattr *nat,
659 const struct nf_conn *ct, struct ip_nat_range *range)
660 {
661 struct nfattr *tb[CTA_NAT_MAX];
662 int err;
664 DEBUGP("entered %s\n", __FUNCTION__);
666 memset(range, 0, sizeof(*range));
668 nfattr_parse_nested(tb, CTA_NAT_MAX, nat);
670 if (nfattr_bad_size(tb, CTA_NAT_MAX, cta_min_nat))
671 return -EINVAL;
673 if (tb[CTA_NAT_MINIP-1])
674 range->min_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MINIP-1]);
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]);
681 if (range->min_ip)
682 range->flags |= IP_NAT_RANGE_MAP_IPS;
684 if (!tb[CTA_NAT_PROTO-1])
685 return 0;
687 err = ctnetlink_parse_nat_proto(tb[CTA_NAT_PROTO-1], ct, range);
688 if (err < 0)
689 return err;
691 DEBUGP("leaving\n");
692 return 0;
693 }
694 #endif
696 static inline int
697 ctnetlink_parse_help(struct nfattr *attr, char **helper_name)
698 {
699 struct nfattr *tb[CTA_HELP_MAX];
701 DEBUGP("entered %s\n", __FUNCTION__);
703 nfattr_parse_nested(tb, CTA_HELP_MAX, attr);
705 if (!tb[CTA_HELP_NAME-1])
706 return -EINVAL;
708 *helper_name = NFA_DATA(tb[CTA_HELP_NAME-1]);
710 return 0;
711 }
713 static 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 };
721 static int
722 ctnetlink_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;
732 DEBUGP("entered %s\n", __FUNCTION__);
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 /* Flush the whole table */
743 nf_conntrack_flush();
744 return 0;
745 }
747 if (err < 0)
748 return err;
750 h = nf_conntrack_find_get(&tuple, NULL);
751 if (!h) {
752 DEBUGP("tuple not found in conntrack hash\n");
753 return -ENOENT;
754 }
756 ct = nf_ct_tuplehash_to_ctrack(h);
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);
768 nf_ct_put(ct);
769 DEBUGP("leaving\n");
771 return 0;
772 }
774 static int
775 ctnetlink_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;
786 DEBUGP("entered %s\n", __FUNCTION__);
788 if (nlh->nlmsg_flags & NLM_F_DUMP) {
789 u32 rlen;
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 }
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 }
815 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
816 return -EINVAL;
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;
825 if (err < 0)
826 return err;
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);
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;
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;
850 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
851 if (err < 0)
852 goto out;
854 DEBUGP("leaving\n");
855 return 0;
857 free:
858 kfree_skb(skb2);
859 out:
860 return err;
861 }
863 static inline int
864 ctnetlink_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;
870 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
871 /* unchangeable */
872 return -EINVAL;
874 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
875 /* SEEN_REPLY bit can only be set */
876 return -EINVAL;
879 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
880 /* ASSURED bit can only be set */
881 return -EINVAL;
883 if (cda[CTA_NAT_SRC-1] || cda[CTA_NAT_DST-1]) {
884 #ifndef CONFIG_IP_NF_NAT_NEEDED
885 return -EINVAL;
886 #else
887 struct ip_nat_range range;
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 }
907 #endif
908 }
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 }
918 static inline int
919 ctnetlink_change_helper(struct nf_conn *ct, struct nfattr *cda[])
920 {
921 struct nf_conntrack_helper *helper;
922 struct nf_conn_help *help = nfct_help(ct);
923 char *helpname;
924 int err;
926 DEBUGP("entered %s\n", __FUNCTION__);
928 if (!help) {
929 /* FIXME: we need to reallocate and rehash */
930 return -EBUSY;
931 }
933 /* don't change helper of sibling connections */
934 if (ct->master)
935 return -EINVAL;
937 err = ctnetlink_parse_help(cda[CTA_HELP-1], &helpname);
938 if (err < 0)
939 return err;
941 helper = __nf_conntrack_helper_find_byname(helpname);
942 if (!helper) {
943 if (!strcmp(helpname, ""))
944 helper = NULL;
945 else
946 return -EINVAL;
947 }
949 if (help->helper) {
950 if (!helper) {
951 /* we had a helper before ... */
952 nf_ct_remove_expectations(ct);
953 help->helper = NULL;
954 } else {
955 /* need to zero data of old helper */
956 memset(&help->help, 0, sizeof(help->help));
957 }
958 }
960 help->helper = helper;
962 return 0;
963 }
965 static inline int
966 ctnetlink_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]));
970 if (!del_timer(&ct->timeout))
971 return -ETIME;
973 ct->timeout.expires = jiffies + timeout * HZ;
974 add_timer(&ct->timeout);
976 return 0;
977 }
979 static inline int
980 ctnetlink_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;
988 nfattr_parse_nested(tb, CTA_PROTOINFO_MAX, attr);
990 proto = nf_ct_proto_find_get(l3num, npt);
992 if (proto->from_nfattr)
993 err = proto->from_nfattr(tb, ct);
994 nf_ct_proto_put(proto);
996 return err;
997 }
999 static int
1000 ctnetlink_change_conntrack(struct nf_conn *ct, struct nfattr *cda[])
1002 int err;
1004 DEBUGP("entered %s\n", __FUNCTION__);
1006 if (cda[CTA_HELP-1]) {
1007 err = ctnetlink_change_helper(ct, cda);
1008 if (err < 0)
1009 return err;
1012 if (cda[CTA_TIMEOUT-1]) {
1013 err = ctnetlink_change_timeout(ct, cda);
1014 if (err < 0)
1015 return err;
1018 if (cda[CTA_STATUS-1]) {
1019 err = ctnetlink_change_status(ct, cda);
1020 if (err < 0)
1021 return err;
1024 if (cda[CTA_PROTOINFO-1]) {
1025 err = ctnetlink_change_protoinfo(ct, cda);
1026 if (err < 0)
1027 return err;
1030 #if defined(CONFIG_NF_CONNTRACK_MARK)
1031 if (cda[CTA_MARK-1])
1032 ct->mark = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_MARK-1]));
1033 #endif
1035 DEBUGP("all done\n");
1036 return 0;
1039 static int
1040 ctnetlink_create_conntrack(struct nfattr *cda[],
1041 struct nf_conntrack_tuple *otuple,
1042 struct nf_conntrack_tuple *rtuple)
1044 struct nf_conn *ct;
1045 int err = -EINVAL;
1047 DEBUGP("entered %s\n", __FUNCTION__);
1049 ct = nf_conntrack_alloc(otuple, rtuple);
1050 if (ct == NULL || IS_ERR(ct))
1051 return -ENOMEM;
1053 if (!cda[CTA_TIMEOUT-1])
1054 goto err;
1055 ct->timeout.expires = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
1057 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1058 ct->status |= IPS_CONFIRMED;
1060 err = ctnetlink_change_status(ct, cda);
1061 if (err < 0)
1062 goto err;
1064 if (cda[CTA_PROTOINFO-1]) {
1065 err = ctnetlink_change_protoinfo(ct, cda);
1066 if (err < 0)
1067 return err;
1070 #if defined(CONFIG_NF_CONNTRACK_MARK)
1071 if (cda[CTA_MARK-1])
1072 ct->mark = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_MARK-1]));
1073 #endif
1075 add_timer(&ct->timeout);
1076 nf_conntrack_hash_insert(ct);
1078 DEBUGP("conntrack with id %u inserted\n", ct->id);
1079 return 0;
1081 err:
1082 nf_conntrack_free(ct);
1083 return err;
1086 static int
1087 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1088 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
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;
1096 DEBUGP("entered %s\n", __FUNCTION__);
1098 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
1099 return -EINVAL;
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;
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;
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);
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;
1127 /* implicit 'else' */
1129 /* we only allow nat config for new conntracks */
1130 if (cda[CTA_NAT_SRC-1] || cda[CTA_NAT_DST-1]) {
1131 err = -EINVAL;
1132 goto out_unlock;
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);
1142 out_unlock:
1143 write_unlock_bh(&nf_conntrack_lock);
1144 return err;
1147 /***********************************************************************
1148 * EXPECT
1149 ***********************************************************************/
1151 static inline int
1152 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1153 const struct nf_conntrack_tuple *tuple,
1154 enum ctattr_expect type)
1156 struct nfattr *nest_parms = NFA_NEST(skb, type);
1158 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1159 goto nfattr_failure;
1161 NFA_NEST_END(skb, nest_parms);
1163 return 0;
1165 nfattr_failure:
1166 return -1;
1169 static inline int
1170 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1171 const struct nf_conntrack_tuple *tuple,
1172 const struct nf_conntrack_tuple *mask)
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);
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);
1183 if (unlikely(ret < 0))
1184 goto nfattr_failure;
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;
1192 NFA_NEST_END(skb, nest_parms);
1194 return 0;
1196 nfattr_failure:
1197 return -1;
1200 static inline int
1201 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1202 const struct nf_conntrack_expect *exp)
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);
1208 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1209 goto nfattr_failure;
1210 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
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;
1217 NFA_PUT(skb, CTA_EXPECT_TIMEOUT, sizeof(timeout), &timeout);
1218 NFA_PUT(skb, CTA_EXPECT_ID, sizeof(u_int32_t), &id);
1220 return 0;
1222 nfattr_failure:
1223 return -1;
1226 static int
1227 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1228 int event,
1229 int nowait,
1230 const struct nf_conntrack_expect *exp)
1232 struct nlmsghdr *nlh;
1233 struct nfgenmsg *nfmsg;
1234 unsigned char *b;
1236 b = skb->tail;
1238 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1239 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1240 nfmsg = NLMSG_DATA(nlh);
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;
1247 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1248 goto nfattr_failure;
1250 nlh->nlmsg_len = skb->tail - b;
1251 return skb->len;
1253 nlmsg_failure:
1254 nfattr_failure:
1255 skb_trim(skb, b - skb->data);
1256 return -1;
1259 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1260 static int ctnetlink_expect_event(struct notifier_block *this,
1261 unsigned long events, void *ptr)
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;
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;
1277 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1278 if (!skb)
1279 return NOTIFY_DONE;
1281 b = skb->tail;
1283 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1284 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1285 nfmsg = NLMSG_DATA(nlh);
1287 nlh->nlmsg_flags = flags;
1288 nfmsg->nfgen_family = exp->tuple.src.l3num;
1289 nfmsg->version = NFNETLINK_V0;
1290 nfmsg->res_id = 0;
1292 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1293 goto nfattr_failure;
1295 nlh->nlmsg_len = skb->tail - b;
1296 nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
1297 return NOTIFY_DONE;
1299 nlmsg_failure:
1300 nfattr_failure:
1301 kfree_skb(skb);
1302 return NOTIFY_DONE;
1304 #endif
1306 static int
1307 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1309 struct nf_conntrack_expect *exp = NULL;
1310 struct list_head *i;
1311 u_int32_t *id = (u_int32_t *) &cb->args[0];
1312 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
1313 u_int8_t l3proto = nfmsg->nfgen_family;
1315 DEBUGP("entered %s, last id=%llu\n", __FUNCTION__, *id);
1317 read_lock_bh(&nf_conntrack_lock);
1318 list_for_each_prev(i, &nf_conntrack_expect_list) {
1319 exp = (struct nf_conntrack_expect *) i;
1320 if (l3proto && exp->tuple.src.l3num != l3proto)
1321 continue;
1322 if (exp->id <= *id)
1323 continue;
1324 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1325 cb->nlh->nlmsg_seq,
1326 IPCTNL_MSG_EXP_NEW,
1327 1, exp) < 0)
1328 goto out;
1329 *id = exp->id;
1331 out:
1332 read_unlock_bh(&nf_conntrack_lock);
1334 DEBUGP("leaving, last id=%llu\n", *id);
1336 return skb->len;
1339 static const size_t cta_min_exp[CTA_EXPECT_MAX] = {
1340 [CTA_EXPECT_TIMEOUT-1] = sizeof(u_int32_t),
1341 [CTA_EXPECT_ID-1] = sizeof(u_int32_t)
1342 };
1344 static int
1345 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1346 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1348 struct nf_conntrack_tuple tuple;
1349 struct nf_conntrack_expect *exp;
1350 struct sk_buff *skb2;
1351 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1352 u_int8_t u3 = nfmsg->nfgen_family;
1353 int err = 0;
1355 DEBUGP("entered %s\n", __FUNCTION__);
1357 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1358 return -EINVAL;
1360 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1361 u32 rlen;
1363 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
1364 ctnetlink_exp_dump_table,
1365 ctnetlink_done)) != 0)
1366 return -EINVAL;
1367 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1368 if (rlen > skb->len)
1369 rlen = skb->len;
1370 skb_pull(skb, rlen);
1371 return 0;
1374 if (cda[CTA_EXPECT_MASTER-1])
1375 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1376 else
1377 return -EINVAL;
1379 if (err < 0)
1380 return err;
1382 exp = nf_conntrack_expect_find(&tuple);
1383 if (!exp)
1384 return -ENOENT;
1386 if (cda[CTA_EXPECT_ID-1]) {
1387 u_int32_t id = *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1388 if (exp->id != ntohl(id)) {
1389 nf_conntrack_expect_put(exp);
1390 return -ENOENT;
1394 err = -ENOMEM;
1395 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1396 if (!skb2)
1397 goto out;
1398 NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
1400 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1401 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1402 1, exp);
1403 if (err <= 0)
1404 goto free;
1406 nf_conntrack_expect_put(exp);
1408 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1410 free:
1411 kfree_skb(skb2);
1412 out:
1413 nf_conntrack_expect_put(exp);
1414 return err;
1417 static int
1418 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1419 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1421 struct nf_conntrack_expect *exp, *tmp;
1422 struct nf_conntrack_tuple tuple;
1423 struct nf_conntrack_helper *h;
1424 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1425 u_int8_t u3 = nfmsg->nfgen_family;
1426 int err;
1428 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1429 return -EINVAL;
1431 if (cda[CTA_EXPECT_TUPLE-1]) {
1432 /* delete a single expect by tuple */
1433 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1434 if (err < 0)
1435 return err;
1437 /* bump usage count to 2 */
1438 exp = nf_conntrack_expect_find(&tuple);
1439 if (!exp)
1440 return -ENOENT;
1442 if (cda[CTA_EXPECT_ID-1]) {
1443 u_int32_t id =
1444 *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1445 if (exp->id != ntohl(id)) {
1446 nf_conntrack_expect_put(exp);
1447 return -ENOENT;
1451 /* after list removal, usage count == 1 */
1452 nf_conntrack_unexpect_related(exp);
1453 /* have to put what we 'get' above.
1454 * after this line usage count == 0 */
1455 nf_conntrack_expect_put(exp);
1456 } else if (cda[CTA_EXPECT_HELP_NAME-1]) {
1457 char *name = NFA_DATA(cda[CTA_EXPECT_HELP_NAME-1]);
1459 /* delete all expectations for this helper */
1460 write_lock_bh(&nf_conntrack_lock);
1461 h = __nf_conntrack_helper_find_byname(name);
1462 if (!h) {
1463 write_unlock_bh(&nf_conntrack_lock);
1464 return -EINVAL;
1466 list_for_each_entry_safe(exp, tmp, &nf_conntrack_expect_list,
1467 list) {
1468 struct nf_conn_help *m_help = nfct_help(exp->master);
1469 if (m_help->helper == h
1470 && del_timer(&exp->timeout)) {
1471 nf_ct_unlink_expect(exp);
1472 nf_conntrack_expect_put(exp);
1475 write_unlock_bh(&nf_conntrack_lock);
1476 } else {
1477 /* This basically means we have to flush everything*/
1478 write_lock_bh(&nf_conntrack_lock);
1479 list_for_each_entry_safe(exp, tmp, &nf_conntrack_expect_list,
1480 list) {
1481 if (del_timer(&exp->timeout)) {
1482 nf_ct_unlink_expect(exp);
1483 nf_conntrack_expect_put(exp);
1486 write_unlock_bh(&nf_conntrack_lock);
1489 return 0;
1491 static int
1492 ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nfattr *cda[])
1494 return -EOPNOTSUPP;
1497 static int
1498 ctnetlink_create_expect(struct nfattr *cda[], u_int8_t u3)
1500 struct nf_conntrack_tuple tuple, mask, master_tuple;
1501 struct nf_conntrack_tuple_hash *h = NULL;
1502 struct nf_conntrack_expect *exp;
1503 struct nf_conn *ct;
1504 struct nf_conn_help *help;
1505 int err = 0;
1507 DEBUGP("entered %s\n", __FUNCTION__);
1509 /* caller guarantees that those three CTA_EXPECT_* exist */
1510 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1511 if (err < 0)
1512 return err;
1513 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1514 if (err < 0)
1515 return err;
1516 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1517 if (err < 0)
1518 return err;
1520 /* Look for master conntrack of this expectation */
1521 h = nf_conntrack_find_get(&master_tuple, NULL);
1522 if (!h)
1523 return -ENOENT;
1524 ct = nf_ct_tuplehash_to_ctrack(h);
1525 help = nfct_help(ct);
1527 if (!help || !help->helper) {
1528 /* such conntrack hasn't got any helper, abort */
1529 err = -EINVAL;
1530 goto out;
1533 exp = nf_conntrack_expect_alloc(ct);
1534 if (!exp) {
1535 err = -ENOMEM;
1536 goto out;
1539 exp->expectfn = NULL;
1540 exp->flags = 0;
1541 exp->master = ct;
1542 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
1543 memcpy(&exp->mask, &mask, sizeof(struct nf_conntrack_tuple));
1545 err = nf_conntrack_expect_related(exp);
1546 nf_conntrack_expect_put(exp);
1548 out:
1549 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1550 return err;
1553 static int
1554 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1555 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1557 struct nf_conntrack_tuple tuple;
1558 struct nf_conntrack_expect *exp;
1559 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1560 u_int8_t u3 = nfmsg->nfgen_family;
1561 int err = 0;
1563 DEBUGP("entered %s\n", __FUNCTION__);
1565 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1566 return -EINVAL;
1568 if (!cda[CTA_EXPECT_TUPLE-1]
1569 || !cda[CTA_EXPECT_MASK-1]
1570 || !cda[CTA_EXPECT_MASTER-1])
1571 return -EINVAL;
1573 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1574 if (err < 0)
1575 return err;
1577 write_lock_bh(&nf_conntrack_lock);
1578 exp = __nf_conntrack_expect_find(&tuple);
1580 if (!exp) {
1581 write_unlock_bh(&nf_conntrack_lock);
1582 err = -ENOENT;
1583 if (nlh->nlmsg_flags & NLM_F_CREATE)
1584 err = ctnetlink_create_expect(cda, u3);
1585 return err;
1588 err = -EEXIST;
1589 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1590 err = ctnetlink_change_expect(exp, cda);
1591 write_unlock_bh(&nf_conntrack_lock);
1593 DEBUGP("leaving\n");
1595 return err;
1598 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1599 static struct notifier_block ctnl_notifier = {
1600 .notifier_call = ctnetlink_conntrack_event,
1601 };
1603 static struct notifier_block ctnl_notifier_exp = {
1604 .notifier_call = ctnetlink_expect_event,
1605 };
1606 #endif
1608 static struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1609 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1610 .attr_count = CTA_MAX, },
1611 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1612 .attr_count = CTA_MAX, },
1613 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1614 .attr_count = CTA_MAX, },
1615 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1616 .attr_count = CTA_MAX, },
1617 };
1619 static struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1620 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1621 .attr_count = CTA_EXPECT_MAX, },
1622 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1623 .attr_count = CTA_EXPECT_MAX, },
1624 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1625 .attr_count = CTA_EXPECT_MAX, },
1626 };
1628 static struct nfnetlink_subsystem ctnl_subsys = {
1629 .name = "conntrack",
1630 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1631 .cb_count = IPCTNL_MSG_MAX,
1632 .cb = ctnl_cb,
1633 };
1635 static struct nfnetlink_subsystem ctnl_exp_subsys = {
1636 .name = "conntrack_expect",
1637 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1638 .cb_count = IPCTNL_MSG_EXP_MAX,
1639 .cb = ctnl_exp_cb,
1640 };
1642 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
1643 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
1645 static int __init ctnetlink_init(void)
1647 int ret;
1649 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1650 ret = nfnetlink_subsys_register(&ctnl_subsys);
1651 if (ret < 0) {
1652 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1653 goto err_out;
1656 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1657 if (ret < 0) {
1658 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1659 goto err_unreg_subsys;
1662 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1663 ret = nf_conntrack_register_notifier(&ctnl_notifier);
1664 if (ret < 0) {
1665 printk("ctnetlink_init: cannot register notifier.\n");
1666 goto err_unreg_exp_subsys;
1669 ret = nf_conntrack_expect_register_notifier(&ctnl_notifier_exp);
1670 if (ret < 0) {
1671 printk("ctnetlink_init: cannot expect register notifier.\n");
1672 goto err_unreg_notifier;
1674 #endif
1676 return 0;
1678 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1679 err_unreg_notifier:
1680 nf_conntrack_unregister_notifier(&ctnl_notifier);
1681 err_unreg_exp_subsys:
1682 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1683 #endif
1684 err_unreg_subsys:
1685 nfnetlink_subsys_unregister(&ctnl_subsys);
1686 err_out:
1687 return ret;
1690 static void __exit ctnetlink_exit(void)
1692 printk("ctnetlink: unregistering from nfnetlink.\n");
1694 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1695 nf_conntrack_expect_unregister_notifier(&ctnl_notifier_exp);
1696 nf_conntrack_unregister_notifier(&ctnl_notifier);
1697 #endif
1699 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1700 nfnetlink_subsys_unregister(&ctnl_subsys);
1701 return;
1704 module_init(ctnetlink_init);
1705 module_exit(ctnetlink_exit);