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

Subversion Repositories or1k

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/* net/atm/clip.c - RFC1577 Classical IP over ATM */
2
 
3
/* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
4
 
5
 
6
#include <linux/config.h>
7
#include <linux/string.h>
8
#include <linux/errno.h>
9
#include <linux/kernel.h> /* for UINT_MAX */
10
#include <linux/module.h>
11
#include <linux/init.h>
12
#include <linux/netdevice.h>
13
#include <linux/skbuff.h>
14
#include <linux/wait.h>
15
#include <linux/timer.h>
16
#include <linux/if_arp.h> /* for some manifest constants */
17
#include <linux/notifier.h>
18
#include <linux/atm.h>
19
#include <linux/atmdev.h>
20
#include <linux/atmclip.h>
21
#include <linux/atmarp.h>
22
#include <linux/ip.h> /* for net/route.h */
23
#include <linux/in.h> /* for struct sockaddr_in */
24
#include <linux/if.h> /* for IFF_UP */
25
#include <linux/inetdevice.h>
26
#include <linux/bitops.h>
27
#include <net/route.h> /* for struct rtable and routing */
28
#include <net/icmp.h> /* icmp_send */
29
#include <asm/param.h> /* for HZ */
30
#include <asm/byteorder.h> /* for htons etc. */
31
#include <asm/system.h> /* save/restore_flags */
32
#include <asm/uaccess.h>
33
#include <asm/atomic.h>
34
 
35
#include "common.h"
36
#include "resources.h"
37
#include "ipcommon.h"
38
#include <net/atmclip.h>
39
 
40
 
41
#if 0
42
#define DPRINTK(format,args...) printk(format,##args)
43
#else
44
#define DPRINTK(format,args...)
45
#endif
46
 
47
 
48
struct net_device *clip_devs = NULL;
49
struct atm_vcc *atmarpd = NULL;
50
static struct neigh_table clip_tbl;
51
static struct timer_list idle_timer;
52
static int start_timer = 1;
53
 
54
 
55
static int to_atmarpd(enum atmarp_ctrl_type type,int itf,unsigned long ip)
56
{
57
        struct atmarp_ctrl *ctrl;
58
        struct sk_buff *skb;
59
 
60
        DPRINTK("to_atmarpd(%d)\n",type);
61
        if (!atmarpd) return -EUNATCH;
62
        skb = alloc_skb(sizeof(struct atmarp_ctrl),GFP_ATOMIC);
63
        if (!skb) return -ENOMEM;
64
        ctrl = (struct atmarp_ctrl *) skb_put(skb,sizeof(struct atmarp_ctrl));
65
        ctrl->type = type;
66
        ctrl->itf_num = itf;
67
        ctrl->ip = ip;
68
        atm_force_charge(atmarpd,skb->truesize);
69
        skb_queue_tail(&atmarpd->sk->receive_queue,skb);
70
        wake_up(&atmarpd->sleep);
71
        return 0;
72
}
73
 
74
 
75
static void link_vcc(struct clip_vcc *clip_vcc,struct atmarp_entry *entry)
76
{
77
        DPRINTK("link_vcc %p to entry %p (neigh %p)\n",clip_vcc,entry,
78
            entry->neigh);
79
        clip_vcc->entry = entry;
80
        clip_vcc->xoff = 0; /* @@@ may overrun buffer by one packet */
81
        clip_vcc->next = entry->vccs;
82
        entry->vccs = clip_vcc;
83
        entry->neigh->used = jiffies;
84
}
85
 
86
 
