OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [net/] [ipv6/] [sit.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 *      IPv6 over IPv4 tunnel device - Simple Internet Transition (SIT)
3
 *      Linux INET6 implementation
4
 *
5
 *      Authors:
6
 *      Pedro Roque             <roque@di.fc.ul.pt>
7
 *      Alexey Kuznetsov        <kuznet@ms2.inr.ac.ru>
8
 *
9
 *      $Id: sit.c,v 1.1.1.1 2004-04-15 01:14:34 phoenix Exp $
10
 *
11
 *      This program is free software; you can redistribute it and/or
12
 *      modify it under the terms of the GNU General Public License
13
 *      as published by the Free Software Foundation; either version
14
 *      2 of the License, or (at your option) any later version.
15
 *
16
 *      Changes:
17
 * Roger Venning <r.venning@telstra.com>:       6to4 support
18
 * Nate Thompson <nate@thebog.net>:             6to4 support
19
 */
20
 
21
#include <linux/config.h>
22
#include <linux/module.h>
23
#include <linux/errno.h>
24
#include <linux/types.h>
25
#include <linux/socket.h>
26
#include <linux/sockios.h>
27
#include <linux/sched.h>
28
#include <linux/net.h>
29
#include <linux/in6.h>
30
#include <linux/netdevice.h>
31
#include <linux/if_arp.h>
32
#include <linux/icmp.h>
33
#include <asm/uaccess.h>
34
#include <linux/init.h>
35
#include <linux/netfilter_ipv4.h>
36
 
37
#include <net/sock.h>
38
#include <net/snmp.h>
39
 
40
#include <net/ipv6.h>
41
#include <net/protocol.h>
42
#include <net/transp_v6.h>
43
#include <net/ip6_fib.h>
44
#include <net/ip6_route.h>
45
#include <net/ndisc.h>
46
#include <net/addrconf.h>
47
#include <net/ip.h>
48
#include <net/udp.h>
49
#include <net/icmp.h>
50
#include <net/ipip.h>
51
#include <net/inet_ecn.h>
52
 
53
/*
54
   This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c
55
 
56
   For comments look at net/ipv4/ip_gre.c --ANK
57
 */
58
 
59
#define HASH_SIZE  16
60
#define HASH(addr) ((addr^(addr>>4))&0xF)
61
 
62
static int ipip6_fb_tunnel_init(struct net_device *dev);
63
static int ipip6_tunnel_init(struct net_device *dev);
64
 
65
static struct net_device ipip6_fb_tunnel_dev = {
66
        name:           "sit0",
67
        init:           ipip6_fb_tunnel_init,
68
};
69
 
70
static struct ip_tunnel ipip6_fb_tunnel = {
71
        NULL, &ipip6_fb_tunnel_dev, {0, }, 0, 0, 0, 0, 0, 0, 0, {"sit0", }
72
};
73
 
74
static struct ip_tunnel *tunnels_r_l[HASH_SIZE];
75
static struct ip_tunnel *tunnels_r[HASH_SIZE];
76
static struct ip_tunnel *tunnels_l[HASH_SIZE];
77
static struct ip_tunnel *tunnels_wc[1];
78
static struct ip_tunnel **tunnels[4] = { tunnels_wc, tunnels_l, tunnels_r, tunnels_r_l };
79
 
80
static rwlock_t ipip6_lock = RW_LOCK_UNLOCKED;
81
 
82
static struct ip_tunnel * ipip6_tunnel_lookup(u32 remote, u32 local)
83
{
84
        unsigned h0 = HASH(remote);
85
        unsigned h1 = HASH(local);
86
        struct ip_tunnel *t;
87
 
88
        for (t = tunnels_r_l[h0^h1]; t; t = t->next) {
89
                if (local == t->parms.iph.saddr &&
90
                    remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
91
                        return t;
92
        }
93
        for (t = tunnels_r[h0]; t; t = t->next) {
94
                if (remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
95
                        return t;
96
        }
97
        for (t = tunnels_l[h1]; t; t = t->next) {
98
                if (local == t->parms.iph.saddr && (t->dev->flags&IFF_UP))
99
                        return t;
100
        }
101
        if ((t = tunnels_wc[0]) != NULL && (t->dev->flags&IFF_UP))
102
                return t;
103
        return NULL;
104
}
105
 
106
static struct ip_tunnel ** ipip6_bucket(struct ip_tunnel *t)
107
{
108
        u32 remote = t->parms.iph.daddr;
109
        u32 local = t->parms.iph.saddr;
110
        unsigned h = 0;
111
        int prio = 0;
112
 
113
        if (remote) {
114
                prio |= 2;
115
                h ^= HASH(remote);
116
        }
117
        if (local) {
118
                prio |= 1;
119
                h ^= HASH(local);
120
        }
121
        return &tunnels[prio][h];
122
}
123
 
124
static void ipip6_tunnel_unlink(struct ip_tunnel *t)
125
{
126
        struct ip_tunnel **tp;
127
 
128
        for (tp = ipip6_bucket(t); *tp; tp = &(*tp)->next) {
129
                if (t == *tp) {
130
                        write_lock_bh(&ipip6_lock);
131
                        *tp = t->next;
132
                        write_unlock_bh(&ipip6_lock);
133
                        break;
134
                }
135
        }
136
}
137
 
138
static void ipip6_tunnel_link(struct ip_tunnel *t)
139
{
140
        struct ip_tunnel **tp = ipip6_bucket(t);
141
 
142
        write_lock_bh(&ipip6_lock);
143
        t->next = *tp;
144
        write_unlock_bh(&ipip6_lock);
145
        *tp = t;
146
}
147
 
148
struct ip_tunnel * ipip6_tunnel_locate(struct ip_tunnel_parm *parms, int create)
149
{
150
        u32 remote = parms->iph.daddr;
151
        u32 local = parms->iph.saddr;
152
        struct ip_tunnel *t, **tp, *nt;
153
        struct net_device *dev;
154
        unsigned h = 0;
155
        int prio = 0;
156
 
157
        if (remote) {
158
                prio |= 2;
159
                h ^= HASH(remote);
160
        }
161
        if (local) {
162
                prio |= 1;
163
                h ^= HASH(local);
164
        }
165
        for (tp = &tunnels[prio][h]; (t = *tp) != NULL; tp = &t->next) {
166
                if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr)
167
                        return t;
168
        }
169
        if (!create)
170
                return NULL;
171
 
172
        MOD_INC_USE_COUNT;
173
        dev = kmalloc(sizeof(*dev) + sizeof(*t), GFP_KERNEL);
174
        if (dev == NULL) {
175
                MOD_DEC_USE_COUNT;
176
                return NULL;
177
        }
178
        memset(dev, 0, sizeof(*dev) + sizeof(*t));
179
        dev->priv = (void*)(dev+1);
180
        nt = (struct ip_tunnel*)dev->priv;
181
        nt->dev = dev;
182
        dev->init = ipip6_tunnel_init;
183
        dev->features |= NETIF_F_DYNALLOC;
184
        memcpy(&nt->parms, parms, sizeof(*parms));
185
        nt->parms.name[IFNAMSIZ-1] = '\0';
186
        strcpy(dev->name, nt->parms.name);
187
        if (dev->name[0] == 0) {
188
                int i;
189
                for (i=1; i<100; i++) {
190
                        sprintf(dev->name, "sit%d", i);
191
                        if (__dev_get_by_name(dev->name) == NULL)
192
                                break;
193
                }
194
                if (i==100)
195
                        goto failed;
196
                memcpy(nt->parms.name, dev->name, IFNAMSIZ);
197
        }
198
        if (register_netdevice(dev) < 0)
199
                goto failed;
200
 
201
        dev_hold(dev);
202
        ipip6_tunnel_link(nt);
203
        /* Do not decrement MOD_USE_COUNT here. */
204
        return nt;
205
 
206
failed:
207
        kfree(dev);
208
        MOD_DEC_USE_COUNT;
209
        return NULL;
210
}
211
 
212
static void ipip6_tunnel_destructor(struct net_device *dev)
213
{
214
        if (dev != &ipip6_fb_tunnel_dev) {
215
                MOD_DEC_USE_COUNT;
216
        }
217
}
218
 
219
static void ipip6_tunnel_uninit(struct net_device *dev)
220
{
221
        if (dev == &ipip6_fb_tunnel_dev) {
222
                write_lock_bh(&ipip6_lock);
223
                tunnels_wc[0] = NULL;
224
                write_unlock_bh(&ipip6_lock);
225
                dev_put(dev);
226
        } else {
227
                ipip6_tunnel_unlink((struct ip_tunnel*)dev->priv);
228
                dev_put(dev);
229
        }
230
}
231
 
232
 
233
void ipip6_err(struct sk_buff *skb, u32 info)
234
{
235
#ifndef I_WISH_WORLD_WERE_PERFECT
236
 
237
/* It is not :-( All the routers (except for Linux) return only
238
   8 bytes of packet payload. It means, that precise relaying of
239
   ICMP in the real Internet is absolutely infeasible.
240
 */
241
        struct iphdr *iph = (struct iphdr*)skb->data;
242
        int type = skb->h.icmph->type;
243
        int code = skb->h.icmph->code;
244
        struct ip_tunnel *t;
245
 
246
        switch (type) {
247
        default:
248
        case ICMP_PARAMETERPROB:
249
                return;
250
 
251
        case ICMP_DEST_UNREACH:
252
                switch (code) {
253
                case ICMP_SR_FAILED:
254
                case ICMP_PORT_UNREACH:
255
                        /* Impossible event. */
256
                        return;
257
                case ICMP_FRAG_NEEDED:
258
                        /* Soft state for pmtu is maintained by IP core. */
259
                        return;
260
                default:
261
                        /* All others are translated to HOST_UNREACH.
262
                           rfc2003 contains "deep thoughts" about NET_UNREACH,
263
                           I believe they are just ether pollution. --ANK
264
                         */
265
                        break;
266
                }
267
                break;
268
        case ICMP_TIME_EXCEEDED:
269
                if (code != ICMP_EXC_TTL)
270
                        return;
271
                break;
272
        }
273
 
274
        read_lock(&ipip6_lock);
275
        t = ipip6_tunnel_lookup(iph->daddr, iph->saddr);
276
        if (t == NULL || t->parms.iph.daddr == 0)
277
                goto out;
278
        if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
279
                goto out;
280
 
281
        if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO)
282
                t->err_count++;
283
        else
284
                t->err_count = 1;
285
        t->err_time = jiffies;
286
out:
287
        read_unlock(&ipip6_lock);
288
        return;
289
#else
290
        struct iphdr *iph = (struct iphdr*)dp;
291
        int hlen = iph->ihl<<2;
292
        struct ipv6hdr *iph6;
293
        int type = skb->h.icmph->type;
294
        int code = skb->h.icmph->code;
295
        int rel_type = 0;
296
        int rel_code = 0;
297
        int rel_info = 0;
298
        struct sk_buff *skb2;
299
        struct rt6_info *rt6i;
300
 
301
        if (len < hlen + sizeof(struct ipv6hdr))
302
                return;
303
        iph6 = (struct ipv6hdr*)(dp + hlen);
304
 
305
        switch (type) {
306
        default:
307
                return;
308
        case ICMP_PARAMETERPROB:
309
                if (skb->h.icmph->un.gateway < hlen)
310
                        return;
311
 
312
                /* So... This guy found something strange INSIDE encapsulated
313
                   packet. Well, he is fool, but what can we do ?
314
                 */
315
                rel_type = ICMPV6_PARAMPROB;
316
                rel_info = skb->h.icmph->un.gateway - hlen;
317
                break;
318
 
319
        case ICMP_DEST_UNREACH:
320
                switch (code) {
321
                case ICMP_SR_FAILED:
322
                case ICMP_PORT_UNREACH:
323
                        /* Impossible event. */
324
                        return;
325
                case ICMP_FRAG_NEEDED:
326
                        /* Too complicated case ... */
327
                        return;
328
                default:
329
                        /* All others are translated to HOST_UNREACH.
330
                           rfc2003 contains "deep thoughts" about NET_UNREACH,
331
                           I believe, it is just ether pollution. --ANK
332
                         */
333
                        rel_type = ICMPV6_DEST_UNREACH;
334
                        rel_code = ICMPV6_ADDR_UNREACH;
335
                        break;
336
                }
337
                break;
338
        case ICMP_TIME_EXCEEDED:
339
                if (code != ICMP_EXC_TTL)
340
                        return;
341
                rel_type = ICMPV6_TIME_EXCEED;
342
                rel_code = ICMPV6_EXC_HOPLIMIT;
343
                break;
344
        }
345
 
346
        /* Prepare fake skb to feed it to icmpv6_send */
347
        skb2 = skb_clone(skb, GFP_ATOMIC);
348
        if (skb2 == NULL)
349
                return;
350
        dst_release(skb2->dst);
351
        skb2->dst = NULL;
352
        skb_pull(skb2, skb->data - (u8*)iph6);
353
        skb2->nh.raw = skb2->data;
354
 
355
        /* Try to guess incoming interface */
356
        rt6i = rt6_lookup(&iph6->saddr, NULL, NULL, 0);
357
        if (rt6i && rt6i->rt6i_dev) {
358
                skb2->dev = rt6i->rt6i_dev;
359
 
360
                rt6i = rt6_lookup(&iph6->daddr, &iph6->saddr, NULL, 0);
361
 
362
                if (rt6i && rt6i->rt6i_dev && rt6i->rt6i_dev->type == ARPHRD_SIT) {
363
                        struct ip_tunnel * t = (struct ip_tunnel*)rt6i->rt6i_dev->priv;
364
                        if (rel_type == ICMPV6_TIME_EXCEED && t->parms.iph.ttl) {
365
                                rel_type = ICMPV6_DEST_UNREACH;
366
                                rel_code = ICMPV6_ADDR_UNREACH;
367
                        }
368
                        icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
369
                }
370
        }
371
        kfree_skb(skb2);
372
        return;
373
#endif
374
}
375
 
376
static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
377
{
378
        if (INET_ECN_is_ce(iph->tos) &&
379
            INET_ECN_is_not_ce(ip6_get_dsfield(skb->nh.ipv6h)))
380
                IP6_ECN_set_ce(skb->nh.ipv6h);
381
}
382
 
383
int ipip6_rcv(struct sk_buff *skb)
384
{
385
        struct iphdr *iph;
386
        struct ip_tunnel *tunnel;
387
 
388
        if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
389
                goto out;
390
 
391
        iph = skb->nh.iph;
392
 
393
        read_lock(&ipip6_lock);
394
        if ((tunnel = ipip6_tunnel_lookup(iph->saddr, iph->daddr)) != NULL) {
395
                skb->mac.raw = skb->nh.raw;
396
                skb->nh.raw = skb->data;
397
                memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
398
                skb->protocol = htons(ETH_P_IPV6);
399
                skb->pkt_type = PACKET_HOST;
400
                tunnel->stat.rx_packets++;
401
                tunnel->stat.rx_bytes += skb->len;
402
                skb->dev = tunnel->dev;
403
                dst_release(skb->dst);
404
                skb->dst = NULL;
405
#ifdef CONFIG_NETFILTER
406
                nf_conntrack_put(skb->nfct);
407
                skb->nfct = NULL;
408
#ifdef CONFIG_NETFILTER_DEBUG
409
                skb->nf_debug = 0;
410
#endif
411
#endif
412
                ipip6_ecn_decapsulate(iph, skb);
413
                netif_rx(skb);
414
                read_unlock(&ipip6_lock);
415
                return 0;
416
        }
417
 
418
        icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, 0);
419
        kfree_skb(skb);
420
        read_unlock(&ipip6_lock);
421
out:
422
        return 0;
423
}
424
 
