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