87
static void unlink_clip_vcc(struct clip_vcc *clip_vcc)
88
{
89
        struct atmarp_entry *entry = clip_vcc->entry;
90
        struct clip_vcc **walk;
91
 
92
        if (!entry) {
93
                printk(KERN_CRIT "!clip_vcc->entry (clip_vcc %p)\n",clip_vcc);
94
                return;
95
        }
96
        spin_lock_bh(&entry->neigh->dev->xmit_lock);    /* block clip_start_xmit() */
97
        entry->neigh->used = jiffies;
98
        for (walk = &entry->vccs; *walk; walk = &(*walk)->next)
99
                if (*walk == clip_vcc) {
100
                        int error;
101
 
102
                        *walk = clip_vcc->next; /* atomic */
103
                        clip_vcc->entry = NULL;
104
                        if (clip_vcc->xoff)
105
                                netif_wake_queue(entry->neigh->dev);
106
                        if (entry->vccs)
107
                                goto out;
108
                        entry->expires = jiffies-1;
109
                                /* force resolution or expiration */
110
                        error = neigh_update(entry->neigh,NULL,NUD_NONE,0,0);
111
                        if (error)
112
                                printk(KERN_CRIT "unlink_clip_vcc: "
113
                                    "neigh_update failed with %d\n",error);
114
                        goto out;
115
                }
116
        printk(KERN_CRIT "ATMARP: unlink_clip_vcc failed (entry %p, vcc "
117
          "0x%p)\n",entry,clip_vcc);
118
out:
119
        spin_unlock_bh(&entry->neigh->dev->xmit_lock);
120
}
121
 
122
 
123
static void idle_timer_check(unsigned long dummy)
124
{
125
        int i;
126
 
127
        /*DPRINTK("idle_timer_check\n");*/
128
        write_lock(&clip_tbl.lock);
129
        for (i = 0; i <= NEIGH_HASHMASK; i++) {
130
                struct neighbour **np;
131
 
132
                for (np = &clip_tbl.hash_buckets[i]; *np;) {
133
                        struct neighbour *n = *np;
134
                        struct atmarp_entry *entry = NEIGH2ENTRY(n);
135
                        struct clip_vcc *clip_vcc;
136
 
137
                        write_lock(&n->lock);
138
 
139
                        for (clip_vcc = entry->vccs; clip_vcc;
140
                            clip_vcc = clip_vcc->next)
141
                                if (clip_vcc->idle_timeout &&
142
                                    time_after(jiffies, clip_vcc->last_use+
143
                                    clip_vcc->idle_timeout)) {
144
                                        DPRINTK("releasing vcc %p->%p of "
145
                                            "entry %p\n",clip_vcc,clip_vcc->vcc,
146
                                            entry);
147
                                        vcc_release_async(clip_vcc->vcc,
148
                                                          -ETIMEDOUT);
149
                                }
150
                        if (entry->vccs ||
151
                            time_before(jiffies, entry->expires)) {
152
                                np = &n->next;
153
                                write_unlock(&n->lock);
154
                                continue;
155
                        }
156
                        if (atomic_read(&n->refcnt) > 1) {
157
                                struct sk_buff *skb;
158
 
159
                                DPRINTK("destruction postponed with ref %d\n",
160
                                    atomic_read(&n->refcnt));
161
                                while ((skb = skb_dequeue(&n->arp_queue)) !=
162
                                     NULL)
163
                                        dev_kfree_skb(skb);
164
                                np = &n->next;
165
                                write_unlock(&n->lock);
166
                                continue;
167
                        }
168
                        *np = n->next;
169
                        DPRINTK("expired neigh %p\n",n);
170
                        n->dead = 1;
171
                        write_unlock(&n->lock);
172
                        neigh_release(n);
173
                }
174
        }
175
        mod_timer(&idle_timer, jiffies+CLIP_CHECK_INTERVAL*HZ);
176
        write_unlock(&clip_tbl.lock);
177
}
178
 
179
 
180
static int clip_arp_rcv(struct sk_buff *skb)
181
{
182
        struct atm_vcc *vcc;
183
 
184
        DPRINTK("clip_arp_rcv\n");
185
        vcc = ATM_SKB(skb)->vcc;
186
        if (!vcc || !atm_charge(vcc,skb->truesize)) {
187
                dev_kfree_skb_any(skb);
188
                return 0;
189
        }
190
        DPRINTK("pushing to %p\n",vcc);
191
        DPRINTK("using %p\n",CLIP_VCC(vcc)->old_push);
192
        CLIP_VCC(vcc)->old_push(vcc,skb);
193
        return 0;
194
}
195
 
196
 