425
/* Need this wrapper because NF_HOOK takes the function address */
426
static inline int do_ip_send(struct sk_buff *skb)
427
{
428
        return ip_send(skb);
429
}
430
 
431
 
432
/* Returns the embedded IPv4 address if the IPv6 address
433
   comes from 6to4 (draft-ietf-ngtrans-6to4-04) addr space */
434
 
435
static inline u32 try_6to4(struct in6_addr *v6dst)
436
{
437
        u32 dst = 0;
438
 
439
        if (v6dst->s6_addr16[0] == htons(0x2002)) {
440
                /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
441
                memcpy(&dst, &v6dst->s6_addr16[1], 4);
442
        }
443
        return dst;
444
}
445
 
446
/*
447
 *      This function assumes it is being called from dev_queue_xmit()
448
 *      and that skb is filled properly by that function.
449
 */
450
 
451
static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
452
{
453
        struct ip_tunnel *tunnel = (struct ip_tunnel*)dev->priv;
454
        struct net_device_stats *stats = &tunnel->stat;
455
        struct iphdr  *tiph = &tunnel->parms.iph;
456
        struct ipv6hdr *iph6 = skb->nh.ipv6h;
457
        u8     tos = tunnel->parms.iph.tos;
458
        struct rtable *rt;                      /* Route to the other host */
459
        struct net_device *tdev;                        /* Device to other host */
460
        struct iphdr  *iph;                     /* Our new IP header */
461
        int    max_headroom;                    /* The extra header space needed */
462
        u32    dst = tiph->daddr;
463
        int    mtu;
464
        struct in6_addr *addr6;
465
        int addr_type;
466
 
467
        if (tunnel->recursion++) {
468
                tunnel->stat.collisions++;
469
                goto tx_error;
470
        }
471
 
472
        if (skb->protocol != htons(ETH_P_IPV6))
473
                goto tx_error;
474
 
475
        if (!dst)
476
                dst = try_6to4(&iph6->daddr);
477
 
478
        if (!dst) {
479
                struct neighbour *neigh = NULL;
480
 
481
                if (skb->dst)
482
                        neigh = skb->dst->neighbour;
483
 
484
                if (neigh == NULL) {
485
                        if (net_ratelimit())
486
                                printk(KERN_DEBUG "sit: nexthop == NULL\n");
487
                        goto tx_error;
488
                }
489
 
490
                addr6 = (struct in6_addr*)&neigh->primary_key;
491
                addr_type = ipv6_addr_type(addr6);
492
 
493
                if (addr_type == IPV6_ADDR_ANY) {
494
                        addr6 = &skb->nh.ipv6h->daddr;
495
                        addr_type = ipv6_addr_type(addr6);
496
                }
497
 
498
                if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
499
                        goto tx_error_icmp;
500
 
501
                dst = addr6->s6_addr32[3];
502
        }
503
 
504
        if (ip_route_output(&rt, dst, tiph->saddr, RT_TOS(tos), tunnel->parms.link)) {
505
                tunnel->stat.tx_carrier_errors++;
506
                goto tx_error_icmp;
507
        }
508
        if (rt->rt_type != RTN_UNICAST) {
509
                tunnel->stat.tx_carrier_errors++;
510
                goto tx_error_icmp;
511
        }
512
        tdev = rt->u.dst.dev;
513
 
514
        if (tdev == dev) {
515
                ip_rt_put(rt);
516
                tunnel->stat.collisions++;
517
                goto tx_error;
518
        }
519
 
520
        if (tiph->frag_off)
521
                mtu = rt->u.dst.pmtu - sizeof(struct iphdr);
522
        else
523
                mtu = skb->dst ? skb->dst->pmtu : dev->mtu;
524
 
525
        if (mtu < 68) {
526
                tunnel->stat.collisions++;
527
                ip_rt_put(rt);
528
                goto tx_error;
529
        }
530
        if (mtu < IPV6_MIN_MTU)
531
                mtu = IPV6_MIN_MTU;
532
        if (skb->dst && mtu < skb->dst->pmtu) {
533
                struct rt6_info *rt6 = (struct rt6_info*)skb->dst;
534
                if (mtu < rt6->u.dst.pmtu) {
535
                        if (tunnel->parms.iph.daddr || rt6->rt6i_dst.plen == 128) {
536
                                rt6->rt6i_flags |= RTF_MODIFIED;
537
                                rt6->u.dst.pmtu = mtu;
538
                        }
539
                }
540
        }
541
        if (skb->len > mtu) {
542
                icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
543
                ip_rt_put(rt);
544
                goto tx_error;
545
        }
546
 
547
        if (tunnel->err_count > 0) {
548
                if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
549
                        tunnel->err_count--;
550
                        dst_link_failure(skb);
551
                } else
552
                        tunnel->err_count = 0;
553
        }
