blob: d5665739e3b1442516b3d0bede07eb945381d2ae [file] [log] [blame]
Patrick McHardyf09943f2006-12-02 22:09:41 -08001/*
2 * ip_conntrack_proto_gre.c - Version 3.0
3 *
4 * Connection tracking protocol helper module for GRE.
5 *
6 * GRE is a generic encapsulation protocol, which is generally not very
7 * suited for NAT, as it has no protocol-specific part as port numbers.
8 *
9 * It has an optional key field, which may help us distinguishing two
10 * connections between the same two hosts.
11 *
12 * GRE is defined in RFC 1701 and RFC 1702, as well as RFC 2784
13 *
14 * PPTP is built on top of a modified version of GRE, and has a mandatory
15 * field called "CallID", which serves us for the same purpose as the key
16 * field in plain GRE.
17 *
18 * Documentation about PPTP can be found in RFC 2637
19 *
20 * (C) 2000-2005 by Harald Welte <laforge@gnumonks.org>
21 *
22 * Development of this code funded by Astaro AG (http://www.astaro.com/)
23 *
Patrick McHardyf229f6c2013-04-06 15:24:29 +020024 * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
Patrick McHardyf09943f2006-12-02 22:09:41 -080025 */
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>
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +020033#include <linux/netdevice.h>
Patrick McHardyf09943f2006-12-02 22:09:41 -080034#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +020036#include <net/dst.h>
37#include <net/net_namespace.h>
38#include <net/netns/generic.h>
Patrick McHardyf09943f2006-12-02 22:09:41 -080039#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 <linux/netfilter/nf_conntrack_proto_gre.h>
43#include <linux/netfilter/nf_conntrack_pptp.h>
44
Pablo Neira Ayusob8883412012-02-28 18:56:39 +010045enum grep_conntrack {
46 GRE_CT_UNREPLIED,
47 GRE_CT_REPLIED,
48 GRE_CT_MAX
49};
50
51static unsigned int gre_timeouts[GRE_CT_MAX] = {
52 [GRE_CT_UNREPLIED] = 30*HZ,
53 [GRE_CT_REPLIED] = 180*HZ,
54};
Patrick McHardyf09943f2006-12-02 22:09:41 -080055
Eric Dumazetf99189b2009-11-17 10:42:49 +000056static int proto_gre_net_id __read_mostly;
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +020057struct netns_proto_gre {
Gao feng4f71d802012-05-28 21:04:21 +000058 struct nf_proto_net nf;
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +020059 rwlock_t keymap_lock;
60 struct list_head keymap_list;
Gao feng4f71d802012-05-28 21:04:21 +000061 unsigned int gre_timeouts[GRE_CT_MAX];
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +020062};
Patrick McHardyf09943f2006-12-02 22:09:41 -080063
Gao feng4f71d802012-05-28 21:04:21 +000064static inline struct netns_proto_gre *gre_pernet(struct net *net)
65{
66 return net_generic(net, proto_gre_net_id);
67}
68
Andrey Vagin8142b222014-03-31 18:14:18 +040069static void nf_ct_gre_keymap_flush(struct net *net)
Patrick McHardyf09943f2006-12-02 22:09:41 -080070{
Gao feng4f71d802012-05-28 21:04:21 +000071 struct netns_proto_gre *net_gre = gre_pernet(net);
Alexey Dobriyan51807e92008-09-07 18:20:36 -070072 struct nf_ct_gre_keymap *km, *tmp;
Patrick McHardyf09943f2006-12-02 22:09:41 -080073
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +020074 write_lock_bh(&net_gre->keymap_lock);
75 list_for_each_entry_safe(km, tmp, &net_gre->keymap_list, list) {
Alexey Dobriyan51807e92008-09-07 18:20:36 -070076 list_del(&km->list);
77 kfree(km);
Patrick McHardyf09943f2006-12-02 22:09:41 -080078 }
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +020079 write_unlock_bh(&net_gre->keymap_lock);
Patrick McHardyf09943f2006-12-02 22:09:41 -080080}
Patrick McHardyf09943f2006-12-02 22:09:41 -080081
82static inline int gre_key_cmpfn(const struct nf_ct_gre_keymap *km,
83 const struct nf_conntrack_tuple *t)
84{
85 return km->tuple.src.l3num == t->src.l3num &&
86 !memcmp(&km->tuple.src.u3, &t->src.u3, sizeof(t->src.u3)) &&
87 !memcmp(&km->tuple.dst.u3, &t->dst.u3, sizeof(t->dst.u3)) &&
88 km->tuple.dst.protonum == t->dst.protonum &&
89 km->tuple.dst.u.all == t->dst.u.all;
90}
91
92/* look up the source key for a given tuple */
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +020093static __be16 gre_keymap_lookup(struct net *net, struct nf_conntrack_tuple *t)
Patrick McHardyf09943f2006-12-02 22:09:41 -080094{
Gao feng4f71d802012-05-28 21:04:21 +000095 struct netns_proto_gre *net_gre = gre_pernet(net);
Patrick McHardyf09943f2006-12-02 22:09:41 -080096 struct nf_ct_gre_keymap *km;
97 __be16 key = 0;
98
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +020099 read_lock_bh(&net_gre->keymap_lock);
100 list_for_each_entry(km, &net_gre->keymap_list, list) {
Patrick McHardyf09943f2006-12-02 22:09:41 -0800101 if (gre_key_cmpfn(km, t)) {
102 key = km->tuple.src.u.gre.key;
103 break;
104 }
105 }
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200106 read_unlock_bh(&net_gre->keymap_lock);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800107
Patrick McHardy0d537782007-07-07 22:39:38 -0700108 pr_debug("lookup src key 0x%x for ", key);
Jan Engelhardt3c9fba62008-04-14 11:15:54 +0200109 nf_ct_dump_tuple(t);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800110
111 return key;
112}
113
114/* add a single keymap entry, associate with specified master ct */
115int nf_ct_gre_keymap_add(struct nf_conn *ct, enum ip_conntrack_dir dir,
116 struct nf_conntrack_tuple *t)
117{
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200118 struct net *net = nf_ct_net(ct);
Gao feng4f71d802012-05-28 21:04:21 +0000119 struct netns_proto_gre *net_gre = gre_pernet(net);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +0200120 struct nf_ct_pptp_master *ct_pptp_info = nfct_help_data(ct);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800121 struct nf_ct_gre_keymap **kmp, *km;
122
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +0200123 kmp = &ct_pptp_info->keymap[dir];
Patrick McHardyf09943f2006-12-02 22:09:41 -0800124 if (*kmp) {
125 /* check whether it's a retransmission */
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200126 read_lock_bh(&net_gre->keymap_lock);
127 list_for_each_entry(km, &net_gre->keymap_list, list) {
Alexey Dobriyan887464a2008-09-07 18:20:08 -0700128 if (gre_key_cmpfn(km, t) && km == *kmp) {
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200129 read_unlock_bh(&net_gre->keymap_lock);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800130 return 0;
Alexey Dobriyan887464a2008-09-07 18:20:08 -0700131 }
Patrick McHardyf09943f2006-12-02 22:09:41 -0800132 }
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200133 read_unlock_bh(&net_gre->keymap_lock);
Patrick McHardy0d537782007-07-07 22:39:38 -0700134 pr_debug("trying to override keymap_%s for ct %p\n",
135 dir == IP_CT_DIR_REPLY ? "reply" : "orig", ct);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800136 return -EEXIST;
137 }
138
139 km = kmalloc(sizeof(*km), GFP_ATOMIC);
140 if (!km)
141 return -ENOMEM;
142 memcpy(&km->tuple, t, sizeof(*t));
143 *kmp = km;
144
Patrick McHardy0d537782007-07-07 22:39:38 -0700145 pr_debug("adding new entry %p: ", km);
Jan Engelhardt3c9fba62008-04-14 11:15:54 +0200146 nf_ct_dump_tuple(&km->tuple);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800147
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200148 write_lock_bh(&net_gre->keymap_lock);
149 list_add_tail(&km->list, &net_gre->keymap_list);
150 write_unlock_bh(&net_gre->keymap_lock);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800151
152 return 0;
153}
154EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_add);
155
156/* destroy the keymap entries associated with specified master ct */
157void nf_ct_gre_keymap_destroy(struct nf_conn *ct)
158{
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200159 struct net *net = nf_ct_net(ct);
Gao feng4f71d802012-05-28 21:04:21 +0000160 struct netns_proto_gre *net_gre = gre_pernet(net);
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +0200161 struct nf_ct_pptp_master *ct_pptp_info = nfct_help_data(ct);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800162 enum ip_conntrack_dir dir;
163
Patrick McHardy0d537782007-07-07 22:39:38 -0700164 pr_debug("entering for ct %p\n", ct);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800165
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200166 write_lock_bh(&net_gre->keymap_lock);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800167 for (dir = IP_CT_DIR_ORIGINAL; dir < IP_CT_DIR_MAX; dir++) {
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +0200168 if (ct_pptp_info->keymap[dir]) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700169 pr_debug("removing %p from list\n",
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +0200170 ct_pptp_info->keymap[dir]);
171 list_del(&ct_pptp_info->keymap[dir]->list);
172 kfree(ct_pptp_info->keymap[dir]);
173 ct_pptp_info->keymap[dir] = NULL;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800174 }
175 }
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200176 write_unlock_bh(&net_gre->keymap_lock);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800177}
178EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_destroy);
179
180/* PUBLIC CONNTRACK PROTO HELPER FUNCTIONS */
181
182/* invert gre part of tuple */
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200183static bool gre_invert_tuple(struct nf_conntrack_tuple *tuple,
184 const struct nf_conntrack_tuple *orig)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800185{
186 tuple->dst.u.gre.key = orig->src.u.gre.key;
187 tuple->src.u.gre.key = orig->dst.u.gre.key;
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200188 return true;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800189}
190
191/* gre hdr info to tuple */
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200192static bool gre_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
193 struct nf_conntrack_tuple *tuple)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800194{
Eric Dumazetadf30902009-06-02 05:19:30 +0000195 struct net *net = dev_net(skb->dev ? skb->dev : skb_dst(skb)->dev);
Jan Engelhardtdc35dc52008-01-31 04:52:46 -0800196 const struct gre_hdr_pptp *pgrehdr;
197 struct gre_hdr_pptp _pgrehdr;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800198 __be16 srckey;
Jan Engelhardtdc35dc52008-01-31 04:52:46 -0800199 const struct gre_hdr *grehdr;
200 struct gre_hdr _grehdr;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800201
202 /* first only delinearize old RFC1701 GRE header */
203 grehdr = skb_header_pointer(skb, dataoff, sizeof(_grehdr), &_grehdr);
204 if (!grehdr || grehdr->version != GRE_VERSION_PPTP) {
205 /* try to behave like "nf_conntrack_proto_generic" */
206 tuple->src.u.all = 0;
207 tuple->dst.u.all = 0;
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200208 return true;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800209 }
210
211 /* PPTP header is variable length, only need up to the call_id field */
212 pgrehdr = skb_header_pointer(skb, dataoff, 8, &_pgrehdr);
213 if (!pgrehdr)
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200214 return true;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800215
216 if (ntohs(grehdr->protocol) != GRE_PROTOCOL_PPTP) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700217 pr_debug("GRE_VERSION_PPTP but unknown proto\n");
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200218 return false;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800219 }
220
221 tuple->dst.u.gre.key = pgrehdr->call_id;
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200222 srckey = gre_keymap_lookup(net, tuple);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800223 tuple->src.u.gre.key = srckey;
224
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200225 return true;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800226}
227
228/* print gre part of tuple */
229static int gre_print_tuple(struct seq_file *s,
230 const struct nf_conntrack_tuple *tuple)
231{
232 return seq_printf(s, "srckey=0x%x dstkey=0x%x ",
233 ntohs(tuple->src.u.gre.key),
234 ntohs(tuple->dst.u.gre.key));
235}
236
237/* print private data for conntrack */
Patrick McHardy440f0d52009-06-10 14:32:47 +0200238static int gre_print_conntrack(struct seq_file *s, struct nf_conn *ct)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800239{
240 return seq_printf(s, "timeout=%u, stream_timeout=%u ",
241 (ct->proto.gre.timeout / HZ),
242 (ct->proto.gre.stream_timeout / HZ));
243}
244
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100245static unsigned int *gre_get_timeouts(struct net *net)
246{
Gao feng4f71d802012-05-28 21:04:21 +0000247 return gre_pernet(net)->gre_timeouts;
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100248}
249
Patrick McHardyf09943f2006-12-02 22:09:41 -0800250/* Returns verdict for packet, and may modify conntrack */
251static int gre_packet(struct nf_conn *ct,
252 const struct sk_buff *skb,
253 unsigned int dataoff,
254 enum ip_conntrack_info ctinfo,
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200255 u_int8_t pf,
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100256 unsigned int hooknum,
257 unsigned int *timeouts)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800258{
259 /* If we've seen traffic both ways, this is a GRE connection.
260 * Extend timeout. */
261 if (ct->status & IPS_SEEN_REPLY) {
262 nf_ct_refresh_acct(ct, ctinfo, skb,
263 ct->proto.gre.stream_timeout);
264 /* Also, more likely to be important, and not a probe. */
Florian Westphal98d9ae82011-09-30 16:38:29 +0200265 if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
266 nf_conntrack_event_cache(IPCT_ASSURED, ct);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800267 } else
268 nf_ct_refresh_acct(ct, ctinfo, skb,
269 ct->proto.gre.timeout);
270
271 return NF_ACCEPT;
272}
273
274/* Called when a new connection for this protocol found. */
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200275static bool gre_new(struct nf_conn *ct, const struct sk_buff *skb,
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100276 unsigned int dataoff, unsigned int *timeouts)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800277{
Patrick McHardy0d537782007-07-07 22:39:38 -0700278 pr_debug(": ");
Jan Engelhardt3c9fba62008-04-14 11:15:54 +0200279 nf_ct_dump_tuple(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800280
281 /* initialize to sane value. Ideally a conntrack helper
282 * (e.g. in case of pptp) is increasing them */
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100283 ct->proto.gre.stream_timeout = timeouts[GRE_CT_REPLIED];
284 ct->proto.gre.timeout = timeouts[GRE_CT_UNREPLIED];
Patrick McHardyf09943f2006-12-02 22:09:41 -0800285
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200286 return true;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800287}
288
289/* Called when a conntrack entry has already been removed from the hashes
290 * and is about to be deleted from memory */
291static void gre_destroy(struct nf_conn *ct)
292{
293 struct nf_conn *master = ct->master;
Patrick McHardy0d537782007-07-07 22:39:38 -0700294 pr_debug(" entering\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800295
296 if (!master)
Patrick McHardy0d537782007-07-07 22:39:38 -0700297 pr_debug("no master !?!\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800298 else
299 nf_ct_gre_keymap_destroy(master);
300}
301
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100302#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
303
304#include <linux/netfilter/nfnetlink.h>
305#include <linux/netfilter/nfnetlink_cttimeout.h>
306
Gao feng8264deb2012-05-28 21:04:23 +0000307static int gre_timeout_nlattr_to_obj(struct nlattr *tb[],
308 struct net *net, void *data)
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100309{
310 unsigned int *timeouts = data;
Gao feng8264deb2012-05-28 21:04:23 +0000311 struct netns_proto_gre *net_gre = gre_pernet(net);
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100312
313 /* set default timeouts for GRE. */
Gao feng8264deb2012-05-28 21:04:23 +0000314 timeouts[GRE_CT_UNREPLIED] = net_gre->gre_timeouts[GRE_CT_UNREPLIED];
315 timeouts[GRE_CT_REPLIED] = net_gre->gre_timeouts[GRE_CT_REPLIED];
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100316
317 if (tb[CTA_TIMEOUT_GRE_UNREPLIED]) {
318 timeouts[GRE_CT_UNREPLIED] =
319 ntohl(nla_get_be32(tb[CTA_TIMEOUT_GRE_UNREPLIED])) * HZ;
320 }
321 if (tb[CTA_TIMEOUT_GRE_REPLIED]) {
322 timeouts[GRE_CT_REPLIED] =
323 ntohl(nla_get_be32(tb[CTA_TIMEOUT_GRE_REPLIED])) * HZ;
324 }
325 return 0;
326}
327
328static int
329gre_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
330{
331 const unsigned int *timeouts = data;
332
David S. Miller242ddfc2012-04-01 18:52:03 -0400333 if (nla_put_be32(skb, CTA_TIMEOUT_GRE_UNREPLIED,
334 htonl(timeouts[GRE_CT_UNREPLIED] / HZ)) ||
335 nla_put_be32(skb, CTA_TIMEOUT_GRE_REPLIED,
336 htonl(timeouts[GRE_CT_REPLIED] / HZ)))
337 goto nla_put_failure;
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100338 return 0;
339
340nla_put_failure:
341 return -ENOSPC;
342}
343
344static const struct nla_policy
345gre_timeout_nla_policy[CTA_TIMEOUT_GRE_MAX+1] = {
346 [CTA_TIMEOUT_GRE_UNREPLIED] = { .type = NLA_U32 },
347 [CTA_TIMEOUT_GRE_REPLIED] = { .type = NLA_U32 },
348};
349#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
350
Gao fengf1caad22012-06-21 04:36:39 +0000351static int gre_init_net(struct net *net, u_int16_t proto)
Gao feng4f71d802012-05-28 21:04:21 +0000352{
353 struct netns_proto_gre *net_gre = gre_pernet(net);
354 int i;
355
356 rwlock_init(&net_gre->keymap_lock);
357 INIT_LIST_HEAD(&net_gre->keymap_list);
358 for (i = 0; i < GRE_CT_MAX; i++)
359 net_gre->gre_timeouts[i] = gre_timeouts[i];
360
361 return 0;
362}
363
Patrick McHardyf09943f2006-12-02 22:09:41 -0800364/* protocol helper struct */
Patrick McHardy61075af2007-07-14 20:48:19 -0700365static struct nf_conntrack_l4proto nf_conntrack_l4proto_gre4 __read_mostly = {
Patrick McHardyf09943f2006-12-02 22:09:41 -0800366 .l3proto = AF_INET,
367 .l4proto = IPPROTO_GRE,
368 .name = "gre",
369 .pkt_to_tuple = gre_pkt_to_tuple,
370 .invert_tuple = gre_invert_tuple,
371 .print_tuple = gre_print_tuple,
372 .print_conntrack = gre_print_conntrack,
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100373 .get_timeouts = gre_get_timeouts,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800374 .packet = gre_packet,
375 .new = gre_new,
376 .destroy = gre_destroy,
377 .me = THIS_MODULE,
Igor Maravićc0cd1152011-12-12 02:58:24 +0000378#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Patrick McHardyfdf70832007-09-28 14:37:41 -0700379 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100380 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
Patrick McHardyfdf70832007-09-28 14:37:41 -0700381 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
Patrick McHardyf73e9242007-09-28 14:39:55 -0700382 .nla_policy = nf_ct_port_nla_policy,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800383#endif
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100384#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
385 .ctnl_timeout = {
386 .nlattr_to_obj = gre_timeout_nlattr_to_obj,
387 .obj_to_nlattr = gre_timeout_obj_to_nlattr,
388 .nlattr_max = CTA_TIMEOUT_GRE_MAX,
389 .obj_size = sizeof(unsigned int) * GRE_CT_MAX,
390 .nla_policy = gre_timeout_nla_policy,
391 },
392#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
Gao feng4f71d802012-05-28 21:04:21 +0000393 .net_id = &proto_gre_net_id,
394 .init_net = gre_init_net,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800395};
396
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200397static int proto_gre_net_init(struct net *net)
398{
Gao feng4f71d802012-05-28 21:04:21 +0000399 int ret = 0;
Gao fengc296bb42013-01-23 12:51:10 +0100400 ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_gre4);
Gao feng4f71d802012-05-28 21:04:21 +0000401 if (ret < 0)
Gao fengc296bb42013-01-23 12:51:10 +0100402 pr_err("nf_conntrack_gre4: pernet registration failed.\n");
Gao feng4f71d802012-05-28 21:04:21 +0000403 return ret;
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200404}
405
406static void proto_gre_net_exit(struct net *net)
407{
Gao fengc296bb42013-01-23 12:51:10 +0100408 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_gre4);
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200409 nf_ct_gre_keymap_flush(net);
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200410}
411
412static struct pernet_operations proto_gre_net_ops = {
413 .init = proto_gre_net_init,
414 .exit = proto_gre_net_exit,
Eric W. Biedermane8d02882009-11-29 15:46:08 +0000415 .id = &proto_gre_net_id,
416 .size = sizeof(struct netns_proto_gre),
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200417};
418
Patrick McHardyf09943f2006-12-02 22:09:41 -0800419static int __init nf_ct_proto_gre_init(void)
420{
Gao fengc296bb42013-01-23 12:51:10 +0100421 int ret;
422
Gao fengc296bb42013-01-23 12:51:10 +0100423 ret = register_pernet_subsys(&proto_gre_net_ops);
424 if (ret < 0)
425 goto out_pernet;
426
Gao feng0d98da52013-03-07 17:20:46 +0000427 ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_gre4);
428 if (ret < 0)
429 goto out_gre4;
430
Gao fengc296bb42013-01-23 12:51:10 +0100431 return 0;
Gao fengc296bb42013-01-23 12:51:10 +0100432out_gre4:
Gao feng0d98da52013-03-07 17:20:46 +0000433 unregister_pernet_subsys(&proto_gre_net_ops);
434out_pernet:
Gao fengc296bb42013-01-23 12:51:10 +0100435 return ret;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800436}
437
Alexey Dobriyan56bc0f92008-11-20 10:01:37 +0100438static void __exit nf_ct_proto_gre_fini(void)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800439{
Gao fengc296bb42013-01-23 12:51:10 +0100440 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_gre4);
Eric W. Biedermane8d02882009-11-29 15:46:08 +0000441 unregister_pernet_subsys(&proto_gre_net_ops);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800442}
443
444module_init(nf_ct_proto_gre_init);
445module_exit(nf_ct_proto_gre_fini);
446
447MODULE_LICENSE("GPL");