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