554
 
555
        /*
556
         * Okay, now see if we can stuff it in the buffer as-is.
557
         */
558
        max_headroom = (((tdev->hard_header_len+15)&~15)+sizeof(struct iphdr));
559
 
560
        if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) {
561
                struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
562
                if (!new_skb) {
563
                        ip_rt_put(rt);
564
                        stats->tx_dropped++;
565
                        dev_kfree_skb(skb);
566
                        tunnel->recursion--;
567
                        return 0;
568
                }
569
                if (skb->sk)
570
                        skb_set_owner_w(new_skb, skb->sk);
571
                dev_kfree_skb(skb);
572
                skb = new_skb;
573
                iph6 = skb->nh.ipv6h;
574
        }
575
 
576
        skb->h.raw = skb->nh.raw;
577
        skb->nh.raw = skb_push(skb, sizeof(struct iphdr));
578
        memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
579
        dst_release(skb->dst);
580
        skb->dst = &rt->u.dst;
581
 
582
        /*
583
         *      Push down and install the IPIP header.
584
         */
585
 
586
        iph                     =       skb->nh.iph;
587
        iph->version            =       4;
588
        iph->ihl                =       sizeof(struct iphdr)>>2;
589
        if (mtu > IPV6_MIN_MTU)
590
                iph->frag_off   =       htons(IP_DF);
