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