Debugged version of the EGRE patch.
[linux-2.6.git] / linux-2.6-700-egre.patch
1 diff -Nurb linux-2.6.27-660/drivers/net/Kconfig linux-2.6.27-700/drivers/net/Kconfig
2 --- linux-2.6.27-660/drivers/net/Kconfig        2009-04-10 17:34:59.000000000 -0400
3 +++ linux-2.6.27-700/drivers/net/Kconfig        2009-04-10 17:56:31.000000000 -0400
4 @@ -39,6 +39,12 @@
5           'ifb1' etc.
6           Look at the iproute2 documentation directory for usage etc
7  
8 +config EGRE
9 +       tristate "EGRE module for Ethernet over GRE Tunnels"
10 +       ---help---
11 +         This is an improvement over the GRE tunnel driver that facilitates
12 +      the transport of Ethernet frames over GRE tunnels.
13 +      
14  config DUMMY
15         tristate "Dummy net driver support"
16         ---help---
17 diff -Nurb linux-2.6.27-660/drivers/net/Makefile linux-2.6.27-700/drivers/net/Makefile
18 --- linux-2.6.27-660/drivers/net/Makefile       2008-10-09 18:13:53.000000000 -0400
19 +++ linux-2.6.27-700/drivers/net/Makefile       2009-04-10 17:53:47.000000000 -0400
20 @@ -2,6 +2,7 @@
21  # Makefile for the Linux network (ethercard) device drivers.
22  #
23  
24 +obj-$(CONFIG_EGRE) += gre.o
25  obj-$(CONFIG_E1000) += e1000/
26  obj-$(CONFIG_E1000E) += e1000e/
27  obj-$(CONFIG_IBM_NEW_EMAC) += ibm_newemac/
28 diff -Nurb linux-2.6.27-660/drivers/net/gre.c linux-2.6.27-700/drivers/net/gre.c
29 --- linux-2.6.27-660/drivers/net/gre.c  1969-12-31 19:00:00.000000000 -0500
30 +++ linux-2.6.27-700/drivers/net/gre.c  2009-04-10 17:35:46.000000000 -0400
31 @@ -0,0 +1,1632 @@
32 +/*
33 + *     Linux NET3:     GRE over IP protocol decoder.
34 + *
35 + *     Authors: Alexey Kuznetsov (kuznet@ms2.inr.ac.ru)
36 + *
37 + *     This program is free software; you can redistribute it and/or
38 + *     modify it under the terms of the GNU General Public License
39 + *     as published by the Free Software Foundation; either version
40 + *     2 of the License, or (at your option) any later version.
41 + *
42 + */
43 +
44 +#include <linux/capability.h>
45 +#include <linux/module.h>
46 +#include <linux/types.h>
47 +#include <linux/sched.h>
48 +#include <linux/kernel.h>
49 +#include <asm/uaccess.h>
50 +#include <linux/skbuff.h>
51 +#include <linux/netdevice.h>
52 +#include <linux/in.h>
53 +#include <linux/tcp.h>
54 +#include <linux/udp.h>
55 +#include <linux/if_arp.h>
56 +#include <linux/mroute.h>
57 +#include <linux/init.h>
58 +#include <linux/in6.h>
59 +#include <linux/inetdevice.h>
60 +#include <linux/etherdevice.h>   /**XXX added XXX */
61 +#include <linux/igmp.h>
62 +#include <linux/netfilter_ipv4.h>
63 +#include <linux/if_ether.h>
64 +
65 +#include <net/sock.h>
66 +#include <net/ip.h>
67 +#include <net/icmp.h>
68 +#include <net/protocol.h>
69 +#include <net/ipip.h>
70 +#include <net/arp.h>
71 +#include <net/checksum.h>
72 +#include <net/dsfield.h>
73 +#include <net/inet_ecn.h>
74 +#include <net/xfrm.h>
75 +
76 +#ifdef CONFIG_IPV6
77 +#include <net/ipv6.h>
78 +#include <net/ip6_fib.h>
79 +#include <net/ip6_route.h>
80 +#endif
81 +
82 +//#define GRE_DEBUG 1
83 +
84 +/*
85 +   Problems & solutions
86 +   --------------------
87 +
88 +   1. The most important issue is detecting local dead loops.
89 +   They would cause complete host lockup in transmit, which
90 +   would be "resolved" by stack overflow or, if queueing is enabled,
91 +   with infinite looping in net_bh.
92 +
93 +   We cannot track such dead loops during route installation,
94 +   it is infeasible task. The most general solutions would be
95 +   to keep skb->encapsulation counter (sort of local ttl),
96 +   and silently drop packet when it expires. It is the best
97 +   solution, but it supposes maintaing new variable in ALL
98 +   skb, even if no tunneling is used.
99 +
100 +   Current solution: t->recursion lock breaks dead loops. It looks
101 +   like dev->tbusy flag, but I preferred new variable, because
102 +   the semantics is different. One day, when hard_start_xmit
103 +   will be multithreaded we will have to use skb->encapsulation.
104 +
105 +
106 +
107 +   2. Networking dead loops would not kill routers, but would really
108 +   kill network. IP hop limit plays role of "t->recursion" in this case,
109 +   if we copy it from packet being encapsulated to upper header.
110 +   It is very good solution, but it introduces two problems:
111 +
112 +   - Routing protocols, using packets with ttl=1 (OSPF, RIP2),
113 +     do not work over tunnels.
114 +   - traceroute does not work. I planned to relay ICMP from tunnel,
115 +     so that this problem would be solved and traceroute output
116 +     would even more informative. This idea appeared to be wrong:
117 +     only Linux complies to rfc1812 now (yes, guys, Linux is the only
118 +     true router now :-)), all routers (at least, in neighbourhood of mine)
119 +     return only 8 bytes of payload. It is the end.
120 +
121 +   Hence, if we want that OSPF worked or traceroute said something reasonable,
122 +   we should search for another solution.
123 +
124 +   One of them is to parse packet trying to detect inner encapsulation
125 +   made by our node. It is difficult or even impossible, especially,
126 +   taking into account fragmentation. TO be short, tt is not solution at all.
127 +
128 +   Current solution: The solution was UNEXPECTEDLY SIMPLE.
129 +   We force DF flag on tunnels with preconfigured hop limit,
130 +   that is ALL. :-) Well, it does not remove the problem completely,
131 +   but exponential growth of network traffic is changed to linear
132 +   (branches, that exceed pmtu are pruned) and tunnel mtu
133 +   fastly degrades to value <68, where looping stops.
134 +   Yes, it is not good if there exists a router in the loop,
135 +   which does not force DF, even when encapsulating packets have DF set.
136 +   But it is not our problem! Nobody could accuse us, we made
137 +   all that we could make. Even if it is your gated who injected
138 +   fatal route to network, even if it were you who configured
139 +   fatal static route: you are innocent. :-)
140 +
141 +
142 +
143 +   3. Really, ipv4/ipip.c, ipv4/ip_gre.c and ipv6/sit.c contain
144 +   practically identical code. It would be good to glue them
145 +   together, but it is not very evident, how to make them modular.
146 +   sit is integral part of IPv6, ipip and gre are naturally modular.
147 +   We could extract common parts (hash table, ioctl etc)
148 +   to a separate module (ip_tunnel.c).
149 +
150 +   Alexey Kuznetsov.
151 + */
152 +
153 +static int ipgre_tunnel_init(struct net_device *dev);
154 +static void ipgre_ip_tunnel_setup(struct net_device *dev);
155 +static void ipgre_eth_tunnel_setup(struct net_device *dev);
156 +
157 +/* Fallback tunnel: no source, no destination, no key, no options */
158 +
159 +static int ipgre_fb_tunnel_init(struct net_device *dev);
160 +
161 +static struct net_device *ipgre_fb_tunnel_dev;
162 +
163 +/* Tunnel hash table */
164 +
165 +/*
166 +   4 hash tables:
167 +
168 +   3: (remote,local)
169 +   2: (remote,*)
170 +   1: (*,local)
171 +   0: (*,*)
172 +
173 +   We require exact key match i.e. if a key is present in packet
174 +   it will match only tunnel with the same key; if it is not present,
175 +   it will match only keyless tunnel.
176 +
177 +   All keysless packets, if not matched configured keyless tunnels
178 +   will match fallback tunnel.
179 + */
180 +
181 +#define HASH_SIZE  1024
182 +#define HASH(addr) (ntohl(addr)&1023)
183 +
184 +static struct ip_tunnel *tunnels[4][HASH_SIZE];
185 +
186 +#define tunnels_r_l    (tunnels[3])
187 +#define tunnels_r      (tunnels[2])
188 +#define tunnels_l      (tunnels[1])
189 +#define tunnels_wc     (tunnels[0])
190 +
191 +static DEFINE_RWLOCK(ipgre_lock);
192 +
193 +/* Given src, dst and key, find appropriate for input tunnel. */
194 +
195 +static struct ip_tunnel * ipgre_tunnel_lookup(__be32 remote, __be32 local, __be32 key)
196 +{
197 +       /* HACK */
198 +       unsigned hash_value = HASH(key);
199 +       struct ip_tunnel *t;
200 +
201 +       t = tunnels_r_l[hash_value];
202 +
203 +       if (t && (t->parms.i_key == key) && (t->dev->flags&IFF_UP)) {
204 +               return t;
205 +       }
206 +
207 +       t = tunnels_r[hash_value];
208 +                       if (t && (t->parms.i_key == key) && (t->dev->flags&IFF_UP))
209 +                               return t;
210 +
211 +       t = tunnels_l[hash_value];
212 +                       if (t && (t->parms.i_key == key) && (t->dev->flags&IFF_UP))
213 +                               return t;
214 +       t = tunnels_wc[hash_value];
215 +               if (t && (t->parms.i_key == key) && (t->dev->flags&IFF_UP))
216 +                       return t;
217 +       if (ipgre_fb_tunnel_dev->flags&IFF_UP)
218 +               return netdev_priv(ipgre_fb_tunnel_dev);
219 +       return NULL;
220 +}
221 +
222 +static struct ip_tunnel **ipgre_bucket(struct ip_tunnel *t)
223 +{
224 +       __be32 remote = t->parms.iph.daddr;
225 +       __be32 local = t->parms.iph.saddr;
226 +       __be32 key = t->parms.i_key;
227 +       unsigned h = HASH(key);
228 +       int prio = 0;
229 +
230 +       if (local)
231 +               prio |= 1;
232 +       if (remote && !MULTICAST(remote)) {
233 +               prio |= 2;
234 +               //h ^= HASH(remote);
235 +       }
236 +
237 +       return &tunnels[prio][h];
238 +}
239 +
240 +static void ipgre_tunnel_link(struct ip_tunnel *t)
241 +{
242 +       struct ip_tunnel **tp = ipgre_bucket(t);
243 +
244 +       t->next = *tp;
245 +       write_lock_bh(&ipgre_lock);
246 +       *tp = t;
247 +       write_unlock_bh(&ipgre_lock);
248 +}
249 +
250 +static void ipgre_tunnel_unlink(struct ip_tunnel *t)
251 +{
252 +       struct ip_tunnel **tp;
253 +
254 +       for (tp = ipgre_bucket(t); *tp; tp = &(*tp)->next) {
255 +               if (t == *tp) {
256 +                       write_lock_bh(&ipgre_lock);
257 +                       *tp = t->next;
258 +                       write_unlock_bh(&ipgre_lock);
259 +                       break;
260 +               }
261 +       }
262 +}
263 +
264 +static struct ip_tunnel * ipgre_tunnel_locate(struct ip_tunnel_parm *parms, int create)
265 +{
266 +       __be32 remote = parms->iph.daddr;
267 +       __be32 local = parms->iph.saddr;
268 +       __be32 key = parms->i_key;
269 +       __be16 proto = parms->proto_type;
270 +       struct ip_tunnel *t, **tp, *nt;
271 +       struct net_device *dev;
272 +       unsigned h = HASH(key);
273 +       int prio = 0;
274 +       char name[IFNAMSIZ];
275 +
276 +       if (local)
277 +               prio |= 1;
278 +       if (remote && !MULTICAST(remote)) {
279 +               prio |= 2;
280 +               //h ^= HASH(remote);
281 +       }
282 +       for (tp = &tunnels[prio][h]; (t = *tp) != NULL; tp = &t->next) {
283 +               if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr) {
284 +                       if (key == t->parms.i_key)
285 +                               return t;
286 +               }
287 +       }
288 +       if (!create)
289 +               return NULL;
290 +
291 +       printk(KERN_CRIT "Adding tunnel %s with key %d\n", parms->name, ntohl(key));
292 +
293 +       if (parms->name[0])
294 +               strlcpy(name, parms->name, IFNAMSIZ);
295 +       else {
296 +               int i;
297 +               for (i=1; i<100; i++) {
298 +                       sprintf(name, "gre%d", i);
299 +                       if (__dev_get_by_name(&init_net, name) == NULL)
300 +                               break;
301 +               }
302 +               if (i==100)
303 +                       goto failed;
304 +       }
305 +       
306 +       /* Tunnel creation: check payload type and call appropriate
307 +        * function */
308 +       switch (proto)
309 +       {
310 +           case ETH_P_IP:
311 +               dev = alloc_netdev(sizeof(*t), name, ipgre_ip_tunnel_setup);
312 +               break;
313 +           case ETH_P_ETH:
314 +               dev = alloc_netdev(sizeof(*t), name, ipgre_eth_tunnel_setup);
315 +               break;
316 +           default:
317 +               return NULL;
318 +       }
319 +
320 +       if (!dev)
321 +         return NULL;
322 +
323 +       dev->init = ipgre_tunnel_init;
324 +       nt = netdev_priv(dev);
325 +       nt->parms = *parms;
326 +
327 +       if (register_netdevice(dev) < 0) {
328 +               free_netdev(dev);
329 +               goto failed;
330 +       }
331 +
332 +       dev_hold(dev);
333 +       ipgre_tunnel_link(nt);
334 +       return nt;
335 +
336 +failed:
337 +       return NULL;
338 +}
339 +
340 +static void ipgre_tunnel_uninit(struct net_device *dev)
341 +{
342 +       ipgre_tunnel_unlink(netdev_priv(dev));
343 +       dev_put(dev);
344 +}
345 +
346 +
347 +static void ipgre_err(struct sk_buff *skb, u32 info)
348 +{
349 +#ifndef I_WISH_WORLD_WERE_PERFECT
350 +
351 +/* It is not :-( All the routers (except for Linux) return only
352 +   8 bytes of packet payload. It means, that precise relaying of
353 +   ICMP in the real Internet is absolutely infeasible.
354 +
355 +   Moreover, Cisco "wise men" put GRE key to the third word
356 +   in GRE header. It makes impossible maintaining even soft state for keyed
357 +   GRE tunnels with enabled checksum. Tell them "thank you".
358 +
359 +   Well, I wonder, rfc1812 was written by Cisco employee,
360 +   what the hell these idiots break standrads established
361 +   by themself???
362 + */
363 +
364 +       struct iphdr *iph = (struct iphdr*)skb->data;
365 +       __be16       *p = (__be16*)(skb->data+(iph->ihl<<2));
366 +       int grehlen = (iph->ihl<<2) + 4;
367 +       int type = icmp_hdr(skb)->type;
368 +       int code = icmp_hdr(skb)->code;
369 +       struct ip_tunnel *t;
370 +       __be16 flags;
371 +
372 +       flags = p[0];
373 +       if (flags&(GRE_CSUM|GRE_KEY|GRE_SEQ|GRE_ROUTING|GRE_VERSION)) {
374 +               if (flags&(GRE_VERSION|GRE_ROUTING))
375 +                       return;
376 +               if (flags&GRE_KEY) {
377 +                       grehlen += 4;
378 +                       if (flags&GRE_CSUM)
379 +                               grehlen += 4;
380 +               }
381 +       }
382 +
383 +       /* If only 8 bytes returned, keyed message will be dropped here */
384 +       if (skb_headlen(skb) < grehlen)
385 +               return;
386 +
387 +       switch (type) {
388 +       default:
389 +       case ICMP_PARAMETERPROB:
390 +               return;
391 +
392 +       case ICMP_DEST_UNREACH:
393 +               switch (code) {
394 +               case ICMP_SR_FAILED:
395 +               case ICMP_PORT_UNREACH:
396 +                       /* Impossible event. */
397 +                       return;
398 +               case ICMP_FRAG_NEEDED:
399 +                       /* Soft state for pmtu is maintained by IP core. */
400 +                       return;
401 +               default:
402 +                       /* All others are translated to HOST_UNREACH.
403 +                          rfc2003 contains "deep thoughts" about NET_UNREACH,
404 +                          I believe they are just ether pollution. --ANK
405 +                        */
406 +                       break;
407 +               }
408 +               break;
409 +       case ICMP_TIME_EXCEEDED:
410 +               if (code != ICMP_EXC_TTL)
411 +                       return;
412 +               break;
413 +       }
414 +
415 +       read_lock(&ipgre_lock);
416 +       t = ipgre_tunnel_lookup(iph->daddr, iph->saddr, (flags&GRE_KEY) ? *(((__be32*)p) + (grehlen>>2) - 1) : 0);
417 +       if (t == NULL || t->parms.iph.daddr == 0 || MULTICAST(t->parms.iph.daddr))
418 +               goto out;
419 +
420 +       if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
421 +               goto out;
422 +
423 +       if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO)
424 +               t->err_count++;
425 +       else
426 +               t->err_count = 1;
427 +       t->err_time = jiffies;
428 +out:
429 +       read_unlock(&ipgre_lock);
430 +       return;
431 +#else
432 +       struct iphdr *iph = (struct iphdr*)dp;
433 +       struct iphdr *eiph;
434 +       __be16       *p = (__be16*)(dp+(iph->ihl<<2));
435 +       int type = skb->h.icmph->type;
436 +       int code = skb->h.icmph->code;
437 +       int rel_type = 0;
438 +       int rel_code = 0;
439 +       __be32 rel_info = 0;
440 +       __u32 n = 0;
441 +       __be16 flags;
442 +       int grehlen = (iph->ihl<<2) + 4;
443 +       struct sk_buff *skb2;
444 +       struct flowi fl;
445 +       struct rtable *rt;
446 +
447 +       if (skb->dev->nd_net != &init_net)
448 +               return;
449 +
450 +       if (p[1] != htons(ETH_P_IP))
451 +               return;
452 +
453 +       flags = p[0];
454 +       if (flags&(GRE_CSUM|GRE_KEY|GRE_SEQ|GRE_ROUTING|GRE_VERSION)) {
455 +               if (flags&(GRE_VERSION|GRE_ROUTING))
456 +                       return;
457 +               if (flags&GRE_CSUM)
458 +                       grehlen += 4;
459 +               if (flags&GRE_KEY)
460 +                       grehlen += 4;
461 +               if (flags&GRE_SEQ)
462 +                       grehlen += 4;
463 +       }
464 +       if (len < grehlen + sizeof(struct iphdr))
465 +               return;
466 +       eiph = (struct iphdr*)(dp + grehlen);
467 +
468 +       switch (type) {
469 +       default:
470 +               return;
471 +       case ICMP_PARAMETERPROB:
472 +               n = ntohl(skb->h.icmph->un.gateway) >> 24;
473 +               if (n < (iph->ihl<<2))
474 +                       return;
475 +
476 +               /* So... This guy found something strange INSIDE encapsulated
477 +                  packet. Well, he is fool, but what can we do ?
478 +                */
479 +               rel_type = ICMP_PARAMETERPROB;
480 +               n -= grehlen;
481 +               rel_info = htonl(n << 24);
482 +               break;
483 +
484 +       case ICMP_DEST_UNREACH:
485 +               switch (code) {
486 +               case ICMP_SR_FAILED:
487 +               case ICMP_PORT_UNREACH:
488 +                       /* Impossible event. */
489 +                       return;
490 +               case ICMP_FRAG_NEEDED:
491 +                       /* And it is the only really necessary thing :-) */
492 +                       n = ntohs(skb->h.icmph->un.frag.mtu);
493 +                       if (n < grehlen+68)
494 +                               return;
495 +                       n -= grehlen;
496 +                       /* BSD 4.2 MORE DOES NOT EXIST IN NATURE. */
497 +                       if (n > ntohs(eiph->tot_len))
498 +                               return;
499 +                       rel_info = htonl(n);
500 +                       break;
501 +               default:
502 +                       /* All others are translated to HOST_UNREACH.
503 +                          rfc2003 contains "deep thoughts" about NET_UNREACH,
504 +                          I believe, it is just ether pollution. --ANK
505 +                        */
506 +                       rel_type = ICMP_DEST_UNREACH;
507 +                       rel_code = ICMP_HOST_UNREACH;
508 +                       break;
509 +               }
510 +               break;
511 +       case ICMP_TIME_EXCEEDED:
512 +               if (code != ICMP_EXC_TTL)
513 +                       return;
514 +               break;
515 +       }
516 +
517 +       /* Prepare fake skb to feed it to icmp_send */
518 +       skb2 = skb_clone(skb, GFP_ATOMIC);
519 +       if (skb2 == NULL)
520 +               return;
521 +       dst_release(skb2->dst);
522 +       skb2->dst = NULL;
523 +       skb_pull(skb2, skb->data - (u8*)eiph);
524 +       skb_reset_network_header(skb2);
525 +
526 +       /* Try to guess incoming interface */
527 +       memset(&fl, 0, sizeof(fl));
528 +       fl.fl_net = &init_net;
529 +       fl.fl4_dst = eiph->saddr;
530 +       fl.fl4_tos = RT_TOS(eiph->tos);
531 +       fl.proto = IPPROTO_GRE;
532 +       if (ip_route_output_key(&rt, &fl)) {
533 +               kfree_skb(skb2);
534 +               return;
535 +       }
536 +       skb2->dev = rt->u.dst.dev;
537 +
538 +       /* route "incoming" packet */
539 +       if (rt->rt_flags&RTCF_LOCAL) {
540 +               ip_rt_put(rt);
541 +               rt = NULL;
542 +               fl.fl4_dst = eiph->daddr;
543 +               fl.fl4_src = eiph->saddr;
544 +               fl.fl4_tos = eiph->tos;
545 +               if (ip_route_output_key(&rt, &fl) ||
546 +                   rt->u.dst.dev->type != ARPHRD_IPGRE) {
547 +                       ip_rt_put(rt);
548 +                       kfree_skb(skb2);
549 +                       return;
550 +               }
551 +       } else {
552 +               ip_rt_put(rt);
553 +               if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos, skb2->dev) ||
554 +                   skb2->dst->dev->type != ARPHRD_IPGRE) {
555 +                       kfree_skb(skb2);
556 +                       return;
557 +               }
558 +       }
559 +
560 +       /* change mtu on this route */
561 +       if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
562 +               if (n > dst_mtu(skb2->dst)) {
563 +                       kfree_skb(skb2);
564 +                       return;
565 +               }
566 +               skb2->dst->ops->update_pmtu(skb2->dst, n);
567 +       } else if (type == ICMP_TIME_EXCEEDED) {
568 +               struct ip_tunnel *t = netdev_priv(skb2->dev);
569 +               if (t->parms.iph.ttl) {
570 +                       rel_type = ICMP_DEST_UNREACH;
571 +                       rel_code = ICMP_HOST_UNREACH;
572 +               }
573 +       }
574 +
575 +       icmp_send(skb2, rel_type, rel_code, rel_info);
576 +       kfree_skb(skb2);
577 +#endif
578 +}
579 +
580 +static inline void ipgre_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
581 +{
582 +       if (INET_ECN_is_ce(iph->tos)) {
583 +               if (skb->protocol == htons(ETH_P_IP)) {
584 +                       IP_ECN_set_ce(ip_hdr(skb));
585 +               } else if (skb->protocol == htons(ETH_P_IPV6)) {
586 +                       IP6_ECN_set_ce(ipv6_hdr(skb));
587 +               }
588 +       }
589 +}
590 +
591 +static inline u8
592 +ipgre_ecn_encapsulate(u8 tos, struct iphdr *old_iph, struct sk_buff *skb)
593 +{
594 +       u8 inner = 0;
595 +       if (skb->protocol == htons(ETH_P_IP))
596 +               inner = old_iph->tos;
597 +       else if (skb->protocol == htons(ETH_P_IPV6))
598 +               inner = ipv6_get_dsfield((struct ipv6hdr *)old_iph);
599 +       return INET_ECN_encapsulate(tos, inner);
600 +}
601 +
602 +static int ipgre_rcv(struct sk_buff *skb)
603 +{
604 +       struct iphdr *iph;
605 +       u8     *h;
606 +       __be16    flags;
607 +       __sum16   csum = 0;
608 +       __be32 key = 0;
609 +       u32    seqno = 0;
610 +       struct ip_tunnel *tunnel;
611 +       int    offset = 4;
612 +       __be16 proto;
613 +
614 +       if (skb->dev->nd_net != &init_net) {
615 +               kfree_skb(skb);
616 +               return 0;
617 +       }
618 +       if (!pskb_may_pull(skb, 16))
619 +               goto drop_nolock;
620 +
621 +       iph = ip_hdr(skb);
622 +       h = skb->data;
623 +       flags = *(__be16*)h;
624 +
625 +#ifdef GRE_DEBUG
626 +       printk(KERN_DEBUG "gre.c [601] src:%x dst:%x  proto:%d %x", iph->saddr, iph->daddr, iph->protocol, skb->data);
627 +#endif 
628 +       proto = ntohs(*(__be16*)(h+2)); /* XXX added XXX */
629 +       
630 +       if (flags&(GRE_CSUM|GRE_KEY|GRE_ROUTING|GRE_SEQ|GRE_VERSION)) {
631 +               /* - Version must be 0.
632 +                  - We do not support routing headers.
633 +                */
634 +               if (flags&(GRE_VERSION|GRE_ROUTING))
635 +                       goto drop_nolock;
636 +
637 +               if (flags&GRE_CSUM) {
638 +                       switch (skb->ip_summed) {
639 +                       case CHECKSUM_COMPLETE:
640 +                               csum = csum_fold(skb->csum);
641 +                               if (!csum)
642 +                                       break;
643 +                               /* fall through */
644 +                       case CHECKSUM_NONE:
645 +                               skb->csum = 0;
646 +                               csum = __skb_checksum_complete(skb);
647 +                               skb->ip_summed = CHECKSUM_COMPLETE;
648 +                       }
649 +                       offset += 4;
650 +               }
651 +               if (flags&GRE_KEY) {
652 +                       key = *(__be32*)(h + offset);
653 +                       offset += 4;
654 +               }
655 +               if (flags&GRE_SEQ) {
656 +                       seqno = ntohl(*(__be32*)(h + offset));
657 +                       offset += 4;
658 +               }
659 +       }
660 +
661 +       read_lock(&ipgre_lock);
662 +       if ((tunnel = ipgre_tunnel_lookup(iph->saddr, iph->daddr, key)) != NULL) {
663 +               secpath_reset(skb);
664 +
665 +               skb->protocol = *(__be16*)(h + 2);
666 +               /* WCCP version 1 and 2 protocol decoding.
667 +                * - Change protocol to IP
668 +                * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
669 +                */
670 +               if (flags == 0 &&
671 +                   skb->protocol == htons(ETH_P_WCCP)) {
672 +                       skb->protocol = htons(ETH_P_IP);
673 +                       if ((*(h + offset) & 0xF0) != 0x40)
674 +                               offset += 4;
675 +               }
676 +
677 +               //skb->mac.raw = skb->nh.raw;
678 +               skb_reset_mac_header(skb);
679 +               __pskb_pull(skb, offset);
680 +               skb_reset_network_header(skb);
681 +               skb_postpull_rcsum(skb, skb_transport_header(skb), offset);
682 +               if(proto == ETH_P_ETH)
683 +                 {
684 +#ifdef GRE_DEBUG
685 +                   unsigned char* tmp_hdr = skb->data;
686 +                   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);
687 +#endif             
688 +                   skb->protocol = eth_type_trans(skb, tunnel->dev);
689 +
690 +                   /* XXX added these lines to make arp work? XXX */
691 +                   /*skb->mac.raw = skb->data;*/
692 +                   skb->network_header = skb->network_header + ETH_HLEN;
693 +                   /* XXX added these lines to make arp work? XXX */
694 +
695 +#ifdef GRE_DEBUG
696 +                   tmp_hdr = skb->data;
697 +                   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);
698 +                   printk(KERN_ALERT "gre.c [671] received ethernet on gre %x %x\n",skb->protocol, ((skb->nh).iph)->protocol); 
699 +#endif
700 +                   memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
701 +                 }
702 +               else
703 +                 skb->pkt_type = PACKET_HOST;
704 +#ifdef CONFIG_NET_IPGRE_BROADCAST
705 +               if (MULTICAST(iph->daddr)) {
706 +                       /* Looped back packet, drop it! */
707 +                       if (((struct rtable*)skb->dst)->fl.iif == 0)
708 +                               goto drop;
709 +                       tunnel->stat.multicast++;
710 +                       skb->pkt_type = PACKET_BROADCAST;
711 +               }
712 +#endif
713 +
714 +               if (((flags&GRE_CSUM) && csum) ||
715 +                   (!(flags&GRE_CSUM) && tunnel->parms.i_flags&GRE_CSUM)) {
716 +                       tunnel->stat.rx_crc_errors++;
717 +                       tunnel->stat.rx_errors++;
718 +                       goto drop;
719 +               }
720 +               if (tunnel->parms.i_flags&GRE_SEQ) {
721 +                       if (!(flags&GRE_SEQ) ||
722 +                           (tunnel->i_seqno && (s32)(seqno - tunnel->i_seqno) < 0)) {
723 +                               tunnel->stat.rx_fifo_errors++;
724 +                               tunnel->stat.rx_errors++;
725 +                               goto drop;
726 +                       }
727 +                       tunnel->i_seqno = seqno + 1;
728 +               }
729 +               tunnel->stat.rx_packets++;
730 +               tunnel->stat.rx_bytes += skb->len;
731 +               skb->dev = tunnel->dev;
732 +               dst_release(skb->dst);
733 +               skb->dst = NULL;
734 +               nf_reset(skb);
735 +               ipgre_ecn_decapsulate(iph, skb);
736 +               netif_rx(skb);
737 +               read_unlock(&ipgre_lock);
738 +               return(0);
739 +       }
740 +       icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
741 +
742 +drop:
743 +       read_unlock(&ipgre_lock);
744 +drop_nolock:
745 +       kfree_skb(skb);
746 +       return(0);
747 +}
748 +
749 +static int ipgre_ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
750 +{
751 +       struct ip_tunnel *tunnel = netdev_priv(dev);
752 +       struct net_device_stats *stats = &tunnel->stat;
753 +       struct iphdr  *old_iph = ip_hdr(skb);
754 +       struct iphdr  *tiph;
755 +       u8     tos;
756 +       __be16 df;
757 +       struct rtable *rt;                      /* Route to the other host */
758 +       struct net_device *tdev;                        /* Device to other host */
759 +       struct iphdr  *iph;                     /* Our new IP header */
760 +       int    max_headroom;                    /* The extra header space needed */
761 +       int    gre_hlen;
762 +       __be32 dst;
763 +       int    mtu;
764 +
765 +       if (tunnel->recursion++) {
766 +               tunnel->stat.collisions++;
767 +               goto tx_error;
768 +       }
769 +
770 +       if (dev->hard_header) {
771 +               gre_hlen = 0;
772 +               tiph = (struct iphdr*)skb->data;
773 +       } else {
774 +               gre_hlen = tunnel->hlen;
775 +               tiph = &tunnel->parms.iph;
776 +       }
777 +
778 +       if ((dst = tiph->daddr) == 0) {
779 +               /* NBMA tunnel */
780 +
781 +               if (skb->dst == NULL) {
782 +                       tunnel->stat.tx_fifo_errors++;
783 +                       goto tx_error;
784 +               }
785 +
786 +               if (skb->protocol == htons(ETH_P_IP)) {
787 +                       rt = (struct rtable*)skb->dst;
788 +                       if ((dst = rt->rt_gateway) == 0)
789 +                               goto tx_error_icmp;
790 +               }
791 +#ifdef CONFIG_IPV6
792 +               else if (skb->protocol == htons(ETH_P_IPV6)) {
793 +                       struct in6_addr *addr6;
794 +                       int addr_type;
795 +                       struct neighbour *neigh = skb->dst->neighbour;
796 +
797 +                       if (neigh == NULL)
798 +                               goto tx_error;
799 +
800 +                       addr6 = (struct in6_addr*)&neigh->primary_key;
801 +                       addr_type = ipv6_addr_type(addr6);
802 +
803 +                       if (addr_type == IPV6_ADDR_ANY) {
804 +                               addr6 = &ipv6_hdr(skb)->daddr;
805 +                               addr_type = ipv6_addr_type(addr6);
806 +                       }
807 +
808 +                       if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
809 +                               goto tx_error_icmp;
810 +
811 +               }
812 +#endif
813 +               else
814 +                       goto tx_error;
815 +       }
816 +
817 +       tos = tiph->tos;
818 +       if (tos&1) {
819 +               if (skb->protocol == htons(ETH_P_IP))
820 +                       tos = old_iph->tos;
821 +               tos &= ~1;
822 +       }
823 +
824 +       {
825 +               struct flowi fl = { .fl_net = &init_net,
826 +                                   .oif = tunnel->parms.link,
827 +                                   .nl_u = { .ip4_u =
828 +                                             { .daddr = dst,
829 +                                               .saddr = tiph->saddr,
830 +                                               .tos = RT_TOS(tos) } },
831 +                                   .proto = IPPROTO_GRE };
832 +               if (ip_route_output_key(&rt, &fl)) {
833 +                       tunnel->stat.tx_carrier_errors++;
834 +                       goto tx_error;
835 +               }
836 +       }
837 +       tdev = rt->u.dst.dev;
838 +
839 +
840 +       if (tdev == dev) {
841 +               ip_rt_put(rt);
842 +               tunnel->stat.collisions++;
843 +               goto tx_error;
844 +       }
845 +
846 +       df = tiph->frag_off;
847 +       if (df)
848 +               mtu = dst_mtu(&rt->u.dst) - tunnel->hlen;
849 +       else
850 +               mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu;
851 +
852 +       if (skb->dst)
853 +               skb->dst->ops->update_pmtu(skb->dst, mtu);
854 +
855 +       if (skb->protocol == htons(ETH_P_IP)) {
856 +               df |= (old_iph->frag_off&htons(IP_DF));
857 +
858 +               if ((old_iph->frag_off&htons(IP_DF)) &&
859 +                   mtu < ntohs(old_iph->tot_len)) {
860 +                       icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
861 +                       ip_rt_put(rt);
862 +                       goto tx_error;
863 +               }
864 +       }
865 +#ifdef CONFIG_IPV6
866 +       else if (skb->protocol == htons(ETH_P_IPV6)) {
867 +               struct rt6_info *rt6 = (struct rt6_info*)skb->dst;
868 +
869 +               if (rt6 && mtu < dst_mtu(skb->dst) && mtu >= IPV6_MIN_MTU) {
870 +                       if ((tunnel->parms.iph.daddr && !MULTICAST(tunnel->parms.iph.daddr)) ||
871 +                           rt6->rt6i_dst.plen == 128) {
872 +                               rt6->rt6i_flags |= RTF_MODIFIED;
873 +                               skb->dst->metrics[RTAX_MTU-1] = mtu;
874 +                       }
875 +               }
876 +
877 +               if (mtu >= IPV6_MIN_MTU && mtu < skb->len - tunnel->hlen + gre_hlen) {
878 +                       icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
879 +                       ip_rt_put(rt);
880 +                       goto tx_error;
881 +               }
882 +       }
883 +#endif
884 +
885 +       if (tunnel->err_count > 0) {
886 +               if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
887 +                       tunnel->err_count--;
888 +
889 +                       dst_link_failure(skb);
890 +               } else
891 +                       tunnel->err_count = 0;
892 +       }
893 +
894 +       max_headroom = LL_RESERVED_SPACE(tdev) + gre_hlen;
895 +
896 +       if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) {
897 +               struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
898 +               if (!new_skb) {
899 +                       ip_rt_put(rt);
900 +                       stats->tx_dropped++;
901 +                       dev_kfree_skb(skb);
902 +                       tunnel->recursion--;
903 +                       return 0;
904 +               }
905 +               if (skb->sk)
906 +                       skb_set_owner_w(new_skb, skb->sk);
907 +               dev_kfree_skb(skb);
908 +               skb = new_skb;
909 +               old_iph = ip_hdr(skb);
910 +       }
911 +
912 +       skb->transport_header = skb->network_header;
913 +       skb_push(skb, gre_hlen);
914 +       memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
915 +       IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
916 +                             IPSKB_REROUTED);
917 +       dst_release(skb->dst);
918 +       skb->dst = &rt->u.dst;
919 +
920 +       /*
921 +        *      Push down and install the IPIP header.
922 +        */
923 +
924 +       iph                     =       ip_hdr(skb);
925 +       iph->version            =       4;
926 +       iph->ihl                =       sizeof(struct iphdr) >> 2;
927 +       iph->frag_off           =       df;
928 +       iph->protocol           =       IPPROTO_GRE;
929 +       iph->tos                =       ipgre_ecn_encapsulate(tos, old_iph, skb);
930 +       iph->daddr              =       rt->rt_dst;
931 +       iph->saddr              =       rt->rt_src;
932 +
933 +       if ((iph->ttl = tiph->ttl) == 0) {
934 +               if (skb->protocol == htons(ETH_P_IP))
935 +                       iph->ttl = old_iph->ttl;
936 +#ifdef CONFIG_IPV6
937 +               else if (skb->protocol == htons(ETH_P_IPV6))
938 +                       iph->ttl = ((struct ipv6hdr*)old_iph)->hop_limit;
939 +#endif
940 +               else
941 +                       iph->ttl = dst_metric(&rt->u.dst, RTAX_HOPLIMIT);
942 +       }
943 +
944 +       ((__be16*)(iph+1))[0] = tunnel->parms.o_flags;
945 +       ((__be16*)(iph+1))[1] = skb->protocol;
946 +
947 +       if (tunnel->parms.o_flags&(GRE_KEY|GRE_CSUM|GRE_SEQ)) {
948 +               __be32 *ptr = (__be32*)(((u8*)iph) + tunnel->hlen - 4);
949 +
950 +               if (tunnel->parms.o_flags&GRE_SEQ) {
951 +                       ++tunnel->o_seqno;
952 +                       *ptr = htonl(tunnel->o_seqno);
953 +                       ptr--;
954 +               }
955 +               if (tunnel->parms.o_flags&GRE_KEY) {
956 +                       *ptr = tunnel->parms.o_key;
957 +                       ptr--;
958 +               }
959 +               if (tunnel->parms.o_flags&GRE_CSUM) {
960 +                       *ptr = 0;
961 +                       *(__sum16*)ptr = ip_compute_csum((void*)(iph+1), skb->len - sizeof(struct iphdr));
962 +               }
963 +       }
964 +
965 +       nf_reset(skb);
966 +
967 +       IPTUNNEL_XMIT();
968 +       tunnel->recursion--;
969 +       return 0;
970 +
971 +tx_error_icmp:
972 +       dst_link_failure(skb);
973 +
974 +tx_error:
975 +       stats->tx_errors++;
976 +       dev_kfree_skb(skb);
977 +       tunnel->recursion--;
978 +       return 0;
979 +}
980 +
981 +static int ipgre_eth_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
982 +{
983 +       struct ip_tunnel *tunnel = netdev_priv(dev);
984 +       struct net_device_stats *stats = &tunnel->stat;
985 +       struct iphdr *old_iph = ip_hdr(skb);
986 +       struct iphdr *tiph = &tunnel->parms.iph;
987 +       u8     tos;
988 +       __be16 df;
989 +       struct rtable *rt;              /* Route to the other host */
990 +       struct net_device *tdev;        /* Device to other host */
991 +       int    gre_hlen = tunnel->hlen; /* XXX changed XXX*/
992 +       //struct etheriphdr  *ethiph;
993 +       struct iphdr  *iph;             /* Our new IP header */
994 +       int    max_headroom;            /* The extra header space needed */
995 +       int    mtu;
996 +
997 +#ifdef GRE_DEBUG
998 +       printk(KERN_ALERT "gre.c:972 Starting xmit\n");
999 +#endif
1000 +
1001 +       if (tunnel->recursion++) {
1002 +               stats->collisions++;
1003 +               goto tx_error;
1004 +       }
1005 +
1006 +       /* Need valid non-multicast daddr.  */
1007 +       if (tiph->daddr == 0 || MULTICAST(tiph->daddr))
1008 +               goto tx_error;
1009 +
1010 +       tos = tiph->tos;
1011 +       if (tos&1) {
1012 +               if (skb->protocol == htons(ETH_P_IP))
1013 +                       tos = old_iph->tos;
1014 +               tos &= ~1;
1015 +       }
1016 +#ifdef GRE_DEBUG
1017 +       printk(KERN_ALERT "gre.c:991 Passed tos assignment.\n");
1018 +#endif
1019 +
1020 +
1021 +       {
1022 +               struct flowi fl = { .fl_net = &init_net,
1023 +                                   .oif = tunnel->parms.link,
1024 +                                   .nl_u = { .ip4_u =
1025 +                                             { .daddr = tiph->daddr,
1026 +                                               .saddr = tiph->saddr,
1027 +                                               .tos = RT_TOS(tos) } },
1028 +                                   .proto = IPPROTO_GRE };
1029 +               if (ip_route_output_key(&rt, &fl)) {
1030 +                       stats->tx_carrier_errors++;
1031 +                       goto tx_error_icmp;
1032 +               }
1033 +       }
1034 +       tdev = rt->u.dst.dev;
1035 +#ifdef GRE_DEBUG
1036 +       printk(KERN_ALERT "gre.c:1006 Passed the route retrieval\n");
1037 +#endif
1038 +       if (tdev == dev) {
1039 +               ip_rt_put(rt);
1040 +               stats->collisions++;
1041 +               goto tx_error;
1042 +       }
1043 +#ifdef GRE_DEBUG
1044 +       printk(KERN_ALERT "gre.c:1018 Passed tdev collision check.\n");
1045 +#endif
1046 +
1047 +       /* Check MTU stuff if kernel panic */
1048 +       df = tiph->frag_off;
1049 +       if (df)
1050 +               mtu = dst_mtu(&rt->u.dst) - tunnel->hlen;
1051 +       else
1052 +               mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu;
1053 +/*
1054 +       if (skb->dst)
1055 +               skb->dst->ops->update_pmtu(skb->dst, mtu);
1056 +        XXX */
1057 +#ifdef GRE_DEBUG
1058 +       printk(KERN_ALERT "gre.c:1032 Passed the pmtu setting.\n");
1059 +#endif
1060 +
1061 +       if (skb->protocol == htons(ETH_P_IP)) {
1062 +               df |= (old_iph->frag_off&htons(IP_DF));
1063 +
1064 +               if ((old_iph->frag_off & htons(IP_DF)) &&
1065 +                   mtu < ntohs(old_iph->tot_len)) {
1066 +                       icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
1067 +                       ip_rt_put(rt);
1068 +                       goto tx_error;
1069 +               }
1070 +       }
1071 +#ifdef CONFIG_IPV6
1072 +       else if (skb->protocol == htons(ETH_P_IPV6)) {
1073 +               struct rt6_info *rt6 = (struct rt6_info*)skb->dst;
1074 +
1075 +               if (rt6 && mtu < dst_mtu(skb->dst) && mtu >= IPV6_MIN_MTU) {
1076 +                       if (tiph->daddr || rt6->rt6i_dst.plen == 128) {
1077 +                               rt6->rt6i_flags |= RTF_MODIFIED;
1078 +                               skb->dst->metrics[RTAX_MTU-1] = mtu;
1079 +                       }
1080 +               }
1081 +
1082 +               /* @@@ Is this correct?  */
1083 +               if (mtu >= IPV6_MIN_MTU && mtu < skb->len - tunnel->hlen + gre_hlen) {
1084 +                       icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
1085 +                       ip_rt_put(rt);
1086 +                       goto tx_error;
1087 +               }
1088 +       }
1089 +#endif
1090 +#ifdef GRE_DEBUG
1091 +       printk(KERN_ALERT "gre.c:1065 Passed the fragmentation check.\n");
1092 +#endif
1093 +
1094 +       if (tunnel->err_count > 0) {
1095 +               if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
1096 +                       tunnel->err_count--;
1097 +                       dst_link_failure(skb);
1098 +               } else
1099 +                       tunnel->err_count = 0;
1100 +       }
1101 +
1102 +       max_headroom = LL_RESERVED_SPACE(tdev) + gre_hlen;
1103 +
1104 +       if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) {
1105 +               struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
1106 +               if (!new_skb) {
1107 +                       ip_rt_put(rt);
1108 +                       stats->tx_dropped++;
1109 +                       dev_kfree_skb(skb);
1110 +                       tunnel->recursion--;
1111 +                       return 0;
1112 +               }
1113 +               if (skb->sk)
1114 +                       skb_set_owner_w(new_skb, skb->sk);
1115 +               dev_kfree_skb(skb);
1116 +               skb = new_skb;
1117 +               old_iph = ip_hdr(skb);
1118 +       }
1119 +#ifdef GRE_DEBUG
1120 +       printk(KERN_ALERT "gre.c:1094 Passed the headroom calculation\n");
1121 +#endif
1122 +
1123 +
1124 +       skb->transport_header = skb->mac_header; // Added by valas
1125 +       skb_push(skb, gre_hlen);
1126 +       skb_reset_network_header(skb);
1127 +       memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1128 +       dst_release(skb->dst);
1129 +       skb->dst = &rt->u.dst;
1130 +
1131 +       /*
1132 +        *      Push down and install the etherip header.
1133 +        */
1134 +
1135 +       iph                     =       ip_hdr(skb);
1136 +       iph->version            =       4;
1137 +       iph->ihl                =       sizeof(struct iphdr) >> 2;
1138 +       iph->frag_off           =       df;
1139 +       iph->protocol           =       IPPROTO_GRE;
1140 +       iph->tos                =       ipgre_ecn_encapsulate(tos, old_iph, skb);
1141 +       iph->daddr              =       rt->rt_dst;
1142 +       iph->saddr              =       rt->rt_src;
1143 +
1144 +/*     ethiph->version         =       htons(ETHERIP_VERSION); */
1145 +#ifdef GRE_DEBUG
1146 +       printk(KERN_ALERT "gre.c:1121 Passed outer IP header construction.\n");
1147 +#endif
1148 +
1149 +       if ((iph->ttl = tiph->ttl) == 0) {
1150 +               if (skb->protocol == htons(ETH_P_IP))
1151 +                       iph->ttl = old_iph->ttl;
1152 +#ifdef CONFIG_IPV6
1153 +               else if (skb->protocol == htons(ETH_P_IPV6))
1154 +                       iph->ttl = ((struct ipv6hdr*)old_iph)->hop_limit;
1155 +#endif
1156 +               else
1157 +                       iph->ttl = dst_metric(&rt->u.dst, RTAX_HOPLIMIT);
1158 +       }
1159 +#ifdef GRE_DEBUG
1160 +       printk(KERN_ALERT "gre.c:1006 Passed the TTL check.\n");
1161 +#endif
1162 +
1163 +       ((__be16*)(iph+1))[0] = tunnel->parms.o_flags;
1164 +       ((__be16*)(iph+1))[1] = htons(tunnel->parms.proto_type);
1165 +
1166 +       if (tunnel->parms.o_flags&(GRE_KEY|GRE_CSUM|GRE_SEQ)) {
1167 +               __be32 *ptr = (__be32*)(((u8*)iph) + tunnel->hlen - 4);
1168 +
1169 +               if (tunnel->parms.o_flags&GRE_SEQ) {
1170 +                       ++tunnel->o_seqno;
1171 +                       *ptr = htonl(tunnel->o_seqno);
1172 +                       ptr--;
1173 +               }
1174 +               if (tunnel->parms.o_flags&GRE_KEY) {
1175 +                       *ptr = tunnel->parms.o_key;
1176 +                       ptr--;
1177 +               }
1178 +               if (tunnel->parms.o_flags&GRE_CSUM) {
1179 +                       *ptr = 0;
1180 +                       *(__sum16*)ptr = ip_compute_csum((void*)(iph+1), skb->len - sizeof(struct iphdr));
1181 +               }
1182 +       }
1183 +#ifdef GRE_DEBUG
1184 +       printk(KERN_ALERT "gre.c:1006 Passed the tunnel transmit.\n");
1185 +#endif
1186 +
1187 +       nf_reset(skb);
1188 +
1189 +       IPTUNNEL_XMIT();
1190 +       tunnel->recursion--;
1191 +       return 0;
1192 +
1193 +tx_error_icmp:
1194 +       dst_link_failure(skb);
1195 +
1196 +tx_error:
1197 +       stats->tx_errors++;
1198 +       dev_kfree_skb(skb);
1199 +       tunnel->recursion--;
1200 +       return 0;
1201 +}
1202 +
1203 +
1204 +static int
1205 +ipgre_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
1206 +{
1207 +       int err = 0;
1208 +       struct ip_tunnel_parm p;
1209 +       struct ip_tunnel *t;
1210 +
1211 +        printk(KERN_ALERT "1174 GRE: entering gre ioctl. command is: %d\n", cmd);
1212 +
1213 +       switch (cmd) {
1214 +       case SIOCGETTUNNEL:
1215 +               t = NULL;
1216 +               if (dev == ipgre_fb_tunnel_dev) {
1217 +                       if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
1218 +                               err = -EFAULT;
1219 +                               break;
1220 +                       }
1221 +                       t = ipgre_tunnel_locate(&p, 0);
1222 +               }
1223 +               if (t == NULL)
1224 +                       t = netdev_priv(dev);
1225 +               memcpy(&p, &t->parms, sizeof(p));
1226 +               if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1227 +                       err = -EFAULT;
1228 +               break;
1229 +
1230 +       case SIOCADDTUNNEL:
1231 +       case SIOCCHGTUNNEL:
1232 +               err = -EPERM;
1233 +               if (!capable(CAP_NET_ADMIN))
1234 +                       goto done;
1235 +
1236 +               err = -EFAULT;
1237 +               if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1238 +                       goto done;
1239 +
1240 +               err = -EINVAL;
1241 +               if (p.iph.version != 4 || p.iph.protocol != IPPROTO_GRE ||
1242 +                   p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)) ||
1243 +                   ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING)))
1244 +                       goto done;
1245 +               if (p.iph.ttl)
1246 +                       p.iph.frag_off |= htons(IP_DF);
1247 +
1248 +               if (!(p.i_flags&GRE_KEY))
1249 +                       p.i_key = 0;
1250 +               if (!(p.o_flags&GRE_KEY))
1251 +                       p.o_key = 0;
1252 +
1253 +               t = ipgre_tunnel_locate(&p, cmd == SIOCADDTUNNEL);
1254 +               if (t) printk(KERN_ALERT "1174 GRE: proto %s %d\n", p.name, p.proto_type);
1255 +               if (dev != ipgre_fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
1256 +                       if (t != NULL) {
1257 +                               if (t->dev != dev) {
1258 +                                       err = -EEXIST;
1259 +                                       break;
1260 +                               }
1261 +                       } else {
1262 +                               unsigned nflags=0;
1263 +
1264 +                               t = netdev_priv(dev);
1265 +
1266 +                               if (MULTICAST(p.iph.daddr))
1267 +                                       nflags = IFF_BROADCAST;
1268 +                               else if (p.iph.daddr)
1269 +                                       nflags = IFF_POINTOPOINT;
1270 +                               
1271 +                               /* XXX:Set back IFF_BROADCAST if
1272 +                                * transporting ethernet */
1273 +                               printk(KERN_ALERT "1193 GRE: proto %s %d\n", p.name, p.proto_type);
1274 +                               if (p.proto_type == ETH_P_ETH)
1275 +                                       nflags = IFF_BROADCAST;
1276 +
1277 +                               if ((dev->flags^nflags)&(IFF_POINTOPOINT|IFF_BROADCAST)) {
1278 +                                       err = -EINVAL;
1279 +                                       break;
1280 +                               }
1281 +                               ipgre_tunnel_unlink(t);
1282 +                               t->parms.iph.saddr = p.iph.saddr;
1283 +                               t->parms.iph.daddr = p.iph.daddr;
1284 +                               t->parms.i_key = p.i_key;
1285 +                               t->parms.o_key = p.o_key;
1286 +                               /* XXX:Copy in the protocol field */
1287 +                               t->parms.proto_type = p.proto_type;
1288 +                               if (t->parms.proto_type != ETH_P_ETH)
1289 +                               {
1290 +                                       memcpy(dev->dev_addr, &p.iph.saddr, 4);
1291 +                                       memcpy(dev->broadcast, &p.iph.daddr, 4);
1292 +                               }
1293 +                               ipgre_tunnel_link(t);
1294 +                               netdev_state_change(dev);
1295 +                       }
1296 +               }
1297 +
1298 +               if (t) {
1299 +                       err = 0;
1300 +                       if (cmd == SIOCCHGTUNNEL) {
1301 +                               t->parms.iph.ttl = p.iph.ttl;
1302 +                               t->parms.iph.tos = p.iph.tos;
1303 +                               t->parms.iph.frag_off = p.iph.frag_off;
1304 +                       }
1305 +                       if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
1306 +                               err = -EFAULT;
1307 +               } else
1308 +                       err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
1309 +               break;
1310 +
1311 +       case SIOCDELTUNNEL:
1312 +               err = -EPERM;
1313 +               if (!capable(CAP_NET_ADMIN))
1314 +                       goto done;
1315 +
1316 +               if (dev == ipgre_fb_tunnel_dev) {
1317 +                       err = -EFAULT;
1318 +                       if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1319 +                               goto done;
1320 +                       err = -ENOENT;
1321 +                       if ((t = ipgre_tunnel_locate(&p, 0)) == NULL)
1322 +                               goto done;
1323 +                       err = -EPERM;
1324 +                       if (t == netdev_priv(ipgre_fb_tunnel_dev))
1325 +                               goto done;
1326 +                       dev = t->dev;
1327 +               }
1328 +               unregister_netdevice(dev); // added by Valas
1329 +               break;
1330 +
1331 +       default:
1332 +               err = -EINVAL;
1333 +       }
1334 +
1335 +done:
1336 +       return err;
1337 +}
1338 +
1339 +static struct net_device_stats *ipgre_tunnel_get_stats(struct net_device *dev)
1340 +{
1341 +       return &(((struct ip_tunnel*)netdev_priv(dev))->stat);
1342 +}
1343 +
1344 +static int ipgre_tunnel_change_mtu(struct net_device *dev, int new_mtu)
1345 +{
1346 +       struct ip_tunnel *tunnel = netdev_priv(dev);
1347 +       if (new_mtu < 68 || new_mtu > 0xFFF8 - tunnel->hlen)
1348 +               return -EINVAL;
1349 +       dev->mtu = new_mtu;
1350 +       return 0;
1351 +}
1352 +
1353 +#ifdef CONFIG_NET_IPGRE_BROADCAST
1354 +/* Nice toy. Unfortunately, useless in real life :-)
1355 +   It allows to construct virtual multiprotocol broadcast "LAN"
1356 +   over the Internet, provided multicast routing is tuned.
1357 +
1358 +
1359 +   I have no idea was this bicycle invented before me,
1360 +   so that I had to set ARPHRD_IPGRE to a random value.
1361 +   I have an impression, that Cisco could make something similar,
1362 +   but this feature is apparently missing in IOS<=11.2(8).
1363 +
1364 +   I set up 10.66.66/24 and fec0:6666:6666::0/96 as virtual networks
1365 +   with broadcast 224.66.66.66. If you have access to mbone, play with me :-)
1366 +
1367 +   ping -t 255 224.66.66.66
1368 +
1369 +   If nobody answers, mbone does not work.
1370 +
1371 +   ip tunnel add Universe mode gre remote 224.66.66.66 local <Your_real_addr> ttl 255
1372 +   ip addr add 10.66.66.<somewhat>/24 dev Universe
1373 +   ifconfig Universe up
1374 +   ifconfig Universe add fe80::<Your_real_addr>/10
1375 +   ifconfig Universe add fec0:6666:6666::<Your_real_addr>/96
1376 +   ftp 10.66.66.66
1377 +   ...
1378 +   ftp fec0:6666:6666::193.233.7.65
1379 +   ...
1380 +
1381 + */
1382 +
1383 +static int ipgre_header(struct sk_buff *skb, struct net_device *dev, unsigned short type,
1384 +                       void *daddr, void *saddr, unsigned len)
1385 +{
1386 +       struct ip_tunnel *t = netdev_priv(dev);
1387 +       struct iphdr *iph = (struct iphdr *)skb_push(skb, t->hlen);
1388 +       __be16 *p = (__be16*)(iph+1);
1389 +
1390 +       memcpy(iph, &t->parms.iph, sizeof(struct iphdr));
1391 +       p[0]            = t->parms.o_flags;
1392 +       p[1]            = htons(type);
1393 +
1394 +       /*
1395 +        *      Set the source hardware address.
1396 +        */
1397 +
1398 +       if (saddr)
1399 +               memcpy(&iph->saddr, saddr, 4);
1400 +
1401 +       if (daddr) {
1402 +               memcpy(&iph->daddr, daddr, 4);
1403 +               return t->hlen;
1404 +       }
1405 +       if (iph->daddr && !MULTICAST(iph->daddr))
1406 +               return t->hlen;
1407 +
1408 +       return -t->hlen;
1409 +}
1410 +
1411 +static int ipgre_open(struct net_device *dev)
1412 +{
1413 +       struct ip_tunnel *t = netdev_priv(dev);
1414 +
1415 +       if (MULTICAST(t->parms.iph.daddr)) {
1416 +               struct flowi fl = { .fl_net = &init_net,
1417 +                                   .oif = t->parms.link,
1418 +                                   .nl_u = { .ip4_u =
1419 +                                             { .daddr = t->parms.iph.daddr,
1420 +                                               .saddr = t->parms.iph.saddr,
1421 +                                               .tos = RT_TOS(t->parms.iph.tos) } },
1422 +                                   .proto = IPPROTO_GRE };
1423 +               struct rtable *rt;
1424 +               if (ip_route_output_key(&rt, &fl))
1425 +                       return -EADDRNOTAVAIL;
1426 +               dev = rt->u.dst.dev;
1427 +               ip_rt_put(rt);
1428 +               if (__in_dev_get_rtnl(dev) == NULL)
1429 +                       return -EADDRNOTAVAIL;
1430 +               t->mlink = dev->ifindex;
1431 +               ip_mc_inc_group(__in_dev_get_rtnl(dev), t->parms.iph.daddr);
1432 +       }
1433 +       return 0;
1434 +}
1435 +
1436 +static int ipgre_close(struct net_device *dev)
1437 +{
1438 +       struct ip_tunnel *t = netdev_priv(dev);
1439 +       if (MULTICAST(t->parms.iph.daddr) && t->mlink) {
1440 +               struct in_device *in_dev = inetdev_by_index(&init_net, t->mlink);
1441 +               if (in_dev) {
1442 +                       ip_mc_dec_group(in_dev, t->parms.iph.daddr);
1443 +                       in_dev_put(in_dev);
1444 +               }
1445 +       }
1446 +       return 0;
1447 +}
1448 +
1449 +#endif
1450 +
1451 +static void ipgre_ip_tunnel_setup(struct net_device *dev)
1452 +{
1453 +       SET_MODULE_OWNER(dev);
1454 +       dev->uninit             = ipgre_tunnel_uninit;
1455 +       dev->destructor         = free_netdev;
1456 +       dev->hard_start_xmit    = ipgre_ip_tunnel_xmit;
1457 +       dev->get_stats          = ipgre_tunnel_get_stats;
1458 +       dev->do_ioctl           = ipgre_tunnel_ioctl;
1459 +       dev->change_mtu         = ipgre_tunnel_change_mtu;
1460 +
1461 +       dev->type               = ARPHRD_IPGRE;
1462 +       dev->hard_header_len    = LL_MAX_HEADER + sizeof(struct iphdr) + 4;
1463 +       dev->mtu                = ETH_DATA_LEN - sizeof(struct iphdr) - 4;
1464 +       dev->flags              = IFF_NOARP;
1465 +       dev->iflink             = 0;
1466 +       dev->addr_len           = 4;
1467 +}
1468 +
1469 +/* Tunnel setup for ipgre_eth */
1470 +static void ipgre_eth_tunnel_setup(struct net_device *dev)
1471 +{
1472 +       SET_MODULE_OWNER(dev);
1473 +       ether_setup(dev);
1474 +
1475 +       dev->uninit             = ipgre_tunnel_uninit;
1476 +       dev->destructor         = free_netdev;
1477 +       dev->hard_start_xmit    = ipgre_eth_tunnel_xmit;
1478 +       dev->get_stats          = ipgre_tunnel_get_stats;
1479 +       dev->do_ioctl           = ipgre_tunnel_ioctl;
1480 +       dev->change_mtu         = ipgre_tunnel_change_mtu;
1481 +
1482 +       dev->hard_header_len    = ETH_HLEN + sizeof(struct iphdr) + 4;
1483 +       dev->tx_queue_len       = 0;
1484 +       random_ether_addr(dev->dev_addr);
1485 +
1486 +#ifdef GRE_DEBUG
1487 +       unsigned char* d = dev->dev_addr;
1488 +       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]);
1489 +#endif 
1490 +
1491 +       dev->iflink             = 0;
1492 +}
1493 +
1494 +
1495 +static int ipgre_tunnel_init(struct net_device *dev)
1496 +{
1497 +       struct net_device *tdev = NULL;
1498 +       struct ip_tunnel *tunnel;
1499 +       struct iphdr *iph;
1500 +       int hlen = LL_MAX_HEADER;
1501 +       int mtu = ETH_DATA_LEN;
1502 +       int addend = sizeof(struct iphdr) + 4;
1503 +
1504 +       tunnel = netdev_priv(dev);
1505 +       iph = &tunnel->parms.iph;
1506 +
1507 +       tunnel->dev = dev;
1508 +       strcpy(tunnel->parms.name, dev->name);
1509 +
1510 +       if (tunnel->parms.proto_type != ETH_P_ETH)
1511 +       {
1512 +               memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
1513 +               memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
1514 +       }
1515 +
1516 +       /* Guess output device to choose reasonable mtu and hard_header_len */
1517 +
1518 +       if (iph->daddr) {
1519 +               struct flowi fl = { .fl_net = &init_net,
1520 +                                   .oif = tunnel->parms.link,
1521 +                                   .nl_u = { .ip4_u =
1522 +                                             { .daddr = iph->daddr,
1523 +                                               .saddr = iph->saddr,
1524 +                                               .tos = RT_TOS(iph->tos) } },
1525 +                                   .proto = IPPROTO_GRE };
1526 +               struct rtable *rt;
1527 +               if (!ip_route_output_key(&rt, &fl)) {
1528 +                       tdev = rt->u.dst.dev;
1529 +                       ip_rt_put(rt);
1530 +               }
1531 +
1532 +               if (tunnel->parms.proto_type == ETH_P_ETH)
1533 +               {
1534 +                   dev->flags |= IFF_BROADCAST;
1535 +               }
1536 +               else
1537 +               {
1538 +                       dev->flags |= IFF_POINTOPOINT;
1539 +               }
1540 +
1541 +#ifdef CONFIG_NET_IPGRE_BROADCAST
1542 +               if (MULTICAST(iph->daddr)) {
1543 +                       if (!iph->saddr)
1544 +                               return -EINVAL;
1545 +                       dev->flags = IFF_BROADCAST;
1546 +                       dev->hard_header = ipgre_header;
1547 +                       dev->open = ipgre_open;
1548 +                       dev->stop = ipgre_close;
1549 +               }
1550 +#endif
1551 +       }
1552 +
1553 +       if (!tdev && tunnel->parms.link)
1554 +               tdev = __dev_get_by_index(&init_net, tunnel->parms.link);
1555 +
1556 +       if (tdev) {
1557 +               hlen = tdev->hard_header_len;
1558 +               mtu = tdev->mtu;
1559 +       }
1560 +       dev->iflink = tunnel->parms.link;
1561 +
1562 +       /* Precalculate GRE options length */
1563 +       if (tunnel->parms.o_flags&(GRE_CSUM|GRE_KEY|GRE_SEQ)) {
1564 +               if (tunnel->parms.o_flags&GRE_CSUM)
1565 +                       addend += 4;
1566 +               if (tunnel->parms.o_flags&GRE_KEY)
1567 +                       addend += 4;
1568 +               if (tunnel->parms.o_flags&GRE_SEQ)
1569 +                       addend += 4;
1570 +       }
1571 +       dev->hard_header_len = hlen + addend;
1572 +       dev->mtu = mtu - addend;
1573 +       tunnel->hlen = addend;
1574 +       return 0;
1575 +}
1576 +
1577 +static int __init ipgre_fb_tunnel_init(struct net_device *dev)
1578 +{
1579 +       struct ip_tunnel *tunnel = netdev_priv(dev);
1580 +       struct iphdr *iph = &tunnel->parms.iph;
1581 +
1582 +       tunnel->dev = dev;
1583 +       strcpy(tunnel->parms.name, dev->name);
1584 +
1585 +       iph->version            = 4;
1586 +       iph->protocol           = IPPROTO_GRE;
1587 +       iph->ihl                = 5;
1588 +       tunnel->hlen            = sizeof(struct iphdr) + 4;
1589 +
1590 +       dev_hold(dev);
1591 +       tunnels_wc[0]           = tunnel;
1592 +       return 0;
1593 +}
1594 +
1595 +
1596 +static struct net_protocol ipgre_protocol = {
1597 +       .handler        =       ipgre_rcv,
1598 +       .err_handler    =       ipgre_err,
1599 +};
1600 +
1601 +
1602 +/*
1603 + *     And now the modules code and kernel interface.
1604 + */
1605 +
1606 +static int __init ipgre_init(void)
1607 +{
1608 +       int err;
1609 +
1610 +       printk(KERN_INFO "GRE over IPv4 tunneling driver\n");
1611 +
1612 +       if (inet_add_protocol(&ipgre_protocol, IPPROTO_GRE) < 0) {
1613 +               printk(KERN_INFO "ipgre init: can't add protocol\n");
1614 +               return -EAGAIN;
1615 +       }
1616 +
1617 +       ipgre_fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "gre0",
1618 +                                          ipgre_ip_tunnel_setup);
1619 +       if (!ipgre_fb_tunnel_dev) {
1620 +               err = -ENOMEM;
1621 +               goto err1;
1622 +       }
1623 +
1624 +       ipgre_fb_tunnel_dev->init = ipgre_fb_tunnel_init;
1625 +
1626 +       if ((err = register_netdev(ipgre_fb_tunnel_dev)))
1627 +               goto err2;
1628 +out:
1629 +       return err;
1630 +err2:
1631 +       free_netdev(ipgre_fb_tunnel_dev);
1632 +err1:
1633 +       inet_del_protocol(&ipgre_protocol, IPPROTO_GRE);
1634 +       goto out;
1635 +}
1636 +
1637 +static void __exit ipgre_destroy_tunnels(void)
1638 +{
1639 +       int prio;
1640 +
1641 +       for (prio = 0; prio < 4; prio++) {
1642 +               int h;
1643 +               for (h = 0; h < HASH_SIZE; h++) {
1644 +                       struct ip_tunnel *t;
1645 +                       while ((t = tunnels[prio][h]) != NULL)
1646 +                               unregister_netdevice(t->dev);
1647 +               }
1648 +       }
1649 +}
1650 +
1651 +static void __exit ipgre_fini(void)
1652 +{
1653 +       if (inet_del_protocol(&ipgre_protocol, IPPROTO_GRE) < 0)
1654 +               printk(KERN_INFO "ipgre close: can't remove protocol\n");
1655 +
1656 +       rtnl_lock();
1657 +       ipgre_destroy_tunnels();
1658 +       rtnl_unlock();
1659 +}
1660 +
1661 +module_init(ipgre_init);
1662 +module_exit(ipgre_fini);
1663 +MODULE_LICENSE("GPL");
1664 diff -Nurb linux-2.6.27-660/include/linux/if_ether.h linux-2.6.27-700/include/linux/if_ether.h
1665 --- linux-2.6.27-660/include/linux/if_ether.h   2008-10-09 18:13:53.000000000 -0400
1666 +++ linux-2.6.27-700/include/linux/if_ether.h   2009-04-10 17:35:46.000000000 -0400
1667 @@ -56,6 +56,7 @@
1668  #define ETH_P_DIAG      0x6005          /* DEC Diagnostics              */
1669  #define ETH_P_CUST      0x6006          /* DEC Customer use             */
1670  #define ETH_P_SCA       0x6007          /* DEC Systems Comms Arch       */
1671 +#define ETH_P_ETH       0x6558          /* Ethernet in Ethernet         */
1672  #define ETH_P_RARP      0x8035         /* Reverse Addr Res packet      */
1673  #define ETH_P_ATALK    0x809B          /* Appletalk DDP                */
1674  #define ETH_P_AARP     0x80F3          /* Appletalk AARP               */
1675 diff -Nurb linux-2.6.27-660/include/linux/if_tunnel.h linux-2.6.27-700/include/linux/if_tunnel.h
1676 --- linux-2.6.27-660/include/linux/if_tunnel.h  2008-10-09 18:13:53.000000000 -0400
1677 +++ linux-2.6.27-700/include/linux/if_tunnel.h  2009-04-10 17:35:46.000000000 -0400
1678 @@ -29,6 +29,7 @@
1679         __be16                  o_flags;
1680         __be32                  i_key;
1681         __be32                  o_key;
1682 +        __be16                  proto_type;   /*Added*/
1683         struct iphdr            iph;
1684  };
1685  
1686 diff -Nurb linux-2.6.27-660/net/ipv4/ip_gre.c linux-2.6.27-700/net/ipv4/ip_gre.c
1687 --- linux-2.6.27-660/net/ipv4/ip_gre.c  2008-10-09 18:13:53.000000000 -0400
1688 +++ linux-2.6.27-700/net/ipv4/ip_gre.c  2009-04-10 18:20:30.000000000 -0400
1689 @@ -25,6 +25,7 @@
1690  #include <linux/init.h>
1691  #include <linux/in6.h>
1692  #include <linux/inetdevice.h>
1693 +#include <linux/etherdevice.h>   /**XXX added XXX */
1694  #include <linux/igmp.h>
1695  #include <linux/netfilter_ipv4.h>
1696  #include <linux/if_ether.h>
1697 @@ -48,6 +49,8 @@
1698  #include <net/ip6_route.h>
1699  #endif
1700  
1701 +//#define GRE_DEBUG 1
1702 +
1703  /*
1704     Problems & solutions
1705     --------------------
1706 @@ -118,7 +121,8 @@
1707   */
1708  
1709  static int ipgre_tunnel_init(struct net_device *dev);
1710 -static void ipgre_tunnel_setup(struct net_device *dev);
1711 +static void ipgre_ip_tunnel_setup(struct net_device *dev);
1712 +static void ipgre_eth_tunnel_setup(struct net_device *dev);
1713  
1714  /* Fallback tunnel: no source, no destination, no key, no options */
1715  
1716 @@ -255,6 +259,7 @@
1717         __be32 remote = parms->iph.daddr;
1718         __be32 local = parms->iph.saddr;
1719         __be32 key = parms->i_key;
1720 +       __be16 proto = parms->proto_type;
1721         struct ip_tunnel *t, **tp, *nt;
1722         struct net_device *dev;
1723         char name[IFNAMSIZ];
1724 @@ -269,12 +274,28 @@
1725         if (!create)
1726                 return NULL;
1727  
1728 +       printk(KERN_CRIT "Adding tunnel %s with key %d\n", parms->name, ntohl(key));
1729 +
1730         if (parms->name[0])
1731                 strlcpy(name, parms->name, IFNAMSIZ);
1732         else
1733                 sprintf(name, "gre%%d");
1734  
1735 -       dev = alloc_netdev(sizeof(*t), name, ipgre_tunnel_setup);
1736 +
1737 +       /* Tunnel creation: check payload type and call appropriate
1738 +        * function */
1739 +       switch (proto)
1740 +       {
1741 +           case ETH_P_IP:
1742 +               dev = alloc_netdev(sizeof(*t), name, ipgre_ip_tunnel_setup);
1743 +               break;
1744 +           case ETH_P_ETH:
1745 +               dev = alloc_netdev(sizeof(*t), name, ipgre_eth_tunnel_setup);
1746 +               break;
1747 +           default:
1748 +               return NULL;
1749 +       }
1750 +
1751         if (!dev)
1752           return NULL;
1753  
1754 @@ -431,6 +452,7 @@
1755         u32    seqno = 0;
1756         struct ip_tunnel *tunnel;
1757         int    offset = 4;
1758 +    __be16 proto;
1759  
1760         if (!pskb_may_pull(skb, 16))
1761                 goto drop_nolock;
1762 @@ -439,6 +461,11 @@
1763         h = skb->data;
1764         flags = *(__be16*)h;
1765  
1766 +#ifdef GRE_DEBUG
1767 +       printk(KERN_DEBUG "gre.c [601] src:%x dst:%x  proto:%d %p", iph->saddr, iph->daddr, iph->protocol, skb->data);
1768 +#endif 
1769 +       proto = ntohs(*(__be16*)(h+2)); /* XXX added XXX */
1770 +       
1771         if (flags&(GRE_CSUM|GRE_KEY|GRE_ROUTING|GRE_SEQ|GRE_VERSION)) {
1772                 /* - Version must be 0.
1773                    - We do not support routing headers.
1774 @@ -493,7 +520,29 @@
1775                 __pskb_pull(skb, offset);
1776                 skb_reset_network_header(skb);
1777                 skb_postpull_rcsum(skb, skb_transport_header(skb), offset);
1778 +               if(proto == ETH_P_ETH)
1779 +                 {
1780 + #ifdef GRE_DEBUG
1781 +                   unsigned char* tmp_hdr = skb->data;
1782 +                   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);
1783 + #endif                    
1784 +                   skb->protocol = eth_type_trans(skb, tunnel->dev);
1785
1786 +                   /* XXX added these lines to make arp work? XXX */
1787 +                   /*skb->mac.raw = skb->data;*/
1788 +                   skb->network_header = skb->network_header + ETH_HLEN;
1789 +                   /* XXX added these lines to make arp work? XXX */
1790
1791 + #ifdef GRE_DEBUG
1792 +                   tmp_hdr = skb->data;
1793 +                   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);
1794 +                   printk(KERN_ALERT "gre.c [671] received ethernet on gre %x\n",skb->protocol); 
1795 + #endif
1796 +                   memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
1797 +                 }
1798 +               else
1799                 skb->pkt_type = PACKET_HOST;
1800 +
1801  #ifdef CONFIG_NET_IPGRE_BROADCAST
1802                 if (ipv4_is_multicast(iph->daddr)) {
1803                         /* Looped back packet, drop it! */
1804 @@ -539,7 +588,7 @@
1805         return(0);
1806  }
1807  
1808 -static int ipgre_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
1809 +static int ipgre_ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
1810  {
1811         struct ip_tunnel *tunnel = netdev_priv(dev);
1812         struct net_device_stats *stats = &tunnel->dev->stats;
1813 @@ -799,9 +848,17 @@
1814                         tdev = rt->u.dst.dev;
1815                         ip_rt_put(rt);
1816                 }
1817 +               if (tunnel->parms.proto_type == ETH_P_ETH)
1818 +               {
1819 +                   dev->flags |= IFF_BROADCAST;
1820 +               }
1821 +               else
1822 +               {
1823                 dev->flags |= IFF_POINTOPOINT;
1824         }
1825  
1826 +       }
1827 +
1828         if (!tdev && tunnel->parms.link)
1829                 tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
1830  
1831 @@ -822,10 +879,234 @@
1832         }
1833         dev->hard_header_len = hlen + addend;
1834         dev->mtu = mtu - addend;
1835 +       if (tunnel->parms.proto_type == ETH_P_ETH)
1836 +               dev->mtu -= ETH_HLEN;
1837         tunnel->hlen = addend;
1838  
1839  }
1840  
1841 +static int ipgre_eth_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
1842 +{
1843 +       struct ip_tunnel *tunnel = netdev_priv(dev);
1844 +       struct net_device_stats *stats = &tunnel->stat;
1845 +       struct iphdr *old_iph = ip_hdr(skb);
1846 +       struct iphdr *tiph = &tunnel->parms.iph;
1847 +       u8     tos;
1848 +       __be16 df;
1849 +       struct rtable *rt;              /* Route to the other host */
1850 +       struct net_device *tdev;        /* Device to other host */
1851 +       int    gre_hlen = tunnel->hlen; /* XXX changed XXX*/
1852 +       //struct etheriphdr  *ethiph;
1853 +       struct iphdr  *iph;             /* Our new IP header */
1854 +       int    max_headroom;            /* The extra header space needed */
1855 +       int    mtu;
1856 +
1857 +#ifdef GRE_DEBUG
1858 +       printk(KERN_ALERT "gre.c:972 Starting xmit\n");
1859 +#endif
1860 +
1861 +       if (tunnel->recursion++) {
1862 +               stats->collisions++;
1863 +               goto tx_error;
1864 +       }
1865 +
1866 +       /* Need valid non-multicast daddr.  */
1867 +       if (tiph->daddr == 0 || MULTICAST(tiph->daddr))
1868 +               goto tx_error;
1869 +
1870 +       tos = tiph->tos;
1871 +       if (tos&1) {
1872 +               if (skb->protocol == htons(ETH_P_IP))
1873 +                       tos = old_iph->tos;
1874 +               tos &= ~1;
1875 +       }
1876 +#ifdef GRE_DEBUG
1877 +       printk(KERN_ALERT "gre.c:991 Passed tos assignment.\n");
1878 +#endif
1879 +
1880 +
1881 +       {
1882 +               struct flowi fl = { .fl_net = &init_net,
1883 +                                   .oif = tunnel->parms.link,
1884 +                                   .nl_u = { .ip4_u =
1885 +                                             { .daddr = tiph->daddr,
1886 +                                               .saddr = tiph->saddr,
1887 +                                               .tos = RT_TOS(tos) } },
1888 +                                   .proto = IPPROTO_GRE };
1889 +               if (ip_route_output_key(&rt, &fl)) {
1890 +                       stats->tx_carrier_errors++;
1891 +                       goto tx_error_icmp;
1892 +               }
1893 +       }
1894 +       tdev = rt->u.dst.dev;
1895 +#ifdef GRE_DEBUG
1896 +       printk(KERN_ALERT "gre.c:1006 Passed the route retrieval\n");
1897 +#endif
1898 +       if (tdev == dev) {
1899 +               ip_rt_put(rt);
1900 +               stats->collisions++;
1901 +               goto tx_error;
1902 +       }
1903 +#ifdef GRE_DEBUG
1904 +       printk(KERN_ALERT "gre.c:1018 Passed tdev collision check.\n");
1905 +#endif
1906 +
1907 +       /* Check MTU stuff if kernel panic */
1908 +       df = tiph->frag_off;
1909 +       if (df)
1910 +               mtu = dst_mtu(&rt->u.dst) - tunnel->hlen;
1911 +       else
1912 +               mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu;
1913 +/*
1914 +       if (skb->dst)
1915 +               skb->dst->ops->update_pmtu(skb->dst, mtu);
1916 +        XXX */
1917 +#ifdef GRE_DEBUG
1918 +       printk(KERN_ALERT "gre.c:1032 Passed the pmtu setting.\n");
1919 +#endif
1920 +
1921 +       if (skb->protocol == htons(ETH_P_IP)) {
1922 +               df |= (old_iph->frag_off&htons(IP_DF));
1923 +
1924 +               if ((old_iph->frag_off & htons(IP_DF)) &&
1925 +                   mtu < ntohs(old_iph->tot_len)) {
1926 +                       icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
1927 +                       ip_rt_put(rt);
1928 +                       goto tx_error;
1929 +               }
1930 +       }
1931 +#ifdef CONFIG_IPV6
1932 +       else if (skb->protocol == htons(ETH_P_IPV6)) {
1933 +               struct rt6_info *rt6 = (struct rt6_info*)skb->dst;
1934 +
1935 +               if (rt6 && mtu < dst_mtu(skb->dst) && mtu >= IPV6_MIN_MTU) {
1936 +                       if (tiph->daddr || rt6->rt6i_dst.plen == 128) {
1937 +                               rt6->rt6i_flags |= RTF_MODIFIED;
1938 +                               skb->dst->metrics[RTAX_MTU-1] = mtu;
1939 +                       }
1940 +               }
1941 +
1942 +               /* @@@ Is this correct?  */
1943 +               if (mtu >= IPV6_MIN_MTU && mtu < skb->len - tunnel->hlen + gre_hlen) {
1944 +                       icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
1945 +                       ip_rt_put(rt);
1946 +                       goto tx_error;
1947 +               }
1948 +       }
1949 +#endif
1950 +#ifdef GRE_DEBUG
1951 +       printk(KERN_ALERT "gre.c:1065 Passed the fragmentation check.\n");
1952 +#endif
1953 +
1954 +       if (tunnel->err_count > 0) {
1955 +               if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
1956 +                       tunnel->err_count--;
1957 +                       dst_link_failure(skb);
1958 +               } else
1959 +                       tunnel->err_count = 0;
1960 +       }
1961 +
1962 +       max_headroom = LL_RESERVED_SPACE(tdev) + gre_hlen;
1963 +
1964 +       if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) {
1965 +               struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
1966 +               if (!new_skb) {
1967 +                       ip_rt_put(rt);
1968 +                       stats->tx_dropped++;
1969 +                       dev_kfree_skb(skb);
1970 +                       tunnel->recursion--;
1971 +                       return 0;
1972 +               }
1973 +               if (skb->sk)
1974 +                       skb_set_owner_w(new_skb, skb->sk);
1975 +               dev_kfree_skb(skb);
1976 +               skb = new_skb;
1977 +               old_iph = ip_hdr(skb);
1978 +       }
1979 +#ifdef GRE_DEBUG
1980 +       printk(KERN_ALERT "gre.c:1094 Passed the headroom calculation\n");
1981 +#endif
1982 +
1983 +       skb->transport_header = skb->data;
1984 +       skb_push(skb, gre_hlen);
1985 +       skb_reset_network_header(skb);
1986 +       memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1987 +       dst_release(skb->dst);
1988 +       skb->dst = &rt->u.dst;
1989 +
1990 +       /*
1991 +        *      Push down and install the etherip header.
1992 +        */
1993 +
1994 +       iph                     =       ip_hdr(skb);
1995 +       iph->version            =       4;
1996 +       iph->ihl                =       sizeof(struct iphdr) >> 2;
1997 +       iph->frag_off           =       df;
1998 +       iph->protocol           =       IPPROTO_GRE;
1999 +       iph->tos                =       ipgre_ecn_encapsulate(tos, old_iph, skb);
2000 +       iph->daddr              =       rt->rt_dst;
2001 +       iph->saddr              =       rt->rt_src;
2002 +
2003 +/*     ethiph->version         =       htons(ETHERIP_VERSION); */
2004 +#ifdef GRE_DEBUG
2005 +       printk(KERN_ALERT "gre.c:1121 Passed outer IP header construction.\n");
2006 +#endif
2007 +
2008 +       if ((iph->ttl = tiph->ttl) == 0) {
2009 +               if (skb->protocol == htons(ETH_P_IP))
2010 +                       iph->ttl = old_iph->ttl;
2011 +#ifdef CONFIG_IPV6
2012 +               else if (skb->protocol == htons(ETH_P_IPV6))
2013 +                       iph->ttl = ((struct ipv6hdr*)old_iph)->hop_limit;
2014 +#endif
2015 +               else
2016 +                       iph->ttl = dst_metric(&rt->u.dst, RTAX_HOPLIMIT);
2017 +       }
2018 +#ifdef GRE_DEBUG
2019 +       printk(KERN_ALERT "gre.c:1006 Passed the TTL check.\n");
2020 +#endif
2021 +
2022 +       ((__be16*)(iph+1))[0] = tunnel->parms.o_flags;
2023 +       ((__be16*)(iph+1))[1] = htons(tunnel->parms.proto_type);
2024 +
2025 +       if (tunnel->parms.o_flags&(GRE_KEY|GRE_CSUM|GRE_SEQ)) {
2026 +               __be32 *ptr = (__be32*)(((u8*)iph) + tunnel->hlen - 4);
2027 +
2028 +               if (tunnel->parms.o_flags&GRE_SEQ) {
2029 +                       ++tunnel->o_seqno;
2030 +                       *ptr = htonl(tunnel->o_seqno);
2031 +                       ptr--;
2032 +               }
2033 +               if (tunnel->parms.o_flags&GRE_KEY) {
2034 +                       *ptr = tunnel->parms.o_key;
2035 +                       ptr--;
2036 +               }
2037 +               if (tunnel->parms.o_flags&GRE_CSUM) {
2038 +                       *ptr = 0;
2039 +                       *(__sum16*)ptr = ip_compute_csum((void*)(iph+1), skb->len - sizeof(struct iphdr));
2040 +               }
2041 +       }
2042 +#ifdef GRE_DEBUG
2043 +       printk(KERN_ALERT "gre.c:1006 Passed the tunnel transmit.\n");
2044 +#endif
2045 +
2046 +       nf_reset(skb);
2047 +
2048 +       IPTUNNEL_XMIT();
2049 +       tunnel->recursion--;
2050 +       return 0;
2051 +
2052 +tx_error_icmp:
2053 +       dst_link_failure(skb);
2054 +
2055 +tx_error:
2056 +       stats->tx_errors++;
2057 +       dev_kfree_skb(skb);
2058 +       tunnel->recursion--;
2059 +       return 0;
2060 +}
2061 +
2062 +
2063  static int
2064  ipgre_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
2065  {
2066 @@ -876,6 +1157,7 @@
2067                         p.o_key = 0;
2068  
2069                 t = ipgre_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
2070 +               if (t) printk(KERN_ALERT "1174 GRE: proto %s %x\n", p.name, p.proto_type);
2071  
2072                 if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
2073                         if (t != NULL) {
2074 @@ -893,6 +1175,12 @@
2075                                 else if (p.iph.daddr)
2076                                         nflags = IFF_POINTOPOINT;
2077  
2078 +                               /* XXX:Set back IFF_BROADCAST if
2079 +                                * transporting ethernet */
2080 +                               printk(KERN_ALERT "1193 GRE: proto %s %d\n", p.name, p.proto_type);
2081 +                               if (p.proto_type == ETH_P_ETH)
2082 +                                       nflags = IFF_BROADCAST;
2083 +
2084                                 if ((dev->flags^nflags)&(IFF_POINTOPOINT|IFF_BROADCAST)) {
2085                                         err = -EINVAL;
2086                                         break;
2087 @@ -902,8 +1190,13 @@
2088                                 t->parms.iph.daddr = p.iph.daddr;
2089                                 t->parms.i_key = p.i_key;
2090                                 t->parms.o_key = p.o_key;
2091 +                               /* XXX:Copy in the protocol field */
2092 +                               t->parms.proto_type = p.proto_type;
2093 +                               if (t->parms.proto_type != ETH_P_ETH) {
2094                                 memcpy(dev->dev_addr, &p.iph.saddr, 4);
2095                                 memcpy(dev->broadcast, &p.iph.daddr, 4);
2096 +                               }
2097 +
2098                                 ipgre_tunnel_link(ign, t);
2099                                 netdev_state_change(dev);
2100                         }
2101 @@ -1076,13 +1369,13 @@
2102  
2103  #endif
2104  
2105 -static void ipgre_tunnel_setup(struct net_device *dev)
2106 +static void ipgre_ip_tunnel_setup(struct net_device *dev)
2107  {
2108         dev->uninit             = ipgre_tunnel_uninit;
2109         dev->destructor         = free_netdev;
2110 -       dev->hard_start_xmit    = ipgre_tunnel_xmit;
2111         dev->do_ioctl           = ipgre_tunnel_ioctl;
2112         dev->change_mtu         = ipgre_tunnel_change_mtu;
2113 +       dev->hard_start_xmit    = ipgre_ip_tunnel_xmit;
2114  
2115         dev->type               = ARPHRD_IPGRE;
2116         dev->hard_header_len    = LL_MAX_HEADER + sizeof(struct iphdr) + 4;
2117 @@ -1093,6 +1386,35 @@
2118         dev->features           |= NETIF_F_NETNS_LOCAL;
2119  }
2120  
2121 +/* Tunnel setup for ipgre_eth */
2122 +static void ipgre_eth_tunnel_setup(struct net_device *dev)
2123 +{
2124 +       SET_MODULE_OWNER(dev);
2125 +
2126 +       // Set default values for Ethernet device
2127 +       ether_setup(dev);
2128 +
2129 +       dev->uninit             = ipgre_tunnel_uninit;
2130 +       dev->destructor         = free_netdev;
2131 +       dev->hard_start_xmit    = ipgre_eth_tunnel_xmit;
2132 +       dev->get_stats          = ipgre_tunnel_get_stats;
2133 +       dev->do_ioctl           = ipgre_tunnel_ioctl;
2134 +       dev->change_mtu         = ipgre_tunnel_change_mtu;
2135 +
2136 +       dev->hard_header_len    = LL_MAX_HEADER + ETH_HLEN + sizeof(struct iphdr) + 4;
2137 +       dev->mtu                = ETH_DATA_LEN - ETH_HLEN - sizeof(struct iphdr) - 4;
2138 +       dev->tx_queue_len       = 0;
2139 +       dev->iflink             = 0;
2140 +
2141 +       random_ether_addr(dev->dev_addr);
2142 +
2143 +#ifdef GRE_DEBUG
2144 +       { unsigned char* d = dev->dev_addr;
2145 +       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]); }
2146 +#endif 
2147 +}
2148 +
2149 +
2150  static int ipgre_tunnel_init(struct net_device *dev)
2151  {
2152         struct ip_tunnel *tunnel;
2153 @@ -1104,8 +1426,10 @@
2154         tunnel->dev = dev;
2155         strcpy(tunnel->parms.name, dev->name);
2156  
2157 +       if (tunnel->parms.proto_type != ETH_P_ETH) {
2158         memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
2159         memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
2160 +       } 
2161  
2162         ipgre_tunnel_bind_dev(dev);
2163  
2164 @@ -1181,7 +1505,7 @@
2165                 goto err_assign;
2166  
2167         ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "gre0",
2168 -                                          ipgre_tunnel_setup);
2169 +                                          ipgre_ip_tunnel_setup);
2170         if (!ign->fb_tunnel_dev) {
2171                 err = -ENOMEM;
2172                 goto err_alloc_dev;
2173 diff -Nurb linux-2.6.27-660/rej linux-2.6.27-700/rej
2174 --- linux-2.6.27-660/rej        1969-12-31 19:00:00.000000000 -0500
2175 +++ linux-2.6.27-700/rej        2009-04-10 17:50:33.000000000 -0400
2176 @@ -0,0 +1,2 @@
2177 +./net/ipv4/ip_gre.c.rej
2178 +./drivers/net/Makefile.rej