1 diff -Nurb linux-2.6.27-660/Makefile linux-2.6.27-700/Makefile
2 --- linux-2.6.27-660/Makefile 2009-04-16 10:27:07.000000000 -0400
3 +++ linux-2.6.27-700/Makefile 2009-04-16 10:27:39.000000000 -0400
8 -EXTRAVERSION = .14-vs2.3.0.36.4
10 NAME = Trembling Tortoise
13 diff -Nurb linux-2.6.27-660/drivers/net/Kconfig linux-2.6.27-700/drivers/net/Kconfig
14 --- linux-2.6.27-660/drivers/net/Kconfig 2009-04-16 10:27:01.000000000 -0400
15 +++ linux-2.6.27-700/drivers/net/Kconfig 2009-04-16 10:27:39.000000000 -0400
18 Look at the iproute2 documentation directory for usage etc
21 + tristate "EGRE module for Ethernet over GRE Tunnels"
24 tristate "Dummy net driver support"
26 diff -Nurb linux-2.6.27-660/drivers/net/Makefile linux-2.6.27-700/drivers/net/Makefile
27 --- linux-2.6.27-660/drivers/net/Makefile 2008-10-09 18:13:53.000000000 -0400
28 +++ linux-2.6.27-700/drivers/net/Makefile 2009-04-16 10:27:39.000000000 -0400
30 # Makefile for the Linux network (ethercard) device drivers.
33 +obj-$(CONFIG_EGRE) += gre.o
34 obj-$(CONFIG_E1000) += e1000/
35 obj-$(CONFIG_E1000E) += e1000e/
36 obj-$(CONFIG_IBM_NEW_EMAC) += ibm_newemac/
37 diff -Nurb linux-2.6.27-660/drivers/net/gre.c linux-2.6.27-700/drivers/net/gre.c
38 --- linux-2.6.27-660/drivers/net/gre.c 1969-12-31 19:00:00.000000000 -0500
39 +++ linux-2.6.27-700/drivers/net/gre.c 2009-04-16 12:48:33.000000000 -0400
42 + * Linux NET3: GRE over IP protocol decoder.
44 + * Authors: Alexey Kuznetsov (kuznet@ms2.inr.ac.ru)
46 + * This program is free software; you can redistribute it and/or
47 + * modify it under the terms of the GNU General Public License
48 + * as published by the Free Software Foundation; either version
49 + * 2 of the License, or (at your option) any later version.
53 +#include <linux/capability.h>
54 +#include <linux/module.h>
55 +#include <linux/types.h>
56 +#include <linux/sched.h>
57 +#include <linux/kernel.h>
58 +#include <asm/uaccess.h>
59 +#include <linux/skbuff.h>
60 +#include <linux/netdevice.h>
61 +#include <linux/in.h>
62 +#include <linux/tcp.h>
63 +#include <linux/udp.h>
64 +#include <linux/if_arp.h>
65 +#include <linux/mroute.h>
66 +#include <linux/init.h>
67 +#include <linux/in6.h>
68 +#include <linux/inetdevice.h>
69 +#include <linux/etherdevice.h> /**XXX added XXX */
70 +#include <linux/igmp.h>
71 +#include <linux/netfilter_ipv4.h>
72 +#include <linux/if_ether.h>
74 +#include <net/sock.h>
76 +#include <net/icmp.h>
77 +#include <net/protocol.h>
78 +#include <net/ipip.h>
80 +#include <net/checksum.h>
81 +#include <net/dsfield.h>
82 +#include <net/inet_ecn.h>
83 +#include <net/xfrm.h>
86 +#include <net/ipv6.h>
87 +#include <net/ip6_fib.h>
88 +#include <net/ip6_route.h>
91 +#define ipv4_is_multicast(x) (((x) & htonl(0xf0000000)) == htonl(0xe0000000))
93 +//#define GRE_DEBUG 1
96 + Problems & solutions
97 + --------------------
99 + 1. The most important issue is detecting local dead loops.
100 + They would cause complete host lockup in transmit, which
101 + would be "resolved" by stack overflow or, if queueing is enabled,
102 + with infinite looping in net_bh.
104 + We cannot track such dead loops during route installation,
105 + it is infeasible task. The most general solutions would be
106 + to keep skb->encapsulation counter (sort of local ttl),
107 + and silently drop packet when it expires. It is the best
108 + solution, but it supposes maintaing new variable in ALL
109 + skb, even if no tunneling is used.
111 + Current solution: t->recursion lock breaks dead loops. It looks
112 + like dev->tbusy flag, but I preferred new variable, because
113 + the semantics is different. One day, when hard_start_xmit
114 + will be multithreaded we will have to use skb->encapsulation.
118 + 2. Networking dead loops would not kill routers, but would really
119 + kill network. IP hop limit plays role of "t->recursion" in this case,
120 + if we copy it from packet being encapsulated to upper header.
121 + It is very good solution, but it introduces two problems:
123 + - Routing protocols, using packets with ttl=1 (OSPF, RIP2),
124 + do not work over tunnels.
125 + - traceroute does not work. I planned to relay ICMP from tunnel,
126 + so that this problem would be solved and traceroute output
127 + would even more informative. This idea appeared to be wrong:
128 + only Linux complies to rfc1812 now (yes, guys, Linux is the only
129 + true router now :-)), all routers (at least, in neighbourhood of mine)
130 + return only 8 bytes of payload. It is the end.
132 + Hence, if we want that OSPF worked or traceroute said something reasonable,
133 + we should search for another solution.
135 + One of them is to parse packet trying to detect inner encapsulation
136 + made by our node. It is difficult or even impossible, especially,
137 + taking into account fragmentation. TO be short, tt is not solution at all.
139 + Current solution: The solution was UNEXPECTEDLY SIMPLE.
140 + We force DF flag on tunnels with preconfigured hop limit,
141 + that is ALL. :-) Well, it does not remove the problem completely,
142 + but exponential growth of network traffic is changed to linear
143 + (branches, that exceed pmtu are pruned) and tunnel mtu
144 + fastly degrades to value <68, where looping stops.
145 + Yes, it is not good if there exists a router in the loop,
146 + which does not force DF, even when encapsulating packets have DF set.
147 + But it is not our problem! Nobody could accuse us, we made
148 + all that we could make. Even if it is your gated who injected
149 + fatal route to network, even if it were you who configured
150 + fatal static route: you are innocent. :-)
154 + 3. Really, ipv4/ipip.c, ipv4/ip_gre.c and ipv6/sit.c contain
155 + practically identical code. It would be good to glue them
156 + together, but it is not very evident, how to make them modular.
157 + sit is integral part of IPv6, ipip and gre are naturally modular.
158 + We could extract common parts (hash table, ioctl etc)
159 + to a separate module (ip_tunnel.c).
164 +static int ipgre_tunnel_init(struct net_device *dev);
165 +static void ipgre_ip_tunnel_setup(struct net_device *dev);
166 +static void ipgre_eth_tunnel_setup(struct net_device *dev);
168 +/* Fallback tunnel: no source, no destination, no key, no options */
170 +static int ipgre_fb_tunnel_init(struct net_device *dev);
172 +static struct net_device *ipgre_fb_tunnel_dev;
174 +/* Tunnel hash table */
184 + We require exact key match i.e. if a key is present in packet
185 + it will match only tunnel with the same key; if it is not present,
186 + it will match only keyless tunnel.
188 + All keysless packets, if not matched configured keyless tunnels
189 + will match fallback tunnel.
192 +#define HASH_SIZE 1024
193 +#define HASH(addr) (ntohl(addr)&1023)
195 +static struct ip_tunnel *tunnels[4][HASH_SIZE];
197 +#define tunnels_r_l (tunnels[3])
198 +#define tunnels_r (tunnels[2])
199 +#define tunnels_l (tunnels[1])
200 +#define tunnels_wc (tunnels[0])
202 +static DEFINE_RWLOCK(ipgre_lock);
204 +/* Given src, dst and key, find appropriate for input tunnel. */
206 +static struct ip_tunnel * ipgre_tunnel_lookup(__be32 remote, __be32 local, __be32 key)
209 + unsigned hash_value = HASH(key);
210 + struct ip_tunnel *t;
212 + t = tunnels_r_l[hash_value];
214 + if (t && (t->parms.i_key == key) && (t->dev->flags&IFF_UP)) {
218 + t = tunnels_r[hash_value];
219 + if (t && (t->parms.i_key == key) && (t->dev->flags&IFF_UP))
222 + t = tunnels_l[hash_value];
223 + if (t && (t->parms.i_key == key) && (t->dev->flags&IFF_UP))
225 + t = tunnels_wc[hash_value];
226 + if (t && (t->parms.i_key == key) && (t->dev->flags&IFF_UP))
228 + if (ipgre_fb_tunnel_dev->flags&IFF_UP)
229 + return netdev_priv(ipgre_fb_tunnel_dev);
233 +static struct ip_tunnel **ipgre_bucket(struct ip_tunnel *t)
235 + __be32 remote = t->parms.iph.daddr;
236 + __be32 local = t->parms.iph.saddr;
237 + __be32 key = t->parms.i_key;
238 + unsigned h = HASH(key);
243 + if (remote && !ipv4_is_multicast(remote)) {
245 + //h ^= HASH(remote);
248 + return &tunnels[prio][h];
251 +static void ipgre_tunnel_link(struct ip_tunnel *t)
253 + struct ip_tunnel **tp = ipgre_bucket(t);
256 + write_lock_bh(&ipgre_lock);
258 + write_unlock_bh(&ipgre_lock);
261 +static void ipgre_tunnel_unlink(struct ip_tunnel *t)
263 + struct ip_tunnel **tp;
265 + for (tp = ipgre_bucket(t); *tp; tp = &(*tp)->next) {
267 + write_lock_bh(&ipgre_lock);
269 + write_unlock_bh(&ipgre_lock);
275 +static struct ip_tunnel * ipgre_tunnel_locate(struct ip_tunnel_parm *parms, int create)
277 + __be32 remote = parms->iph.daddr;
278 + __be32 local = parms->iph.saddr;
279 + __be32 key = parms->i_key;
280 + __be16 proto = parms->proto_type;
281 + struct ip_tunnel *t, **tp, *nt;
282 + struct net_device *dev;
283 + unsigned h = HASH(key);
285 + char name[IFNAMSIZ];
289 + if (remote && !ipv4_is_multicast(remote)) {
291 + //h ^= HASH(remote);
293 + for (tp = &tunnels[prio][h]; (t = *tp) != NULL; tp = &t->next) {
294 + if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr) {
295 + if (key == t->parms.i_key)
302 + printk(KERN_CRIT "Adding tunnel %s with key %d\n", parms->name, ntohl(key));
304 + if (parms->name[0])
305 + strlcpy(name, parms->name, IFNAMSIZ);
308 + for (i=1; i<100; i++) {
309 + sprintf(name, "gre%d", i);
310 + if (__dev_get_by_name(&init_net, name) == NULL)
317 + /* Tunnel creation: check payload type and call appropriate
322 + dev = alloc_netdev(sizeof(*t), name, ipgre_ip_tunnel_setup);
325 + dev = alloc_netdev(sizeof(*t), name, ipgre_eth_tunnel_setup);
334 + dev->init = ipgre_tunnel_init;
335 + nt = netdev_priv(dev);
336 + nt->parms = *parms;
338 + if (register_netdevice(dev) < 0) {
344 + ipgre_tunnel_link(nt);
351 +static void ipgre_tunnel_uninit(struct net_device *dev)
353 + ipgre_tunnel_unlink(netdev_priv(dev));
358 +static void ipgre_err(struct sk_buff *skb, u32 info)
360 +#ifndef I_WISH_WORLD_WERE_PERFECT
362 +/* It is not :-( All the routers (except for Linux) return only
363 + 8 bytes of packet payload. It means, that precise relaying of
364 + ICMP in the real Internet is absolutely infeasible.
366 + Moreover, Cisco "wise men" put GRE key to the third word
367 + in GRE header. It makes impossible maintaining even soft state for keyed
368 + GRE tunnels with enabled checksum. Tell them "thank you".
370 + Well, I wonder, rfc1812 was written by Cisco employee,
371 + what the hell these idiots break standrads established
375 + struct iphdr *iph = (struct iphdr*)skb->data;
376 + __be16 *p = (__be16*)(skb->data+(iph->ihl<<2));
377 + int grehlen = (iph->ihl<<2) + 4;
378 + int type = icmp_hdr(skb)->type;
379 + int code = icmp_hdr(skb)->code;
380 + struct ip_tunnel *t;
384 + if (flags&(GRE_CSUM|GRE_KEY|GRE_SEQ|GRE_ROUTING|GRE_VERSION)) {
385 + if (flags&(GRE_VERSION|GRE_ROUTING))
387 + if (flags&GRE_KEY) {
389 + if (flags&GRE_CSUM)
394 + /* If only 8 bytes returned, keyed message will be dropped here */
395 + if (skb_headlen(skb) < grehlen)
400 + case ICMP_PARAMETERPROB:
403 + case ICMP_DEST_UNREACH:
405 + case ICMP_SR_FAILED:
406 + case ICMP_PORT_UNREACH:
407 + /* Impossible event. */
409 + case ICMP_FRAG_NEEDED:
410 + /* Soft state for pmtu is maintained by IP core. */
413 + /* All others are translated to HOST_UNREACH.
414 + rfc2003 contains "deep thoughts" about NET_UNREACH,
415 + I believe they are just ether pollution. --ANK
420 + case ICMP_TIME_EXCEEDED:
421 + if (code != ICMP_EXC_TTL)
426 + read_lock(&ipgre_lock);
427 + t = ipgre_tunnel_lookup(iph->daddr, iph->saddr, (flags&GRE_KEY) ? *(((__be32*)p) + (grehlen>>2) - 1) : 0);
428 + if (t == NULL || t->parms.iph.daddr == 0 || ipv4_is_multicast(t->parms.iph.daddr))
431 + if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
434 + if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO)
438 + t->err_time = jiffies;
440 + read_unlock(&ipgre_lock);
443 + struct iphdr *iph = (struct iphdr*)dp;
444 + struct iphdr *eiph;
445 + __be16 *p = (__be16*)(dp+(iph->ihl<<2));
446 + int type = skb->h.icmph->type;
447 + int code = skb->h.icmph->code;
450 + __be32 rel_info = 0;
453 + int grehlen = (iph->ihl<<2) + 4;
454 + struct sk_buff *skb2;
458 + if (skb->dev->nd_net != &init_net)
461 + if (p[1] != htons(ETH_P_IP))
465 + if (flags&(GRE_CSUM|GRE_KEY|GRE_SEQ|GRE_ROUTING|GRE_VERSION)) {
466 + if (flags&(GRE_VERSION|GRE_ROUTING))
468 + if (flags&GRE_CSUM)
475 + if (len < grehlen + sizeof(struct iphdr))
477 + eiph = (struct iphdr*)(dp + grehlen);
482 + case ICMP_PARAMETERPROB:
483 + n = ntohl(skb->h.icmph->un.gateway) >> 24;
484 + if (n < (iph->ihl<<2))
487 + /* So... This guy found something strange INSIDE encapsulated
488 + packet. Well, he is fool, but what can we do ?
490 + rel_type = ICMP_PARAMETERPROB;
492 + rel_info = htonl(n << 24);
495 + case ICMP_DEST_UNREACH:
497 + case ICMP_SR_FAILED:
498 + case ICMP_PORT_UNREACH:
499 + /* Impossible event. */
501 + case ICMP_FRAG_NEEDED:
502 + /* And it is the only really necessary thing :-) */
503 + n = ntohs(skb->h.icmph->un.frag.mtu);
504 + if (n < grehlen+68)
507 + /* BSD 4.2 MORE DOES NOT EXIST IN NATURE. */
508 + if (n > ntohs(eiph->tot_len))
510 + rel_info = htonl(n);
513 + /* All others are translated to HOST_UNREACH.
514 + rfc2003 contains "deep thoughts" about NET_UNREACH,
515 + I believe, it is just ether pollution. --ANK
517 + rel_type = ICMP_DEST_UNREACH;
518 + rel_code = ICMP_HOST_UNREACH;
522 + case ICMP_TIME_EXCEEDED:
523 + if (code != ICMP_EXC_TTL)
528 + /* Prepare fake skb to feed it to icmp_send */
529 + skb2 = skb_clone(skb, GFP_ATOMIC);
532 + dst_release(skb2->dst);
534 + skb_pull(skb2, skb->data - (u8*)eiph);
535 + skb_reset_network_header(skb2);
537 + /* Try to guess incoming interface */
538 + memset(&fl, 0, sizeof(fl));
539 + //fl.fl_net = &init_net;
540 + fl.fl4_dst = eiph->saddr;
541 + fl.fl4_tos = RT_TOS(eiph->tos);
542 + fl.proto = IPPROTO_GRE;
543 + if (ip_route_output_key(dev_net(dev),&rt, &fl)) {
547 + skb2->dev = rt->u.dst.dev;
549 + /* route "incoming" packet */
550 + if (rt->rt_flags&RTCF_LOCAL) {
553 + fl.fl4_dst = eiph->daddr;
554 + fl.fl4_src = eiph->saddr;
555 + fl.fl4_tos = eiph->tos;
556 + if (ip_route_output_key(&rt, &fl) ||
557 + rt->u.dst.dev->type != ARPHRD_IPGRE) {
564 + if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos, skb2->dev) ||
565 + skb2->dst->dev->type != ARPHRD_IPGRE) {
571 + /* change mtu on this route */
572 + if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
573 + if (n > dst_mtu(skb2->dst)) {
577 + skb2->dst->ops->update_pmtu(skb2->dst, n);
578 + } else if (type == ICMP_TIME_EXCEEDED) {
579 + struct ip_tunnel *t = netdev_priv(skb2->dev);
580 + if (t->parms.iph.ttl) {
581 + rel_type = ICMP_DEST_UNREACH;
582 + rel_code = ICMP_HOST_UNREACH;
586 + icmp_send(skb2, rel_type, rel_code, rel_info);
591 +static inline void ipgre_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
593 + if (INET_ECN_is_ce(iph->tos)) {
594 + if (skb->protocol == htons(ETH_P_IP)) {
595 + IP_ECN_set_ce(ip_hdr(skb));
596 + } else if (skb->protocol == htons(ETH_P_IPV6)) {
597 + IP6_ECN_set_ce(ipv6_hdr(skb));
603 +ipgre_ecn_encapsulate(u8 tos, struct iphdr *old_iph, struct sk_buff *skb)
606 + if (skb->protocol == htons(ETH_P_IP))
607 + inner = old_iph->tos;
608 + else if (skb->protocol == htons(ETH_P_IPV6))
609 + inner = ipv6_get_dsfield((struct ipv6hdr *)old_iph);
610 + return INET_ECN_encapsulate(tos, inner);
613 +static int ipgre_rcv(struct sk_buff *skb)
621 + struct ip_tunnel *tunnel;
625 + if (skb->dev->nd_net != &init_net) {
629 + if (!pskb_may_pull(skb, 16))
634 + flags = *(__be16*)h;
637 + printk(KERN_DEBUG "gre.c [601] src:%x dst:%x proto:%d %x", iph->saddr, iph->daddr, iph->protocol, skb->data);
639 + proto = ntohs(*(__be16*)(h+2)); /* XXX added XXX */
641 + if (flags&(GRE_CSUM|GRE_KEY|GRE_ROUTING|GRE_SEQ|GRE_VERSION)) {
642 + /* - Version must be 0.
643 + - We do not support routing headers.
645 + if (flags&(GRE_VERSION|GRE_ROUTING))
648 + if (flags&GRE_CSUM) {
649 + switch (skb->ip_summed) {
650 + case CHECKSUM_COMPLETE:
651 + csum = csum_fold(skb->csum);
655 + case CHECKSUM_NONE:
657 + csum = __skb_checksum_complete(skb);
658 + skb->ip_summed = CHECKSUM_COMPLETE;
662 + if (flags&GRE_KEY) {
663 + key = *(__be32*)(h + offset);
666 + if (flags&GRE_SEQ) {
667 + seqno = ntohl(*(__be32*)(h + offset));
672 + read_lock(&ipgre_lock);
673 + if ((tunnel = ipgre_tunnel_lookup(iph->saddr, iph->daddr, key)) != NULL) {
674 + secpath_reset(skb);
676 + skb->protocol = *(__be16*)(h + 2);
677 + /* WCCP version 1 and 2 protocol decoding.
678 + * - Change protocol to IP
679 + * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
682 + skb->protocol == htons(ETH_P_WCCP)) {
683 + skb->protocol = htons(ETH_P_IP);
684 + if ((*(h + offset) & 0xF0) != 0x40)
688 + //skb->mac.raw = skb->nh.raw;
689 + skb_reset_mac_header(skb);
690 + __pskb_pull(skb, offset);
691 + skb_reset_network_header(skb);
692 + skb_postpull_rcsum(skb, skb_transport_header(skb), offset);
693 + if(proto == ETH_P_ETH)
696 + unsigned char* tmp_hdr = skb->data;
697 + printk(KERN_DEBUG "gre.c [658] %x %x %x %x %x %x\tskb %x\n", tmp_hdr[0], tmp_hdr[1], tmp_hdr[2], tmp_hdr[3], tmp_hdr[4], tmp_hdr[5], skb->data);
699 + skb->protocol = eth_type_trans(skb, tunnel->dev);
701 + /* XXX added these lines to make arp work? XXX */
702 + /*skb->mac.raw = skb->data;*/
703 + skb->network_header = skb->network_header + ETH_HLEN;
704 + /* XXX added these lines to make arp work? XXX */
707 + tmp_hdr = skb->data;
708 + printk(KERN_DEBUG "gre.c [669] %x %x %x %x %x %x\tskb %x\n", tmp_hdr[0], tmp_hdr[1], tmp_hdr[2], tmp_hdr[3], tmp_hdr[4], tmp_hdr[5], skb->data);
709 + printk(KERN_ALERT "gre.c [671] received ethernet on gre %x %x\n",skb->protocol, ((skb->nh).iph)->protocol);
711 + memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
714 + skb->pkt_type = PACKET_HOST;
715 +#ifdef CONFIG_NET_IPGRE_BROADCAST
716 + if (ipv4_is_multicast(iph->daddr)) {
717 + /* Looped back packet, drop it! */
718 + if (((struct rtable*)skb->dst)->fl.iif == 0)
720 + tunnel->dev->stats.multicast++;
721 + skb->pkt_type = PACKET_BROADCAST;
725 + if (((flags&GRE_CSUM) && csum) ||
726 + (!(flags&GRE_CSUM) && tunnel->parms.i_flags&GRE_CSUM)) {
727 + tunnel->dev->stats.rx_crc_errors++;
728 + tunnel->dev->stats.rx_errors++;
731 + if (tunnel->parms.i_flags&GRE_SEQ) {
732 + if (!(flags&GRE_SEQ) ||
733 + (tunnel->i_seqno && (s32)(seqno - tunnel->i_seqno) < 0)) {
734 + tunnel->dev->stats.rx_fifo_errors++;
735 + tunnel->dev->stats.rx_errors++;
738 + tunnel->i_seqno = seqno + 1;
740 + tunnel->dev->stats.rx_packets++;
741 + tunnel->dev->stats.rx_bytes += skb->len;
742 + skb->dev = tunnel->dev;
743 + dst_release(skb->dst);
746 + ipgre_ecn_decapsulate(iph, skb);
748 + read_unlock(&ipgre_lock);
751 + icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
754 + read_unlock(&ipgre_lock);
760 +static int ipgre_ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
762 + struct ip_tunnel *tunnel = netdev_priv(dev);
763 + struct net_device_stats *stats = &tunnel->dev->stats;
764 + struct iphdr *old_iph = ip_hdr(skb);
765 + struct iphdr *tiph;
768 + struct rtable *rt; /* Route to the other host */
769 + struct net_device *tdev; /* Device to other host */
770 + struct iphdr *iph; /* Our new IP header */
771 + int max_headroom; /* The extra header space needed */
776 + if (tunnel->recursion++) {
777 + tunnel->dev->stats.collisions++;
781 + if (dev->header_ops) {
783 + tiph = (struct iphdr*)skb->data;
785 + gre_hlen = tunnel->hlen;
786 + tiph = &tunnel->parms.iph;
789 + if ((dst = tiph->daddr) == 0) {
792 + if (skb->dst == NULL) {
793 + tunnel->dev->stats.tx_fifo_errors++;
797 + if (skb->protocol == htons(ETH_P_IP)) {
798 + rt = (struct rtable*)skb->dst;
799 + if ((dst = rt->rt_gateway) == 0)
800 + goto tx_error_icmp;
803 + else if (skb->protocol == htons(ETH_P_IPV6)) {
804 + struct in6_addr *addr6;
806 + struct neighbour *neigh = skb->dst->neighbour;
811 + addr6 = (struct in6_addr*)&neigh->primary_key;
812 + addr_type = ipv6_addr_type(addr6);
814 + if (addr_type == IPV6_ADDR_ANY) {
815 + addr6 = &ipv6_hdr(skb)->daddr;
816 + addr_type = ipv6_addr_type(addr6);
819 + if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
820 + goto tx_error_icmp;
830 + if (skb->protocol == htons(ETH_P_IP))
831 + tos = old_iph->tos;
836 + struct flowi fl = { //.fl_net = &init_net,
837 + .oif = tunnel->parms.link,
840 + .saddr = tiph->saddr,
841 + .tos = RT_TOS(tos) } },
842 + .proto = IPPROTO_GRE };
843 + if (ip_route_output_key(dev_net(dev),&rt, &fl)) {
844 + tunnel->dev->stats.tx_carrier_errors++;
848 + tdev = rt->u.dst.dev;
853 + tunnel->dev->stats.collisions++;
857 + df = tiph->frag_off;
859 + mtu = dst_mtu(&rt->u.dst) - tunnel->hlen;
861 + mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu;
864 + skb->dst->ops->update_pmtu(skb->dst, mtu);
866 + if (skb->protocol == htons(ETH_P_IP)) {
867 + df |= (old_iph->frag_off&htons(IP_DF));
869 + if ((old_iph->frag_off&htons(IP_DF)) &&
870 + mtu < ntohs(old_iph->tot_len)) {
871 + icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
877 + else if (skb->protocol == htons(ETH_P_IPV6)) {
878 + struct rt6_info *rt6 = (struct rt6_info*)skb->dst;
880 + if (rt6 && mtu < dst_mtu(skb->dst) && mtu >= IPV6_MIN_MTU) {
881 + if ((tunnel->parms.iph.daddr && !ipv4_is_multicast(tunnel->parms.iph.daddr)) ||
882 + rt6->rt6i_dst.plen == 128) {
883 + rt6->rt6i_flags |= RTF_MODIFIED;
884 + skb->dst->metrics[RTAX_MTU-1] = mtu;
888 + if (mtu >= IPV6_MIN_MTU && mtu < skb->len - tunnel->hlen + gre_hlen) {
889 + icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
896 + if (tunnel->err_count > 0) {
897 + if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
898 + tunnel->err_count--;
900 + dst_link_failure(skb);
902 + tunnel->err_count = 0;
905 + max_headroom = LL_RESERVED_SPACE(tdev) + gre_hlen;
907 + if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) {
908 + struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
911 + stats->tx_dropped++;
912 + dev_kfree_skb(skb);
913 + tunnel->recursion--;
917 + skb_set_owner_w(new_skb, skb->sk);
918 + dev_kfree_skb(skb);
920 + old_iph = ip_hdr(skb);
923 + skb->transport_header = skb->network_header;
924 + skb_push(skb, gre_hlen);
925 + memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
926 + IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
928 + dst_release(skb->dst);
929 + skb->dst = &rt->u.dst;
932 + * Push down and install the IPIP header.
937 + iph->ihl = sizeof(struct iphdr) >> 2;
938 + iph->frag_off = df;
939 + iph->protocol = IPPROTO_GRE;
940 + iph->tos = ipgre_ecn_encapsulate(tos, old_iph, skb);
941 + iph->daddr = rt->rt_dst;
942 + iph->saddr = rt->rt_src;
944 + if ((iph->ttl = tiph->ttl) == 0) {
945 + if (skb->protocol == htons(ETH_P_IP))
946 + iph->ttl = old_iph->ttl;
948 + else if (skb->protocol == htons(ETH_P_IPV6))
949 + iph->ttl = ((struct ipv6hdr*)old_iph)->hop_limit;
952 + iph->ttl = dst_metric(&rt->u.dst, RTAX_HOPLIMIT);
955 + ((__be16*)(iph+1))[0] = tunnel->parms.o_flags;
956 + ((__be16*)(iph+1))[1] = skb->protocol;
958 + if (tunnel->parms.o_flags&(GRE_KEY|GRE_CSUM|GRE_SEQ)) {
959 + __be32 *ptr = (__be32*)(((u8*)iph) + tunnel->hlen - 4);
961 + if (tunnel->parms.o_flags&GRE_SEQ) {
963 + *ptr = htonl(tunnel->o_seqno);
966 + if (tunnel->parms.o_flags&GRE_KEY) {
967 + *ptr = tunnel->parms.o_key;
970 + if (tunnel->parms.o_flags&GRE_CSUM) {
972 + *(__sum16*)ptr = ip_compute_csum((void*)(iph+1), skb->len - sizeof(struct iphdr));
979 + tunnel->recursion--;
983 + dst_link_failure(skb);
986 + stats->tx_errors++;
987 + dev_kfree_skb(skb);
988 + tunnel->recursion--;
992 +static int ipgre_eth_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
994 + struct ip_tunnel *tunnel = netdev_priv(dev);
995 + struct net_device_stats *stats = &tunnel->dev->stats;
996 + struct iphdr *old_iph = ip_hdr(skb);
997 + struct iphdr *tiph = &tunnel->parms.iph;
1000 + struct rtable *rt; /* Route to the other host */
1001 + struct net_device *tdev; /* Device to other host */
1002 + int gre_hlen = tunnel->hlen; /* XXX changed XXX*/
1003 + //struct etheriphdr *ethiph;
1004 + struct iphdr *iph; /* Our new IP header */
1005 + int max_headroom; /* The extra header space needed */
1009 + printk(KERN_ALERT "gre.c:972 Starting xmit\n");
1012 + if (tunnel->recursion++) {
1013 + stats->collisions++;
1017 + /* Need valid non-ipv4_is_multicast daddr. */
1018 + if (tiph->daddr == 0 || ipv4_is_multicast(tiph->daddr))
1023 + if (skb->protocol == htons(ETH_P_IP))
1024 + tos = old_iph->tos;
1028 + printk(KERN_ALERT "gre.c:991 Passed tos assignment.\n");
1033 + struct flowi fl = { //.fl_net = &init_net,
1034 + .oif = tunnel->parms.link,
1035 + .nl_u = { .ip4_u =
1036 + { .daddr = tiph->daddr,
1037 + .saddr = tiph->saddr,
1038 + .tos = RT_TOS(tos) } },
1039 + .proto = IPPROTO_GRE };
1040 + if (ip_route_output_key(dev_net(dev),&rt, &fl)) {
1041 + stats->tx_carrier_errors++;
1042 + goto tx_error_icmp;
1045 + tdev = rt->u.dst.dev;
1047 + printk(KERN_ALERT "gre.c:1006 Passed the route retrieval\n");
1049 + if (tdev == dev) {
1051 + stats->collisions++;
1055 + printk(KERN_ALERT "gre.c:1018 Passed tdev collision check.\n");
1058 + /* Check MTU stuff if kernel panic */
1059 + df = tiph->frag_off;
1061 + mtu = dst_mtu(&rt->u.dst) - tunnel->hlen;
1063 + mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu;
1066 + skb->dst->ops->update_pmtu(skb->dst, mtu);
1069 + printk(KERN_ALERT "gre.c:1032 Passed the pmtu setting.\n");
1072 + if (skb->protocol == htons(ETH_P_IP)) {
1073 + df |= (old_iph->frag_off&htons(IP_DF));
1075 + if ((old_iph->frag_off & htons(IP_DF)) &&
1076 + mtu < ntohs(old_iph->tot_len)) {
1077 + icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
1083 + else if (skb->protocol == htons(ETH_P_IPV6)) {
1084 + struct rt6_info *rt6 = (struct rt6_info*)skb->dst;
1086 + if (rt6 && mtu < dst_mtu(skb->dst) && mtu >= IPV6_MIN_MTU) {
1087 + if (tiph->daddr || rt6->rt6i_dst.plen == 128) {
1088 + rt6->rt6i_flags |= RTF_MODIFIED;
1089 + skb->dst->metrics[RTAX_MTU-1] = mtu;
1093 + /* @@@ Is this correct? */
1094 + if (mtu >= IPV6_MIN_MTU && mtu < skb->len - tunnel->hlen + gre_hlen) {
1095 + icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
1102 + printk(KERN_ALERT "gre.c:1065 Passed the fragmentation check.\n");
1105 + if (tunnel->err_count > 0) {
1106 + if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
1107 + tunnel->err_count--;
1108 + dst_link_failure(skb);
1110 + tunnel->err_count = 0;
1113 + max_headroom = LL_RESERVED_SPACE(tdev) + gre_hlen;
1115 + if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) {
1116 + struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
1119 + stats->tx_dropped++;
1120 + dev_kfree_skb(skb);
1121 + tunnel->recursion--;
1125 + skb_set_owner_w(new_skb, skb->sk);
1126 + dev_kfree_skb(skb);
1128 + old_iph = ip_hdr(skb);
1131 + printk(KERN_ALERT "gre.c:1094 Passed the headroom calculation\n");
1135 + skb->transport_header = skb->mac_header; // Added by valas
1136 + skb_push(skb, gre_hlen);
1137 + skb_reset_network_header(skb);
1138 + memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1139 + dst_release(skb->dst);
1140 + skb->dst = &rt->u.dst;
1143 + * Push down and install the etherip header.
1146 + iph = ip_hdr(skb);
1148 + iph->ihl = sizeof(struct iphdr) >> 2;
1149 + iph->frag_off = df;
1150 + iph->protocol = IPPROTO_GRE;
1151 + iph->tos = ipgre_ecn_encapsulate(tos, old_iph, skb);
1152 + iph->daddr = rt->rt_dst;
1153 + iph->saddr = rt->rt_src;
1155 +/* ethiph->version = htons(ETHERIP_VERSION); */
1157 + printk(KERN_ALERT "gre.c:1121 Passed outer IP header construction.\n");
1160 + if ((iph->ttl = tiph->ttl) == 0) {
1161 + if (skb->protocol == htons(ETH_P_IP))
1162 + iph->ttl = old_iph->ttl;
1164 + else if (skb->protocol == htons(ETH_P_IPV6))
1165 + iph->ttl = ((struct ipv6hdr*)old_iph)->hop_limit;
1168 + iph->ttl = dst_metric(&rt->u.dst, RTAX_HOPLIMIT);
1171 + printk(KERN_ALERT "gre.c:1006 Passed the TTL check.\n");
1174 + ((__be16*)(iph+1))[0] = tunnel->parms.o_flags;
1175 + ((__be16*)(iph+1))[1] = htons(tunnel->parms.proto_type);
1177 + if (tunnel->parms.o_flags&(GRE_KEY|GRE_CSUM|GRE_SEQ)) {
1178 + __be32 *ptr = (__be32*)(((u8*)iph) + tunnel->hlen - 4);
1180 + if (tunnel->parms.o_flags&GRE_SEQ) {
1181 + ++tunnel->o_seqno;
1182 + *ptr = htonl(tunnel->o_seqno);
1185 + if (tunnel->parms.o_flags&GRE_KEY) {
1186 + *ptr = tunnel->parms.o_key;
1189 + if (tunnel->parms.o_flags&GRE_CSUM) {
1191 + *(__sum16*)ptr = ip_compute_csum((void*)(iph+1), skb->len - sizeof(struct iphdr));
1195 + printk(KERN_ALERT "gre.c:1006 Passed the tunnel transmit.\n");
1201 + tunnel->recursion--;
1205 + dst_link_failure(skb);
1208 + stats->tx_errors++;
1209 + dev_kfree_skb(skb);
1210 + tunnel->recursion--;
1216 +ipgre_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
1219 + struct ip_tunnel_parm p;
1220 + struct ip_tunnel *t;
1222 + printk(KERN_ALERT "1174 GRE: entering gre ioctl. command is: %d\n", cmd);
1225 + case SIOCGETTUNNEL:
1227 + if (dev == ipgre_fb_tunnel_dev) {
1228 + if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
1232 + t = ipgre_tunnel_locate(&p, 0);
1235 + t = netdev_priv(dev);
1236 + memcpy(&p, &t->parms, sizeof(p));
1237 + if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1241 + case SIOCADDTUNNEL:
1242 + case SIOCCHGTUNNEL:
1244 + if (!capable(CAP_NET_ADMIN))
1248 + if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1252 + if (p.iph.version != 4 || p.iph.protocol != IPPROTO_GRE ||
1253 + p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)) ||
1254 + ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING)))
1257 + p.iph.frag_off |= htons(IP_DF);
1259 + if (!(p.i_flags&GRE_KEY))
1261 + if (!(p.o_flags&GRE_KEY))
1264 + t = ipgre_tunnel_locate(&p, cmd == SIOCADDTUNNEL);
1265 + if (t) printk(KERN_ALERT "1174 GRE: proto %s %d\n", p.name, p.proto_type);
1266 + if (dev != ipgre_fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
1268 + if (t->dev != dev) {
1273 + unsigned nflags=0;
1275 + t = netdev_priv(dev);
1277 + if (ipv4_is_multicast(p.iph.daddr))
1278 + nflags = IFF_BROADCAST;
1279 + else if (p.iph.daddr)
1280 + nflags = IFF_POINTOPOINT;
1282 + /* XXX:Set back IFF_BROADCAST if
1283 + * transporting ethernet */
1284 + printk(KERN_ALERT "1193 GRE: proto %s %d\n", p.name, p.proto_type);
1285 + if (p.proto_type == ETH_P_ETH)
1286 + nflags = IFF_BROADCAST;
1288 + if ((dev->flags^nflags)&(IFF_POINTOPOINT|IFF_BROADCAST)) {
1292 + ipgre_tunnel_unlink(t);
1293 + t->parms.iph.saddr = p.iph.saddr;
1294 + t->parms.iph.daddr = p.iph.daddr;
1295 + t->parms.i_key = p.i_key;
1296 + t->parms.o_key = p.o_key;
1297 + /* XXX:Copy in the protocol field */
1298 + t->parms.proto_type = p.proto_type;
1299 + if (t->parms.proto_type != ETH_P_ETH)
1301 + memcpy(dev->dev_addr, &p.iph.saddr, 4);
1302 + memcpy(dev->broadcast, &p.iph.daddr, 4);
1304 + ipgre_tunnel_link(t);
1305 + netdev_state_change(dev);
1311 + if (cmd == SIOCCHGTUNNEL) {
1312 + t->parms.iph.ttl = p.iph.ttl;
1313 + t->parms.iph.tos = p.iph.tos;
1314 + t->parms.iph.frag_off = p.iph.frag_off;
1316 + if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
1319 + err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
1322 + case SIOCDELTUNNEL:
1324 + if (!capable(CAP_NET_ADMIN))
1327 + if (dev == ipgre_fb_tunnel_dev) {
1329 + if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1332 + if ((t = ipgre_tunnel_locate(&p, 0)) == NULL)
1335 + if (t == netdev_priv(ipgre_fb_tunnel_dev))
1339 + unregister_netdevice(dev); // added by Valas
1350 +static struct net_device_stats *ipgre_tunnel_get_stats(struct net_device *dev)
1352 + return &(((struct ip_tunnel*)netdev_priv(dev))->dev->stats);
1355 +static int ipgre_tunnel_change_mtu(struct net_device *dev, int new_mtu)
1357 + struct ip_tunnel *tunnel = netdev_priv(dev);
1358 + if (new_mtu < 68 || new_mtu > 0xFFF8 - tunnel->hlen)
1360 + dev->mtu = new_mtu;
1364 +#ifdef CONFIG_NET_IPGRE_BROADCAST
1365 +/* Nice toy. Unfortunately, useless in real life :-)
1366 + It allows to construct virtual multiprotocol broadcast "LAN"
1367 + over the Internet, provided ipv4_is_multicast routing is tuned.
1370 + I have no idea was this bicycle invented before me,
1371 + so that I had to set ARPHRD_IPGRE to a random value.
1372 + I have an impression, that Cisco could make something similar,
1373 + but this feature is apparently missing in IOS<=11.2(8).
1375 + I set up 10.66.66/24 and fec0:6666:6666::0/96 as virtual networks
1376 + with broadcast 224.66.66.66. If you have access to mbone, play with me :-)
1378 + ping -t 255 224.66.66.66
1380 + If nobody answers, mbone does not work.
1382 + ip tunnel add Universe mode gre remote 224.66.66.66 local <Your_real_addr> ttl 255
1383 + ip addr add 10.66.66.<somewhat>/24 dev Universe
1384 + ifconfig Universe up
1385 + ifconfig Universe add fe80::<Your_real_addr>/10
1386 + ifconfig Universe add fec0:6666:6666::<Your_real_addr>/96
1389 + ftp fec0:6666:6666::193.233.7.65
1394 +static int ipgre_open(struct net_device *dev)
1396 + struct ip_tunnel *t = netdev_priv(dev);
1398 + if (ipv4_is_multicast(t->parms.iph.daddr)) {
1399 + struct flowi fl = { //.fl_net = &init_net,
1400 + .oif = t->parms.link,
1401 + .nl_u = { .ip4_u =
1402 + { .daddr = t->parms.iph.daddr,
1403 + .saddr = t->parms.iph.saddr,
1404 + .tos = RT_TOS(t->parms.iph.tos) } },
1405 + .proto = IPPROTO_GRE };
1406 + struct rtable *rt;
1407 + if (ip_route_output_key(dev_net(dev),&rt, &fl))
1408 + return -EADDRNOTAVAIL;
1409 + dev = rt->u.dst.dev;
1411 + if (__in_dev_get_rtnl(dev) == NULL)
1412 + return -EADDRNOTAVAIL;
1413 + t->mlink = dev->ifindex;
1414 + ip_mc_inc_group(__in_dev_get_rtnl(dev), t->parms.iph.daddr);
1419 +static int ipgre_close(struct net_device *dev)
1421 + struct ip_tunnel *t = netdev_priv(dev);
1422 + if (ipv4_is_multicast(t->parms.iph.daddr) && t->mlink) {
1423 + struct in_device *in_dev = inetdev_by_index(&init_net, t->mlink);
1425 + ip_mc_dec_group(in_dev, t->parms.iph.daddr);
1426 + in_dev_put(in_dev);
1434 +static void ipgre_ip_tunnel_setup(struct net_device *dev)
1436 + //SET_MODULE_OWNER(dev);
1437 + dev->uninit = ipgre_tunnel_uninit;
1438 + dev->destructor = free_netdev;
1439 + dev->hard_start_xmit = ipgre_ip_tunnel_xmit;
1440 + dev->get_stats = ipgre_tunnel_get_stats;
1441 + dev->do_ioctl = ipgre_tunnel_ioctl;
1442 + dev->change_mtu = ipgre_tunnel_change_mtu;
1444 + dev->type = ARPHRD_IPGRE;
1445 + dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr) + 4;
1446 + dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 4;
1447 + dev->flags = IFF_NOARP;
1449 + dev->addr_len = 4;
1452 +/* Tunnel setup for ipgre_eth */
1453 +static void ipgre_eth_tunnel_setup(struct net_device *dev)
1455 + //SET_MODULE_OWNER(dev);
1458 + dev->uninit = ipgre_tunnel_uninit;
1459 + dev->destructor = free_netdev;
1460 + dev->hard_start_xmit = ipgre_eth_tunnel_xmit;
1461 + dev->get_stats = ipgre_tunnel_get_stats;
1462 + dev->do_ioctl = ipgre_tunnel_ioctl;
1463 + dev->change_mtu = ipgre_tunnel_change_mtu;
1465 + dev->hard_header_len = ETH_HLEN + sizeof(struct iphdr) + 4;
1466 + dev->tx_queue_len = 0;
1467 + random_ether_addr(dev->dev_addr);
1470 + unsigned char* d = dev->dev_addr;
1471 + printk(KERN_ALERT "Here is the address we got:%x%x%x%x%x%x\n",d[0],d[1],d[2],d[3],d[4],d[5]);
1477 +static int ipgre_header(struct sk_buff *skb, struct net_device *dev,
1478 + unsigned short type,
1479 + const void *daddr, const void *saddr, unsigned len)
1481 + struct ip_tunnel *t = netdev_priv(dev);
1482 + struct iphdr *iph = (struct iphdr *)skb_push(skb, t->hlen);
1483 + __be16 *p = (__be16*)(iph+1);
1485 + memcpy(iph, &t->parms.iph, sizeof(struct iphdr));
1486 + p[0] = t->parms.o_flags;
1487 + p[1] = htons(type);
1490 + * Set the source hardware address.
1494 + memcpy(&iph->saddr, saddr, 4);
1497 + memcpy(&iph->daddr, daddr, 4);
1500 + if (iph->daddr && !ipv4_is_multicast(iph->daddr))
1506 +static int ipgre_header_parse(const struct sk_buff *skb, unsigned char *haddr)
1508 + struct iphdr *iph = (struct iphdr*) skb_mac_header(skb);
1509 + memcpy(haddr, &iph->saddr, 4);
1513 +static const struct header_ops ipgre_header_ops = {
1514 + .create = ipgre_header,
1515 + .parse = ipgre_header_parse,
1518 +static int ipgre_tunnel_init(struct net_device *dev)
1520 + struct net_device *tdev = NULL;
1521 + struct ip_tunnel *tunnel;
1522 + struct iphdr *iph;
1523 + int hlen = LL_MAX_HEADER;
1524 + int mtu = ETH_DATA_LEN;
1525 + int addend = sizeof(struct iphdr) + 4;
1527 + tunnel = netdev_priv(dev);
1528 + iph = &tunnel->parms.iph;
1530 + tunnel->dev = dev;
1531 + strcpy(tunnel->parms.name, dev->name);
1533 + if (tunnel->parms.proto_type != ETH_P_ETH)
1535 + memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
1536 + memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
1539 + /* Guess output device to choose reasonable mtu and hard_header_len */
1542 + struct flowi fl = { //.fl_net = &init_net,
1543 + .oif = tunnel->parms.link,
1544 + .nl_u = { .ip4_u =
1545 + { .daddr = iph->daddr,
1546 + .saddr = iph->saddr,
1547 + .tos = RT_TOS(iph->tos) } },
1548 + .proto = IPPROTO_GRE };
1549 + struct rtable *rt;
1550 + if (!ip_route_output_key(dev_net(dev), &rt, &fl)) {
1551 + tdev = rt->u.dst.dev;
1555 + if (tunnel->parms.proto_type == ETH_P_ETH)
1557 + dev->flags |= IFF_BROADCAST;
1561 + dev->flags |= IFF_POINTOPOINT;
1564 +#ifdef CONFIG_NET_IPGRE_BROADCAST
1565 + if (ipv4_is_multicast(iph->daddr)) {
1568 + dev->flags = IFF_BROADCAST;
1569 + dev->header_ops = &ipgre_header_ops;
1570 + dev->open = ipgre_open;
1571 + dev->stop = ipgre_close;
1576 + if (!tdev && tunnel->parms.link)
1577 + tdev = __dev_get_by_index(&init_net, tunnel->parms.link);
1580 + hlen = tdev->hard_header_len;
1583 + dev->iflink = tunnel->parms.link;
1585 + /* Precalculate GRE options length */
1586 + if (tunnel->parms.o_flags&(GRE_CSUM|GRE_KEY|GRE_SEQ)) {
1587 + if (tunnel->parms.o_flags&GRE_CSUM)
1589 + if (tunnel->parms.o_flags&GRE_KEY)
1591 + if (tunnel->parms.o_flags&GRE_SEQ)
1594 + dev->hard_header_len = hlen + addend;
1595 + dev->mtu = mtu - addend;
1596 + tunnel->hlen = addend;
1600 +static int __init ipgre_fb_tunnel_init(struct net_device *dev)
1602 + struct ip_tunnel *tunnel = netdev_priv(dev);
1603 + struct iphdr *iph = &tunnel->parms.iph;
1605 + tunnel->dev = dev;
1606 + strcpy(tunnel->parms.name, dev->name);
1609 + iph->protocol = IPPROTO_GRE;
1611 + tunnel->hlen = sizeof(struct iphdr) + 4;
1614 + tunnels_wc[0] = tunnel;
1619 +static struct net_protocol ipgre_protocol = {
1620 + .handler = ipgre_rcv,
1621 + .err_handler = ipgre_err,
1626 + * And now the modules code and kernel interface.
1629 +static int __init ipgre_init(void)
1633 + printk(KERN_INFO "GRE over IPv4 tunneling driver\n");
1635 + if (inet_add_protocol(&ipgre_protocol, IPPROTO_GRE) < 0) {
1636 + printk(KERN_INFO "ipgre init: can't add protocol\n");
1640 + ipgre_fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "gre0",
1641 + ipgre_ip_tunnel_setup);
1642 + if (!ipgre_fb_tunnel_dev) {
1647 + ipgre_fb_tunnel_dev->init = ipgre_fb_tunnel_init;
1649 + if ((err = register_netdev(ipgre_fb_tunnel_dev)))
1654 + free_netdev(ipgre_fb_tunnel_dev);
1656 + inet_del_protocol(&ipgre_protocol, IPPROTO_GRE);
1660 +static void __exit ipgre_destroy_tunnels(void)
1664 + for (prio = 0; prio < 4; prio++) {
1666 + for (h = 0; h < HASH_SIZE; h++) {
1667 + struct ip_tunnel *t;
1668 + while ((t = tunnels[prio][h]) != NULL)
1669 + unregister_netdevice(t->dev);
1674 +static void __exit ipgre_fini(void)
1676 + if (inet_del_protocol(&ipgre_protocol, IPPROTO_GRE) < 0)
1677 + printk(KERN_INFO "ipgre close: can't remove protocol\n");
1680 + ipgre_destroy_tunnels();
1684 +module_init(ipgre_init);
1685 +module_exit(ipgre_fini);
1686 +MODULE_LICENSE("GPL");
1687 diff -Nurb linux-2.6.27-660/include/linux/if_ether.h linux-2.6.27-700/include/linux/if_ether.h
1688 --- linux-2.6.27-660/include/linux/if_ether.h 2008-10-09 18:13:53.000000000 -0400
1689 +++ linux-2.6.27-700/include/linux/if_ether.h 2009-04-16 10:27:39.000000000 -0400
1691 #define ETH_P_DIAG 0x6005 /* DEC Diagnostics */
1692 #define ETH_P_CUST 0x6006 /* DEC Customer use */
1693 #define ETH_P_SCA 0x6007 /* DEC Systems Comms Arch */
1694 +#define ETH_P_ETH 0x6558 /* Ethernet in Ethernet */
1695 #define ETH_P_RARP 0x8035 /* Reverse Addr Res packet */
1696 #define ETH_P_ATALK 0x809B /* Appletalk DDP */
1697 #define ETH_P_AARP 0x80F3 /* Appletalk AARP */
1698 diff -Nurb linux-2.6.27-660/include/linux/if_tunnel.h linux-2.6.27-700/include/linux/if_tunnel.h
1699 --- linux-2.6.27-660/include/linux/if_tunnel.h 2008-10-09 18:13:53.000000000 -0400
1700 +++ linux-2.6.27-700/include/linux/if_tunnel.h 2009-04-16 10:27:39.000000000 -0400
1705 + __be16 proto_type; /*Added*/
1709 diff -Nurb linux-2.6.27-660/net/ipv4/ip_gre.c linux-2.6.27-700/net/ipv4/ip_gre.c
1710 --- linux-2.6.27-660/net/ipv4/ip_gre.c 2008-10-09 18:13:53.000000000 -0400
1711 +++ linux-2.6.27-700/net/ipv4/ip_gre.c 2009-04-16 12:48:33.000000000 -0400
1713 #include <linux/init.h>
1714 #include <linux/in6.h>
1715 #include <linux/inetdevice.h>
1716 +#include <linux/etherdevice.h> /**XXX added XXX */
1717 #include <linux/igmp.h>
1718 #include <linux/netfilter_ipv4.h>
1719 #include <linux/if_ether.h>
1721 #include <net/ip6_route.h>
1724 +#define MULTICAST(x) (((x) & htonl(0xf0000000)) == htonl(0xe0000000))
1726 +//#define GRE_DEBUG 1
1729 Problems & solutions
1730 --------------------
1734 static int ipgre_tunnel_init(struct net_device *dev);
1735 -static void ipgre_tunnel_setup(struct net_device *dev);
1736 +static void ipgre_ip_tunnel_setup(struct net_device *dev);
1737 +static void ipgre_eth_tunnel_setup(struct net_device *dev);
1739 /* Fallback tunnel: no source, no destination, no key, no options */
1742 __be32 remote = parms->iph.daddr;
1743 __be32 local = parms->iph.saddr;
1744 __be32 key = parms->i_key;
1745 + __be16 proto = parms->proto_type;
1746 struct ip_tunnel *t, **tp, *nt;
1747 struct net_device *dev;
1748 char name[IFNAMSIZ];
1749 @@ -269,12 +276,28 @@
1753 + printk(KERN_CRIT "Adding tunnel %s with key %d\n", parms->name, ntohl(key));
1756 strlcpy(name, parms->name, IFNAMSIZ);
1758 sprintf(name, "gre%%d");
1760 - dev = alloc_netdev(sizeof(*t), name, ipgre_tunnel_setup);
1762 + /* Tunnel creation: check payload type and call appropriate
1767 + dev = alloc_netdev(sizeof(*t), name, ipgre_ip_tunnel_setup);
1770 + dev = alloc_netdev(sizeof(*t), name, ipgre_eth_tunnel_setup);
1781 struct ip_tunnel *tunnel;
1785 if (!pskb_may_pull(skb, 16))
1787 @@ -439,6 +463,11 @@
1789 flags = *(__be16*)h;
1792 + printk(KERN_DEBUG "gre.c [601] src:%x dst:%x proto:%d %p", iph->saddr, iph->daddr, iph->protocol, skb->data);
1794 + proto = ntohs(*(__be16*)(h+2)); /* XXX added XXX */
1796 if (flags&(GRE_CSUM|GRE_KEY|GRE_ROUTING|GRE_SEQ|GRE_VERSION)) {
1797 /* - Version must be 0.
1798 - We do not support routing headers.
1799 @@ -493,7 +522,29 @@
1800 __pskb_pull(skb, offset);
1801 skb_reset_network_header(skb);
1802 skb_postpull_rcsum(skb, skb_transport_header(skb), offset);
1803 + if(proto == ETH_P_ETH)
1806 + unsigned char* tmp_hdr = skb->data;
1807 + printk(KERN_DEBUG "gre.c [658] %x %x %x %x %x %x\tskb %p\n", tmp_hdr[0], tmp_hdr[1], tmp_hdr[2], tmp_hdr[3], tmp_hdr[4], tmp_hdr[5], skb->data);
1809 + skb->protocol = eth_type_trans(skb, tunnel->dev);
1811 + /* XXX added these lines to make arp work? XXX */
1812 + /*skb->mac.raw = skb->data;*/
1813 + skb->network_header = skb->network_header + ETH_HLEN;
1814 + /* XXX added these lines to make arp work? XXX */
1817 + tmp_hdr = skb->data;
1818 + printk(KERN_DEBUG "gre.c [669] %x %x %x %x %x %x\tskb %p\n", tmp_hdr[0], tmp_hdr[1], tmp_hdr[2], tmp_hdr[3], tmp_hdr[4], tmp_hdr[5], skb->data);
1819 + printk(KERN_ALERT "gre.c [671] received ethernet on gre %x\n",skb->protocol);
1821 + memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
1824 skb->pkt_type = PACKET_HOST;
1826 #ifdef CONFIG_NET_IPGRE_BROADCAST
1827 if (ipv4_is_multicast(iph->daddr)) {
1828 /* Looped back packet, drop it! */
1833 -static int ipgre_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
1834 +static int ipgre_ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
1836 struct ip_tunnel *tunnel = netdev_priv(dev);
1837 struct net_device_stats *stats = &tunnel->dev->stats;
1838 @@ -799,9 +850,17 @@
1839 tdev = rt->u.dst.dev;
1842 + if (tunnel->parms.proto_type == ETH_P_ETH)
1844 + dev->flags |= IFF_BROADCAST;
1848 dev->flags |= IFF_POINTOPOINT;
1853 if (!tdev && tunnel->parms.link)
1854 tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
1856 @@ -822,10 +881,234 @@
1858 dev->hard_header_len = hlen + addend;
1859 dev->mtu = mtu - addend;
1860 + if (tunnel->parms.proto_type == ETH_P_ETH)
1861 + dev->mtu -= ETH_HLEN;
1862 tunnel->hlen = addend;
1866 +static int ipgre_eth_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
1868 + struct ip_tunnel *tunnel = netdev_priv(dev);
1869 + struct net_device_stats *stats = &tunnel->dev->stats;
1870 + struct iphdr *old_iph = ip_hdr(skb);
1871 + struct iphdr *tiph = &tunnel->parms.iph;
1874 + struct rtable *rt; /* Route to the other host */
1875 + struct net_device *tdev; /* Device to other host */
1876 + int gre_hlen = tunnel->hlen; /* XXX changed XXX*/
1877 + //struct etheriphdr *ethiph;
1878 + struct iphdr *iph; /* Our new IP header */
1879 + int max_headroom; /* The extra header space needed */
1883 + printk(KERN_ALERT "gre.c:972 Starting xmit\n");
1886 + if (tunnel->recursion++) {
1887 + stats->collisions++;
1891 + /* Need valid non-multicast daddr. */
1892 + if (tiph->daddr == 0 || MULTICAST(tiph->daddr))
1897 + if (skb->protocol == htons(ETH_P_IP))
1898 + tos = old_iph->tos;
1902 + printk(KERN_ALERT "gre.c:991 Passed tos assignment.\n");
1907 + struct flowi fl = { //.fl_net = &init_net,
1908 + .oif = tunnel->parms.link,
1909 + .nl_u = { .ip4_u =
1910 + { .daddr = tiph->daddr,
1911 + .saddr = tiph->saddr,
1912 + .tos = RT_TOS(tos) } },
1913 + .proto = IPPROTO_GRE };
1914 + if (ip_route_output_key(dev_net(dev),&rt, &fl)) {
1915 + stats->tx_carrier_errors++;
1916 + goto tx_error_icmp;
1919 + tdev = rt->u.dst.dev;
1921 + printk(KERN_ALERT "gre.c:1006 Passed the route retrieval\n");
1923 + if (tdev == dev) {
1925 + stats->collisions++;
1929 + printk(KERN_ALERT "gre.c:1018 Passed tdev collision check.\n");
1932 + /* Check MTU stuff if kernel panic */
1933 + df = tiph->frag_off;
1935 + mtu = dst_mtu(&rt->u.dst) - tunnel->hlen;
1937 + mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu;
1940 + skb->dst->ops->update_pmtu(skb->dst, mtu);
1943 + printk(KERN_ALERT "gre.c:1032 Passed the pmtu setting.\n");
1946 + if (skb->protocol == htons(ETH_P_IP)) {
1947 + df |= (old_iph->frag_off&htons(IP_DF));
1949 + if ((old_iph->frag_off & htons(IP_DF)) &&
1950 + mtu < ntohs(old_iph->tot_len)) {
1951 + icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
1957 + else if (skb->protocol == htons(ETH_P_IPV6)) {
1958 + struct rt6_info *rt6 = (struct rt6_info*)skb->dst;
1960 + if (rt6 && mtu < dst_mtu(skb->dst) && mtu >= IPV6_MIN_MTU) {
1961 + if (tiph->daddr || rt6->rt6i_dst.plen == 128) {
1962 + rt6->rt6i_flags |= RTF_MODIFIED;
1963 + skb->dst->metrics[RTAX_MTU-1] = mtu;
1967 + /* @@@ Is this correct? */
1968 + if (mtu >= IPV6_MIN_MTU && mtu < skb->len - tunnel->hlen + gre_hlen) {
1969 + icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
1976 + printk(KERN_ALERT "gre.c:1065 Passed the fragmentation check.\n");
1979 + if (tunnel->err_count > 0) {
1980 + if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
1981 + tunnel->err_count--;
1982 + dst_link_failure(skb);
1984 + tunnel->err_count = 0;
1987 + max_headroom = LL_RESERVED_SPACE(tdev) + gre_hlen;
1989 + if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) {
1990 + struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
1993 + stats->tx_dropped++;
1994 + dev_kfree_skb(skb);
1995 + tunnel->recursion--;
1999 + skb_set_owner_w(new_skb, skb->sk);
2000 + dev_kfree_skb(skb);
2002 + old_iph = ip_hdr(skb);
2005 + printk(KERN_ALERT "gre.c:1094 Passed the headroom calculation\n");
2008 + skb->transport_header = skb->data;
2009 + skb_push(skb, gre_hlen);
2010 + skb_reset_network_header(skb);
2011 + memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
2012 + dst_release(skb->dst);
2013 + skb->dst = &rt->u.dst;
2016 + * Push down and install the etherip header.
2019 + iph = ip_hdr(skb);
2021 + iph->ihl = sizeof(struct iphdr) >> 2;
2022 + iph->frag_off = df;
2023 + iph->protocol = IPPROTO_GRE;
2024 + iph->tos = ipgre_ecn_encapsulate(tos, old_iph, skb);
2025 + iph->daddr = rt->rt_dst;
2026 + iph->saddr = rt->rt_src;
2028 +/* ethiph->version = htons(ETHERIP_VERSION); */
2030 + printk(KERN_ALERT "gre.c:1121 Passed outer IP header construction.\n");
2033 + if ((iph->ttl = tiph->ttl) == 0) {
2034 + if (skb->protocol == htons(ETH_P_IP))
2035 + iph->ttl = old_iph->ttl;
2037 + else if (skb->protocol == htons(ETH_P_IPV6))
2038 + iph->ttl = ((struct ipv6hdr*)old_iph)->hop_limit;
2041 + iph->ttl = dst_metric(&rt->u.dst, RTAX_HOPLIMIT);
2044 + printk(KERN_ALERT "gre.c:1006 Passed the TTL check.\n");
2047 + ((__be16*)(iph+1))[0] = tunnel->parms.o_flags;
2048 + ((__be16*)(iph+1))[1] = htons(tunnel->parms.proto_type);
2050 + if (tunnel->parms.o_flags&(GRE_KEY|GRE_CSUM|GRE_SEQ)) {
2051 + __be32 *ptr = (__be32*)(((u8*)iph) + tunnel->hlen - 4);
2053 + if (tunnel->parms.o_flags&GRE_SEQ) {
2054 + ++tunnel->o_seqno;
2055 + *ptr = htonl(tunnel->o_seqno);
2058 + if (tunnel->parms.o_flags&GRE_KEY) {
2059 + *ptr = tunnel->parms.o_key;
2062 + if (tunnel->parms.o_flags&GRE_CSUM) {
2064 + *(__sum16*)ptr = ip_compute_csum((void*)(iph+1), skb->len - sizeof(struct iphdr));
2068 + printk(KERN_ALERT "gre.c:1006 Passed the tunnel transmit.\n");
2074 + tunnel->recursion--;
2078 + dst_link_failure(skb);
2081 + stats->tx_errors++;
2082 + dev_kfree_skb(skb);
2083 + tunnel->recursion--;
2089 ipgre_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
2091 @@ -876,6 +1159,7 @@
2094 t = ipgre_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
2095 + if (t) printk(KERN_ALERT "1174 GRE: proto %s %x\n", p.name, p.proto_type);
2097 if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
2099 @@ -893,6 +1177,12 @@
2100 else if (p.iph.daddr)
2101 nflags = IFF_POINTOPOINT;
2103 + /* XXX:Set back IFF_BROADCAST if
2104 + * transporting ethernet */
2105 + printk(KERN_ALERT "1193 GRE: proto %s %d\n", p.name, p.proto_type);
2106 + if (p.proto_type == ETH_P_ETH)
2107 + nflags = IFF_BROADCAST;
2109 if ((dev->flags^nflags)&(IFF_POINTOPOINT|IFF_BROADCAST)) {
2112 @@ -902,8 +1192,13 @@
2113 t->parms.iph.daddr = p.iph.daddr;
2114 t->parms.i_key = p.i_key;
2115 t->parms.o_key = p.o_key;
2116 + /* XXX:Copy in the protocol field */
2117 + t->parms.proto_type = p.proto_type;
2118 + if (t->parms.proto_type != ETH_P_ETH) {
2119 memcpy(dev->dev_addr, &p.iph.saddr, 4);
2120 memcpy(dev->broadcast, &p.iph.daddr, 4);
2123 ipgre_tunnel_link(ign, t);
2124 netdev_state_change(dev);
2126 @@ -1076,13 +1371,13 @@
2130 -static void ipgre_tunnel_setup(struct net_device *dev)
2131 +static void ipgre_ip_tunnel_setup(struct net_device *dev)
2133 dev->uninit = ipgre_tunnel_uninit;
2134 dev->destructor = free_netdev;
2135 - dev->hard_start_xmit = ipgre_tunnel_xmit;
2136 dev->do_ioctl = ipgre_tunnel_ioctl;
2137 dev->change_mtu = ipgre_tunnel_change_mtu;
2138 + dev->hard_start_xmit = ipgre_ip_tunnel_xmit;
2140 dev->type = ARPHRD_IPGRE;
2141 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr) + 4;
2142 @@ -1093,6 +1388,36 @@
2143 dev->features |= NETIF_F_NETNS_LOCAL;
2146 +/* Tunnel setup for ipgre_eth */
2147 +static void ipgre_eth_tunnel_setup(struct net_device *dev)
2149 + //SET_MODULE_OWNER(dev);
2151 + // Set default values for Ethernet device
2154 + dev->uninit = ipgre_tunnel_uninit;
2155 + dev->destructor = free_netdev;
2156 + dev->hard_start_xmit = ipgre_eth_tunnel_xmit;
2157 + //dev->get_stats = ipgre_tunnel_get_stats;
2158 + dev->do_ioctl = ipgre_tunnel_ioctl;
2159 + dev->change_mtu = ipgre_tunnel_change_mtu;
2161 + dev->hard_header_len = LL_MAX_HEADER + ETH_HLEN + sizeof(struct iphdr) + 4;
2162 + dev->mtu = ETH_DATA_LEN - ETH_HLEN - sizeof(struct iphdr) - 4;
2163 + dev->tx_queue_len = 0;
2165 + dev->features |= NETIF_F_NETNS_LOCAL;
2167 + random_ether_addr(dev->dev_addr);
2170 + { unsigned char* d = dev->dev_addr;
2171 + printk(KERN_ALERT "Here is the address we got:%x%x%x%x%x%x\n",d[0],d[1],d[2],d[3],d[4],d[5]); }
2176 static int ipgre_tunnel_init(struct net_device *dev)
2178 struct ip_tunnel *tunnel;
2179 @@ -1104,8 +1429,10 @@
2181 strcpy(tunnel->parms.name, dev->name);
2183 + if (tunnel->parms.proto_type != ETH_P_ETH) {
2184 memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
2185 memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
2188 ipgre_tunnel_bind_dev(dev);
2190 @@ -1181,7 +1508,7 @@
2193 ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "gre0",
2194 - ipgre_tunnel_setup);
2195 + ipgre_ip_tunnel_setup);
2196 if (!ign->fb_tunnel_dev) {