591
        else
592
                iph->frag_off   =       0;
593
 
594
        iph->protocol           =       IPPROTO_IPV6;
595
        iph->tos                =       INET_ECN_encapsulate(tos, ip6_get_dsfield(iph6));
596
        iph->daddr              =       rt->rt_dst;
597
        iph->saddr              =       rt->rt_src;
598
 
599
        if ((iph->ttl = tiph->ttl) == 0)
600
                iph->ttl        =       iph6->hop_limit;
601
 
602
#ifdef CONFIG_NETFILTER
603
        nf_conntrack_put(skb->nfct);
604
        skb->nfct = NULL;
605
#ifdef CONFIG_NETFILTER_DEBUG
606
        skb->nf_debug = 0;
607
#endif
608
#endif
609
 
610
        IPTUNNEL_XMIT();
611
        tunnel->recursion--;
612
        return 0;
613
 
614
tx_error_icmp:
615
        dst_link_failure(skb);
616
tx_error:
617
        stats->tx_errors++;
618
        dev_kfree_skb(skb);
619
        tunnel->recursion--;
620
        return 0;
621
}
622
 
623
static int
624
ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
625
{
626
        int err = 0;
627
        struct ip_tunnel_parm p;
628
        struct ip_tunnel *t;
629
 
630
        MOD_INC_USE_COUNT;
631
 
632
        switch (cmd) {
633
        case SIOCGETTUNNEL:
634
                t = NULL;
635
                if (dev == &ipip6_fb_tunnel_dev) {
636
                        if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
637
                                err = -EFAULT;
638
                                break;
639
                        }
640
                        t = ipip6_tunnel_locate(&p, 0);
641
                }
642
                if (t == NULL)
643
                        t = (struct ip_tunnel*)dev->priv;
644
                memcpy(&p, &t->parms, sizeof(p));
645
                if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
646
                        err = -EFAULT;
647
                break;
648
 
649
        case SIOCADDTUNNEL:
650
        case SIOCCHGTUNNEL:
651
                err = -EPERM;
652
                if (!capable(CAP_NET_ADMIN))
653
                        goto done;
654
 
655
                err = -EFAULT;
656
                if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
657
                        goto done;
658
 
659
                err = -EINVAL;
660
                if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
661
                    p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
662
                        goto done;
663
                if (p.iph.ttl)
664
                        p.iph.frag_off |= htons(IP_DF);
665
 
666
                t = ipip6_tunnel_locate(&p, cmd == SIOCADDTUNNEL);
667
 
668
                if (dev != &ipip6_fb_tunnel_dev && cmd == SIOCCHGTUNNEL &&
669
                    t != &ipip6_fb_tunnel) {
670
                        if (t != NULL) {
671
                                if (t->dev != dev) {
672
                                        err = -EEXIST;
673
                                        break;
674
                                }
675
                        } else {
676
                                if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
677
                                    (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
678
                                        err = -EINVAL;
679
                                        break;
680
                                }
681
                                t = (struct ip_tunnel*)dev->priv;
682
                                ipip6_tunnel_unlink(t);
683
                                t->parms.iph.saddr = p.iph.saddr;
684
                                t->parms.iph.daddr = p.iph.daddr;
685
                                memcpy(dev->dev_addr, &p.iph.saddr, 4);
686
                                memcpy(dev->broadcast, &p.iph.daddr, 4);
687
                                ipip6_tunnel_link(t);
688
                                netdev_state_change(dev);
689
                        }
690
                }
691
 
692
                if (t) {
693
                        err = 0;
694
                        if (cmd == SIOCCHGTUNNEL) {
695
                                t->parms.iph.ttl = p.iph.ttl;
696
                                t->parms.iph.tos = p.iph.tos;
697
                        }
698
                        if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
699
                                err = -EFAULT;
700
                } else
701
                        err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
702
                break;
703
 
704
        case SIOCDELTUNNEL:
705
                err = -EPERM;
706
                if (!capable(CAP_NET_ADMIN))
707
                        goto done;
708
 
709
                if (dev == &ipip6_fb_tunnel_dev) {
710
                        err = -EFAULT;
711
                        if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
712
                                goto done;
713
                        err = -ENOENT;
714
                        if ((t = ipip6_tunnel_locate(&p, 0)) == NULL)
715
                                goto done;
716
                        err = -EPERM;
717
                        if (t == &ipip6_fb_tunnel)
718
                                goto done;
719
                        dev = t->dev;
720
                }
721
                err = unregister_netdevice(dev);
722
                break;
723
 
724
        default:
725
                err = -EINVAL;
726
        }
727
 
728
done:
729
        MOD_DEC_USE_COUNT;
730
        return err;
731
}
732
 
733
static struct net_device_stats *ipip6_tunnel_get_stats(struct net_device *dev)
734
{
735
        return &(((struct ip_tunnel*)dev->priv)->stat);
736
}
737
 
738
static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
739
{
740
        if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
741
                return -EINVAL;
742
        dev->mtu = new_mtu;
743
        return 0;
744
}
745
 
746
static void ipip6_tunnel_init_gen(struct net_device *dev)
747
{
748
        struct ip_tunnel *t = (struct ip_tunnel*)dev->priv;
749
 
750
        dev->destructor         = ipip6_tunnel_destructor;
751
        dev->uninit             = ipip6_tunnel_uninit;
752
        dev->hard_start_xmit    = ipip6_tunnel_xmit;
753
        dev->get_stats          = ipip6_tunnel_get_stats;
754
        dev->do_ioctl           = ipip6_tunnel_ioctl;
755
        dev->change_mtu         = ipip6_tunnel_change_mtu;
756
 
757
        dev->type               = ARPHRD_SIT;
758
        dev->hard_header_len    = LL_MAX_HEADER + sizeof(struct iphdr);
759
        dev->mtu                = 1500 - sizeof(struct iphdr);
760
        dev->flags              = IFF_NOARP;
761
        dev->iflink             = 0;
762
        dev->addr_len           = 4;
763
        memcpy(dev->dev_addr, &t->parms.iph.saddr, 4);
764
        memcpy(dev->broadcast, &t->parms.iph.daddr, 4);
765
}
766
 
767
static int ipip6_tunnel_init(struct net_device *dev)
768
{
769
        struct net_device *tdev = NULL;
770
        struct ip_tunnel *tunnel;
771
        struct iphdr *iph;
772
 
773
        tunnel = (struct ip_tunnel*)dev->priv;
774
        iph = &tunnel->parms.iph;
775
 
776
        ipip6_tunnel_init_gen(dev);
777
 
778
        if (iph->daddr) {
779
                struct rtable *rt;
780
                if (!ip_route_output(&rt, iph->daddr, iph->saddr, RT_TOS(iph->tos), tunnel->parms.link)) {
781
                        tdev = rt->u.dst.dev;
782
                        ip_rt_put(rt);
783
                }
784
                dev->flags |= IFF_POINTOPOINT;
785
        }
786
 
787
        if (!tdev && tunnel->parms.link)
788
                tdev = __dev_get_by_index(tunnel->parms.link);
789
 
790
        if (tdev) {
791
                dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
792
                dev->mtu = tdev->mtu - sizeof(struct iphdr);
793
                if (dev->mtu < IPV6_MIN_MTU)
794
                        dev->mtu = IPV6_MIN_MTU;
795
        }
796
        dev->iflink = tunnel->parms.link;
797
 
798
        return 0;
799
}
800
 
