Linux 6.10-rc4
[linux-2.6-block.git] / net / netfilter / nf_conntrack_proto_gre.c
CommitLineData
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 45static 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 */
51static DEFINE_SPINLOCK(keymap_lock);
f09943fe 52
22fc4c4c 53static 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
f09943fe
PM
58static inline int gre_key_cmpfn(const struct nf_ct_gre_keymap *km,
59 const struct nf_conntrack_tuple *t)
60{
61 return km->tuple.src.l3num == t->src.l3num &&
62 !memcmp(&km->tuple.src.u3, &t->src.u3, sizeof(t->src.u3)) &&
63 !memcmp(&km->tuple.dst.u3, &t->dst.u3, sizeof(t->dst.u3)) &&
64 km->tuple.dst.protonum == t->dst.protonum &&
65 km->tuple.dst.u.all == t->dst.u.all;
66}
67
68/* look up the source key for a given tuple */
3bb0d1c0 69static __be16 gre_keymap_lookup(struct net *net, struct nf_conntrack_tuple *t)
f09943fe 70{
22fc4c4c 71 struct nf_gre_net *net_gre = gre_pernet(net);
f09943fe
PM
72 struct nf_ct_gre_keymap *km;
73 __be16 key = 0;
74
202e651c 75 list_for_each_entry_rcu(km, &net_gre->keymap_list, list) {
f09943fe
PM
76 if (gre_key_cmpfn(km, t)) {
77 key = km->tuple.src.u.gre.key;
78 break;
79 }
80 }
f09943fe 81
0d53778e 82 pr_debug("lookup src key 0x%x for ", key);
3c9fba65 83 nf_ct_dump_tuple(t);
f09943fe
PM
84
85 return key;
86}
87
88/* add a single keymap entry, associate with specified master ct */
89int nf_ct_gre_keymap_add(struct nf_conn *ct, enum ip_conntrack_dir dir,
90 struct nf_conntrack_tuple *t)
91{
3bb0d1c0 92 struct net *net = nf_ct_net(ct);
22fc4c4c 93 struct nf_gre_net *net_gre = gre_pernet(net);
1afc5679 94 struct nf_ct_pptp_master *ct_pptp_info = nfct_help_data(ct);
f09943fe
PM
95 struct nf_ct_gre_keymap **kmp, *km;
96
1afc5679 97 kmp = &ct_pptp_info->keymap[dir];
f09943fe
PM
98 if (*kmp) {
99 /* check whether it's a retransmission */
202e651c
FW
100 list_for_each_entry_rcu(km, &net_gre->keymap_list, list) {
101 if (gre_key_cmpfn(km, t) && km == *kmp)
f09943fe
PM
102 return 0;
103 }
0d53778e
PM
104 pr_debug("trying to override keymap_%s for ct %p\n",
105 dir == IP_CT_DIR_REPLY ? "reply" : "orig", ct);
f09943fe
PM
106 return -EEXIST;
107 }
108
109 km = kmalloc(sizeof(*km), GFP_ATOMIC);
110 if (!km)
111 return -ENOMEM;
112 memcpy(&km->tuple, t, sizeof(*t));
113 *kmp = km;
114
0d53778e 115 pr_debug("adding new entry %p: ", km);
3c9fba65 116 nf_ct_dump_tuple(&km->tuple);
f09943fe 117
202e651c 118 spin_lock_bh(&keymap_lock);
3bb0d1c0 119 list_add_tail(&km->list, &net_gre->keymap_list);
202e651c 120 spin_unlock_bh(&keymap_lock);
f09943fe
PM
121
122 return 0;
123}
124EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_add);
125
126/* destroy the keymap entries associated with specified master ct */
127void nf_ct_gre_keymap_destroy(struct nf_conn *ct)
128{
1afc5679 129 struct nf_ct_pptp_master *ct_pptp_info = nfct_help_data(ct);
f09943fe
PM
130 enum ip_conntrack_dir dir;
131
0d53778e 132 pr_debug("entering for ct %p\n", ct);
f09943fe 133
202e651c 134 spin_lock_bh(&keymap_lock);
f09943fe 135 for (dir = IP_CT_DIR_ORIGINAL; dir < IP_CT_DIR_MAX; dir++) {
1afc5679 136 if (ct_pptp_info->keymap[dir]) {
0d53778e 137 pr_debug("removing %p from list\n",
1afc5679 138 ct_pptp_info->keymap[dir]);
202e651c
FW
139 list_del_rcu(&ct_pptp_info->keymap[dir]->list);
140 kfree_rcu(ct_pptp_info->keymap[dir], rcu);
1afc5679 141 ct_pptp_info->keymap[dir] = NULL;
f09943fe
PM
142 }
143 }
202e651c 144 spin_unlock_bh(&keymap_lock);
f09943fe
PM
145}
146EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_destroy);
147
148/* PUBLIC CONNTRACK PROTO HELPER FUNCTIONS */
149
f09943fe 150/* gre hdr info to tuple */
df5e1629
FW
151bool gre_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
152 struct net *net, struct nf_conntrack_tuple *tuple)
f09943fe 153{
c579a9e7
GF
154 const struct pptp_gre_header *pgrehdr;
155 struct pptp_gre_header _pgrehdr;
f09943fe 156 __be16 srckey;
c579a9e7
GF
157 const struct gre_base_hdr *grehdr;
158 struct gre_base_hdr _grehdr;
f09943fe
PM
159
160 /* first only delinearize old RFC1701 GRE header */
161 grehdr = skb_header_pointer(skb, dataoff, sizeof(_grehdr), &_grehdr);
c579a9e7 162 if (!grehdr || (grehdr->flags & GRE_VERSION) != GRE_VERSION_1) {
f09943fe
PM
163 /* try to behave like "nf_conntrack_proto_generic" */
164 tuple->src.u.all = 0;
165 tuple->dst.u.all = 0;
09f263cd 166 return true;
f09943fe
PM
167 }
168
169 /* PPTP header is variable length, only need up to the call_id field */
170 pgrehdr = skb_header_pointer(skb, dataoff, 8, &_pgrehdr);
171 if (!pgrehdr)
09f263cd 172 return true;
f09943fe 173
ecc6569f 174 if (grehdr->protocol != GRE_PROTO_PPP) {
c579a9e7 175 pr_debug("Unsupported GRE proto(0x%x)\n", ntohs(grehdr->protocol));
09f263cd 176 return false;
f09943fe
PM
177 }
178
179 tuple->dst.u.gre.key = pgrehdr->call_id;
3bb0d1c0 180 srckey = gre_keymap_lookup(net, tuple);
f09943fe
PM
181 tuple->src.u.gre.key = srckey;
182
09f263cd 183 return true;
f09943fe
PM
184}
185
ea48cc83 186#ifdef CONFIG_NF_CONNTRACK_PROCFS
f09943fe 187/* print private data for conntrack */
37246a58 188static void gre_print_conntrack(struct seq_file *s, struct nf_conn *ct)
f09943fe 189{
37246a58
SRRH
190 seq_printf(s, "timeout=%u, stream_timeout=%u ",
191 (ct->proto.gre.timeout / HZ),
192 (ct->proto.gre.stream_timeout / HZ));
f09943fe 193}
ea48cc83 194#endif
f09943fe 195
2c8503f5
PNA
196static unsigned int *gre_get_timeouts(struct net *net)
197{
22fc4c4c 198 return gre_pernet(net)->timeouts;
2c8503f5
PNA
199}
200
f09943fe 201/* Returns verdict for packet, and may modify conntrack */
44fb87f6
FW
202int nf_conntrack_gre_packet(struct nf_conn *ct,
203 struct sk_buff *skb,
204 unsigned int dataoff,
205 enum ip_conntrack_info ctinfo,
206 const struct nf_hook_state *state)
f09943fe 207{
8a9dc07b
FW
208 unsigned long status;
209
9976fc6e
FW
210 if (!nf_ct_is_confirmed(ct)) {
211 unsigned int *timeouts = nf_ct_timeout_lookup(ct);
212
213 if (!timeouts)
214 timeouts = gre_get_timeouts(nf_ct_net(ct));
215
216 /* initialize to sane value. Ideally a conntrack helper
217 * (e.g. in case of pptp) is increasing them */
218 ct->proto.gre.stream_timeout = timeouts[GRE_CT_REPLIED];
219 ct->proto.gre.timeout = timeouts[GRE_CT_UNREPLIED];
220 }
221
8a9dc07b 222 status = READ_ONCE(ct->status);
f09943fe
PM
223 /* If we've seen traffic both ways, this is a GRE connection.
224 * Extend timeout. */
8a9dc07b 225 if (status & IPS_SEEN_REPLY) {
f09943fe
PM
226 nf_ct_refresh_acct(ct, ctinfo, skb,
227 ct->proto.gre.stream_timeout);
8a9dc07b
FW
228
229 /* never set ASSURED for IPS_NAT_CLASH, they time out soon */
230 if (unlikely((status & IPS_NAT_CLASH)))
231 return NF_ACCEPT;
232
f09943fe 233 /* Also, more likely to be important, and not a probe. */
98d9ae84
FW
234 if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
235 nf_conntrack_event_cache(IPCT_ASSURED, ct);
f09943fe
PM
236 } else
237 nf_ct_refresh_acct(ct, ctinfo, skb,
238 ct->proto.gre.timeout);
239
240 return NF_ACCEPT;
241}
242
a874752a 243#ifdef CONFIG_NF_CONNTRACK_TIMEOUT
50978462
PNA
244
245#include <linux/netfilter/nfnetlink.h>
246#include <linux/netfilter/nfnetlink_cttimeout.h>
247
8264deb8
G
248static int gre_timeout_nlattr_to_obj(struct nlattr *tb[],
249 struct net *net, void *data)
50978462
PNA
250{
251 unsigned int *timeouts = data;
22fc4c4c 252 struct nf_gre_net *net_gre = gre_pernet(net);
50978462 253
c779e849
FW
254 if (!timeouts)
255 timeouts = gre_get_timeouts(net);
50978462 256 /* set default timeouts for GRE. */
22fc4c4c
FW
257 timeouts[GRE_CT_UNREPLIED] = net_gre->timeouts[GRE_CT_UNREPLIED];
258 timeouts[GRE_CT_REPLIED] = net_gre->timeouts[GRE_CT_REPLIED];
50978462
PNA
259
260 if (tb[CTA_TIMEOUT_GRE_UNREPLIED]) {
261 timeouts[GRE_CT_UNREPLIED] =
262 ntohl(nla_get_be32(tb[CTA_TIMEOUT_GRE_UNREPLIED])) * HZ;
263 }
264 if (tb[CTA_TIMEOUT_GRE_REPLIED]) {
265 timeouts[GRE_CT_REPLIED] =
266 ntohl(nla_get_be32(tb[CTA_TIMEOUT_GRE_REPLIED])) * HZ;
267 }
268 return 0;
269}
270
271static int
272gre_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
273{
274 const unsigned int *timeouts = data;
275
242ddfc0
DM
276 if (nla_put_be32(skb, CTA_TIMEOUT_GRE_UNREPLIED,
277 htonl(timeouts[GRE_CT_UNREPLIED] / HZ)) ||
278 nla_put_be32(skb, CTA_TIMEOUT_GRE_REPLIED,
279 htonl(timeouts[GRE_CT_REPLIED] / HZ)))
280 goto nla_put_failure;
50978462
PNA
281 return 0;
282
283nla_put_failure:
284 return -ENOSPC;
285}
286
287static const struct nla_policy
288gre_timeout_nla_policy[CTA_TIMEOUT_GRE_MAX+1] = {
289 [CTA_TIMEOUT_GRE_UNREPLIED] = { .type = NLA_U32 },
290 [CTA_TIMEOUT_GRE_REPLIED] = { .type = NLA_U32 },
291};
a874752a 292#endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
50978462 293
2a389de8 294void nf_conntrack_gre_init_net(struct net *net)
4f71d80f 295{
22fc4c4c 296 struct nf_gre_net *net_gre = gre_pernet(net);
4f71d80f
G
297 int i;
298
4f71d80f
G
299 INIT_LIST_HEAD(&net_gre->keymap_list);
300 for (i = 0; i < GRE_CT_MAX; i++)
22fc4c4c 301 net_gre->timeouts[i] = gre_timeouts[i];
4f71d80f
G
302}
303
f09943fe 304/* protocol helper struct */
22fc4c4c 305const struct nf_conntrack_l4proto nf_conntrack_l4proto_gre = {
f09943fe 306 .l4proto = IPPROTO_GRE,
d671fd82 307 .allow_clash = true,
ea48cc83 308#ifdef CONFIG_NF_CONNTRACK_PROCFS
f09943fe 309 .print_conntrack = gre_print_conntrack,
ea48cc83 310#endif
c0cd1156 311#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
fdf70832 312 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
a400c30e 313 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
fdf70832 314 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
f73e924c 315 .nla_policy = nf_ct_port_nla_policy,
f09943fe 316#endif
a874752a 317#ifdef CONFIG_NF_CONNTRACK_TIMEOUT
50978462
PNA
318 .ctnl_timeout = {
319 .nlattr_to_obj = gre_timeout_nlattr_to_obj,
320 .obj_to_nlattr = gre_timeout_obj_to_nlattr,
321 .nlattr_max = CTA_TIMEOUT_GRE_MAX,
322 .obj_size = sizeof(unsigned int) * GRE_CT_MAX,
323 .nla_policy = gre_timeout_nla_policy,
324 },
a874752a 325#endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
f09943fe 326};