197
static void clip_push(struct atm_vcc *vcc,struct sk_buff *skb)
198
{
199
        struct clip_vcc *clip_vcc = CLIP_VCC(vcc);
200
 
201
        DPRINTK("clip push\n");
202
        if (!skb) {
203
                DPRINTK("removing VCC %p\n",clip_vcc);
204
                if (clip_vcc->entry) unlink_clip_vcc(clip_vcc);
205
                clip_vcc->old_push(vcc,NULL); /* pass on the bad news */
206
                kfree(clip_vcc);
207
                return;
208
        }
209
        atm_return(vcc,skb->truesize);
210
        skb->dev = clip_vcc->entry ? clip_vcc->entry->neigh->dev : clip_devs;
211
                /* clip_vcc->entry == NULL if we don't have an IP address yet */
212
        if (!skb->dev) {
213
                dev_kfree_skb_any(skb);
214
                return;
215
        }
216
        ATM_SKB(skb)->vcc = vcc;
217
        skb->mac.raw = skb->data;
218
        if (!clip_vcc->encap || skb->len < RFC1483LLC_LEN || memcmp(skb->data,
219
            llc_oui,sizeof(llc_oui))) skb->protocol = htons(ETH_P_IP);
220
        else {
221
                skb->protocol = ((u16 *) skb->data)[3];
222
                skb_pull(skb,RFC1483LLC_LEN);
223
                if (skb->protocol == htons(ETH_P_ARP)) {
224
                        PRIV(skb->dev)->stats.rx_packets++;
225
                        PRIV(skb->dev)->stats.rx_bytes += skb->len;
226
                        clip_arp_rcv(skb);
227
                        return;
228
                }
229
        }
230
        clip_vcc->last_use = jiffies;
231
        PRIV(skb->dev)->stats.rx_packets++;
232
        PRIV(skb->dev)->stats.rx_bytes += skb->len;
233
        memset(ATM_SKB(skb), 0, sizeof(struct atm_skb_data));
234
        netif_rx(skb);
235
}
236
 
237
 
238
/*
239
 * Note: these spinlocks _must_not_ block on non-SMP. The only goal is that
240
 * clip_pop is atomic with respect to the critical section in clip_start_xmit.
241
 */
242
 
243
 
244
static void clip_pop(struct atm_vcc *vcc,struct sk_buff *skb)
245
{
246
        struct clip_vcc *clip_vcc = CLIP_VCC(vcc);
247
        struct net_device *dev = skb->dev;
248
        int old;
249
        unsigned long flags;
250
 
251
        DPRINTK("clip_pop(vcc %p)\n",vcc);
252
        clip_vcc->old_pop(vcc,skb);
253
        /* skb->dev == NULL in outbound ARP packets */
254
        if (!dev) return;
255
        spin_lock_irqsave(&PRIV(dev)->xoff_lock,flags);
256
        if (atm_may_send(vcc,0)) {
257
                old = xchg(&clip_vcc->xoff,0);
258
                if (old) netif_wake_queue(dev);
259
        }
260
        spin_unlock_irqrestore(&PRIV(dev)->xoff_lock,flags);
261
}
262
 
263
 
264
static void clip_neigh_destroy(struct neighbour *neigh)
265
{
266
        DPRINTK("clip_neigh_destroy (neigh %p)\n",neigh);
267
        if (NEIGH2ENTRY(neigh)->vccs)
268
                printk(KERN_CRIT "clip_neigh_destroy: vccs != NULL !!!\n");
269
        NEIGH2ENTRY(neigh)->vccs = (void *) 0xdeadbeef;
270
}
271
 
272
 
273
static void clip_neigh_solicit(struct neighbour *neigh,struct sk_buff *skb)
274
{
275
        DPRINTK("clip_neigh_solicit (neigh %p, skb %p)\n",neigh,skb);
276
        to_atmarpd(act_need,PRIV(neigh->dev)->number,NEIGH2ENTRY(neigh)->ip);
277
}
278
 
279
 
280
static void clip_neigh_error(struct neighbour *neigh,struct sk_buff *skb)
281
{
282
#ifndef CONFIG_ATM_CLIP_NO_ICMP
283
        icmp_send(skb,ICMP_DEST_UNREACH,ICMP_HOST_UNREACH,0);
284
#endif
285
        kfree_skb(skb);
286
}
287
 
288
 
289
static struct neigh_ops clip_neigh_ops = {
290
        family:                 AF_INET,
291
        destructor:             clip_neigh_destroy,
292
        solicit:                clip_neigh_solicit,
293
        error_report:           clip_neigh_error,
294
        output:                 dev_queue_xmit,
295
        connected_output:       dev_queue_xmit,
296
        hh_output:              dev_queue_xmit,
297
        queue_xmit:             dev_queue_xmit,
298
};
299
 
300
 
301
static int clip_constructor(struct neighbour *neigh)
302
{
303
        struct atmarp_entry *entry = NEIGH2ENTRY(neigh);
304
        struct net_device *dev = neigh->dev;
305
        struct in_device *in_dev = dev->ip_ptr;
306
 
307
        DPRINTK("clip_constructor (neigh %p, entry %p)\n",neigh,entry);
308
        if (!in_dev) return -EINVAL;
309
        neigh->type = inet_addr_type(entry->ip);
310
        if (neigh->type != RTN_UNICAST) return -EINVAL;
311
        if (in_dev->arp_parms) neigh->parms = in_dev->arp_parms;
312
        neigh->ops = &clip_neigh_ops;
313
        neigh->output = neigh->nud_state & NUD_VALID ?
314
            neigh->ops->connected_output : neigh->ops->output;
315
        entry->neigh = neigh;
316
        entry->vccs = NULL;
317
        entry->expires = jiffies-1;
318
        return 0;
319
}
320
 
321
static u32 clip_hash(const void *pkey, const struct net_device *dev)
322
{
323
        u32 hash_val;
324
 
325
        hash_val = *(u32*)pkey;
326
        hash_val ^= (hash_val>>16);
327
        hash_val ^= hash_val>>8;
328
        hash_val ^= hash_val>>3;
329
        hash_val = (hash_val^dev->ifindex)&NEIGH_HASHMASK;
330
 
331
        return hash_val;
332
}
333
 
334
 
335
static struct neigh_table clip_tbl = {
336
        NULL,                   /* next */
337
        AF_INET,                /* family */
338
        sizeof(struct neighbour)+sizeof(struct atmarp_entry), /* entry_size */
339
        4,                      /* key_len */
340
        clip_hash,
341
        clip_constructor,       /* constructor */
342
        NULL,                   /* pconstructor */
343
        NULL,                   /* pdestructor */
344
        NULL,                   /* proxy_redo */
345
        "clip_arp_cache",
346
        {                       /* neigh_parms */
347
                NULL,           /* next */
348
                NULL,           /* neigh_setup */
349
                &clip_tbl,      /* tbl */
350
                0,               /* entries */
351
                NULL,           /* priv */
352
                NULL,           /* sysctl_table */
353
                30*HZ,          /* base_reachable_time */
354
                1*HZ,           /* retrans_time */
355
                60*HZ,          /* gc_staletime */
356
                30*HZ,          /* reachable_time */
357
                5*HZ,           /* delay_probe_time */
358
                3,              /* queue_len */
359
                3,              /* ucast_probes */
360
                0,               /* app_probes */
361
                3,              /* mcast_probes */
362
                1*HZ,           /* anycast_delay */
363
                (8*HZ)/10,      /* proxy_delay */
364
                1*HZ,           /* proxy_qlen */
365
                64              /* locktime */
366
        },
367
        30*HZ,128,512,1024      /* copied from ARP ... */
368
};
369
 
370
 
371
/* @@@ copy bh locking from arp.c -- need to bh-enable atm code before */
372
 
373
/*
374
 * We play with the resolve flag: 0 and 1 have the usual meaning, but -1 means
375
 * to allocate the neighbour entry but not to ask atmarpd for resolution. Also,
376
 * don't increment the usage count. This is used to create entries in
377
 * clip_setentry.
378
 */
379
 
380
 
381
static int clip_encap(struct atm_vcc *vcc,int mode)
382
{
383
        CLIP_VCC(vcc)->encap = mode;
384
        return 0;
385
}
386
 
387
 
388
static int clip_start_xmit(struct sk_buff *skb,struct net_device *dev)
389
{
390
        struct clip_priv *clip_priv = PRIV(dev);
391
        struct atmarp_entry *entry;
392
        struct atm_vcc *vcc;
393
        int old;
394
        unsigned long flags;
395
 
396
        DPRINTK("clip_start_xmit (skb %p)\n",skb);
397
        if (!skb->dst) {
398
                printk(KERN_ERR "clip_start_xmit: skb->dst == NULL\n");
399
                dev_kfree_skb(skb);
400
                clip_priv->stats.tx_dropped++;
401
                return 0;
402
        }
403
        if (!skb->dst->neighbour) {
404
#if 0
405
                skb->dst->neighbour = clip_find_neighbour(skb->dst,1);
406
                if (!skb->dst->neighbour) {
407
                        dev_kfree_skb(skb); /* lost that one */
408
                        clip_priv->stats.tx_dropped++;
409
                        return 0;
410
                }
411
#endif
412
                printk(KERN_ERR "clip_start_xmit: NO NEIGHBOUR !\n");
413
                dev_kfree_skb(skb);
414
                clip_priv->stats.tx_dropped++;
415
                return 0;
416
        }
417
        entry = NEIGH2ENTRY(skb->dst->neighbour);
418
        if (!entry->vccs) {
419
                if (time_after(jiffies, entry->expires)) {
420
                        /* should be resolved */
421
                        entry->expires = jiffies+ATMARP_RETRY_DELAY*HZ;
422
                        to_atmarpd(act_need,PRIV(dev)->number,entry->ip);
423
                }
424
                if (entry->neigh->arp_queue.qlen < ATMARP_MAX_UNRES_PACKETS)
425
                        skb_queue_tail(&entry->neigh->arp_queue,skb);
426
                else {
427
                        dev_kfree_skb(skb);
428
                        clip_priv->stats.tx_dropped++;
429
                }
430
                return 0;
431
        }
432
        DPRINTK("neigh %p, vccs %p\n",entry,entry->vccs);
433
        ATM_SKB(skb)->vcc = vcc = entry->vccs->vcc;
434
        DPRINTK("using neighbour %p, vcc %p\n",skb->dst->neighbour,vcc);
435
        if (entry->vccs->encap) {
436
                void *here;
437
 
438
                here = skb_push(skb,RFC1483LLC_LEN);
439
                memcpy(here,llc_oui,sizeof(llc_oui));
440
                ((u16 *) here)[3] = skb->protocol;
441
        }
442
        atomic_add(skb->truesize,&vcc->sk->wmem_alloc);
443
        ATM_SKB(skb)->atm_options = vcc->atm_options;
444
        entry->vccs->last_use = jiffies;
445
        DPRINTK("atm_skb(%p)->vcc(%p)->dev(%p)\n",skb,vcc,vcc->dev);
446
        old = xchg(&entry->vccs->xoff,1); /* assume XOFF ... */
447
        if (old) {
448
                printk(KERN_WARNING "clip_start_xmit: XOFF->XOFF transition\n");
449
                return 0;
450
        }
451
        clip_priv->stats.tx_packets++;
452
        clip_priv->stats.tx_bytes += skb->len;
453
        (void) vcc->send(vcc,skb);
454
        if (atm_may_send(vcc,0)) {
455
                entry->vccs->xoff = 0;
456
                return 0;
457
        }
458
        spin_lock_irqsave(&clip_priv->xoff_lock,flags);
459
        netif_stop_queue(dev); /* XOFF -> throttle immediately */
460
        barrier();
461
        if (!entry->vccs->xoff)
462
                netif_start_queue(dev);
463
                /* Oh, we just raced with clip_pop. netif_start_queue should be
464
                   good enough, because nothing should really be asleep because
465
                   of the brief netif_stop_queue. If this isn't true or if it
466
                   changes, use netif_wake_queue instead. */
467
        spin_unlock_irqrestore(&clip_priv->xoff_lock,flags);
468
        return 0;
469
}
470
 
471
 
472
static struct net_device_stats *clip_get_stats(struct net_device *dev)
473
{
474
        return &PRIV(dev)->stats;
475
}
476
 
477
 
478
static int clip_mkip(struct atm_vcc *vcc,int timeout)
479
{
480
        struct clip_vcc *clip_vcc;
481
        struct sk_buff_head copy;
482
        struct sk_buff *skb;
483
 
484
        if (!vcc->push) return -EBADFD;
485
        clip_vcc = kmalloc(sizeof(struct clip_vcc),GFP_KERNEL);
486
        if (!clip_vcc) return -ENOMEM;
487
        DPRINTK("mkip clip_vcc %p vcc %p\n",clip_vcc,vcc);
488
        clip_vcc->vcc = vcc;
489
        vcc->user_back = clip_vcc;
490
        clip_vcc->entry = NULL;
491
        clip_vcc->xoff = 0;
492
        clip_vcc->encap = 1;
493
        clip_vcc->last_use = jiffies;
494
        clip_vcc->idle_timeout = timeout*HZ;
495
        clip_vcc->old_push = vcc->push;
496
        clip_vcc->old_pop = vcc->pop;
497
        vcc->push = clip_push;
498
        vcc->pop = clip_pop;
499
        skb_queue_head_init(&copy);
500
        skb_migrate(&vcc->sk->receive_queue,&copy);
501
        /* re-process everything received between connection setup and MKIP */
502
        while ((skb = skb_dequeue(&copy)))
503
                if (!clip_devs) {
504
                        atm_return(vcc,skb->truesize);
505
                        kfree_skb(skb);
506
                }
507
                else {
508
                        unsigned int len = skb->len;
509
 
510
                        clip_push(vcc,skb);
511
                        PRIV(skb->dev)->stats.rx_packets--;
512
                        PRIV(skb->dev)->stats.rx_bytes -= len;
513
                }
514
        return 0;
515
}
516
 
517
 
518
static int clip_setentry(struct atm_vcc *vcc,u32 ip)
519
{
520
        struct neighbour *neigh;
521
        struct atmarp_entry *entry;
522
        int error;
523
        struct clip_vcc *clip_vcc;
524
        struct rtable *rt;
525
 
526
        if (vcc->push != clip_push) {
527
                printk(KERN_WARNING "clip_setentry: non-CLIP VCC\n");
528
                return -EBADF;
529
        }
530
        clip_vcc = CLIP_VCC(vcc);
531
        if (!ip) {
532
                if (!clip_vcc->entry) {
533
                        printk(KERN_ERR "hiding hidden ATMARP entry\n");
534
                        return 0;
535
                }
536
                DPRINTK("setentry: remove\n");
537
                unlink_clip_vcc(clip_vcc);
538
                return 0;
539
        }
540
        error = ip_route_output(&rt,ip,0,1,0);
541
        if (error) return error;
542
        neigh = __neigh_lookup(&clip_tbl,&ip,rt->u.dst.dev,1);
543
        ip_rt_put(rt);
544
        if (!neigh)
545
                return -ENOMEM;
546
        entry = NEIGH2ENTRY(neigh);
547
        if (entry != clip_vcc->entry) {
548
                if (!clip_vcc->entry) DPRINTK("setentry: add\n");
549
                else {
550
                        DPRINTK("setentry: update\n");
551
                        unlink_clip_vcc(clip_vcc);
552
                }
553
                link_vcc(clip_vcc,entry);
554
        }
555
        error = neigh_update(neigh,llc_oui,NUD_PERMANENT,1,0);
556
        neigh_release(neigh);
557
        return error;
558
}
559
 
560
 
561
static int clip_init(struct net_device *dev)
562
{
563
        DPRINTK("clip_init %s\n",dev->name);
564
        dev->hard_start_xmit = clip_start_xmit;
565
        /* sg_xmit ... */
566
        dev->hard_header = NULL;
567
        dev->rebuild_header = NULL;
568
        dev->set_mac_address = NULL;
569
        dev->hard_header_parse = NULL;
570
        dev->hard_header_cache = NULL;
571
        dev->header_cache_update = NULL;
572
        dev->change_mtu = NULL;
573
        dev->do_ioctl = NULL;
574
        dev->get_stats = clip_get_stats;
575
        dev->type = ARPHRD_ATM;
576
        dev->hard_header_len = RFC1483LLC_LEN;
577
        dev->mtu = RFC1626_MTU;
578
        dev->addr_len = 0;
579
        dev->tx_queue_len = 100; /* "normal" queue (packets) */
580
            /* When using a "real" qdisc, the qdisc determines the queue */
581
            /* length. tx_queue_len is only used for the default case, */
582
            /* without any more elaborate queuing. 100 is a reasonable */
583
            /* compromise between decent burst-tolerance and protection */
584
            /* against memory hogs. */
585
        dev->flags = 0;
586
        return 0;
587
}
588
 
589
 
590
static int clip_create(int number)
591
{
592
        struct net_device *dev;
593
        struct clip_priv *clip_priv;
594
        int error;
595
 
596
        if (number != -1) {
597
                for (dev = clip_devs; dev; dev = PRIV(dev)->next)
598
                        if (PRIV(dev)->number == number) return -EEXIST;
599
        }
600
        else {
601
                number = 0;
602
                for (dev = clip_devs; dev; dev = PRIV(dev)->next)
603
                        if (PRIV(dev)->number >= number)
604
                                number = PRIV(dev)->number+1;
605
        }
606
        dev = kmalloc(sizeof(struct net_device)+sizeof(struct clip_priv),
607
            GFP_KERNEL);
608
        if (!dev) return -ENOMEM;
609
        memset(dev,0,sizeof(struct net_device)+sizeof(struct clip_priv));
610
        clip_priv = PRIV(dev);
611
        sprintf(dev->name,"atm%d",number);
612
        dev->init = clip_init;
613
        spin_lock_init(&clip_priv->xoff_lock);
614
        clip_priv->number = number;
615
        error = register_netdev(dev);
616
        if (error) {
617
                kfree(dev);
618
                return error;
619
        }
620
        clip_priv->next = clip_devs;
621
        clip_devs = dev;
622
        DPRINTK("registered (net:%s)\n",dev->name);
623
        return number;
624
}
625
 
626
 
627
static int clip_device_event(struct notifier_block *this,unsigned long event,
628
    void *dev)
629
{
630
        /* ignore non-CLIP devices */
631
        if (((struct net_device *) dev)->type != ARPHRD_ATM ||
632
            ((struct net_device *) dev)->init != clip_init)
633
                return NOTIFY_DONE;
634
        switch (event) {
635
                case NETDEV_UP:
636
                        DPRINTK("clip_device_event NETDEV_UP\n");
637
                        (void) to_atmarpd(act_up,PRIV(dev)->number,0);
638
                        break;
639
                case NETDEV_GOING_DOWN:
640
                        DPRINTK("clip_device_event NETDEV_DOWN\n");
641
                        (void) to_atmarpd(act_down,PRIV(dev)->number,0);
642
                        break;
643
                case NETDEV_CHANGE:
644
                case NETDEV_CHANGEMTU:
645
                        DPRINTK("clip_device_event NETDEV_CHANGE*\n");
646
                        (void) to_atmarpd(act_change,PRIV(dev)->number,0);
647
                        break;
648
                case NETDEV_REBOOT:
649
                case NETDEV_REGISTER:
650
                case NETDEV_DOWN:
651
                        DPRINTK("clip_device_event %ld\n",event);
652
                        /* ignore */
653
                        break;
654
                default:
655
                        printk(KERN_WARNING "clip_device_event: unknown event "
656
                            "%ld\n",event);
657
                        break;
658
        }
659
        return NOTIFY_DONE;
660
}
661
 
662
 
663
static int clip_inet_event(struct notifier_block *this,unsigned long event,
664
    void *ifa)
665
{
666
        struct in_device *in_dev;
667
 
668
        in_dev = ((struct in_ifaddr *) ifa)->ifa_dev;
669
        if (!in_dev || !in_dev->dev) {
670
                printk(KERN_WARNING "clip_inet_event: no device\n");
671
                return NOTIFY_DONE;
672
        }
673
        /*
674
         * Transitions are of the down-change-up type, so it's sufficient to
675
         * handle the change on up.
676
         */
677
        if (event != NETDEV_UP) return NOTIFY_DONE;
678
        return clip_device_event(this,NETDEV_CHANGE,in_dev->dev);
679
}
680
 
681
 
682
static struct notifier_block clip_dev_notifier = {
683
        clip_device_event,
684
        NULL,
685
 
686
};
687
 
688
 
689
 
690
static struct notifier_block clip_inet_notifier = {
691
        clip_inet_event,
692
        NULL,
693
 
694
};
695
 
696
 
697
 
698
static void atmarpd_close(struct atm_vcc *vcc)
699
{
700
        DPRINTK("atmarpd_close\n");
701
        atmarpd = NULL; /* assumed to be atomic */
702
        barrier();
703
        unregister_inetaddr_notifier(&clip_inet_notifier);
704
        unregister_netdevice_notifier(&clip_dev_notifier);
705
        if (skb_peek(&vcc->sk->receive_queue))
706
                printk(KERN_ERR "atmarpd_close: closing with requests "
707
                    "pending\n");
708
        skb_queue_purge(&vcc->sk->receive_queue);
709
        DPRINTK("(done)\n");
710
        MOD_DEC_USE_COUNT;
711
}
712
 
