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