Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 1 | /* |
| 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 McHardy | f229f6c | 2013-04-06 15:24:29 +0200 | [diff] [blame] | 24 | * (C) 2006-2012 Patrick McHardy <kaber@trash.net> |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 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> |
Alexey Dobriyan | 3bb0d1c | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 33 | #include <linux/netdevice.h> |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 34 | #include <linux/skbuff.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 35 | #include <linux/slab.h> |
Alexey Dobriyan | 3bb0d1c | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 36 | #include <net/dst.h> |
| 37 | #include <net/net_namespace.h> |
| 38 | #include <net/netns/generic.h> |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 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 <linux/netfilter/nf_conntrack_proto_gre.h> |
| 43 | #include <linux/netfilter/nf_conntrack_pptp.h> |
| 44 | |
Pablo Neira Ayuso | b888341 | 2012-02-28 18:56:39 +0100 | [diff] [blame] | 45 | enum grep_conntrack { |
| 46 | GRE_CT_UNREPLIED, |
| 47 | GRE_CT_REPLIED, |
| 48 | GRE_CT_MAX |
| 49 | }; |
| 50 | |
| 51 | static unsigned int gre_timeouts[GRE_CT_MAX] = { |
| 52 | [GRE_CT_UNREPLIED] = 30*HZ, |
| 53 | [GRE_CT_REPLIED] = 180*HZ, |
| 54 | }; |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 55 | |
Eric Dumazet | f99189b | 2009-11-17 10:42:49 +0000 | [diff] [blame] | 56 | static int proto_gre_net_id __read_mostly; |
Alexey Dobriyan | 3bb0d1c | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 57 | struct netns_proto_gre { |
Gao feng | 4f71d80 | 2012-05-28 21:04:21 +0000 | [diff] [blame] | 58 | struct nf_proto_net nf; |
Alexey Dobriyan | 3bb0d1c | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 59 | rwlock_t keymap_lock; |
| 60 | struct list_head keymap_list; |
Gao feng | 4f71d80 | 2012-05-28 21:04:21 +0000 | [diff] [blame] | 61 | unsigned int gre_timeouts[GRE_CT_MAX]; |
Alexey Dobriyan | 3bb0d1c | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 62 | }; |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 63 | |
Gao feng | 4f71d80 | 2012-05-28 21:04:21 +0000 | [diff] [blame] | 64 | static inline struct netns_proto_gre *gre_pernet(struct net *net) |
| 65 | { |
| 66 | return net_generic(net, proto_gre_net_id); |
| 67 | } |
| 68 | |
Alexey Dobriyan | 3bb0d1c | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 69 | void nf_ct_gre_keymap_flush(struct net *net) |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 70 | { |
Gao feng | 4f71d80 | 2012-05-28 21:04:21 +0000 | [diff] [blame] | 71 | struct netns_proto_gre *net_gre = gre_pernet(net); |
Alexey Dobriyan | 51807e9 | 2008-09-07 18:20:36 -0700 | [diff] [blame] | 72 | struct nf_ct_gre_keymap *km, *tmp; |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 73 | |
Alexey Dobriyan | 3bb0d1c | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 74 | write_lock_bh(&net_gre->keymap_lock); |
| 75 | list_for_each_entry_safe(km, tmp, &net_gre->keymap_list, list) { |
Alexey Dobriyan | 51807e9 | 2008-09-07 18:20:36 -0700 | [diff] [blame] | 76 | list_del(&km->list); |
| 77 | kfree(km); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 78 | } |
Alexey Dobriyan | 3bb0d1c | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 79 | write_unlock_bh(&net_gre->keymap_lock); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 80 | } |
| 81 | EXPORT_SYMBOL(nf_ct_gre_keymap_flush); |
| 82 | |
| 83 | static inline int gre_key_cmpfn(const struct nf_ct_gre_keymap *km, |
| 84 | const struct nf_conntrack_tuple *t) |
| 85 | { |
| 86 | return km->tuple.src.l3num == t->src.l3num && |
| 87 | !memcmp(&km->tuple.src.u3, &t->src.u3, sizeof(t->src.u3)) && |
| 88 | !memcmp(&km->tuple.dst.u3, &t->dst.u3, sizeof(t->dst.u3)) && |
| 89 | km->tuple.dst.protonum == t->dst.protonum && |
| 90 | km->tuple.dst.u.all == t->dst.u.all; |
| 91 | } |
| 92 | |
| 93 | /* look up the source key for a given tuple */ |
Alexey Dobriyan | 3bb0d1c | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 94 | static __be16 gre_keymap_lookup(struct net *net, struct nf_conntrack_tuple *t) |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 95 | { |
Gao feng | 4f71d80 | 2012-05-28 21:04:21 +0000 | [diff] [blame] | 96 | struct netns_proto_gre *net_gre = gre_pernet(net); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 97 | struct nf_ct_gre_keymap *km; |
| 98 | __be16 key = 0; |
| 99 | |
Alexey Dobriyan | 3bb0d1c | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 100 | read_lock_bh(&net_gre->keymap_lock); |
| 101 | list_for_each_entry(km, &net_gre->keymap_list, list) { |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 102 | if (gre_key_cmpfn(km, t)) { |
| 103 | key = km->tuple.src.u.gre.key; |
| 104 | break; |
| 105 | } |
| 106 | } |
Alexey Dobriyan | 3bb0d1c | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 107 | read_unlock_bh(&net_gre->keymap_lock); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 108 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 109 | pr_debug("lookup src key 0x%x for ", key); |
Jan Engelhardt | 3c9fba6 | 2008-04-14 11:15:54 +0200 | [diff] [blame] | 110 | nf_ct_dump_tuple(t); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 111 | |
| 112 | return key; |
| 113 | } |
| 114 | |
| 115 | /* add a single keymap entry, associate with specified master ct */ |
| 116 | int nf_ct_gre_keymap_add(struct nf_conn *ct, enum ip_conntrack_dir dir, |
| 117 | struct nf_conntrack_tuple *t) |
| 118 | { |
Alexey Dobriyan | 3bb0d1c | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 119 | struct net *net = nf_ct_net(ct); |
Gao feng | 4f71d80 | 2012-05-28 21:04:21 +0000 | [diff] [blame] | 120 | struct netns_proto_gre *net_gre = gre_pernet(net); |
Pablo Neira Ayuso | 1afc567 | 2012-06-07 12:11:50 +0200 | [diff] [blame] | 121 | struct nf_ct_pptp_master *ct_pptp_info = nfct_help_data(ct); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 122 | struct nf_ct_gre_keymap **kmp, *km; |
| 123 | |
Pablo Neira Ayuso | 1afc567 | 2012-06-07 12:11:50 +0200 | [diff] [blame] | 124 | kmp = &ct_pptp_info->keymap[dir]; |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 125 | if (*kmp) { |
| 126 | /* check whether it's a retransmission */ |
Alexey Dobriyan | 3bb0d1c | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 127 | read_lock_bh(&net_gre->keymap_lock); |
| 128 | list_for_each_entry(km, &net_gre->keymap_list, list) { |
Alexey Dobriyan | 887464a | 2008-09-07 18:20:08 -0700 | [diff] [blame] | 129 | if (gre_key_cmpfn(km, t) && km == *kmp) { |
Alexey Dobriyan | 3bb0d1c | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 130 | read_unlock_bh(&net_gre->keymap_lock); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 131 | return 0; |
Alexey Dobriyan | 887464a | 2008-09-07 18:20:08 -0700 | [diff] [blame] | 132 | } |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 133 | } |
Alexey Dobriyan | 3bb0d1c | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 134 | read_unlock_bh(&net_gre->keymap_lock); |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 135 | pr_debug("trying to override keymap_%s for ct %p\n", |
| 136 | dir == IP_CT_DIR_REPLY ? "reply" : "orig", ct); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 137 | return -EEXIST; |
| 138 | } |
| 139 | |
| 140 | km = kmalloc(sizeof(*km), GFP_ATOMIC); |
| 141 | if (!km) |
| 142 | return -ENOMEM; |
| 143 | memcpy(&km->tuple, t, sizeof(*t)); |
| 144 | *kmp = km; |
| 145 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 146 | pr_debug("adding new entry %p: ", km); |
Jan Engelhardt | 3c9fba6 | 2008-04-14 11:15:54 +0200 | [diff] [blame] | 147 | nf_ct_dump_tuple(&km->tuple); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 148 | |
Alexey Dobriyan | 3bb0d1c | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 149 | write_lock_bh(&net_gre->keymap_lock); |
| 150 | list_add_tail(&km->list, &net_gre->keymap_list); |
| 151 | write_unlock_bh(&net_gre->keymap_lock); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_add); |
| 156 | |
| 157 | /* destroy the keymap entries associated with specified master ct */ |
| 158 | void nf_ct_gre_keymap_destroy(struct nf_conn *ct) |
| 159 | { |
Alexey Dobriyan | 3bb0d1c | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 160 | struct net *net = nf_ct_net(ct); |
Gao feng | 4f71d80 | 2012-05-28 21:04:21 +0000 | [diff] [blame] | 161 | struct netns_proto_gre *net_gre = gre_pernet(net); |
Pablo Neira Ayuso | 1afc567 | 2012-06-07 12:11:50 +0200 | [diff] [blame] | 162 | struct nf_ct_pptp_master *ct_pptp_info = nfct_help_data(ct); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 163 | enum ip_conntrack_dir dir; |
| 164 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 165 | pr_debug("entering for ct %p\n", ct); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 166 | |
Alexey Dobriyan | 3bb0d1c | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 167 | write_lock_bh(&net_gre->keymap_lock); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 168 | for (dir = IP_CT_DIR_ORIGINAL; dir < IP_CT_DIR_MAX; dir++) { |
Pablo Neira Ayuso | 1afc567 | 2012-06-07 12:11:50 +0200 | [diff] [blame] | 169 | if (ct_pptp_info->keymap[dir]) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 170 | pr_debug("removing %p from list\n", |
Pablo Neira Ayuso | 1afc567 | 2012-06-07 12:11:50 +0200 | [diff] [blame] | 171 | ct_pptp_info->keymap[dir]); |
| 172 | list_del(&ct_pptp_info->keymap[dir]->list); |
| 173 | kfree(ct_pptp_info->keymap[dir]); |
| 174 | ct_pptp_info->keymap[dir] = NULL; |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 175 | } |
| 176 | } |
Alexey Dobriyan | 3bb0d1c | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 177 | write_unlock_bh(&net_gre->keymap_lock); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 178 | } |
| 179 | EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_destroy); |
| 180 | |
| 181 | /* PUBLIC CONNTRACK PROTO HELPER FUNCTIONS */ |
| 182 | |
| 183 | /* invert gre part of tuple */ |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 184 | static bool gre_invert_tuple(struct nf_conntrack_tuple *tuple, |
| 185 | const struct nf_conntrack_tuple *orig) |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 186 | { |
| 187 | tuple->dst.u.gre.key = orig->src.u.gre.key; |
| 188 | tuple->src.u.gre.key = orig->dst.u.gre.key; |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 189 | return true; |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /* gre hdr info to tuple */ |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 193 | static bool gre_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff, |
| 194 | struct nf_conntrack_tuple *tuple) |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 195 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 196 | struct net *net = dev_net(skb->dev ? skb->dev : skb_dst(skb)->dev); |
Jan Engelhardt | dc35dc5 | 2008-01-31 04:52:46 -0800 | [diff] [blame] | 197 | const struct gre_hdr_pptp *pgrehdr; |
| 198 | struct gre_hdr_pptp _pgrehdr; |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 199 | __be16 srckey; |
Jan Engelhardt | dc35dc5 | 2008-01-31 04:52:46 -0800 | [diff] [blame] | 200 | const struct gre_hdr *grehdr; |
| 201 | struct gre_hdr _grehdr; |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 202 | |
| 203 | /* first only delinearize old RFC1701 GRE header */ |
| 204 | grehdr = skb_header_pointer(skb, dataoff, sizeof(_grehdr), &_grehdr); |
| 205 | if (!grehdr || grehdr->version != GRE_VERSION_PPTP) { |
| 206 | /* try to behave like "nf_conntrack_proto_generic" */ |
| 207 | tuple->src.u.all = 0; |
| 208 | tuple->dst.u.all = 0; |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 209 | return true; |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /* PPTP header is variable length, only need up to the call_id field */ |
| 213 | pgrehdr = skb_header_pointer(skb, dataoff, 8, &_pgrehdr); |
| 214 | if (!pgrehdr) |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 215 | return true; |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 216 | |
| 217 | if (ntohs(grehdr->protocol) != GRE_PROTOCOL_PPTP) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 218 | pr_debug("GRE_VERSION_PPTP but unknown proto\n"); |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 219 | return false; |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | tuple->dst.u.gre.key = pgrehdr->call_id; |
Alexey Dobriyan | 3bb0d1c | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 223 | srckey = gre_keymap_lookup(net, tuple); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 224 | tuple->src.u.gre.key = srckey; |
| 225 | |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 226 | return true; |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* print gre part of tuple */ |
| 230 | static int gre_print_tuple(struct seq_file *s, |
| 231 | const struct nf_conntrack_tuple *tuple) |
| 232 | { |
| 233 | return seq_printf(s, "srckey=0x%x dstkey=0x%x ", |
| 234 | ntohs(tuple->src.u.gre.key), |
| 235 | ntohs(tuple->dst.u.gre.key)); |
| 236 | } |
| 237 | |
| 238 | /* print private data for conntrack */ |
Patrick McHardy | 440f0d5 | 2009-06-10 14:32:47 +0200 | [diff] [blame] | 239 | static int gre_print_conntrack(struct seq_file *s, struct nf_conn *ct) |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 240 | { |
| 241 | return seq_printf(s, "timeout=%u, stream_timeout=%u ", |
| 242 | (ct->proto.gre.timeout / HZ), |
| 243 | (ct->proto.gre.stream_timeout / HZ)); |
| 244 | } |
| 245 | |
Pablo Neira Ayuso | 2c8503f | 2012-02-28 18:23:31 +0100 | [diff] [blame] | 246 | static unsigned int *gre_get_timeouts(struct net *net) |
| 247 | { |
Gao feng | 4f71d80 | 2012-05-28 21:04:21 +0000 | [diff] [blame] | 248 | return gre_pernet(net)->gre_timeouts; |
Pablo Neira Ayuso | 2c8503f | 2012-02-28 18:23:31 +0100 | [diff] [blame] | 249 | } |
| 250 | |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 251 | /* Returns verdict for packet, and may modify conntrack */ |
| 252 | static int gre_packet(struct nf_conn *ct, |
| 253 | const struct sk_buff *skb, |
| 254 | unsigned int dataoff, |
| 255 | enum ip_conntrack_info ctinfo, |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 256 | u_int8_t pf, |
Pablo Neira Ayuso | 2c8503f | 2012-02-28 18:23:31 +0100 | [diff] [blame] | 257 | unsigned int hooknum, |
| 258 | unsigned int *timeouts) |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 259 | { |
| 260 | /* If we've seen traffic both ways, this is a GRE connection. |
| 261 | * Extend timeout. */ |
| 262 | if (ct->status & IPS_SEEN_REPLY) { |
| 263 | nf_ct_refresh_acct(ct, ctinfo, skb, |
| 264 | ct->proto.gre.stream_timeout); |
| 265 | /* Also, more likely to be important, and not a probe. */ |
Florian Westphal | 98d9ae8 | 2011-09-30 16:38:29 +0200 | [diff] [blame] | 266 | if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status)) |
| 267 | nf_conntrack_event_cache(IPCT_ASSURED, ct); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 268 | } else |
| 269 | nf_ct_refresh_acct(ct, ctinfo, skb, |
| 270 | ct->proto.gre.timeout); |
| 271 | |
| 272 | return NF_ACCEPT; |
| 273 | } |
| 274 | |
| 275 | /* Called when a new connection for this protocol found. */ |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 276 | static bool gre_new(struct nf_conn *ct, const struct sk_buff *skb, |
Pablo Neira Ayuso | 2c8503f | 2012-02-28 18:23:31 +0100 | [diff] [blame] | 277 | unsigned int dataoff, unsigned int *timeouts) |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 278 | { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 279 | pr_debug(": "); |
Jan Engelhardt | 3c9fba6 | 2008-04-14 11:15:54 +0200 | [diff] [blame] | 280 | nf_ct_dump_tuple(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 281 | |
| 282 | /* initialize to sane value. Ideally a conntrack helper |
| 283 | * (e.g. in case of pptp) is increasing them */ |
Pablo Neira Ayuso | 2c8503f | 2012-02-28 18:23:31 +0100 | [diff] [blame] | 284 | ct->proto.gre.stream_timeout = timeouts[GRE_CT_REPLIED]; |
| 285 | ct->proto.gre.timeout = timeouts[GRE_CT_UNREPLIED]; |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 286 | |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 287 | return true; |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /* Called when a conntrack entry has already been removed from the hashes |
| 291 | * and is about to be deleted from memory */ |
| 292 | static void gre_destroy(struct nf_conn *ct) |
| 293 | { |
| 294 | struct nf_conn *master = ct->master; |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 295 | pr_debug(" entering\n"); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 296 | |
| 297 | if (!master) |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 298 | pr_debug("no master !?!\n"); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 299 | else |
| 300 | nf_ct_gre_keymap_destroy(master); |
| 301 | } |
| 302 | |
Pablo Neira Ayuso | 5097846 | 2012-02-28 19:13:48 +0100 | [diff] [blame] | 303 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT) |
| 304 | |
| 305 | #include <linux/netfilter/nfnetlink.h> |
| 306 | #include <linux/netfilter/nfnetlink_cttimeout.h> |
| 307 | |
Gao feng | 8264deb | 2012-05-28 21:04:23 +0000 | [diff] [blame] | 308 | static int gre_timeout_nlattr_to_obj(struct nlattr *tb[], |
| 309 | struct net *net, void *data) |
Pablo Neira Ayuso | 5097846 | 2012-02-28 19:13:48 +0100 | [diff] [blame] | 310 | { |
| 311 | unsigned int *timeouts = data; |
Gao feng | 8264deb | 2012-05-28 21:04:23 +0000 | [diff] [blame] | 312 | struct netns_proto_gre *net_gre = gre_pernet(net); |
Pablo Neira Ayuso | 5097846 | 2012-02-28 19:13:48 +0100 | [diff] [blame] | 313 | |
| 314 | /* set default timeouts for GRE. */ |
Gao feng | 8264deb | 2012-05-28 21:04:23 +0000 | [diff] [blame] | 315 | timeouts[GRE_CT_UNREPLIED] = net_gre->gre_timeouts[GRE_CT_UNREPLIED]; |
| 316 | timeouts[GRE_CT_REPLIED] = net_gre->gre_timeouts[GRE_CT_REPLIED]; |
Pablo Neira Ayuso | 5097846 | 2012-02-28 19:13:48 +0100 | [diff] [blame] | 317 | |
| 318 | if (tb[CTA_TIMEOUT_GRE_UNREPLIED]) { |
| 319 | timeouts[GRE_CT_UNREPLIED] = |
| 320 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_GRE_UNREPLIED])) * HZ; |
| 321 | } |
| 322 | if (tb[CTA_TIMEOUT_GRE_REPLIED]) { |
| 323 | timeouts[GRE_CT_REPLIED] = |
| 324 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_GRE_REPLIED])) * HZ; |
| 325 | } |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | static int |
| 330 | gre_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data) |
| 331 | { |
| 332 | const unsigned int *timeouts = data; |
| 333 | |
David S. Miller | 242ddfc | 2012-04-01 18:52:03 -0400 | [diff] [blame] | 334 | if (nla_put_be32(skb, CTA_TIMEOUT_GRE_UNREPLIED, |
| 335 | htonl(timeouts[GRE_CT_UNREPLIED] / HZ)) || |
| 336 | nla_put_be32(skb, CTA_TIMEOUT_GRE_REPLIED, |
| 337 | htonl(timeouts[GRE_CT_REPLIED] / HZ))) |
| 338 | goto nla_put_failure; |
Pablo Neira Ayuso | 5097846 | 2012-02-28 19:13:48 +0100 | [diff] [blame] | 339 | return 0; |
| 340 | |
| 341 | nla_put_failure: |
| 342 | return -ENOSPC; |
| 343 | } |
| 344 | |
| 345 | static const struct nla_policy |
| 346 | gre_timeout_nla_policy[CTA_TIMEOUT_GRE_MAX+1] = { |
| 347 | [CTA_TIMEOUT_GRE_UNREPLIED] = { .type = NLA_U32 }, |
| 348 | [CTA_TIMEOUT_GRE_REPLIED] = { .type = NLA_U32 }, |
| 349 | }; |
| 350 | #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */ |
| 351 | |
Gao feng | f1caad2 | 2012-06-21 04:36:39 +0000 | [diff] [blame] | 352 | static int gre_init_net(struct net *net, u_int16_t proto) |
Gao feng | 4f71d80 | 2012-05-28 21:04:21 +0000 | [diff] [blame] | 353 | { |
| 354 | struct netns_proto_gre *net_gre = gre_pernet(net); |
| 355 | int i; |
| 356 | |
| 357 | rwlock_init(&net_gre->keymap_lock); |
| 358 | INIT_LIST_HEAD(&net_gre->keymap_list); |
| 359 | for (i = 0; i < GRE_CT_MAX; i++) |
| 360 | net_gre->gre_timeouts[i] = gre_timeouts[i]; |
| 361 | |
| 362 | return 0; |
| 363 | } |
| 364 | |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 365 | /* protocol helper struct */ |
Patrick McHardy | 61075af | 2007-07-14 20:48:19 -0700 | [diff] [blame] | 366 | static struct nf_conntrack_l4proto nf_conntrack_l4proto_gre4 __read_mostly = { |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 367 | .l3proto = AF_INET, |
| 368 | .l4proto = IPPROTO_GRE, |
| 369 | .name = "gre", |
| 370 | .pkt_to_tuple = gre_pkt_to_tuple, |
| 371 | .invert_tuple = gre_invert_tuple, |
| 372 | .print_tuple = gre_print_tuple, |
| 373 | .print_conntrack = gre_print_conntrack, |
Pablo Neira Ayuso | 2c8503f | 2012-02-28 18:23:31 +0100 | [diff] [blame] | 374 | .get_timeouts = gre_get_timeouts, |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 375 | .packet = gre_packet, |
| 376 | .new = gre_new, |
| 377 | .destroy = gre_destroy, |
| 378 | .me = THIS_MODULE, |
Igor Maravić | c0cd115 | 2011-12-12 02:58:24 +0000 | [diff] [blame] | 379 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 380 | .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr, |
Holger Eitzenberger | a400c30 | 2009-03-25 21:53:39 +0100 | [diff] [blame] | 381 | .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size, |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 382 | .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple, |
Patrick McHardy | f73e924 | 2007-09-28 14:39:55 -0700 | [diff] [blame] | 383 | .nla_policy = nf_ct_port_nla_policy, |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 384 | #endif |
Pablo Neira Ayuso | 5097846 | 2012-02-28 19:13:48 +0100 | [diff] [blame] | 385 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT) |
| 386 | .ctnl_timeout = { |
| 387 | .nlattr_to_obj = gre_timeout_nlattr_to_obj, |
| 388 | .obj_to_nlattr = gre_timeout_obj_to_nlattr, |
| 389 | .nlattr_max = CTA_TIMEOUT_GRE_MAX, |
| 390 | .obj_size = sizeof(unsigned int) * GRE_CT_MAX, |
| 391 | .nla_policy = gre_timeout_nla_policy, |
| 392 | }, |
| 393 | #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */ |
Gao feng | 4f71d80 | 2012-05-28 21:04:21 +0000 | [diff] [blame] | 394 | .net_id = &proto_gre_net_id, |
| 395 | .init_net = gre_init_net, |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 396 | }; |
| 397 | |
Alexey Dobriyan | 3bb0d1c | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 398 | static int proto_gre_net_init(struct net *net) |
| 399 | { |
Gao feng | 4f71d80 | 2012-05-28 21:04:21 +0000 | [diff] [blame] | 400 | int ret = 0; |
Gao feng | c296bb4 | 2013-01-23 12:51:10 +0100 | [diff] [blame] | 401 | ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_gre4); |
Gao feng | 4f71d80 | 2012-05-28 21:04:21 +0000 | [diff] [blame] | 402 | if (ret < 0) |
Gao feng | c296bb4 | 2013-01-23 12:51:10 +0100 | [diff] [blame] | 403 | pr_err("nf_conntrack_gre4: pernet registration failed.\n"); |
Gao feng | 4f71d80 | 2012-05-28 21:04:21 +0000 | [diff] [blame] | 404 | return ret; |
Alexey Dobriyan | 3bb0d1c | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | static void proto_gre_net_exit(struct net *net) |
| 408 | { |
Gao feng | c296bb4 | 2013-01-23 12:51:10 +0100 | [diff] [blame] | 409 | nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_gre4); |
Alexey Dobriyan | 3bb0d1c | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 410 | nf_ct_gre_keymap_flush(net); |
Alexey Dobriyan | 3bb0d1c | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | static struct pernet_operations proto_gre_net_ops = { |
| 414 | .init = proto_gre_net_init, |
| 415 | .exit = proto_gre_net_exit, |
Eric W. Biederman | e8d0288 | 2009-11-29 15:46:08 +0000 | [diff] [blame] | 416 | .id = &proto_gre_net_id, |
| 417 | .size = sizeof(struct netns_proto_gre), |
Alexey Dobriyan | 3bb0d1c | 2008-10-08 11:35:10 +0200 | [diff] [blame] | 418 | }; |
| 419 | |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 420 | static int __init nf_ct_proto_gre_init(void) |
| 421 | { |
Gao feng | c296bb4 | 2013-01-23 12:51:10 +0100 | [diff] [blame] | 422 | int ret; |
| 423 | |
Gao feng | c296bb4 | 2013-01-23 12:51:10 +0100 | [diff] [blame] | 424 | ret = register_pernet_subsys(&proto_gre_net_ops); |
| 425 | if (ret < 0) |
| 426 | goto out_pernet; |
| 427 | |
Gao feng | 0d98da5 | 2013-03-07 17:20:46 +0000 | [diff] [blame] | 428 | ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_gre4); |
| 429 | if (ret < 0) |
| 430 | goto out_gre4; |
| 431 | |
Gao feng | c296bb4 | 2013-01-23 12:51:10 +0100 | [diff] [blame] | 432 | return 0; |
Gao feng | c296bb4 | 2013-01-23 12:51:10 +0100 | [diff] [blame] | 433 | out_gre4: |
Gao feng | 0d98da5 | 2013-03-07 17:20:46 +0000 | [diff] [blame] | 434 | unregister_pernet_subsys(&proto_gre_net_ops); |
| 435 | out_pernet: |
Gao feng | c296bb4 | 2013-01-23 12:51:10 +0100 | [diff] [blame] | 436 | return ret; |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 437 | } |
| 438 | |
Alexey Dobriyan | 56bc0f9 | 2008-11-20 10:01:37 +0100 | [diff] [blame] | 439 | static void __exit nf_ct_proto_gre_fini(void) |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 440 | { |
Gao feng | c296bb4 | 2013-01-23 12:51:10 +0100 | [diff] [blame] | 441 | nf_ct_l4proto_unregister(&nf_conntrack_l4proto_gre4); |
Eric W. Biederman | e8d0288 | 2009-11-29 15:46:08 +0000 | [diff] [blame] | 442 | unregister_pernet_subsys(&proto_gre_net_ops); |
Patrick McHardy | f09943f | 2006-12-02 22:09:41 -0800 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | module_init(nf_ct_proto_gre_init); |
| 446 | module_exit(nf_ct_proto_gre_fini); |
| 447 | |
| 448 | MODULE_LICENSE("GPL"); |