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