ocfs2: Make the left masklogs compat.
[taoma-kernel.git] / net / netfilter / nf_conntrack_netlink.c
blobeead9db6f899ad71869013dec0b3e2de480361a1
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-2008 by Pablo Neira Ayuso <pablo@netfilter.org>
9 * Initial connection tracking via netlink development funded and
10 * generally made possible by Network Robots, Inc. (www.networkrobots.com)
12 * Further development of this code funded by Astaro AG (http://www.astaro.com)
14 * This software may be used and distributed according to the terms
15 * of the GNU General Public License, incorporated herein by reference.
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/rculist.h>
22 #include <linux/rculist_nulls.h>
23 #include <linux/types.h>
24 #include <linux/timer.h>
25 #include <linux/security.h>
26 #include <linux/skbuff.h>
27 #include <linux/errno.h>
28 #include <linux/netlink.h>
29 #include <linux/spinlock.h>
30 #include <linux/interrupt.h>
31 #include <linux/slab.h>
33 #include <linux/netfilter.h>
34 #include <net/netlink.h>
35 #include <net/sock.h>
36 #include <net/netfilter/nf_conntrack.h>
37 #include <net/netfilter/nf_conntrack_core.h>
38 #include <net/netfilter/nf_conntrack_expect.h>
39 #include <net/netfilter/nf_conntrack_helper.h>
40 #include <net/netfilter/nf_conntrack_l3proto.h>
41 #include <net/netfilter/nf_conntrack_l4proto.h>
42 #include <net/netfilter/nf_conntrack_tuple.h>
43 #include <net/netfilter/nf_conntrack_acct.h>
44 #include <net/netfilter/nf_conntrack_zones.h>
45 #ifdef CONFIG_NF_NAT_NEEDED
46 #include <net/netfilter/nf_nat_core.h>
47 #include <net/netfilter/nf_nat_protocol.h>
48 #endif
50 #include <linux/netfilter/nfnetlink.h>
51 #include <linux/netfilter/nfnetlink_conntrack.h>
53 MODULE_LICENSE("GPL");
55 static char __initdata version[] = "0.93";
57 static inline int
58 ctnetlink_dump_tuples_proto(struct sk_buff *skb,
59 const struct nf_conntrack_tuple *tuple,
60 struct nf_conntrack_l4proto *l4proto)
62 int ret = 0;
63 struct nlattr *nest_parms;
65 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
66 if (!nest_parms)
67 goto nla_put_failure;
68 NLA_PUT_U8(skb, CTA_PROTO_NUM, tuple->dst.protonum);
70 if (likely(l4proto->tuple_to_nlattr))
71 ret = l4proto->tuple_to_nlattr(skb, tuple);
73 nla_nest_end(skb, nest_parms);
75 return ret;
77 nla_put_failure:
78 return -1;
81 static inline int
82 ctnetlink_dump_tuples_ip(struct sk_buff *skb,
83 const struct nf_conntrack_tuple *tuple,
84 struct nf_conntrack_l3proto *l3proto)
86 int ret = 0;
87 struct nlattr *nest_parms;
89 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
90 if (!nest_parms)
91 goto nla_put_failure;
93 if (likely(l3proto->tuple_to_nlattr))
94 ret = l3proto->tuple_to_nlattr(skb, tuple);
96 nla_nest_end(skb, nest_parms);
98 return ret;
100 nla_put_failure:
101 return -1;
104 static int
105 ctnetlink_dump_tuples(struct sk_buff *skb,
106 const struct nf_conntrack_tuple *tuple)
108 int ret;
109 struct nf_conntrack_l3proto *l3proto;
110 struct nf_conntrack_l4proto *l4proto;
112 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
113 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
115 if (unlikely(ret < 0))
116 return ret;
118 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
119 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
121 return ret;
124 static inline int
125 ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
127 NLA_PUT_BE32(skb, CTA_STATUS, htonl(ct->status));
128 return 0;
130 nla_put_failure:
131 return -1;
134 static inline int
135 ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
137 long timeout = (ct->timeout.expires - jiffies) / HZ;
139 if (timeout < 0)
140 timeout = 0;
142 NLA_PUT_BE32(skb, CTA_TIMEOUT, htonl(timeout));
143 return 0;
145 nla_put_failure:
146 return -1;
149 static inline int
150 ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
152 struct nf_conntrack_l4proto *l4proto;
153 struct nlattr *nest_proto;
154 int ret;
156 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
157 if (!l4proto->to_nlattr)
158 return 0;
160 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
161 if (!nest_proto)
162 goto nla_put_failure;
164 ret = l4proto->to_nlattr(skb, nest_proto, ct);
166 nla_nest_end(skb, nest_proto);
168 return ret;
170 nla_put_failure:
171 return -1;
174 static inline int
175 ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
177 struct nlattr *nest_helper;
178 const struct nf_conn_help *help = nfct_help(ct);
179 struct nf_conntrack_helper *helper;
181 if (!help)
182 return 0;
184 helper = rcu_dereference(help->helper);
185 if (!helper)
186 goto out;
188 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
189 if (!nest_helper)
190 goto nla_put_failure;
191 NLA_PUT_STRING(skb, CTA_HELP_NAME, helper->name);
193 if (helper->to_nlattr)
194 helper->to_nlattr(skb, ct);
196 nla_nest_end(skb, nest_helper);
197 out:
198 return 0;
200 nla_put_failure:
201 return -1;
204 static int
205 ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct,
206 enum ip_conntrack_dir dir)
208 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
209 struct nlattr *nest_count;
210 const struct nf_conn_counter *acct;
212 acct = nf_conn_acct_find(ct);
213 if (!acct)
214 return 0;
216 nest_count = nla_nest_start(skb, type | NLA_F_NESTED);
217 if (!nest_count)
218 goto nla_put_failure;
220 NLA_PUT_BE64(skb, CTA_COUNTERS_PACKETS,
221 cpu_to_be64(acct[dir].packets));
222 NLA_PUT_BE64(skb, CTA_COUNTERS_BYTES,
223 cpu_to_be64(acct[dir].bytes));
225 nla_nest_end(skb, nest_count);
227 return 0;
229 nla_put_failure:
230 return -1;
233 #ifdef CONFIG_NF_CONNTRACK_MARK
234 static inline int
235 ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
237 NLA_PUT_BE32(skb, CTA_MARK, htonl(ct->mark));
238 return 0;
240 nla_put_failure:
241 return -1;
243 #else
244 #define ctnetlink_dump_mark(a, b) (0)
245 #endif
247 #ifdef CONFIG_NF_CONNTRACK_SECMARK
248 static inline int
249 ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
251 struct nlattr *nest_secctx;
252 int len, ret;
253 char *secctx;
255 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
256 if (ret)
257 return 0;
259 ret = -1;
260 nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED);
261 if (!nest_secctx)
262 goto nla_put_failure;
264 NLA_PUT_STRING(skb, CTA_SECCTX_NAME, secctx);
265 nla_nest_end(skb, nest_secctx);
267 ret = 0;
268 nla_put_failure:
269 security_release_secctx(secctx, len);
270 return ret;
272 #else
273 #define ctnetlink_dump_secctx(a, b) (0)
274 #endif
276 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
278 static inline int
279 ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
281 struct nlattr *nest_parms;
283 if (!(ct->status & IPS_EXPECTED))
284 return 0;
286 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
287 if (!nest_parms)
288 goto nla_put_failure;
289 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
290 goto nla_put_failure;
291 nla_nest_end(skb, nest_parms);
293 return 0;
295 nla_put_failure:
296 return -1;
299 #ifdef CONFIG_NF_NAT_NEEDED
300 static int
301 dump_nat_seq_adj(struct sk_buff *skb, const struct nf_nat_seq *natseq, int type)
303 struct nlattr *nest_parms;
305 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
306 if (!nest_parms)
307 goto nla_put_failure;
309 NLA_PUT_BE32(skb, CTA_NAT_SEQ_CORRECTION_POS,
310 htonl(natseq->correction_pos));
311 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_BEFORE,
312 htonl(natseq->offset_before));
313 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_AFTER,
314 htonl(natseq->offset_after));
316 nla_nest_end(skb, nest_parms);
318 return 0;
320 nla_put_failure:
321 return -1;
324 static inline int
325 ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct)
327 struct nf_nat_seq *natseq;
328 struct nf_conn_nat *nat = nfct_nat(ct);
330 if (!(ct->status & IPS_SEQ_ADJUST) || !nat)
331 return 0;
333 natseq = &nat->seq[IP_CT_DIR_ORIGINAL];
334 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1)
335 return -1;
337 natseq = &nat->seq[IP_CT_DIR_REPLY];
338 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1)
339 return -1;
341 return 0;
343 #else
344 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
345 #endif
347 static inline int
348 ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
350 NLA_PUT_BE32(skb, CTA_ID, htonl((unsigned long)ct));
351 return 0;
353 nla_put_failure:
354 return -1;
357 static inline int
358 ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
360 NLA_PUT_BE32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use)));
361 return 0;
363 nla_put_failure:
364 return -1;
367 static int
368 ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
369 int event, struct nf_conn *ct)
371 struct nlmsghdr *nlh;
372 struct nfgenmsg *nfmsg;
373 struct nlattr *nest_parms;
374 unsigned int flags = pid ? NLM_F_MULTI : 0;
376 event |= NFNL_SUBSYS_CTNETLINK << 8;
377 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
378 if (nlh == NULL)
379 goto nlmsg_failure;
381 nfmsg = nlmsg_data(nlh);
382 nfmsg->nfgen_family = nf_ct_l3num(ct);
383 nfmsg->version = NFNETLINK_V0;
384 nfmsg->res_id = 0;
386 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
387 if (!nest_parms)
388 goto nla_put_failure;
389 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
390 goto nla_put_failure;
391 nla_nest_end(skb, nest_parms);
393 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
394 if (!nest_parms)
395 goto nla_put_failure;
396 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
397 goto nla_put_failure;
398 nla_nest_end(skb, nest_parms);
400 if (nf_ct_zone(ct))
401 NLA_PUT_BE16(skb, CTA_ZONE, htons(nf_ct_zone(ct)));
403 if (ctnetlink_dump_status(skb, ct) < 0 ||
404 ctnetlink_dump_timeout(skb, ct) < 0 ||
405 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
406 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
407 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
408 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
409 ctnetlink_dump_mark(skb, ct) < 0 ||
410 ctnetlink_dump_secctx(skb, ct) < 0 ||
411 ctnetlink_dump_id(skb, ct) < 0 ||
412 ctnetlink_dump_use(skb, ct) < 0 ||
413 ctnetlink_dump_master(skb, ct) < 0 ||
414 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
415 goto nla_put_failure;
417 nlmsg_end(skb, nlh);
418 return skb->len;
420 nlmsg_failure:
421 nla_put_failure:
422 nlmsg_cancel(skb, nlh);
423 return -1;
426 #ifdef CONFIG_NF_CONNTRACK_EVENTS
427 static inline size_t
428 ctnetlink_proto_size(const struct nf_conn *ct)
430 struct nf_conntrack_l3proto *l3proto;
431 struct nf_conntrack_l4proto *l4proto;
432 size_t len = 0;
434 rcu_read_lock();
435 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
436 len += l3proto->nla_size;
438 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
439 len += l4proto->nla_size;
440 rcu_read_unlock();
442 return len;
445 static inline size_t
446 ctnetlink_counters_size(const struct nf_conn *ct)
448 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
449 return 0;
450 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
451 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
452 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
456 static inline int
457 ctnetlink_secctx_size(const struct nf_conn *ct)
459 #ifdef CONFIG_NF_CONNTRACK_SECMARK
460 int len, ret;
462 ret = security_secid_to_secctx(ct->secmark, NULL, &len);
463 if (ret)
464 return 0;
466 return nla_total_size(0) /* CTA_SECCTX */
467 + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
468 #else
469 return 0;
470 #endif
473 static inline size_t
474 ctnetlink_nlmsg_size(const struct nf_conn *ct)
476 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
477 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
478 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
479 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
480 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
481 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
482 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
483 + ctnetlink_counters_size(ct)
484 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
485 + nla_total_size(0) /* CTA_PROTOINFO */
486 + nla_total_size(0) /* CTA_HELP */
487 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
488 + ctnetlink_secctx_size(ct)
489 #ifdef CONFIG_NF_NAT_NEEDED
490 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
491 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
492 #endif
493 #ifdef CONFIG_NF_CONNTRACK_MARK
494 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
495 #endif
496 + ctnetlink_proto_size(ct)
500 static int
501 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
503 struct net *net;
504 struct nlmsghdr *nlh;
505 struct nfgenmsg *nfmsg;
506 struct nlattr *nest_parms;
507 struct nf_conn *ct = item->ct;
508 struct sk_buff *skb;
509 unsigned int type;
510 unsigned int flags = 0, group;
511 int err;
513 /* ignore our fake conntrack entry */
514 if (nf_ct_is_untracked(ct))
515 return 0;
517 if (events & (1 << IPCT_DESTROY)) {
518 type = IPCTNL_MSG_CT_DELETE;
519 group = NFNLGRP_CONNTRACK_DESTROY;
520 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
521 type = IPCTNL_MSG_CT_NEW;
522 flags = NLM_F_CREATE|NLM_F_EXCL;
523 group = NFNLGRP_CONNTRACK_NEW;
524 } else if (events) {
525 type = IPCTNL_MSG_CT_NEW;
526 group = NFNLGRP_CONNTRACK_UPDATE;
527 } else
528 return 0;
530 net = nf_ct_net(ct);
531 if (!item->report && !nfnetlink_has_listeners(net, group))
532 return 0;
534 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
535 if (skb == NULL)
536 goto errout;
538 type |= NFNL_SUBSYS_CTNETLINK << 8;
539 nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
540 if (nlh == NULL)
541 goto nlmsg_failure;
543 nfmsg = nlmsg_data(nlh);
544 nfmsg->nfgen_family = nf_ct_l3num(ct);
545 nfmsg->version = NFNETLINK_V0;
546 nfmsg->res_id = 0;
548 rcu_read_lock();
549 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
550 if (!nest_parms)
551 goto nla_put_failure;
552 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
553 goto nla_put_failure;
554 nla_nest_end(skb, nest_parms);
556 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
557 if (!nest_parms)
558 goto nla_put_failure;
559 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
560 goto nla_put_failure;
561 nla_nest_end(skb, nest_parms);
563 if (nf_ct_zone(ct))
564 NLA_PUT_BE16(skb, CTA_ZONE, htons(nf_ct_zone(ct)));
566 if (ctnetlink_dump_id(skb, ct) < 0)
567 goto nla_put_failure;
569 if (ctnetlink_dump_status(skb, ct) < 0)
570 goto nla_put_failure;
572 if (events & (1 << IPCT_DESTROY)) {
573 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
574 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
575 goto nla_put_failure;
576 } else {
577 if (ctnetlink_dump_timeout(skb, ct) < 0)
578 goto nla_put_failure;
580 if (events & (1 << IPCT_PROTOINFO)
581 && ctnetlink_dump_protoinfo(skb, ct) < 0)
582 goto nla_put_failure;
584 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
585 && ctnetlink_dump_helpinfo(skb, ct) < 0)
586 goto nla_put_failure;
588 #ifdef CONFIG_NF_CONNTRACK_SECMARK
589 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
590 && ctnetlink_dump_secctx(skb, ct) < 0)
591 goto nla_put_failure;
592 #endif
594 if (events & (1 << IPCT_RELATED) &&
595 ctnetlink_dump_master(skb, ct) < 0)
596 goto nla_put_failure;
598 if (events & (1 << IPCT_NATSEQADJ) &&
599 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
600 goto nla_put_failure;
603 #ifdef CONFIG_NF_CONNTRACK_MARK
604 if ((events & (1 << IPCT_MARK) || ct->mark)
605 && ctnetlink_dump_mark(skb, ct) < 0)
606 goto nla_put_failure;
607 #endif
608 rcu_read_unlock();
610 nlmsg_end(skb, nlh);
611 err = nfnetlink_send(skb, net, item->pid, group, item->report,
612 GFP_ATOMIC);
613 if (err == -ENOBUFS || err == -EAGAIN)
614 return -ENOBUFS;
616 return 0;
618 nla_put_failure:
619 rcu_read_unlock();
620 nlmsg_cancel(skb, nlh);
621 nlmsg_failure:
622 kfree_skb(skb);
623 errout:
624 if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
625 return -ENOBUFS;
627 return 0;
629 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
631 static int ctnetlink_done(struct netlink_callback *cb)
633 if (cb->args[1])
634 nf_ct_put((struct nf_conn *)cb->args[1]);
635 return 0;
638 static int
639 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
641 struct net *net = sock_net(skb->sk);
642 struct nf_conn *ct, *last;
643 struct nf_conntrack_tuple_hash *h;
644 struct hlist_nulls_node *n;
645 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
646 u_int8_t l3proto = nfmsg->nfgen_family;
648 spin_lock_bh(&nf_conntrack_lock);
649 last = (struct nf_conn *)cb->args[1];
650 for (; cb->args[0] < net->ct.htable_size; cb->args[0]++) {
651 restart:
652 hlist_nulls_for_each_entry(h, n, &net->ct.hash[cb->args[0]],
653 hnnode) {
654 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
655 continue;
656 ct = nf_ct_tuplehash_to_ctrack(h);
657 /* Dump entries of a given L3 protocol number.
658 * If it is not specified, ie. l3proto == 0,
659 * then dump everything. */
660 if (l3proto && nf_ct_l3num(ct) != l3proto)
661 continue;
662 if (cb->args[1]) {
663 if (ct != last)
664 continue;
665 cb->args[1] = 0;
667 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
668 cb->nlh->nlmsg_seq,
669 IPCTNL_MSG_CT_NEW, ct) < 0) {
670 nf_conntrack_get(&ct->ct_general);
671 cb->args[1] = (unsigned long)ct;
672 goto out;
675 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) ==
676 IPCTNL_MSG_CT_GET_CTRZERO) {
677 struct nf_conn_counter *acct;
679 acct = nf_conn_acct_find(ct);
680 if (acct)
681 memset(acct, 0, sizeof(struct nf_conn_counter[IP_CT_DIR_MAX]));
684 if (cb->args[1]) {
685 cb->args[1] = 0;
686 goto restart;
689 out:
690 spin_unlock_bh(&nf_conntrack_lock);
691 if (last)
692 nf_ct_put(last);
694 return skb->len;
697 static inline int
698 ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
700 struct nlattr *tb[CTA_IP_MAX+1];
701 struct nf_conntrack_l3proto *l3proto;
702 int ret = 0;
704 nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
706 rcu_read_lock();
707 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
709 if (likely(l3proto->nlattr_to_tuple)) {
710 ret = nla_validate_nested(attr, CTA_IP_MAX,
711 l3proto->nla_policy);
712 if (ret == 0)
713 ret = l3proto->nlattr_to_tuple(tb, tuple);
716 rcu_read_unlock();
718 return ret;
721 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
722 [CTA_PROTO_NUM] = { .type = NLA_U8 },
725 static inline int
726 ctnetlink_parse_tuple_proto(struct nlattr *attr,
727 struct nf_conntrack_tuple *tuple)
729 struct nlattr *tb[CTA_PROTO_MAX+1];
730 struct nf_conntrack_l4proto *l4proto;
731 int ret = 0;
733 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
734 if (ret < 0)
735 return ret;
737 if (!tb[CTA_PROTO_NUM])
738 return -EINVAL;
739 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
741 rcu_read_lock();
742 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
744 if (likely(l4proto->nlattr_to_tuple)) {
745 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
746 l4proto->nla_policy);
747 if (ret == 0)
748 ret = l4proto->nlattr_to_tuple(tb, tuple);
751 rcu_read_unlock();
753 return ret;
756 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
757 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
758 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
761 static int
762 ctnetlink_parse_tuple(const struct nlattr * const cda[],
763 struct nf_conntrack_tuple *tuple,
764 enum ctattr_tuple type, u_int8_t l3num)
766 struct nlattr *tb[CTA_TUPLE_MAX+1];
767 int err;
769 memset(tuple, 0, sizeof(*tuple));
771 nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy);
773 if (!tb[CTA_TUPLE_IP])
774 return -EINVAL;
776 tuple->src.l3num = l3num;
778 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
779 if (err < 0)
780 return err;
782 if (!tb[CTA_TUPLE_PROTO])
783 return -EINVAL;
785 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
786 if (err < 0)
787 return err;
789 /* orig and expect tuples get DIR_ORIGINAL */
790 if (type == CTA_TUPLE_REPLY)
791 tuple->dst.dir = IP_CT_DIR_REPLY;
792 else
793 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
795 return 0;
798 static int
799 ctnetlink_parse_zone(const struct nlattr *attr, u16 *zone)
801 if (attr)
802 #ifdef CONFIG_NF_CONNTRACK_ZONES
803 *zone = ntohs(nla_get_be16(attr));
804 #else
805 return -EOPNOTSUPP;
806 #endif
807 else
808 *zone = 0;
810 return 0;
813 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
814 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING },
817 static inline int
818 ctnetlink_parse_help(const struct nlattr *attr, char **helper_name)
820 struct nlattr *tb[CTA_HELP_MAX+1];
822 nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy);
824 if (!tb[CTA_HELP_NAME])
825 return -EINVAL;
827 *helper_name = nla_data(tb[CTA_HELP_NAME]);
829 return 0;
832 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
833 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
834 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
835 [CTA_STATUS] = { .type = NLA_U32 },
836 [CTA_PROTOINFO] = { .type = NLA_NESTED },
837 [CTA_HELP] = { .type = NLA_NESTED },
838 [CTA_NAT_SRC] = { .type = NLA_NESTED },
839 [CTA_TIMEOUT] = { .type = NLA_U32 },
840 [CTA_MARK] = { .type = NLA_U32 },
841 [CTA_ID] = { .type = NLA_U32 },
842 [CTA_NAT_DST] = { .type = NLA_NESTED },
843 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
844 [CTA_ZONE] = { .type = NLA_U16 },
847 static int
848 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
849 const struct nlmsghdr *nlh,
850 const struct nlattr * const cda[])
852 struct net *net = sock_net(ctnl);
853 struct nf_conntrack_tuple_hash *h;
854 struct nf_conntrack_tuple tuple;
855 struct nf_conn *ct;
856 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
857 u_int8_t u3 = nfmsg->nfgen_family;
858 u16 zone;
859 int err;
861 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
862 if (err < 0)
863 return err;
865 if (cda[CTA_TUPLE_ORIG])
866 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
867 else if (cda[CTA_TUPLE_REPLY])
868 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
869 else {
870 /* Flush the whole table */
871 nf_conntrack_flush_report(net,
872 NETLINK_CB(skb).pid,
873 nlmsg_report(nlh));
874 return 0;
877 if (err < 0)
878 return err;
880 h = nf_conntrack_find_get(net, zone, &tuple);
881 if (!h)
882 return -ENOENT;
884 ct = nf_ct_tuplehash_to_ctrack(h);
886 if (cda[CTA_ID]) {
887 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
888 if (id != (u32)(unsigned long)ct) {
889 nf_ct_put(ct);
890 return -ENOENT;
894 if (nf_conntrack_event_report(IPCT_DESTROY, ct,
895 NETLINK_CB(skb).pid,
896 nlmsg_report(nlh)) < 0) {
897 nf_ct_delete_from_lists(ct);
898 /* we failed to report the event, try later */
899 nf_ct_insert_dying_list(ct);
900 nf_ct_put(ct);
901 return 0;
904 /* death_by_timeout would report the event again */
905 set_bit(IPS_DYING_BIT, &ct->status);
907 nf_ct_kill(ct);
908 nf_ct_put(ct);
910 return 0;
913 static int
914 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
915 const struct nlmsghdr *nlh,
916 const struct nlattr * const cda[])
918 struct net *net = sock_net(ctnl);
919 struct nf_conntrack_tuple_hash *h;
920 struct nf_conntrack_tuple tuple;
921 struct nf_conn *ct;
922 struct sk_buff *skb2 = NULL;
923 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
924 u_int8_t u3 = nfmsg->nfgen_family;
925 u16 zone;
926 int err;
928 if (nlh->nlmsg_flags & NLM_F_DUMP)
929 return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
930 ctnetlink_done);
932 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
933 if (err < 0)
934 return err;
936 if (cda[CTA_TUPLE_ORIG])
937 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
938 else if (cda[CTA_TUPLE_REPLY])
939 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
940 else
941 return -EINVAL;
943 if (err < 0)
944 return err;
946 h = nf_conntrack_find_get(net, zone, &tuple);
947 if (!h)
948 return -ENOENT;
950 ct = nf_ct_tuplehash_to_ctrack(h);
952 err = -ENOMEM;
953 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
954 if (skb2 == NULL) {
955 nf_ct_put(ct);
956 return -ENOMEM;
959 rcu_read_lock();
960 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
961 IPCTNL_MSG_CT_NEW, ct);
962 rcu_read_unlock();
963 nf_ct_put(ct);
964 if (err <= 0)
965 goto free;
967 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
968 if (err < 0)
969 goto out;
971 return 0;
973 free:
974 kfree_skb(skb2);
975 out:
976 /* this avoids a loop in nfnetlink. */
977 return err == -EAGAIN ? -ENOBUFS : err;
980 #ifdef CONFIG_NF_NAT_NEEDED
981 static int
982 ctnetlink_parse_nat_setup(struct nf_conn *ct,
983 enum nf_nat_manip_type manip,
984 const struct nlattr *attr)
986 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
988 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
989 if (!parse_nat_setup) {
990 #ifdef CONFIG_MODULES
991 rcu_read_unlock();
992 spin_unlock_bh(&nf_conntrack_lock);
993 nfnl_unlock();
994 if (request_module("nf-nat-ipv4") < 0) {
995 nfnl_lock();
996 spin_lock_bh(&nf_conntrack_lock);
997 rcu_read_lock();
998 return -EOPNOTSUPP;
1000 nfnl_lock();
1001 spin_lock_bh(&nf_conntrack_lock);
1002 rcu_read_lock();
1003 if (nfnetlink_parse_nat_setup_hook)
1004 return -EAGAIN;
1005 #endif
1006 return -EOPNOTSUPP;
1009 return parse_nat_setup(ct, manip, attr);
1011 #endif
1013 static int
1014 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1016 unsigned long d;
1017 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1018 d = ct->status ^ status;
1020 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1021 /* unchangeable */
1022 return -EBUSY;
1024 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1025 /* SEEN_REPLY bit can only be set */
1026 return -EBUSY;
1028 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1029 /* ASSURED bit can only be set */
1030 return -EBUSY;
1032 /* Be careful here, modifying NAT bits can screw up things,
1033 * so don't let users modify them directly if they don't pass
1034 * nf_nat_range. */
1035 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
1036 return 0;
1039 static int
1040 ctnetlink_change_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1042 #ifdef CONFIG_NF_NAT_NEEDED
1043 int ret;
1045 if (cda[CTA_NAT_DST]) {
1046 ret = ctnetlink_parse_nat_setup(ct,
1047 IP_NAT_MANIP_DST,
1048 cda[CTA_NAT_DST]);
1049 if (ret < 0)
1050 return ret;
1052 if (cda[CTA_NAT_SRC]) {
1053 ret = ctnetlink_parse_nat_setup(ct,
1054 IP_NAT_MANIP_SRC,
1055 cda[CTA_NAT_SRC]);
1056 if (ret < 0)
1057 return ret;
1059 return 0;
1060 #else
1061 return -EOPNOTSUPP;
1062 #endif
1065 static inline int
1066 ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[])
1068 struct nf_conntrack_helper *helper;
1069 struct nf_conn_help *help = nfct_help(ct);
1070 char *helpname = NULL;
1071 int err;
1073 /* don't change helper of sibling connections */
1074 if (ct->master)
1075 return -EBUSY;
1077 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
1078 if (err < 0)
1079 return err;
1081 if (!strcmp(helpname, "")) {
1082 if (help && help->helper) {
1083 /* we had a helper before ... */
1084 nf_ct_remove_expectations(ct);
1085 rcu_assign_pointer(help->helper, NULL);
1088 return 0;
1091 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1092 nf_ct_protonum(ct));
1093 if (helper == NULL) {
1094 #ifdef CONFIG_MODULES
1095 spin_unlock_bh(&nf_conntrack_lock);
1097 if (request_module("nfct-helper-%s", helpname) < 0) {
1098 spin_lock_bh(&nf_conntrack_lock);
1099 return -EOPNOTSUPP;
1102 spin_lock_bh(&nf_conntrack_lock);
1103 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1104 nf_ct_protonum(ct));
1105 if (helper)
1106 return -EAGAIN;
1107 #endif
1108 return -EOPNOTSUPP;
1111 if (help) {
1112 if (help->helper == helper)
1113 return 0;
1114 if (help->helper)
1115 return -EBUSY;
1116 /* need to zero data of old helper */
1117 memset(&help->help, 0, sizeof(help->help));
1118 } else {
1119 /* we cannot set a helper for an existing conntrack */
1120 return -EOPNOTSUPP;
1123 rcu_assign_pointer(help->helper, helper);
1125 return 0;
1128 static inline int
1129 ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[])
1131 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1133 if (!del_timer(&ct->timeout))
1134 return -ETIME;
1136 ct->timeout.expires = jiffies + timeout * HZ;
1137 add_timer(&ct->timeout);
1139 return 0;
1142 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1143 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1144 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1145 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1148 static inline int
1149 ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[])
1151 const struct nlattr *attr = cda[CTA_PROTOINFO];
1152 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1153 struct nf_conntrack_l4proto *l4proto;
1154 int err = 0;
1156 nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy);
1158 rcu_read_lock();
1159 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1160 if (l4proto->from_nlattr)
1161 err = l4proto->from_nlattr(tb, ct);
1162 rcu_read_unlock();
1164 return err;
1167 #ifdef CONFIG_NF_NAT_NEEDED
1168 static const struct nla_policy nat_seq_policy[CTA_NAT_SEQ_MAX+1] = {
1169 [CTA_NAT_SEQ_CORRECTION_POS] = { .type = NLA_U32 },
1170 [CTA_NAT_SEQ_OFFSET_BEFORE] = { .type = NLA_U32 },
1171 [CTA_NAT_SEQ_OFFSET_AFTER] = { .type = NLA_U32 },
1174 static inline int
1175 change_nat_seq_adj(struct nf_nat_seq *natseq, const struct nlattr * const attr)
1177 struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1179 nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, nat_seq_policy);
1181 if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1182 return -EINVAL;
1184 natseq->correction_pos =
1185 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
1187 if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1188 return -EINVAL;
1190 natseq->offset_before =
1191 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
1193 if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1194 return -EINVAL;
1196 natseq->offset_after =
1197 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
1199 return 0;
1202 static int
1203 ctnetlink_change_nat_seq_adj(struct nf_conn *ct,
1204 const struct nlattr * const cda[])
1206 int ret = 0;
1207 struct nf_conn_nat *nat = nfct_nat(ct);
1209 if (!nat)
1210 return 0;
1212 if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1213 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1214 cda[CTA_NAT_SEQ_ADJ_ORIG]);
1215 if (ret < 0)
1216 return ret;
1218 ct->status |= IPS_SEQ_ADJUST;
1221 if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1222 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1223 cda[CTA_NAT_SEQ_ADJ_REPLY]);
1224 if (ret < 0)
1225 return ret;
1227 ct->status |= IPS_SEQ_ADJUST;
1230 return 0;
1232 #endif
1234 static int
1235 ctnetlink_change_conntrack(struct nf_conn *ct,
1236 const struct nlattr * const cda[])
1238 int err;
1240 /* only allow NAT changes and master assignation for new conntracks */
1241 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1242 return -EOPNOTSUPP;
1244 if (cda[CTA_HELP]) {
1245 err = ctnetlink_change_helper(ct, cda);
1246 if (err < 0)
1247 return err;
1250 if (cda[CTA_TIMEOUT]) {
1251 err = ctnetlink_change_timeout(ct, cda);
1252 if (err < 0)
1253 return err;
1256 if (cda[CTA_STATUS]) {
1257 err = ctnetlink_change_status(ct, cda);
1258 if (err < 0)
1259 return err;
1262 if (cda[CTA_PROTOINFO]) {
1263 err = ctnetlink_change_protoinfo(ct, cda);
1264 if (err < 0)
1265 return err;
1268 #if defined(CONFIG_NF_CONNTRACK_MARK)
1269 if (cda[CTA_MARK])
1270 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1271 #endif
1273 #ifdef CONFIG_NF_NAT_NEEDED
1274 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1275 err = ctnetlink_change_nat_seq_adj(ct, cda);
1276 if (err < 0)
1277 return err;
1279 #endif
1281 return 0;
1284 static struct nf_conn *
1285 ctnetlink_create_conntrack(struct net *net, u16 zone,
1286 const struct nlattr * const cda[],
1287 struct nf_conntrack_tuple *otuple,
1288 struct nf_conntrack_tuple *rtuple,
1289 u8 u3)
1291 struct nf_conn *ct;
1292 int err = -EINVAL;
1293 struct nf_conntrack_helper *helper;
1295 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1296 if (IS_ERR(ct))
1297 return ERR_PTR(-ENOMEM);
1299 if (!cda[CTA_TIMEOUT])
1300 goto err1;
1301 ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1303 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1305 rcu_read_lock();
1306 if (cda[CTA_HELP]) {
1307 char *helpname = NULL;
1309 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
1310 if (err < 0)
1311 goto err2;
1313 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1314 nf_ct_protonum(ct));
1315 if (helper == NULL) {
1316 rcu_read_unlock();
1317 #ifdef CONFIG_MODULES
1318 if (request_module("nfct-helper-%s", helpname) < 0) {
1319 err = -EOPNOTSUPP;
1320 goto err1;
1323 rcu_read_lock();
1324 helper = __nf_conntrack_helper_find(helpname,
1325 nf_ct_l3num(ct),
1326 nf_ct_protonum(ct));
1327 if (helper) {
1328 err = -EAGAIN;
1329 goto err2;
1331 rcu_read_unlock();
1332 #endif
1333 err = -EOPNOTSUPP;
1334 goto err1;
1335 } else {
1336 struct nf_conn_help *help;
1338 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1339 if (help == NULL) {
1340 err = -ENOMEM;
1341 goto err2;
1344 /* not in hash table yet so not strictly necessary */
1345 rcu_assign_pointer(help->helper, helper);
1347 } else {
1348 /* try an implicit helper assignation */
1349 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1350 if (err < 0)
1351 goto err2;
1354 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1355 err = ctnetlink_change_nat(ct, cda);
1356 if (err < 0)
1357 goto err2;
1360 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1361 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1362 /* we must add conntrack extensions before confirmation. */
1363 ct->status |= IPS_CONFIRMED;
1365 if (cda[CTA_STATUS]) {
1366 err = ctnetlink_change_status(ct, cda);
1367 if (err < 0)
1368 goto err2;
1371 #ifdef CONFIG_NF_NAT_NEEDED
1372 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1373 err = ctnetlink_change_nat_seq_adj(ct, cda);
1374 if (err < 0)
1375 goto err2;
1377 #endif
1379 if (cda[CTA_PROTOINFO]) {
1380 err = ctnetlink_change_protoinfo(ct, cda);
1381 if (err < 0)
1382 goto err2;
1385 #if defined(CONFIG_NF_CONNTRACK_MARK)
1386 if (cda[CTA_MARK])
1387 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1388 #endif
1390 /* setup master conntrack: this is a confirmed expectation */
1391 if (cda[CTA_TUPLE_MASTER]) {
1392 struct nf_conntrack_tuple master;
1393 struct nf_conntrack_tuple_hash *master_h;
1394 struct nf_conn *master_ct;
1396 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3);
1397 if (err < 0)
1398 goto err2;
1400 master_h = nf_conntrack_find_get(net, zone, &master);
1401 if (master_h == NULL) {
1402 err = -ENOENT;
1403 goto err2;
1405 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1406 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1407 ct->master = master_ct;
1410 add_timer(&ct->timeout);
1411 nf_conntrack_hash_insert(ct);
1412 rcu_read_unlock();
1414 return ct;
1416 err2:
1417 rcu_read_unlock();
1418 err1:
1419 nf_conntrack_free(ct);
1420 return ERR_PTR(err);
1423 static int
1424 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1425 const struct nlmsghdr *nlh,
1426 const struct nlattr * const cda[])
1428 struct net *net = sock_net(ctnl);
1429 struct nf_conntrack_tuple otuple, rtuple;
1430 struct nf_conntrack_tuple_hash *h = NULL;
1431 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1432 u_int8_t u3 = nfmsg->nfgen_family;
1433 u16 zone;
1434 int err;
1436 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1437 if (err < 0)
1438 return err;
1440 if (cda[CTA_TUPLE_ORIG]) {
1441 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1442 if (err < 0)
1443 return err;
1446 if (cda[CTA_TUPLE_REPLY]) {
1447 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1448 if (err < 0)
1449 return err;
1452 spin_lock_bh(&nf_conntrack_lock);
1453 if (cda[CTA_TUPLE_ORIG])
1454 h = __nf_conntrack_find(net, zone, &otuple);
1455 else if (cda[CTA_TUPLE_REPLY])
1456 h = __nf_conntrack_find(net, zone, &rtuple);
1458 if (h == NULL) {
1459 err = -ENOENT;
1460 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1461 struct nf_conn *ct;
1462 enum ip_conntrack_events events;
1464 ct = ctnetlink_create_conntrack(net, zone, cda, &otuple,
1465 &rtuple, u3);
1466 if (IS_ERR(ct)) {
1467 err = PTR_ERR(ct);
1468 goto out_unlock;
1470 err = 0;
1471 nf_conntrack_get(&ct->ct_general);
1472 spin_unlock_bh(&nf_conntrack_lock);
1473 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1474 events = IPCT_RELATED;
1475 else
1476 events = IPCT_NEW;
1478 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1479 (1 << IPCT_ASSURED) |
1480 (1 << IPCT_HELPER) |
1481 (1 << IPCT_PROTOINFO) |
1482 (1 << IPCT_NATSEQADJ) |
1483 (1 << IPCT_MARK) | events,
1484 ct, NETLINK_CB(skb).pid,
1485 nlmsg_report(nlh));
1486 nf_ct_put(ct);
1487 } else
1488 spin_unlock_bh(&nf_conntrack_lock);
1490 return err;
1492 /* implicit 'else' */
1494 /* We manipulate the conntrack inside the global conntrack table lock,
1495 * so there's no need to increase the refcount */
1496 err = -EEXIST;
1497 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1498 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
1500 err = ctnetlink_change_conntrack(ct, cda);
1501 if (err == 0) {
1502 nf_conntrack_get(&ct->ct_general);
1503 spin_unlock_bh(&nf_conntrack_lock);
1504 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1505 (1 << IPCT_ASSURED) |
1506 (1 << IPCT_HELPER) |
1507 (1 << IPCT_PROTOINFO) |
1508 (1 << IPCT_NATSEQADJ) |
1509 (1 << IPCT_MARK),
1510 ct, NETLINK_CB(skb).pid,
1511 nlmsg_report(nlh));
1512 nf_ct_put(ct);
1513 } else
1514 spin_unlock_bh(&nf_conntrack_lock);
1516 return err;
1519 out_unlock:
1520 spin_unlock_bh(&nf_conntrack_lock);
1521 return err;
1524 /***********************************************************************
1525 * EXPECT
1526 ***********************************************************************/
1528 static inline int
1529 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1530 const struct nf_conntrack_tuple *tuple,
1531 enum ctattr_expect type)
1533 struct nlattr *nest_parms;
1535 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
1536 if (!nest_parms)
1537 goto nla_put_failure;
1538 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1539 goto nla_put_failure;
1540 nla_nest_end(skb, nest_parms);
1542 return 0;
1544 nla_put_failure:
1545 return -1;
1548 static inline int
1549 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1550 const struct nf_conntrack_tuple *tuple,
1551 const struct nf_conntrack_tuple_mask *mask)
1553 int ret;
1554 struct nf_conntrack_l3proto *l3proto;
1555 struct nf_conntrack_l4proto *l4proto;
1556 struct nf_conntrack_tuple m;
1557 struct nlattr *nest_parms;
1559 memset(&m, 0xFF, sizeof(m));
1560 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
1561 m.src.u.all = mask->src.u.all;
1562 m.dst.protonum = tuple->dst.protonum;
1564 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
1565 if (!nest_parms)
1566 goto nla_put_failure;
1568 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
1569 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
1571 if (unlikely(ret < 0))
1572 goto nla_put_failure;
1574 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
1575 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
1576 if (unlikely(ret < 0))
1577 goto nla_put_failure;
1579 nla_nest_end(skb, nest_parms);
1581 return 0;
1583 nla_put_failure:
1584 return -1;
1587 static int
1588 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1589 const struct nf_conntrack_expect *exp)
1591 struct nf_conn *master = exp->master;
1592 long timeout = (exp->timeout.expires - jiffies) / HZ;
1593 struct nf_conn_help *help;
1595 if (timeout < 0)
1596 timeout = 0;
1598 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1599 goto nla_put_failure;
1600 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1601 goto nla_put_failure;
1602 if (ctnetlink_exp_dump_tuple(skb,
1603 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1604 CTA_EXPECT_MASTER) < 0)
1605 goto nla_put_failure;
1607 NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout));
1608 NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp));
1609 NLA_PUT_BE32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags));
1610 help = nfct_help(master);
1611 if (help) {
1612 struct nf_conntrack_helper *helper;
1614 helper = rcu_dereference(help->helper);
1615 if (helper)
1616 NLA_PUT_STRING(skb, CTA_EXPECT_HELP_NAME, helper->name);
1619 return 0;
1621 nla_put_failure:
1622 return -1;
1625 static int
1626 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1627 int event, const struct nf_conntrack_expect *exp)
1629 struct nlmsghdr *nlh;
1630 struct nfgenmsg *nfmsg;
1631 unsigned int flags = pid ? NLM_F_MULTI : 0;
1633 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1634 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
1635 if (nlh == NULL)
1636 goto nlmsg_failure;
1638 nfmsg = nlmsg_data(nlh);
1639 nfmsg->nfgen_family = exp->tuple.src.l3num;
1640 nfmsg->version = NFNETLINK_V0;
1641 nfmsg->res_id = 0;
1643 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1644 goto nla_put_failure;
1646 nlmsg_end(skb, nlh);
1647 return skb->len;
1649 nlmsg_failure:
1650 nla_put_failure:
1651 nlmsg_cancel(skb, nlh);
1652 return -1;
1655 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1656 static int
1657 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
1659 struct nf_conntrack_expect *exp = item->exp;
1660 struct net *net = nf_ct_exp_net(exp);
1661 struct nlmsghdr *nlh;
1662 struct nfgenmsg *nfmsg;
1663 struct sk_buff *skb;
1664 unsigned int type, group;
1665 int flags = 0;
1667 if (events & (1 << IPEXP_DESTROY)) {
1668 type = IPCTNL_MSG_EXP_DELETE;
1669 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
1670 } else if (events & (1 << IPEXP_NEW)) {
1671 type = IPCTNL_MSG_EXP_NEW;
1672 flags = NLM_F_CREATE|NLM_F_EXCL;
1673 group = NFNLGRP_CONNTRACK_EXP_NEW;
1674 } else
1675 return 0;
1677 if (!item->report && !nfnetlink_has_listeners(net, group))
1678 return 0;
1680 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1681 if (skb == NULL)
1682 goto errout;
1684 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1685 nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
1686 if (nlh == NULL)
1687 goto nlmsg_failure;
1689 nfmsg = nlmsg_data(nlh);
1690 nfmsg->nfgen_family = exp->tuple.src.l3num;
1691 nfmsg->version = NFNETLINK_V0;
1692 nfmsg->res_id = 0;
1694 rcu_read_lock();
1695 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1696 goto nla_put_failure;
1697 rcu_read_unlock();
1699 nlmsg_end(skb, nlh);
1700 nfnetlink_send(skb, net, item->pid, group, item->report, GFP_ATOMIC);
1701 return 0;
1703 nla_put_failure:
1704 rcu_read_unlock();
1705 nlmsg_cancel(skb, nlh);
1706 nlmsg_failure:
1707 kfree_skb(skb);
1708 errout:
1709 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
1710 return 0;
1712 #endif
1713 static int ctnetlink_exp_done(struct netlink_callback *cb)
1715 if (cb->args[1])
1716 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
1717 return 0;
1720 static int
1721 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1723 struct net *net = sock_net(skb->sk);
1724 struct nf_conntrack_expect *exp, *last;
1725 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1726 struct hlist_node *n;
1727 u_int8_t l3proto = nfmsg->nfgen_family;
1729 rcu_read_lock();
1730 last = (struct nf_conntrack_expect *)cb->args[1];
1731 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
1732 restart:
1733 hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]],
1734 hnode) {
1735 if (l3proto && exp->tuple.src.l3num != l3proto)
1736 continue;
1737 if (cb->args[1]) {
1738 if (exp != last)
1739 continue;
1740 cb->args[1] = 0;
1742 if (ctnetlink_exp_fill_info(skb,
1743 NETLINK_CB(cb->skb).pid,
1744 cb->nlh->nlmsg_seq,
1745 IPCTNL_MSG_EXP_NEW,
1746 exp) < 0) {
1747 if (!atomic_inc_not_zero(&exp->use))
1748 continue;
1749 cb->args[1] = (unsigned long)exp;
1750 goto out;
1753 if (cb->args[1]) {
1754 cb->args[1] = 0;
1755 goto restart;
1758 out:
1759 rcu_read_unlock();
1760 if (last)
1761 nf_ct_expect_put(last);
1763 return skb->len;
1766 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
1767 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
1768 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
1769 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
1770 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
1771 [CTA_EXPECT_ID] = { .type = NLA_U32 },
1772 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING },
1773 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
1774 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
1777 static int
1778 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1779 const struct nlmsghdr *nlh,
1780 const struct nlattr * const cda[])
1782 struct net *net = sock_net(ctnl);
1783 struct nf_conntrack_tuple tuple;
1784 struct nf_conntrack_expect *exp;
1785 struct sk_buff *skb2;
1786 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1787 u_int8_t u3 = nfmsg->nfgen_family;
1788 u16 zone;
1789 int err;
1791 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1792 return netlink_dump_start(ctnl, skb, nlh,
1793 ctnetlink_exp_dump_table,
1794 ctnetlink_exp_done);
1797 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
1798 if (err < 0)
1799 return err;
1801 if (cda[CTA_EXPECT_MASTER])
1802 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1803 else
1804 return -EINVAL;
1806 if (err < 0)
1807 return err;
1809 exp = nf_ct_expect_find_get(net, zone, &tuple);
1810 if (!exp)
1811 return -ENOENT;
1813 if (cda[CTA_EXPECT_ID]) {
1814 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1815 if (ntohl(id) != (u32)(unsigned long)exp) {
1816 nf_ct_expect_put(exp);
1817 return -ENOENT;
1821 err = -ENOMEM;
1822 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1823 if (skb2 == NULL)
1824 goto out;
1826 rcu_read_lock();
1827 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1828 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
1829 rcu_read_unlock();
1830 if (err <= 0)
1831 goto free;
1833 nf_ct_expect_put(exp);
1835 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1837 free:
1838 kfree_skb(skb2);
1839 out:
1840 nf_ct_expect_put(exp);
1841 return err;
1844 static int
1845 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1846 const struct nlmsghdr *nlh,
1847 const struct nlattr * const cda[])
1849 struct net *net = sock_net(ctnl);
1850 struct nf_conntrack_expect *exp;
1851 struct nf_conntrack_tuple tuple;
1852 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1853 struct hlist_node *n, *next;
1854 u_int8_t u3 = nfmsg->nfgen_family;
1855 unsigned int i;
1856 u16 zone;
1857 int err;
1859 if (cda[CTA_EXPECT_TUPLE]) {
1860 /* delete a single expect by tuple */
1861 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
1862 if (err < 0)
1863 return err;
1865 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1866 if (err < 0)
1867 return err;
1869 /* bump usage count to 2 */
1870 exp = nf_ct_expect_find_get(net, zone, &tuple);
1871 if (!exp)
1872 return -ENOENT;
1874 if (cda[CTA_EXPECT_ID]) {
1875 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1876 if (ntohl(id) != (u32)(unsigned long)exp) {
1877 nf_ct_expect_put(exp);
1878 return -ENOENT;
1882 /* after list removal, usage count == 1 */
1883 spin_lock_bh(&nf_conntrack_lock);
1884 if (del_timer(&exp->timeout)) {
1885 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).pid,
1886 nlmsg_report(nlh));
1887 nf_ct_expect_put(exp);
1889 spin_unlock_bh(&nf_conntrack_lock);
1890 /* have to put what we 'get' above.
1891 * after this line usage count == 0 */
1892 nf_ct_expect_put(exp);
1893 } else if (cda[CTA_EXPECT_HELP_NAME]) {
1894 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
1895 struct nf_conn_help *m_help;
1897 /* delete all expectations for this helper */
1898 spin_lock_bh(&nf_conntrack_lock);
1899 for (i = 0; i < nf_ct_expect_hsize; i++) {
1900 hlist_for_each_entry_safe(exp, n, next,
1901 &net->ct.expect_hash[i],
1902 hnode) {
1903 m_help = nfct_help(exp->master);
1904 if (!strcmp(m_help->helper->name, name) &&
1905 del_timer(&exp->timeout)) {
1906 nf_ct_unlink_expect_report(exp,
1907 NETLINK_CB(skb).pid,
1908 nlmsg_report(nlh));
1909 nf_ct_expect_put(exp);
1913 spin_unlock_bh(&nf_conntrack_lock);
1914 } else {
1915 /* This basically means we have to flush everything*/
1916 spin_lock_bh(&nf_conntrack_lock);
1917 for (i = 0; i < nf_ct_expect_hsize; i++) {
1918 hlist_for_each_entry_safe(exp, n, next,
1919 &net->ct.expect_hash[i],
1920 hnode) {
1921 if (del_timer(&exp->timeout)) {
1922 nf_ct_unlink_expect_report(exp,
1923 NETLINK_CB(skb).pid,
1924 nlmsg_report(nlh));
1925 nf_ct_expect_put(exp);
1929 spin_unlock_bh(&nf_conntrack_lock);
1932 return 0;
1934 static int
1935 ctnetlink_change_expect(struct nf_conntrack_expect *x,
1936 const struct nlattr * const cda[])
1938 return -EOPNOTSUPP;
1941 static int
1942 ctnetlink_create_expect(struct net *net, u16 zone,
1943 const struct nlattr * const cda[],
1944 u_int8_t u3,
1945 u32 pid, int report)
1947 struct nf_conntrack_tuple tuple, mask, master_tuple;
1948 struct nf_conntrack_tuple_hash *h = NULL;
1949 struct nf_conntrack_expect *exp;
1950 struct nf_conn *ct;
1951 struct nf_conn_help *help;
1952 int err = 0;
1954 /* caller guarantees that those three CTA_EXPECT_* exist */
1955 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1956 if (err < 0)
1957 return err;
1958 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1959 if (err < 0)
1960 return err;
1961 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1962 if (err < 0)
1963 return err;
1965 /* Look for master conntrack of this expectation */
1966 h = nf_conntrack_find_get(net, zone, &master_tuple);
1967 if (!h)
1968 return -ENOENT;
1969 ct = nf_ct_tuplehash_to_ctrack(h);
1970 exp = nf_ct_expect_alloc(ct);
1971 if (!exp) {
1972 err = -ENOMEM;
1973 goto out;
1975 help = nfct_help(ct);
1976 if (!help) {
1977 if (!cda[CTA_EXPECT_TIMEOUT]) {
1978 err = -EINVAL;
1979 goto out;
1981 exp->timeout.expires =
1982 jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
1984 exp->flags = NF_CT_EXPECT_USERSPACE;
1985 if (cda[CTA_EXPECT_FLAGS]) {
1986 exp->flags |=
1987 ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
1989 } else {
1990 if (cda[CTA_EXPECT_FLAGS]) {
1991 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
1992 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
1993 } else
1994 exp->flags = 0;
1997 exp->class = 0;
1998 exp->expectfn = NULL;
1999 exp->master = ct;
2000 exp->helper = NULL;
2001 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
2002 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
2003 exp->mask.src.u.all = mask.src.u.all;
2005 err = nf_ct_expect_related_report(exp, pid, report);
2006 nf_ct_expect_put(exp);
2008 out:
2009 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
2010 return err;
2013 static int
2014 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
2015 const struct nlmsghdr *nlh,
2016 const struct nlattr * const cda[])
2018 struct net *net = sock_net(ctnl);
2019 struct nf_conntrack_tuple tuple;
2020 struct nf_conntrack_expect *exp;
2021 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2022 u_int8_t u3 = nfmsg->nfgen_family;
2023 u16 zone;
2024 int err;
2026 if (!cda[CTA_EXPECT_TUPLE]
2027 || !cda[CTA_EXPECT_MASK]
2028 || !cda[CTA_EXPECT_MASTER])
2029 return -EINVAL;
2031 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2032 if (err < 0)
2033 return err;
2035 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2036 if (err < 0)
2037 return err;
2039 spin_lock_bh(&nf_conntrack_lock);
2040 exp = __nf_ct_expect_find(net, zone, &tuple);
2042 if (!exp) {
2043 spin_unlock_bh(&nf_conntrack_lock);
2044 err = -ENOENT;
2045 if (nlh->nlmsg_flags & NLM_F_CREATE) {
2046 err = ctnetlink_create_expect(net, zone, cda,
2048 NETLINK_CB(skb).pid,
2049 nlmsg_report(nlh));
2051 return err;
2054 err = -EEXIST;
2055 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
2056 err = ctnetlink_change_expect(exp, cda);
2057 spin_unlock_bh(&nf_conntrack_lock);
2059 return err;
2062 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2063 static struct nf_ct_event_notifier ctnl_notifier = {
2064 .fcn = ctnetlink_conntrack_event,
2067 static struct nf_exp_event_notifier ctnl_notifier_exp = {
2068 .fcn = ctnetlink_expect_event,
2070 #endif
2072 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
2073 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
2074 .attr_count = CTA_MAX,
2075 .policy = ct_nla_policy },
2076 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
2077 .attr_count = CTA_MAX,
2078 .policy = ct_nla_policy },
2079 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
2080 .attr_count = CTA_MAX,
2081 .policy = ct_nla_policy },
2082 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
2083 .attr_count = CTA_MAX,
2084 .policy = ct_nla_policy },
2087 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
2088 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
2089 .attr_count = CTA_EXPECT_MAX,
2090 .policy = exp_nla_policy },
2091 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
2092 .attr_count = CTA_EXPECT_MAX,
2093 .policy = exp_nla_policy },
2094 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
2095 .attr_count = CTA_EXPECT_MAX,
2096 .policy = exp_nla_policy },
2099 static const struct nfnetlink_subsystem ctnl_subsys = {
2100 .name = "conntrack",
2101 .subsys_id = NFNL_SUBSYS_CTNETLINK,
2102 .cb_count = IPCTNL_MSG_MAX,
2103 .cb = ctnl_cb,
2106 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
2107 .name = "conntrack_expect",
2108 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
2109 .cb_count = IPCTNL_MSG_EXP_MAX,
2110 .cb = ctnl_exp_cb,
2113 MODULE_ALIAS("ip_conntrack_netlink");
2114 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
2115 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
2117 static int __init ctnetlink_init(void)
2119 int ret;
2121 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
2122 ret = nfnetlink_subsys_register(&ctnl_subsys);
2123 if (ret < 0) {
2124 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
2125 goto err_out;
2128 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
2129 if (ret < 0) {
2130 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
2131 goto err_unreg_subsys;
2134 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2135 ret = nf_conntrack_register_notifier(&ctnl_notifier);
2136 if (ret < 0) {
2137 pr_err("ctnetlink_init: cannot register notifier.\n");
2138 goto err_unreg_exp_subsys;
2141 ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp);
2142 if (ret < 0) {
2143 pr_err("ctnetlink_init: cannot expect register notifier.\n");
2144 goto err_unreg_notifier;
2146 #endif
2148 return 0;
2150 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2151 err_unreg_notifier:
2152 nf_conntrack_unregister_notifier(&ctnl_notifier);
2153 err_unreg_exp_subsys:
2154 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2155 #endif
2156 err_unreg_subsys:
2157 nfnetlink_subsys_unregister(&ctnl_subsys);
2158 err_out:
2159 return ret;
2162 static void __exit ctnetlink_exit(void)
2164 pr_info("ctnetlink: unregistering from nfnetlink.\n");
2166 nf_ct_remove_userspace_expectations();
2167 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2168 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp);
2169 nf_conntrack_unregister_notifier(&ctnl_notifier);
2170 #endif
2172 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2173 nfnetlink_subsys_unregister(&ctnl_subsys);
2176 module_init(ctnetlink_init);
2177 module_exit(ctnetlink_exit);