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