Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
f09943fe | 2 | /* |
f09943fe PM |
3 | * Connection tracking protocol helper module for GRE. |
4 | * | |
5 | * GRE is a generic encapsulation protocol, which is generally not very | |
6 | * suited for NAT, as it has no protocol-specific part as port numbers. | |
7 | * | |
8 | * It has an optional key field, which may help us distinguishing two | |
9 | * connections between the same two hosts. | |
10 | * | |
11 | * GRE is defined in RFC 1701 and RFC 1702, as well as RFC 2784 | |
12 | * | |
13 | * PPTP is built on top of a modified version of GRE, and has a mandatory | |
14 | * field called "CallID", which serves us for the same purpose as the key | |
15 | * field in plain GRE. | |
16 | * | |
17 | * Documentation about PPTP can be found in RFC 2637 | |
18 | * | |
19 | * (C) 2000-2005 by Harald Welte <laforge@gnumonks.org> | |
20 | * | |
21 | * Development of this code funded by Astaro AG (http://www.astaro.com/) | |
22 | * | |
f229f6ce | 23 | * (C) 2006-2012 Patrick McHardy <kaber@trash.net> |
f09943fe PM |
24 | */ |
25 | ||
26 | #include <linux/module.h> | |
27 | #include <linux/types.h> | |
28 | #include <linux/timer.h> | |
29 | #include <linux/list.h> | |
30 | #include <linux/seq_file.h> | |
31 | #include <linux/in.h> | |
3bb0d1c0 | 32 | #include <linux/netdevice.h> |
f09943fe | 33 | #include <linux/skbuff.h> |
5a0e3ad6 | 34 | #include <linux/slab.h> |
3bb0d1c0 AD |
35 | #include <net/dst.h> |
36 | #include <net/net_namespace.h> | |
37 | #include <net/netns/generic.h> | |
f09943fe PM |
38 | #include <net/netfilter/nf_conntrack_l4proto.h> |
39 | #include <net/netfilter/nf_conntrack_helper.h> | |
40 | #include <net/netfilter/nf_conntrack_core.h> | |
c779e849 | 41 | #include <net/netfilter/nf_conntrack_timeout.h> |
f09943fe PM |
42 | #include <linux/netfilter/nf_conntrack_proto_gre.h> |
43 | #include <linux/netfilter/nf_conntrack_pptp.h> | |
44 | ||
2c9e8637 | 45 | static const unsigned int gre_timeouts[GRE_CT_MAX] = { |
b888341c PNA |
46 | [GRE_CT_UNREPLIED] = 30*HZ, |
47 | [GRE_CT_REPLIED] = 180*HZ, | |
48 | }; | |
f09943fe | 49 | |
202e651c FW |
50 | /* used when expectation is added */ |
51 | static DEFINE_SPINLOCK(keymap_lock); | |
f09943fe | 52 | |
22fc4c4c | 53 | static inline struct nf_gre_net *gre_pernet(struct net *net) |
4f71d80f | 54 | { |
22fc4c4c | 55 | return &net->ct.nf_ct_proto.gre; |
4f71d80f G |
56 | } |
57 | ||
22fc4c4c | 58 | void nf_ct_gre_keymap_flush(struct net *net) |
f09943fe | 59 | { |
22fc4c4c | 60 | struct nf_gre_net *net_gre = gre_pernet(net); |
51807e91 | 61 | struct nf_ct_gre_keymap *km, *tmp; |
f09943fe | 62 | |
202e651c | 63 | spin_lock_bh(&keymap_lock); |
3bb0d1c0 | 64 | list_for_each_entry_safe(km, tmp, &net_gre->keymap_list, list) { |
202e651c FW |
65 | list_del_rcu(&km->list); |
66 | kfree_rcu(km, rcu); | |
f09943fe | 67 | } |
202e651c | 68 | spin_unlock_bh(&keymap_lock); |
f09943fe | 69 | } |
f09943fe PM |
70 | |
71 | static inline int gre_key_cmpfn(const struct nf_ct_gre_keymap *km, | |
72 | const struct nf_conntrack_tuple *t) | |
73 | { | |
74 | return km->tuple.src.l3num == t->src.l3num && | |
75 | !memcmp(&km->tuple.src.u3, &t->src.u3, sizeof(t->src.u3)) && | |
76 | !memcmp(&km->tuple.dst.u3, &t->dst.u3, sizeof(t->dst.u3)) && | |
77 | km->tuple.dst.protonum == t->dst.protonum && | |
78 | km->tuple.dst.u.all == t->dst.u.all; | |
79 | } | |
80 | ||
81 | /* look up the source key for a given tuple */ | |
3bb0d1c0 | 82 | static __be16 gre_keymap_lookup(struct net *net, struct nf_conntrack_tuple *t) |
f09943fe | 83 | { |
22fc4c4c | 84 | struct nf_gre_net *net_gre = gre_pernet(net); |
f09943fe PM |
85 | struct nf_ct_gre_keymap *km; |
86 | __be16 key = 0; | |
87 | ||
202e651c | 88 | list_for_each_entry_rcu(km, &net_gre->keymap_list, list) { |
f09943fe PM |
89 | if (gre_key_cmpfn(km, t)) { |
90 | key = km->tuple.src.u.gre.key; | |
91 | break; | |
92 | } | |
93 | } | |
f09943fe | 94 | |
0d53778e | 95 | pr_debug("lookup src key 0x%x for ", key); |
3c9fba65 | 96 | nf_ct_dump_tuple(t); |
f09943fe PM |
97 | |
98 | return key; | |
99 | } | |
100 | ||
101 | /* add a single keymap entry, associate with specified master ct */ | |
102 | int nf_ct_gre_keymap_add(struct nf_conn *ct, enum ip_conntrack_dir dir, | |
103 | struct nf_conntrack_tuple *t) | |
104 | { | |
3bb0d1c0 | 105 | struct net *net = nf_ct_net(ct); |
22fc4c4c | 106 | struct nf_gre_net *net_gre = gre_pernet(net); |
1afc5679 | 107 | struct nf_ct_pptp_master *ct_pptp_info = nfct_help_data(ct); |
f09943fe PM |
108 | struct nf_ct_gre_keymap **kmp, *km; |
109 | ||
1afc5679 | 110 | kmp = &ct_pptp_info->keymap[dir]; |
f09943fe PM |
111 | if (*kmp) { |
112 | /* check whether it's a retransmission */ | |
202e651c FW |
113 | list_for_each_entry_rcu(km, &net_gre->keymap_list, list) { |
114 | if (gre_key_cmpfn(km, t) && km == *kmp) | |
f09943fe PM |
115 | return 0; |
116 | } | |
0d53778e PM |
117 | pr_debug("trying to override keymap_%s for ct %p\n", |
118 | dir == IP_CT_DIR_REPLY ? "reply" : "orig", ct); | |
f09943fe PM |
119 | return -EEXIST; |
120 | } | |
121 | ||
122 | km = kmalloc(sizeof(*km), GFP_ATOMIC); | |
123 | if (!km) | |
124 | return -ENOMEM; | |
125 | memcpy(&km->tuple, t, sizeof(*t)); | |
126 | *kmp = km; | |
127 | ||
0d53778e | 128 | pr_debug("adding new entry %p: ", km); |
3c9fba65 | 129 | nf_ct_dump_tuple(&km->tuple); |
f09943fe | 130 | |
202e651c | 131 | spin_lock_bh(&keymap_lock); |
3bb0d1c0 | 132 | list_add_tail(&km->list, &net_gre->keymap_list); |
202e651c | 133 | spin_unlock_bh(&keymap_lock); |
f09943fe PM |
134 | |
135 | return 0; | |
136 | } | |
137 | EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_add); | |
138 | ||
139 | /* destroy the keymap entries associated with specified master ct */ | |
140 | void nf_ct_gre_keymap_destroy(struct nf_conn *ct) | |
141 | { | |
1afc5679 | 142 | struct nf_ct_pptp_master *ct_pptp_info = nfct_help_data(ct); |
f09943fe PM |
143 | enum ip_conntrack_dir dir; |
144 | ||
0d53778e | 145 | pr_debug("entering for ct %p\n", ct); |
f09943fe | 146 | |
202e651c | 147 | spin_lock_bh(&keymap_lock); |
f09943fe | 148 | for (dir = IP_CT_DIR_ORIGINAL; dir < IP_CT_DIR_MAX; dir++) { |
1afc5679 | 149 | if (ct_pptp_info->keymap[dir]) { |
0d53778e | 150 | pr_debug("removing %p from list\n", |
1afc5679 | 151 | ct_pptp_info->keymap[dir]); |
202e651c FW |
152 | list_del_rcu(&ct_pptp_info->keymap[dir]->list); |
153 | kfree_rcu(ct_pptp_info->keymap[dir], rcu); | |
1afc5679 | 154 | ct_pptp_info->keymap[dir] = NULL; |
f09943fe PM |
155 | } |
156 | } | |
202e651c | 157 | spin_unlock_bh(&keymap_lock); |
f09943fe PM |
158 | } |
159 | EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_destroy); | |
160 | ||
161 | /* PUBLIC CONNTRACK PROTO HELPER FUNCTIONS */ | |
162 | ||
f09943fe | 163 | /* gre hdr info to tuple */ |
df5e1629 FW |
164 | bool gre_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff, |
165 | struct net *net, struct nf_conntrack_tuple *tuple) | |
f09943fe | 166 | { |
c579a9e7 GF |
167 | const struct pptp_gre_header *pgrehdr; |
168 | struct pptp_gre_header _pgrehdr; | |
f09943fe | 169 | __be16 srckey; |
c579a9e7 GF |
170 | const struct gre_base_hdr *grehdr; |
171 | struct gre_base_hdr _grehdr; | |
f09943fe PM |
172 | |
173 | /* first only delinearize old RFC1701 GRE header */ | |
174 | grehdr = skb_header_pointer(skb, dataoff, sizeof(_grehdr), &_grehdr); | |
c579a9e7 | 175 | if (!grehdr || (grehdr->flags & GRE_VERSION) != GRE_VERSION_1) { |
f09943fe PM |
176 | /* try to behave like "nf_conntrack_proto_generic" */ |
177 | tuple->src.u.all = 0; | |
178 | tuple->dst.u.all = 0; | |
09f263cd | 179 | return true; |
f09943fe PM |
180 | } |
181 | ||
182 | /* PPTP header is variable length, only need up to the call_id field */ | |
183 | pgrehdr = skb_header_pointer(skb, dataoff, 8, &_pgrehdr); | |
184 | if (!pgrehdr) | |
09f263cd | 185 | return true; |
f09943fe | 186 | |
ecc6569f | 187 | if (grehdr->protocol != GRE_PROTO_PPP) { |
c579a9e7 | 188 | pr_debug("Unsupported GRE proto(0x%x)\n", ntohs(grehdr->protocol)); |
09f263cd | 189 | return false; |
f09943fe PM |
190 | } |
191 | ||
192 | tuple->dst.u.gre.key = pgrehdr->call_id; | |
3bb0d1c0 | 193 | srckey = gre_keymap_lookup(net, tuple); |
f09943fe PM |
194 | tuple->src.u.gre.key = srckey; |
195 | ||
09f263cd | 196 | return true; |
f09943fe PM |
197 | } |
198 | ||
ea48cc83 | 199 | #ifdef CONFIG_NF_CONNTRACK_PROCFS |
f09943fe | 200 | /* print private data for conntrack */ |
37246a58 | 201 | static void gre_print_conntrack(struct seq_file *s, struct nf_conn *ct) |
f09943fe | 202 | { |
37246a58 SRRH |
203 | seq_printf(s, "timeout=%u, stream_timeout=%u ", |
204 | (ct->proto.gre.timeout / HZ), | |
205 | (ct->proto.gre.stream_timeout / HZ)); | |
f09943fe | 206 | } |
ea48cc83 | 207 | #endif |
f09943fe | 208 | |
2c8503f5 PNA |
209 | static unsigned int *gre_get_timeouts(struct net *net) |
210 | { | |
22fc4c4c | 211 | return gre_pernet(net)->timeouts; |
2c8503f5 PNA |
212 | } |
213 | ||
f09943fe | 214 | /* Returns verdict for packet, and may modify conntrack */ |
44fb87f6 FW |
215 | int nf_conntrack_gre_packet(struct nf_conn *ct, |
216 | struct sk_buff *skb, | |
217 | unsigned int dataoff, | |
218 | enum ip_conntrack_info ctinfo, | |
219 | const struct nf_hook_state *state) | |
f09943fe | 220 | { |
9976fc6e FW |
221 | if (!nf_ct_is_confirmed(ct)) { |
222 | unsigned int *timeouts = nf_ct_timeout_lookup(ct); | |
223 | ||
224 | if (!timeouts) | |
225 | timeouts = gre_get_timeouts(nf_ct_net(ct)); | |
226 | ||
227 | /* initialize to sane value. Ideally a conntrack helper | |
228 | * (e.g. in case of pptp) is increasing them */ | |
229 | ct->proto.gre.stream_timeout = timeouts[GRE_CT_REPLIED]; | |
230 | ct->proto.gre.timeout = timeouts[GRE_CT_UNREPLIED]; | |
231 | } | |
232 | ||
f09943fe PM |
233 | /* If we've seen traffic both ways, this is a GRE connection. |
234 | * Extend timeout. */ | |
235 | if (ct->status & IPS_SEEN_REPLY) { | |
236 | nf_ct_refresh_acct(ct, ctinfo, skb, | |
237 | ct->proto.gre.stream_timeout); | |
238 | /* Also, more likely to be important, and not a probe. */ | |
98d9ae84 FW |
239 | if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status)) |
240 | nf_conntrack_event_cache(IPCT_ASSURED, ct); | |
f09943fe PM |
241 | } else |
242 | nf_ct_refresh_acct(ct, ctinfo, skb, | |
243 | ct->proto.gre.timeout); | |
244 | ||
245 | return NF_ACCEPT; | |
246 | } | |
247 | ||
a874752a | 248 | #ifdef CONFIG_NF_CONNTRACK_TIMEOUT |
50978462 PNA |
249 | |
250 | #include <linux/netfilter/nfnetlink.h> | |
251 | #include <linux/netfilter/nfnetlink_cttimeout.h> | |
252 | ||
8264deb8 G |
253 | static int gre_timeout_nlattr_to_obj(struct nlattr *tb[], |
254 | struct net *net, void *data) | |
50978462 PNA |
255 | { |
256 | unsigned int *timeouts = data; | |
22fc4c4c | 257 | struct nf_gre_net *net_gre = gre_pernet(net); |
50978462 | 258 | |
c779e849 FW |
259 | if (!timeouts) |
260 | timeouts = gre_get_timeouts(net); | |
50978462 | 261 | /* set default timeouts for GRE. */ |
22fc4c4c FW |
262 | timeouts[GRE_CT_UNREPLIED] = net_gre->timeouts[GRE_CT_UNREPLIED]; |
263 | timeouts[GRE_CT_REPLIED] = net_gre->timeouts[GRE_CT_REPLIED]; | |
50978462 PNA |
264 | |
265 | if (tb[CTA_TIMEOUT_GRE_UNREPLIED]) { | |
266 | timeouts[GRE_CT_UNREPLIED] = | |
267 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_GRE_UNREPLIED])) * HZ; | |
268 | } | |
269 | if (tb[CTA_TIMEOUT_GRE_REPLIED]) { | |
270 | timeouts[GRE_CT_REPLIED] = | |
271 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_GRE_REPLIED])) * HZ; | |
272 | } | |
273 | return 0; | |
274 | } | |
275 | ||
276 | static int | |
277 | gre_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data) | |
278 | { | |
279 | const unsigned int *timeouts = data; | |
280 | ||
242ddfc0 DM |
281 | if (nla_put_be32(skb, CTA_TIMEOUT_GRE_UNREPLIED, |
282 | htonl(timeouts[GRE_CT_UNREPLIED] / HZ)) || | |
283 | nla_put_be32(skb, CTA_TIMEOUT_GRE_REPLIED, | |
284 | htonl(timeouts[GRE_CT_REPLIED] / HZ))) | |
285 | goto nla_put_failure; | |
50978462 PNA |
286 | return 0; |
287 | ||
288 | nla_put_failure: | |
289 | return -ENOSPC; | |
290 | } | |
291 | ||
292 | static const struct nla_policy | |
293 | gre_timeout_nla_policy[CTA_TIMEOUT_GRE_MAX+1] = { | |
294 | [CTA_TIMEOUT_GRE_UNREPLIED] = { .type = NLA_U32 }, | |
295 | [CTA_TIMEOUT_GRE_REPLIED] = { .type = NLA_U32 }, | |
296 | }; | |
a874752a | 297 | #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */ |
50978462 | 298 | |
2a389de8 | 299 | void nf_conntrack_gre_init_net(struct net *net) |
4f71d80f | 300 | { |
22fc4c4c | 301 | struct nf_gre_net *net_gre = gre_pernet(net); |
4f71d80f G |
302 | int i; |
303 | ||
4f71d80f G |
304 | INIT_LIST_HEAD(&net_gre->keymap_list); |
305 | for (i = 0; i < GRE_CT_MAX; i++) | |
22fc4c4c | 306 | net_gre->timeouts[i] = gre_timeouts[i]; |
4f71d80f G |
307 | } |
308 | ||
f09943fe | 309 | /* protocol helper struct */ |
22fc4c4c | 310 | const struct nf_conntrack_l4proto nf_conntrack_l4proto_gre = { |
f09943fe | 311 | .l4proto = IPPROTO_GRE, |
ea48cc83 | 312 | #ifdef CONFIG_NF_CONNTRACK_PROCFS |
f09943fe | 313 | .print_conntrack = gre_print_conntrack, |
ea48cc83 | 314 | #endif |
c0cd1156 | 315 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
fdf70832 | 316 | .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr, |
a400c30e | 317 | .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size, |
fdf70832 | 318 | .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple, |
f73e924c | 319 | .nla_policy = nf_ct_port_nla_policy, |
f09943fe | 320 | #endif |
a874752a | 321 | #ifdef CONFIG_NF_CONNTRACK_TIMEOUT |
50978462 PNA |
322 | .ctnl_timeout = { |
323 | .nlattr_to_obj = gre_timeout_nlattr_to_obj, | |
324 | .obj_to_nlattr = gre_timeout_obj_to_nlattr, | |
325 | .nlattr_max = CTA_TIMEOUT_GRE_MAX, | |
326 | .obj_size = sizeof(unsigned int) * GRE_CT_MAX, | |
327 | .nla_policy = gre_timeout_nla_policy, | |
328 | }, | |
a874752a | 329 | #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */ |
f09943fe | 330 | }; |