Fix device type and address length
[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,1634 @@
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 +/*     XXX skb->h.raw = skb->nh.raw; XXX */
1098 +//     skb->h.raw = skb->mac.raw;
1099 +       skb->transport_header = skb->mac_header; // Added by valas
1100 +       
1101 +       skb_push(skb, gre_hlen);
1102 +       memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1103 +       dst_release(skb->dst);
1104 +       skb->dst = &rt->u.dst;
1105 +
1106 +       /*
1107 +        *      Push down and install the etherip header.
1108 +        */
1109 +
1110 +       iph                     =       ip_hdr(skb);
1111 +       iph->version            =       4;
1112 +       iph->ihl                =       sizeof(struct iphdr) >> 2;
1113 +       iph->frag_off           =       df;
1114 +       iph->protocol           =       IPPROTO_GRE;
1115 +       iph->tos                =       ipgre_ecn_encapsulate(tos, old_iph, skb);
1116 +       iph->daddr              =       rt->rt_dst;
1117 +       iph->saddr              =       rt->rt_src;
1118 +
1119 +/*     ethiph->version         =       htons(ETHERIP_VERSION); */
1120 +#ifdef GRE_DEBUG
1121 +       printk(KERN_ALERT "gre.c:1121 Passed outer IP header construction.\n");
1122 +#endif
1123 +
1124 +       if ((iph->ttl = tiph->ttl) == 0) {
1125 +               if (skb->protocol == htons(ETH_P_IP))
1126 +                       iph->ttl = old_iph->ttl;
1127 +#ifdef CONFIG_IPV6
1128 +               else if (skb->protocol == htons(ETH_P_IPV6))
1129 +                       iph->ttl = ((struct ipv6hdr*)old_iph)->hop_limit;
1130 +#endif
1131 +               else
1132 +                       iph->ttl = dst_metric(&rt->u.dst, RTAX_HOPLIMIT);
1133 +       }
1134 +#ifdef GRE_DEBUG
1135 +       printk(KERN_ALERT "gre.c:1006 Passed the TTL check.\n");
1136 +#endif
1137 +
1138 +       ((__be16*)(iph+1))[0] = tunnel->parms.o_flags;
1139 +       ((__be16*)(iph+1))[1] = htons(tunnel->parms.proto_type);
1140 +
1141 +       if (tunnel->parms.o_flags&(GRE_KEY|GRE_CSUM|GRE_SEQ)) {
1142 +               __be32 *ptr = (__be32*)(((u8*)iph) + tunnel->hlen - 4);
1143 +
1144 +               if (tunnel->parms.o_flags&GRE_SEQ) {
1145 +                       ++tunnel->o_seqno;
1146 +                       *ptr = htonl(tunnel->o_seqno);
1147 +                       ptr--;
1148 +               }
1149 +               if (tunnel->parms.o_flags&GRE_KEY) {
1150 +                       *ptr = tunnel->parms.o_key;
1151 +                       ptr--;
1152 +               }
1153 +               if (tunnel->parms.o_flags&GRE_CSUM) {
1154 +                       *ptr = 0;
1155 +                       *(__sum16*)ptr = ip_compute_csum((void*)(iph+1), skb->len - sizeof(struct iphdr));
1156 +               }
1157 +       }
1158 +#ifdef GRE_DEBUG
1159 +       printk(KERN_ALERT "gre.c:1006 Passed the tunnel transmit.\n");
1160 +#endif
1161 +
1162 +       nf_reset(skb);
1163 +
1164 +       IPTUNNEL_XMIT();
1165 +       tunnel->recursion--;
1166 +       return 0;
1167 +
1168 +tx_error_icmp:
1169 +       dst_link_failure(skb);
1170 +
1171 +tx_error:
1172 +       stats->tx_errors++;
1173 +       dev_kfree_skb(skb);
1174 +       tunnel->recursion--;
1175 +       return 0;
1176 +}
1177 +
1178 +
1179 +static int
1180 +ipgre_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
1181 +{
1182 +       int err = 0;
1183 +       struct ip_tunnel_parm p;
1184 +       struct ip_tunnel *t;
1185 +
1186 +        printk(KERN_ALERT "1174 GRE: entering gre ioctl. command is: %d\n", cmd);
1187 +
1188 +       switch (cmd) {
1189 +       case SIOCGETTUNNEL:
1190 +               t = NULL;
1191 +               if (dev == ipgre_fb_tunnel_dev) {
1192 +                       if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
1193 +                               err = -EFAULT;
1194 +                               break;
1195 +                       }
1196 +                       t = ipgre_tunnel_locate(&p, 0);
1197 +               }
1198 +               if (t == NULL)
1199 +                       t = netdev_priv(dev);
1200 +               memcpy(&p, &t->parms, sizeof(p));
1201 +               if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1202 +                       err = -EFAULT;
1203 +               break;
1204 +
1205 +       case SIOCADDTUNNEL:
1206 +       case SIOCCHGTUNNEL:
1207 +               err = -EPERM;
1208 +               if (!capable(CAP_NET_ADMIN))
1209 +                       goto done;
1210 +
1211 +               err = -EFAULT;
1212 +               if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1213 +                       goto done;
1214 +
1215 +               err = -EINVAL;
1216 +               if (p.iph.version != 4 || p.iph.protocol != IPPROTO_GRE ||
1217 +                   p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)) ||
1218 +                   ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING)))
1219 +                       goto done;
1220 +               if (p.iph.ttl)
1221 +                       p.iph.frag_off |= htons(IP_DF);
1222 +
1223 +               if (!(p.i_flags&GRE_KEY))
1224 +                       p.i_key = 0;
1225 +               if (!(p.o_flags&GRE_KEY))
1226 +                       p.o_key = 0;
1227 +
1228 +               t = ipgre_tunnel_locate(&p, cmd == SIOCADDTUNNEL);
1229 +               if (t) printk(KERN_ALERT "1174 GRE: proto %s %d\n", p.name, p.proto_type);
1230 +               if (dev != ipgre_fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
1231 +                       if (t != NULL) {
1232 +                               if (t->dev != dev) {
1233 +                                       err = -EEXIST;
1234 +                                       break;
1235 +                               }
1236 +                       } else {
1237 +                               unsigned nflags=0;
1238 +
1239 +                               t = netdev_priv(dev);
1240 +
1241 +                               if (MULTICAST(p.iph.daddr))
1242 +                                       nflags = IFF_BROADCAST;
1243 +                               else if (p.iph.daddr)
1244 +                                       nflags = IFF_POINTOPOINT;
1245 +                               
1246 +                               /* XXX:Set back IFF_BROADCAST if
1247 +                                * transporting ethernet */
1248 +                               printk(KERN_ALERT "1193 GRE: proto %s %d\n", p.name, p.proto_type);
1249 +                               if (p.proto_type == ETH_P_ETH)
1250 +                                       nflags = IFF_BROADCAST;
1251 +
1252 +                               if ((dev->flags^nflags)&(IFF_POINTOPOINT|IFF_BROADCAST)) {
1253 +                                       err = -EINVAL;
1254 +                                       break;
1255 +                               }
1256 +                               ipgre_tunnel_unlink(t);
1257 +                               t->parms.iph.saddr = p.iph.saddr;
1258 +                               t->parms.iph.daddr = p.iph.daddr;
1259 +                               t->parms.i_key = p.i_key;
1260 +                               t->parms.o_key = p.o_key;
1261 +                               /* XXX:Copy in the protocol field */
1262 +                               t->parms.proto_type = p.proto_type;
1263 +                               if (t->parms.proto_type != ETH_P_ETH)
1264 +                               {
1265 +                                       memcpy(dev->dev_addr, &p.iph.saddr, 4);
1266 +                                       memcpy(dev->broadcast, &p.iph.daddr, 4);
1267 +                               }
1268 +                               ipgre_tunnel_link(t);
1269 +                               netdev_state_change(dev);
1270 +                       }
1271 +               }
1272 +
1273 +               if (t) {
1274 +                       err = 0;
1275 +                       if (cmd == SIOCCHGTUNNEL) {
1276 +                               t->parms.iph.ttl = p.iph.ttl;
1277 +                               t->parms.iph.tos = p.iph.tos;
1278 +                               t->parms.iph.frag_off = p.iph.frag_off;
1279 +                       }
1280 +                       if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
1281 +                               err = -EFAULT;
1282 +               } else
1283 +                       err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
1284 +               break;
1285 +
1286 +       case SIOCDELTUNNEL:
1287 +               err = -EPERM;
1288 +               if (!capable(CAP_NET_ADMIN))
1289 +                       goto done;
1290 +
1291 +               if (dev == ipgre_fb_tunnel_dev) {
1292 +                       err = -EFAULT;
1293 +                       if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1294 +                               goto done;
1295 +                       err = -ENOENT;
1296 +                       if ((t = ipgre_tunnel_locate(&p, 0)) == NULL)
1297 +                               goto done;
1298 +                       err = -EPERM;
1299 +                       if (t == netdev_priv(ipgre_fb_tunnel_dev))
1300 +                               goto done;
1301 +                       dev = t->dev;
1302 +               }
1303 +               unregister_netdevice(dev); // added by Valas
1304 +               break;
1305 +
1306 +       default:
1307 +               err = -EINVAL;
1308 +       }
1309 +
1310 +done:
1311 +       return err;
1312 +}
1313 +
1314 +static struct net_device_stats *ipgre_tunnel_get_stats(struct net_device *dev)
1315 +{
1316 +       return &(((struct ip_tunnel*)netdev_priv(dev))->stat);
1317 +}
1318 +
1319 +static int ipgre_tunnel_change_mtu(struct net_device *dev, int new_mtu)
1320 +{
1321 +       struct ip_tunnel *tunnel = netdev_priv(dev);
1322 +       if (new_mtu < 68 || new_mtu > 0xFFF8 - tunnel->hlen)
1323 +               return -EINVAL;
1324 +       dev->mtu = new_mtu;
1325 +       return 0;
1326 +}
1327 +
1328 +#ifdef CONFIG_NET_IPGRE_BROADCAST
1329 +/* Nice toy. Unfortunately, useless in real life :-)
1330 +   It allows to construct virtual multiprotocol broadcast "LAN"
1331 +   over the Internet, provided multicast routing is tuned.
1332 +
1333 +
1334 +   I have no idea was this bicycle invented before me,
1335 +   so that I had to set ARPHRD_IPGRE to a random value.
1336 +   I have an impression, that Cisco could make something similar,
1337 +   but this feature is apparently missing in IOS<=11.2(8).
1338 +
1339 +   I set up 10.66.66/24 and fec0:6666:6666::0/96 as virtual networks
1340 +   with broadcast 224.66.66.66. If you have access to mbone, play with me :-)
1341 +
1342 +   ping -t 255 224.66.66.66
1343 +
1344 +   If nobody answers, mbone does not work.
1345 +
1346 +   ip tunnel add Universe mode gre remote 224.66.66.66 local <Your_real_addr> ttl 255
1347 +   ip addr add 10.66.66.<somewhat>/24 dev Universe
1348 +   ifconfig Universe up
1349 +   ifconfig Universe add fe80::<Your_real_addr>/10
1350 +   ifconfig Universe add fec0:6666:6666::<Your_real_addr>/96
1351 +   ftp 10.66.66.66
1352 +   ...
1353 +   ftp fec0:6666:6666::193.233.7.65
1354 +   ...
1355 +
1356 + */
1357 +
1358 +static int ipgre_header(struct sk_buff *skb, struct net_device *dev, unsigned short type,
1359 +                       void *daddr, void *saddr, unsigned len)
1360 +{
1361 +       struct ip_tunnel *t = netdev_priv(dev);
1362 +       struct iphdr *iph = (struct iphdr *)skb_push(skb, t->hlen);
1363 +       __be16 *p = (__be16*)(iph+1);
1364 +
1365 +       memcpy(iph, &t->parms.iph, sizeof(struct iphdr));
1366 +       p[0]            = t->parms.o_flags;
1367 +       p[1]            = htons(type);
1368 +
1369 +       /*
1370 +        *      Set the source hardware address.
1371 +        */
1372 +
1373 +       if (saddr)
1374 +               memcpy(&iph->saddr, saddr, 4);
1375 +
1376 +       if (daddr) {
1377 +               memcpy(&iph->daddr, daddr, 4);
1378 +               return t->hlen;
1379 +       }
1380 +       if (iph->daddr && !MULTICAST(iph->daddr))
1381 +               return t->hlen;
1382 +
1383 +       return -t->hlen;
1384 +}
1385 +
1386 +static int ipgre_open(struct net_device *dev)
1387 +{
1388 +       struct ip_tunnel *t = netdev_priv(dev);
1389 +
1390 +       if (MULTICAST(t->parms.iph.daddr)) {
1391 +               struct flowi fl = { .fl_net = &init_net,
1392 +                                   .oif = t->parms.link,
1393 +                                   .nl_u = { .ip4_u =
1394 +                                             { .daddr = t->parms.iph.daddr,
1395 +                                               .saddr = t->parms.iph.saddr,
1396 +                                               .tos = RT_TOS(t->parms.iph.tos) } },
1397 +                                   .proto = IPPROTO_GRE };
1398 +               struct rtable *rt;
1399 +               if (ip_route_output_key(&rt, &fl))
1400 +                       return -EADDRNOTAVAIL;
1401 +               dev = rt->u.dst.dev;
1402 +               ip_rt_put(rt);
1403 +               if (__in_dev_get_rtnl(dev) == NULL)
1404 +                       return -EADDRNOTAVAIL;
1405 +               t->mlink = dev->ifindex;
1406 +               ip_mc_inc_group(__in_dev_get_rtnl(dev), t->parms.iph.daddr);
1407 +       }
1408 +       return 0;
1409 +}
1410 +
1411 +static int ipgre_close(struct net_device *dev)
1412 +{
1413 +       struct ip_tunnel *t = netdev_priv(dev);
1414 +       if (MULTICAST(t->parms.iph.daddr) && t->mlink) {
1415 +               struct in_device *in_dev = inetdev_by_index(&init_net, t->mlink);
1416 +               if (in_dev) {
1417 +                       ip_mc_dec_group(in_dev, t->parms.iph.daddr);
1418 +                       in_dev_put(in_dev);
1419 +               }
1420 +       }
1421 +       return 0;
1422 +}
1423 +
1424 +#endif
1425 +
1426 +static void ipgre_ip_tunnel_setup(struct net_device *dev)
1427 +{
1428 +       SET_MODULE_OWNER(dev);
1429 +       dev->uninit             = ipgre_tunnel_uninit;
1430 +       dev->destructor         = free_netdev;
1431 +       dev->hard_start_xmit    = ipgre_ip_tunnel_xmit;
1432 +       dev->get_stats          = ipgre_tunnel_get_stats;
1433 +       dev->do_ioctl           = ipgre_tunnel_ioctl;
1434 +       dev->change_mtu         = ipgre_tunnel_change_mtu;
1435 +
1436 +       dev->type               = ARPHRD_IPGRE;
1437 +       dev->hard_header_len    = LL_MAX_HEADER + sizeof(struct iphdr) + 4;
1438 +       dev->mtu                = ETH_DATA_LEN - sizeof(struct iphdr) - 4;
1439 +       dev->flags              = IFF_NOARP;
1440 +       dev->iflink             = 0;
1441 +       dev->addr_len           = 4;
1442 +}
1443 +
1444 +/* Tunnel setup for ipgre_eth */
1445 +static void ipgre_eth_tunnel_setup(struct net_device *dev)
1446 +{
1447 +       SET_MODULE_OWNER(dev);
1448 +       ether_setup(dev);
1449 +
1450 +       dev->uninit             = ipgre_tunnel_uninit;
1451 +       dev->destructor         = free_netdev;
1452 +       dev->hard_start_xmit    = ipgre_eth_tunnel_xmit;
1453 +       dev->get_stats          = ipgre_tunnel_get_stats;
1454 +       dev->do_ioctl           = ipgre_tunnel_ioctl;
1455 +       dev->change_mtu         = ipgre_tunnel_change_mtu;
1456 +
1457 +       dev->hard_header_len    = ETH_HLEN + sizeof(struct iphdr) + 4;
1458 +       dev->tx_queue_len       = 0;
1459 +       random_ether_addr(dev->dev_addr);
1460 +
1461 +#ifdef GRE_DEBUG
1462 +       unsigned char* d = dev->dev_addr;
1463 +       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]);
1464 +#endif 
1465 +
1466 +       dev->iflink             = 0;
1467 +}
1468 +
1469 +
1470 +static int ipgre_tunnel_init(struct net_device *dev)
1471 +{
1472 +       struct net_device *tdev = NULL;
1473 +       struct ip_tunnel *tunnel;
1474 +       struct iphdr *iph;
1475 +       int hlen = LL_MAX_HEADER;
1476 +       int mtu = ETH_DATA_LEN;
1477 +       int addend = sizeof(struct iphdr) + 4;
1478 +
1479 +       tunnel = netdev_priv(dev);
1480 +       iph = &tunnel->parms.iph;
1481 +
1482 +       tunnel->dev = dev;
1483 +       strcpy(tunnel->parms.name, dev->name);
1484 +
1485 +       if (tunnel->parms.proto_type != ETH_P_ETH)
1486 +       {
1487 +               memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
1488 +               memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
1489 +       }
1490 +
1491 +       /* Guess output device to choose reasonable mtu and hard_header_len */
1492 +
1493 +       if (iph->daddr) {
1494 +               struct flowi fl = { .fl_net = &init_net,
1495 +                                   .oif = tunnel->parms.link,
1496 +                                   .nl_u = { .ip4_u =
1497 +                                             { .daddr = iph->daddr,
1498 +                                               .saddr = iph->saddr,
1499 +                                               .tos = RT_TOS(iph->tos) } },
1500 +                                   .proto = IPPROTO_GRE };
1501 +               struct rtable *rt;
1502 +               if (!ip_route_output_key(&rt, &fl)) {
1503 +                       tdev = rt->u.dst.dev;
1504 +                       ip_rt_put(rt);
1505 +               }
1506 +
1507 +               if (tunnel->parms.proto_type == ETH_P_ETH)
1508 +               {
1509 +                   dev->flags |= IFF_BROADCAST;
1510 +               }
1511 +               else
1512 +               {
1513 +                       dev->flags |= IFF_POINTOPOINT;
1514 +               }
1515 +
1516 +#ifdef CONFIG_NET_IPGRE_BROADCAST
1517 +               if (MULTICAST(iph->daddr)) {
1518 +                       if (!iph->saddr)
1519 +                               return -EINVAL;
1520 +                       dev->flags = IFF_BROADCAST;
1521 +                       dev->hard_header = ipgre_header;
1522 +                       dev->open = ipgre_open;
1523 +                       dev->stop = ipgre_close;
1524 +               }
1525 +#endif
1526 +       }
1527 +
1528 +       if (!tdev && tunnel->parms.link)
1529 +               tdev = __dev_get_by_index(&init_net, tunnel->parms.link);
1530 +
1531 +       if (tdev) {
1532 +               hlen = tdev->hard_header_len;
1533 +               mtu = tdev->mtu;
1534 +       }
1535 +       dev->iflink = tunnel->parms.link;
1536 +
1537 +       /* Precalculate GRE options length */
1538 +       if (tunnel->parms.o_flags&(GRE_CSUM|GRE_KEY|GRE_SEQ)) {
1539 +               if (tunnel->parms.o_flags&GRE_CSUM)
1540 +                       addend += 4;
1541 +               if (tunnel->parms.o_flags&GRE_KEY)
1542 +                       addend += 4;
1543 +               if (tunnel->parms.o_flags&GRE_SEQ)
1544 +                       addend += 4;
1545 +       }
1546 +       dev->hard_header_len = hlen + addend;
1547 +       dev->mtu = mtu - addend;
1548 +       tunnel->hlen = addend;
1549 +       return 0;
1550 +}
1551 +
1552 +static int __init ipgre_fb_tunnel_init(struct net_device *dev)
1553 +{
1554 +       struct ip_tunnel *tunnel = netdev_priv(dev);
1555 +       struct iphdr *iph = &tunnel->parms.iph;
1556 +
1557 +       tunnel->dev = dev;
1558 +       strcpy(tunnel->parms.name, dev->name);
1559 +
1560 +       iph->version            = 4;
1561 +       iph->protocol           = IPPROTO_GRE;
1562 +       iph->ihl                = 5;
1563 +       tunnel->hlen            = sizeof(struct iphdr) + 4;
1564 +
1565 +       dev_hold(dev);
1566 +       tunnels_wc[0]           = tunnel;
1567 +       return 0;
1568 +}
1569 +
1570 +
1571 +static struct net_protocol ipgre_protocol = {
1572 +       .handler        =       ipgre_rcv,
1573 +       .err_handler    =       ipgre_err,
1574 +};
1575 +
1576 +
1577 +/*
1578 + *     And now the modules code and kernel interface.
1579 + */
1580 +
1581 +static int __init ipgre_init(void)
1582 +{
1583 +       int err;
1584 +
1585 +       printk(KERN_INFO "GRE over IPv4 tunneling driver\n");
1586 +
1587 +       if (inet_add_protocol(&ipgre_protocol, IPPROTO_GRE) < 0) {
1588 +               printk(KERN_INFO "ipgre init: can't add protocol\n");
1589 +               return -EAGAIN;
1590 +       }
1591 +
1592 +       ipgre_fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "gre0",
1593 +                                          ipgre_ip_tunnel_setup);
1594 +       if (!ipgre_fb_tunnel_dev) {
1595 +               err = -ENOMEM;
1596 +               goto err1;
1597 +       }
1598 +
1599 +       ipgre_fb_tunnel_dev->init = ipgre_fb_tunnel_init;
1600 +
1601 +       if ((err = register_netdev(ipgre_fb_tunnel_dev)))
1602 +               goto err2;
1603 +out:
1604 +       return err;
1605 +err2:
1606 +       free_netdev(ipgre_fb_tunnel_dev);
1607 +err1:
1608 +       inet_del_protocol(&ipgre_protocol, IPPROTO_GRE);
1609 +       goto out;
1610 +}
1611 +
1612 +static void __exit ipgre_destroy_tunnels(void)
1613 +{
1614 +       int prio;
1615 +
1616 +       for (prio = 0; prio < 4; prio++) {
1617 +               int h;
1618 +               for (h = 0; h < HASH_SIZE; h++) {
1619 +                       struct ip_tunnel *t;
1620 +                       while ((t = tunnels[prio][h]) != NULL)
1621 +                               unregister_netdevice(t->dev);
1622 +               }
1623 +       }
1624 +}
1625 +
1626 +static void __exit ipgre_fini(void)
1627 +{
1628 +       if (inet_del_protocol(&ipgre_protocol, IPPROTO_GRE) < 0)
1629 +               printk(KERN_INFO "ipgre close: can't remove protocol\n");
1630 +
1631 +       rtnl_lock();
1632 +       ipgre_destroy_tunnels();
1633 +       rtnl_unlock();
1634 +}
1635 +
1636 +module_init(ipgre_init);
1637 +module_exit(ipgre_fini);
1638 +MODULE_LICENSE("GPL");
1639 diff -Nurp linux-2.6.22-592/drivers/net/Makefile linux-2.6.22-593/drivers/net/Makefile
1640 --- linux-2.6.22-592/drivers/net/Makefile       2008-02-28 13:51:47.000000000 -0500
1641 +++ linux-2.6.22-593/drivers/net/Makefile       2008-02-28 13:51:50.000000000 -0500
1642 @@ -2,6 +2,7 @@
1643  # Makefile for the Linux network (ethercard) device drivers.
1644  #
1645  
1646 +obj-m +=gre.o
1647  obj-y +=ztun.o shortbridge.o
1648  obj-$(CONFIG_E1000) += e1000/
1649  obj-$(CONFIG_E1000E) += e1000e/
1650 diff -Nurp linux-2.6.22-592/include/linux/if_ether.h linux-2.6.22-593/include/linux/if_ether.h
1651 --- linux-2.6.22-592/include/linux/if_ether.h   2007-07-08 19:32:17.000000000 -0400
1652 +++ linux-2.6.22-593/include/linux/if_ether.h   2008-02-28 13:51:50.000000000 -0500
1653 @@ -56,6 +56,7 @@
1654  #define ETH_P_DIAG      0x6005          /* DEC Diagnostics              */
1655  #define ETH_P_CUST      0x6006          /* DEC Customer use             */
1656  #define ETH_P_SCA       0x6007          /* DEC Systems Comms Arch       */
1657 +#define ETH_P_ETH       0x6558          /* Ethernet in Ethernet         */
1658  #define ETH_P_RARP      0x8035         /* Reverse Addr Res packet      */
1659  #define ETH_P_ATALK    0x809B          /* Appletalk DDP                */
1660  #define ETH_P_AARP     0x80F3          /* Appletalk AARP               */
1661 diff -Nurp linux-2.6.22-592/include/linux/if_tunnel.h linux-2.6.22-593/include/linux/if_tunnel.h
1662 --- linux-2.6.22-592/include/linux/if_tunnel.h  2007-07-08 19:32:17.000000000 -0400
1663 +++ linux-2.6.22-593/include/linux/if_tunnel.h  2008-02-28 13:51:50.000000000 -0500
1664 @@ -25,6 +25,7 @@ struct ip_tunnel_parm
1665         __be16                  o_flags;
1666         __be32                  i_key;
1667         __be32                  o_key;
1668 +        __be16                  proto_type;   /*Added*/
1669         struct iphdr            iph;
1670  };
1671  
1672 diff -Nurp linux-2.6.22-592/net/ipv4/ip_gre.c linux-2.6.22-593/net/ipv4/ip_gre.c
1673 --- linux-2.6.22-592/net/ipv4/ip_gre.c  2008-02-28 13:51:40.000000000 -0500
1674 +++ linux-2.6.22-593/net/ipv4/ip_gre.c  2008-02-28 14:05:12.000000000 -0500
1675 @@ -25,6 +25,7 @@
1676  #include <linux/init.h>
1677  #include <linux/in6.h>
1678  #include <linux/inetdevice.h>
1679 +#include <linux/etherdevice.h>   /**XXX added XXX */
1680  #include <linux/igmp.h>
1681  #include <linux/netfilter_ipv4.h>
1682  #include <linux/if_ether.h>
1683 @@ -46,6 +47,8 @@
1684  #include <net/ip6_route.h>
1685  #endif
1686  
1687 +#define GRE_DEBUG 1
1688 +
1689  /*
1690     Problems & solutions
1691     --------------------
1692 @@ -116,7 +119,8 @@
1693   */
1694  
1695  static int ipgre_tunnel_init(struct net_device *dev);
1696 -static void ipgre_tunnel_setup(struct net_device *dev);
1697 +static void ipgre_ip_tunnel_setup(struct net_device *dev);
1698 +static void ipgre_eth_tunnel_setup(struct net_device *dev);
1699  
1700  /* Fallback tunnel: no source, no destination, no key, no options */
1701  
1702 @@ -243,6 +247,7 @@ static struct ip_tunnel * ipgre_tunnel_l
1703         __be32 remote = parms->iph.daddr;
1704         __be32 local = parms->iph.saddr;
1705         __be32 key = parms->i_key;
1706 +       __be16 proto = parms->proto_type;
1707         struct ip_tunnel *t, **tp, *nt;
1708         struct net_device *dev;
1709         char name[IFNAMSIZ];
1710 @@ -256,6 +261,8 @@ static struct ip_tunnel * ipgre_tunnel_l
1711         if (!create)
1712                 return NULL;
1713  
1714 +       printk(KERN_CRIT "Adding tunnel %s with key %d\n", parms->name, ntohl(key));
1715 +
1716         if (parms->name[0])
1717                 strlcpy(name, parms->name, IFNAMSIZ);
1718         else {
1719 @@ -268,8 +275,21 @@ static struct ip_tunnel * ipgre_tunnel_l
1720                 if (i==100)
1721                         goto failed;
1722         }
1723 +       
1724 +       /* Tunnel creation: check payload type and call appropriate
1725 +        * function */
1726 +       switch (proto)
1727 +       {
1728 +           case ETH_P_IP:
1729 +               dev = alloc_netdev(sizeof(*t), name, ipgre_ip_tunnel_setup);
1730 +               break;
1731 +           case ETH_P_ETH:
1732 +               dev = alloc_netdev(sizeof(*t), name, ipgre_eth_tunnel_setup);
1733 +               break;
1734 +           default:
1735 +               return NULL;
1736 +       }
1737  
1738 -       dev = alloc_netdev(sizeof(*t), name, ipgre_tunnel_setup);
1739         if (!dev)
1740           return NULL;
1741  
1742 @@ -562,6 +582,7 @@ static int ipgre_rcv(struct sk_buff *skb
1743         u32    seqno = 0;
1744         struct ip_tunnel *tunnel;
1745         int    offset = 4;
1746 +       __be16 proto;
1747  
1748         if (skb->dev->nd_net != &init_net) {
1749                 kfree_skb(skb);
1750 @@ -574,6 +595,11 @@ static int ipgre_rcv(struct sk_buff *skb
1751         h = skb->data;
1752         flags = *(__be16*)h;
1753  
1754 +#ifdef GRE_DEBUG
1755 +       printk(KERN_DEBUG "gre.c [601] src:%x dst:%x  proto:%d %p", iph->saddr, iph->daddr, iph->protocol, skb->data);
1756 +#endif 
1757 +       proto = ntohs(*(__be16*)(h+2)); /* XXX added XXX */
1758 +       
1759         if (flags&(GRE_CSUM|GRE_KEY|GRE_ROUTING|GRE_SEQ|GRE_VERSION)) {
1760                 /* - Version must be 0.
1761                    - We do not support routing headers.
1762 @@ -625,7 +651,28 @@ static int ipgre_rcv(struct sk_buff *skb
1763                 __pskb_pull(skb, offset);
1764                 skb_reset_network_header(skb);
1765                 skb_postpull_rcsum(skb, skb_transport_header(skb), offset);
1766 -               skb->pkt_type = PACKET_HOST;
1767 +               if(proto == ETH_P_ETH)
1768 +                 {
1769 +#ifdef GRE_DEBUG
1770 +                   unsigned char* tmp_hdr = skb->data;
1771 +                   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);
1772 +#endif             
1773 +                   skb->protocol = eth_type_trans(skb, tunnel->dev);
1774 +
1775 +                   /* XXX added these lines to make arp work? XXX */
1776 +                   /*skb->mac.raw = skb->data;*/
1777 +                   skb->network_header = skb->network_header + ETH_HLEN;
1778 +                   /* XXX added these lines to make arp work? XXX */
1779 +
1780 +#ifdef GRE_DEBUG
1781 +                   tmp_hdr = skb->data;
1782 +                   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);
1783 +                   printk(KERN_ALERT "gre.c [671] received ethernet on gre %x\n",skb->protocol); 
1784 +#endif
1785 +                   memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
1786 +                 }
1787 +               else
1788 +                 skb->pkt_type = PACKET_HOST;
1789  #ifdef CONFIG_NET_IPGRE_BROADCAST
1790                 if (MULTICAST(iph->daddr)) {
1791                         /* Looped back packet, drop it! */
1792 @@ -671,7 +718,7 @@ drop_nolock:
1793         return(0);
1794  }
1795  
1796 -static int ipgre_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
1797 +static int ipgre_ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
1798  {
1799         struct ip_tunnel *tunnel = netdev_priv(dev);
1800         struct net_device_stats *stats = &tunnel->stat;
1801 @@ -904,6 +951,231 @@ tx_error:
1802         return 0;
1803  }
1804  
1805 +static int ipgre_eth_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
1806 +{
1807 +       struct ip_tunnel *tunnel = netdev_priv(dev);
1808 +       struct net_device_stats *stats = &tunnel->stat;
1809 +       struct iphdr *old_iph = ip_hdr(skb);
1810 +       struct iphdr *tiph = &tunnel->parms.iph;
1811 +       u8     tos;
1812 +       __be16 df;
1813 +       struct rtable *rt;              /* Route to the other host */
1814 +       struct net_device *tdev;        /* Device to other host */
1815 +       int    gre_hlen = tunnel->hlen; /* XXX changed XXX*/
1816 +       //struct etheriphdr  *ethiph;
1817 +       struct iphdr  *iph;             /* Our new IP header */
1818 +       int    max_headroom;            /* The extra header space needed */
1819 +       int    mtu;
1820 +
1821 +#ifdef GRE_DEBUG
1822 +       printk(KERN_ALERT "gre.c:972 Starting xmit\n");
1823 +#endif
1824 +
1825 +       if (tunnel->recursion++) {
1826 +               stats->collisions++;
1827 +               goto tx_error;
1828 +       }
1829 +
1830 +       /* Need valid non-multicast daddr.  */
1831 +       if (tiph->daddr == 0 || MULTICAST(tiph->daddr))
1832 +               goto tx_error;
1833 +
1834 +       tos = tiph->tos;
1835 +       if (tos&1) {
1836 +               if (skb->protocol == htons(ETH_P_IP))
1837 +                       tos = old_iph->tos;
1838 +               tos &= ~1;
1839 +       }
1840 +#ifdef GRE_DEBUG
1841 +       printk(KERN_ALERT "gre.c:991 Passed tos assignment.\n");
1842 +#endif
1843 +
1844 +
1845 +       {
1846 +               struct flowi fl = { .fl_net = &init_net,
1847 +                                   .oif = tunnel->parms.link,
1848 +                                   .nl_u = { .ip4_u =
1849 +                                             { .daddr = tiph->daddr,
1850 +                                               .saddr = tiph->saddr,
1851 +                                               .tos = RT_TOS(tos) } },
1852 +                                   .proto = IPPROTO_GRE };
1853 +               if (ip_route_output_key(&rt, &fl)) {
1854 +                       stats->tx_carrier_errors++;
1855 +                       goto tx_error_icmp;
1856 +               }
1857 +       }
1858 +       tdev = rt->u.dst.dev;
1859 +#ifdef GRE_DEBUG
1860 +       printk(KERN_ALERT "gre.c:1006 Passed the route retrieval\n");
1861 +#endif
1862 +       if (tdev == dev) {
1863 +               ip_rt_put(rt);
1864 +               stats->collisions++;
1865 +               goto tx_error;
1866 +       }
1867 +#ifdef GRE_DEBUG
1868 +       printk(KERN_ALERT "gre.c:1018 Passed tdev collision check.\n");
1869 +#endif
1870 +
1871 +       /* Check MTU stuff if kernel panic */
1872 +       df = tiph->frag_off;
1873 +       if (df)
1874 +               mtu = dst_mtu(&rt->u.dst) - tunnel->hlen;
1875 +       else
1876 +               mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu;
1877 +/*
1878 +       if (skb->dst)
1879 +               skb->dst->ops->update_pmtu(skb->dst, mtu);
1880 +        XXX */
1881 +#ifdef GRE_DEBUG
1882 +       printk(KERN_ALERT "gre.c:1032 Passed the pmtu setting.\n");
1883 +#endif
1884 +
1885 +       if (skb->protocol == htons(ETH_P_IP)) {
1886 +               df |= (old_iph->frag_off&htons(IP_DF));
1887 +
1888 +               if ((old_iph->frag_off & htons(IP_DF)) &&
1889 +                   mtu < ntohs(old_iph->tot_len)) {
1890 +                       icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
1891 +                       ip_rt_put(rt);
1892 +                       goto tx_error;
1893 +               }
1894 +       }
1895 +#ifdef CONFIG_IPV6
1896 +       else if (skb->protocol == htons(ETH_P_IPV6)) {
1897 +               struct rt6_info *rt6 = (struct rt6_info*)skb->dst;
1898 +
1899 +               if (rt6 && mtu < dst_mtu(skb->dst) && mtu >= IPV6_MIN_MTU) {
1900 +                       if (tiph->daddr || rt6->rt6i_dst.plen == 128) {
1901 +                               rt6->rt6i_flags |= RTF_MODIFIED;
1902 +                               skb->dst->metrics[RTAX_MTU-1] = mtu;
1903 +                       }
1904 +               }
1905 +
1906 +               /* @@@ Is this correct?  */
1907 +               if (mtu >= IPV6_MIN_MTU && mtu < skb->len - tunnel->hlen + gre_hlen) {
1908 +                       icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
1909 +                       ip_rt_put(rt);
1910 +                       goto tx_error;
1911 +               }
1912 +       }
1913 +#endif
1914 +#ifdef GRE_DEBUG
1915 +       printk(KERN_ALERT "gre.c:1065 Passed the fragmentation check.\n");
1916 +#endif
1917 +
1918 +       if (tunnel->err_count > 0) {
1919 +               if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
1920 +                       tunnel->err_count--;
1921 +                       dst_link_failure(skb);
1922 +               } else
1923 +                       tunnel->err_count = 0;
1924 +       }
1925 +
1926 +       max_headroom = LL_RESERVED_SPACE(tdev) + gre_hlen;
1927 +
1928 +       if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) {
1929 +               struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
1930 +               if (!new_skb) {
1931 +                       ip_rt_put(rt);
1932 +                       stats->tx_dropped++;
1933 +                       dev_kfree_skb(skb);
1934 +                       tunnel->recursion--;
1935 +                       return 0;
1936 +               }
1937 +               if (skb->sk)
1938 +                       skb_set_owner_w(new_skb, skb->sk);
1939 +               dev_kfree_skb(skb);
1940 +               skb = new_skb;
1941 +               old_iph = ip_hdr(skb);
1942 +       }
1943 +#ifdef GRE_DEBUG
1944 +       printk(KERN_ALERT "gre.c:1094 Passed the headroom calculation\n");
1945 +#endif
1946 +
1947 +
1948 +/*     XXX skb->h.raw = skb->nh.raw; XXX */
1949 +//     skb->h.raw = skb->mac.raw;
1950 +       skb->transport_header = skb->mac_header; // Added by valas
1951 +       
1952 +       skb_push(skb, gre_hlen);
1953 +       memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1954 +       dst_release(skb->dst);
1955 +       skb->dst = &rt->u.dst;
1956 +
1957 +       /*
1958 +        *      Push down and install the etherip header.
1959 +        */
1960 +
1961 +       iph                     =       ip_hdr(skb);
1962 +       iph->version            =       4;
1963 +       iph->ihl                =       sizeof(struct iphdr) >> 2;
1964 +       iph->frag_off           =       df;
1965 +       iph->protocol           =       IPPROTO_GRE;
1966 +       iph->tos                =       ipgre_ecn_encapsulate(tos, old_iph, skb);
1967 +       iph->daddr              =       rt->rt_dst;
1968 +       iph->saddr              =       rt->rt_src;
1969 +
1970 +/*     ethiph->version         =       htons(ETHERIP_VERSION); */
1971 +#ifdef GRE_DEBUG
1972 +       printk(KERN_ALERT "gre.c:1121 Passed outer IP header construction.\n");
1973 +#endif
1974 +
1975 +       if ((iph->ttl = tiph->ttl) == 0) {
1976 +               if (skb->protocol == htons(ETH_P_IP))
1977 +                       iph->ttl = old_iph->ttl;
1978 +#ifdef CONFIG_IPV6
1979 +               else if (skb->protocol == htons(ETH_P_IPV6))
1980 +                       iph->ttl = ((struct ipv6hdr*)old_iph)->hop_limit;
1981 +#endif
1982 +               else
1983 +                       iph->ttl = dst_metric(&rt->u.dst, RTAX_HOPLIMIT);
1984 +       }
1985 +#ifdef GRE_DEBUG
1986 +       printk(KERN_ALERT "gre.c:1006 Passed the TTL check.\n");
1987 +#endif
1988 +
1989 +       ((__be16*)(iph+1))[0] = tunnel->parms.o_flags;
1990 +       ((__be16*)(iph+1))[1] = htons(tunnel->parms.proto_type);
1991 +
1992 +       if (tunnel->parms.o_flags&(GRE_KEY|GRE_CSUM|GRE_SEQ)) {
1993 +               __be32 *ptr = (__be32*)(((u8*)iph) + tunnel->hlen - 4);
1994 +
1995 +               if (tunnel->parms.o_flags&GRE_SEQ) {
1996 +                       ++tunnel->o_seqno;
1997 +                       *ptr = htonl(tunnel->o_seqno);
1998 +                       ptr--;
1999 +               }
2000 +               if (tunnel->parms.o_flags&GRE_KEY) {
2001 +                       *ptr = tunnel->parms.o_key;
2002 +                       ptr--;
2003 +               }
2004 +               if (tunnel->parms.o_flags&GRE_CSUM) {
2005 +                       *ptr = 0;
2006 +                       *(__sum16*)ptr = ip_compute_csum((void*)(iph+1), skb->len - sizeof(struct iphdr));
2007 +               }
2008 +       }
2009 +#ifdef GRE_DEBUG
2010 +       printk(KERN_ALERT "gre.c:1006 Passed the tunnel transmit.\n");
2011 +#endif
2012 +
2013 +       nf_reset(skb);
2014 +
2015 +       IPTUNNEL_XMIT();
2016 +       tunnel->recursion--;
2017 +       return 0;
2018 +
2019 +tx_error_icmp:
2020 +       dst_link_failure(skb);
2021 +
2022 +tx_error:
2023 +       stats->tx_errors++;
2024 +       dev_kfree_skb(skb);
2025 +       tunnel->recursion--;
2026 +       return 0;
2027 +}
2028 +
2029 +
2030  static int
2031  ipgre_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
2032  {
2033 @@ -911,6 +1183,8 @@ ipgre_tunnel_ioctl (struct net_device *d
2034         struct ip_tunnel_parm p;
2035         struct ip_tunnel *t;
2036  
2037 +        printk(KERN_ALERT "1174 GRE: entering gre ioctl. command is: %d\n", cmd);
2038 +
2039         switch (cmd) {
2040         case SIOCGETTUNNEL:
2041                 t = NULL;
2042 @@ -952,7 +1226,7 @@ ipgre_tunnel_ioctl (struct net_device *d
2043                         p.o_key = 0;
2044  
2045                 t = ipgre_tunnel_locate(&p, cmd == SIOCADDTUNNEL);
2046 -
2047 +               if (t) printk(KERN_ALERT "1174 GRE: proto %s %x\n", p.name, p.proto_type);
2048                 if (dev != ipgre_fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
2049                         if (t != NULL) {
2050                                 if (t->dev != dev) {
2051 @@ -968,6 +1242,12 @@ ipgre_tunnel_ioctl (struct net_device *d
2052                                         nflags = IFF_BROADCAST;
2053                                 else if (p.iph.daddr)
2054                                         nflags = IFF_POINTOPOINT;
2055 +                               
2056 +                               /* XXX:Set back IFF_BROADCAST if
2057 +                                * transporting ethernet */
2058 +                               printk(KERN_ALERT "1193 GRE: proto %s %d\n", p.name, p.proto_type);
2059 +                               if (p.proto_type == ETH_P_ETH)
2060 +                                       nflags = IFF_BROADCAST;
2061  
2062                                 if ((dev->flags^nflags)&(IFF_POINTOPOINT|IFF_BROADCAST)) {
2063                                         err = -EINVAL;
2064 @@ -978,6 +1258,8 @@ ipgre_tunnel_ioctl (struct net_device *d
2065                                 t->parms.iph.daddr = p.iph.daddr;
2066                                 t->parms.i_key = p.i_key;
2067                                 t->parms.o_key = p.o_key;
2068 +                               /* XXX:Copy in the protocol field */
2069 +                               t->parms.proto_type = p.proto_type;
2070                                 memcpy(dev->dev_addr, &p.iph.saddr, 4);
2071                                 memcpy(dev->broadcast, &p.iph.daddr, 4);
2072                                 ipgre_tunnel_link(t);
2073 @@ -1139,12 +1421,12 @@ static int ipgre_close(struct net_device
2074  
2075  #endif
2076  
2077 -static void ipgre_tunnel_setup(struct net_device *dev)
2078 +static void ipgre_ip_tunnel_setup(struct net_device *dev)
2079  {
2080         SET_MODULE_OWNER(dev);
2081         dev->uninit             = ipgre_tunnel_uninit;
2082         dev->destructor         = free_netdev;
2083 -       dev->hard_start_xmit    = ipgre_tunnel_xmit;
2084 +       dev->hard_start_xmit    = ipgre_ip_tunnel_xmit;
2085         dev->get_stats          = ipgre_tunnel_get_stats;
2086         dev->do_ioctl           = ipgre_tunnel_ioctl;
2087         dev->change_mtu         = ipgre_tunnel_change_mtu;
2088 @@ -1157,6 +1439,36 @@ static void ipgre_tunnel_setup(struct ne
2089         dev->addr_len           = 4;
2090  }
2091  
2092 +/* Tunnel setup for ipgre_eth */
2093 +static void ipgre_eth_tunnel_setup(struct net_device *dev)
2094 +{
2095 +       SET_MODULE_OWNER(dev);
2096 +       ether_setup(dev);
2097 +
2098 +       dev->uninit             = ipgre_tunnel_uninit;
2099 +       dev->destructor         = free_netdev;
2100 +       dev->hard_start_xmit    = ipgre_eth_tunnel_xmit;
2101 +       dev->get_stats          = ipgre_tunnel_get_stats;
2102 +       dev->do_ioctl           = ipgre_tunnel_ioctl;
2103 +       dev->change_mtu         = ipgre_tunnel_change_mtu;
2104 +
2105 +       dev->type               = ARPHRD_ETHER;
2106 +       dev->hard_header_len    = ETH_HLEN + sizeof(struct iphdr) + 4;
2107 +       dev->mtu                = ETH_DATA_LEN - sizeof(struct iphdr) - 4;
2108 +       dev->flags              = 0;
2109 +       dev->tx_queue_len       = 0;
2110 +       dev->iflink             = 0;
2111 +       dev->addr_len           = 6;
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 @@ -1191,7 +1503,14 @@ static int ipgre_tunnel_init(struct net_
2126                         ip_rt_put(rt);
2127                 }
2128  
2129 -               dev->flags |= IFF_POINTOPOINT;
2130 +               if (tunnel->parms.proto_type == ETH_P_ETH)
2131 +               {
2132 +                   dev->flags |= IFF_BROADCAST;
2133 +               }
2134 +               else
2135 +               {
2136 +                       dev->flags |= IFF_POINTOPOINT;
2137 +               }
2138  
2139  #ifdef CONFIG_NET_IPGRE_BROADCAST
2140                 if (MULTICAST(iph->daddr)) {
2141 @@ -1270,7 +1589,7 @@ static int __init ipgre_init(void)
2142         }
2143  
2144         ipgre_fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "gre0",
2145 -                                          ipgre_tunnel_setup);
2146 +                                          ipgre_ip_tunnel_setup);
2147         if (!ipgre_fb_tunnel_dev) {
2148                 err = -ENOMEM;
2149                 goto err1;