1 // SPDX-License-Identifier: GPL-2.0-only
3 * ip_conntrack_proto_gre.c - Version 3.0
5 * Connection tracking protocol helper module for GRE.
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.
10 * It has an optional key field, which may help us distinguishing two
11 * connections between the same two hosts.
13 * GRE is defined in RFC 1701 and RFC 1702, as well as RFC 2784
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
19 * Documentation about PPTP can be found in RFC 2637
21 * (C) 2000-2005 by Harald Welte <laforge@gnumonks.org>
23 * Development of this code funded by Astaro AG (http://www.astaro.com/)
25 * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
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>
34 #include <linux/netdevice.h>
35 #include <linux/skbuff.h>
36 #include <linux/slab.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>
47 static const unsigned int gre_timeouts
[GRE_CT_MAX
] = {
48 [GRE_CT_UNREPLIED
] = 30*HZ
,
49 [GRE_CT_REPLIED
] = 180*HZ
,
52 /* used when expectation is added */
53 static DEFINE_SPINLOCK(keymap_lock
);
55 static inline struct nf_gre_net
*gre_pernet(struct net
*net
)
57 return &net
->ct
.nf_ct_proto
.gre
;
60 void nf_ct_gre_keymap_flush(struct net
*net
)
62 struct nf_gre_net
*net_gre
= gre_pernet(net
);
63 struct nf_ct_gre_keymap
*km
, *tmp
;
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
);
70 spin_unlock_bh(&keymap_lock
);
73 static inline int gre_key_cmpfn(const struct nf_ct_gre_keymap
*km
,
74 const struct nf_conntrack_tuple
*t
)
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
;
83 /* look up the source key for a given tuple */
84 static __be16
gre_keymap_lookup(struct net
*net
, struct nf_conntrack_tuple
*t
)
86 struct nf_gre_net
*net_gre
= gre_pernet(net
);
87 struct nf_ct_gre_keymap
*km
;
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
;
97 pr_debug("lookup src key 0x%x for ", key
);
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
)
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
;
112 kmp
= &ct_pptp_info
->keymap
[dir
];
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
)
119 pr_debug("trying to override keymap_%s for ct %p\n",
120 dir
== IP_CT_DIR_REPLY
? "reply" : "orig", ct
);
124 km
= kmalloc(sizeof(*km
), GFP_ATOMIC
);
127 memcpy(&km
->tuple
, t
, sizeof(*t
));
130 pr_debug("adding new entry %p: ", km
);
131 nf_ct_dump_tuple(&km
->tuple
);
133 spin_lock_bh(&keymap_lock
);
134 list_add_tail(&km
->list
, &net_gre
->keymap_list
);
135 spin_unlock_bh(&keymap_lock
);
139 EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_add
);
141 /* destroy the keymap entries associated with specified master ct */
142 void nf_ct_gre_keymap_destroy(struct nf_conn
*ct
)
144 struct nf_ct_pptp_master
*ct_pptp_info
= nfct_help_data(ct
);
145 enum ip_conntrack_dir dir
;
147 pr_debug("entering for ct %p\n", ct
);
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
;
159 spin_unlock_bh(&keymap_lock
);
161 EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_destroy
);
163 /* PUBLIC CONNTRACK PROTO HELPER FUNCTIONS */
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
)
169 const struct pptp_gre_header
*pgrehdr
;
170 struct pptp_gre_header _pgrehdr
;
172 const struct gre_base_hdr
*grehdr
;
173 struct gre_base_hdr _grehdr
;
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;
184 /* PPTP header is variable length, only need up to the call_id field */
185 pgrehdr
= skb_header_pointer(skb
, dataoff
, 8, &_pgrehdr
);
189 if (grehdr
->protocol
!= GRE_PROTO_PPP
) {
190 pr_debug("Unsupported GRE proto(0x%x)\n", ntohs(grehdr
->protocol
));
194 tuple
->dst
.u
.gre
.key
= pgrehdr
->call_id
;
195 srckey
= gre_keymap_lookup(net
, tuple
);
196 tuple
->src
.u
.gre
.key
= srckey
;
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
)
205 seq_printf(s
, "timeout=%u, stream_timeout=%u ",
206 (ct
->proto
.gre
.timeout
/ HZ
),
207 (ct
->proto
.gre
.stream_timeout
/ HZ
));
211 static unsigned int *gre_get_timeouts(struct net
*net
)
213 return gre_pernet(net
)->timeouts
;
216 /* Returns verdict for packet, and may modify conntrack */
217 int nf_conntrack_gre_packet(struct nf_conn
*ct
,
219 unsigned int dataoff
,
220 enum ip_conntrack_info ctinfo
,
221 const struct nf_hook_state
*state
)
223 if (state
->pf
!= NFPROTO_IPV4
)
226 if (!nf_ct_is_confirmed(ct
)) {
227 unsigned int *timeouts
= nf_ct_timeout_lookup(ct
);
230 timeouts
= gre_get_timeouts(nf_ct_net(ct
));
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
];
238 /* If we've seen traffic both ways, this is a GRE connection.
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
);
247 nf_ct_refresh_acct(ct
, ctinfo
, skb
,
248 ct
->proto
.gre
.timeout
);
253 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
255 #include <linux/netfilter/nfnetlink.h>
256 #include <linux/netfilter/nfnetlink_cttimeout.h>
258 static int gre_timeout_nlattr_to_obj(struct nlattr
*tb
[],
259 struct net
*net
, void *data
)
261 unsigned int *timeouts
= data
;
262 struct nf_gre_net
*net_gre
= gre_pernet(net
);
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
];
270 if (tb
[CTA_TIMEOUT_GRE_UNREPLIED
]) {
271 timeouts
[GRE_CT_UNREPLIED
] =
272 ntohl(nla_get_be32(tb
[CTA_TIMEOUT_GRE_UNREPLIED
])) * HZ
;
274 if (tb
[CTA_TIMEOUT_GRE_REPLIED
]) {
275 timeouts
[GRE_CT_REPLIED
] =
276 ntohl(nla_get_be32(tb
[CTA_TIMEOUT_GRE_REPLIED
])) * HZ
;
282 gre_timeout_obj_to_nlattr(struct sk_buff
*skb
, const void *data
)
284 const unsigned int *timeouts
= data
;
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
;
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
},
302 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
304 void nf_conntrack_gre_init_net(struct net
*net
)
306 struct nf_gre_net
*net_gre
= gre_pernet(net
);
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
];
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
,
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
,
326 #ifdef CONFIG_NF_CONNTRACK_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
,
334 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */