📄 ip_gre.c
字号:
/* * Linux NET3: GRE over IP protocol decoder. * * Authors: Alexey Kuznetsov (kuznet@ms2.inr.ac.ru) * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. * */#include <linux/config.h>#include <linux/module.h>#include <linux/types.h>#include <linux/sched.h>#include <linux/kernel.h>#include <asm/uaccess.h>#include <linux/skbuff.h>#include <linux/netdevice.h>#include <linux/in.h>#include <linux/tcp.h>#include <linux/udp.h>#include <linux/if_arp.h>#include <linux/mroute.h>#include <linux/init.h>#include <linux/in6.h>#include <linux/inetdevice.h>#include <linux/igmp.h>#include <linux/netfilter_ipv4.h>#include <net/sock.h>#include <net/ip.h>#include <net/icmp.h>#include <net/protocol.h>#include <net/ipip.h>#include <net/arp.h>#include <net/checksum.h>#include <net/inet_ecn.h>#ifdef CONFIG_IPV6#include <net/ipv6.h>#include <net/ip6_fib.h>#include <net/ip6_route.h>#endif/* Problems & solutions -------------------- 1. The most important issue is detecting local dead loops. They would cause complete host lockup in transmit, which would be "resolved" by stack overflow or, if queueing is enabled, with infinite looping in net_bh. We cannot track such dead loops during route installation, it is infeasible task. The most general solutions would be to keep skb->encapsulation counter (sort of local ttl), and silently drop packet when it expires. It is the best solution, but it supposes maintaing new variable in ALL skb, even if no tunneling is used. Current solution: t->recursion lock breaks dead loops. It looks like dev->tbusy flag, but I preferred new variable, because the semantics is different. One day, when hard_start_xmit will be multithreaded we will have to use skb->encapsulation. 2. Networking dead loops would not kill routers, but would really kill network. IP hop limit plays role of "t->recursion" in this case, if we copy it from packet being encapsulated to upper header. It is very good solution, but it introduces two problems: - Routing protocols, using packets with ttl=1 (OSPF, RIP2), do not work over tunnels. - traceroute does not work. I planned to relay ICMP from tunnel, so that this problem would be solved and traceroute output would even more informative. This idea appeared to be wrong: only Linux complies to rfc1812 now (yes, guys, Linux is the only true router now :-)), all routers (at least, in neighbourhood of mine) return only 8 bytes of payload. It is the end. Hence, if we want that OSPF worked or traceroute said something reasonable, we should search for another solution. One of them is to parse packet trying to detect inner encapsulation made by our node. It is difficult or even impossible, especially, taking into account fragmentation. TO be short, tt is not solution at all. Current solution: The solution was UNEXPECTEDLY SIMPLE. We force DF flag on tunnels with preconfigured hop limit, that is ALL. :-) Well, it does not remove the problem completely, but exponential growth of network traffic is changed to linear (branches, that exceed pmtu are pruned) and tunnel mtu fastly degrades to value <68, where looping stops. Yes, it is not good if there exists a router in the loop, which does not force DF, even when encapsulating packets have DF set. But it is not our problem! Nobody could accuse us, we made all that we could make. Even if it is your gated who injected fatal route to network, even if it were you who configured fatal static route: you are innocent. :-) 3. Really, ipv4/ipip.c, ipv4/ip_gre.c and ipv6/sit.c contain practically identical code. It would be good to glue them together, but it is not very evident, how to make them modular. sit is integral part of IPv6, ipip and gre are naturally modular. We could extract common parts (hash table, ioctl etc) to a separate module (ip_tunnel.c). Alexey Kuznetsov. */static int ipgre_tunnel_init(struct net_device *dev);/* Fallback tunnel: no source, no destination, no key, no options */static int ipgre_fb_tunnel_init(struct net_device *dev);static struct net_device ipgre_fb_tunnel_dev = { "gre0", 0x0, 0x0, 0x0, 0x0, 0, 0, 0, 0, 0, NULL, ipgre_fb_tunnel_init,};static struct ip_tunnel ipgre_fb_tunnel = { NULL, &ipgre_fb_tunnel_dev, {0, }, 0, 0, 0, 0, 0, 0, 0, {"gre0", }};/* Tunnel hash table *//* 4 hash tables: 3: (remote,local) 2: (remote,*) 1: (*,local) 0: (*,*) We require exact key match i.e. if a key is present in packet it will match only tunnel with the same key; if it is not present, it will match only keyless tunnel. All keysless packets, if not matched configured keyless tunnels will match fallback tunnel. */#define HASH_SIZE 16#define HASH(addr) ((addr^(addr>>4))&0xF)static struct ip_tunnel *tunnels[4][HASH_SIZE];#define tunnels_r_l (tunnels[3])#define tunnels_r (tunnels[2])#define tunnels_l (tunnels[1])#define tunnels_wc (tunnels[0])static rwlock_t ipgre_lock = RW_LOCK_UNLOCKED;/* Given src, dst and key, find approriate for input tunnel. */static struct ip_tunnel * ipgre_tunnel_lookup(u32 remote, u32 local, u32 key){ unsigned h0 = HASH(remote); unsigned h1 = HASH(key); struct ip_tunnel *t; for (t = tunnels_r_l[h0^h1]; t; t = t->next) { if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr) { if (t->parms.i_key == key && (t->dev->flags&IFF_UP)) return t; } } for (t = tunnels_r[h0^h1]; t; t = t->next) { if (remote == t->parms.iph.daddr) { if (t->parms.i_key == key && (t->dev->flags&IFF_UP)) return t; } } for (t = tunnels_l[h1]; t; t = t->next) { if (local == t->parms.iph.saddr || (local == t->parms.iph.daddr && MULTICAST(local))) { if (t->parms.i_key == key && (t->dev->flags&IFF_UP)) return t; } } for (t = tunnels_wc[h1]; t; t = t->next) { if (t->parms.i_key == key && (t->dev->flags&IFF_UP)) return t; } if (ipgre_fb_tunnel_dev.flags&IFF_UP) return &ipgre_fb_tunnel; return NULL;}static struct ip_tunnel **ipgre_bucket(struct ip_tunnel *t){ u32 remote = t->parms.iph.daddr; u32 local = t->parms.iph.saddr; u32 key = t->parms.i_key; unsigned h = HASH(key); int prio = 0; if (local) prio |= 1; if (remote && !MULTICAST(remote)) { prio |= 2; h ^= HASH(remote); } return &tunnels[prio][h];}static void ipgre_tunnel_link(struct ip_tunnel *t){ struct ip_tunnel **tp = ipgre_bucket(t); t->next = *tp; write_lock_bh(&ipgre_lock); *tp = t; write_unlock_bh(&ipgre_lock);}static void ipgre_tunnel_unlink(struct ip_tunnel *t){ struct ip_tunnel **tp; for (tp = ipgre_bucket(t); *tp; tp = &(*tp)->next) { if (t == *tp) { write_lock_bh(&ipgre_lock); *tp = t->next; write_unlock_bh(&ipgre_lock); break; } }}static struct ip_tunnel * ipgre_tunnel_locate(struct ip_tunnel_parm *parms, int create){ u32 remote = parms->iph.daddr; u32 local = parms->iph.saddr; u32 key = parms->i_key; struct ip_tunnel *t, **tp, *nt; struct net_device *dev; unsigned h = HASH(key); int prio = 0; if (local) prio |= 1; if (remote && !MULTICAST(remote)) { prio |= 2; h ^= HASH(remote); } for (tp = &tunnels[prio][h]; (t = *tp) != NULL; tp = &t->next) { if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr) { if (key == t->parms.i_key) return t; } } if (!create) return NULL; MOD_INC_USE_COUNT; dev = kmalloc(sizeof(*dev) + sizeof(*t), GFP_KERNEL); if (dev == NULL) { MOD_DEC_USE_COUNT; return NULL; } memset(dev, 0, sizeof(*dev) + sizeof(*t)); dev->priv = (void*)(dev+1); nt = (struct ip_tunnel*)dev->priv; nt->dev = dev; dev->init = ipgre_tunnel_init; dev->features |= NETIF_F_DYNALLOC; memcpy(&nt->parms, parms, sizeof(*parms)); nt->parms.name[IFNAMSIZ-1] = '\0'; strcpy(dev->name, nt->parms.name); if (dev->name[0] == 0) { int i; for (i=1; i<100; i++) { sprintf(dev->name, "gre%d", i); if (__dev_get_by_name(dev->name) == NULL) break; } if (i==100) goto failed; memcpy(nt->parms.name, dev->name, IFNAMSIZ); } if (register_netdevice(dev) < 0) goto failed; dev_hold(dev); ipgre_tunnel_link(nt); /* Do not decrement MOD_USE_COUNT here. */ return nt;failed: kfree(dev); MOD_DEC_USE_COUNT; return NULL;}static void ipgre_tunnel_destructor(struct net_device *dev){ if (dev != &ipgre_fb_tunnel_dev) { MOD_DEC_USE_COUNT; }}static void ipgre_tunnel_uninit(struct net_device *dev){ ipgre_tunnel_unlink((struct ip_tunnel*)dev->priv); dev_put(dev);}void ipgre_err(struct sk_buff *skb, u32 info){#ifndef I_WISH_WORLD_WERE_PERFECT/* It is not :-( All the routers (except for Linux) return only 8 bytes of packet payload. It means, that precise relaying of ICMP in the real Internet is absolutely infeasible. Moreover, Cisco "wise men" put GRE key to the third word in GRE header. It makes impossible maintaining even soft state for keyed GRE tunnels with enabled checksum. Tell them "thank you". Well, I wonder, rfc1812 was written by Cisco employee, what the hell these idiots break standrads established by themself??? */ struct iphdr *iph = (struct iphdr*)skb->data; u16 *p = (u16*)(skb->data+(iph->ihl<<2)); int grehlen = (iph->ihl<<2) + 4; int type = skb->h.icmph->type; int code = skb->h.icmph->code; struct ip_tunnel *t; u16 flags; flags = p[0]; if (flags&(GRE_CSUM|GRE_KEY|GRE_SEQ|GRE_ROUTING|GRE_VERSION)) { if (flags&(GRE_VERSION|GRE_ROUTING)) return; if (flags&GRE_KEY) { grehlen += 4; if (flags&GRE_CSUM) grehlen += 4; } } /* If only 8 bytes returned, keyed message will be dropped here */ if (skb_headlen(skb) < grehlen) return; switch (type) { default: case ICMP_PARAMETERPROB: return; case ICMP_DEST_UNREACH: switch (code) { case ICMP_SR_FAILED: case ICMP_PORT_UNREACH: /* Impossible event. */ return; case ICMP_FRAG_NEEDED: /* Soft state for pmtu is maintained by IP core. */ return; default: /* All others are translated to HOST_UNREACH. rfc2003 contains "deep thoughts" about NET_UNREACH, I believe they are just ether pollution. --ANK */ break; } break; case ICMP_TIME_EXCEEDED: if (code != ICMP_EXC_TTL) return; break; } read_lock(&ipgre_lock); t = ipgre_tunnel_lookup(iph->daddr, iph->saddr, (flags&GRE_KEY) ? *(((u32*)p) + (grehlen>>2) - 1) : 0); if (t == NULL || t->parms.iph.daddr == 0 || MULTICAST(t->parms.iph.daddr)) goto out; if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED) goto out; if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO) t->err_count++; else t->err_count = 1; t->err_time = jiffies;out: read_unlock(&ipgre_lock); return;#else struct iphdr *iph = (struct iphdr*)dp; struct iphdr *eiph; u16 *p = (u16*)(dp+(iph->ihl<<2)); int type = skb->h.icmph->type; int code = skb->h.icmph->code; int rel_type = 0; int rel_code = 0; int rel_info = 0; u16 flags; int grehlen = (iph->ihl<<2) + 4; struct sk_buff *skb2; struct rtable *rt; if (p[1] != __constant_htons(ETH_P_IP)) return; flags = p[0]; if (flags&(GRE_CSUM|GRE_KEY|GRE_SEQ|GRE_ROUTING|GRE_VERSION)) { if (flags&(GRE_VERSION|GRE_ROUTING)) return; if (flags&GRE_CSUM) grehlen += 4; if (flags&GRE_KEY) grehlen += 4; if (flags&GRE_SEQ) grehlen += 4; } if (len < grehlen + sizeof(struct iphdr)) return; eiph = (struct iphdr*)(dp + grehlen); switch (type) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -