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