801
#ifdef MODULE
802
static int ipip6_fb_tunnel_open(struct net_device *dev)
803
{
804
        MOD_INC_USE_COUNT;
805
        return 0;
806
}
807
 
808
static int ipip6_fb_tunnel_close(struct net_device *dev)
809
{
810
        MOD_DEC_USE_COUNT;
811
        return 0;
812
}
813
#endif
814
 
815
int __init ipip6_fb_tunnel_init(struct net_device *dev)
816
{
817
        struct iphdr *iph;
818
 
819
        ipip6_tunnel_init_gen(dev);
820
#ifdef MODULE
821
        dev->open               = ipip6_fb_tunnel_open;
822
        dev->stop               = ipip6_fb_tunnel_close;
823
#endif
824
 
825
        iph = &ipip6_fb_tunnel.parms.iph;
826
        iph->version            = 4;
827
        iph->protocol           = IPPROTO_IPV6;
828
        iph->ihl                = 5;
829
        iph->ttl                = 64;
830
 
831
        dev_hold(dev);
832
        tunnels_wc[0]            = &ipip6_fb_tunnel;
833
        return 0;
834
}
835
 
836
static struct inet_protocol sit_protocol = {
837
        ipip6_rcv,
838
        ipip6_err,
839
        0,
840
        IPPROTO_IPV6,
841
        0,
842
        NULL,
843
        "IPv6"
844
};
845
 
846
#ifdef MODULE
847
void sit_cleanup(void)
848
{
849
        inet_del_protocol(&sit_protocol);
850
        unregister_netdev(&ipip6_fb_tunnel_dev);
851
}
852
#endif
853
 
854
int __init sit_init(void)
855
{
856
        printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
857
 
858
        ipip6_fb_tunnel_dev.priv = (void*)&ipip6_fb_tunnel;
859
        strcpy(ipip6_fb_tunnel_dev.name, ipip6_fb_tunnel.parms.name);
860
        register_netdev(&ipip6_fb_tunnel_dev);
861
        inet_add_protocol(&sit_protocol);
862
        return 0;
863
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.