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