1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include <linux/module.h>
28#include <linux/types.h>
29#include <linux/timer.h>
30#include <linux/list.h>
31#include <linux/seq_file.h>
32#include <linux/in.h>
33#include <linux/netdevice.h>
34#include <linux/skbuff.h>
35#include <linux/slab.h>
36#include <net/dst.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 <linux/netfilter/nf_conntrack_proto_gre.h>
44#include <linux/netfilter/nf_conntrack_pptp.h>
45
46static const unsigned int gre_timeouts[GRE_CT_MAX] = {
47 [GRE_CT_UNREPLIED] = 30*HZ,
48 [GRE_CT_REPLIED] = 180*HZ,
49};
50
51
52static DEFINE_SPINLOCK(keymap_lock);
53
54static inline struct nf_gre_net *gre_pernet(struct net *net)
55{
56 return &net->nf_ct_gre;
57}
58
59void nf_ct_gre_keymap_flush(struct net *net)
60{
61 struct nf_gre_net *net_gre = gre_pernet(net);
62 struct nf_ct_gre_keymap *km, *tmp;
63
64 spin_lock_bh(&keymap_lock);
65 list_for_each_entry_safe(km, tmp, &net_gre->keymap_list, list) {
66 list_del_rcu(&km->list);
67 kfree_rcu(km, rcu);
68 }
69 spin_unlock_bh(&keymap_lock);
70}
71
72static inline int gre_key_cmpfn(const struct nf_ct_gre_keymap *km,
73 const struct nf_conntrack_tuple *t)
74{
75 return km->tuple.src.l3num == t->src.l3num &&
76 !memcmp(&km->tuple.src.u3, &t->src.u3, sizeof(t->src.u3)) &&
77 !memcmp(&km->tuple.dst.u3, &t->dst.u3, sizeof(t->dst.u3)) &&
78 km->tuple.dst.protonum == t->dst.protonum &&
79 km->tuple.dst.u.all == t->dst.u.all;
80}
81
82
83static __be16 gre_keymap_lookup(struct net *net, struct nf_conntrack_tuple *t)
84{
85 struct nf_gre_net *net_gre = gre_pernet(net);
86 struct nf_ct_gre_keymap *km;
87 __be16 key = 0;
88
89 list_for_each_entry_rcu(km, &net_gre->keymap_list, list) {
90 if (gre_key_cmpfn(km, t)) {
91 key = km->tuple.src.u.gre.key;
92 break;
93 }
94 }
95
96 pr_debug("lookup src key 0x%x for ", key);
97 nf_ct_dump_tuple(t);
98
99 return key;
100}
101
102
103int nf_ct_gre_keymap_add(struct nf_conn *ct, enum ip_conntrack_dir dir,
104 struct nf_conntrack_tuple *t)
105{
106 struct net *net = nf_ct_net(ct);
107 struct nf_gre_net *net_gre = gre_pernet(net);
108 struct nf_ct_pptp_master *ct_pptp_info = nfct_help_data(ct);
109 struct nf_ct_gre_keymap **kmp, *km;
110
111 kmp = &ct_pptp_info->keymap[dir];
112 if (*kmp) {
113
114 list_for_each_entry_rcu(km, &net_gre->keymap_list, list) {
115 if (gre_key_cmpfn(km, t) && km == *kmp)
116 return 0;
117 }
118 pr_debug("trying to override keymap_%s for ct %p\n",
119 dir == IP_CT_DIR_REPLY ? "reply" : "orig", ct);
120 return -EEXIST;
121 }
122
123 km = kmalloc(sizeof(*km), GFP_ATOMIC);
124 if (!km)
125 return -ENOMEM;
126 memcpy(&km->tuple, t, sizeof(*t));
127 *kmp = km;
128
129 pr_debug("adding new entry %p: ", km);
130 nf_ct_dump_tuple(&km->tuple);
131
132 spin_lock_bh(&keymap_lock);
133 list_add_tail(&km->list, &net_gre->keymap_list);
134 spin_unlock_bh(&keymap_lock);
135
136 return 0;
137}
138EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_add);
139
140
141void nf_ct_gre_keymap_destroy(struct nf_conn *ct)
142{
143 struct nf_ct_pptp_master *ct_pptp_info = nfct_help_data(ct);
144 enum ip_conntrack_dir dir;
145
146 pr_debug("entering for ct %p\n", ct);
147
148 spin_lock_bh(&keymap_lock);
149 for (dir = IP_CT_DIR_ORIGINAL; dir < IP_CT_DIR_MAX; dir++) {
150 if (ct_pptp_info->keymap[dir]) {
151 pr_debug("removing %p from list\n",
152 ct_pptp_info->keymap[dir]);
153 list_del_rcu(&ct_pptp_info->keymap[dir]->list);
154 kfree_rcu(ct_pptp_info->keymap[dir], rcu);
155 ct_pptp_info->keymap[dir] = NULL;
156 }
157 }
158 spin_unlock_bh(&keymap_lock);
159}
160EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_destroy);
161
162
163
164
165bool gre_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
166 struct net *net, struct nf_conntrack_tuple *tuple)
167{
168 const struct pptp_gre_header *pgrehdr;
169 struct pptp_gre_header _pgrehdr;
170 __be16 srckey;
171 const struct gre_base_hdr *grehdr;
172 struct gre_base_hdr _grehdr;
173
174
175 grehdr = skb_header_pointer(skb, dataoff, sizeof(_grehdr), &_grehdr);
176 if (!grehdr || (grehdr->flags & GRE_VERSION) != GRE_VERSION_1) {
177
178 tuple->src.u.all = 0;
179 tuple->dst.u.all = 0;
180 return true;
181 }
182
183
184 pgrehdr = skb_header_pointer(skb, dataoff, 8, &_pgrehdr);
185 if (!pgrehdr)
186 return true;
187
188 if (grehdr->protocol != GRE_PROTO_PPP) {
189 pr_debug("Unsupported GRE proto(0x%x)\n", ntohs(grehdr->protocol));
190 return false;
191 }
192
193 tuple->dst.u.gre.key = pgrehdr->call_id;
194 srckey = gre_keymap_lookup(net, tuple);
195 tuple->src.u.gre.key = srckey;
196
197 return true;
198}
199
200#ifdef CONFIG_NF_CONNTRACK_PROCFS
201
202static void gre_print_conntrack(struct seq_file *s, struct nf_conn *ct)
203{
204 seq_printf(s, "timeout=%u, stream_timeout=%u ",
205 (ct->proto.gre.timeout / HZ),
206 (ct->proto.gre.stream_timeout / HZ));
207}
208#endif
209
210static unsigned int *gre_get_timeouts(struct net *net)
211{
212 return gre_pernet(net)->timeouts;
213}
214
215
216int nf_conntrack_gre_packet(struct nf_conn *ct,
217 struct sk_buff *skb,
218 unsigned int dataoff,
219 enum ip_conntrack_info ctinfo,
220 const struct nf_hook_state *state)
221{
222 if (state->pf != NFPROTO_IPV4)
223 return -NF_ACCEPT;
224
225 if (!nf_ct_is_confirmed(ct)) {
226 unsigned int *timeouts = nf_ct_timeout_lookup(ct);
227
228 if (!timeouts)
229 timeouts = gre_get_timeouts(nf_ct_net(ct));
230
231
232
233 ct->proto.gre.stream_timeout = timeouts[GRE_CT_REPLIED];
234 ct->proto.gre.timeout = timeouts[GRE_CT_UNREPLIED];
235 }
236
237
238
239 if (ct->status & IPS_SEEN_REPLY) {
240 nf_ct_refresh_acct(ct, ctinfo, skb,
241 ct->proto.gre.stream_timeout);
242
243 if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
244 nf_conntrack_event_cache(IPCT_ASSURED, ct);
245 } else
246 nf_ct_refresh_acct(ct, ctinfo, skb,
247 ct->proto.gre.timeout);
248
249 return NF_ACCEPT;
250}
251
252
253
254static void gre_destroy(struct nf_conn *ct)
255{
256 struct nf_conn *master = ct->master;
257 pr_debug(" entering\n");
258
259 if (!master)
260 pr_debug("no master !?!\n");
261 else
262 nf_ct_gre_keymap_destroy(master);
263}
264
265#ifdef CONFIG_NF_CONNTRACK_TIMEOUT
266
267#include <linux/netfilter/nfnetlink.h>
268#include <linux/netfilter/nfnetlink_cttimeout.h>
269
270static int gre_timeout_nlattr_to_obj(struct nlattr *tb[],
271 struct net *net, void *data)
272{
273 unsigned int *timeouts = data;
274 struct nf_gre_net *net_gre = gre_pernet(net);
275
276 if (!timeouts)
277 timeouts = gre_get_timeouts(net);
278
279 timeouts[GRE_CT_UNREPLIED] = net_gre->timeouts[GRE_CT_UNREPLIED];
280 timeouts[GRE_CT_REPLIED] = net_gre->timeouts[GRE_CT_REPLIED];
281
282 if (tb[CTA_TIMEOUT_GRE_UNREPLIED]) {
283 timeouts[GRE_CT_UNREPLIED] =
284 ntohl(nla_get_be32(tb[CTA_TIMEOUT_GRE_UNREPLIED])) * HZ;
285 }
286 if (tb[CTA_TIMEOUT_GRE_REPLIED]) {
287 timeouts[GRE_CT_REPLIED] =
288 ntohl(nla_get_be32(tb[CTA_TIMEOUT_GRE_REPLIED])) * HZ;
289 }
290 return 0;
291}
292
293static int
294gre_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
295{
296 const unsigned int *timeouts = data;
297
298 if (nla_put_be32(skb, CTA_TIMEOUT_GRE_UNREPLIED,
299 htonl(timeouts[GRE_CT_UNREPLIED] / HZ)) ||
300 nla_put_be32(skb, CTA_TIMEOUT_GRE_REPLIED,
301 htonl(timeouts[GRE_CT_REPLIED] / HZ)))
302 goto nla_put_failure;
303 return 0;
304
305nla_put_failure:
306 return -ENOSPC;
307}
308
309static const struct nla_policy
310gre_timeout_nla_policy[CTA_TIMEOUT_GRE_MAX+1] = {
311 [CTA_TIMEOUT_GRE_UNREPLIED] = { .type = NLA_U32 },
312 [CTA_TIMEOUT_GRE_REPLIED] = { .type = NLA_U32 },
313};
314#endif
315
316static int gre_init_net(struct net *net)
317{
318 struct nf_gre_net *net_gre = gre_pernet(net);
319 int i;
320
321 INIT_LIST_HEAD(&net_gre->keymap_list);
322 for (i = 0; i < GRE_CT_MAX; i++)
323 net_gre->timeouts[i] = gre_timeouts[i];
324
325 return 0;
326}
327
328
329const struct nf_conntrack_l4proto nf_conntrack_l4proto_gre = {
330 .l4proto = IPPROTO_GRE,
331#ifdef CONFIG_NF_CONNTRACK_PROCFS
332 .print_conntrack = gre_print_conntrack,
333#endif
334 .destroy = gre_destroy,
335#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
336 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
337 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
338 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
339 .nla_policy = nf_ct_port_nla_policy,
340#endif
341#ifdef CONFIG_NF_CONNTRACK_TIMEOUT
342 .ctnl_timeout = {
343 .nlattr_to_obj = gre_timeout_nlattr_to_obj,
344 .obj_to_nlattr = gre_timeout_obj_to_nlattr,
345 .nlattr_max = CTA_TIMEOUT_GRE_MAX,
346 .obj_size = sizeof(unsigned int) * GRE_CT_MAX,
347 .nla_policy = gre_timeout_nla_policy,
348 },
349#endif
350 .init_net = gre_init_net,
351};
352