6b7e2b128e124750622553a2283db88544fa433e
[linux-2.6.git] / linux-2.6-593-egre.patch
1 diff -Nurb linux-2.6.22-592/drivers/net/Makefile linux-2.6.22-593/drivers/net/Makefile
2 --- linux-2.6.22-592/drivers/net/Makefile       2008-03-15 10:50:00.000000000 -0400
3 +++ linux-2.6.22-593/drivers/net/Makefile       2008-03-15 10:51:27.000000000 -0400
4 @@ -2,6 +2,7 @@
5  # Makefile for the Linux network (ethercard) device drivers.
6  #
7  
8 +obj-m +=gre.o
9  obj-y +=ztun.o shortbridge.o
10  obj-$(CONFIG_E1000) += e1000/
11  obj-$(CONFIG_E1000E) += e1000e/
12 diff -Nurb linux-2.6.22-592/drivers/net/gre.c linux-2.6.22-593/drivers/net/gre.c
13 --- linux-2.6.22-592/drivers/net/gre.c  1969-12-31 19:00:00.000000000 -0500
14 +++ linux-2.6.22-593/drivers/net/gre.c  2008-03-15 10:51:27.000000000 -0400
15 @@ -0,0 +1,1632 @@
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  1024
166 +#define HASH(addr) (ntohl(addr)&1023)
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 +       /* HACK */
182 +       unsigned hash_value = HASH(key);
183 +       struct ip_tunnel *t;
184 +
185 +       t = tunnels_r_l[hash_value];
186 +
187 +       if (t && (t->parms.i_key == key) && (t->dev->flags&IFF_UP)) {
188 +               return t;
189 +       }
190 +
191 +       t = tunnels_r[hash_value];
192 +                       if (t && (t->parms.i_key == key) && (t->dev->flags&IFF_UP))
193 +                               return t;
194 +
195 +       t = tunnels_l[hash_value];
196 +                       if (t && (t->parms.i_key == key) && (t->dev->flags&IFF_UP))
197 +                               return t;
198 +       t = tunnels_wc[hash_value];
199 +               if (t && (t->parms.i_key == key) && (t->dev->flags&IFF_UP))
200 +                       return t;
201 +       if (ipgre_fb_tunnel_dev->flags&IFF_UP)
202 +               return netdev_priv(ipgre_fb_tunnel_dev);
203 +       return NULL;
204 +}
205 +
206 +static struct ip_tunnel **ipgre_bucket(struct ip_tunnel *t)
207 +{
208 +       __be32 remote = t->parms.iph.daddr;
209 +       __be32 local = t->parms.iph.saddr;
210 +       __be32 key = t->parms.i_key;
211 +       unsigned h = HASH(key);
212 +       int prio = 0;
213 +
214 +       if (local)
215 +               prio |= 1;
216 +       if (remote && !MULTICAST(remote)) {
217 +               prio |= 2;
218 +               //h ^= HASH(remote);
219 +       }
220 +
221 +       return &tunnels[prio][h];
222 +}
223 +
224 +static void ipgre_tunnel_link(struct ip_tunnel *t)
225 +{
226 +       struct ip_tunnel **tp = ipgre_bucket(t);
227 +
228 +       t->next = *tp;
229 +       write_lock_bh(&ipgre_lock);
230 +       *tp = t;
231 +       write_unlock_bh(&ipgre_lock);
232 +}
233 +
234 +static void ipgre_tunnel_unlink(struct ip_tunnel *t)
235 +{
236 +       struct ip_tunnel **tp;
237 +
238 +       for (tp = ipgre_bucket(t); *tp; tp = &(*tp)->next) {
239 +               if (t == *tp) {
240 +                       write_lock_bh(&ipgre_lock);
241 +                       *tp = t->next;
242 +                       write_unlock_bh(&ipgre_lock);
243 +                       break;
244 +               }
245 +       }
246 +}
247 +
248 +static struct ip_tunnel * ipgre_tunnel_locate(struct ip_tunnel_parm *parms, int create)
249 +{
250 +       __be32 remote = parms->iph.daddr;
251 +       __be32 local = parms->iph.saddr;
252 +       __be32 key = parms->i_key;
253 +       __be16 proto = parms->proto_type;
254 +       struct ip_tunnel *t, **tp, *nt;
255 +       struct net_device *dev;
256 +       unsigned h = HASH(key);
257 +       int prio = 0;
258 +       char name[IFNAMSIZ];
259 +
260 +       if (local)
261 +               prio |= 1;
262 +       if (remote && !MULTICAST(remote)) {
263 +               prio |= 2;
264 +               //h ^= HASH(remote);
265 +       }
266 +       for (tp = &tunnels[prio][h]; (t = *tp) != NULL; tp = &t->next) {
267 +               if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr) {
268 +                       if (key == t->parms.i_key)
269 +                               return t;
270 +               }
271 +       }
272 +       if (!create)
273 +               return NULL;
274 +
275 +       printk(KERN_CRIT "Adding tunnel %s with key %d\n", parms->name, ntohl(key));
276 +
277 +       if (parms->name[0])
278 +               strlcpy(name, parms->name, IFNAMSIZ);
279 +       else {
280 +               int i;
281 +               for (i=1; i<100; i++) {
282 +                       sprintf(name, "gre%d", i);
283 +                       if (__dev_get_by_name(&init_net, name) == NULL)
284 +                               break;
285 +               }
286 +               if (i==100)
287 +                       goto failed;
288 +       }
289 +       
290 +       /* Tunnel creation: check payload type and call appropriate
291 +        * function */
292 +       switch (proto)
293 +       {
294 +           case ETH_P_IP:
295 +               dev = alloc_netdev(sizeof(*t), name, ipgre_ip_tunnel_setup);
296 +               break;
297 +           case ETH_P_ETH:
298 +               dev = alloc_netdev(sizeof(*t), name, ipgre_eth_tunnel_setup);
299 +               break;
300 +           default:
301 +               return NULL;
302 +       }
303 +
304 +       if (!dev)
305 +         return NULL;
306 +
307 +       dev->init = ipgre_tunnel_init;
308 +       nt = netdev_priv(dev);
309 +       nt->parms = *parms;
310 +
311 +       if (register_netdevice(dev) < 0) {
312 +               free_netdev(dev);
313 +               goto failed;
314 +       }
315 +
316 +       dev_hold(dev);
317 +       ipgre_tunnel_link(nt);
318 +       return nt;
319 +
320 +failed:
321 +       return NULL;
322 +}
323 +
324 +static void ipgre_tunnel_uninit(struct net_device *dev)
325 +{
326 +       ipgre_tunnel_unlink(netdev_priv(dev));
327 +       dev_put(dev);
328 +}
329 +
330 +
331 +static void ipgre_err(struct sk_buff *skb, u32 info)
332 +{
333 +#ifndef I_WISH_WORLD_WERE_PERFECT
334 +
335 +/* It is not :-( All the routers (except for Linux) return only
336 +   8 bytes of packet payload. It means, that precise relaying of
337 +   ICMP in the real Internet is absolutely infeasible.
338 +
339 +   Moreover, Cisco "wise men" put GRE key to the third word
340 +   in GRE header. It makes impossible maintaining even soft state for keyed
341 +   GRE tunnels with enabled checksum. Tell them "thank you".
342 +
343 +   Well, I wonder, rfc1812 was written by Cisco employee,
344 +   what the hell these idiots break standrads established
345 +   by themself???
346 + */
347 +
348 +       struct iphdr *iph = (struct iphdr*)skb->data;
349 +       __be16       *p = (__be16*)(skb->data+(iph->ihl<<2));
350 +       int grehlen = (iph->ihl<<2) + 4;
351 +       int type = icmp_hdr(skb)->type;
352 +       int code = icmp_hdr(skb)->code;
353 +       struct ip_tunnel *t;
354 +       __be16 flags;
355 +
356 +       flags = p[0];
357 +       if (flags&(GRE_CSUM|GRE_KEY|GRE_SEQ|GRE_ROUTING|GRE_VERSION)) {
358 +               if (flags&(GRE_VERSION|GRE_ROUTING))
359 +                       return;
360 +               if (flags&GRE_KEY) {
361 +                       grehlen += 4;
362 +                       if (flags&GRE_CSUM)
363 +                               grehlen += 4;
364 +               }
365 +       }
366 +
367 +       /* If only 8 bytes returned, keyed message will be dropped here */
368 +       if (skb_headlen(skb) < grehlen)
369 +               return;
370 +
371 +       switch (type) {
372 +       default:
373 +       case ICMP_PARAMETERPROB:
374 +               return;
375 +
376 +       case ICMP_DEST_UNREACH:
377 +               switch (code) {
378 +               case ICMP_SR_FAILED:
379 +               case ICMP_PORT_UNREACH:
380 +                       /* Impossible event. */
381 +                       return;
382 +               case ICMP_FRAG_NEEDED:
383 +                       /* Soft state for pmtu is maintained by IP core. */
384 +                       return;
385 +               default:
386 +                       /* All others are translated to HOST_UNREACH.
387 +                          rfc2003 contains "deep thoughts" about NET_UNREACH,
388 +                          I believe they are just ether pollution. --ANK
389 +                        */
390 +                       break;
391 +               }
392 +               break;
393 +       case ICMP_TIME_EXCEEDED:
394 +               if (code != ICMP_EXC_TTL)
395 +                       return;
396 +               break;
397 +       }
398 +
399 +       read_lock(&ipgre_lock);
400 +       t = ipgre_tunnel_lookup(iph->daddr, iph->saddr, (flags&GRE_KEY) ? *(((__be32*)p) + (grehlen>>2) - 1) : 0);
401 +       if (t == NULL || t->parms.iph.daddr == 0 || MULTICAST(t->parms.iph.daddr))
402 +               goto out;
403 +
404 +       if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
405 +               goto out;
406 +
407 +       if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO)
408 +               t->err_count++;
409 +       else
410 +               t->err_count = 1;
411 +       t->err_time = jiffies;
412 +out:
413 +       read_unlock(&ipgre_lock);
414 +       return;
415 +#else
416 +       struct iphdr *iph = (struct iphdr*)dp;
417 +       struct iphdr *eiph;
418 +       __be16       *p = (__be16*)(dp+(iph->ihl<<2));
419 +       int type = skb->h.icmph->type;
420 +       int code = skb->h.icmph->code;
421 +       int rel_type = 0;
422 +       int rel_code = 0;
423 +       __be32 rel_info = 0;
424 +       __u32 n = 0;
425 +       __be16 flags;
426 +       int grehlen = (iph->ihl<<2) + 4;
427 +       struct sk_buff *skb2;
428 +       struct flowi fl;
429 +       struct rtable *rt;
430 +
431 +       if (skb->dev->nd_net != &init_net)
432 +               return;
433 +
434 +       if (p[1] != htons(ETH_P_IP))
435 +               return;
436 +
437 +       flags = p[0];
438 +       if (flags&(GRE_CSUM|GRE_KEY|GRE_SEQ|GRE_ROUTING|GRE_VERSION)) {
439 +               if (flags&(GRE_VERSION|GRE_ROUTING))
440 +                       return;
441 +               if (flags&GRE_CSUM)
442 +                       grehlen += 4;
443 +               if (flags&GRE_KEY)
444 +                       grehlen += 4;
445 +               if (flags&GRE_SEQ)
446 +                       grehlen += 4;
447 +       }
448 +       if (len < grehlen + sizeof(struct iphdr))
449 +               return;
450 +       eiph = (struct iphdr*)(dp + grehlen);
451 +
452 +       switch (type) {
453 +       default:
454 +               return;
455 +       case ICMP_PARAMETERPROB:
456 +               n = ntohl(skb->h.icmph->un.gateway) >> 24;
457 +               if (n < (iph->ihl<<2))
458 +                       return;
459 +
460 +               /* So... This guy found something strange INSIDE encapsulated
461 +                  packet. Well, he is fool, but what can we do ?
462 +                */
463 +               rel_type = ICMP_PARAMETERPROB;
464 +               n -= grehlen;
465 +               rel_info = htonl(n << 24);
466 +               break;
467 +
468 +       case ICMP_DEST_UNREACH:
469 +               switch (code) {
470 +               case ICMP_SR_FAILED:
471 +               case ICMP_PORT_UNREACH:
472 +                       /* Impossible event. */
473 +                       return;
474 +               case ICMP_FRAG_NEEDED:
475 +                       /* And it is the only really necessary thing :-) */
476 +                       n = ntohs(skb->h.icmph->un.frag.mtu);
477 +                       if (n < grehlen+68)
478 +                               return;
479 +                       n -= grehlen;
480 +                       /* BSD 4.2 MORE DOES NOT EXIST IN NATURE. */
481 +                       if (n > ntohs(eiph->tot_len))
482 +                               return;
483 +                       rel_info = htonl(n);
484 +                       break;
485 +               default:
486 +                       /* All others are translated to HOST_UNREACH.
487 +                          rfc2003 contains "deep thoughts" about NET_UNREACH,
488 +                          I believe, it is just ether pollution. --ANK
489 +                        */
490 +                       rel_type = ICMP_DEST_UNREACH;
491 +                       rel_code = ICMP_HOST_UNREACH;
492 +                       break;
493 +               }
494 +               break;
495 +       case ICMP_TIME_EXCEEDED:
496 +               if (code != ICMP_EXC_TTL)
497 +                       return;
498 +               break;
499 +       }
500 +
501 +       /* Prepare fake skb to feed it to icmp_send */
502 +       skb2 = skb_clone(skb, GFP_ATOMIC);
503 +       if (skb2 == NULL)
504 +               return;
505 +       dst_release(skb2->dst);
506 +       skb2->dst = NULL;
507 +       skb_pull(skb2, skb->data - (u8*)eiph);
508 +       skb_reset_network_header(skb2);
509 +
510 +       /* Try to guess incoming interface */
511 +       memset(&fl, 0, sizeof(fl));
512 +       fl.fl_net = &init_net;
513 +       fl.fl4_dst = eiph->saddr;
514 +       fl.fl4_tos = RT_TOS(eiph->tos);
515 +       fl.proto = IPPROTO_GRE;
516 +       if (ip_route_output_key(&rt, &fl)) {
517 +               kfree_skb(skb2);
518 +               return;
519 +       }
520 +       skb2->dev = rt->u.dst.dev;
521 +
522 +       /* route "incoming" packet */
523 +       if (rt->rt_flags&RTCF_LOCAL) {
524 +               ip_rt_put(rt);
525 +               rt = NULL;
526 +               fl.fl4_dst = eiph->daddr;
527 +               fl.fl4_src = eiph->saddr;
528 +               fl.fl4_tos = eiph->tos;
529 +               if (ip_route_output_key(&rt, &fl) ||
530 +                   rt->u.dst.dev->type != ARPHRD_IPGRE) {
531 +                       ip_rt_put(rt);
532 +                       kfree_skb(skb2);
533 +                       return;
534 +               }
535 +       } else {
536 +               ip_rt_put(rt);
537 +               if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos, skb2->dev) ||
538 +                   skb2->dst->dev->type != ARPHRD_IPGRE) {
539 +                       kfree_skb(skb2);
540 +                       return;
541 +               }
542 +       }
543 +
544 +       /* change mtu on this route */
545 +       if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
546 +               if (n > dst_mtu(skb2->dst)) {
547 +                       kfree_skb(skb2);
548 +                       return;
549 +               }
550 +               skb2->dst->ops->update_pmtu(skb2->dst, n);
551 +       } else if (type == ICMP_TIME_EXCEEDED) {
552 +               struct ip_tunnel *t = netdev_priv(skb2->dev);
553 +               if (t->parms.iph.ttl) {
554 +                       rel_type = ICMP_DEST_UNREACH;
555 +                       rel_code = ICMP_HOST_UNREACH;
556 +               }
557 +       }
558 +
559 +       icmp_send(skb2, rel_type, rel_code, rel_info);
560 +       kfree_skb(skb2);
561 +#endif
562 +}
563 +
564 +static inline void ipgre_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
565 +{
566 +       if (INET_ECN_is_ce(iph->tos)) {
567 +               if (skb->protocol == htons(ETH_P_IP)) {
568 +                       IP_ECN_set_ce(ip_hdr(skb));
569 +               } else if (skb->protocol == htons(ETH_P_IPV6)) {
570 +                       IP6_ECN_set_ce(ipv6_hdr(skb));
571 +               }
572 +       }
573 +}
574 +
575 +static inline u8
576 +ipgre_ecn_encapsulate(u8 tos, struct iphdr *old_iph, struct sk_buff *skb)
577 +{
578 +       u8 inner = 0;
579 +       if (skb->protocol == htons(ETH_P_IP))
580 +               inner = old_iph->tos;
581 +       else if (skb->protocol == htons(ETH_P_IPV6))
582 +               inner = ipv6_get_dsfield((struct ipv6hdr *)old_iph);
583 +       return INET_ECN_encapsulate(tos, inner);
584 +}
585 +
586 +static int ipgre_rcv(struct sk_buff *skb)
587 +{
588 +       struct iphdr *iph;
589 +       u8     *h;
590 +       __be16    flags;
591 +       __sum16   csum = 0;
592 +       __be32 key = 0;
593 +       u32    seqno = 0;
594 +       struct ip_tunnel *tunnel;
595 +       int    offset = 4;
596 +       __be16 proto;
597 +
598 +       if (skb->dev->nd_net != &init_net) {
599 +               kfree_skb(skb);
600 +               return 0;
601 +       }
602 +       if (!pskb_may_pull(skb, 16))
603 +               goto drop_nolock;
604 +
605 +       iph = ip_hdr(skb);
606 +       h = skb->data;
607 +       flags = *(__be16*)h;
608 +
609 +#ifdef GRE_DEBUG
610 +       printk(KERN_DEBUG "gre.c [601] src:%x dst:%x  proto:%d %x", iph->saddr, iph->daddr, iph->protocol, skb->data);
611 +#endif 
612 +       proto = ntohs(*(__be16*)(h+2)); /* XXX added XXX */
613 +       
614 +       if (flags&(GRE_CSUM|GRE_KEY|GRE_ROUTING|GRE_SEQ|GRE_VERSION)) {
615 +               /* - Version must be 0.
616 +                  - We do not support routing headers.
617 +                */
618 +               if (flags&(GRE_VERSION|GRE_ROUTING))
619 +                       goto drop_nolock;
620 +
621 +               if (flags&GRE_CSUM) {
622 +                       switch (skb->ip_summed) {
623 +                       case CHECKSUM_COMPLETE:
624 +                               csum = csum_fold(skb->csum);
625 +                               if (!csum)
626 +                                       break;
627 +                               /* fall through */
628 +                       case CHECKSUM_NONE:
629 +                               skb->csum = 0;
630 +                               csum = __skb_checksum_complete(skb);
631 +                               skb->ip_summed = CHECKSUM_COMPLETE;
632 +                       }
633 +                       offset += 4;
634 +               }
635 +               if (flags&GRE_KEY) {
636 +                       key = *(__be32*)(h + offset);
637 +                       offset += 4;
638 +               }
639 +               if (flags&GRE_SEQ) {
640 +                       seqno = ntohl(*(__be32*)(h + offset));
641 +                       offset += 4;
642 +               }
643 +       }
644 +
645 +       read_lock(&ipgre_lock);
646 +       if ((tunnel = ipgre_tunnel_lookup(iph->saddr, iph->daddr, key)) != NULL) {
647 +               secpath_reset(skb);
648 +
649 +               skb->protocol = *(__be16*)(h + 2);
650 +               /* WCCP version 1 and 2 protocol decoding.
651 +                * - Change protocol to IP
652 +                * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
653 +                */
654 +               if (flags == 0 &&
655 +                   skb->protocol == htons(ETH_P_WCCP)) {
656 +                       skb->protocol = htons(ETH_P_IP);
657 +                       if ((*(h + offset) & 0xF0) != 0x40)
658 +                               offset += 4;
659 +               }
660 +
661 +               //skb->mac.raw = skb->nh.raw;
662 +               skb_reset_mac_header(skb);
663 +               __pskb_pull(skb, offset);
664 +               skb_reset_network_header(skb);
665 +               skb_postpull_rcsum(skb, skb_transport_header(skb), offset);
666 +               if(proto == ETH_P_ETH)
667 +                 {
668 +#ifdef GRE_DEBUG
669 +                   unsigned char* tmp_hdr = skb->data;
670 +                   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);
671 +#endif             
672 +                   skb->protocol = eth_type_trans(skb, tunnel->dev);
673 +
674 +                   /* XXX added these lines to make arp work? XXX */
675 +                   /*skb->mac.raw = skb->data;*/
676 +                   skb->network_header = skb->network_header + ETH_HLEN;
677 +                   /* XXX added these lines to make arp work? XXX */
678 +
679 +#ifdef GRE_DEBUG
680 +                   tmp_hdr = skb->data;
681 +                   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);
682 +                   printk(KERN_ALERT "gre.c [671] received ethernet on gre %x %x\n",skb->protocol, ((skb->nh).iph)->protocol); 
683 +#endif
684 +                   memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
685 +                 }
686 +               else
687 +                 skb->pkt_type = PACKET_HOST;
688 +#ifdef CONFIG_NET_IPGRE_BROADCAST
689 +               if (MULTICAST(iph->daddr)) {
690 +                       /* Looped back packet, drop it! */
691 +                       if (((struct rtable*)skb->dst)->fl.iif == 0)
692 +                               goto drop;
693 +                       tunnel->stat.multicast++;
694 +                       skb->pkt_type = PACKET_BROADCAST;
695 +               }
696 +#endif
697 +
698 +               if (((flags&GRE_CSUM) && csum) ||
699 +                   (!(flags&GRE_CSUM) && tunnel->parms.i_flags&GRE_CSUM)) {
700 +                       tunnel->stat.rx_crc_errors++;
701 +                       tunnel->stat.rx_errors++;
702 +                       goto drop;
703 +               }
704 +               if (tunnel->parms.i_flags&GRE_SEQ) {
705 +                       if (!(flags&GRE_SEQ) ||
706 +                           (tunnel->i_seqno && (s32)(seqno - tunnel->i_seqno) < 0)) {
707 +                               tunnel->stat.rx_fifo_errors++;
708 +                               tunnel->stat.rx_errors++;
709 +                               goto drop;
710 +                       }
711 +                       tunnel->i_seqno = seqno + 1;
712 +               }
713 +               tunnel->stat.rx_packets++;
714 +               tunnel->stat.rx_bytes += skb->len;
715 +               skb->dev = tunnel->dev;
716 +               dst_release(skb->dst);
717 +               skb->dst = NULL;
718 +               nf_reset(skb);
719 +               ipgre_ecn_decapsulate(iph, skb);
720 +               netif_rx(skb);
721 +               read_unlock(&ipgre_lock);
722 +               return(0);
723 +       }
724 +       icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
725 +
726 +drop:
727 +       read_unlock(&ipgre_lock);
728 +drop_nolock:
729 +       kfree_skb(skb);
730 +       return(0);
731 +}
732 +
733 +static int ipgre_ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
734 +{
735 +       struct ip_tunnel *tunnel = netdev_priv(dev);
736 +       struct net_device_stats *stats = &tunnel->stat;
737 +       struct iphdr  *old_iph = ip_hdr(skb);
738 +       struct iphdr  *tiph;
739 +       u8     tos;
740 +       __be16 df;
741 +       struct rtable *rt;                      /* Route to the other host */
742 +       struct net_device *tdev;                        /* Device to other host */
743 +       struct iphdr  *iph;                     /* Our new IP header */
744 +       int    max_headroom;                    /* The extra header space needed */
745 +       int    gre_hlen;
746 +       __be32 dst;
747 +       int    mtu;
748 +
749 +       if (tunnel->recursion++) {
750 +               tunnel->stat.collisions++;
751 +               goto tx_error;
752 +       }
753 +
754 +       if (dev->hard_header) {
755 +               gre_hlen = 0;
756 +               tiph = (struct iphdr*)skb->data;
757 +       } else {
758 +               gre_hlen = tunnel->hlen;
759 +               tiph = &tunnel->parms.iph;
760 +       }
761 +
762 +       if ((dst = tiph->daddr) == 0) {
763 +               /* NBMA tunnel */
764 +
765 +               if (skb->dst == NULL) {
766 +                       tunnel->stat.tx_fifo_errors++;
767 +                       goto tx_error;
768 +               }
769 +
770 +               if (skb->protocol == htons(ETH_P_IP)) {
771 +                       rt = (struct rtable*)skb->dst;
772 +                       if ((dst = rt->rt_gateway) == 0)
773 +                               goto tx_error_icmp;
774 +               }
775 +#ifdef CONFIG_IPV6
776 +               else if (skb->protocol == htons(ETH_P_IPV6)) {
777 +                       struct in6_addr *addr6;
778 +                       int addr_type;
779 +                       struct neighbour *neigh = skb->dst->neighbour;
780 +
781 +                       if (neigh == NULL)
782 +                               goto tx_error;
783 +
784 +                       addr6 = (struct in6_addr*)&neigh->primary_key;
785 +                       addr_type = ipv6_addr_type(addr6);
786 +
787 +                       if (addr_type == IPV6_ADDR_ANY) {
788 +                               addr6 = &ipv6_hdr(skb)->daddr;
789 +                               addr_type = ipv6_addr_type(addr6);
790 +                       }
791 +
792 +                       if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
793 +                               goto tx_error_icmp;
794 +
795 +               }
796 +#endif
797 +               else
798 +                       goto tx_error;
799 +       }
800 +
801 +       tos = tiph->tos;
802 +       if (tos&1) {
803 +               if (skb->protocol == htons(ETH_P_IP))
804 +                       tos = old_iph->tos;
805 +               tos &= ~1;
806 +       }
807 +
808 +       {
809 +               struct flowi fl = { .fl_net = &init_net,
810 +                                   .oif = tunnel->parms.link,
811 +                                   .nl_u = { .ip4_u =
812 +                                             { .daddr = dst,
813 +                                               .saddr = tiph->saddr,
814 +                                               .tos = RT_TOS(tos) } },
815 +                                   .proto = IPPROTO_GRE };
816 +               if (ip_route_output_key(&rt, &fl)) {
817 +                       tunnel->stat.tx_carrier_errors++;
818 +                       goto tx_error;
819 +               }
820 +       }
821 +       tdev = rt->u.dst.dev;
822 +
823 +
824 +       if (tdev == dev) {
825 +               ip_rt_put(rt);
826 +               tunnel->stat.collisions++;
827 +               goto tx_error;
828 +       }
829 +
830 +       df = tiph->frag_off;
831 +       if (df)
832 +               mtu = dst_mtu(&rt->u.dst) - tunnel->hlen;
833 +       else
834 +               mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu;
835 +
836 +       if (skb->dst)
837 +               skb->dst->ops->update_pmtu(skb->dst, mtu);
838 +
839 +       if (skb->protocol == htons(ETH_P_IP)) {
840 +               df |= (old_iph->frag_off&htons(IP_DF));
841 +
842 +               if ((old_iph->frag_off&htons(IP_DF)) &&
843 +                   mtu < ntohs(old_iph->tot_len)) {
844 +                       icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
845 +                       ip_rt_put(rt);
846 +                       goto tx_error;
847 +               }
848 +       }
849 +#ifdef CONFIG_IPV6
850 +       else if (skb->protocol == htons(ETH_P_IPV6)) {
851 +               struct rt6_info *rt6 = (struct rt6_info*)skb->dst;
852 +
853 +               if (rt6 && mtu < dst_mtu(skb->dst) && mtu >= IPV6_MIN_MTU) {
854 +                       if ((tunnel->parms.iph.daddr && !MULTICAST(tunnel->parms.iph.daddr)) ||
855 +                           rt6->rt6i_dst.plen == 128) {
856 +                               rt6->rt6i_flags |= RTF_MODIFIED;
857 +                               skb->dst->metrics[RTAX_MTU-1] = mtu;
858 +                       }
859 +               }
860 +
861 +               if (mtu >= IPV6_MIN_MTU && mtu < skb->len - tunnel->hlen + gre_hlen) {
862 +                       icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
863 +                       ip_rt_put(rt);
864 +                       goto tx_error;
865 +               }
866 +       }
867 +#endif
868 +
869 +       if (tunnel->err_count > 0) {
870 +               if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
871 +                       tunnel->err_count--;
872 +
873 +                       dst_link_failure(skb);
874 +               } else
875 +                       tunnel->err_count = 0;
876 +       }
877 +
878 +       max_headroom = LL_RESERVED_SPACE(tdev) + gre_hlen;
879 +
880 +       if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) {
881 +               struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
882 +               if (!new_skb) {
883 +                       ip_rt_put(rt);
884 +                       stats->tx_dropped++;
885 +                       dev_kfree_skb(skb);
886 +                       tunnel->recursion--;
887 +                       return 0;
888 +               }
889 +               if (skb->sk)
890 +                       skb_set_owner_w(new_skb, skb->sk);
891 +               dev_kfree_skb(skb);
892 +               skb = new_skb;
893 +               old_iph = ip_hdr(skb);
894 +       }
895 +
896 +       skb->transport_header = skb->network_header;
897 +       skb_push(skb, gre_hlen);
898 +       memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
899 +       IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
900 +                             IPSKB_REROUTED);
901 +       dst_release(skb->dst);
902 +       skb->dst = &rt->u.dst;
903 +
904 +       /*
905 +        *      Push down and install the IPIP header.
906 +        */
907 +
908 +       iph                     =       ip_hdr(skb);
909 +       iph->version            =       4;
910 +       iph->ihl                =       sizeof(struct iphdr) >> 2;
911 +       iph->frag_off           =       df;
912 +       iph->protocol           =       IPPROTO_GRE;
913 +       iph->tos                =       ipgre_ecn_encapsulate(tos, old_iph, skb);
914 +       iph->daddr              =       rt->rt_dst;
915 +       iph->saddr              =       rt->rt_src;
916 +
917 +       if ((iph->ttl = tiph->ttl) == 0) {
918 +               if (skb->protocol == htons(ETH_P_IP))
919 +                       iph->ttl = old_iph->ttl;
920 +#ifdef CONFIG_IPV6
921 +               else if (skb->protocol == htons(ETH_P_IPV6))
922 +                       iph->ttl = ((struct ipv6hdr*)old_iph)->hop_limit;
923 +#endif
924 +               else
925 +                       iph->ttl = dst_metric(&rt->u.dst, RTAX_HOPLIMIT);
926 +       }
927 +
928 +       ((__be16*)(iph+1))[0] = tunnel->parms.o_flags;
929 +       ((__be16*)(iph+1))[1] = skb->protocol;
930 +
931 +       if (tunnel->parms.o_flags&(GRE_KEY|GRE_CSUM|GRE_SEQ)) {
932 +               __be32 *ptr = (__be32*)(((u8*)iph) + tunnel->hlen - 4);
933 +
934 +               if (tunnel->parms.o_flags&GRE_SEQ) {
935 +                       ++tunnel->o_seqno;
936 +                       *ptr = htonl(tunnel->o_seqno);
937 +                       ptr--;
938 +               }
939 +               if (tunnel->parms.o_flags&GRE_KEY) {
940 +                       *ptr = tunnel->parms.o_key;
941 +                       ptr--;
942 +               }
943 +               if (tunnel->parms.o_flags&GRE_CSUM) {
944 +                       *ptr = 0;
945 +                       *(__sum16*)ptr = ip_compute_csum((void*)(iph+1), skb->len - sizeof(struct iphdr));
946 +               }
947 +       }
948 +
949 +       nf_reset(skb);
950 +
951 +       IPTUNNEL_XMIT();
952 +       tunnel->recursion--;
953 +       return 0;
954 +
955 +tx_error_icmp:
956 +       dst_link_failure(skb);
957 +
958 +tx_error:
959 +       stats->tx_errors++;
960 +       dev_kfree_skb(skb);
961 +       tunnel->recursion--;
962 +       return 0;
963 +}
964 +
965 +static int ipgre_eth_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
966 +{
967 +       struct ip_tunnel *tunnel = netdev_priv(dev);
968 +       struct net_device_stats *stats = &tunnel->stat;
969 +       struct iphdr *old_iph = ip_hdr(skb);
970 +       struct iphdr *tiph = &tunnel->parms.iph;
971 +       u8     tos;
972 +       __be16 df;
973 +       struct rtable *rt;              /* Route to the other host */
974 +       struct net_device *tdev;        /* Device to other host */
975 +       int    gre_hlen = tunnel->hlen; /* XXX changed XXX*/
976 +       //struct etheriphdr  *ethiph;
977 +       struct iphdr  *iph;             /* Our new IP header */
978 +       int    max_headroom;            /* The extra header space needed */
979 +       int    mtu;
980 +
981 +#ifdef GRE_DEBUG
982 +       printk(KERN_ALERT "gre.c:972 Starting xmit\n");
983 +#endif
984 +
985 +       if (tunnel->recursion++) {
986 +               stats->collisions++;
987 +               goto tx_error;
988 +       }
989 +
990 +       /* Need valid non-multicast daddr.  */
991 +       if (tiph->daddr == 0 || MULTICAST(tiph->daddr))
992 +               goto tx_error;
993 +
994 +       tos = tiph->tos;
995 +       if (tos&1) {
996 +               if (skb->protocol == htons(ETH_P_IP))
997 +                       tos = old_iph->tos;
998 +               tos &= ~1;
999 +       }
1000 +#ifdef GRE_DEBUG
1001 +       printk(KERN_ALERT "gre.c:991 Passed tos assignment.\n");
1002 +#endif
1003 +
1004 +
1005 +       {
1006 +               struct flowi fl = { .fl_net = &init_net,
1007 +                                   .oif = tunnel->parms.link,
1008 +                                   .nl_u = { .ip4_u =
1009 +                                             { .daddr = tiph->daddr,
1010 +                                               .saddr = tiph->saddr,
1011 +                                               .tos = RT_TOS(tos) } },
1012 +                                   .proto = IPPROTO_GRE };
1013 +               if (ip_route_output_key(&rt, &fl)) {
1014 +                       stats->tx_carrier_errors++;
1015 +                       goto tx_error_icmp;
1016 +               }
1017 +       }
1018 +       tdev = rt->u.dst.dev;
1019 +#ifdef GRE_DEBUG
1020 +       printk(KERN_ALERT "gre.c:1006 Passed the route retrieval\n");
1021 +#endif
1022 +       if (tdev == dev) {
1023 +               ip_rt_put(rt);
1024 +               stats->collisions++;
1025 +               goto tx_error;
1026 +       }
1027 +#ifdef GRE_DEBUG
1028 +       printk(KERN_ALERT "gre.c:1018 Passed tdev collision check.\n");
1029 +#endif
1030 +
1031 +       /* Check MTU stuff if kernel panic */
1032 +       df = tiph->frag_off;
1033 +       if (df)
1034 +               mtu = dst_mtu(&rt->u.dst) - tunnel->hlen;
1035 +       else
1036 +               mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu;
1037 +/*
1038 +       if (skb->dst)
1039 +               skb->dst->ops->update_pmtu(skb->dst, mtu);
1040 +        XXX */
1041 +#ifdef GRE_DEBUG
1042 +       printk(KERN_ALERT "gre.c:1032 Passed the pmtu setting.\n");
1043 +#endif
1044 +
1045 +       if (skb->protocol == htons(ETH_P_IP)) {
1046 +               df |= (old_iph->frag_off&htons(IP_DF));
1047 +
1048 +               if ((old_iph->frag_off & htons(IP_DF)) &&
1049 +                   mtu < ntohs(old_iph->tot_len)) {
1050 +                       icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
1051 +                       ip_rt_put(rt);
1052 +                       goto tx_error;
1053 +               }
1054 +       }
1055 +#ifdef CONFIG_IPV6
1056 +       else if (skb->protocol == htons(ETH_P_IPV6)) {
1057 +               struct rt6_info *rt6 = (struct rt6_info*)skb->dst;
1058 +
1059 +               if (rt6 && mtu < dst_mtu(skb->dst) && mtu >= IPV6_MIN_MTU) {
1060 +                       if (tiph->daddr || rt6->rt6i_dst.plen == 128) {
1061 +                               rt6->rt6i_flags |= RTF_MODIFIED;
1062 +                               skb->dst->metrics[RTAX_MTU-1] = mtu;
1063 +                       }
1064 +               }
1065 +
1066 +               /* @@@ Is this correct?  */
1067 +               if (mtu >= IPV6_MIN_MTU && mtu < skb->len - tunnel->hlen + gre_hlen) {
1068 +                       icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
1069 +                       ip_rt_put(rt);
1070 +                       goto tx_error;
1071 +               }
1072 +       }
1073 +#endif
1074 +#ifdef GRE_DEBUG
1075 +       printk(KERN_ALERT "gre.c:1065 Passed the fragmentation check.\n");
1076 +#endif
1077 +
1078 +       if (tunnel->err_count > 0) {
1079 +               if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
1080 +                       tunnel->err_count--;
1081 +                       dst_link_failure(skb);
1082 +               } else
1083 +                       tunnel->err_count = 0;
1084 +       }
1085 +
1086 +       max_headroom = LL_RESERVED_SPACE(tdev) + gre_hlen;
1087 +
1088 +       if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) {
1089 +               struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
1090 +               if (!new_skb) {
1091 +                       ip_rt_put(rt);
1092 +                       stats->tx_dropped++;
1093 +                       dev_kfree_skb(skb);
1094 +                       tunnel->recursion--;
1095 +                       return 0;
1096 +               }
1097 +               if (skb->sk)
1098 +                       skb_set_owner_w(new_skb, skb->sk);
1099 +               dev_kfree_skb(skb);
1100 +               skb = new_skb;
1101 +               old_iph = ip_hdr(skb);
1102 +       }
1103 +#ifdef GRE_DEBUG
1104 +       printk(KERN_ALERT "gre.c:1094 Passed the headroom calculation\n");
1105 +#endif
1106 +
1107 +
1108 +       skb->transport_header = skb->mac_header; // Added by valas
1109 +       skb_push(skb, gre_hlen);
1110 +       skb_reset_network_header(skb);
1111 +       memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1112 +       dst_release(skb->dst);
1113 +       skb->dst = &rt->u.dst;
1114 +
1115 +       /*
1116 +        *      Push down and install the etherip header.
1117 +        */
1118 +
1119 +       iph                     =       ip_hdr(skb);
1120 +       iph->version            =       4;
1121 +       iph->ihl                =       sizeof(struct iphdr) >> 2;
1122 +       iph->frag_off           =       df;
1123 +       iph->protocol           =       IPPROTO_GRE;
1124 +       iph->tos                =       ipgre_ecn_encapsulate(tos, old_iph, skb);
1125 +       iph->daddr              =       rt->rt_dst;
1126 +       iph->saddr              =       rt->rt_src;
1127 +
1128 +/*     ethiph->version         =       htons(ETHERIP_VERSION); */
1129 +#ifdef GRE_DEBUG
1130 +       printk(KERN_ALERT "gre.c:1121 Passed outer IP header construction.\n");
1131 +#endif
1132 +
1133 +       if ((iph->ttl = tiph->ttl) == 0) {
1134 +               if (skb->protocol == htons(ETH_P_IP))
1135 +                       iph->ttl = old_iph->ttl;
1136 +#ifdef CONFIG_IPV6
1137 +               else if (skb->protocol == htons(ETH_P_IPV6))
1138 +                       iph->ttl = ((struct ipv6hdr*)old_iph)->hop_limit;
1139 +#endif
1140 +               else
1141 +                       iph->ttl = dst_metric(&rt->u.dst, RTAX_HOPLIMIT);
1142 +       }
1143 +#ifdef GRE_DEBUG
1144 +       printk(KERN_ALERT "gre.c:1006 Passed the TTL check.\n");
1145 +#endif
1146 +
1147 +       ((__be16*)(iph+1))[0] = tunnel->parms.o_flags;
1148 +       ((__be16*)(iph+1))[1] = htons(tunnel->parms.proto_type);
1149 +
1150 +       if (tunnel->parms.o_flags&(GRE_KEY|GRE_CSUM|GRE_SEQ)) {
1151 +               __be32 *ptr = (__be32*)(((u8*)iph) + tunnel->hlen - 4);
1152 +
1153 +               if (tunnel->parms.o_flags&GRE_SEQ) {
1154 +                       ++tunnel->o_seqno;
1155 +                       *ptr = htonl(tunnel->o_seqno);
1156 +                       ptr--;
1157 +               }
1158 +               if (tunnel->parms.o_flags&GRE_KEY) {
1159 +                       *ptr = tunnel->parms.o_key;
1160 +                       ptr--;
1161 +               }
1162 +               if (tunnel->parms.o_flags&GRE_CSUM) {
1163 +                       *ptr = 0;
1164 +                       *(__sum16*)ptr = ip_compute_csum((void*)(iph+1), skb->len - sizeof(struct iphdr));
1165 +               }
1166 +       }
1167 +#ifdef GRE_DEBUG
1168 +       printk(KERN_ALERT "gre.c:1006 Passed the tunnel transmit.\n");
1169 +#endif
1170 +
1171 +       nf_reset(skb);
1172 +
1173 +       IPTUNNEL_XMIT();
1174 +       tunnel->recursion--;
1175 +       return 0;
1176 +
1177 +tx_error_icmp:
1178 +       dst_link_failure(skb);
1179 +
1180 +tx_error:
1181 +       stats->tx_errors++;
1182 +       dev_kfree_skb(skb);
1183 +       tunnel->recursion--;
1184 +       return 0;
1185 +}
1186 +
1187 +
1188 +static int
1189 +ipgre_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
1190 +{
1191 +       int err = 0;
1192 +       struct ip_tunnel_parm p;
1193 +       struct ip_tunnel *t;
1194 +
1195 +        printk(KERN_ALERT "1174 GRE: entering gre ioctl. command is: %d\n", cmd);
1196 +
1197 +       switch (cmd) {
1198 +       case SIOCGETTUNNEL:
1199 +               t = NULL;
1200 +               if (dev == ipgre_fb_tunnel_dev) {
1201 +                       if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
1202 +                               err = -EFAULT;
1203 +                               break;
1204 +                       }
1205 +                       t = ipgre_tunnel_locate(&p, 0);
1206 +               }
1207 +               if (t == NULL)
1208 +                       t = netdev_priv(dev);
1209 +               memcpy(&p, &t->parms, sizeof(p));
1210 +               if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1211 +                       err = -EFAULT;
1212 +               break;
1213 +
1214 +       case SIOCADDTUNNEL:
1215 +       case SIOCCHGTUNNEL:
1216 +               err = -EPERM;
1217 +               if (!capable(CAP_NET_ADMIN))
1218 +                       goto done;
1219 +
1220 +               err = -EFAULT;
1221 +               if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1222 +                       goto done;
1223 +
1224 +               err = -EINVAL;
1225 +               if (p.iph.version != 4 || p.iph.protocol != IPPROTO_GRE ||
1226 +                   p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)) ||
1227 +                   ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING)))
1228 +                       goto done;
1229 +               if (p.iph.ttl)
1230 +                       p.iph.frag_off |= htons(IP_DF);
1231 +
1232 +               if (!(p.i_flags&GRE_KEY))
1233 +                       p.i_key = 0;
1234 +               if (!(p.o_flags&GRE_KEY))
1235 +                       p.o_key = 0;
1236 +
1237 +               t = ipgre_tunnel_locate(&p, cmd == SIOCADDTUNNEL);
1238 +               if (t) printk(KERN_ALERT "1174 GRE: proto %s %d\n", p.name, p.proto_type);
1239 +               if (dev != ipgre_fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
1240 +                       if (t != NULL) {
1241 +                               if (t->dev != dev) {
1242 +                                       err = -EEXIST;
1243 +                                       break;
1244 +                               }
1245 +                       } else {
1246 +                               unsigned nflags=0;
1247 +
1248 +                               t = netdev_priv(dev);
1249 +
1250 +                               if (MULTICAST(p.iph.daddr))
1251 +                                       nflags = IFF_BROADCAST;
1252 +                               else if (p.iph.daddr)
1253 +                                       nflags = IFF_POINTOPOINT;
1254 +                               
1255 +                               /* XXX:Set back IFF_BROADCAST if
1256 +                                * transporting ethernet */
1257 +                               printk(KERN_ALERT "1193 GRE: proto %s %d\n", p.name, p.proto_type);
1258 +                               if (p.proto_type == ETH_P_ETH)
1259 +                                       nflags = IFF_BROADCAST;
1260 +
1261 +                               if ((dev->flags^nflags)&(IFF_POINTOPOINT|IFF_BROADCAST)) {
1262 +                                       err = -EINVAL;
1263 +                                       break;
1264 +                               }
1265 +                               ipgre_tunnel_unlink(t);
1266 +                               t->parms.iph.saddr = p.iph.saddr;
1267 +                               t->parms.iph.daddr = p.iph.daddr;
1268 +                               t->parms.i_key = p.i_key;
1269 +                               t->parms.o_key = p.o_key;
1270 +                               /* XXX:Copy in the protocol field */
1271 +                               t->parms.proto_type = p.proto_type;
1272 +                               if (t->parms.proto_type != ETH_P_ETH)
1273 +                               {
1274 +                                       memcpy(dev->dev_addr, &p.iph.saddr, 4);
1275 +                                       memcpy(dev->broadcast, &p.iph.daddr, 4);
1276 +                               }
1277 +                               ipgre_tunnel_link(t);
1278 +                               netdev_state_change(dev);
1279 +                       }
1280 +               }
1281 +
1282 +               if (t) {
1283 +                       err = 0;
1284 +                       if (cmd == SIOCCHGTUNNEL) {
1285 +                               t->parms.iph.ttl = p.iph.ttl;
1286 +                               t->parms.iph.tos = p.iph.tos;
1287 +                               t->parms.iph.frag_off = p.iph.frag_off;
1288 +                       }
1289 +                       if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
1290 +                               err = -EFAULT;
1291 +               } else
1292 +                       err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
1293 +               break;
1294 +
1295 +       case SIOCDELTUNNEL:
1296 +               err = -EPERM;
1297 +               if (!capable(CAP_NET_ADMIN))
1298 +                       goto done;
1299 +
1300 +               if (dev == ipgre_fb_tunnel_dev) {
1301 +                       err = -EFAULT;
1302 +                       if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1303 +                               goto done;
1304 +                       err = -ENOENT;
1305 +                       if ((t = ipgre_tunnel_locate(&p, 0)) == NULL)
1306 +                               goto done;
1307 +                       err = -EPERM;
1308 +                       if (t == netdev_priv(ipgre_fb_tunnel_dev))
1309 +                               goto done;
1310 +                       dev = t->dev;
1311 +               }
1312 +               unregister_netdevice(dev); // added by Valas
1313 +               break;
1314 +
1315 +       default:
1316 +               err = -EINVAL;
1317 +       }
1318 +
1319 +done:
1320 +       return err;
1321 +}
1322 +
1323 +static struct net_device_stats *ipgre_tunnel_get_stats(struct net_device *dev)
1324 +{
1325 +       return &(((struct ip_tunnel*)netdev_priv(dev))->stat);
1326 +}
1327 +
1328 +static int ipgre_tunnel_change_mtu(struct net_device *dev, int new_mtu)
1329 +{
1330 +       struct ip_tunnel *tunnel = netdev_priv(dev);
1331 +       if (new_mtu < 68 || new_mtu > 0xFFF8 - tunnel->hlen)
1332 +               return -EINVAL;
1333 +       dev->mtu = new_mtu;
1334 +       return 0;
1335 +}
1336 +
1337 +#ifdef CONFIG_NET_IPGRE_BROADCAST
1338 +/* Nice toy. Unfortunately, useless in real life :-)
1339 +   It allows to construct virtual multiprotocol broadcast "LAN"
1340 +   over the Internet, provided multicast routing is tuned.
1341 +
1342 +
1343 +   I have no idea was this bicycle invented before me,
1344 +   so that I had to set ARPHRD_IPGRE to a random value.
1345 +   I have an impression, that Cisco could make something similar,
1346 +   but this feature is apparently missing in IOS<=11.2(8).
1347 +
1348 +   I set up 10.66.66/24 and fec0:6666:6666::0/96 as virtual networks
1349 +   with broadcast 224.66.66.66. If you have access to mbone, play with me :-)
1350 +
1351 +   ping -t 255 224.66.66.66
1352 +
1353 +   If nobody answers, mbone does not work.
1354 +
1355 +   ip tunnel add Universe mode gre remote 224.66.66.66 local <Your_real_addr> ttl 255
1356 +   ip addr add 10.66.66.<somewhat>/24 dev Universe
1357 +   ifconfig Universe up
1358 +   ifconfig Universe add fe80::<Your_real_addr>/10
1359 +   ifconfig Universe add fec0:6666:6666::<Your_real_addr>/96
1360 +   ftp 10.66.66.66
1361 +   ...
1362 +   ftp fec0:6666:6666::193.233.7.65
1363 +   ...
1364 +
1365 + */
1366 +
1367 +static int ipgre_header(struct sk_buff *skb, struct net_device *dev, unsigned short type,
1368 +                       void *daddr, void *saddr, unsigned len)
1369 +{
1370 +       struct ip_tunnel *t = netdev_priv(dev);
1371 +       struct iphdr *iph = (struct iphdr *)skb_push(skb, t->hlen);
1372 +       __be16 *p = (__be16*)(iph+1);
1373 +
1374 +       memcpy(iph, &t->parms.iph, sizeof(struct iphdr));
1375 +       p[0]            = t->parms.o_flags;
1376 +       p[1]            = htons(type);
1377 +
1378 +       /*
1379 +        *      Set the source hardware address.
1380 +        */
1381 +
1382 +       if (saddr)
1383 +               memcpy(&iph->saddr, saddr, 4);
1384 +
1385 +       if (daddr) {
1386 +               memcpy(&iph->daddr, daddr, 4);
1387 +               return t->hlen;
1388 +       }
1389 +       if (iph->daddr && !MULTICAST(iph->daddr))
1390 +               return t->hlen;
1391 +
1392 +       return -t->hlen;
1393 +}
1394 +
1395 +static int ipgre_open(struct net_device *dev)
1396 +{
1397 +       struct ip_tunnel *t = netdev_priv(dev);
1398 +
1399 +       if (MULTICAST(t->parms.iph.daddr)) {
1400 +               struct flowi fl = { .fl_net = &init_net,
1401 +                                   .oif = t->parms.link,
1402 +                                   .nl_u = { .ip4_u =
1403 +                                             { .daddr = t->parms.iph.daddr,
1404 +                                               .saddr = t->parms.iph.saddr,
1405 +                                               .tos = RT_TOS(t->parms.iph.tos) } },
1406 +                                   .proto = IPPROTO_GRE };
1407 +               struct rtable *rt;
1408 +               if (ip_route_output_key(&rt, &fl))
1409 +                       return -EADDRNOTAVAIL;
1410 +               dev = rt->u.dst.dev;
1411 +               ip_rt_put(rt);
1412 +               if (__in_dev_get_rtnl(dev) == NULL)
1413 +                       return -EADDRNOTAVAIL;
1414 +               t->mlink = dev->ifindex;
1415 +               ip_mc_inc_group(__in_dev_get_rtnl(dev), t->parms.iph.daddr);
1416 +       }
1417 +       return 0;
1418 +}
1419 +
1420 +static int ipgre_close(struct net_device *dev)
1421 +{
1422 +       struct ip_tunnel *t = netdev_priv(dev);
1423 +       if (MULTICAST(t->parms.iph.daddr) && t->mlink) {
1424 +               struct in_device *in_dev = inetdev_by_index(&init_net, t->mlink);
1425 +               if (in_dev) {
1426 +                       ip_mc_dec_group(in_dev, t->parms.iph.daddr);
1427 +                       in_dev_put(in_dev);
1428 +               }
1429 +       }
1430 +       return 0;
1431 +}
1432 +
1433 +#endif
1434 +
1435 +static void ipgre_ip_tunnel_setup(struct net_device *dev)
1436 +{
1437 +       SET_MODULE_OWNER(dev);
1438 +       dev->uninit             = ipgre_tunnel_uninit;
1439 +       dev->destructor         = free_netdev;
1440 +       dev->hard_start_xmit    = ipgre_ip_tunnel_xmit;
1441 +       dev->get_stats          = ipgre_tunnel_get_stats;
1442 +       dev->do_ioctl           = ipgre_tunnel_ioctl;
1443 +       dev->change_mtu         = ipgre_tunnel_change_mtu;
1444 +
1445 +       dev->type               = ARPHRD_IPGRE;
1446 +       dev->hard_header_len    = LL_MAX_HEADER + sizeof(struct iphdr) + 4;
1447 +       dev->mtu                = ETH_DATA_LEN - sizeof(struct iphdr) - 4;
1448 +       dev->flags              = IFF_NOARP;
1449 +       dev->iflink             = 0;
1450 +       dev->addr_len           = 4;
1451 +}
1452 +
1453 +/* Tunnel setup for ipgre_eth */
1454 +static void ipgre_eth_tunnel_setup(struct net_device *dev)
1455 +{
1456 +       SET_MODULE_OWNER(dev);
1457 +       ether_setup(dev);
1458 +
1459 +       dev->uninit             = ipgre_tunnel_uninit;
1460 +       dev->destructor         = free_netdev;
1461 +       dev->hard_start_xmit    = ipgre_eth_tunnel_xmit;
1462 +       dev->get_stats          = ipgre_tunnel_get_stats;
1463 +       dev->do_ioctl           = ipgre_tunnel_ioctl;
1464 +       dev->change_mtu         = ipgre_tunnel_change_mtu;
1465 +
1466 +       dev->hard_header_len    = ETH_HLEN + sizeof(struct iphdr) + 4;
1467 +       dev->tx_queue_len       = 0;
1468 +       random_ether_addr(dev->dev_addr);
1469 +
1470 +#ifdef GRE_DEBUG
1471 +       unsigned char* d = dev->dev_addr;
1472 +       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]);
1473 +#endif 
1474 +
1475 +       dev->iflink             = 0;
1476 +}
1477 +
1478 +
1479 +static int ipgre_tunnel_init(struct net_device *dev)
1480 +{
1481 +       struct net_device *tdev = NULL;
1482 +       struct ip_tunnel *tunnel;
1483 +       struct iphdr *iph;
1484 +       int hlen = LL_MAX_HEADER;
1485 +       int mtu = ETH_DATA_LEN;
1486 +       int addend = sizeof(struct iphdr) + 4;
1487 +
1488 +       tunnel = netdev_priv(dev);
1489 +       iph = &tunnel->parms.iph;
1490 +
1491 +       tunnel->dev = dev;
1492 +       strcpy(tunnel->parms.name, dev->name);
1493 +
1494 +       if (tunnel->parms.proto_type != ETH_P_ETH)
1495 +       {
1496 +               memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
1497 +               memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
1498 +       }
1499 +
1500 +       /* Guess output device to choose reasonable mtu and hard_header_len */
1501 +
1502 +       if (iph->daddr) {
1503 +               struct flowi fl = { .fl_net = &init_net,
1504 +                                   .oif = tunnel->parms.link,
1505 +                                   .nl_u = { .ip4_u =
1506 +                                             { .daddr = iph->daddr,
1507 +                                               .saddr = iph->saddr,
1508 +                                               .tos = RT_TOS(iph->tos) } },
1509 +                                   .proto = IPPROTO_GRE };
1510 +               struct rtable *rt;
1511 +               if (!ip_route_output_key(&rt, &fl)) {
1512 +                       tdev = rt->u.dst.dev;
1513 +                       ip_rt_put(rt);
1514 +               }
1515 +
1516 +               if (tunnel->parms.proto_type == ETH_P_ETH)
1517 +               {
1518 +                   dev->flags |= IFF_BROADCAST;
1519 +               }
1520 +               else
1521 +               {
1522 +                       dev->flags |= IFF_POINTOPOINT;
1523 +               }
1524 +
1525 +#ifdef CONFIG_NET_IPGRE_BROADCAST
1526 +               if (MULTICAST(iph->daddr)) {
1527 +                       if (!iph->saddr)
1528 +                               return -EINVAL;
1529 +                       dev->flags = IFF_BROADCAST;
1530 +                       dev->hard_header = ipgre_header;
1531 +                       dev->open = ipgre_open;
1532 +                       dev->stop = ipgre_close;
1533 +               }
1534 +#endif
1535 +       }
1536 +
1537 +       if (!tdev && tunnel->parms.link)
1538 +               tdev = __dev_get_by_index(&init_net, tunnel->parms.link);
1539 +
1540 +       if (tdev) {
1541 +               hlen = tdev->hard_header_len;
1542 +               mtu = tdev->mtu;
1543 +       }
1544 +       dev->iflink = tunnel->parms.link;
1545 +
1546 +       /* Precalculate GRE options length */
1547 +       if (tunnel->parms.o_flags&(GRE_CSUM|GRE_KEY|GRE_SEQ)) {
1548 +               if (tunnel->parms.o_flags&GRE_CSUM)
1549 +                       addend += 4;
1550 +               if (tunnel->parms.o_flags&GRE_KEY)
1551 +                       addend += 4;
1552 +               if (tunnel->parms.o_flags&GRE_SEQ)
1553 +                       addend += 4;
1554 +       }
1555 +       dev->hard_header_len = hlen + addend;
1556 +       dev->mtu = mtu - addend;
1557 +       tunnel->hlen = addend;
1558 +       return 0;
1559 +}
1560 +
1561 +static int __init ipgre_fb_tunnel_init(struct net_device *dev)
1562 +{
1563 +       struct ip_tunnel *tunnel = netdev_priv(dev);
1564 +       struct iphdr *iph = &tunnel->parms.iph;
1565 +
1566 +       tunnel->dev = dev;
1567 +       strcpy(tunnel->parms.name, dev->name);
1568 +
1569 +       iph->version            = 4;
1570 +       iph->protocol           = IPPROTO_GRE;
1571 +       iph->ihl                = 5;
1572 +       tunnel->hlen            = sizeof(struct iphdr) + 4;
1573 +
1574 +       dev_hold(dev);
1575 +       tunnels_wc[0]           = tunnel;
1576 +       return 0;
1577 +}
1578 +
1579 +
1580 +static struct net_protocol ipgre_protocol = {
1581 +       .handler        =       ipgre_rcv,
1582 +       .err_handler    =       ipgre_err,
1583 +};
1584 +
1585 +
1586 +/*
1587 + *     And now the modules code and kernel interface.
1588 + */
1589 +
1590 +static int __init ipgre_init(void)
1591 +{
1592 +       int err;
1593 +
1594 +       printk(KERN_INFO "GRE over IPv4 tunneling driver\n");
1595 +
1596 +       if (inet_add_protocol(&ipgre_protocol, IPPROTO_GRE) < 0) {
1597 +               printk(KERN_INFO "ipgre init: can't add protocol\n");
1598 +               return -EAGAIN;
1599 +       }
1600 +
1601 +       ipgre_fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "gre0",
1602 +                                          ipgre_ip_tunnel_setup);
1603 +       if (!ipgre_fb_tunnel_dev) {
1604 +               err = -ENOMEM;
1605 +               goto err1;
1606 +       }
1607 +
1608 +       ipgre_fb_tunnel_dev->init = ipgre_fb_tunnel_init;
1609 +
1610 +       if ((err = register_netdev(ipgre_fb_tunnel_dev)))
1611 +               goto err2;
1612 +out:
1613 +       return err;
1614 +err2:
1615 +       free_netdev(ipgre_fb_tunnel_dev);
1616 +err1:
1617 +       inet_del_protocol(&ipgre_protocol, IPPROTO_GRE);
1618 +       goto out;
1619 +}
1620 +
1621 +static void __exit ipgre_destroy_tunnels(void)
1622 +{
1623 +       int prio;
1624 +
1625 +       for (prio = 0; prio < 4; prio++) {
1626 +               int h;
1627 +               for (h = 0; h < HASH_SIZE; h++) {
1628 +                       struct ip_tunnel *t;
1629 +                       while ((t = tunnels[prio][h]) != NULL)
1630 +                               unregister_netdevice(t->dev);
1631 +               }
1632 +       }
1633 +}
1634 +
1635 +static void __exit ipgre_fini(void)
1636 +{
1637 +       if (inet_del_protocol(&ipgre_protocol, IPPROTO_GRE) < 0)
1638 +               printk(KERN_INFO "ipgre close: can't remove protocol\n");
1639 +
1640 +       rtnl_lock();
1641 +       ipgre_destroy_tunnels();
1642 +       rtnl_unlock();
1643 +}
1644 +
1645 +module_init(ipgre_init);
1646 +module_exit(ipgre_fini);
1647 +MODULE_LICENSE("GPL");
1648 diff -Nurb linux-2.6.22-592/include/linux/if_ether.h linux-2.6.22-593/include/linux/if_ether.h
1649 --- linux-2.6.22-592/include/linux/if_ether.h   2007-07-08 19:32:17.000000000 -0400
1650 +++ linux-2.6.22-593/include/linux/if_ether.h   2008-03-15 10:51:27.000000000 -0400
1651 @@ -56,6 +56,7 @@
1652  #define ETH_P_DIAG      0x6005          /* DEC Diagnostics              */
1653  #define ETH_P_CUST      0x6006          /* DEC Customer use             */
1654  #define ETH_P_SCA       0x6007          /* DEC Systems Comms Arch       */
1655 +#define ETH_P_ETH       0x6558          /* Ethernet in Ethernet         */
1656  #define ETH_P_RARP      0x8035         /* Reverse Addr Res packet      */
1657  #define ETH_P_ATALK    0x809B          /* Appletalk DDP                */
1658  #define ETH_P_AARP     0x80F3          /* Appletalk AARP               */
1659 diff -Nurb linux-2.6.22-592/include/linux/if_tunnel.h linux-2.6.22-593/include/linux/if_tunnel.h
1660 --- linux-2.6.22-592/include/linux/if_tunnel.h  2007-07-08 19:32:17.000000000 -0400
1661 +++ linux-2.6.22-593/include/linux/if_tunnel.h  2008-03-15 10:51:27.000000000 -0400
1662 @@ -25,6 +25,7 @@
1663         __be16                  o_flags;
1664         __be32                  i_key;
1665         __be32                  o_key;
1666 +        __be16                  proto_type;   /*Added*/
1667         struct iphdr            iph;
1668  };
1669  
1670 diff -Nurb linux-2.6.22-592/net/ipv4/ip_gre.c linux-2.6.22-593/net/ipv4/ip_gre.c
1671 --- linux-2.6.22-592/net/ipv4/ip_gre.c  2007-07-08 19:32:17.000000000 -0400
1672 +++ linux-2.6.22-593/net/ipv4/ip_gre.c  2008-03-15 10:51:27.000000000 -0400
1673 @@ -25,6 +25,7 @@
1674  #include <linux/init.h>
1675  #include <linux/in6.h>
1676  #include <linux/inetdevice.h>
1677 +#include <linux/etherdevice.h>   /**XXX added XXX */
1678  #include <linux/igmp.h>
1679  #include <linux/netfilter_ipv4.h>
1680  #include <linux/if_ether.h>
1681 @@ -46,6 +47,8 @@
1682  #include <net/ip6_route.h>
1683  #endif
1684  
1685 +//#define GRE_DEBUG 1
1686 +
1687  /*
1688     Problems & solutions
1689     --------------------
1690 @@ -116,7 +119,8 @@
1691   */
1692  
1693  static int ipgre_tunnel_init(struct net_device *dev);
1694 -static void ipgre_tunnel_setup(struct net_device *dev);
1695 +static void ipgre_ip_tunnel_setup(struct net_device *dev);
1696 +static void ipgre_eth_tunnel_setup(struct net_device *dev);
1697  
1698  /* Fallback tunnel: no source, no destination, no key, no options */
1699  
1700 @@ -243,6 +247,7 @@
1701         __be32 remote = parms->iph.daddr;
1702         __be32 local = parms->iph.saddr;
1703         __be32 key = parms->i_key;
1704 +       __be16 proto = parms->proto_type;
1705         struct ip_tunnel *t, **tp, *nt;
1706         struct net_device *dev;
1707         char name[IFNAMSIZ];
1708 @@ -256,6 +261,8 @@
1709         if (!create)
1710                 return NULL;
1711  
1712 +       printk(KERN_CRIT "Adding tunnel %s with key %d\n", parms->name, ntohl(key));
1713 +
1714         if (parms->name[0])
1715                 strlcpy(name, parms->name, IFNAMSIZ);
1716         else {
1717 @@ -269,7 +276,20 @@
1718                         goto failed;
1719         }
1720  
1721 -       dev = alloc_netdev(sizeof(*t), name, ipgre_tunnel_setup);
1722 +       /* Tunnel creation: check payload type and call appropriate
1723 +        * function */
1724 +       switch (proto)
1725 +       {
1726 +           case ETH_P_IP:
1727 +               dev = alloc_netdev(sizeof(*t), name, ipgre_ip_tunnel_setup);
1728 +               break;
1729 +           case ETH_P_ETH:
1730 +               dev = alloc_netdev(sizeof(*t), name, ipgre_eth_tunnel_setup);
1731 +               break;
1732 +           default:
1733 +               return NULL;
1734 +       }
1735 +
1736         if (!dev)
1737           return NULL;
1738  
1739 @@ -558,6 +578,7 @@
1740         u32    seqno = 0;
1741         struct ip_tunnel *tunnel;
1742         int    offset = 4;
1743 +       __be16 proto;
1744  
1745         if (!pskb_may_pull(skb, 16))
1746                 goto drop_nolock;
1747 @@ -566,6 +587,11 @@
1748         h = skb->data;
1749         flags = *(__be16*)h;
1750  
1751 +#ifdef GRE_DEBUG
1752 +       printk(KERN_DEBUG "gre.c [601] src:%x dst:%x  proto:%d %p", iph->saddr, iph->daddr, iph->protocol, skb->data);
1753 +#endif 
1754 +       proto = ntohs(*(__be16*)(h+2)); /* XXX added XXX */
1755 +       
1756         if (flags&(GRE_CSUM|GRE_KEY|GRE_ROUTING|GRE_SEQ|GRE_VERSION)) {
1757                 /* - Version must be 0.
1758                    - We do not support routing headers.
1759 @@ -617,6 +643,27 @@
1760                 __pskb_pull(skb, offset);
1761                 skb_reset_network_header(skb);
1762                 skb_postpull_rcsum(skb, skb_transport_header(skb), offset);
1763 +               if(proto == ETH_P_ETH)
1764 +                 {
1765 +#ifdef GRE_DEBUG
1766 +                   unsigned char* tmp_hdr = skb->data;
1767 +                   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);
1768 +#endif             
1769 +                   skb->protocol = eth_type_trans(skb, tunnel->dev);
1770 +
1771 +                   /* XXX added these lines to make arp work? XXX */
1772 +                   /*skb->mac.raw = skb->data;*/
1773 +                   skb->network_header = skb->network_header + ETH_HLEN;
1774 +                   /* XXX added these lines to make arp work? XXX */
1775 +
1776 +#ifdef GRE_DEBUG
1777 +                   tmp_hdr = skb->data;
1778 +                   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);
1779 +                   printk(KERN_ALERT "gre.c [671] received ethernet on gre %x\n",skb->protocol); 
1780 +#endif
1781 +                   memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
1782 +                 }
1783 +               else
1784                 skb->pkt_type = PACKET_HOST;
1785  #ifdef CONFIG_NET_IPGRE_BROADCAST
1786                 if (MULTICAST(iph->daddr)) {
1787 @@ -663,7 +710,7 @@
1788         return(0);
1789  }
1790  
1791 -static int ipgre_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
1792 +static int ipgre_ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
1793  {
1794         struct ip_tunnel *tunnel = netdev_priv(dev);
1795         struct net_device_stats *stats = &tunnel->stat;
1796 @@ -895,6 +942,228 @@
1797         return 0;
1798  }
1799  
1800 +static int ipgre_eth_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
1801 +{
1802 +       struct ip_tunnel *tunnel = netdev_priv(dev);
1803 +       struct net_device_stats *stats = &tunnel->stat;
1804 +       struct iphdr *old_iph = ip_hdr(skb);
1805 +       struct iphdr *tiph = &tunnel->parms.iph;
1806 +       u8     tos;
1807 +       __be16 df;
1808 +       struct rtable *rt;              /* Route to the other host */
1809 +       struct net_device *tdev;        /* Device to other host */
1810 +       int    gre_hlen = tunnel->hlen; /* XXX changed XXX*/
1811 +       //struct etheriphdr  *ethiph;
1812 +       struct iphdr  *iph;             /* Our new IP header */
1813 +       int    max_headroom;            /* The extra header space needed */
1814 +       int    mtu;
1815 +
1816 +#ifdef GRE_DEBUG
1817 +       printk(KERN_ALERT "gre.c:972 Starting xmit\n");
1818 +#endif
1819 +
1820 +       if (tunnel->recursion++) {
1821 +               stats->collisions++;
1822 +               goto tx_error;
1823 +       }
1824 +
1825 +       /* Need valid non-multicast daddr.  */
1826 +       if (tiph->daddr == 0 || MULTICAST(tiph->daddr))
1827 +               goto tx_error;
1828 +
1829 +       tos = tiph->tos;
1830 +       if (tos&1) {
1831 +               if (skb->protocol == htons(ETH_P_IP))
1832 +                       tos = old_iph->tos;
1833 +               tos &= ~1;
1834 +       }
1835 +#ifdef GRE_DEBUG
1836 +       printk(KERN_ALERT "gre.c:991 Passed tos assignment.\n");
1837 +#endif
1838 +
1839 +
1840 +       {
1841 +               struct flowi fl = { .fl_net = &init_net,
1842 +                                   .oif = tunnel->parms.link,
1843 +                                   .nl_u = { .ip4_u =
1844 +                                             { .daddr = tiph->daddr,
1845 +                                               .saddr = tiph->saddr,
1846 +                                               .tos = RT_TOS(tos) } },
1847 +                                   .proto = IPPROTO_GRE };
1848 +               if (ip_route_output_key(&rt, &fl)) {
1849 +                       stats->tx_carrier_errors++;
1850 +                       goto tx_error_icmp;
1851 +               }
1852 +       }
1853 +       tdev = rt->u.dst.dev;
1854 +#ifdef GRE_DEBUG
1855 +       printk(KERN_ALERT "gre.c:1006 Passed the route retrieval\n");
1856 +#endif
1857 +       if (tdev == dev) {
1858 +               ip_rt_put(rt);
1859 +               stats->collisions++;
1860 +               goto tx_error;
1861 +       }
1862 +#ifdef GRE_DEBUG
1863 +       printk(KERN_ALERT "gre.c:1018 Passed tdev collision check.\n");
1864 +#endif
1865 +
1866 +       /* Check MTU stuff if kernel panic */
1867 +       df = tiph->frag_off;
1868 +       if (df)
1869 +               mtu = dst_mtu(&rt->u.dst) - tunnel->hlen;
1870 +       else
1871 +               mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu;
1872 +/*
1873 +       if (skb->dst)
1874 +               skb->dst->ops->update_pmtu(skb->dst, mtu);
1875 +        XXX */
1876 +#ifdef GRE_DEBUG
1877 +       printk(KERN_ALERT "gre.c:1032 Passed the pmtu setting.\n");
1878 +#endif
1879 +
1880 +       if (skb->protocol == htons(ETH_P_IP)) {
1881 +               df |= (old_iph->frag_off&htons(IP_DF));
1882 +
1883 +               if ((old_iph->frag_off & htons(IP_DF)) &&
1884 +                   mtu < ntohs(old_iph->tot_len)) {
1885 +                       icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
1886 +                       ip_rt_put(rt);
1887 +                       goto tx_error;
1888 +               }
1889 +       }
1890 +#ifdef CONFIG_IPV6
1891 +       else if (skb->protocol == htons(ETH_P_IPV6)) {
1892 +               struct rt6_info *rt6 = (struct rt6_info*)skb->dst;
1893 +
1894 +               if (rt6 && mtu < dst_mtu(skb->dst) && mtu >= IPV6_MIN_MTU) {
1895 +                       if (tiph->daddr || rt6->rt6i_dst.plen == 128) {
1896 +                               rt6->rt6i_flags |= RTF_MODIFIED;
1897 +                               skb->dst->metrics[RTAX_MTU-1] = mtu;
1898 +                       }
1899 +               }
1900 +
1901 +               /* @@@ Is this correct?  */
1902 +               if (mtu >= IPV6_MIN_MTU && mtu < skb->len - tunnel->hlen + gre_hlen) {
1903 +                       icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
1904 +                       ip_rt_put(rt);
1905 +                       goto tx_error;
1906 +               }
1907 +       }
1908 +#endif
1909 +#ifdef GRE_DEBUG
1910 +       printk(KERN_ALERT "gre.c:1065 Passed the fragmentation check.\n");
1911 +#endif
1912 +
1913 +       if (tunnel->err_count > 0) {
1914 +               if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
1915 +                       tunnel->err_count--;
1916 +                       dst_link_failure(skb);
1917 +               } else
1918 +                       tunnel->err_count = 0;
1919 +       }
1920 +
1921 +       max_headroom = LL_RESERVED_SPACE(tdev) + gre_hlen;
1922 +
1923 +       if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) {
1924 +               struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
1925 +               if (!new_skb) {
1926 +                       ip_rt_put(rt);
1927 +                       stats->tx_dropped++;
1928 +                       dev_kfree_skb(skb);
1929 +                       tunnel->recursion--;
1930 +                       return 0;
1931 +               }
1932 +               if (skb->sk)
1933 +                       skb_set_owner_w(new_skb, skb->sk);
1934 +               dev_kfree_skb(skb);
1935 +               skb = new_skb;
1936 +               old_iph = ip_hdr(skb);
1937 +       }
1938 +#ifdef GRE_DEBUG
1939 +       printk(KERN_ALERT "gre.c:1094 Passed the headroom calculation\n");
1940 +#endif
1941 +
1942 +       skb->transport_header = skb->data;
1943 +       skb_push(skb, gre_hlen);
1944 +       skb_reset_network_header(skb);
1945 +       memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1946 +       dst_release(skb->dst);
1947 +       skb->dst = &rt->u.dst;
1948 +
1949 +       /*
1950 +        *      Push down and install the etherip header.
1951 +        */
1952 +
1953 +       iph                     =       ip_hdr(skb);
1954 +       iph->version            =       4;
1955 +       iph->ihl                =       sizeof(struct iphdr) >> 2;
1956 +       iph->frag_off           =       df;
1957 +       iph->protocol           =       IPPROTO_GRE;
1958 +       iph->tos                =       ipgre_ecn_encapsulate(tos, old_iph, skb);
1959 +       iph->daddr              =       rt->rt_dst;
1960 +       iph->saddr              =       rt->rt_src;
1961 +
1962 +/*     ethiph->version         =       htons(ETHERIP_VERSION); */
1963 +#ifdef GRE_DEBUG
1964 +       printk(KERN_ALERT "gre.c:1121 Passed outer IP header construction.\n");
1965 +#endif
1966 +
1967 +       if ((iph->ttl = tiph->ttl) == 0) {
1968 +               if (skb->protocol == htons(ETH_P_IP))
1969 +                       iph->ttl = old_iph->ttl;
1970 +#ifdef CONFIG_IPV6
1971 +               else if (skb->protocol == htons(ETH_P_IPV6))
1972 +                       iph->ttl = ((struct ipv6hdr*)old_iph)->hop_limit;
1973 +#endif
1974 +               else
1975 +                       iph->ttl = dst_metric(&rt->u.dst, RTAX_HOPLIMIT);
1976 +       }
1977 +#ifdef GRE_DEBUG
1978 +       printk(KERN_ALERT "gre.c:1006 Passed the TTL check.\n");
1979 +#endif
1980 +
1981 +       ((__be16*)(iph+1))[0] = tunnel->parms.o_flags;
1982 +       ((__be16*)(iph+1))[1] = htons(tunnel->parms.proto_type);
1983 +
1984 +       if (tunnel->parms.o_flags&(GRE_KEY|GRE_CSUM|GRE_SEQ)) {
1985 +               __be32 *ptr = (__be32*)(((u8*)iph) + tunnel->hlen - 4);
1986 +
1987 +               if (tunnel->parms.o_flags&GRE_SEQ) {
1988 +                       ++tunnel->o_seqno;
1989 +                       *ptr = htonl(tunnel->o_seqno);
1990 +                       ptr--;
1991 +               }
1992 +               if (tunnel->parms.o_flags&GRE_KEY) {
1993 +                       *ptr = tunnel->parms.o_key;
1994 +                       ptr--;
1995 +               }
1996 +               if (tunnel->parms.o_flags&GRE_CSUM) {
1997 +                       *ptr = 0;
1998 +                       *(__sum16*)ptr = ip_compute_csum((void*)(iph+1), skb->len - sizeof(struct iphdr));
1999 +               }
2000 +       }
2001 +#ifdef GRE_DEBUG
2002 +       printk(KERN_ALERT "gre.c:1006 Passed the tunnel transmit.\n");
2003 +#endif
2004 +
2005 +       nf_reset(skb);
2006 +
2007 +       IPTUNNEL_XMIT();
2008 +       tunnel->recursion--;
2009 +       return 0;
2010 +
2011 +tx_error_icmp:
2012 +       dst_link_failure(skb);
2013 +
2014 +tx_error:
2015 +       stats->tx_errors++;
2016 +       dev_kfree_skb(skb);
2017 +       tunnel->recursion--;
2018 +       return 0;
2019 +}
2020 +
2021 +
2022  static int
2023  ipgre_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
2024  {
2025 @@ -902,6 +1171,8 @@
2026         struct ip_tunnel_parm p;
2027         struct ip_tunnel *t;
2028  
2029 +        printk(KERN_ALERT "1174 GRE: entering gre ioctl. command is: %d\n", cmd);
2030 +
2031         switch (cmd) {
2032         case SIOCGETTUNNEL:
2033                 t = NULL;
2034 @@ -943,7 +1214,7 @@
2035                         p.o_key = 0;
2036  
2037                 t = ipgre_tunnel_locate(&p, cmd == SIOCADDTUNNEL);
2038 -
2039 +               if (t) printk(KERN_ALERT "1174 GRE: proto %s %x\n", p.name, p.proto_type);
2040                 if (dev != ipgre_fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
2041                         if (t != NULL) {
2042                                 if (t->dev != dev) {
2043 @@ -960,6 +1231,12 @@
2044                                 else if (p.iph.daddr)
2045                                         nflags = IFF_POINTOPOINT;
2046  
2047 +                               /* XXX:Set back IFF_BROADCAST if
2048 +                                * transporting ethernet */
2049 +                               printk(KERN_ALERT "1193 GRE: proto %s %d\n", p.name, p.proto_type);
2050 +                               if (p.proto_type == ETH_P_ETH)
2051 +                                       nflags = IFF_BROADCAST;
2052 +
2053                                 if ((dev->flags^nflags)&(IFF_POINTOPOINT|IFF_BROADCAST)) {
2054                                         err = -EINVAL;
2055                                         break;
2056 @@ -969,8 +1246,12 @@
2057                                 t->parms.iph.daddr = p.iph.daddr;
2058                                 t->parms.i_key = p.i_key;
2059                                 t->parms.o_key = p.o_key;
2060 +                               /* XXX:Copy in the protocol field */
2061 +                               t->parms.proto_type = p.proto_type;
2062 +                               if (t->parms.proto_type != ETH_P_ETH) {
2063                                 memcpy(dev->dev_addr, &p.iph.saddr, 4);
2064                                 memcpy(dev->broadcast, &p.iph.daddr, 4);
2065 +                               }
2066                                 ipgre_tunnel_link(t);
2067                                 netdev_state_change(dev);
2068                         }
2069 @@ -1129,12 +1410,12 @@
2070  
2071  #endif
2072  
2073 -static void ipgre_tunnel_setup(struct net_device *dev)
2074 +static void ipgre_ip_tunnel_setup(struct net_device *dev)
2075  {
2076         SET_MODULE_OWNER(dev);
2077         dev->uninit             = ipgre_tunnel_uninit;
2078         dev->destructor         = free_netdev;
2079 -       dev->hard_start_xmit    = ipgre_tunnel_xmit;
2080 +       dev->hard_start_xmit    = ipgre_ip_tunnel_xmit;
2081         dev->get_stats          = ipgre_tunnel_get_stats;
2082         dev->do_ioctl           = ipgre_tunnel_ioctl;
2083         dev->change_mtu         = ipgre_tunnel_change_mtu;
2084 @@ -1147,6 +1428,35 @@
2085         dev->addr_len           = 4;
2086  }
2087  
2088 +/* Tunnel setup for ipgre_eth */
2089 +static void ipgre_eth_tunnel_setup(struct net_device *dev)
2090 +{
2091 +       SET_MODULE_OWNER(dev);
2092 +
2093 +       // Set default values for Ethernet device
2094 +       ether_setup(dev);
2095 +
2096 +       dev->uninit             = ipgre_tunnel_uninit;
2097 +       dev->destructor         = free_netdev;
2098 +       dev->hard_start_xmit    = ipgre_eth_tunnel_xmit;
2099 +       dev->get_stats          = ipgre_tunnel_get_stats;
2100 +       dev->do_ioctl           = ipgre_tunnel_ioctl;
2101 +       dev->change_mtu         = ipgre_tunnel_change_mtu;
2102 +
2103 +       dev->hard_header_len    = LL_MAX_HEADER + ETH_HLEN + sizeof(struct iphdr) + 4;
2104 +       dev->mtu                = ETH_DATA_LEN - ETH_HLEN - sizeof(struct iphdr) - 4;
2105 +       dev->tx_queue_len       = 0;
2106 +       dev->iflink             = 0;
2107 +
2108 +       random_ether_addr(dev->dev_addr);
2109 +
2110 +#ifdef GRE_DEBUG
2111 +       { unsigned char* d = dev->dev_addr;
2112 +       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]); }
2113 +#endif 
2114 +}
2115 +
2116 +
2117  static int ipgre_tunnel_init(struct net_device *dev)
2118  {
2119         struct net_device *tdev = NULL;
2120 @@ -1162,8 +1472,12 @@
2121         tunnel->dev = dev;
2122         strcpy(tunnel->parms.name, dev->name);
2123  
2124 +       if (tunnel->parms.proto_type != ETH_P_ETH) {
2125         memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
2126         memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
2127 +       } else {
2128 +               addend += ETH_HLEN;
2129 +       }
2130  
2131         /* Guess output device to choose reasonable mtu and hard_header_len */
2132  
2133 @@ -1180,7 +1494,14 @@
2134                         ip_rt_put(rt);
2135                 }
2136  
2137 +               if (tunnel->parms.proto_type == ETH_P_ETH)
2138 +               {
2139 +                   dev->flags |= IFF_BROADCAST;
2140 +               }
2141 +               else
2142 +               {
2143                 dev->flags |= IFF_POINTOPOINT;
2144 +               }
2145  
2146  #ifdef CONFIG_NET_IPGRE_BROADCAST
2147                 if (MULTICAST(iph->daddr)) {
2148 @@ -1259,7 +1580,7 @@
2149         }
2150  
2151         ipgre_fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "gre0",
2152 -                                          ipgre_tunnel_setup);
2153 +                                          ipgre_ip_tunnel_setup);
2154         if (!ipgre_fb_tunnel_dev) {
2155                 err = -ENOMEM;
2156                 goto err1;
2157 diff -Nurb linux-2.6.22-592/net/ipv4/ip_gre.c.orig linux-2.6.22-593/net/ipv4/ip_gre.c.orig
2158 --- linux-2.6.22-592/net/ipv4/ip_gre.c.orig     1969-12-31 19:00:00.000000000 -0500
2159 +++ linux-2.6.22-593/net/ipv4/ip_gre.c.orig     2007-07-08 19:32:17.000000000 -0400
2160 @@ -0,0 +1,1307 @@
2161 +/*
2162 + *     Linux NET3:     GRE over IP protocol decoder.
2163 + *
2164 + *     Authors: Alexey Kuznetsov (kuznet@ms2.inr.ac.ru)
2165 + *
2166 + *     This program is free software; you can redistribute it and/or
2167 + *     modify it under the terms of the GNU General Public License
2168 + *     as published by the Free Software Foundation; either version
2169 + *     2 of the License, or (at your option) any later version.
2170 + *
2171 + */
2172 +
2173 +#include <linux/capability.h>
2174 +#include <linux/module.h>
2175 +#include <linux/types.h>
2176 +#include <linux/kernel.h>
2177 +#include <asm/uaccess.h>
2178 +#include <linux/skbuff.h>
2179 +#include <linux/netdevice.h>
2180 +#include <linux/in.h>
2181 +#include <linux/tcp.h>
2182 +#include <linux/udp.h>
2183 +#include <linux/if_arp.h>
2184 +#include <linux/mroute.h>
2185 +#include <linux/init.h>
2186 +#include <linux/in6.h>
2187 +#include <linux/inetdevice.h>
2188 +#include <linux/igmp.h>
2189 +#include <linux/netfilter_ipv4.h>
2190 +#include <linux/if_ether.h>
2191 +
2192 +#include <net/sock.h>
2193 +#include <net/ip.h>
2194 +#include <net/icmp.h>
2195 +#include <net/protocol.h>
2196 +#include <net/ipip.h>
2197 +#include <net/arp.h>
2198 +#include <net/checksum.h>
2199 +#include <net/dsfield.h>
2200 +#include <net/inet_ecn.h>
2201 +#include <net/xfrm.h>
2202 +
2203 +#ifdef CONFIG_IPV6
2204 +#include <net/ipv6.h>
2205 +#include <net/ip6_fib.h>
2206 +#include <net/ip6_route.h>
2207 +#endif
2208 +
2209 +/*
2210 +   Problems & solutions
2211 +   --------------------
2212 +
2213 +   1. The most important issue is detecting local dead loops.
2214 +   They would cause complete host lockup in transmit, which
2215 +   would be "resolved" by stack overflow or, if queueing is enabled,
2216 +   with infinite looping in net_bh.
2217 +
2218 +   We cannot track such dead loops during route installation,
2219 +   it is infeasible task. The most general solutions would be
2220 +   to keep skb->encapsulation counter (sort of local ttl),
2221 +   and silently drop packet when it expires. It is the best
2222 +   solution, but it supposes maintaing new variable in ALL
2223 +   skb, even if no tunneling is used.
2224 +
2225 +   Current solution: t->recursion lock breaks dead loops. It looks
2226 +   like dev->tbusy flag, but I preferred new variable, because
2227 +   the semantics is different. One day, when hard_start_xmit
2228 +   will be multithreaded we will have to use skb->encapsulation.
2229 +
2230 +
2231 +
2232 +   2. Networking dead loops would not kill routers, but would really
2233 +   kill network. IP hop limit plays role of "t->recursion" in this case,
2234 +   if we copy it from packet being encapsulated to upper header.
2235 +   It is very good solution, but it introduces two problems:
2236 +
2237 +   - Routing protocols, using packets with ttl=1 (OSPF, RIP2),
2238 +     do not work over tunnels.
2239 +   - traceroute does not work. I planned to relay ICMP from tunnel,
2240 +     so that this problem would be solved and traceroute output
2241 +     would even more informative. This idea appeared to be wrong:
2242 +     only Linux complies to rfc1812 now (yes, guys, Linux is the only
2243 +     true router now :-)), all routers (at least, in neighbourhood of mine)
2244 +     return only 8 bytes of payload. It is the end.
2245 +
2246 +   Hence, if we want that OSPF worked or traceroute said something reasonable,
2247 +   we should search for another solution.
2248 +
2249 +   One of them is to parse packet trying to detect inner encapsulation
2250 +   made by our node. It is difficult or even impossible, especially,
2251 +   taking into account fragmentation. TO be short, tt is not solution at all.
2252 +
2253 +   Current solution: The solution was UNEXPECTEDLY SIMPLE.
2254 +   We force DF flag on tunnels with preconfigured hop limit,
2255 +   that is ALL. :-) Well, it does not remove the problem completely,
2256 +   but exponential growth of network traffic is changed to linear
2257 +   (branches, that exceed pmtu are pruned) and tunnel mtu
2258 +   fastly degrades to value <68, where looping stops.
2259 +   Yes, it is not good if there exists a router in the loop,
2260 +   which does not force DF, even when encapsulating packets have DF set.
2261 +   But it is not our problem! Nobody could accuse us, we made
2262 +   all that we could make. Even if it is your gated who injected
2263 +   fatal route to network, even if it were you who configured
2264 +   fatal static route: you are innocent. :-)
2265 +
2266 +
2267 +
2268 +   3. Really, ipv4/ipip.c, ipv4/ip_gre.c and ipv6/sit.c contain
2269 +   practically identical code. It would be good to glue them
2270 +   together, but it is not very evident, how to make them modular.
2271 +   sit is integral part of IPv6, ipip and gre are naturally modular.
2272 +   We could extract common parts (hash table, ioctl etc)
2273 +   to a separate module (ip_tunnel.c).
2274 +
2275 +   Alexey Kuznetsov.
2276 + */
2277 +
2278 +static int ipgre_tunnel_init(struct net_device *dev);
2279 +static void ipgre_tunnel_setup(struct net_device *dev);
2280 +
2281 +/* Fallback tunnel: no source, no destination, no key, no options */
2282 +
2283 +static int ipgre_fb_tunnel_init(struct net_device *dev);
2284 +
2285 +static struct net_device *ipgre_fb_tunnel_dev;
2286 +
2287 +/* Tunnel hash table */
2288 +
2289 +/*
2290 +   4 hash tables:
2291 +
2292 +   3: (remote,local)
2293 +   2: (remote,*)
2294 +   1: (*,local)
2295 +   0: (*,*)
2296 +
2297 +   We require exact key match i.e. if a key is present in packet
2298 +   it will match only tunnel with the same key; if it is not present,
2299 +   it will match only keyless tunnel.
2300 +
2301 +   All keysless packets, if not matched configured keyless tunnels
2302 +   will match fallback tunnel.
2303 + */
2304 +
2305 +#define HASH_SIZE  16
2306 +#define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
2307 +
2308 +static struct ip_tunnel *tunnels[4][HASH_SIZE];
2309 +
2310 +#define tunnels_r_l    (tunnels[3])
2311 +#define tunnels_r      (tunnels[2])
2312 +#define tunnels_l      (tunnels[1])
2313 +#define tunnels_wc     (tunnels[0])
2314 +
2315 +static DEFINE_RWLOCK(ipgre_lock);
2316 +
2317 +/* Given src, dst and key, find appropriate for input tunnel. */
2318 +
2319 +static struct ip_tunnel * ipgre_tunnel_lookup(__be32 remote, __be32 local, __be32 key)
2320 +{
2321 +       unsigned h0 = HASH(remote);
2322 +       unsigned h1 = HASH(key);
2323 +       struct ip_tunnel *t;
2324 +
2325 +       for (t = tunnels_r_l[h0^h1]; t; t = t->next) {
2326 +               if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr) {
2327 +                       if (t->parms.i_key == key && (t->dev->flags&IFF_UP))
2328 +                               return t;
2329 +               }
2330 +       }
2331 +       for (t = tunnels_r[h0^h1]; t; t = t->next) {
2332 +               if (remote == t->parms.iph.daddr) {
2333 +                       if (t->parms.i_key == key && (t->dev->flags&IFF_UP))
2334 +                               return t;
2335 +               }
2336 +       }
2337 +       for (t = tunnels_l[h1]; t; t = t->next) {
2338 +               if (local == t->parms.iph.saddr ||
2339 +                    (local == t->parms.iph.daddr && MULTICAST(local))) {
2340 +                       if (t->parms.i_key == key && (t->dev->flags&IFF_UP))
2341 +                               return t;
2342 +               }
2343 +       }
2344 +       for (t = tunnels_wc[h1]; t; t = t->next) {
2345 +               if (t->parms.i_key == key && (t->dev->flags&IFF_UP))
2346 +                       return t;
2347 +       }
2348 +
2349 +       if (ipgre_fb_tunnel_dev->flags&IFF_UP)
2350 +               return netdev_priv(ipgre_fb_tunnel_dev);
2351 +       return NULL;
2352 +}
2353 +
2354 +static struct ip_tunnel **__ipgre_bucket(struct ip_tunnel_parm *parms)
2355 +{
2356 +       __be32 remote = parms->iph.daddr;
2357 +       __be32 local = parms->iph.saddr;
2358 +       __be32 key = parms->i_key;
2359 +       unsigned h = HASH(key);
2360 +       int prio = 0;
2361 +
2362 +       if (local)
2363 +               prio |= 1;
2364 +       if (remote && !MULTICAST(remote)) {
2365 +               prio |= 2;
2366 +               h ^= HASH(remote);
2367 +       }
2368 +
2369 +       return &tunnels[prio][h];
2370 +}
2371 +
2372 +static inline struct ip_tunnel **ipgre_bucket(struct ip_tunnel *t)
2373 +{
2374 +       return __ipgre_bucket(&t->parms);
2375 +}
2376 +
2377 +static void ipgre_tunnel_link(struct ip_tunnel *t)
2378 +{
2379 +       struct ip_tunnel **tp = ipgre_bucket(t);
2380 +
2381 +       t->next = *tp;
2382 +       write_lock_bh(&ipgre_lock);
2383 +       *tp = t;
2384 +       write_unlock_bh(&ipgre_lock);
2385 +}
2386 +
2387 +static void ipgre_tunnel_unlink(struct ip_tunnel *t)
2388 +{
2389 +       struct ip_tunnel **tp;
2390 +
2391 +       for (tp = ipgre_bucket(t); *tp; tp = &(*tp)->next) {
2392 +               if (t == *tp) {
2393 +                       write_lock_bh(&ipgre_lock);
2394 +                       *tp = t->next;
2395 +                       write_unlock_bh(&ipgre_lock);
2396 +                       break;
2397 +               }
2398 +       }
2399 +}
2400 +
2401 +static struct ip_tunnel * ipgre_tunnel_locate(struct ip_tunnel_parm *parms, int create)
2402 +{
2403 +       __be32 remote = parms->iph.daddr;
2404 +       __be32 local = parms->iph.saddr;
2405 +       __be32 key = parms->i_key;
2406 +       struct ip_tunnel *t, **tp, *nt;
2407 +       struct net_device *dev;
2408 +       char name[IFNAMSIZ];
2409 +
2410 +       for (tp = __ipgre_bucket(parms); (t = *tp) != NULL; tp = &t->next) {
2411 +               if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr) {
2412 +                       if (key == t->parms.i_key)
2413 +                               return t;
2414 +               }
2415 +       }
2416 +       if (!create)
2417 +               return NULL;
2418 +
2419 +       if (parms->name[0])
2420 +               strlcpy(name, parms->name, IFNAMSIZ);
2421 +       else {
2422 +               int i;
2423 +               for (i=1; i<100; i++) {
2424 +                       sprintf(name, "gre%d", i);
2425 +                       if (__dev_get_by_name(name) == NULL)
2426 +                               break;
2427 +               }
2428 +               if (i==100)
2429 +                       goto failed;
2430 +       }
2431 +
2432 +       dev = alloc_netdev(sizeof(*t), name, ipgre_tunnel_setup);
2433 +       if (!dev)
2434 +         return NULL;
2435 +
2436 +       dev->init = ipgre_tunnel_init;
2437 +       nt = netdev_priv(dev);
2438 +       nt->parms = *parms;
2439 +
2440 +       if (register_netdevice(dev) < 0) {
2441 +               free_netdev(dev);
2442 +               goto failed;
2443 +       }
2444 +
2445 +       dev_hold(dev);
2446 +       ipgre_tunnel_link(nt);
2447 +       return nt;
2448 +
2449 +failed:
2450 +       return NULL;
2451 +}
2452 +
2453 +static void ipgre_tunnel_uninit(struct net_device *dev)
2454 +{
2455 +       ipgre_tunnel_unlink(netdev_priv(dev));
2456 +       dev_put(dev);
2457 +}
2458 +
2459 +
2460 +static void ipgre_err(struct sk_buff *skb, u32 info)
2461 +{
2462 +#ifndef I_WISH_WORLD_WERE_PERFECT
2463 +
2464 +/* It is not :-( All the routers (except for Linux) return only
2465 +   8 bytes of packet payload. It means, that precise relaying of
2466 +   ICMP in the real Internet is absolutely infeasible.
2467 +
2468 +   Moreover, Cisco "wise men" put GRE key to the third word
2469 +   in GRE header. It makes impossible maintaining even soft state for keyed
2470 +   GRE tunnels with enabled checksum. Tell them "thank you".
2471 +
2472 +   Well, I wonder, rfc1812 was written by Cisco employee,
2473 +   what the hell these idiots break standrads established
2474 +   by themself???
2475 + */
2476 +
2477 +       struct iphdr *iph = (struct iphdr*)skb->data;
2478 +       __be16       *p = (__be16*)(skb->data+(iph->ihl<<2));
2479 +       int grehlen = (iph->ihl<<2) + 4;
2480 +       const int type = icmp_hdr(skb)->type;
2481 +       const int code = icmp_hdr(skb)->code;
2482 +       struct ip_tunnel *t;
2483 +       __be16 flags;
2484 +
2485 +       flags = p[0];
2486 +       if (flags&(GRE_CSUM|GRE_KEY|GRE_SEQ|GRE_ROUTING|GRE_VERSION)) {
2487 +               if (flags&(GRE_VERSION|GRE_ROUTING))
2488 +                       return;
2489 +               if (flags&GRE_KEY) {
2490 +                       grehlen += 4;
2491 +                       if (flags&GRE_CSUM)
2492 +                               grehlen += 4;
2493 +               }
2494 +       }
2495 +
2496 +       /* If only 8 bytes returned, keyed message will be dropped here */
2497 +       if (skb_headlen(skb) < grehlen)
2498 +               return;
2499 +
2500 +       switch (type) {
2501 +       default:
2502 +       case ICMP_PARAMETERPROB:
2503 +               return;
2504 +
2505 +       case ICMP_DEST_UNREACH:
2506 +               switch (code) {
2507 +               case ICMP_SR_FAILED:
2508 +               case ICMP_PORT_UNREACH:
2509 +                       /* Impossible event. */
2510 +                       return;
2511 +               case ICMP_FRAG_NEEDED:
2512 +                       /* Soft state for pmtu is maintained by IP core. */
2513 +                       return;
2514 +               default:
2515 +                       /* All others are translated to HOST_UNREACH.
2516 +                          rfc2003 contains "deep thoughts" about NET_UNREACH,
2517 +                          I believe they are just ether pollution. --ANK
2518 +                        */
2519 +                       break;
2520 +               }
2521 +               break;
2522 +       case ICMP_TIME_EXCEEDED:
2523 +               if (code != ICMP_EXC_TTL)
2524 +                       return;
2525 +               break;
2526 +       }
2527 +
2528 +       read_lock(&ipgre_lock);
2529 +       t = ipgre_tunnel_lookup(iph->daddr, iph->saddr, (flags&GRE_KEY) ? *(((__be32*)p) + (grehlen>>2) - 1) : 0);
2530 +       if (t == NULL || t->parms.iph.daddr == 0 || MULTICAST(t->parms.iph.daddr))
2531 +               goto out;
2532 +
2533 +       if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
2534 +               goto out;
2535 +
2536 +       if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO)
2537 +               t->err_count++;
2538 +       else
2539 +               t->err_count = 1;
2540 +       t->err_time = jiffies;
2541 +out:
2542 +       read_unlock(&ipgre_lock);
2543 +       return;
2544 +#else
2545 +       struct iphdr *iph = (struct iphdr*)dp;
2546 +       struct iphdr *eiph;
2547 +       __be16       *p = (__be16*)(dp+(iph->ihl<<2));
2548 +       const int type = icmp_hdr(skb)->type;
2549 +       const int code = icmp_hdr(skb)->code;
2550 +       int rel_type = 0;
2551 +       int rel_code = 0;
2552 +       __be32 rel_info = 0;
2553 +       __u32 n = 0;
2554 +       __be16 flags;
2555 +       int grehlen = (iph->ihl<<2) + 4;
2556 +       struct sk_buff *skb2;
2557 +       struct flowi fl;
2558 +       struct rtable *rt;
2559 +
2560 +       if (p[1] != htons(ETH_P_IP))
2561 +               return;
2562 +
2563 +       flags = p[0];
2564 +       if (flags&(GRE_CSUM|GRE_KEY|GRE_SEQ|GRE_ROUTING|GRE_VERSION)) {
2565 +               if (flags&(GRE_VERSION|GRE_ROUTING))
2566 +                       return;
2567 +               if (flags&GRE_CSUM)
2568 +                       grehlen += 4;
2569 +               if (flags&GRE_KEY)
2570 +                       grehlen += 4;
2571 +               if (flags&GRE_SEQ)
2572 +                       grehlen += 4;
2573 +       }
2574 +       if (len < grehlen + sizeof(struct iphdr))
2575 +               return;
2576 +       eiph = (struct iphdr*)(dp + grehlen);
2577 +
2578 +       switch (type) {
2579 +       default:
2580 +               return;
2581 +       case ICMP_PARAMETERPROB:
2582 +               n = ntohl(icmp_hdr(skb)->un.gateway) >> 24;
2583 +               if (n < (iph->ihl<<2))
2584 +                       return;
2585 +
2586 +               /* So... This guy found something strange INSIDE encapsulated
2587 +                  packet. Well, he is fool, but what can we do ?
2588 +                */
2589 +               rel_type = ICMP_PARAMETERPROB;
2590 +               n -= grehlen;
2591 +               rel_info = htonl(n << 24);
2592 +               break;
2593 +
2594 +       case ICMP_DEST_UNREACH:
2595 +               switch (code) {
2596 +               case ICMP_SR_FAILED:
2597 +               case ICMP_PORT_UNREACH:
2598 +                       /* Impossible event. */
2599 +                       return;
2600 +               case ICMP_FRAG_NEEDED:
2601 +                       /* And it is the only really necessary thing :-) */
2602 +                       n = ntohs(icmp_hdr(skb)->un.frag.mtu);
2603 +                       if (n < grehlen+68)
2604 +                               return;
2605 +                       n -= grehlen;
2606 +                       /* BSD 4.2 MORE DOES NOT EXIST IN NATURE. */
2607 +                       if (n > ntohs(eiph->tot_len))
2608 +                               return;
2609 +                       rel_info = htonl(n);
2610 +                       break;
2611 +               default:
2612 +                       /* All others are translated to HOST_UNREACH.
2613 +                          rfc2003 contains "deep thoughts" about NET_UNREACH,
2614 +                          I believe, it is just ether pollution. --ANK
2615 +                        */
2616 +                       rel_type = ICMP_DEST_UNREACH;
2617 +                       rel_code = ICMP_HOST_UNREACH;
2618 +                       break;
2619 +               }
2620 +               break;
2621 +       case ICMP_TIME_EXCEEDED:
2622 +               if (code != ICMP_EXC_TTL)
2623 +                       return;
2624 +               break;
2625 +       }
2626 +
2627 +       /* Prepare fake skb to feed it to icmp_send */
2628 +       skb2 = skb_clone(skb, GFP_ATOMIC);
2629 +       if (skb2 == NULL)
2630 +               return;
2631 +       dst_release(skb2->dst);
2632 +       skb2->dst = NULL;
2633 +       skb_pull(skb2, skb->data - (u8*)eiph);
2634 +       skb_reset_network_header(skb2);
2635 +
2636 +       /* Try to guess incoming interface */
2637 +       memset(&fl, 0, sizeof(fl));
2638 +       fl.fl4_dst = eiph->saddr;
2639 +       fl.fl4_tos = RT_TOS(eiph->tos);
2640 +       fl.proto = IPPROTO_GRE;
2641 +       if (ip_route_output_key(&rt, &fl)) {
2642 +               kfree_skb(skb2);
2643 +               return;
2644 +       }
2645 +       skb2->dev = rt->u.dst.dev;
2646 +
2647 +       /* route "incoming" packet */
2648 +       if (rt->rt_flags&RTCF_LOCAL) {
2649 +               ip_rt_put(rt);
2650 +               rt = NULL;
2651 +               fl.fl4_dst = eiph->daddr;
2652 +               fl.fl4_src = eiph->saddr;
2653 +               fl.fl4_tos = eiph->tos;
2654 +               if (ip_route_output_key(&rt, &fl) ||
2655 +                   rt->u.dst.dev->type != ARPHRD_IPGRE) {
2656 +                       ip_rt_put(rt);
2657 +                       kfree_skb(skb2);
2658 +                       return;
2659 +               }
2660 +       } else {
2661 +               ip_rt_put(rt);
2662 +               if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos, skb2->dev) ||
2663 +                   skb2->dst->dev->type != ARPHRD_IPGRE) {
2664 +                       kfree_skb(skb2);
2665 +                       return;
2666 +               }
2667 +       }
2668 +
2669 +       /* change mtu on this route */
2670 +       if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
2671 +               if (n > dst_mtu(skb2->dst)) {
2672 +                       kfree_skb(skb2);
2673 +                       return;
2674 +               }
2675 +               skb2->dst->ops->update_pmtu(skb2->dst, n);
2676 +       } else if (type == ICMP_TIME_EXCEEDED) {
2677 +               struct ip_tunnel *t = netdev_priv(skb2->dev);
2678 +               if (t->parms.iph.ttl) {
2679 +                       rel_type = ICMP_DEST_UNREACH;
2680 +                       rel_code = ICMP_HOST_UNREACH;
2681 +               }
2682 +       }
2683 +
2684 +       icmp_send(skb2, rel_type, rel_code, rel_info);
2685 +       kfree_skb(skb2);
2686 +#endif
2687 +}
2688 +
2689 +static inline void ipgre_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
2690 +{
2691 +       if (INET_ECN_is_ce(iph->tos)) {
2692 +               if (skb->protocol == htons(ETH_P_IP)) {
2693 +                       IP_ECN_set_ce(ip_hdr(skb));
2694 +               } else if (skb->protocol == htons(ETH_P_IPV6)) {
2695 +                       IP6_ECN_set_ce(ipv6_hdr(skb));
2696 +               }
2697 +       }
2698 +}
2699 +
2700 +static inline u8
2701 +ipgre_ecn_encapsulate(u8 tos, struct iphdr *old_iph, struct sk_buff *skb)
2702 +{
2703 +       u8 inner = 0;
2704 +       if (skb->protocol == htons(ETH_P_IP))
2705 +               inner = old_iph->tos;
2706 +       else if (skb->protocol == htons(ETH_P_IPV6))
2707 +               inner = ipv6_get_dsfield((struct ipv6hdr *)old_iph);
2708 +       return INET_ECN_encapsulate(tos, inner);
2709 +}
2710 +
2711 +static int ipgre_rcv(struct sk_buff *skb)
2712 +{
2713 +       struct iphdr *iph;
2714 +       u8     *h;
2715 +       __be16    flags;
2716 +       __sum16   csum = 0;
2717 +       __be32 key = 0;
2718 +       u32    seqno = 0;
2719 +       struct ip_tunnel *tunnel;
2720 +       int    offset = 4;
2721 +
2722 +       if (!pskb_may_pull(skb, 16))
2723 +               goto drop_nolock;
2724 +
2725 +       iph = ip_hdr(skb);
2726 +       h = skb->data;
2727 +       flags = *(__be16*)h;
2728 +
2729 +       if (flags&(GRE_CSUM|GRE_KEY|GRE_ROUTING|GRE_SEQ|GRE_VERSION)) {
2730 +               /* - Version must be 0.
2731 +                  - We do not support routing headers.
2732 +                */
2733 +               if (flags&(GRE_VERSION|GRE_ROUTING))
2734 +                       goto drop_nolock;
2735 +
2736 +               if (flags&GRE_CSUM) {
2737 +                       switch (skb->ip_summed) {
2738 +                       case CHECKSUM_COMPLETE:
2739 +                               csum = csum_fold(skb->csum);
2740 +                               if (!csum)
2741 +                                       break;
2742 +                               /* fall through */
2743 +                       case CHECKSUM_NONE:
2744 +                               skb->csum = 0;
2745 +                               csum = __skb_checksum_complete(skb);
2746 +                               skb->ip_summed = CHECKSUM_COMPLETE;
2747 +                       }
2748 +                       offset += 4;
2749 +               }
2750 +               if (flags&GRE_KEY) {
2751 +                       key = *(__be32*)(h + offset);
2752 +                       offset += 4;
2753 +               }
2754 +               if (flags&GRE_SEQ) {
2755 +                       seqno = ntohl(*(__be32*)(h + offset));
2756 +                       offset += 4;
2757 +               }
2758 +       }
2759 +
2760 +       read_lock(&ipgre_lock);
2761 +       if ((tunnel = ipgre_tunnel_lookup(iph->saddr, iph->daddr, key)) != NULL) {
2762 +               secpath_reset(skb);
2763 +
2764 +               skb->protocol = *(__be16*)(h + 2);
2765 +               /* WCCP version 1 and 2 protocol decoding.
2766 +                * - Change protocol to IP
2767 +                * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
2768 +                */
2769 +               if (flags == 0 &&
2770 +                   skb->protocol == htons(ETH_P_WCCP)) {
2771 +                       skb->protocol = htons(ETH_P_IP);
2772 +                       if ((*(h + offset) & 0xF0) != 0x40)
2773 +                               offset += 4;
2774 +               }
2775 +
2776 +               skb_reset_mac_header(skb);
2777 +               __pskb_pull(skb, offset);
2778 +               skb_reset_network_header(skb);
2779 +               skb_postpull_rcsum(skb, skb_transport_header(skb), offset);
2780 +               skb->pkt_type = PACKET_HOST;
2781 +#ifdef CONFIG_NET_IPGRE_BROADCAST
2782 +               if (MULTICAST(iph->daddr)) {
2783 +                       /* Looped back packet, drop it! */
2784 +                       if (((struct rtable*)skb->dst)->fl.iif == 0)
2785 +                               goto drop;
2786 +                       tunnel->stat.multicast++;
2787 +                       skb->pkt_type = PACKET_BROADCAST;
2788 +               }
2789 +#endif
2790 +
2791 +               if (((flags&GRE_CSUM) && csum) ||
2792 +                   (!(flags&GRE_CSUM) && tunnel->parms.i_flags&GRE_CSUM)) {
2793 +                       tunnel->stat.rx_crc_errors++;
2794 +                       tunnel->stat.rx_errors++;
2795 +                       goto drop;
2796 +               }
2797 +               if (tunnel->parms.i_flags&GRE_SEQ) {
2798 +                       if (!(flags&GRE_SEQ) ||
2799 +                           (tunnel->i_seqno && (s32)(seqno - tunnel->i_seqno) < 0)) {
2800 +                               tunnel->stat.rx_fifo_errors++;
2801 +                               tunnel->stat.rx_errors++;
2802 +                               goto drop;
2803 +                       }
2804 +                       tunnel->i_seqno = seqno + 1;
2805 +               }
2806 +               tunnel->stat.rx_packets++;
2807 +               tunnel->stat.rx_bytes += skb->len;
2808 +               skb->dev = tunnel->dev;
2809 +               dst_release(skb->dst);
2810 +               skb->dst = NULL;
2811 +               nf_reset(skb);
2812 +               ipgre_ecn_decapsulate(iph, skb);
2813 +               netif_rx(skb);
2814 +               read_unlock(&ipgre_lock);
2815 +               return(0);
2816 +       }
2817 +       icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
2818 +
2819 +drop:
2820 +       read_unlock(&ipgre_lock);
2821 +drop_nolock:
2822 +       kfree_skb(skb);
2823 +       return(0);
2824 +}
2825 +
2826 +static int ipgre_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
2827 +{
2828 +       struct ip_tunnel *tunnel = netdev_priv(dev);
2829 +       struct net_device_stats *stats = &tunnel->stat;
2830 +       struct iphdr  *old_iph = ip_hdr(skb);
2831 +       struct iphdr  *tiph;
2832 +       u8     tos;
2833 +       __be16 df;
2834 +       struct rtable *rt;                      /* Route to the other host */
2835 +       struct net_device *tdev;                        /* Device to other host */
2836 +       struct iphdr  *iph;                     /* Our new IP header */
2837 +       int    max_headroom;                    /* The extra header space needed */
2838 +       int    gre_hlen;
2839 +       __be32 dst;
2840 +       int    mtu;
2841 +
2842 +       if (tunnel->recursion++) {
2843 +               tunnel->stat.collisions++;
2844 +               goto tx_error;
2845 +       }
2846 +
2847 +       if (dev->hard_header) {
2848 +               gre_hlen = 0;
2849 +               tiph = (struct iphdr*)skb->data;
2850 +       } else {
2851 +               gre_hlen = tunnel->hlen;
2852 +               tiph = &tunnel->parms.iph;
2853 +       }
2854 +
2855 +       if ((dst = tiph->daddr) == 0) {
2856 +               /* NBMA tunnel */
2857 +
2858 +               if (skb->dst == NULL) {
2859 +                       tunnel->stat.tx_fifo_errors++;
2860 +                       goto tx_error;
2861 +               }
2862 +
2863 +               if (skb->protocol == htons(ETH_P_IP)) {
2864 +                       rt = (struct rtable*)skb->dst;
2865 +                       if ((dst = rt->rt_gateway) == 0)
2866 +                               goto tx_error_icmp;
2867 +               }
2868 +#ifdef CONFIG_IPV6
2869 +               else if (skb->protocol == htons(ETH_P_IPV6)) {
2870 +                       struct in6_addr *addr6;
2871 +                       int addr_type;
2872 +                       struct neighbour *neigh = skb->dst->neighbour;
2873 +
2874 +                       if (neigh == NULL)
2875 +                               goto tx_error;
2876 +
2877 +                       addr6 = (struct in6_addr*)&neigh->primary_key;
2878 +                       addr_type = ipv6_addr_type(addr6);
2879 +
2880 +                       if (addr_type == IPV6_ADDR_ANY) {
2881 +                               addr6 = &ipv6_hdr(skb)->daddr;
2882 +                               addr_type = ipv6_addr_type(addr6);
2883 +                       }
2884 +
2885 +                       if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
2886 +                               goto tx_error_icmp;
2887 +
2888 +                       dst = addr6->s6_addr32[3];
2889 +               }
2890 +#endif
2891 +               else
2892 +                       goto tx_error;
2893 +       }
2894 +
2895 +       tos = tiph->tos;
2896 +       if (tos&1) {
2897 +               if (skb->protocol == htons(ETH_P_IP))
2898 +                       tos = old_iph->tos;
2899 +               tos &= ~1;
2900 +       }
2901 +
2902 +       {
2903 +               struct flowi fl = { .oif = tunnel->parms.link,
2904 +                                   .nl_u = { .ip4_u =
2905 +                                             { .daddr = dst,
2906 +                                               .saddr = tiph->saddr,
2907 +                                               .tos = RT_TOS(tos) } },
2908 +                                   .proto = IPPROTO_GRE };
2909 +               if (ip_route_output_key(&rt, &fl)) {
2910 +                       tunnel->stat.tx_carrier_errors++;
2911 +                       goto tx_error;
2912 +               }
2913 +       }
2914 +       tdev = rt->u.dst.dev;
2915 +
2916 +       if (tdev == dev) {
2917 +               ip_rt_put(rt);
2918 +               tunnel->stat.collisions++;
2919 +               goto tx_error;
2920 +       }
2921 +
2922 +       df = tiph->frag_off;
2923 +       if (df)
2924 +               mtu = dst_mtu(&rt->u.dst) - tunnel->hlen;
2925 +       else
2926 +               mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu;
2927 +
2928 +       if (skb->dst)
2929 +               skb->dst->ops->update_pmtu(skb->dst, mtu);
2930 +
2931 +       if (skb->protocol == htons(ETH_P_IP)) {
2932 +               df |= (old_iph->frag_off&htons(IP_DF));
2933 +
2934 +               if ((old_iph->frag_off&htons(IP_DF)) &&
2935 +                   mtu < ntohs(old_iph->tot_len)) {
2936 +                       icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
2937 +                       ip_rt_put(rt);
2938 +                       goto tx_error;
2939 +               }
2940 +       }
2941 +#ifdef CONFIG_IPV6
2942 +       else if (skb->protocol == htons(ETH_P_IPV6)) {
2943 +               struct rt6_info *rt6 = (struct rt6_info*)skb->dst;
2944 +
2945 +               if (rt6 && mtu < dst_mtu(skb->dst) && mtu >= IPV6_MIN_MTU) {
2946 +                       if ((tunnel->parms.iph.daddr && !MULTICAST(tunnel->parms.iph.daddr)) ||
2947 +                           rt6->rt6i_dst.plen == 128) {
2948 +                               rt6->rt6i_flags |= RTF_MODIFIED;
2949 +                               skb->dst->metrics[RTAX_MTU-1] = mtu;
2950 +                       }
2951 +               }
2952 +
2953 +               if (mtu >= IPV6_MIN_MTU && mtu < skb->len - tunnel->hlen + gre_hlen) {
2954 +                       icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
2955 +                       ip_rt_put(rt);
2956 +                       goto tx_error;
2957 +               }
2958 +       }
2959 +#endif
2960 +
2961 +       if (tunnel->err_count > 0) {
2962 +               if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
2963 +                       tunnel->err_count--;
2964 +
2965 +                       dst_link_failure(skb);
2966 +               } else
2967 +                       tunnel->err_count = 0;
2968 +       }
2969 +
2970 +       max_headroom = LL_RESERVED_SPACE(tdev) + gre_hlen;
2971 +
2972 +       if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) {
2973 +               struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
2974 +               if (!new_skb) {
2975 +                       ip_rt_put(rt);
2976 +                       stats->tx_dropped++;
2977 +                       dev_kfree_skb(skb);
2978 +                       tunnel->recursion--;
2979 +                       return 0;
2980 +               }
2981 +               if (skb->sk)
2982 +                       skb_set_owner_w(new_skb, skb->sk);
2983 +               dev_kfree_skb(skb);
2984 +               skb = new_skb;
2985 +               old_iph = ip_hdr(skb);
2986 +       }
2987 +
2988 +       skb->transport_header = skb->network_header;
2989 +       skb_push(skb, gre_hlen);
2990 +       skb_reset_network_header(skb);
2991 +       memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
2992 +       IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
2993 +                             IPSKB_REROUTED);
2994 +       dst_release(skb->dst);
2995 +       skb->dst = &rt->u.dst;
2996 +
2997 +       /*
2998 +        *      Push down and install the IPIP header.
2999 +        */
3000 +
3001 +       iph                     =       ip_hdr(skb);
3002 +       iph->version            =       4;
3003 +       iph->ihl                =       sizeof(struct iphdr) >> 2;
3004 +       iph->frag_off           =       df;
3005 +       iph->protocol           =       IPPROTO_GRE;
3006 +       iph->tos                =       ipgre_ecn_encapsulate(tos, old_iph, skb);
3007 +       iph->daddr              =       rt->rt_dst;
3008 +       iph->saddr              =       rt->rt_src;
3009 +
3010 +       if ((iph->ttl = tiph->ttl) == 0) {
3011 +               if (skb->protocol == htons(ETH_P_IP))
3012 +                       iph->ttl = old_iph->ttl;
3013 +#ifdef CONFIG_IPV6
3014 +               else if (skb->protocol == htons(ETH_P_IPV6))
3015 +                       iph->ttl = ((struct ipv6hdr*)old_iph)->hop_limit;
3016 +#endif
3017 +               else
3018 +                       iph->ttl = dst_metric(&rt->u.dst, RTAX_HOPLIMIT);
3019 +       }
3020 +
3021 +       ((__be16*)(iph+1))[0] = tunnel->parms.o_flags;
3022 +       ((__be16*)(iph+1))[1] = skb->protocol;
3023 +
3024 +       if (tunnel->parms.o_flags&(GRE_KEY|GRE_CSUM|GRE_SEQ)) {
3025 +               __be32 *ptr = (__be32*)(((u8*)iph) + tunnel->hlen - 4);
3026 +
3027 +               if (tunnel->parms.o_flags&GRE_SEQ) {
3028 +                       ++tunnel->o_seqno;
3029 +                       *ptr = htonl(tunnel->o_seqno);
3030 +                       ptr--;
3031 +               }
3032 +               if (tunnel->parms.o_flags&GRE_KEY) {
3033 +                       *ptr = tunnel->parms.o_key;
3034 +                       ptr--;
3035 +               }
3036 +               if (tunnel->parms.o_flags&GRE_CSUM) {
3037 +                       *ptr = 0;
3038 +                       *(__sum16*)ptr = ip_compute_csum((void*)(iph+1), skb->len - sizeof(struct iphdr));
3039 +               }
3040 +       }
3041 +
3042 +       nf_reset(skb);
3043 +
3044 +       IPTUNNEL_XMIT();
3045 +       tunnel->recursion--;
3046 +       return 0;
3047 +
3048 +tx_error_icmp:
3049 +       dst_link_failure(skb);
3050 +
3051 +tx_error:
3052 +       stats->tx_errors++;
3053 +       dev_kfree_skb(skb);
3054 +       tunnel->recursion--;
3055 +       return 0;
3056 +}
3057 +
3058 +static int
3059 +ipgre_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
3060 +{
3061 +       int err = 0;
3062 +       struct ip_tunnel_parm p;
3063 +       struct ip_tunnel *t;
3064 +
3065 +       switch (cmd) {
3066 +       case SIOCGETTUNNEL:
3067 +               t = NULL;
3068 +               if (dev == ipgre_fb_tunnel_dev) {
3069 +                       if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
3070 +                               err = -EFAULT;
3071 +                               break;
3072 +                       }
3073 +                       t = ipgre_tunnel_locate(&p, 0);
3074 +               }
3075 +               if (t == NULL)
3076 +                       t = netdev_priv(dev);
3077 +               memcpy(&p, &t->parms, sizeof(p));
3078 +               if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
3079 +                       err = -EFAULT;
3080 +               break;
3081 +
3082 +       case SIOCADDTUNNEL:
3083 +       case SIOCCHGTUNNEL:
3084 +               err = -EPERM;
3085 +               if (!capable(CAP_NET_ADMIN))
3086 +                       goto done;
3087 +
3088 +               err = -EFAULT;
3089 +               if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
3090 +                       goto done;
3091 +
3092 +               err = -EINVAL;
3093 +               if (p.iph.version != 4 || p.iph.protocol != IPPROTO_GRE ||
3094 +                   p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)) ||
3095 +                   ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING)))
3096 +                       goto done;
3097 +               if (p.iph.ttl)
3098 +                       p.iph.frag_off |= htons(IP_DF);
3099 +
3100 +               if (!(p.i_flags&GRE_KEY))
3101 +                       p.i_key = 0;
3102 +               if (!(p.o_flags&GRE_KEY))
3103 +                       p.o_key = 0;
3104 +
3105 +               t = ipgre_tunnel_locate(&p, cmd == SIOCADDTUNNEL);
3106 +
3107 +               if (dev != ipgre_fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
3108 +                       if (t != NULL) {
3109 +                               if (t->dev != dev) {
3110 +                                       err = -EEXIST;
3111 +                                       break;
3112 +                               }
3113 +                       } else {
3114 +                               unsigned nflags=0;
3115 +
3116 +                               t = netdev_priv(dev);
3117 +
3118 +                               if (MULTICAST(p.iph.daddr))
3119 +                                       nflags = IFF_BROADCAST;
3120 +                               else if (p.iph.daddr)
3121 +                                       nflags = IFF_POINTOPOINT;
3122 +
3123 +                               if ((dev->flags^nflags)&(IFF_POINTOPOINT|IFF_BROADCAST)) {
3124 +                                       err = -EINVAL;
3125 +                                       break;
3126 +                               }
3127 +                               ipgre_tunnel_unlink(t);
3128 +                               t->parms.iph.saddr = p.iph.saddr;
3129 +                               t->parms.iph.daddr = p.iph.daddr;
3130 +                               t->parms.i_key = p.i_key;
3131 +                               t->parms.o_key = p.o_key;
3132 +                               memcpy(dev->dev_addr, &p.iph.saddr, 4);
3133 +                               memcpy(dev->broadcast, &p.iph.daddr, 4);
3134 +                               ipgre_tunnel_link(t);
3135 +                               netdev_state_change(dev);
3136 +                       }
3137 +               }
3138 +
3139 +               if (t) {
3140 +                       err = 0;
3141 +                       if (cmd == SIOCCHGTUNNEL) {
3142 +                               t->parms.iph.ttl = p.iph.ttl;
3143 +                               t->parms.iph.tos = p.iph.tos;
3144 +                               t->parms.iph.frag_off = p.iph.frag_off;
3145 +                       }
3146 +                       if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
3147 +                               err = -EFAULT;
3148 +               } else
3149 +                       err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
3150 +               break;
3151 +
3152 +       case SIOCDELTUNNEL:
3153 +               err = -EPERM;
3154 +               if (!capable(CAP_NET_ADMIN))
3155 +                       goto done;
3156 +
3157 +               if (dev == ipgre_fb_tunnel_dev) {
3158 +                       err = -EFAULT;
3159 +                       if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
3160 +                               goto done;
3161 +                       err = -ENOENT;
3162 +                       if ((t = ipgre_tunnel_locate(&p, 0)) == NULL)
3163 +                               goto done;
3164 +                       err = -EPERM;
3165 +                       if (t == netdev_priv(ipgre_fb_tunnel_dev))
3166 +                               goto done;
3167 +                       dev = t->dev;
3168 +               }
3169 +               unregister_netdevice(dev);
3170 +               err = 0;
3171 +               break;
3172 +
3173 +       default:
3174 +               err = -EINVAL;
3175 +       }
3176 +
3177 +done:
3178 +       return err;
3179 +}
3180 +
3181 +static struct net_device_stats *ipgre_tunnel_get_stats(struct net_device *dev)
3182 +{
3183 +       return &(((struct ip_tunnel*)netdev_priv(dev))->stat);
3184 +}
3185 +
3186 +static int ipgre_tunnel_change_mtu(struct net_device *dev, int new_mtu)
3187 +{
3188 +       struct ip_tunnel *tunnel = netdev_priv(dev);
3189 +       if (new_mtu < 68 || new_mtu > 0xFFF8 - tunnel->hlen)
3190 +               return -EINVAL;
3191 +       dev->mtu = new_mtu;
3192 +       return 0;
3193 +}
3194 +
3195 +#ifdef CONFIG_NET_IPGRE_BROADCAST
3196 +/* Nice toy. Unfortunately, useless in real life :-)
3197 +   It allows to construct virtual multiprotocol broadcast "LAN"
3198 +   over the Internet, provided multicast routing is tuned.
3199 +
3200 +
3201 +   I have no idea was this bicycle invented before me,
3202 +   so that I had to set ARPHRD_IPGRE to a random value.
3203 +   I have an impression, that Cisco could make something similar,
3204 +   but this feature is apparently missing in IOS<=11.2(8).
3205 +
3206 +   I set up 10.66.66/24 and fec0:6666:6666::0/96 as virtual networks
3207 +   with broadcast 224.66.66.66. If you have access to mbone, play with me :-)
3208 +
3209 +   ping -t 255 224.66.66.66
3210 +
3211 +   If nobody answers, mbone does not work.
3212 +
3213 +   ip tunnel add Universe mode gre remote 224.66.66.66 local <Your_real_addr> ttl 255
3214 +   ip addr add 10.66.66.<somewhat>/24 dev Universe
3215 +   ifconfig Universe up
3216 +   ifconfig Universe add fe80::<Your_real_addr>/10
3217 +   ifconfig Universe add fec0:6666:6666::<Your_real_addr>/96
3218 +   ftp 10.66.66.66
3219 +   ...
3220 +   ftp fec0:6666:6666::193.233.7.65
3221 +   ...
3222 +
3223 + */
3224 +
3225 +static int ipgre_header(struct sk_buff *skb, struct net_device *dev, unsigned short type,
3226 +                       void *daddr, void *saddr, unsigned len)
3227 +{
3228 +       struct ip_tunnel *t = netdev_priv(dev);
3229 +       struct iphdr *iph = (struct iphdr *)skb_push(skb, t->hlen);
3230 +       __be16 *p = (__be16*)(iph+1);
3231 +
3232 +       memcpy(iph, &t->parms.iph, sizeof(struct iphdr));
3233 +       p[0]            = t->parms.o_flags;
3234 +       p[1]            = htons(type);
3235 +
3236 +       /*
3237 +        *      Set the source hardware address.
3238 +        */
3239 +
3240 +       if (saddr)
3241 +               memcpy(&iph->saddr, saddr, 4);
3242 +
3243 +       if (daddr) {
3244 +               memcpy(&iph->daddr, daddr, 4);
3245 +               return t->hlen;
3246 +       }
3247 +       if (iph->daddr && !MULTICAST(iph->daddr))
3248 +               return t->hlen;
3249 +
3250 +       return -t->hlen;
3251 +}
3252 +
3253 +static int ipgre_open(struct net_device *dev)
3254 +{
3255 +       struct ip_tunnel *t = netdev_priv(dev);
3256 +
3257 +       if (MULTICAST(t->parms.iph.daddr)) {
3258 +               struct flowi fl = { .oif = t->parms.link,
3259 +                                   .nl_u = { .ip4_u =
3260 +                                             { .daddr = t->parms.iph.daddr,
3261 +                                               .saddr = t->parms.iph.saddr,
3262 +                                               .tos = RT_TOS(t->parms.iph.tos) } },
3263 +                                   .proto = IPPROTO_GRE };
3264 +               struct rtable *rt;
3265 +               if (ip_route_output_key(&rt, &fl))
3266 +                       return -EADDRNOTAVAIL;
3267 +               dev = rt->u.dst.dev;
3268 +               ip_rt_put(rt);
3269 +               if (__in_dev_get_rtnl(dev) == NULL)
3270 +                       return -EADDRNOTAVAIL;
3271 +               t->mlink = dev->ifindex;
3272 +               ip_mc_inc_group(__in_dev_get_rtnl(dev), t->parms.iph.daddr);
3273 +       }
3274 +       return 0;
3275 +}
3276 +
3277 +static int ipgre_close(struct net_device *dev)
3278 +{
3279 +       struct ip_tunnel *t = netdev_priv(dev);
3280 +       if (MULTICAST(t->parms.iph.daddr) && t->mlink) {
3281 +               struct in_device *in_dev = inetdev_by_index(t->mlink);
3282 +               if (in_dev) {
3283 +                       ip_mc_dec_group(in_dev, t->parms.iph.daddr);
3284 +                       in_dev_put(in_dev);
3285 +               }
3286 +       }
3287 +       return 0;
3288 +}
3289 +
3290 +#endif
3291 +
3292 +static void ipgre_tunnel_setup(struct net_device *dev)
3293 +{
3294 +       SET_MODULE_OWNER(dev);
3295 +       dev->uninit             = ipgre_tunnel_uninit;
3296 +       dev->destructor         = free_netdev;
3297 +       dev->hard_start_xmit    = ipgre_tunnel_xmit;
3298 +       dev->get_stats          = ipgre_tunnel_get_stats;
3299 +       dev->do_ioctl           = ipgre_tunnel_ioctl;
3300 +       dev->change_mtu         = ipgre_tunnel_change_mtu;
3301 +
3302 +       dev->type               = ARPHRD_IPGRE;
3303 +       dev->hard_header_len    = LL_MAX_HEADER + sizeof(struct iphdr) + 4;
3304 +       dev->mtu                = ETH_DATA_LEN - sizeof(struct iphdr) - 4;
3305 +       dev->flags              = IFF_NOARP;
3306 +       dev->iflink             = 0;
3307 +       dev->addr_len           = 4;
3308 +}
3309 +
3310 +static int ipgre_tunnel_init(struct net_device *dev)
3311 +{
3312 +       struct net_device *tdev = NULL;
3313 +       struct ip_tunnel *tunnel;
3314 +       struct iphdr *iph;
3315 +       int hlen = LL_MAX_HEADER;
3316 +       int mtu = ETH_DATA_LEN;
3317 +       int addend = sizeof(struct iphdr) + 4;
3318 +
3319 +       tunnel = netdev_priv(dev);
3320 +       iph = &tunnel->parms.iph;
3321 +
3322 +       tunnel->dev = dev;
3323 +       strcpy(tunnel->parms.name, dev->name);
3324 +
3325 +       memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
3326 +       memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
3327 +
3328 +       /* Guess output device to choose reasonable mtu and hard_header_len */
3329 +
3330 +       if (iph->daddr) {
3331 +               struct flowi fl = { .oif = tunnel->parms.link,
3332 +                                   .nl_u = { .ip4_u =
3333 +                                             { .daddr = iph->daddr,
3334 +                                               .saddr = iph->saddr,
3335 +                                               .tos = RT_TOS(iph->tos) } },
3336 +                                   .proto = IPPROTO_GRE };
3337 +               struct rtable *rt;
3338 +               if (!ip_route_output_key(&rt, &fl)) {
3339 +                       tdev = rt->u.dst.dev;
3340 +                       ip_rt_put(rt);
3341 +               }
3342 +
3343 +               dev->flags |= IFF_POINTOPOINT;
3344 +
3345 +#ifdef CONFIG_NET_IPGRE_BROADCAST
3346 +               if (MULTICAST(iph->daddr)) {
3347 +                       if (!iph->saddr)
3348 +                               return -EINVAL;
3349 +                       dev->flags = IFF_BROADCAST;
3350 +                       dev->hard_header = ipgre_header;
3351 +                       dev->open = ipgre_open;
3352 +                       dev->stop = ipgre_close;
3353 +               }
3354 +#endif
3355 +       }
3356 +
3357 +       if (!tdev && tunnel->parms.link)
3358 +               tdev = __dev_get_by_index(tunnel->parms.link);
3359 +
3360 +       if (tdev) {
3361 +               hlen = tdev->hard_header_len;
3362 +               mtu = tdev->mtu;
3363 +       }
3364 +       dev->iflink = tunnel->parms.link;
3365 +
3366 +       /* Precalculate GRE options length */
3367 +       if (tunnel->parms.o_flags&(GRE_CSUM|GRE_KEY|GRE_SEQ)) {
3368 +               if (tunnel->parms.o_flags&GRE_CSUM)
3369 +                       addend += 4;
3370 +               if (tunnel->parms.o_flags&GRE_KEY)
3371 +                       addend += 4;
3372 +               if (tunnel->parms.o_flags&GRE_SEQ)
3373 +                       addend += 4;
3374 +       }
3375 +       dev->hard_header_len = hlen + addend;
3376 +       dev->mtu = mtu - addend;
3377 +       tunnel->hlen = addend;
3378 +       return 0;
3379 +}
3380 +
3381 +static int __init ipgre_fb_tunnel_init(struct net_device *dev)
3382 +{
3383 +       struct ip_tunnel *tunnel = netdev_priv(dev);
3384 +       struct iphdr *iph = &tunnel->parms.iph;
3385 +
3386 +       tunnel->dev = dev;
3387 +       strcpy(tunnel->parms.name, dev->name);
3388 +
3389 +       iph->version            = 4;
3390 +       iph->protocol           = IPPROTO_GRE;
3391 +       iph->ihl                = 5;
3392 +       tunnel->hlen            = sizeof(struct iphdr) + 4;
3393 +
3394 +       dev_hold(dev);
3395 +       tunnels_wc[0]           = tunnel;
3396 +       return 0;
3397 +}
3398 +
3399 +
3400 +static struct net_protocol ipgre_protocol = {
3401 +       .handler        =       ipgre_rcv,
3402 +       .err_handler    =       ipgre_err,
3403 +};
3404 +
3405 +
3406 +/*
3407 + *     And now the modules code and kernel interface.
3408 + */
3409 +
3410 +static int __init ipgre_init(void)
3411 +{
3412 +       int err;
3413 +
3414 +       printk(KERN_INFO "GRE over IPv4 tunneling driver\n");
3415 +
3416 +       if (inet_add_protocol(&ipgre_protocol, IPPROTO_GRE) < 0) {
3417 +               printk(KERN_INFO "ipgre init: can't add protocol\n");
3418 +               return -EAGAIN;
3419 +       }
3420 +
3421 +       ipgre_fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "gre0",
3422 +                                          ipgre_tunnel_setup);
3423 +       if (!ipgre_fb_tunnel_dev) {
3424 +               err = -ENOMEM;
3425 +               goto err1;
3426 +       }
3427 +
3428 +       ipgre_fb_tunnel_dev->init = ipgre_fb_tunnel_init;
3429 +
3430 +       if ((err = register_netdev(ipgre_fb_tunnel_dev)))
3431 +               goto err2;
3432 +out:
3433 +       return err;
3434 +err2:
3435 +       free_netdev(ipgre_fb_tunnel_dev);
3436 +err1:
3437 +       inet_del_protocol(&ipgre_protocol, IPPROTO_GRE);
3438 +       goto out;
3439 +}
3440 +
3441 +static void __exit ipgre_destroy_tunnels(void)
3442 +{
3443 +       int prio;
3444 +
3445 +       for (prio = 0; prio < 4; prio++) {
3446 +               int h;
3447 +               for (h = 0; h < HASH_SIZE; h++) {
3448 +                       struct ip_tunnel *t;
3449 +                       while ((t = tunnels[prio][h]) != NULL)
3450 +                               unregister_netdevice(t->dev);
3451 +               }
3452 +       }
3453 +}
3454 +
3455 +static void __exit ipgre_fini(void)
3456 +{
3457 +       if (inet_del_protocol(&ipgre_protocol, IPPROTO_GRE) < 0)
3458 +               printk(KERN_INFO "ipgre close: can't remove protocol\n");
3459 +
3460 +       rtnl_lock();
3461 +       ipgre_destroy_tunnels();
3462 +       rtnl_unlock();
3463 +}
3464 +
3465 +module_init(ipgre_init);
3466 +module_exit(ipgre_fini);
3467 +MODULE_LICENSE("GPL");