Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [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 | * |
| 24 | */ |
| 25 | |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 26 | #include <linux/module.h> |
| 27 | #include <linux/types.h> |
| 28 | #include <linux/timer.h> |
| 29 | #include <linux/netfilter.h> |
| 30 | #include <linux/ip.h> |
| 31 | #include <linux/in.h> |
| 32 | #include <linux/list.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 33 | #include <linux/seq_file.h> |
David S. Miller | f09484f | 2006-01-17 02:42:02 -0800 | [diff] [blame] | 34 | #include <linux/interrupt.h> |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 35 | |
| 36 | static DEFINE_RWLOCK(ip_ct_gre_lock); |
| 37 | #define ASSERT_READ_LOCK(x) |
| 38 | #define ASSERT_WRITE_LOCK(x) |
| 39 | |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 40 | #include <linux/netfilter_ipv4/ip_conntrack_protocol.h> |
| 41 | #include <linux/netfilter_ipv4/ip_conntrack_helper.h> |
| 42 | #include <linux/netfilter_ipv4/ip_conntrack_core.h> |
| 43 | |
| 44 | #include <linux/netfilter_ipv4/ip_conntrack_proto_gre.h> |
| 45 | #include <linux/netfilter_ipv4/ip_conntrack_pptp.h> |
| 46 | |
| 47 | MODULE_LICENSE("GPL"); |
| 48 | MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>"); |
| 49 | MODULE_DESCRIPTION("netfilter connection tracking protocol helper for GRE"); |
| 50 | |
| 51 | /* shamelessly stolen from ip_conntrack_proto_udp.c */ |
| 52 | #define GRE_TIMEOUT (30*HZ) |
| 53 | #define GRE_STREAM_TIMEOUT (180*HZ) |
| 54 | |
| 55 | #if 0 |
| 56 | #define DEBUGP(format, args...) printk(KERN_DEBUG "%s:%s: " format, __FILE__, __FUNCTION__, ## args) |
| 57 | #define DUMP_TUPLE_GRE(x) printk("%u.%u.%u.%u:0x%x -> %u.%u.%u.%u:0x%x\n", \ |
| 58 | NIPQUAD((x)->src.ip), ntohs((x)->src.u.gre.key), \ |
| 59 | NIPQUAD((x)->dst.ip), ntohs((x)->dst.u.gre.key)) |
| 60 | #else |
| 61 | #define DEBUGP(x, args...) |
| 62 | #define DUMP_TUPLE_GRE(x) |
| 63 | #endif |
| 64 | |
| 65 | /* GRE KEYMAP HANDLING FUNCTIONS */ |
| 66 | static LIST_HEAD(gre_keymap_list); |
| 67 | |
| 68 | static inline int gre_key_cmpfn(const struct ip_ct_gre_keymap *km, |
| 69 | const struct ip_conntrack_tuple *t) |
| 70 | { |
| 71 | return ((km->tuple.src.ip == t->src.ip) && |
| 72 | (km->tuple.dst.ip == t->dst.ip) && |
| 73 | (km->tuple.dst.protonum == t->dst.protonum) && |
| 74 | (km->tuple.dst.u.all == t->dst.u.all)); |
| 75 | } |
| 76 | |
| 77 | /* look up the source key for a given tuple */ |
Alexey Dobriyan | c45fb10 | 2006-05-29 18:27:32 -0700 | [diff] [blame] | 78 | static __be16 gre_keymap_lookup(struct ip_conntrack_tuple *t) |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 79 | { |
| 80 | struct ip_ct_gre_keymap *km; |
Alexey Dobriyan | c45fb10 | 2006-05-29 18:27:32 -0700 | [diff] [blame] | 81 | __be16 key = 0; |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 82 | |
| 83 | read_lock_bh(&ip_ct_gre_lock); |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame^] | 84 | list_for_each_entry(km, &gre_keymap_list, list) { |
| 85 | if (gre_key_cmpfn(km, t)) { |
| 86 | key = km->tuple.src.u.gre.key; |
| 87 | break; |
| 88 | } |
| 89 | } |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 90 | read_unlock_bh(&ip_ct_gre_lock); |
| 91 | |
| 92 | DEBUGP("lookup src key 0x%x up key for ", key); |
| 93 | DUMP_TUPLE_GRE(t); |
| 94 | |
| 95 | return key; |
| 96 | } |
| 97 | |
| 98 | /* add a single keymap entry, associate with specified master ct */ |
| 99 | int |
| 100 | ip_ct_gre_keymap_add(struct ip_conntrack *ct, |
| 101 | struct ip_conntrack_tuple *t, int reply) |
| 102 | { |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame^] | 103 | struct ip_ct_gre_keymap **exist_km, *km; |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 104 | |
| 105 | if (!ct->helper || strcmp(ct->helper->name, "pptp")) { |
| 106 | DEBUGP("refusing to add GRE keymap to non-pptp session\n"); |
| 107 | return -1; |
| 108 | } |
| 109 | |
| 110 | if (!reply) |
| 111 | exist_km = &ct->help.ct_pptp_info.keymap_orig; |
| 112 | else |
| 113 | exist_km = &ct->help.ct_pptp_info.keymap_reply; |
| 114 | |
| 115 | if (*exist_km) { |
| 116 | /* check whether it's a retransmission */ |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame^] | 117 | list_for_each_entry(km, &gre_keymap_list, list) { |
| 118 | if (gre_key_cmpfn(km, t) && km == *exist_km) |
| 119 | return 0; |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 120 | } |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 121 | DEBUGP("trying to override keymap_%s for ct %p\n", |
| 122 | reply? "reply":"orig", ct); |
| 123 | return -EEXIST; |
| 124 | } |
| 125 | |
| 126 | km = kmalloc(sizeof(*km), GFP_ATOMIC); |
| 127 | if (!km) |
| 128 | return -ENOMEM; |
| 129 | |
| 130 | memcpy(&km->tuple, t, sizeof(*t)); |
| 131 | *exist_km = km; |
| 132 | |
| 133 | DEBUGP("adding new entry %p: ", km); |
| 134 | DUMP_TUPLE_GRE(&km->tuple); |
| 135 | |
| 136 | write_lock_bh(&ip_ct_gre_lock); |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame^] | 137 | list_add_tail(&km->list, &gre_keymap_list); |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 138 | write_unlock_bh(&ip_ct_gre_lock); |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | /* destroy the keymap entries associated with specified master ct */ |
| 144 | void ip_ct_gre_keymap_destroy(struct ip_conntrack *ct) |
| 145 | { |
| 146 | DEBUGP("entering for ct %p\n", ct); |
| 147 | |
| 148 | if (!ct->helper || strcmp(ct->helper->name, "pptp")) { |
| 149 | DEBUGP("refusing to destroy GRE keymap to non-pptp session\n"); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | write_lock_bh(&ip_ct_gre_lock); |
| 154 | if (ct->help.ct_pptp_info.keymap_orig) { |
| 155 | DEBUGP("removing %p from list\n", |
| 156 | ct->help.ct_pptp_info.keymap_orig); |
| 157 | list_del(&ct->help.ct_pptp_info.keymap_orig->list); |
| 158 | kfree(ct->help.ct_pptp_info.keymap_orig); |
| 159 | ct->help.ct_pptp_info.keymap_orig = NULL; |
| 160 | } |
| 161 | if (ct->help.ct_pptp_info.keymap_reply) { |
| 162 | DEBUGP("removing %p from list\n", |
| 163 | ct->help.ct_pptp_info.keymap_reply); |
| 164 | list_del(&ct->help.ct_pptp_info.keymap_reply->list); |
| 165 | kfree(ct->help.ct_pptp_info.keymap_reply); |
| 166 | ct->help.ct_pptp_info.keymap_reply = NULL; |
| 167 | } |
| 168 | write_unlock_bh(&ip_ct_gre_lock); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | /* PUBLIC CONNTRACK PROTO HELPER FUNCTIONS */ |
| 173 | |
| 174 | /* invert gre part of tuple */ |
| 175 | static int gre_invert_tuple(struct ip_conntrack_tuple *tuple, |
| 176 | const struct ip_conntrack_tuple *orig) |
| 177 | { |
| 178 | tuple->dst.u.gre.key = orig->src.u.gre.key; |
| 179 | tuple->src.u.gre.key = orig->dst.u.gre.key; |
| 180 | |
| 181 | return 1; |
| 182 | } |
| 183 | |
| 184 | /* gre hdr info to tuple */ |
| 185 | static int gre_pkt_to_tuple(const struct sk_buff *skb, |
| 186 | unsigned int dataoff, |
| 187 | struct ip_conntrack_tuple *tuple) |
| 188 | { |
| 189 | struct gre_hdr_pptp _pgrehdr, *pgrehdr; |
Alexey Dobriyan | c45fb10 | 2006-05-29 18:27:32 -0700 | [diff] [blame] | 190 | __be16 srckey; |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 191 | struct gre_hdr _grehdr, *grehdr; |
| 192 | |
| 193 | /* first only delinearize old RFC1701 GRE header */ |
| 194 | grehdr = skb_header_pointer(skb, dataoff, sizeof(_grehdr), &_grehdr); |
| 195 | if (!grehdr || grehdr->version != GRE_VERSION_PPTP) { |
| 196 | /* try to behave like "ip_conntrack_proto_generic" */ |
| 197 | tuple->src.u.all = 0; |
| 198 | tuple->dst.u.all = 0; |
| 199 | return 1; |
| 200 | } |
| 201 | |
| 202 | /* PPTP header is variable length, only need up to the call_id field */ |
| 203 | pgrehdr = skb_header_pointer(skb, dataoff, 8, &_pgrehdr); |
| 204 | if (!pgrehdr) |
| 205 | return 1; |
| 206 | |
| 207 | if (ntohs(grehdr->protocol) != GRE_PROTOCOL_PPTP) { |
| 208 | DEBUGP("GRE_VERSION_PPTP but unknown proto\n"); |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | tuple->dst.u.gre.key = pgrehdr->call_id; |
| 213 | srckey = gre_keymap_lookup(tuple); |
| 214 | tuple->src.u.gre.key = srckey; |
| 215 | |
| 216 | return 1; |
| 217 | } |
| 218 | |
| 219 | /* print gre part of tuple */ |
| 220 | static int gre_print_tuple(struct seq_file *s, |
| 221 | const struct ip_conntrack_tuple *tuple) |
| 222 | { |
| 223 | return seq_printf(s, "srckey=0x%x dstkey=0x%x ", |
| 224 | ntohs(tuple->src.u.gre.key), |
| 225 | ntohs(tuple->dst.u.gre.key)); |
| 226 | } |
| 227 | |
| 228 | /* print private data for conntrack */ |
| 229 | static int gre_print_conntrack(struct seq_file *s, |
| 230 | const struct ip_conntrack *ct) |
| 231 | { |
| 232 | return seq_printf(s, "timeout=%u, stream_timeout=%u ", |
| 233 | (ct->proto.gre.timeout / HZ), |
| 234 | (ct->proto.gre.stream_timeout / HZ)); |
| 235 | } |
| 236 | |
| 237 | /* Returns verdict for packet, and may modify conntrack */ |
| 238 | static int gre_packet(struct ip_conntrack *ct, |
| 239 | const struct sk_buff *skb, |
| 240 | enum ip_conntrack_info conntrackinfo) |
| 241 | { |
| 242 | /* If we've seen traffic both ways, this is a GRE connection. |
| 243 | * Extend timeout. */ |
| 244 | if (ct->status & IPS_SEEN_REPLY) { |
| 245 | ip_ct_refresh_acct(ct, conntrackinfo, skb, |
| 246 | ct->proto.gre.stream_timeout); |
| 247 | /* Also, more likely to be important, and not a probe. */ |
| 248 | set_bit(IPS_ASSURED_BIT, &ct->status); |
Harald Welte | 8ddec746 | 2005-09-24 16:56:08 -0700 | [diff] [blame] | 249 | ip_conntrack_event_cache(IPCT_STATUS, skb); |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 250 | } else |
| 251 | ip_ct_refresh_acct(ct, conntrackinfo, skb, |
| 252 | ct->proto.gre.timeout); |
| 253 | |
| 254 | return NF_ACCEPT; |
| 255 | } |
| 256 | |
| 257 | /* Called when a new connection for this protocol found. */ |
| 258 | static int gre_new(struct ip_conntrack *ct, |
| 259 | const struct sk_buff *skb) |
| 260 | { |
| 261 | DEBUGP(": "); |
| 262 | DUMP_TUPLE_GRE(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple); |
| 263 | |
| 264 | /* initialize to sane value. Ideally a conntrack helper |
| 265 | * (e.g. in case of pptp) is increasing them */ |
| 266 | ct->proto.gre.stream_timeout = GRE_STREAM_TIMEOUT; |
| 267 | ct->proto.gre.timeout = GRE_TIMEOUT; |
| 268 | |
| 269 | return 1; |
| 270 | } |
| 271 | |
| 272 | /* Called when a conntrack entry has already been removed from the hashes |
| 273 | * and is about to be deleted from memory */ |
| 274 | static void gre_destroy(struct ip_conntrack *ct) |
| 275 | { |
| 276 | struct ip_conntrack *master = ct->master; |
| 277 | DEBUGP(" entering\n"); |
| 278 | |
| 279 | if (!master) |
| 280 | DEBUGP("no master !?!\n"); |
| 281 | else |
| 282 | ip_ct_gre_keymap_destroy(master); |
| 283 | } |
| 284 | |
| 285 | /* protocol helper struct */ |
| 286 | static struct ip_conntrack_protocol gre = { |
| 287 | .proto = IPPROTO_GRE, |
| 288 | .name = "gre", |
| 289 | .pkt_to_tuple = gre_pkt_to_tuple, |
| 290 | .invert_tuple = gre_invert_tuple, |
| 291 | .print_tuple = gre_print_tuple, |
| 292 | .print_conntrack = gre_print_conntrack, |
| 293 | .packet = gre_packet, |
| 294 | .new = gre_new, |
| 295 | .destroy = gre_destroy, |
| 296 | .me = THIS_MODULE, |
| 297 | #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \ |
| 298 | defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE) |
| 299 | .tuple_to_nfattr = ip_ct_port_tuple_to_nfattr, |
| 300 | .nfattr_to_tuple = ip_ct_port_nfattr_to_tuple, |
| 301 | #endif |
| 302 | }; |
| 303 | |
| 304 | /* ip_conntrack_proto_gre initialization */ |
| 305 | int __init ip_ct_proto_gre_init(void) |
| 306 | { |
| 307 | return ip_conntrack_protocol_register(&gre); |
| 308 | } |
| 309 | |
David S. Miller | a776809 | 2006-01-11 15:39:14 -0800 | [diff] [blame] | 310 | /* This cannot be __exit, as it is invoked from ip_conntrack_helper_pptp.c's |
| 311 | * init() code on errors. |
| 312 | */ |
| 313 | void ip_ct_proto_gre_fini(void) |
Harald Welte | 926b50f | 2005-09-19 15:33:08 -0700 | [diff] [blame] | 314 | { |
| 315 | struct list_head *pos, *n; |
| 316 | |
| 317 | /* delete all keymap entries */ |
| 318 | write_lock_bh(&ip_ct_gre_lock); |
| 319 | list_for_each_safe(pos, n, &gre_keymap_list) { |
| 320 | DEBUGP("deleting keymap %p at module unload time\n", pos); |
| 321 | list_del(pos); |
| 322 | kfree(pos); |
| 323 | } |
| 324 | write_unlock_bh(&ip_ct_gre_lock); |
| 325 | |
| 326 | ip_conntrack_protocol_unregister(&gre); |
| 327 | } |
| 328 | |
| 329 | EXPORT_SYMBOL(ip_ct_gre_keymap_add); |
| 330 | EXPORT_SYMBOL(ip_ct_gre_keymap_destroy); |