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