Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 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> |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 5 | * (C) 2002-2006 by Harald Welte <laforge@gnumonks.org> |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 6 | * (C) 2003 by Patrick Mchardy <kaber@trash.net> |
Pablo Neira Ayuso | 0adf9d6 | 2008-06-09 15:56:20 -0700 | [diff] [blame] | 7 | * (C) 2005-2008 by Pablo Neira Ayuso <pablo@netfilter.org> |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 8 | * |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 9 | * Initial connection tracking via netlink development funded and |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 10 | * generally made possible by Network Robots, Inc. (www.networkrobots.com) |
| 11 | * |
| 12 | * Further development of this code funded by Astaro AG (http://www.astaro.com) |
| 13 | * |
| 14 | * This software may be used and distributed according to the terms |
| 15 | * of the GNU General Public License, incorporated herein by reference. |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/kernel.h> |
Ingo Molnar | 711bbdd | 2008-05-17 08:26:25 +0200 | [diff] [blame] | 21 | #include <linux/rculist.h> |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 22 | #include <linux/rculist_nulls.h> |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 23 | #include <linux/types.h> |
| 24 | #include <linux/timer.h> |
| 25 | #include <linux/skbuff.h> |
| 26 | #include <linux/errno.h> |
| 27 | #include <linux/netlink.h> |
| 28 | #include <linux/spinlock.h> |
Yasuyuki Kozakai | 40a839f | 2006-06-27 03:00:35 -0700 | [diff] [blame] | 29 | #include <linux/interrupt.h> |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 30 | |
| 31 | #include <linux/netfilter.h> |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 32 | #include <net/netlink.h> |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 33 | #include <net/netfilter/nf_conntrack.h> |
| 34 | #include <net/netfilter/nf_conntrack_core.h> |
Martin Josefsson | 77ab9cf | 2006-11-29 02:34:58 +0100 | [diff] [blame] | 35 | #include <net/netfilter/nf_conntrack_expect.h> |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 36 | #include <net/netfilter/nf_conntrack_helper.h> |
| 37 | #include <net/netfilter/nf_conntrack_l3proto.h> |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 38 | #include <net/netfilter/nf_conntrack_l4proto.h> |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 39 | #include <net/netfilter/nf_conntrack_tuple.h> |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 40 | #include <net/netfilter/nf_conntrack_acct.h> |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 41 | #ifdef CONFIG_NF_NAT_NEEDED |
| 42 | #include <net/netfilter/nf_nat_core.h> |
| 43 | #include <net/netfilter/nf_nat_protocol.h> |
| 44 | #endif |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 45 | |
| 46 | #include <linux/netfilter/nfnetlink.h> |
| 47 | #include <linux/netfilter/nfnetlink_conntrack.h> |
| 48 | |
| 49 | MODULE_LICENSE("GPL"); |
| 50 | |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 51 | static char __initdata version[] = "0.93"; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 52 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 53 | static inline int |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 54 | ctnetlink_dump_tuples_proto(struct sk_buff *skb, |
Pablo Neira Ayuso | 1cde643 | 2006-03-22 13:54:15 -0800 | [diff] [blame] | 55 | const struct nf_conntrack_tuple *tuple, |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 56 | struct nf_conntrack_l4proto *l4proto) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 57 | { |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 58 | int ret = 0; |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 59 | struct nlattr *nest_parms; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 60 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 61 | nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED); |
| 62 | if (!nest_parms) |
| 63 | goto nla_put_failure; |
Patrick McHardy | 77236b6 | 2007-12-17 22:29:45 -0800 | [diff] [blame] | 64 | NLA_PUT_U8(skb, CTA_PROTO_NUM, tuple->dst.protonum); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 65 | |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 66 | if (likely(l4proto->tuple_to_nlattr)) |
| 67 | ret = l4proto->tuple_to_nlattr(skb, tuple); |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 68 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 69 | nla_nest_end(skb, nest_parms); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 70 | |
| 71 | return ret; |
| 72 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 73 | nla_put_failure: |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 74 | return -1; |
| 75 | } |
| 76 | |
| 77 | static inline int |
Pablo Neira Ayuso | 1cde643 | 2006-03-22 13:54:15 -0800 | [diff] [blame] | 78 | ctnetlink_dump_tuples_ip(struct sk_buff *skb, |
| 79 | const struct nf_conntrack_tuple *tuple, |
| 80 | struct nf_conntrack_l3proto *l3proto) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 81 | { |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 82 | int ret = 0; |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 83 | struct nlattr *nest_parms; |
| 84 | |
| 85 | nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED); |
| 86 | if (!nest_parms) |
| 87 | goto nla_put_failure; |
Pablo Neira Ayuso | 1cde643 | 2006-03-22 13:54:15 -0800 | [diff] [blame] | 88 | |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 89 | if (likely(l3proto->tuple_to_nlattr)) |
| 90 | ret = l3proto->tuple_to_nlattr(skb, tuple); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 91 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 92 | nla_nest_end(skb, nest_parms); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 93 | |
| 94 | return ret; |
| 95 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 96 | nla_put_failure: |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 97 | return -1; |
| 98 | } |
| 99 | |
Ilpo Järvinen | bb5cf80 | 2008-01-05 23:11:31 -0800 | [diff] [blame] | 100 | static int |
Pablo Neira Ayuso | 1cde643 | 2006-03-22 13:54:15 -0800 | [diff] [blame] | 101 | ctnetlink_dump_tuples(struct sk_buff *skb, |
| 102 | const struct nf_conntrack_tuple *tuple) |
| 103 | { |
| 104 | int ret; |
| 105 | struct nf_conntrack_l3proto *l3proto; |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 106 | struct nf_conntrack_l4proto *l4proto; |
Pablo Neira Ayuso | 1cde643 | 2006-03-22 13:54:15 -0800 | [diff] [blame] | 107 | |
Pablo Neira Ayuso | 528a3a6 | 2008-11-17 16:00:40 +0100 | [diff] [blame] | 108 | l3proto = __nf_ct_l3proto_find(tuple->src.l3num); |
Pablo Neira Ayuso | 1cde643 | 2006-03-22 13:54:15 -0800 | [diff] [blame] | 109 | ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto); |
Pablo Neira Ayuso | 1cde643 | 2006-03-22 13:54:15 -0800 | [diff] [blame] | 110 | |
| 111 | if (unlikely(ret < 0)) |
| 112 | return ret; |
| 113 | |
Pablo Neira Ayuso | 528a3a6 | 2008-11-17 16:00:40 +0100 | [diff] [blame] | 114 | l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum); |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 115 | ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto); |
Pablo Neira Ayuso | 1cde643 | 2006-03-22 13:54:15 -0800 | [diff] [blame] | 116 | |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | static inline int |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 121 | ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct) |
| 122 | { |
Patrick McHardy | 77236b6 | 2007-12-17 22:29:45 -0800 | [diff] [blame] | 123 | NLA_PUT_BE32(skb, CTA_STATUS, htonl(ct->status)); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 124 | return 0; |
| 125 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 126 | nla_put_failure: |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | static inline int |
| 131 | ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct) |
| 132 | { |
Patrick McHardy | 77236b6 | 2007-12-17 22:29:45 -0800 | [diff] [blame] | 133 | long timeout = (ct->timeout.expires - jiffies) / HZ; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 134 | |
Patrick McHardy | 77236b6 | 2007-12-17 22:29:45 -0800 | [diff] [blame] | 135 | if (timeout < 0) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 136 | timeout = 0; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 137 | |
Patrick McHardy | 77236b6 | 2007-12-17 22:29:45 -0800 | [diff] [blame] | 138 | NLA_PUT_BE32(skb, CTA_TIMEOUT, htonl(timeout)); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 139 | return 0; |
| 140 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 141 | nla_put_failure: |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 142 | return -1; |
| 143 | } |
| 144 | |
| 145 | static inline int |
Patrick McHardy | 440f0d5 | 2009-06-10 14:32:47 +0200 | [diff] [blame] | 146 | ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 147 | { |
Patrick McHardy | 5e8fbe2 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 148 | struct nf_conntrack_l4proto *l4proto; |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 149 | struct nlattr *nest_proto; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 150 | int ret; |
| 151 | |
Pablo Neira Ayuso | 528a3a6 | 2008-11-17 16:00:40 +0100 | [diff] [blame] | 152 | l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct)); |
| 153 | if (!l4proto->to_nlattr) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 154 | return 0; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 155 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 156 | nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED); |
| 157 | if (!nest_proto) |
| 158 | goto nla_put_failure; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 159 | |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 160 | ret = l4proto->to_nlattr(skb, nest_proto, ct); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 161 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 162 | nla_nest_end(skb, nest_proto); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 163 | |
| 164 | return ret; |
| 165 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 166 | nla_put_failure: |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 167 | return -1; |
| 168 | } |
| 169 | |
| 170 | static inline int |
| 171 | ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct) |
| 172 | { |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 173 | struct nlattr *nest_helper; |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 174 | const struct nf_conn_help *help = nfct_help(ct); |
Patrick McHarrdy | 3c158f7 | 2007-06-05 12:55:27 -0700 | [diff] [blame] | 175 | struct nf_conntrack_helper *helper; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 176 | |
Patrick McHarrdy | 3c158f7 | 2007-06-05 12:55:27 -0700 | [diff] [blame] | 177 | if (!help) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 178 | return 0; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 179 | |
Patrick McHarrdy | 3c158f7 | 2007-06-05 12:55:27 -0700 | [diff] [blame] | 180 | helper = rcu_dereference(help->helper); |
| 181 | if (!helper) |
| 182 | goto out; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 183 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 184 | nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED); |
| 185 | if (!nest_helper) |
| 186 | goto nla_put_failure; |
Patrick McHardy | 77236b6 | 2007-12-17 22:29:45 -0800 | [diff] [blame] | 187 | NLA_PUT_STRING(skb, CTA_HELP_NAME, helper->name); |
Patrick McHarrdy | 3c158f7 | 2007-06-05 12:55:27 -0700 | [diff] [blame] | 188 | |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 189 | if (helper->to_nlattr) |
| 190 | helper->to_nlattr(skb, ct); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 191 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 192 | nla_nest_end(skb, nest_helper); |
Patrick McHarrdy | 3c158f7 | 2007-06-05 12:55:27 -0700 | [diff] [blame] | 193 | out: |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 194 | return 0; |
| 195 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 196 | nla_put_failure: |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 197 | return -1; |
| 198 | } |
| 199 | |
Ilpo Järvinen | bb5cf80 | 2008-01-05 23:11:31 -0800 | [diff] [blame] | 200 | static int |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 201 | ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct, |
| 202 | enum ip_conntrack_dir dir) |
| 203 | { |
| 204 | enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG; |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 205 | struct nlattr *nest_count; |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 206 | const struct nf_conn_counter *acct; |
| 207 | |
| 208 | acct = nf_conn_acct_find(ct); |
| 209 | if (!acct) |
| 210 | return 0; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 211 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 212 | nest_count = nla_nest_start(skb, type | NLA_F_NESTED); |
| 213 | if (!nest_count) |
| 214 | goto nla_put_failure; |
| 215 | |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 216 | NLA_PUT_BE64(skb, CTA_COUNTERS_PACKETS, |
| 217 | cpu_to_be64(acct[dir].packets)); |
| 218 | NLA_PUT_BE64(skb, CTA_COUNTERS_BYTES, |
| 219 | cpu_to_be64(acct[dir].bytes)); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 220 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 221 | nla_nest_end(skb, nest_count); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 222 | |
| 223 | return 0; |
| 224 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 225 | nla_put_failure: |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 226 | return -1; |
| 227 | } |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 228 | |
| 229 | #ifdef CONFIG_NF_CONNTRACK_MARK |
| 230 | static inline int |
| 231 | ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct) |
| 232 | { |
Patrick McHardy | 77236b6 | 2007-12-17 22:29:45 -0800 | [diff] [blame] | 233 | NLA_PUT_BE32(skb, CTA_MARK, htonl(ct->mark)); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 234 | return 0; |
| 235 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 236 | nla_put_failure: |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 237 | return -1; |
| 238 | } |
| 239 | #else |
| 240 | #define ctnetlink_dump_mark(a, b) (0) |
| 241 | #endif |
| 242 | |
Pablo Neira Ayuso | 37fccd8 | 2007-12-17 22:28:41 -0800 | [diff] [blame] | 243 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
| 244 | static inline int |
| 245 | ctnetlink_dump_secmark(struct sk_buff *skb, const struct nf_conn *ct) |
| 246 | { |
Patrick McHardy | 77236b6 | 2007-12-17 22:29:45 -0800 | [diff] [blame] | 247 | NLA_PUT_BE32(skb, CTA_SECMARK, htonl(ct->secmark)); |
Pablo Neira Ayuso | 37fccd8 | 2007-12-17 22:28:41 -0800 | [diff] [blame] | 248 | return 0; |
| 249 | |
| 250 | nla_put_failure: |
| 251 | return -1; |
| 252 | } |
| 253 | #else |
| 254 | #define ctnetlink_dump_secmark(a, b) (0) |
| 255 | #endif |
| 256 | |
Pablo Neira Ayuso | 0f417ce | 2007-12-17 22:28:19 -0800 | [diff] [blame] | 257 | #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple) |
| 258 | |
| 259 | static inline int |
| 260 | ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct) |
| 261 | { |
| 262 | struct nlattr *nest_parms; |
| 263 | |
| 264 | if (!(ct->status & IPS_EXPECTED)) |
| 265 | return 0; |
| 266 | |
| 267 | nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED); |
| 268 | if (!nest_parms) |
| 269 | goto nla_put_failure; |
| 270 | if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0) |
| 271 | goto nla_put_failure; |
| 272 | nla_nest_end(skb, nest_parms); |
| 273 | |
| 274 | return 0; |
| 275 | |
| 276 | nla_put_failure: |
| 277 | return -1; |
| 278 | } |
| 279 | |
Pablo Neira Ayuso | 13eae15 | 2007-12-17 22:28:00 -0800 | [diff] [blame] | 280 | #ifdef CONFIG_NF_NAT_NEEDED |
Ilpo Järvinen | bb5cf80 | 2008-01-05 23:11:31 -0800 | [diff] [blame] | 281 | static int |
Pablo Neira Ayuso | 13eae15 | 2007-12-17 22:28:00 -0800 | [diff] [blame] | 282 | dump_nat_seq_adj(struct sk_buff *skb, const struct nf_nat_seq *natseq, int type) |
| 283 | { |
Pablo Neira Ayuso | 13eae15 | 2007-12-17 22:28:00 -0800 | [diff] [blame] | 284 | struct nlattr *nest_parms; |
| 285 | |
| 286 | nest_parms = nla_nest_start(skb, type | NLA_F_NESTED); |
| 287 | if (!nest_parms) |
| 288 | goto nla_put_failure; |
| 289 | |
Patrick McHardy | 77236b6 | 2007-12-17 22:29:45 -0800 | [diff] [blame] | 290 | NLA_PUT_BE32(skb, CTA_NAT_SEQ_CORRECTION_POS, |
| 291 | htonl(natseq->correction_pos)); |
| 292 | NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_BEFORE, |
| 293 | htonl(natseq->offset_before)); |
| 294 | NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_AFTER, |
| 295 | htonl(natseq->offset_after)); |
Pablo Neira Ayuso | 13eae15 | 2007-12-17 22:28:00 -0800 | [diff] [blame] | 296 | |
| 297 | nla_nest_end(skb, nest_parms); |
| 298 | |
| 299 | return 0; |
| 300 | |
| 301 | nla_put_failure: |
| 302 | return -1; |
| 303 | } |
| 304 | |
| 305 | static inline int |
| 306 | ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct) |
| 307 | { |
| 308 | struct nf_nat_seq *natseq; |
| 309 | struct nf_conn_nat *nat = nfct_nat(ct); |
| 310 | |
| 311 | if (!(ct->status & IPS_SEQ_ADJUST) || !nat) |
| 312 | return 0; |
| 313 | |
| 314 | natseq = &nat->seq[IP_CT_DIR_ORIGINAL]; |
| 315 | if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1) |
| 316 | return -1; |
| 317 | |
| 318 | natseq = &nat->seq[IP_CT_DIR_REPLY]; |
| 319 | if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1) |
| 320 | return -1; |
| 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | #else |
| 325 | #define ctnetlink_dump_nat_seq_adj(a, b) (0) |
| 326 | #endif |
| 327 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 328 | static inline int |
| 329 | ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct) |
| 330 | { |
Patrick McHardy | 77236b6 | 2007-12-17 22:29:45 -0800 | [diff] [blame] | 331 | NLA_PUT_BE32(skb, CTA_ID, htonl((unsigned long)ct)); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 332 | return 0; |
| 333 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 334 | nla_put_failure: |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 335 | return -1; |
| 336 | } |
| 337 | |
| 338 | static inline int |
| 339 | ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct) |
| 340 | { |
Patrick McHardy | 77236b6 | 2007-12-17 22:29:45 -0800 | [diff] [blame] | 341 | NLA_PUT_BE32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use))); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 342 | return 0; |
| 343 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 344 | nla_put_failure: |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 345 | return -1; |
| 346 | } |
| 347 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 348 | static int |
| 349 | ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq, |
Patrick McHardy | 440f0d5 | 2009-06-10 14:32:47 +0200 | [diff] [blame] | 350 | int event, struct nf_conn *ct) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 351 | { |
| 352 | struct nlmsghdr *nlh; |
| 353 | struct nfgenmsg *nfmsg; |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 354 | struct nlattr *nest_parms; |
Pablo Neira Ayuso | 96bcf93 | 2009-06-02 20:07:39 +0200 | [diff] [blame] | 355 | unsigned int flags = pid ? NLM_F_MULTI : 0; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 356 | |
| 357 | event |= NFNL_SUBSYS_CTNETLINK << 8; |
Pablo Neira Ayuso | 96bcf93 | 2009-06-02 20:07:39 +0200 | [diff] [blame] | 358 | nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags); |
| 359 | if (nlh == NULL) |
| 360 | goto nlmsg_failure; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 361 | |
Pablo Neira Ayuso | 96bcf93 | 2009-06-02 20:07:39 +0200 | [diff] [blame] | 362 | nfmsg = nlmsg_data(nlh); |
Patrick McHardy | 5e8fbe2 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 363 | nfmsg->nfgen_family = nf_ct_l3num(ct); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 364 | nfmsg->version = NFNETLINK_V0; |
| 365 | nfmsg->res_id = 0; |
| 366 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 367 | nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED); |
| 368 | if (!nest_parms) |
| 369 | goto nla_put_failure; |
Pablo Neira Ayuso | f2f3e38 | 2009-06-02 20:03:35 +0200 | [diff] [blame] | 370 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 371 | goto nla_put_failure; |
| 372 | nla_nest_end(skb, nest_parms); |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 373 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 374 | nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED); |
| 375 | if (!nest_parms) |
| 376 | goto nla_put_failure; |
Pablo Neira Ayuso | f2f3e38 | 2009-06-02 20:03:35 +0200 | [diff] [blame] | 377 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0) |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 378 | goto nla_put_failure; |
| 379 | nla_nest_end(skb, nest_parms); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 380 | |
| 381 | if (ctnetlink_dump_status(skb, ct) < 0 || |
| 382 | ctnetlink_dump_timeout(skb, ct) < 0 || |
| 383 | ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 || |
| 384 | ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 || |
| 385 | ctnetlink_dump_protoinfo(skb, ct) < 0 || |
| 386 | ctnetlink_dump_helpinfo(skb, ct) < 0 || |
| 387 | ctnetlink_dump_mark(skb, ct) < 0 || |
Pablo Neira Ayuso | 37fccd8 | 2007-12-17 22:28:41 -0800 | [diff] [blame] | 388 | ctnetlink_dump_secmark(skb, ct) < 0 || |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 389 | ctnetlink_dump_id(skb, ct) < 0 || |
Pablo Neira Ayuso | 13eae15 | 2007-12-17 22:28:00 -0800 | [diff] [blame] | 390 | ctnetlink_dump_use(skb, ct) < 0 || |
Pablo Neira Ayuso | 0f417ce | 2007-12-17 22:28:19 -0800 | [diff] [blame] | 391 | ctnetlink_dump_master(skb, ct) < 0 || |
Pablo Neira Ayuso | 13eae15 | 2007-12-17 22:28:00 -0800 | [diff] [blame] | 392 | ctnetlink_dump_nat_seq_adj(skb, ct) < 0) |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 393 | goto nla_put_failure; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 394 | |
Pablo Neira Ayuso | 96bcf93 | 2009-06-02 20:07:39 +0200 | [diff] [blame] | 395 | nlmsg_end(skb, nlh); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 396 | return skb->len; |
| 397 | |
| 398 | nlmsg_failure: |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 399 | nla_put_failure: |
Pablo Neira Ayuso | 96bcf93 | 2009-06-02 20:07:39 +0200 | [diff] [blame] | 400 | nlmsg_cancel(skb, nlh); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 401 | return -1; |
| 402 | } |
| 403 | |
| 404 | #ifdef CONFIG_NF_CONNTRACK_EVENTS |
Pablo Neira Ayuso | 03b64f5 | 2009-06-02 20:08:27 +0200 | [diff] [blame] | 405 | static inline size_t |
| 406 | ctnetlink_proto_size(const struct nf_conn *ct) |
Holger Eitzenberger | 2732c4e | 2009-03-25 21:50:59 +0100 | [diff] [blame] | 407 | { |
| 408 | struct nf_conntrack_l3proto *l3proto; |
| 409 | struct nf_conntrack_l4proto *l4proto; |
Pablo Neira Ayuso | 03b64f5 | 2009-06-02 20:08:27 +0200 | [diff] [blame] | 410 | size_t len = 0; |
Holger Eitzenberger | 2732c4e | 2009-03-25 21:50:59 +0100 | [diff] [blame] | 411 | |
| 412 | rcu_read_lock(); |
Pablo Neira Ayuso | 03b64f5 | 2009-06-02 20:08:27 +0200 | [diff] [blame] | 413 | l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct)); |
Holger Eitzenberger | 2732c4e | 2009-03-25 21:50:59 +0100 | [diff] [blame] | 414 | len += l3proto->nla_size; |
| 415 | |
Pablo Neira Ayuso | 03b64f5 | 2009-06-02 20:08:27 +0200 | [diff] [blame] | 416 | l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct)); |
Holger Eitzenberger | 2732c4e | 2009-03-25 21:50:59 +0100 | [diff] [blame] | 417 | len += l4proto->nla_size; |
| 418 | rcu_read_unlock(); |
| 419 | |
Pablo Neira Ayuso | 03b64f5 | 2009-06-02 20:08:27 +0200 | [diff] [blame] | 420 | return len; |
| 421 | } |
| 422 | |
| 423 | static inline size_t |
| 424 | ctnetlink_nlmsg_size(const struct nf_conn *ct) |
| 425 | { |
| 426 | return NLMSG_ALIGN(sizeof(struct nfgenmsg)) |
| 427 | + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */ |
| 428 | + 3 * nla_total_size(0) /* CTA_TUPLE_IP */ |
| 429 | + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */ |
| 430 | + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */ |
| 431 | + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */ |
| 432 | + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */ |
| 433 | #ifdef CONFIG_NF_CT_ACCT |
| 434 | + 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */ |
| 435 | + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */ |
| 436 | + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */ |
| 437 | #endif |
| 438 | + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */ |
| 439 | + nla_total_size(0) /* CTA_PROTOINFO */ |
| 440 | + nla_total_size(0) /* CTA_HELP */ |
| 441 | + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */ |
| 442 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
| 443 | + nla_total_size(sizeof(u_int32_t)) /* CTA_SECMARK */ |
| 444 | #endif |
| 445 | #ifdef CONFIG_NF_NAT_NEEDED |
| 446 | + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */ |
| 447 | + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */ |
| 448 | #endif |
| 449 | #ifdef CONFIG_NF_CONNTRACK_MARK |
| 450 | + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */ |
| 451 | #endif |
| 452 | + ctnetlink_proto_size(ct) |
| 453 | ; |
Holger Eitzenberger | 2732c4e | 2009-03-25 21:50:59 +0100 | [diff] [blame] | 454 | } |
| 455 | |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 456 | static int |
| 457 | ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 458 | { |
| 459 | struct nlmsghdr *nlh; |
| 460 | struct nfgenmsg *nfmsg; |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 461 | struct nlattr *nest_parms; |
Pablo Neira Ayuso | 19abb7b | 2008-11-18 11:56:20 +0100 | [diff] [blame] | 462 | struct nf_conn *ct = item->ct; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 463 | struct sk_buff *skb; |
| 464 | unsigned int type; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 465 | unsigned int flags = 0, group; |
Pablo Neira Ayuso | dd7669a | 2009-06-13 12:30:52 +0200 | [diff] [blame] | 466 | int err; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 467 | |
| 468 | /* ignore our fake conntrack entry */ |
| 469 | if (ct == &nf_conntrack_untracked) |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 470 | return 0; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 471 | |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 472 | if (events & (1 << IPCT_DESTROY)) { |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 473 | type = IPCTNL_MSG_CT_DELETE; |
| 474 | group = NFNLGRP_CONNTRACK_DESTROY; |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 475 | } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) { |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 476 | type = IPCTNL_MSG_CT_NEW; |
| 477 | flags = NLM_F_CREATE|NLM_F_EXCL; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 478 | group = NFNLGRP_CONNTRACK_NEW; |
Pablo Neira Ayuso | 17e6e4e | 2009-06-02 20:08:46 +0200 | [diff] [blame] | 479 | } else if (events) { |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 480 | type = IPCTNL_MSG_CT_NEW; |
| 481 | group = NFNLGRP_CONNTRACK_UPDATE; |
| 482 | } else |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 483 | return 0; |
Patrick McHardy | a242769 | 2006-03-20 18:03:59 -0800 | [diff] [blame] | 484 | |
Pablo Neira Ayuso | 1f9da25 | 2009-02-09 14:34:26 -0800 | [diff] [blame] | 485 | if (!item->report && !nfnetlink_has_listeners(group)) |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 486 | return 0; |
Patrick McHardy | a242769 | 2006-03-20 18:03:59 -0800 | [diff] [blame] | 487 | |
Pablo Neira Ayuso | 03b64f5 | 2009-06-02 20:08:27 +0200 | [diff] [blame] | 488 | skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC); |
| 489 | if (skb == NULL) |
Pablo Neira Ayuso | 150ace0 | 2009-04-17 17:47:31 +0200 | [diff] [blame] | 490 | goto errout; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 491 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 492 | type |= NFNL_SUBSYS_CTNETLINK << 8; |
Pablo Neira Ayuso | 96bcf93 | 2009-06-02 20:07:39 +0200 | [diff] [blame] | 493 | nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags); |
| 494 | if (nlh == NULL) |
| 495 | goto nlmsg_failure; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 496 | |
Pablo Neira Ayuso | 96bcf93 | 2009-06-02 20:07:39 +0200 | [diff] [blame] | 497 | nfmsg = nlmsg_data(nlh); |
Patrick McHardy | 5e8fbe2 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 498 | nfmsg->nfgen_family = nf_ct_l3num(ct); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 499 | nfmsg->version = NFNETLINK_V0; |
| 500 | nfmsg->res_id = 0; |
| 501 | |
Pablo Neira Ayuso | 528a3a6 | 2008-11-17 16:00:40 +0100 | [diff] [blame] | 502 | rcu_read_lock(); |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 503 | nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED); |
| 504 | if (!nest_parms) |
| 505 | goto nla_put_failure; |
Pablo Neira Ayuso | f2f3e38 | 2009-06-02 20:03:35 +0200 | [diff] [blame] | 506 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 507 | goto nla_put_failure; |
| 508 | nla_nest_end(skb, nest_parms); |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 509 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 510 | nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED); |
| 511 | if (!nest_parms) |
| 512 | goto nla_put_failure; |
Pablo Neira Ayuso | f2f3e38 | 2009-06-02 20:03:35 +0200 | [diff] [blame] | 513 | if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0) |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 514 | goto nla_put_failure; |
| 515 | nla_nest_end(skb, nest_parms); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 516 | |
Eric Leblond | 1eedf69 | 2008-05-13 23:27:11 -0700 | [diff] [blame] | 517 | if (ctnetlink_dump_id(skb, ct) < 0) |
| 518 | goto nla_put_failure; |
| 519 | |
Fabian Hugelshofer | e57dce6 | 2008-06-09 15:59:58 -0700 | [diff] [blame] | 520 | if (ctnetlink_dump_status(skb, ct) < 0) |
| 521 | goto nla_put_failure; |
| 522 | |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 523 | if (events & (1 << IPCT_DESTROY)) { |
Pablo Neira Ayuso | 7b621c1 | 2006-11-29 02:35:32 +0100 | [diff] [blame] | 524 | if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 || |
| 525 | ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0) |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 526 | goto nla_put_failure; |
Pablo Neira Ayuso | 7b621c1 | 2006-11-29 02:35:32 +0100 | [diff] [blame] | 527 | } else { |
Pablo Neira Ayuso | 7b621c1 | 2006-11-29 02:35:32 +0100 | [diff] [blame] | 528 | if (ctnetlink_dump_timeout(skb, ct) < 0) |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 529 | goto nla_put_failure; |
Pablo Neira Ayuso | 7b621c1 | 2006-11-29 02:35:32 +0100 | [diff] [blame] | 530 | |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 531 | if (events & (1 << IPCT_PROTOINFO) |
Pablo Neira Ayuso | 7b621c1 | 2006-11-29 02:35:32 +0100 | [diff] [blame] | 532 | && ctnetlink_dump_protoinfo(skb, ct) < 0) |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 533 | goto nla_put_failure; |
Pablo Neira Ayuso | 7b621c1 | 2006-11-29 02:35:32 +0100 | [diff] [blame] | 534 | |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 535 | if ((events & (1 << IPCT_HELPER) || nfct_help(ct)) |
Pablo Neira Ayuso | 7b621c1 | 2006-11-29 02:35:32 +0100 | [diff] [blame] | 536 | && ctnetlink_dump_helpinfo(skb, ct) < 0) |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 537 | goto nla_put_failure; |
Pablo Neira Ayuso | 7b621c1 | 2006-11-29 02:35:32 +0100 | [diff] [blame] | 538 | |
Pablo Neira Ayuso | 37fccd8 | 2007-12-17 22:28:41 -0800 | [diff] [blame] | 539 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 540 | if ((events & (1 << IPCT_SECMARK) || ct->secmark) |
Pablo Neira Ayuso | 37fccd8 | 2007-12-17 22:28:41 -0800 | [diff] [blame] | 541 | && ctnetlink_dump_secmark(skb, ct) < 0) |
| 542 | goto nla_put_failure; |
| 543 | #endif |
Pablo Neira Ayuso | 7b621c1 | 2006-11-29 02:35:32 +0100 | [diff] [blame] | 544 | |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 545 | if (events & (1 << IPCT_RELATED) && |
Pablo Neira Ayuso | 0f417ce | 2007-12-17 22:28:19 -0800 | [diff] [blame] | 546 | ctnetlink_dump_master(skb, ct) < 0) |
| 547 | goto nla_put_failure; |
| 548 | |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 549 | if (events & (1 << IPCT_NATSEQADJ) && |
Pablo Neira Ayuso | 13eae15 | 2007-12-17 22:28:00 -0800 | [diff] [blame] | 550 | ctnetlink_dump_nat_seq_adj(skb, ct) < 0) |
| 551 | goto nla_put_failure; |
Pablo Neira Ayuso | 7b621c1 | 2006-11-29 02:35:32 +0100 | [diff] [blame] | 552 | } |
Pablo Neira Ayuso | b9a37e0 | 2006-08-22 00:31:49 -0700 | [diff] [blame] | 553 | |
Eric Leblond | a83099a | 2008-01-31 04:44:27 -0800 | [diff] [blame] | 554 | #ifdef CONFIG_NF_CONNTRACK_MARK |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 555 | if ((events & (1 << IPCT_MARK) || ct->mark) |
Eric Leblond | a83099a | 2008-01-31 04:44:27 -0800 | [diff] [blame] | 556 | && ctnetlink_dump_mark(skb, ct) < 0) |
| 557 | goto nla_put_failure; |
| 558 | #endif |
Pablo Neira Ayuso | 528a3a6 | 2008-11-17 16:00:40 +0100 | [diff] [blame] | 559 | rcu_read_unlock(); |
Eric Leblond | a83099a | 2008-01-31 04:44:27 -0800 | [diff] [blame] | 560 | |
Pablo Neira Ayuso | 96bcf93 | 2009-06-02 20:07:39 +0200 | [diff] [blame] | 561 | nlmsg_end(skb, nlh); |
Pablo Neira Ayuso | dd7669a | 2009-06-13 12:30:52 +0200 | [diff] [blame] | 562 | err = nfnetlink_send(skb, item->pid, group, item->report, GFP_ATOMIC); |
| 563 | if (err == -ENOBUFS || err == -EAGAIN) |
| 564 | return -ENOBUFS; |
| 565 | |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 566 | return 0; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 567 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 568 | nla_put_failure: |
Pablo Neira Ayuso | 528a3a6 | 2008-11-17 16:00:40 +0100 | [diff] [blame] | 569 | rcu_read_unlock(); |
Pablo Neira Ayuso | 96bcf93 | 2009-06-02 20:07:39 +0200 | [diff] [blame] | 570 | nlmsg_cancel(skb, nlh); |
Pablo Neira Ayuso | 528a3a6 | 2008-11-17 16:00:40 +0100 | [diff] [blame] | 571 | nlmsg_failure: |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 572 | kfree_skb(skb); |
Pablo Neira Ayuso | 150ace0 | 2009-04-17 17:47:31 +0200 | [diff] [blame] | 573 | errout: |
| 574 | nfnetlink_set_err(0, group, -ENOBUFS); |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 575 | return 0; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 576 | } |
| 577 | #endif /* CONFIG_NF_CONNTRACK_EVENTS */ |
| 578 | |
| 579 | static int ctnetlink_done(struct netlink_callback *cb) |
| 580 | { |
Patrick McHardy | 89f2e21 | 2006-05-29 18:24:58 -0700 | [diff] [blame] | 581 | if (cb->args[1]) |
| 582 | nf_ct_put((struct nf_conn *)cb->args[1]); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | static int |
| 587 | ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb) |
| 588 | { |
Patrick McHardy | 89f2e21 | 2006-05-29 18:24:58 -0700 | [diff] [blame] | 589 | struct nf_conn *ct, *last; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 590 | struct nf_conntrack_tuple_hash *h; |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 591 | struct hlist_nulls_node *n; |
Pablo Neira Ayuso | 96bcf93 | 2009-06-02 20:07:39 +0200 | [diff] [blame] | 592 | struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); |
Pablo Neira Ayuso | 87711cb | 2006-01-05 12:19:23 -0800 | [diff] [blame] | 593 | u_int8_t l3proto = nfmsg->nfgen_family; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 594 | |
Patrick McHardy | 76507f6 | 2008-01-31 04:38:38 -0800 | [diff] [blame] | 595 | rcu_read_lock(); |
Patrick McHardy | d205dc4 | 2006-08-17 18:12:38 -0700 | [diff] [blame] | 596 | last = (struct nf_conn *)cb->args[1]; |
Patrick McHardy | 89f2e21 | 2006-05-29 18:24:58 -0700 | [diff] [blame] | 597 | for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) { |
| 598 | restart: |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 599 | hlist_nulls_for_each_entry_rcu(h, n, &init_net.ct.hash[cb->args[0]], |
| 600 | hnnode) { |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 601 | if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 602 | continue; |
| 603 | ct = nf_ct_tuplehash_to_ctrack(h); |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 604 | if (!atomic_inc_not_zero(&ct->ct_general.use)) |
| 605 | continue; |
Pablo Neira Ayuso | 87711cb | 2006-01-05 12:19:23 -0800 | [diff] [blame] | 606 | /* Dump entries of a given L3 protocol number. |
| 607 | * If it is not specified, ie. l3proto == 0, |
| 608 | * then dump everything. */ |
Patrick McHardy | 5e8fbe2 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 609 | if (l3proto && nf_ct_l3num(ct) != l3proto) |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 610 | goto releasect; |
Patrick McHardy | d205dc4 | 2006-08-17 18:12:38 -0700 | [diff] [blame] | 611 | if (cb->args[1]) { |
| 612 | if (ct != last) |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 613 | goto releasect; |
Patrick McHardy | d205dc4 | 2006-08-17 18:12:38 -0700 | [diff] [blame] | 614 | cb->args[1] = 0; |
Patrick McHardy | 89f2e21 | 2006-05-29 18:24:58 -0700 | [diff] [blame] | 615 | } |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 616 | if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid, |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 617 | cb->nlh->nlmsg_seq, |
Pablo Neira Ayuso | 8b0a231 | 2009-06-02 20:03:34 +0200 | [diff] [blame] | 618 | IPCTNL_MSG_CT_NEW, ct) < 0) { |
Patrick McHardy | 89f2e21 | 2006-05-29 18:24:58 -0700 | [diff] [blame] | 619 | cb->args[1] = (unsigned long)ct; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 620 | goto out; |
Patrick McHardy | 89f2e21 | 2006-05-29 18:24:58 -0700 | [diff] [blame] | 621 | } |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 622 | |
Pablo Neira Ayuso | 01f3484 | 2006-09-20 12:00:45 -0700 | [diff] [blame] | 623 | if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 624 | IPCTNL_MSG_CT_GET_CTRZERO) { |
| 625 | struct nf_conn_counter *acct; |
| 626 | |
| 627 | acct = nf_conn_acct_find(ct); |
| 628 | if (acct) |
| 629 | memset(acct, 0, sizeof(struct nf_conn_counter[IP_CT_DIR_MAX])); |
| 630 | } |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 631 | releasect: |
| 632 | nf_ct_put(ct); |
Patrick McHardy | 89f2e21 | 2006-05-29 18:24:58 -0700 | [diff] [blame] | 633 | } |
Patrick McHardy | d205dc4 | 2006-08-17 18:12:38 -0700 | [diff] [blame] | 634 | if (cb->args[1]) { |
Patrick McHardy | 89f2e21 | 2006-05-29 18:24:58 -0700 | [diff] [blame] | 635 | cb->args[1] = 0; |
| 636 | goto restart; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 637 | } |
| 638 | } |
Patrick McHardy | 89f2e21 | 2006-05-29 18:24:58 -0700 | [diff] [blame] | 639 | out: |
Patrick McHardy | 76507f6 | 2008-01-31 04:38:38 -0800 | [diff] [blame] | 640 | rcu_read_unlock(); |
Patrick McHardy | d205dc4 | 2006-08-17 18:12:38 -0700 | [diff] [blame] | 641 | if (last) |
| 642 | nf_ct_put(last); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 643 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 644 | return skb->len; |
| 645 | } |
| 646 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 647 | static inline int |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 648 | ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 649 | { |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 650 | struct nlattr *tb[CTA_IP_MAX+1]; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 651 | struct nf_conntrack_l3proto *l3proto; |
| 652 | int ret = 0; |
| 653 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 654 | nla_parse_nested(tb, CTA_IP_MAX, attr, NULL); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 655 | |
Florian Westphal | cd91566 | 2009-03-18 17:28:37 +0100 | [diff] [blame] | 656 | rcu_read_lock(); |
| 657 | l3proto = __nf_ct_l3proto_find(tuple->src.l3num); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 658 | |
Patrick McHardy | f73e924 | 2007-09-28 14:39:55 -0700 | [diff] [blame] | 659 | if (likely(l3proto->nlattr_to_tuple)) { |
| 660 | ret = nla_validate_nested(attr, CTA_IP_MAX, |
| 661 | l3proto->nla_policy); |
| 662 | if (ret == 0) |
| 663 | ret = l3proto->nlattr_to_tuple(tb, tuple); |
| 664 | } |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 665 | |
Florian Westphal | cd91566 | 2009-03-18 17:28:37 +0100 | [diff] [blame] | 666 | rcu_read_unlock(); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 667 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 668 | return ret; |
| 669 | } |
| 670 | |
Patrick McHardy | f73e924 | 2007-09-28 14:39:55 -0700 | [diff] [blame] | 671 | static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = { |
| 672 | [CTA_PROTO_NUM] = { .type = NLA_U8 }, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 673 | }; |
| 674 | |
| 675 | static inline int |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 676 | ctnetlink_parse_tuple_proto(struct nlattr *attr, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 677 | struct nf_conntrack_tuple *tuple) |
| 678 | { |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 679 | struct nlattr *tb[CTA_PROTO_MAX+1]; |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 680 | struct nf_conntrack_l4proto *l4proto; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 681 | int ret = 0; |
| 682 | |
Patrick McHardy | f73e924 | 2007-09-28 14:39:55 -0700 | [diff] [blame] | 683 | ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy); |
| 684 | if (ret < 0) |
| 685 | return ret; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 686 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 687 | if (!tb[CTA_PROTO_NUM]) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 688 | return -EINVAL; |
Patrick McHardy | 77236b6 | 2007-12-17 22:29:45 -0800 | [diff] [blame] | 689 | tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 690 | |
Florian Westphal | cd91566 | 2009-03-18 17:28:37 +0100 | [diff] [blame] | 691 | rcu_read_lock(); |
| 692 | l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 693 | |
Patrick McHardy | f73e924 | 2007-09-28 14:39:55 -0700 | [diff] [blame] | 694 | if (likely(l4proto->nlattr_to_tuple)) { |
| 695 | ret = nla_validate_nested(attr, CTA_PROTO_MAX, |
| 696 | l4proto->nla_policy); |
| 697 | if (ret == 0) |
| 698 | ret = l4proto->nlattr_to_tuple(tb, tuple); |
| 699 | } |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 700 | |
Florian Westphal | cd91566 | 2009-03-18 17:28:37 +0100 | [diff] [blame] | 701 | rcu_read_unlock(); |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 702 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 703 | return ret; |
| 704 | } |
| 705 | |
Ilpo Järvinen | bb5cf80 | 2008-01-05 23:11:31 -0800 | [diff] [blame] | 706 | static int |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 707 | ctnetlink_parse_tuple(struct nlattr *cda[], struct nf_conntrack_tuple *tuple, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 708 | enum ctattr_tuple type, u_int8_t l3num) |
| 709 | { |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 710 | struct nlattr *tb[CTA_TUPLE_MAX+1]; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 711 | int err; |
| 712 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 713 | memset(tuple, 0, sizeof(*tuple)); |
| 714 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 715 | nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], NULL); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 716 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 717 | if (!tb[CTA_TUPLE_IP]) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 718 | return -EINVAL; |
| 719 | |
| 720 | tuple->src.l3num = l3num; |
| 721 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 722 | err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 723 | if (err < 0) |
| 724 | return err; |
| 725 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 726 | if (!tb[CTA_TUPLE_PROTO]) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 727 | return -EINVAL; |
| 728 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 729 | err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 730 | if (err < 0) |
| 731 | return err; |
| 732 | |
| 733 | /* orig and expect tuples get DIR_ORIGINAL */ |
| 734 | if (type == CTA_TUPLE_REPLY) |
| 735 | tuple->dst.dir = IP_CT_DIR_REPLY; |
| 736 | else |
| 737 | tuple->dst.dir = IP_CT_DIR_ORIGINAL; |
| 738 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 739 | return 0; |
| 740 | } |
| 741 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 742 | static inline int |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 743 | ctnetlink_parse_help(struct nlattr *attr, char **helper_name) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 744 | { |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 745 | struct nlattr *tb[CTA_HELP_MAX+1]; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 746 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 747 | nla_parse_nested(tb, CTA_HELP_MAX, attr, NULL); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 748 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 749 | if (!tb[CTA_HELP_NAME]) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 750 | return -EINVAL; |
| 751 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 752 | *helper_name = nla_data(tb[CTA_HELP_NAME]); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 753 | |
| 754 | return 0; |
| 755 | } |
| 756 | |
Patrick McHardy | f73e924 | 2007-09-28 14:39:55 -0700 | [diff] [blame] | 757 | static const struct nla_policy ct_nla_policy[CTA_MAX+1] = { |
| 758 | [CTA_STATUS] = { .type = NLA_U32 }, |
| 759 | [CTA_TIMEOUT] = { .type = NLA_U32 }, |
| 760 | [CTA_MARK] = { .type = NLA_U32 }, |
| 761 | [CTA_USE] = { .type = NLA_U32 }, |
| 762 | [CTA_ID] = { .type = NLA_U32 }, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 763 | }; |
| 764 | |
| 765 | static int |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 766 | ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb, |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 767 | struct nlmsghdr *nlh, struct nlattr *cda[]) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 768 | { |
| 769 | struct nf_conntrack_tuple_hash *h; |
| 770 | struct nf_conntrack_tuple tuple; |
| 771 | struct nf_conn *ct; |
Pablo Neira Ayuso | 96bcf93 | 2009-06-02 20:07:39 +0200 | [diff] [blame] | 772 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 773 | u_int8_t u3 = nfmsg->nfgen_family; |
| 774 | int err = 0; |
| 775 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 776 | if (cda[CTA_TUPLE_ORIG]) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 777 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3); |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 778 | else if (cda[CTA_TUPLE_REPLY]) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 779 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3); |
| 780 | else { |
| 781 | /* Flush the whole table */ |
Pablo Neira Ayuso | 274d383 | 2009-06-02 20:08:38 +0200 | [diff] [blame] | 782 | nf_conntrack_flush_report(&init_net, |
| 783 | NETLINK_CB(skb).pid, |
| 784 | nlmsg_report(nlh)); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 785 | return 0; |
| 786 | } |
| 787 | |
| 788 | if (err < 0) |
| 789 | return err; |
| 790 | |
Alexey Dobriyan | 400dad3 | 2008-10-08 11:35:03 +0200 | [diff] [blame] | 791 | h = nf_conntrack_find_get(&init_net, &tuple); |
Pablo Neira Ayuso | 9ea8cfd | 2006-10-12 14:09:16 -0700 | [diff] [blame] | 792 | if (!h) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 793 | return -ENOENT; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 794 | |
| 795 | ct = nf_ct_tuplehash_to_ctrack(h); |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 796 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 797 | if (cda[CTA_ID]) { |
Patrick McHardy | 77236b6 | 2007-12-17 22:29:45 -0800 | [diff] [blame] | 798 | u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID])); |
Patrick McHardy | 7f85f91 | 2007-09-28 14:41:27 -0700 | [diff] [blame] | 799 | if (id != (u32)(unsigned long)ct) { |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 800 | nf_ct_put(ct); |
| 801 | return -ENOENT; |
| 802 | } |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 803 | } |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 804 | |
Pablo Neira Ayuso | dd7669a | 2009-06-13 12:30:52 +0200 | [diff] [blame] | 805 | if (nf_conntrack_event_report(IPCT_DESTROY, ct, |
| 806 | NETLINK_CB(skb).pid, |
| 807 | nlmsg_report(nlh)) < 0) { |
| 808 | nf_ct_delete_from_lists(ct); |
| 809 | /* we failed to report the event, try later */ |
| 810 | nf_ct_insert_dying_list(ct); |
| 811 | nf_ct_put(ct); |
| 812 | return 0; |
| 813 | } |
Pablo Neira Ayuso | 19abb7b | 2008-11-18 11:56:20 +0100 | [diff] [blame] | 814 | |
| 815 | /* death_by_timeout would report the event again */ |
| 816 | set_bit(IPS_DYING_BIT, &ct->status); |
| 817 | |
Patrick McHardy | 5109176 | 2008-06-09 15:59:06 -0700 | [diff] [blame] | 818 | nf_ct_kill(ct); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 819 | nf_ct_put(ct); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 820 | |
| 821 | return 0; |
| 822 | } |
| 823 | |
| 824 | static int |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 825 | ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb, |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 826 | struct nlmsghdr *nlh, struct nlattr *cda[]) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 827 | { |
| 828 | struct nf_conntrack_tuple_hash *h; |
| 829 | struct nf_conntrack_tuple tuple; |
| 830 | struct nf_conn *ct; |
| 831 | struct sk_buff *skb2 = NULL; |
Pablo Neira Ayuso | 96bcf93 | 2009-06-02 20:07:39 +0200 | [diff] [blame] | 832 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 833 | u_int8_t u3 = nfmsg->nfgen_family; |
| 834 | int err = 0; |
| 835 | |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 836 | if (nlh->nlmsg_flags & NLM_F_DUMP) |
Thomas Graf | c702e80 | 2007-03-22 23:30:55 -0700 | [diff] [blame] | 837 | return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table, |
| 838 | ctnetlink_done); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 839 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 840 | if (cda[CTA_TUPLE_ORIG]) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 841 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3); |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 842 | else if (cda[CTA_TUPLE_REPLY]) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 843 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3); |
| 844 | else |
| 845 | return -EINVAL; |
| 846 | |
| 847 | if (err < 0) |
| 848 | return err; |
| 849 | |
Alexey Dobriyan | 400dad3 | 2008-10-08 11:35:03 +0200 | [diff] [blame] | 850 | h = nf_conntrack_find_get(&init_net, &tuple); |
Pablo Neira Ayuso | 9ea8cfd | 2006-10-12 14:09:16 -0700 | [diff] [blame] | 851 | if (!h) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 852 | return -ENOENT; |
Pablo Neira Ayuso | 9ea8cfd | 2006-10-12 14:09:16 -0700 | [diff] [blame] | 853 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 854 | ct = nf_ct_tuplehash_to_ctrack(h); |
| 855 | |
| 856 | err = -ENOMEM; |
Pablo Neira Ayuso | 96bcf93 | 2009-06-02 20:07:39 +0200 | [diff] [blame] | 857 | skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 858 | if (skb2 == NULL) { |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 859 | nf_ct_put(ct); |
| 860 | return -ENOMEM; |
| 861 | } |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 862 | |
Pablo Neira Ayuso | 528a3a6 | 2008-11-17 16:00:40 +0100 | [diff] [blame] | 863 | rcu_read_lock(); |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 864 | err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq, |
Pablo Neira Ayuso | 8b0a231 | 2009-06-02 20:03:34 +0200 | [diff] [blame] | 865 | IPCTNL_MSG_CT_NEW, ct); |
Pablo Neira Ayuso | 528a3a6 | 2008-11-17 16:00:40 +0100 | [diff] [blame] | 866 | rcu_read_unlock(); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 867 | nf_ct_put(ct); |
| 868 | if (err <= 0) |
| 869 | goto free; |
| 870 | |
| 871 | err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT); |
| 872 | if (err < 0) |
| 873 | goto out; |
| 874 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 875 | return 0; |
| 876 | |
| 877 | free: |
| 878 | kfree_skb(skb2); |
| 879 | out: |
| 880 | return err; |
| 881 | } |
| 882 | |
Pablo Neira Ayuso | 6767184 | 2008-10-20 03:34:27 -0700 | [diff] [blame] | 883 | #ifdef CONFIG_NF_NAT_NEEDED |
Ilpo Järvinen | bb5cf80 | 2008-01-05 23:11:31 -0800 | [diff] [blame] | 884 | static int |
Pablo Neira Ayuso | e6a7d3c | 2008-10-14 11:58:31 -0700 | [diff] [blame] | 885 | ctnetlink_parse_nat_setup(struct nf_conn *ct, |
| 886 | enum nf_nat_manip_type manip, |
| 887 | struct nlattr *attr) |
| 888 | { |
| 889 | typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup; |
| 890 | |
| 891 | parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook); |
| 892 | if (!parse_nat_setup) { |
Johannes Berg | 95a5afc | 2008-10-16 15:24:51 -0700 | [diff] [blame] | 893 | #ifdef CONFIG_MODULES |
Pablo Neira Ayuso | e6a7d3c | 2008-10-14 11:58:31 -0700 | [diff] [blame] | 894 | rcu_read_unlock(); |
Patrick McHardy | 748085f | 2009-01-21 12:19:49 -0800 | [diff] [blame] | 895 | spin_unlock_bh(&nf_conntrack_lock); |
Pablo Neira Ayuso | e6a7d3c | 2008-10-14 11:58:31 -0700 | [diff] [blame] | 896 | nfnl_unlock(); |
| 897 | if (request_module("nf-nat-ipv4") < 0) { |
| 898 | nfnl_lock(); |
Patrick McHardy | 748085f | 2009-01-21 12:19:49 -0800 | [diff] [blame] | 899 | spin_lock_bh(&nf_conntrack_lock); |
Pablo Neira Ayuso | e6a7d3c | 2008-10-14 11:58:31 -0700 | [diff] [blame] | 900 | rcu_read_lock(); |
| 901 | return -EOPNOTSUPP; |
| 902 | } |
| 903 | nfnl_lock(); |
Patrick McHardy | 748085f | 2009-01-21 12:19:49 -0800 | [diff] [blame] | 904 | spin_lock_bh(&nf_conntrack_lock); |
Pablo Neira Ayuso | e6a7d3c | 2008-10-14 11:58:31 -0700 | [diff] [blame] | 905 | rcu_read_lock(); |
| 906 | if (nfnetlink_parse_nat_setup_hook) |
| 907 | return -EAGAIN; |
| 908 | #endif |
| 909 | return -EOPNOTSUPP; |
| 910 | } |
| 911 | |
| 912 | return parse_nat_setup(ct, manip, attr); |
| 913 | } |
Pablo Neira Ayuso | 6767184 | 2008-10-20 03:34:27 -0700 | [diff] [blame] | 914 | #endif |
Pablo Neira Ayuso | e6a7d3c | 2008-10-14 11:58:31 -0700 | [diff] [blame] | 915 | |
| 916 | static int |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 917 | ctnetlink_change_status(struct nf_conn *ct, struct nlattr *cda[]) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 918 | { |
| 919 | unsigned long d; |
Patrick McHardy | 77236b6 | 2007-12-17 22:29:45 -0800 | [diff] [blame] | 920 | unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS])); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 921 | d = ct->status ^ status; |
| 922 | |
| 923 | if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING)) |
| 924 | /* unchangeable */ |
Pablo Neira Ayuso | 0adf9d6 | 2008-06-09 15:56:20 -0700 | [diff] [blame] | 925 | return -EBUSY; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 926 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 927 | if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY)) |
| 928 | /* SEEN_REPLY bit can only be set */ |
Pablo Neira Ayuso | 0adf9d6 | 2008-06-09 15:56:20 -0700 | [diff] [blame] | 929 | return -EBUSY; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 930 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 931 | if (d & IPS_ASSURED && !(status & IPS_ASSURED)) |
| 932 | /* ASSURED bit can only be set */ |
Pablo Neira Ayuso | 0adf9d6 | 2008-06-09 15:56:20 -0700 | [diff] [blame] | 933 | return -EBUSY; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 934 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 935 | /* Be careful here, modifying NAT bits can screw up things, |
| 936 | * so don't let users modify them directly if they don't pass |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 937 | * nf_nat_range. */ |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 938 | ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK); |
| 939 | return 0; |
| 940 | } |
| 941 | |
Pablo Neira Ayuso | e6a7d3c | 2008-10-14 11:58:31 -0700 | [diff] [blame] | 942 | static int |
| 943 | ctnetlink_change_nat(struct nf_conn *ct, struct nlattr *cda[]) |
| 944 | { |
| 945 | #ifdef CONFIG_NF_NAT_NEEDED |
| 946 | int ret; |
| 947 | |
| 948 | if (cda[CTA_NAT_DST]) { |
| 949 | ret = ctnetlink_parse_nat_setup(ct, |
| 950 | IP_NAT_MANIP_DST, |
| 951 | cda[CTA_NAT_DST]); |
| 952 | if (ret < 0) |
| 953 | return ret; |
| 954 | } |
| 955 | if (cda[CTA_NAT_SRC]) { |
| 956 | ret = ctnetlink_parse_nat_setup(ct, |
| 957 | IP_NAT_MANIP_SRC, |
| 958 | cda[CTA_NAT_SRC]); |
| 959 | if (ret < 0) |
| 960 | return ret; |
| 961 | } |
| 962 | return 0; |
| 963 | #else |
| 964 | return -EOPNOTSUPP; |
| 965 | #endif |
| 966 | } |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 967 | |
| 968 | static inline int |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 969 | ctnetlink_change_helper(struct nf_conn *ct, struct nlattr *cda[]) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 970 | { |
| 971 | struct nf_conntrack_helper *helper; |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 972 | struct nf_conn_help *help = nfct_help(ct); |
Pablo Neira Ayuso | 29fe1b4 | 2009-04-22 02:26:37 -0700 | [diff] [blame] | 973 | char *helpname = NULL; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 974 | int err; |
| 975 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 976 | /* don't change helper of sibling connections */ |
| 977 | if (ct->master) |
Pablo Neira Ayuso | 0adf9d6 | 2008-06-09 15:56:20 -0700 | [diff] [blame] | 978 | return -EBUSY; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 979 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 980 | err = ctnetlink_parse_help(cda[CTA_HELP], &helpname); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 981 | if (err < 0) |
| 982 | return err; |
| 983 | |
Yasuyuki Kozakai | df293bb | 2007-05-10 14:15:58 -0700 | [diff] [blame] | 984 | if (!strcmp(helpname, "")) { |
| 985 | if (help && help->helper) { |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 986 | /* we had a helper before ... */ |
| 987 | nf_ct_remove_expectations(ct); |
Patrick McHarrdy | 3c158f7 | 2007-06-05 12:55:27 -0700 | [diff] [blame] | 988 | rcu_assign_pointer(help->helper, NULL); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 989 | } |
Yasuyuki Kozakai | df293bb | 2007-05-10 14:15:58 -0700 | [diff] [blame] | 990 | |
| 991 | return 0; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 992 | } |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 993 | |
Yasuyuki Kozakai | df293bb | 2007-05-10 14:15:58 -0700 | [diff] [blame] | 994 | helper = __nf_conntrack_helper_find_byname(helpname); |
Pablo Neira Ayuso | 226c0c0 | 2008-11-18 11:54:05 +0100 | [diff] [blame] | 995 | if (helper == NULL) { |
| 996 | #ifdef CONFIG_MODULES |
| 997 | spin_unlock_bh(&nf_conntrack_lock); |
| 998 | |
| 999 | if (request_module("nfct-helper-%s", helpname) < 0) { |
| 1000 | spin_lock_bh(&nf_conntrack_lock); |
| 1001 | return -EOPNOTSUPP; |
| 1002 | } |
| 1003 | |
| 1004 | spin_lock_bh(&nf_conntrack_lock); |
| 1005 | helper = __nf_conntrack_helper_find_byname(helpname); |
| 1006 | if (helper) |
| 1007 | return -EAGAIN; |
| 1008 | #endif |
Pablo Neira Ayuso | 0adf9d6 | 2008-06-09 15:56:20 -0700 | [diff] [blame] | 1009 | return -EOPNOTSUPP; |
Pablo Neira Ayuso | 226c0c0 | 2008-11-18 11:54:05 +0100 | [diff] [blame] | 1010 | } |
Yasuyuki Kozakai | df293bb | 2007-05-10 14:15:58 -0700 | [diff] [blame] | 1011 | |
Yasuyuki Kozakai | ceceae1 | 2007-07-07 22:23:42 -0700 | [diff] [blame] | 1012 | if (help) { |
| 1013 | if (help->helper == helper) |
| 1014 | return 0; |
| 1015 | if (help->helper) |
| 1016 | return -EBUSY; |
| 1017 | /* need to zero data of old helper */ |
| 1018 | memset(&help->help, 0, sizeof(help->help)); |
| 1019 | } else { |
Pablo Neira Ayuso | fab00c5 | 2008-08-18 21:31:46 -0700 | [diff] [blame] | 1020 | help = nf_ct_helper_ext_add(ct, GFP_ATOMIC); |
Yasuyuki Kozakai | ceceae1 | 2007-07-07 22:23:42 -0700 | [diff] [blame] | 1021 | if (help == NULL) |
| 1022 | return -ENOMEM; |
| 1023 | } |
Yasuyuki Kozakai | df293bb | 2007-05-10 14:15:58 -0700 | [diff] [blame] | 1024 | |
Patrick McHarrdy | 3c158f7 | 2007-06-05 12:55:27 -0700 | [diff] [blame] | 1025 | rcu_assign_pointer(help->helper, helper); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1026 | |
| 1027 | return 0; |
| 1028 | } |
| 1029 | |
| 1030 | static inline int |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1031 | ctnetlink_change_timeout(struct nf_conn *ct, struct nlattr *cda[]) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1032 | { |
Patrick McHardy | 77236b6 | 2007-12-17 22:29:45 -0800 | [diff] [blame] | 1033 | u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT])); |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 1034 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1035 | if (!del_timer(&ct->timeout)) |
| 1036 | return -ETIME; |
| 1037 | |
| 1038 | ct->timeout.expires = jiffies + timeout * HZ; |
| 1039 | add_timer(&ct->timeout); |
| 1040 | |
| 1041 | return 0; |
| 1042 | } |
| 1043 | |
| 1044 | static inline int |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1045 | ctnetlink_change_protoinfo(struct nf_conn *ct, struct nlattr *cda[]) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1046 | { |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1047 | struct nlattr *tb[CTA_PROTOINFO_MAX+1], *attr = cda[CTA_PROTOINFO]; |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 1048 | struct nf_conntrack_l4proto *l4proto; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1049 | int err = 0; |
| 1050 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1051 | nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, NULL); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1052 | |
Florian Westphal | cd91566 | 2009-03-18 17:28:37 +0100 | [diff] [blame] | 1053 | rcu_read_lock(); |
| 1054 | l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct)); |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 1055 | if (l4proto->from_nlattr) |
| 1056 | err = l4proto->from_nlattr(tb, ct); |
Florian Westphal | cd91566 | 2009-03-18 17:28:37 +0100 | [diff] [blame] | 1057 | rcu_read_unlock(); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1058 | |
| 1059 | return err; |
| 1060 | } |
| 1061 | |
Pablo Neira Ayuso | 13eae15 | 2007-12-17 22:28:00 -0800 | [diff] [blame] | 1062 | #ifdef CONFIG_NF_NAT_NEEDED |
| 1063 | static inline int |
| 1064 | change_nat_seq_adj(struct nf_nat_seq *natseq, struct nlattr *attr) |
| 1065 | { |
| 1066 | struct nlattr *cda[CTA_NAT_SEQ_MAX+1]; |
| 1067 | |
| 1068 | nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, NULL); |
| 1069 | |
| 1070 | if (!cda[CTA_NAT_SEQ_CORRECTION_POS]) |
| 1071 | return -EINVAL; |
| 1072 | |
| 1073 | natseq->correction_pos = |
Patrick McHardy | 77236b6 | 2007-12-17 22:29:45 -0800 | [diff] [blame] | 1074 | ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS])); |
Pablo Neira Ayuso | 13eae15 | 2007-12-17 22:28:00 -0800 | [diff] [blame] | 1075 | |
| 1076 | if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE]) |
| 1077 | return -EINVAL; |
| 1078 | |
| 1079 | natseq->offset_before = |
Patrick McHardy | 77236b6 | 2007-12-17 22:29:45 -0800 | [diff] [blame] | 1080 | ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE])); |
Pablo Neira Ayuso | 13eae15 | 2007-12-17 22:28:00 -0800 | [diff] [blame] | 1081 | |
| 1082 | if (!cda[CTA_NAT_SEQ_OFFSET_AFTER]) |
| 1083 | return -EINVAL; |
| 1084 | |
| 1085 | natseq->offset_after = |
Patrick McHardy | 77236b6 | 2007-12-17 22:29:45 -0800 | [diff] [blame] | 1086 | ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER])); |
Pablo Neira Ayuso | 13eae15 | 2007-12-17 22:28:00 -0800 | [diff] [blame] | 1087 | |
| 1088 | return 0; |
| 1089 | } |
| 1090 | |
| 1091 | static int |
| 1092 | ctnetlink_change_nat_seq_adj(struct nf_conn *ct, struct nlattr *cda[]) |
| 1093 | { |
| 1094 | int ret = 0; |
| 1095 | struct nf_conn_nat *nat = nfct_nat(ct); |
| 1096 | |
| 1097 | if (!nat) |
| 1098 | return 0; |
| 1099 | |
| 1100 | if (cda[CTA_NAT_SEQ_ADJ_ORIG]) { |
| 1101 | ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL], |
| 1102 | cda[CTA_NAT_SEQ_ADJ_ORIG]); |
| 1103 | if (ret < 0) |
| 1104 | return ret; |
| 1105 | |
| 1106 | ct->status |= IPS_SEQ_ADJUST; |
| 1107 | } |
| 1108 | |
| 1109 | if (cda[CTA_NAT_SEQ_ADJ_REPLY]) { |
| 1110 | ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY], |
| 1111 | cda[CTA_NAT_SEQ_ADJ_REPLY]); |
| 1112 | if (ret < 0) |
| 1113 | return ret; |
| 1114 | |
| 1115 | ct->status |= IPS_SEQ_ADJUST; |
| 1116 | } |
| 1117 | |
| 1118 | return 0; |
| 1119 | } |
| 1120 | #endif |
| 1121 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1122 | static int |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1123 | ctnetlink_change_conntrack(struct nf_conn *ct, struct nlattr *cda[]) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1124 | { |
| 1125 | int err; |
| 1126 | |
Pablo Neira Ayuso | e098360 | 2009-03-16 15:27:22 +0100 | [diff] [blame] | 1127 | /* only allow NAT changes and master assignation for new conntracks */ |
| 1128 | if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER]) |
| 1129 | return -EOPNOTSUPP; |
| 1130 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1131 | if (cda[CTA_HELP]) { |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1132 | err = ctnetlink_change_helper(ct, cda); |
| 1133 | if (err < 0) |
| 1134 | return err; |
| 1135 | } |
| 1136 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1137 | if (cda[CTA_TIMEOUT]) { |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1138 | err = ctnetlink_change_timeout(ct, cda); |
| 1139 | if (err < 0) |
| 1140 | return err; |
| 1141 | } |
| 1142 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1143 | if (cda[CTA_STATUS]) { |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1144 | err = ctnetlink_change_status(ct, cda); |
| 1145 | if (err < 0) |
| 1146 | return err; |
| 1147 | } |
| 1148 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1149 | if (cda[CTA_PROTOINFO]) { |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1150 | err = ctnetlink_change_protoinfo(ct, cda); |
| 1151 | if (err < 0) |
| 1152 | return err; |
| 1153 | } |
| 1154 | |
Martin Josefsson | bcd1e83 | 2006-04-01 02:23:21 -0800 | [diff] [blame] | 1155 | #if defined(CONFIG_NF_CONNTRACK_MARK) |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1156 | if (cda[CTA_MARK]) |
Patrick McHardy | 77236b6 | 2007-12-17 22:29:45 -0800 | [diff] [blame] | 1157 | ct->mark = ntohl(nla_get_be32(cda[CTA_MARK])); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1158 | #endif |
| 1159 | |
Pablo Neira Ayuso | 13eae15 | 2007-12-17 22:28:00 -0800 | [diff] [blame] | 1160 | #ifdef CONFIG_NF_NAT_NEEDED |
| 1161 | if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) { |
| 1162 | err = ctnetlink_change_nat_seq_adj(ct, cda); |
| 1163 | if (err < 0) |
| 1164 | return err; |
| 1165 | } |
| 1166 | #endif |
| 1167 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1168 | return 0; |
| 1169 | } |
| 1170 | |
Pablo Neira Ayuso | f0a3c08 | 2009-03-16 15:28:09 +0100 | [diff] [blame] | 1171 | static struct nf_conn * |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1172 | ctnetlink_create_conntrack(struct nlattr *cda[], |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1173 | struct nf_conntrack_tuple *otuple, |
Pablo Neira Ayuso | 5faa1f4 | 2007-09-28 14:43:53 -0700 | [diff] [blame] | 1174 | struct nf_conntrack_tuple *rtuple, |
Pablo Neira Ayuso | 7ec4749 | 2009-03-16 15:25:46 +0100 | [diff] [blame] | 1175 | u8 u3) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1176 | { |
| 1177 | struct nf_conn *ct; |
| 1178 | int err = -EINVAL; |
Yasuyuki Kozakai | ceceae1 | 2007-07-07 22:23:42 -0700 | [diff] [blame] | 1179 | struct nf_conntrack_helper *helper; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1180 | |
Patrick McHardy | b54ad40 | 2008-11-24 15:56:17 -0800 | [diff] [blame] | 1181 | ct = nf_conntrack_alloc(&init_net, otuple, rtuple, GFP_ATOMIC); |
Julia Lawall | cd7fcbf | 2009-01-12 00:06:08 +0000 | [diff] [blame] | 1182 | if (IS_ERR(ct)) |
Pablo Neira Ayuso | f0a3c08 | 2009-03-16 15:28:09 +0100 | [diff] [blame] | 1183 | return ERR_PTR(-ENOMEM); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1184 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1185 | if (!cda[CTA_TIMEOUT]) |
Patrick McHardy | 0f5b3e8 | 2009-03-18 17:36:40 +0100 | [diff] [blame] | 1186 | goto err1; |
Patrick McHardy | 77236b6 | 2007-12-17 22:29:45 -0800 | [diff] [blame] | 1187 | ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT])); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1188 | |
| 1189 | ct->timeout.expires = jiffies + ct->timeout.expires * HZ; |
| 1190 | ct->status |= IPS_CONFIRMED; |
| 1191 | |
Patrick McHardy | 58a3c9b | 2008-01-31 04:36:54 -0800 | [diff] [blame] | 1192 | rcu_read_lock(); |
Pablo Neira Ayuso | 226c0c0 | 2008-11-18 11:54:05 +0100 | [diff] [blame] | 1193 | if (cda[CTA_HELP]) { |
Pablo Neira Ayuso | 29fe1b4 | 2009-04-22 02:26:37 -0700 | [diff] [blame] | 1194 | char *helpname = NULL; |
Pablo Neira Ayuso | 226c0c0 | 2008-11-18 11:54:05 +0100 | [diff] [blame] | 1195 | |
| 1196 | err = ctnetlink_parse_help(cda[CTA_HELP], &helpname); |
Patrick McHardy | 0f5b3e8 | 2009-03-18 17:36:40 +0100 | [diff] [blame] | 1197 | if (err < 0) |
| 1198 | goto err2; |
Pablo Neira Ayuso | 226c0c0 | 2008-11-18 11:54:05 +0100 | [diff] [blame] | 1199 | |
| 1200 | helper = __nf_conntrack_helper_find_byname(helpname); |
| 1201 | if (helper == NULL) { |
| 1202 | rcu_read_unlock(); |
| 1203 | #ifdef CONFIG_MODULES |
| 1204 | if (request_module("nfct-helper-%s", helpname) < 0) { |
| 1205 | err = -EOPNOTSUPP; |
Patrick McHardy | 0f5b3e8 | 2009-03-18 17:36:40 +0100 | [diff] [blame] | 1206 | goto err1; |
Pablo Neira Ayuso | 226c0c0 | 2008-11-18 11:54:05 +0100 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | rcu_read_lock(); |
| 1210 | helper = __nf_conntrack_helper_find_byname(helpname); |
| 1211 | if (helper) { |
Pablo Neira Ayuso | 226c0c0 | 2008-11-18 11:54:05 +0100 | [diff] [blame] | 1212 | err = -EAGAIN; |
Patrick McHardy | 0f5b3e8 | 2009-03-18 17:36:40 +0100 | [diff] [blame] | 1213 | goto err2; |
Pablo Neira Ayuso | 226c0c0 | 2008-11-18 11:54:05 +0100 | [diff] [blame] | 1214 | } |
| 1215 | rcu_read_unlock(); |
| 1216 | #endif |
| 1217 | err = -EOPNOTSUPP; |
Patrick McHardy | 0f5b3e8 | 2009-03-18 17:36:40 +0100 | [diff] [blame] | 1218 | goto err1; |
Pablo Neira Ayuso | 226c0c0 | 2008-11-18 11:54:05 +0100 | [diff] [blame] | 1219 | } else { |
| 1220 | struct nf_conn_help *help; |
| 1221 | |
| 1222 | help = nf_ct_helper_ext_add(ct, GFP_ATOMIC); |
| 1223 | if (help == NULL) { |
Pablo Neira Ayuso | 226c0c0 | 2008-11-18 11:54:05 +0100 | [diff] [blame] | 1224 | err = -ENOMEM; |
Patrick McHardy | 0f5b3e8 | 2009-03-18 17:36:40 +0100 | [diff] [blame] | 1225 | goto err2; |
Pablo Neira Ayuso | 226c0c0 | 2008-11-18 11:54:05 +0100 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | /* not in hash table yet so not strictly necessary */ |
| 1229 | rcu_assign_pointer(help->helper, helper); |
| 1230 | } |
| 1231 | } else { |
| 1232 | /* try an implicit helper assignation */ |
| 1233 | err = __nf_ct_try_assign_helper(ct, GFP_ATOMIC); |
Patrick McHardy | 0f5b3e8 | 2009-03-18 17:36:40 +0100 | [diff] [blame] | 1234 | if (err < 0) |
| 1235 | goto err2; |
Patrick McHarrdy | 3c158f7 | 2007-06-05 12:55:27 -0700 | [diff] [blame] | 1236 | } |
Yasuyuki Kozakai | dafc741 | 2006-11-27 10:25:32 -0800 | [diff] [blame] | 1237 | |
Pablo Neira Ayuso | 1575e7e | 2008-08-18 21:30:55 -0700 | [diff] [blame] | 1238 | if (cda[CTA_STATUS]) { |
| 1239 | err = ctnetlink_change_status(ct, cda); |
Patrick McHardy | 0f5b3e8 | 2009-03-18 17:36:40 +0100 | [diff] [blame] | 1240 | if (err < 0) |
| 1241 | goto err2; |
Pablo Neira Ayuso | 1575e7e | 2008-08-18 21:30:55 -0700 | [diff] [blame] | 1242 | } |
| 1243 | |
Pablo Neira Ayuso | e6a7d3c | 2008-10-14 11:58:31 -0700 | [diff] [blame] | 1244 | if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) { |
| 1245 | err = ctnetlink_change_nat(ct, cda); |
Patrick McHardy | 0f5b3e8 | 2009-03-18 17:36:40 +0100 | [diff] [blame] | 1246 | if (err < 0) |
| 1247 | goto err2; |
Pablo Neira Ayuso | e6a7d3c | 2008-10-14 11:58:31 -0700 | [diff] [blame] | 1248 | } |
| 1249 | |
Pablo Neira Ayuso | c969aa7 | 2009-02-09 14:33:57 -0800 | [diff] [blame] | 1250 | #ifdef CONFIG_NF_NAT_NEEDED |
| 1251 | if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) { |
| 1252 | err = ctnetlink_change_nat_seq_adj(ct, cda); |
Patrick McHardy | 0f5b3e8 | 2009-03-18 17:36:40 +0100 | [diff] [blame] | 1253 | if (err < 0) |
| 1254 | goto err2; |
Pablo Neira Ayuso | c969aa7 | 2009-02-09 14:33:57 -0800 | [diff] [blame] | 1255 | } |
| 1256 | #endif |
| 1257 | |
Pablo Neira Ayuso | 1575e7e | 2008-08-18 21:30:55 -0700 | [diff] [blame] | 1258 | if (cda[CTA_PROTOINFO]) { |
| 1259 | err = ctnetlink_change_protoinfo(ct, cda); |
Patrick McHardy | 0f5b3e8 | 2009-03-18 17:36:40 +0100 | [diff] [blame] | 1260 | if (err < 0) |
| 1261 | goto err2; |
Pablo Neira Ayuso | 1575e7e | 2008-08-18 21:30:55 -0700 | [diff] [blame] | 1262 | } |
| 1263 | |
Patrick McHardy | 3ec1925 | 2008-11-26 03:57:44 -0800 | [diff] [blame] | 1264 | nf_ct_acct_ext_add(ct, GFP_ATOMIC); |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 1265 | nf_ct_ecache_ext_add(ct, GFP_ATOMIC); |
Pablo Neira Ayuso | 1575e7e | 2008-08-18 21:30:55 -0700 | [diff] [blame] | 1266 | |
| 1267 | #if defined(CONFIG_NF_CONNTRACK_MARK) |
| 1268 | if (cda[CTA_MARK]) |
| 1269 | ct->mark = ntohl(nla_get_be32(cda[CTA_MARK])); |
| 1270 | #endif |
| 1271 | |
Pablo Neira Ayuso | 5faa1f4 | 2007-09-28 14:43:53 -0700 | [diff] [blame] | 1272 | /* setup master conntrack: this is a confirmed expectation */ |
Pablo Neira Ayuso | 7ec4749 | 2009-03-16 15:25:46 +0100 | [diff] [blame] | 1273 | if (cda[CTA_TUPLE_MASTER]) { |
| 1274 | struct nf_conntrack_tuple master; |
| 1275 | struct nf_conntrack_tuple_hash *master_h; |
| 1276 | struct nf_conn *master_ct; |
| 1277 | |
| 1278 | err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3); |
| 1279 | if (err < 0) |
Patrick McHardy | 0f5b3e8 | 2009-03-18 17:36:40 +0100 | [diff] [blame] | 1280 | goto err2; |
Pablo Neira Ayuso | 7ec4749 | 2009-03-16 15:25:46 +0100 | [diff] [blame] | 1281 | |
Eric Dumazet | ea781f1 | 2009-03-25 21:05:46 +0100 | [diff] [blame] | 1282 | master_h = nf_conntrack_find_get(&init_net, &master); |
Pablo Neira Ayuso | 7ec4749 | 2009-03-16 15:25:46 +0100 | [diff] [blame] | 1283 | if (master_h == NULL) { |
| 1284 | err = -ENOENT; |
Patrick McHardy | 0f5b3e8 | 2009-03-18 17:36:40 +0100 | [diff] [blame] | 1285 | goto err2; |
Pablo Neira Ayuso | 7ec4749 | 2009-03-16 15:25:46 +0100 | [diff] [blame] | 1286 | } |
| 1287 | master_ct = nf_ct_tuplehash_to_ctrack(master_h); |
Pablo Neira Ayuso | f2a8900 | 2007-12-12 10:34:29 -0800 | [diff] [blame] | 1288 | __set_bit(IPS_EXPECTED_BIT, &ct->status); |
Pablo Neira Ayuso | 5faa1f4 | 2007-09-28 14:43:53 -0700 | [diff] [blame] | 1289 | ct->master = master_ct; |
Pablo Neira Ayuso | f2a8900 | 2007-12-12 10:34:29 -0800 | [diff] [blame] | 1290 | } |
Pablo Neira Ayuso | 5faa1f4 | 2007-09-28 14:43:53 -0700 | [diff] [blame] | 1291 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1292 | add_timer(&ct->timeout); |
| 1293 | nf_conntrack_hash_insert(ct); |
Patrick McHardy | 58a3c9b | 2008-01-31 04:36:54 -0800 | [diff] [blame] | 1294 | rcu_read_unlock(); |
Yasuyuki Kozakai | dafc741 | 2006-11-27 10:25:32 -0800 | [diff] [blame] | 1295 | |
Pablo Neira Ayuso | f0a3c08 | 2009-03-16 15:28:09 +0100 | [diff] [blame] | 1296 | return ct; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1297 | |
Patrick McHardy | 0f5b3e8 | 2009-03-18 17:36:40 +0100 | [diff] [blame] | 1298 | err2: |
| 1299 | rcu_read_unlock(); |
| 1300 | err1: |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1301 | nf_conntrack_free(ct); |
Pablo Neira Ayuso | f0a3c08 | 2009-03-16 15:28:09 +0100 | [diff] [blame] | 1302 | return ERR_PTR(err); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1303 | } |
| 1304 | |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 1305 | static int |
| 1306 | ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb, |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1307 | struct nlmsghdr *nlh, struct nlattr *cda[]) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1308 | { |
| 1309 | struct nf_conntrack_tuple otuple, rtuple; |
| 1310 | struct nf_conntrack_tuple_hash *h = NULL; |
Pablo Neira Ayuso | 96bcf93 | 2009-06-02 20:07:39 +0200 | [diff] [blame] | 1311 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1312 | u_int8_t u3 = nfmsg->nfgen_family; |
| 1313 | int err = 0; |
| 1314 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1315 | if (cda[CTA_TUPLE_ORIG]) { |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1316 | err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3); |
| 1317 | if (err < 0) |
| 1318 | return err; |
| 1319 | } |
| 1320 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1321 | if (cda[CTA_TUPLE_REPLY]) { |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1322 | err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3); |
| 1323 | if (err < 0) |
| 1324 | return err; |
| 1325 | } |
| 1326 | |
Patrick McHardy | f8ba1af | 2008-01-31 04:38:58 -0800 | [diff] [blame] | 1327 | spin_lock_bh(&nf_conntrack_lock); |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1328 | if (cda[CTA_TUPLE_ORIG]) |
Alexey Dobriyan | 400dad3 | 2008-10-08 11:35:03 +0200 | [diff] [blame] | 1329 | h = __nf_conntrack_find(&init_net, &otuple); |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1330 | else if (cda[CTA_TUPLE_REPLY]) |
Alexey Dobriyan | 400dad3 | 2008-10-08 11:35:03 +0200 | [diff] [blame] | 1331 | h = __nf_conntrack_find(&init_net, &rtuple); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1332 | |
| 1333 | if (h == NULL) { |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1334 | err = -ENOENT; |
Pablo Neira Ayuso | f0a3c08 | 2009-03-16 15:28:09 +0100 | [diff] [blame] | 1335 | if (nlh->nlmsg_flags & NLM_F_CREATE) { |
| 1336 | struct nf_conn *ct; |
Pablo Neira Ayuso | fecc113 | 2009-05-05 17:48:26 +0200 | [diff] [blame] | 1337 | enum ip_conntrack_events events; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1338 | |
Pablo Neira Ayuso | f0a3c08 | 2009-03-16 15:28:09 +0100 | [diff] [blame] | 1339 | ct = ctnetlink_create_conntrack(cda, &otuple, |
| 1340 | &rtuple, u3); |
| 1341 | if (IS_ERR(ct)) { |
| 1342 | err = PTR_ERR(ct); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1343 | goto out_unlock; |
| 1344 | } |
Pablo Neira Ayuso | f0a3c08 | 2009-03-16 15:28:09 +0100 | [diff] [blame] | 1345 | err = 0; |
| 1346 | nf_conntrack_get(&ct->ct_general); |
| 1347 | spin_unlock_bh(&nf_conntrack_lock); |
Pablo Neira Ayuso | fecc113 | 2009-05-05 17:48:26 +0200 | [diff] [blame] | 1348 | if (test_bit(IPS_EXPECTED_BIT, &ct->status)) |
| 1349 | events = IPCT_RELATED; |
| 1350 | else |
| 1351 | events = IPCT_NEW; |
| 1352 | |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 1353 | nf_conntrack_eventmask_report((1 << IPCT_STATUS) | |
| 1354 | (1 << IPCT_HELPER) | |
| 1355 | (1 << IPCT_PROTOINFO) | |
| 1356 | (1 << IPCT_NATSEQADJ) | |
| 1357 | (1 << IPCT_MARK) | events, |
| 1358 | ct, NETLINK_CB(skb).pid, |
| 1359 | nlmsg_report(nlh)); |
Pablo Neira Ayuso | f0a3c08 | 2009-03-16 15:28:09 +0100 | [diff] [blame] | 1360 | nf_ct_put(ct); |
| 1361 | } else |
| 1362 | spin_unlock_bh(&nf_conntrack_lock); |
Pablo Neira Ayuso | 5faa1f4 | 2007-09-28 14:43:53 -0700 | [diff] [blame] | 1363 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1364 | return err; |
| 1365 | } |
| 1366 | /* implicit 'else' */ |
| 1367 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1368 | /* We manipulate the conntrack inside the global conntrack table lock, |
| 1369 | * so there's no need to increase the refcount */ |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1370 | err = -EEXIST; |
Pablo Neira Ayuso | ff4ca82 | 2007-08-07 18:11:26 -0700 | [diff] [blame] | 1371 | if (!(nlh->nlmsg_flags & NLM_F_EXCL)) { |
Pablo Neira Ayuso | 19abb7b | 2008-11-18 11:56:20 +0100 | [diff] [blame] | 1372 | struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h); |
| 1373 | |
Pablo Neira Ayuso | 19abb7b | 2008-11-18 11:56:20 +0100 | [diff] [blame] | 1374 | err = ctnetlink_change_conntrack(ct, cda); |
| 1375 | if (err == 0) { |
| 1376 | nf_conntrack_get(&ct->ct_general); |
| 1377 | spin_unlock_bh(&nf_conntrack_lock); |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 1378 | nf_conntrack_eventmask_report((1 << IPCT_STATUS) | |
| 1379 | (1 << IPCT_HELPER) | |
| 1380 | (1 << IPCT_PROTOINFO) | |
| 1381 | (1 << IPCT_NATSEQADJ) | |
| 1382 | (1 << IPCT_MARK), |
| 1383 | ct, NETLINK_CB(skb).pid, |
| 1384 | nlmsg_report(nlh)); |
Pablo Neira Ayuso | 19abb7b | 2008-11-18 11:56:20 +0100 | [diff] [blame] | 1385 | nf_ct_put(ct); |
| 1386 | } else |
| 1387 | spin_unlock_bh(&nf_conntrack_lock); |
| 1388 | |
| 1389 | return err; |
Pablo Neira Ayuso | ff4ca82 | 2007-08-07 18:11:26 -0700 | [diff] [blame] | 1390 | } |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1391 | |
| 1392 | out_unlock: |
Patrick McHardy | f8ba1af | 2008-01-31 04:38:58 -0800 | [diff] [blame] | 1393 | spin_unlock_bh(&nf_conntrack_lock); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1394 | return err; |
| 1395 | } |
| 1396 | |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 1397 | /*********************************************************************** |
| 1398 | * EXPECT |
| 1399 | ***********************************************************************/ |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1400 | |
| 1401 | static inline int |
| 1402 | ctnetlink_exp_dump_tuple(struct sk_buff *skb, |
| 1403 | const struct nf_conntrack_tuple *tuple, |
| 1404 | enum ctattr_expect type) |
| 1405 | { |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1406 | struct nlattr *nest_parms; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 1407 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1408 | nest_parms = nla_nest_start(skb, type | NLA_F_NESTED); |
| 1409 | if (!nest_parms) |
| 1410 | goto nla_put_failure; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1411 | if (ctnetlink_dump_tuples(skb, tuple) < 0) |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1412 | goto nla_put_failure; |
| 1413 | nla_nest_end(skb, nest_parms); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1414 | |
| 1415 | return 0; |
| 1416 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1417 | nla_put_failure: |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1418 | return -1; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 1419 | } |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1420 | |
| 1421 | static inline int |
Pablo Neira Ayuso | 1cde643 | 2006-03-22 13:54:15 -0800 | [diff] [blame] | 1422 | ctnetlink_exp_dump_mask(struct sk_buff *skb, |
| 1423 | const struct nf_conntrack_tuple *tuple, |
Patrick McHardy | d4156e8 | 2007-07-07 22:31:32 -0700 | [diff] [blame] | 1424 | const struct nf_conntrack_tuple_mask *mask) |
Pablo Neira Ayuso | 1cde643 | 2006-03-22 13:54:15 -0800 | [diff] [blame] | 1425 | { |
| 1426 | int ret; |
| 1427 | struct nf_conntrack_l3proto *l3proto; |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 1428 | struct nf_conntrack_l4proto *l4proto; |
Patrick McHardy | d4156e8 | 2007-07-07 22:31:32 -0700 | [diff] [blame] | 1429 | struct nf_conntrack_tuple m; |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1430 | struct nlattr *nest_parms; |
Patrick McHardy | d4156e8 | 2007-07-07 22:31:32 -0700 | [diff] [blame] | 1431 | |
| 1432 | memset(&m, 0xFF, sizeof(m)); |
| 1433 | m.src.u.all = mask->src.u.all; |
| 1434 | memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3)); |
| 1435 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1436 | nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED); |
| 1437 | if (!nest_parms) |
| 1438 | goto nla_put_failure; |
Pablo Neira Ayuso | 1cde643 | 2006-03-22 13:54:15 -0800 | [diff] [blame] | 1439 | |
Pablo Neira Ayuso | 528a3a6 | 2008-11-17 16:00:40 +0100 | [diff] [blame] | 1440 | l3proto = __nf_ct_l3proto_find(tuple->src.l3num); |
Patrick McHardy | d4156e8 | 2007-07-07 22:31:32 -0700 | [diff] [blame] | 1441 | ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto); |
Pablo Neira Ayuso | 1cde643 | 2006-03-22 13:54:15 -0800 | [diff] [blame] | 1442 | |
| 1443 | if (unlikely(ret < 0)) |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1444 | goto nla_put_failure; |
Pablo Neira Ayuso | 1cde643 | 2006-03-22 13:54:15 -0800 | [diff] [blame] | 1445 | |
Pablo Neira Ayuso | 528a3a6 | 2008-11-17 16:00:40 +0100 | [diff] [blame] | 1446 | l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum); |
Patrick McHardy | d4156e8 | 2007-07-07 22:31:32 -0700 | [diff] [blame] | 1447 | ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto); |
Pablo Neira Ayuso | 1cde643 | 2006-03-22 13:54:15 -0800 | [diff] [blame] | 1448 | if (unlikely(ret < 0)) |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1449 | goto nla_put_failure; |
Pablo Neira Ayuso | 1cde643 | 2006-03-22 13:54:15 -0800 | [diff] [blame] | 1450 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1451 | nla_nest_end(skb, nest_parms); |
Pablo Neira Ayuso | 1cde643 | 2006-03-22 13:54:15 -0800 | [diff] [blame] | 1452 | |
| 1453 | return 0; |
| 1454 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1455 | nla_put_failure: |
Pablo Neira Ayuso | 1cde643 | 2006-03-22 13:54:15 -0800 | [diff] [blame] | 1456 | return -1; |
| 1457 | } |
| 1458 | |
Ilpo Järvinen | bb5cf80 | 2008-01-05 23:11:31 -0800 | [diff] [blame] | 1459 | static int |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1460 | ctnetlink_exp_dump_expect(struct sk_buff *skb, |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 1461 | const struct nf_conntrack_expect *exp) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1462 | { |
| 1463 | struct nf_conn *master = exp->master; |
Patrick McHardy | d978e5d | 2007-12-17 22:37:03 -0800 | [diff] [blame] | 1464 | long timeout = (exp->timeout.expires - jiffies) / HZ; |
| 1465 | |
| 1466 | if (timeout < 0) |
| 1467 | timeout = 0; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1468 | |
| 1469 | if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0) |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1470 | goto nla_put_failure; |
Pablo Neira Ayuso | 1cde643 | 2006-03-22 13:54:15 -0800 | [diff] [blame] | 1471 | if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0) |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1472 | goto nla_put_failure; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1473 | if (ctnetlink_exp_dump_tuple(skb, |
| 1474 | &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple, |
| 1475 | CTA_EXPECT_MASTER) < 0) |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1476 | goto nla_put_failure; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 1477 | |
Patrick McHardy | d978e5d | 2007-12-17 22:37:03 -0800 | [diff] [blame] | 1478 | NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)); |
Patrick McHardy | 77236b6 | 2007-12-17 22:29:45 -0800 | [diff] [blame] | 1479 | NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1480 | |
| 1481 | return 0; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 1482 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1483 | nla_put_failure: |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1484 | return -1; |
| 1485 | } |
| 1486 | |
| 1487 | static int |
| 1488 | ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq, |
Pablo Neira Ayuso | 8b0a231 | 2009-06-02 20:03:34 +0200 | [diff] [blame] | 1489 | int event, const struct nf_conntrack_expect *exp) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1490 | { |
| 1491 | struct nlmsghdr *nlh; |
| 1492 | struct nfgenmsg *nfmsg; |
Pablo Neira Ayuso | 96bcf93 | 2009-06-02 20:07:39 +0200 | [diff] [blame] | 1493 | unsigned int flags = pid ? NLM_F_MULTI : 0; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1494 | |
| 1495 | event |= NFNL_SUBSYS_CTNETLINK_EXP << 8; |
Pablo Neira Ayuso | 96bcf93 | 2009-06-02 20:07:39 +0200 | [diff] [blame] | 1496 | nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags); |
| 1497 | if (nlh == NULL) |
| 1498 | goto nlmsg_failure; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1499 | |
Pablo Neira Ayuso | 96bcf93 | 2009-06-02 20:07:39 +0200 | [diff] [blame] | 1500 | nfmsg = nlmsg_data(nlh); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1501 | nfmsg->nfgen_family = exp->tuple.src.l3num; |
| 1502 | nfmsg->version = NFNETLINK_V0; |
| 1503 | nfmsg->res_id = 0; |
| 1504 | |
| 1505 | if (ctnetlink_exp_dump_expect(skb, exp) < 0) |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1506 | goto nla_put_failure; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1507 | |
Pablo Neira Ayuso | 96bcf93 | 2009-06-02 20:07:39 +0200 | [diff] [blame] | 1508 | nlmsg_end(skb, nlh); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1509 | return skb->len; |
| 1510 | |
| 1511 | nlmsg_failure: |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1512 | nla_put_failure: |
Pablo Neira Ayuso | 96bcf93 | 2009-06-02 20:07:39 +0200 | [diff] [blame] | 1513 | nlmsg_cancel(skb, nlh); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1514 | return -1; |
| 1515 | } |
| 1516 | |
| 1517 | #ifdef CONFIG_NF_CONNTRACK_EVENTS |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 1518 | static int |
| 1519 | ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1520 | { |
| 1521 | struct nlmsghdr *nlh; |
| 1522 | struct nfgenmsg *nfmsg; |
Pablo Neira Ayuso | 19abb7b | 2008-11-18 11:56:20 +0100 | [diff] [blame] | 1523 | struct nf_conntrack_expect *exp = item->exp; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1524 | struct sk_buff *skb; |
| 1525 | unsigned int type; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1526 | int flags = 0; |
| 1527 | |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 1528 | if (events & (1 << IPEXP_NEW)) { |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1529 | type = IPCTNL_MSG_EXP_NEW; |
| 1530 | flags = NLM_F_CREATE|NLM_F_EXCL; |
| 1531 | } else |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 1532 | return 0; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1533 | |
Pablo Neira Ayuso | 1f9da25 | 2009-02-09 14:34:26 -0800 | [diff] [blame] | 1534 | if (!item->report && |
| 1535 | !nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW)) |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 1536 | return 0; |
Pablo Neira Ayuso | b3a27bf | 2006-08-22 00:32:05 -0700 | [diff] [blame] | 1537 | |
Pablo Neira Ayuso | 96bcf93 | 2009-06-02 20:07:39 +0200 | [diff] [blame] | 1538 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
| 1539 | if (skb == NULL) |
Pablo Neira Ayuso | 150ace0 | 2009-04-17 17:47:31 +0200 | [diff] [blame] | 1540 | goto errout; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1541 | |
Marcus Sundberg | b633ad5 | 2006-02-04 02:11:09 -0800 | [diff] [blame] | 1542 | type |= NFNL_SUBSYS_CTNETLINK_EXP << 8; |
Pablo Neira Ayuso | 96bcf93 | 2009-06-02 20:07:39 +0200 | [diff] [blame] | 1543 | nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags); |
| 1544 | if (nlh == NULL) |
| 1545 | goto nlmsg_failure; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1546 | |
Pablo Neira Ayuso | 96bcf93 | 2009-06-02 20:07:39 +0200 | [diff] [blame] | 1547 | nfmsg = nlmsg_data(nlh); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1548 | nfmsg->nfgen_family = exp->tuple.src.l3num; |
| 1549 | nfmsg->version = NFNETLINK_V0; |
| 1550 | nfmsg->res_id = 0; |
| 1551 | |
Pablo Neira Ayuso | 528a3a6 | 2008-11-17 16:00:40 +0100 | [diff] [blame] | 1552 | rcu_read_lock(); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1553 | if (ctnetlink_exp_dump_expect(skb, exp) < 0) |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1554 | goto nla_put_failure; |
Pablo Neira Ayuso | 528a3a6 | 2008-11-17 16:00:40 +0100 | [diff] [blame] | 1555 | rcu_read_unlock(); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1556 | |
Pablo Neira Ayuso | 96bcf93 | 2009-06-02 20:07:39 +0200 | [diff] [blame] | 1557 | nlmsg_end(skb, nlh); |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 1558 | nfnetlink_send(skb, item->pid, NFNLGRP_CONNTRACK_EXP_NEW, |
| 1559 | item->report, GFP_ATOMIC); |
| 1560 | return 0; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1561 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1562 | nla_put_failure: |
Pablo Neira Ayuso | 528a3a6 | 2008-11-17 16:00:40 +0100 | [diff] [blame] | 1563 | rcu_read_unlock(); |
Pablo Neira Ayuso | 96bcf93 | 2009-06-02 20:07:39 +0200 | [diff] [blame] | 1564 | nlmsg_cancel(skb, nlh); |
Pablo Neira Ayuso | 528a3a6 | 2008-11-17 16:00:40 +0100 | [diff] [blame] | 1565 | nlmsg_failure: |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1566 | kfree_skb(skb); |
Pablo Neira Ayuso | 150ace0 | 2009-04-17 17:47:31 +0200 | [diff] [blame] | 1567 | errout: |
| 1568 | nfnetlink_set_err(0, 0, -ENOBUFS); |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 1569 | return 0; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1570 | } |
| 1571 | #endif |
Patrick McHardy | cf6994c | 2007-07-07 22:32:34 -0700 | [diff] [blame] | 1572 | static int ctnetlink_exp_done(struct netlink_callback *cb) |
| 1573 | { |
Patrick McHardy | 31f1587 | 2007-07-07 22:35:21 -0700 | [diff] [blame] | 1574 | if (cb->args[1]) |
| 1575 | nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]); |
Patrick McHardy | cf6994c | 2007-07-07 22:32:34 -0700 | [diff] [blame] | 1576 | return 0; |
| 1577 | } |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1578 | |
| 1579 | static int |
| 1580 | ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb) |
| 1581 | { |
Alexey Dobriyan | 9b03f38 | 2008-10-08 11:35:03 +0200 | [diff] [blame] | 1582 | struct net *net = &init_net; |
Patrick McHardy | cf6994c | 2007-07-07 22:32:34 -0700 | [diff] [blame] | 1583 | struct nf_conntrack_expect *exp, *last; |
Pablo Neira Ayuso | 96bcf93 | 2009-06-02 20:07:39 +0200 | [diff] [blame] | 1584 | struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); |
Patrick McHardy | 31f1587 | 2007-07-07 22:35:21 -0700 | [diff] [blame] | 1585 | struct hlist_node *n; |
Pablo Neira Ayuso | 87711cb | 2006-01-05 12:19:23 -0800 | [diff] [blame] | 1586 | u_int8_t l3proto = nfmsg->nfgen_family; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1587 | |
Patrick McHardy | 7d0742d | 2008-01-31 04:38:19 -0800 | [diff] [blame] | 1588 | rcu_read_lock(); |
Patrick McHardy | 31f1587 | 2007-07-07 22:35:21 -0700 | [diff] [blame] | 1589 | last = (struct nf_conntrack_expect *)cb->args[1]; |
| 1590 | for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) { |
Patrick McHardy | cf6994c | 2007-07-07 22:32:34 -0700 | [diff] [blame] | 1591 | restart: |
Alexey Dobriyan | 9b03f38 | 2008-10-08 11:35:03 +0200 | [diff] [blame] | 1592 | hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]], |
Patrick McHardy | 31f1587 | 2007-07-07 22:35:21 -0700 | [diff] [blame] | 1593 | hnode) { |
| 1594 | if (l3proto && exp->tuple.src.l3num != l3proto) |
Patrick McHardy | cf6994c | 2007-07-07 22:32:34 -0700 | [diff] [blame] | 1595 | continue; |
Patrick McHardy | 31f1587 | 2007-07-07 22:35:21 -0700 | [diff] [blame] | 1596 | if (cb->args[1]) { |
| 1597 | if (exp != last) |
| 1598 | continue; |
| 1599 | cb->args[1] = 0; |
| 1600 | } |
Pablo Neira Ayuso | 8b0a231 | 2009-06-02 20:03:34 +0200 | [diff] [blame] | 1601 | if (ctnetlink_exp_fill_info(skb, |
| 1602 | NETLINK_CB(cb->skb).pid, |
Patrick McHardy | 31f1587 | 2007-07-07 22:35:21 -0700 | [diff] [blame] | 1603 | cb->nlh->nlmsg_seq, |
| 1604 | IPCTNL_MSG_EXP_NEW, |
Pablo Neira Ayuso | 8b0a231 | 2009-06-02 20:03:34 +0200 | [diff] [blame] | 1605 | exp) < 0) { |
Patrick McHardy | 7d0742d | 2008-01-31 04:38:19 -0800 | [diff] [blame] | 1606 | if (!atomic_inc_not_zero(&exp->use)) |
| 1607 | continue; |
Patrick McHardy | 31f1587 | 2007-07-07 22:35:21 -0700 | [diff] [blame] | 1608 | cb->args[1] = (unsigned long)exp; |
| 1609 | goto out; |
| 1610 | } |
Patrick McHardy | cf6994c | 2007-07-07 22:32:34 -0700 | [diff] [blame] | 1611 | } |
Patrick McHardy | 31f1587 | 2007-07-07 22:35:21 -0700 | [diff] [blame] | 1612 | if (cb->args[1]) { |
| 1613 | cb->args[1] = 0; |
| 1614 | goto restart; |
Patrick McHardy | cf6994c | 2007-07-07 22:32:34 -0700 | [diff] [blame] | 1615 | } |
| 1616 | } |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 1617 | out: |
Patrick McHardy | 7d0742d | 2008-01-31 04:38:19 -0800 | [diff] [blame] | 1618 | rcu_read_unlock(); |
Patrick McHardy | cf6994c | 2007-07-07 22:32:34 -0700 | [diff] [blame] | 1619 | if (last) |
| 1620 | nf_ct_expect_put(last); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1621 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1622 | return skb->len; |
| 1623 | } |
| 1624 | |
Patrick McHardy | f73e924 | 2007-09-28 14:39:55 -0700 | [diff] [blame] | 1625 | static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = { |
| 1626 | [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 }, |
| 1627 | [CTA_EXPECT_ID] = { .type = NLA_U32 }, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1628 | }; |
| 1629 | |
| 1630 | static int |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 1631 | ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb, |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1632 | struct nlmsghdr *nlh, struct nlattr *cda[]) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1633 | { |
| 1634 | struct nf_conntrack_tuple tuple; |
| 1635 | struct nf_conntrack_expect *exp; |
| 1636 | struct sk_buff *skb2; |
Pablo Neira Ayuso | 96bcf93 | 2009-06-02 20:07:39 +0200 | [diff] [blame] | 1637 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1638 | u_int8_t u3 = nfmsg->nfgen_family; |
| 1639 | int err = 0; |
| 1640 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1641 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
Thomas Graf | c702e80 | 2007-03-22 23:30:55 -0700 | [diff] [blame] | 1642 | return netlink_dump_start(ctnl, skb, nlh, |
| 1643 | ctnetlink_exp_dump_table, |
Patrick McHardy | cf6994c | 2007-07-07 22:32:34 -0700 | [diff] [blame] | 1644 | ctnetlink_exp_done); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1645 | } |
| 1646 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1647 | if (cda[CTA_EXPECT_MASTER]) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1648 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3); |
| 1649 | else |
| 1650 | return -EINVAL; |
| 1651 | |
| 1652 | if (err < 0) |
| 1653 | return err; |
| 1654 | |
Alexey Dobriyan | 9b03f38 | 2008-10-08 11:35:03 +0200 | [diff] [blame] | 1655 | exp = nf_ct_expect_find_get(&init_net, &tuple); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1656 | if (!exp) |
| 1657 | return -ENOENT; |
| 1658 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1659 | if (cda[CTA_EXPECT_ID]) { |
Patrick McHardy | 77236b6 | 2007-12-17 22:29:45 -0800 | [diff] [blame] | 1660 | __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]); |
Patrick McHardy | 3583240 | 2007-09-28 14:41:50 -0700 | [diff] [blame] | 1661 | if (ntohl(id) != (u32)(unsigned long)exp) { |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 1662 | nf_ct_expect_put(exp); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1663 | return -ENOENT; |
| 1664 | } |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 1665 | } |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1666 | |
| 1667 | err = -ENOMEM; |
Pablo Neira Ayuso | 96bcf93 | 2009-06-02 20:07:39 +0200 | [diff] [blame] | 1668 | skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1669 | if (skb2 == NULL) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1670 | goto out; |
Thomas Graf | 4e9b826 | 2006-11-27 09:25:58 -0800 | [diff] [blame] | 1671 | |
Pablo Neira Ayuso | 528a3a6 | 2008-11-17 16:00:40 +0100 | [diff] [blame] | 1672 | rcu_read_lock(); |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 1673 | err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid, |
Pablo Neira Ayuso | 8b0a231 | 2009-06-02 20:03:34 +0200 | [diff] [blame] | 1674 | nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp); |
Pablo Neira Ayuso | 528a3a6 | 2008-11-17 16:00:40 +0100 | [diff] [blame] | 1675 | rcu_read_unlock(); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1676 | if (err <= 0) |
| 1677 | goto free; |
| 1678 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 1679 | nf_ct_expect_put(exp); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1680 | |
| 1681 | return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT); |
| 1682 | |
| 1683 | free: |
| 1684 | kfree_skb(skb2); |
| 1685 | out: |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 1686 | nf_ct_expect_put(exp); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1687 | return err; |
| 1688 | } |
| 1689 | |
| 1690 | static int |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 1691 | ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb, |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1692 | struct nlmsghdr *nlh, struct nlattr *cda[]) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1693 | { |
Patrick McHardy | 31f1587 | 2007-07-07 22:35:21 -0700 | [diff] [blame] | 1694 | struct nf_conntrack_expect *exp; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1695 | struct nf_conntrack_tuple tuple; |
| 1696 | struct nf_conntrack_helper *h; |
Pablo Neira Ayuso | 96bcf93 | 2009-06-02 20:07:39 +0200 | [diff] [blame] | 1697 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
Patrick McHardy | 31f1587 | 2007-07-07 22:35:21 -0700 | [diff] [blame] | 1698 | struct hlist_node *n, *next; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1699 | u_int8_t u3 = nfmsg->nfgen_family; |
Patrick McHardy | 31f1587 | 2007-07-07 22:35:21 -0700 | [diff] [blame] | 1700 | unsigned int i; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1701 | int err; |
| 1702 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1703 | if (cda[CTA_EXPECT_TUPLE]) { |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1704 | /* delete a single expect by tuple */ |
| 1705 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); |
| 1706 | if (err < 0) |
| 1707 | return err; |
| 1708 | |
| 1709 | /* bump usage count to 2 */ |
Alexey Dobriyan | 9b03f38 | 2008-10-08 11:35:03 +0200 | [diff] [blame] | 1710 | exp = nf_ct_expect_find_get(&init_net, &tuple); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1711 | if (!exp) |
| 1712 | return -ENOENT; |
| 1713 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1714 | if (cda[CTA_EXPECT_ID]) { |
Patrick McHardy | 77236b6 | 2007-12-17 22:29:45 -0800 | [diff] [blame] | 1715 | __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]); |
Patrick McHardy | 3583240 | 2007-09-28 14:41:50 -0700 | [diff] [blame] | 1716 | if (ntohl(id) != (u32)(unsigned long)exp) { |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 1717 | nf_ct_expect_put(exp); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1718 | return -ENOENT; |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | /* after list removal, usage count == 1 */ |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 1723 | nf_ct_unexpect_related(exp); |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 1724 | /* have to put what we 'get' above. |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1725 | * after this line usage count == 0 */ |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 1726 | nf_ct_expect_put(exp); |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1727 | } else if (cda[CTA_EXPECT_HELP_NAME]) { |
| 1728 | char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]); |
Patrick McHardy | 31f1587 | 2007-07-07 22:35:21 -0700 | [diff] [blame] | 1729 | struct nf_conn_help *m_help; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1730 | |
| 1731 | /* delete all expectations for this helper */ |
Patrick McHardy | f8ba1af | 2008-01-31 04:38:58 -0800 | [diff] [blame] | 1732 | spin_lock_bh(&nf_conntrack_lock); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1733 | h = __nf_conntrack_helper_find_byname(name); |
| 1734 | if (!h) { |
Patrick McHardy | f8ba1af | 2008-01-31 04:38:58 -0800 | [diff] [blame] | 1735 | spin_unlock_bh(&nf_conntrack_lock); |
Pablo Neira Ayuso | 0adf9d6 | 2008-06-09 15:56:20 -0700 | [diff] [blame] | 1736 | return -EOPNOTSUPP; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1737 | } |
Patrick McHardy | 31f1587 | 2007-07-07 22:35:21 -0700 | [diff] [blame] | 1738 | for (i = 0; i < nf_ct_expect_hsize; i++) { |
| 1739 | hlist_for_each_entry_safe(exp, n, next, |
Alexey Dobriyan | 9b03f38 | 2008-10-08 11:35:03 +0200 | [diff] [blame] | 1740 | &init_net.ct.expect_hash[i], |
Patrick McHardy | 31f1587 | 2007-07-07 22:35:21 -0700 | [diff] [blame] | 1741 | hnode) { |
| 1742 | m_help = nfct_help(exp->master); |
| 1743 | if (m_help->helper == h |
| 1744 | && del_timer(&exp->timeout)) { |
| 1745 | nf_ct_unlink_expect(exp); |
| 1746 | nf_ct_expect_put(exp); |
| 1747 | } |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1748 | } |
| 1749 | } |
Patrick McHardy | f8ba1af | 2008-01-31 04:38:58 -0800 | [diff] [blame] | 1750 | spin_unlock_bh(&nf_conntrack_lock); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1751 | } else { |
| 1752 | /* This basically means we have to flush everything*/ |
Patrick McHardy | f8ba1af | 2008-01-31 04:38:58 -0800 | [diff] [blame] | 1753 | spin_lock_bh(&nf_conntrack_lock); |
Patrick McHardy | 31f1587 | 2007-07-07 22:35:21 -0700 | [diff] [blame] | 1754 | for (i = 0; i < nf_ct_expect_hsize; i++) { |
| 1755 | hlist_for_each_entry_safe(exp, n, next, |
Alexey Dobriyan | 9b03f38 | 2008-10-08 11:35:03 +0200 | [diff] [blame] | 1756 | &init_net.ct.expect_hash[i], |
Patrick McHardy | 31f1587 | 2007-07-07 22:35:21 -0700 | [diff] [blame] | 1757 | hnode) { |
| 1758 | if (del_timer(&exp->timeout)) { |
| 1759 | nf_ct_unlink_expect(exp); |
| 1760 | nf_ct_expect_put(exp); |
| 1761 | } |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1762 | } |
| 1763 | } |
Patrick McHardy | f8ba1af | 2008-01-31 04:38:58 -0800 | [diff] [blame] | 1764 | spin_unlock_bh(&nf_conntrack_lock); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1765 | } |
| 1766 | |
| 1767 | return 0; |
| 1768 | } |
| 1769 | static int |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1770 | ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nlattr *cda[]) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1771 | { |
| 1772 | return -EOPNOTSUPP; |
| 1773 | } |
| 1774 | |
| 1775 | static int |
Pablo Neira Ayuso | 19abb7b | 2008-11-18 11:56:20 +0100 | [diff] [blame] | 1776 | ctnetlink_create_expect(struct nlattr *cda[], u_int8_t u3, u32 pid, int report) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1777 | { |
| 1778 | struct nf_conntrack_tuple tuple, mask, master_tuple; |
| 1779 | struct nf_conntrack_tuple_hash *h = NULL; |
| 1780 | struct nf_conntrack_expect *exp; |
| 1781 | struct nf_conn *ct; |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 1782 | struct nf_conn_help *help; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1783 | int err = 0; |
| 1784 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1785 | /* caller guarantees that those three CTA_EXPECT_* exist */ |
| 1786 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); |
| 1787 | if (err < 0) |
| 1788 | return err; |
| 1789 | err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3); |
| 1790 | if (err < 0) |
| 1791 | return err; |
| 1792 | err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3); |
| 1793 | if (err < 0) |
| 1794 | return err; |
| 1795 | |
| 1796 | /* Look for master conntrack of this expectation */ |
Alexey Dobriyan | 400dad3 | 2008-10-08 11:35:03 +0200 | [diff] [blame] | 1797 | h = nf_conntrack_find_get(&init_net, &master_tuple); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1798 | if (!h) |
| 1799 | return -ENOENT; |
| 1800 | ct = nf_ct_tuplehash_to_ctrack(h); |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 1801 | help = nfct_help(ct); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1802 | |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 1803 | if (!help || !help->helper) { |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1804 | /* such conntrack hasn't got any helper, abort */ |
Pablo Neira Ayuso | bfe2967 | 2008-11-17 15:55:48 +0100 | [diff] [blame] | 1805 | err = -EOPNOTSUPP; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1806 | goto out; |
| 1807 | } |
| 1808 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 1809 | exp = nf_ct_expect_alloc(ct); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1810 | if (!exp) { |
| 1811 | err = -ENOMEM; |
| 1812 | goto out; |
| 1813 | } |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 1814 | |
Pablo Neira Ayuso | 626ba8f | 2009-03-16 15:50:51 +0100 | [diff] [blame] | 1815 | exp->class = 0; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1816 | exp->expectfn = NULL; |
| 1817 | exp->flags = 0; |
| 1818 | exp->master = ct; |
Patrick McHardy | 9457d85 | 2006-12-02 22:05:25 -0800 | [diff] [blame] | 1819 | exp->helper = NULL; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1820 | memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple)); |
Patrick McHardy | d4156e8 | 2007-07-07 22:31:32 -0700 | [diff] [blame] | 1821 | memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3)); |
| 1822 | exp->mask.src.u.all = mask.src.u.all; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1823 | |
Pablo Neira Ayuso | 19abb7b | 2008-11-18 11:56:20 +0100 | [diff] [blame] | 1824 | err = nf_ct_expect_related_report(exp, pid, report); |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 1825 | nf_ct_expect_put(exp); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1826 | |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 1827 | out: |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1828 | nf_ct_put(nf_ct_tuplehash_to_ctrack(h)); |
| 1829 | return err; |
| 1830 | } |
| 1831 | |
| 1832 | static int |
| 1833 | ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb, |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1834 | struct nlmsghdr *nlh, struct nlattr *cda[]) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1835 | { |
| 1836 | struct nf_conntrack_tuple tuple; |
| 1837 | struct nf_conntrack_expect *exp; |
Pablo Neira Ayuso | 96bcf93 | 2009-06-02 20:07:39 +0200 | [diff] [blame] | 1838 | struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1839 | u_int8_t u3 = nfmsg->nfgen_family; |
| 1840 | int err = 0; |
| 1841 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 1842 | if (!cda[CTA_EXPECT_TUPLE] |
| 1843 | || !cda[CTA_EXPECT_MASK] |
| 1844 | || !cda[CTA_EXPECT_MASTER]) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1845 | return -EINVAL; |
| 1846 | |
| 1847 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3); |
| 1848 | if (err < 0) |
| 1849 | return err; |
| 1850 | |
Patrick McHardy | f8ba1af | 2008-01-31 04:38:58 -0800 | [diff] [blame] | 1851 | spin_lock_bh(&nf_conntrack_lock); |
Alexey Dobriyan | 9b03f38 | 2008-10-08 11:35:03 +0200 | [diff] [blame] | 1852 | exp = __nf_ct_expect_find(&init_net, &tuple); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1853 | |
| 1854 | if (!exp) { |
Patrick McHardy | f8ba1af | 2008-01-31 04:38:58 -0800 | [diff] [blame] | 1855 | spin_unlock_bh(&nf_conntrack_lock); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1856 | err = -ENOENT; |
Pablo Neira Ayuso | 19abb7b | 2008-11-18 11:56:20 +0100 | [diff] [blame] | 1857 | if (nlh->nlmsg_flags & NLM_F_CREATE) { |
| 1858 | err = ctnetlink_create_expect(cda, |
| 1859 | u3, |
| 1860 | NETLINK_CB(skb).pid, |
| 1861 | nlmsg_report(nlh)); |
| 1862 | } |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1863 | return err; |
| 1864 | } |
| 1865 | |
| 1866 | err = -EEXIST; |
| 1867 | if (!(nlh->nlmsg_flags & NLM_F_EXCL)) |
| 1868 | err = ctnetlink_change_expect(exp, cda); |
Patrick McHardy | f8ba1af | 2008-01-31 04:38:58 -0800 | [diff] [blame] | 1869 | spin_unlock_bh(&nf_conntrack_lock); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1870 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1871 | return err; |
| 1872 | } |
| 1873 | |
| 1874 | #ifdef CONFIG_NF_CONNTRACK_EVENTS |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 1875 | static struct nf_ct_event_notifier ctnl_notifier = { |
| 1876 | .fcn = ctnetlink_conntrack_event, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1877 | }; |
| 1878 | |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 1879 | static struct nf_exp_event_notifier ctnl_notifier_exp = { |
| 1880 | .fcn = ctnetlink_expect_event, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1881 | }; |
| 1882 | #endif |
| 1883 | |
Patrick McHardy | 7c8d4cb | 2007-09-28 14:15:45 -0700 | [diff] [blame] | 1884 | static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = { |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1885 | [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack, |
Patrick McHardy | f73e924 | 2007-09-28 14:39:55 -0700 | [diff] [blame] | 1886 | .attr_count = CTA_MAX, |
| 1887 | .policy = ct_nla_policy }, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1888 | [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack, |
Patrick McHardy | f73e924 | 2007-09-28 14:39:55 -0700 | [diff] [blame] | 1889 | .attr_count = CTA_MAX, |
| 1890 | .policy = ct_nla_policy }, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1891 | [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack, |
Patrick McHardy | f73e924 | 2007-09-28 14:39:55 -0700 | [diff] [blame] | 1892 | .attr_count = CTA_MAX, |
| 1893 | .policy = ct_nla_policy }, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1894 | [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack, |
Patrick McHardy | f73e924 | 2007-09-28 14:39:55 -0700 | [diff] [blame] | 1895 | .attr_count = CTA_MAX, |
| 1896 | .policy = ct_nla_policy }, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1897 | }; |
| 1898 | |
Patrick McHardy | 7c8d4cb | 2007-09-28 14:15:45 -0700 | [diff] [blame] | 1899 | static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = { |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1900 | [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect, |
Patrick McHardy | f73e924 | 2007-09-28 14:39:55 -0700 | [diff] [blame] | 1901 | .attr_count = CTA_EXPECT_MAX, |
| 1902 | .policy = exp_nla_policy }, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1903 | [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect, |
Patrick McHardy | f73e924 | 2007-09-28 14:39:55 -0700 | [diff] [blame] | 1904 | .attr_count = CTA_EXPECT_MAX, |
| 1905 | .policy = exp_nla_policy }, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1906 | [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect, |
Patrick McHardy | f73e924 | 2007-09-28 14:39:55 -0700 | [diff] [blame] | 1907 | .attr_count = CTA_EXPECT_MAX, |
| 1908 | .policy = exp_nla_policy }, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1909 | }; |
| 1910 | |
Patrick McHardy | 7c8d4cb | 2007-09-28 14:15:45 -0700 | [diff] [blame] | 1911 | static const struct nfnetlink_subsystem ctnl_subsys = { |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1912 | .name = "conntrack", |
| 1913 | .subsys_id = NFNL_SUBSYS_CTNETLINK, |
| 1914 | .cb_count = IPCTNL_MSG_MAX, |
| 1915 | .cb = ctnl_cb, |
| 1916 | }; |
| 1917 | |
Patrick McHardy | 7c8d4cb | 2007-09-28 14:15:45 -0700 | [diff] [blame] | 1918 | static const struct nfnetlink_subsystem ctnl_exp_subsys = { |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1919 | .name = "conntrack_expect", |
| 1920 | .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP, |
| 1921 | .cb_count = IPCTNL_MSG_EXP_MAX, |
| 1922 | .cb = ctnl_exp_cb, |
| 1923 | }; |
| 1924 | |
Patrick McHardy | d2483dd | 2006-12-02 22:06:05 -0800 | [diff] [blame] | 1925 | MODULE_ALIAS("ip_conntrack_netlink"); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1926 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK); |
Pablo Neira Ayuso | 34f9a2e | 2006-02-04 02:11:41 -0800 | [diff] [blame] | 1927 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1928 | |
| 1929 | static int __init ctnetlink_init(void) |
| 1930 | { |
| 1931 | int ret; |
| 1932 | |
| 1933 | printk("ctnetlink v%s: registering with nfnetlink.\n", version); |
| 1934 | ret = nfnetlink_subsys_register(&ctnl_subsys); |
| 1935 | if (ret < 0) { |
| 1936 | printk("ctnetlink_init: cannot register with nfnetlink.\n"); |
| 1937 | goto err_out; |
| 1938 | } |
| 1939 | |
| 1940 | ret = nfnetlink_subsys_register(&ctnl_exp_subsys); |
| 1941 | if (ret < 0) { |
| 1942 | printk("ctnetlink_init: cannot register exp with nfnetlink.\n"); |
| 1943 | goto err_unreg_subsys; |
| 1944 | } |
| 1945 | |
| 1946 | #ifdef CONFIG_NF_CONNTRACK_EVENTS |
| 1947 | ret = nf_conntrack_register_notifier(&ctnl_notifier); |
| 1948 | if (ret < 0) { |
| 1949 | printk("ctnetlink_init: cannot register notifier.\n"); |
| 1950 | goto err_unreg_exp_subsys; |
| 1951 | } |
| 1952 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 1953 | ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1954 | if (ret < 0) { |
| 1955 | printk("ctnetlink_init: cannot expect register notifier.\n"); |
| 1956 | goto err_unreg_notifier; |
| 1957 | } |
| 1958 | #endif |
| 1959 | |
| 1960 | return 0; |
| 1961 | |
| 1962 | #ifdef CONFIG_NF_CONNTRACK_EVENTS |
| 1963 | err_unreg_notifier: |
| 1964 | nf_conntrack_unregister_notifier(&ctnl_notifier); |
| 1965 | err_unreg_exp_subsys: |
| 1966 | nfnetlink_subsys_unregister(&ctnl_exp_subsys); |
| 1967 | #endif |
| 1968 | err_unreg_subsys: |
| 1969 | nfnetlink_subsys_unregister(&ctnl_subsys); |
| 1970 | err_out: |
| 1971 | return ret; |
| 1972 | } |
| 1973 | |
| 1974 | static void __exit ctnetlink_exit(void) |
| 1975 | { |
| 1976 | printk("ctnetlink: unregistering from nfnetlink.\n"); |
| 1977 | |
| 1978 | #ifdef CONFIG_NF_CONNTRACK_EVENTS |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 1979 | nf_ct_expect_unregister_notifier(&ctnl_notifier_exp); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1980 | nf_conntrack_unregister_notifier(&ctnl_notifier); |
| 1981 | #endif |
| 1982 | |
| 1983 | nfnetlink_subsys_unregister(&ctnl_exp_subsys); |
| 1984 | nfnetlink_subsys_unregister(&ctnl_subsys); |
| 1985 | return; |
| 1986 | } |
| 1987 | |
| 1988 | module_init(ctnetlink_init); |
| 1989 | module_exit(ctnetlink_exit); |