713
 
714
static struct atmdev_ops atmarpd_dev_ops = {
715
        .close = atmarpd_close,
716
};
717
 
718
 
719
static struct atm_dev atmarpd_dev = {
720
        .ops =          &atmarpd_dev_ops,
721
        .type =         "arpd",
722
        .number =       999,
723
        .lock =         SPIN_LOCK_UNLOCKED
724
};
725
 
726
 
727
static int atm_init_atmarp(struct atm_vcc *vcc)
728
{
729
        struct net_device *dev;
730
 
731
        if (atmarpd) return -EADDRINUSE;
732
        if (start_timer) {
733
                start_timer = 0;
734
                init_timer(&idle_timer);
735
                idle_timer.expires = jiffies+CLIP_CHECK_INTERVAL*HZ;
736
                idle_timer.function = idle_timer_check;
737
                add_timer(&idle_timer);
738
        }
739
        atmarpd = vcc;
740
        set_bit(ATM_VF_META,&vcc->flags);
741
        set_bit(ATM_VF_READY,&vcc->flags);
742
            /* allow replies and avoid getting closed if signaling dies */
743
        vcc->dev = &atmarpd_dev;
744
        vcc_insert_socket(vcc->sk);
745
        vcc->push = NULL;
746
        vcc->pop = NULL; /* crash */
747
        vcc->push_oam = NULL; /* crash */
748
        if (register_netdevice_notifier(&clip_dev_notifier))
749
                printk(KERN_ERR "register_netdevice_notifier failed\n");
750
        if (register_inetaddr_notifier(&clip_inet_notifier))
751
                printk(KERN_ERR "register_inetaddr_notifier failed\n");
752
        for (dev = clip_devs; dev; dev = PRIV(dev)->next)
753
                if (dev->flags & IFF_UP)
754
                        (void) to_atmarpd(act_up,PRIV(dev)->number,0);
755
        MOD_INC_USE_COUNT;
756
        return 0;
757
}
758
 
759
static struct atm_clip_ops __atm_clip_ops = {
760
        .clip_create =          clip_create,
761
        .clip_mkip =            clip_mkip,
762
        .clip_setentry =        clip_setentry,
763
        .clip_encap =           clip_encap,
764
        .clip_push =            clip_push,
765
        .atm_init_atmarp =      atm_init_atmarp,
766
        .owner =                THIS_MODULE
767
};
768
 
769
static int __init atm_clip_init(void)
770
{
771
        /* we should use neigh_table_init() */
772
        clip_tbl.lock = RW_LOCK_UNLOCKED;
773
        clip_tbl.kmem_cachep = kmem_cache_create(clip_tbl.id,
774
            clip_tbl.entry_size, 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
775
 
776
        if (!clip_tbl.kmem_cachep)
777
                return -ENOMEM;
778
 
779
        /* so neigh_ifdown() doesn't complain */
780
        clip_tbl.proxy_timer.data = 0;
781
        clip_tbl.proxy_timer.function = 0;
782
        init_timer(&clip_tbl.proxy_timer);
783
        skb_queue_head_init(&clip_tbl.proxy_queue);
784
 
785
        clip_tbl_hook = &clip_tbl;
786
        atm_clip_ops_set(&__atm_clip_ops);
787
 
788
        return 0;
789
}
790
 
791
static void __exit atm_clip_exit(void)
792
{
793
        struct net_device *dev, *next;
794
 
795
        atm_clip_ops_set(NULL);
796
 
797
        neigh_ifdown(&clip_tbl, NULL);
798
        dev = clip_devs;
799
        while (dev) {
800
                next = PRIV(dev)->next;
801
                unregister_netdev(dev);
802
                kfree(dev);
803
                dev = next;
804
        }
805
        if (start_timer == 0) del_timer(&idle_timer);
806
 
807
        kmem_cache_destroy(clip_tbl.kmem_cachep);
808
 
809
        clip_tbl_hook = NULL;
810
}
811
 
812
module_init(atm_clip_init);
813
module_exit(atm_clip_exit);
814
 
815
MODULE_LICENSE("GPL");

powered by: WebSVN 2.1.0

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