added files
[bcm963xx.git] / kernel / linux / net / atm / rt2684.c
1 /*
2  * net/atm/rt2684.c - RFC1577 Classical IP over ATM
3  *
4  *      shrinked version: only handles encapsulation
5  *      (no atmarp handling)
6  *
7  *      Song Wang (songw@broadcom.com)
8  */
9
10 #include <linux/module.h>
11 #include <linux/config.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/gfp.h>
15 #include <linux/list.h>
16 #include <asm/uaccess.h>
17 #include <linux/netdevice.h>
18 #include <linux/skbuff.h>
19 #include <linux/etherdevice.h>
20 #include <net/arp.h>
21 #include <linux/rtnetlink.h>
22 #include <linux/atmrt2684.h>
23 #include <linux/atmclip.h>
24 #include <linux/ip.h>
25
26 #include "ipcommon.h"
27
28 /*#define DEBUG*/
29 #ifdef DEBUG
30 #define DPRINTK(format, args...) printk(KERN_DEBUG "rt2684: " format, ##args)
31 #else
32 #define DPRINTK(format, args...)
33 #endif
34
35 #ifdef SKB_DEBUG
36 static void skb_debug(const struct sk_buff *skb)
37 {
38 #define NUM2PRINT 50
39         char buf[NUM2PRINT * 3 + 1];    /* 3 chars per byte */
40         int i = 0;
41         for (i = 0; i < skb->len && i < NUM2PRINT; i++) {
42                 sprintf(buf + i * 3, "%2.2x ", 0xff & skb->data[i]);
43         }
44         printk(KERN_DEBUG "rt2684: skb: %s\n", buf);
45 }
46 #else
47 #define skb_debug(skb)  do {} while (0)
48 #endif
49
50 struct rt2684_vcc {
51         struct atm_vcc  *atmvcc;
52         struct rt2684_dev *rtdev;
53
54         int             xoff;           /* 1 if send buffer is full */
55         unsigned long   last_use;       /* last send or receive operation */
56         unsigned long   idle_timeout;   /* keep open idle for so many jiffies*/
57         void (*old_push)(struct atm_vcc *vcc,struct sk_buff *skb);
58                                         /* keep old push fn for chaining */
59         void (*old_pop)(struct atm_vcc *vcc,struct sk_buff *skb);
60                                         /* keep old pop fn for chaining */
61         unsigned char   encap;          /* 0: NULL, 1: LLC/SNAP */
62         struct list_head rtvccs;
63
64
65 #ifdef CONFIG_ATM_rt2684_IPFILTER
66         struct rt2684_filter filter;
67 #endif /* CONFIG_ATM_rt2684_IPFILTER */
68 #ifndef FASTER_VERSION
69         unsigned copies_needed, copies_failed;
70 #endif /* FASTER_VERSION */
71 };
72
73 struct rt2684_dev {
74         struct net_device net_dev;
75         struct list_head rt2684_devs;
76         int number;
77         struct list_head rtvccs; /* one device <=> one vcc (before xmas) */
78         struct net_device_stats stats;
79         spinlock_t xoff_lock;   /* ensures that pop is atomic (SMP) */
80
81 };
82
83 /*
84  * This lock should be held for writing any time the list of devices or
85  * their attached vcc's could be altered.  It should be held for reading
86  * any time these are being queried.  Note that we sometimes need to
87  * do read-locking under interrupt context, so write locking must block
88  * the current CPU's interrupts
89  */
90 static rwlock_t devs_lock = RW_LOCK_UNLOCKED;
91
92 static LIST_HEAD(rt2684_devs);
93
94 static const unsigned char llc_oui[] = {
95         0xaa,   /* DSAP: non-ISO */
96         0xaa,   /* SSAP: non-ISO */
97         0x03,   /* Ctrl: Unnumbered Information Command PDU */
98         0x00,   /* OUI: EtherType */
99         0x00,
100         0x00 };
101
102 static inline struct rt2684_dev *RTPRIV(const struct net_device *net_dev)
103 {
104         return (struct rt2684_dev *) ((char *) (net_dev) -
105             (unsigned long) (&((struct rt2684_dev *) 0)->net_dev));
106 }
107
108 static inline struct rt2684_dev *list_entry_rtdev(const struct list_head *le)
109 {
110         return list_entry(le, struct rt2684_dev, rt2684_devs);
111 }
112
113 static inline struct rt2684_vcc *RT2684_VCC(const struct atm_vcc *atmvcc)
114 {
115         return (struct rt2684_vcc *) (atmvcc->user_back);
116 }
117
118 static inline struct rt2684_vcc *list_entry_rtvcc(const struct list_head *le)
119 {
120         return list_entry(le, struct rt2684_vcc, rtvccs);
121 }
122
123 /* Caller should hold read_lock(&devs_lock) */
124 static struct rt2684_dev *rt2684_find_dev(const struct rt2684_if_spec *s)
125 {
126         struct list_head *lh;
127         struct rt2684_dev *rtdev;
128         switch (s->method) {
129         case RT2684_FIND_BYNUM:
130                 list_for_each(lh, &rt2684_devs) {
131                         rtdev = list_entry_rtdev(lh);
132                         if (rtdev->number == s->spec.devnum)
133                                 return rtdev;
134                 }
135                 break;
136         case RT2684_FIND_BYIFNAME:
137                 list_for_each(lh, &rt2684_devs) {
138                         rtdev = list_entry_rtdev(lh);
139                         if (!strncmp(rtdev->net_dev.name, s->spec.ifname,
140                             sizeof rtdev->net_dev.name))
141                                 return rtdev;
142                 }
143                 break;
144         }
145         return NULL;
146 }
147
148 static inline struct rt2684_vcc *pick_outgoing_vcc(struct sk_buff *skb,
149         struct rt2684_dev *rtdev)
150 {
151         return list_empty(&rtdev->rtvccs) ? NULL :
152             list_entry_rtvcc(rtdev->rtvccs.next); /* 1 vcc/dev right now */
153 }
154
155 /*
156  * Send a packet out a particular vcc.
157  */
158 static int rt2684_start_xmit(struct sk_buff *skb, struct net_device *dev)
159 {
160         struct rt2684_dev *rtdev = RTPRIV(dev);
161         struct rt2684_vcc *rtvcc;
162         struct atm_vcc *atmvcc;
163         int old;
164         unsigned long flags;
165
166         DPRINTK("rt2684_start_xmit, skb->dst=%p\n", skb->dst);
167         read_lock(&devs_lock);
168         rtvcc = pick_outgoing_vcc(skb, rtdev);
169         if (rtvcc == NULL) {
170                 DPRINTK("no vcc attached to dev %s\n", dev->name);
171                 rtdev->stats.tx_errors++;
172                 rtdev->stats.tx_carrier_errors++;
173                 /* netif_stop_queue(dev); */
174                 dev_kfree_skb(skb);
175                 read_unlock(&devs_lock);
176                 return -EUNATCH;
177         }
178
179         if (rtvcc->encap) {
180                 void *here;
181
182                 here = skb_push(skb,RFC1483LLC_LEN);
183                 memcpy(here,llc_oui,sizeof(llc_oui));
184                 ((u16 *) here)[3] = skb->protocol;
185         }
186
187         skb_debug(skb);
188         ATM_SKB(skb)->vcc = atmvcc = rtvcc->atmvcc;
189         DPRINTK("atm_skb(%p)->vcc(%p)->dev(%p)\n", skb, atmvcc, atmvcc->dev);
190         if (!atm_may_send(atmvcc, skb->truesize)) {
191                 /* we free this here for now, because we cannot know in a higher
192                    layer whether the skb point it supplied wasn't freed yet.
193                    now, it always is.
194                 */
195                 dev_kfree_skb(skb);
196                 return 0;
197         }
198         atomic_add(skb->truesize,&atmvcc->sk->sk_wmem_alloc);
199         ATM_SKB(skb)->atm_options = atmvcc->atm_options;
200         rtvcc->last_use = jiffies;
201         DPRINTK("atm_skb(%p)->vcc(%p)->dev(%p)\n",skb,atmvcc,atmvcc->dev);
202         old = xchg(&rtvcc->xoff,1); /* assume XOFF ... */
203         if (old) {
204                 printk(KERN_WARNING "rt2684_start_xmit: XOFF->XOFF transition\n");
205                 return 0;
206         }
207         rtdev->stats.tx_packets++;
208         rtdev->stats.tx_bytes += skb->len;
209         if( atmvcc->send(atmvcc, skb) != 0 )
210             rtdev->stats.tx_dropped++;
211
212         if (atm_may_send(atmvcc,0)) {
213                 rtvcc->xoff = 0;
214                 return 0;
215         }
216         spin_lock_irqsave(&rtdev->xoff_lock,flags);
217         netif_stop_queue(dev); /* XOFF -> throttle immediately */
218         barrier();
219         if (!rtvcc->xoff)
220                 netif_start_queue(dev);
221                 /* Oh, we just raced with rt2684_pop. netif_start_queue should be
222                    good enough, because nothing should really be asleep because
223                    of the brief netif_stop_queue. If this isn't true or if it
224                    changes, use netif_wake_queue instead. */
225         spin_unlock_irqrestore(&rtdev->xoff_lock,flags);
226
227         read_unlock(&devs_lock);
228         return 0;
229 }
230
231 static struct net_device_stats *rt2684_get_stats(struct net_device *dev)
232 {
233         DPRINTK("rt2684_get_stats\n");
234         return &RTPRIV(dev)->stats;
235 }
236
237 #ifdef CONFIG_ATM_rt2684_IPFILTER
238 /* this IOCTL is experimental. */
239 static int rt2684_setfilt(struct atm_vcc *atmvcc, unsigned long arg)
240 {
241         struct rt2684_vcc *rtvcc;
242         struct rt2684_filter_set fs;
243
244         if (copy_from_user(&fs, (void *) arg, sizeof fs))
245                 return -EFAULT;
246         if (fs.ifspec.method != RT2684_FIND_BYNOTHING) {
247                 /*
248                  * This is really a per-vcc thing, but we can also search
249                  * by device
250                  */
251                 struct rt2684_dev *rtdev;
252                 read_lock(&devs_lock);
253                 rtdev = rt2684_find_dev(&fs.ifspec);
254                 if (rtdev == NULL || list_empty(&rtdev->rtvccs) ||
255                     rtdev->rtvccs.next != rtdev->rtvccs.prev)  /* >1 VCC */
256                         rtvcc = NULL;
257                 else
258                         rtvcc = list_entry_rtvcc(rtdev->rtvccs.next);
259                 read_unlock(&devs_lock);
260                 if (rtvcc == NULL)
261                         return -ESRCH;
262         } else
263                 rtvcc = rt2684_VCC(atmvcc);
264         memcpy(&rtvcc->filter, &fs.filter, sizeof(rtvcc->filter));
265         return 0;
266 }
267
268 /* Returns 1 if packet should be dropped */
269 static inline int
270 packet_fails_filter(u16 type, struct rt2684_vcc *rtvcc, struct sk_buff *skb)
271 {
272         if (rtvcc->filter.netmask == 0)
273                 return 0;                       /* no filter in place */
274         if (type == __constant_htons(ETH_P_IP) &&
275             (((struct iphdr *) (skb->data))->daddr & rtvcc->filter.
276              netmask) == rtvcc->filter.prefix)
277                 return 0;
278         if (type == __constant_htons(ETH_P_ARP))
279                 return 0;
280         /* TODO: we should probably filter ARPs too.. don't want to have
281          *   them returning values that don't make sense, or is that ok?
282          */
283         return 1;               /* drop */
284 }
285 #endif /* CONFIG_ATM_rt2684_IPFILTER */
286
287 static void rt2684_close_vcc(struct rt2684_vcc *rtvcc)
288 {
289         DPRINTK("removing VCC %p from dev %p\n", rtvcc, rtvcc->rtdev);
290         write_lock_irq(&devs_lock);
291         list_del(&rtvcc->rtvccs);
292         write_unlock_irq(&devs_lock);
293         rtvcc->atmvcc->user_back = NULL;        /* what about vcc->recvq ??? */
294         rtvcc->old_push(rtvcc->atmvcc, NULL);   /* pass on the bad news */
295         kfree(rtvcc);
296 }
297
298 static void rt2684_pop(struct atm_vcc *atmvcc,struct sk_buff *skb)
299 {
300         struct rt2684_vcc *rtvcc = RT2684_VCC(atmvcc);
301         struct rt2684_dev *rtdev = rtvcc->rtdev;
302         struct net_device *dev = skb->dev;
303         int old;
304         unsigned long flags;
305
306         DPRINTK("rt2684_pop(vcc %p)\n",atmvcc);
307         rtvcc->old_pop(atmvcc,skb);
308         /* skb->dev == NULL in outbound ARP packets */
309         if (!dev) return;
310         spin_lock_irqsave(&rtdev->xoff_lock,flags);
311         if (atm_may_send(atmvcc,0)) {
312                 old = xchg(&rtvcc->xoff,0);
313                 if (old) netif_wake_queue(dev);
314         }
315         spin_unlock_irqrestore(&rtdev->xoff_lock,flags);
316 }
317
318 /* when AAL5 PDU comes in: */
319 static void rt2684_push(struct atm_vcc *atmvcc, struct sk_buff *skb)
320 {
321         struct rt2684_vcc *rtvcc = RT2684_VCC(atmvcc);
322         struct rt2684_dev *rtdev = rtvcc->rtdev;
323
324         DPRINTK("rt2684_push\n");
325
326         if (skb == NULL) {      /* skb==NULL means VCC is being destroyed */
327                 rt2684_close_vcc(rtvcc);
328                 return;
329         }
330
331 #if defined(CONFIG_MIPS_BRCM)
332 //      skb->__unused=FROM_WAN;
333 #endif  
334         skb_debug(skb);
335         atm_return(atmvcc, skb->truesize);
336         DPRINTK("skb from rtdev %p\n", rtdev);
337
338         skb->dev = &rtdev->net_dev;
339         if (!skb->dev) {
340                 dev_kfree_skb_any(skb);
341                 return;
342         }
343         ATM_SKB(skb)->vcc = atmvcc;
344         skb->mac.raw = skb->data;
345         if (!rtvcc->encap || skb->len < RFC1483LLC_LEN || memcmp(skb->data,
346             llc_oui,sizeof(llc_oui))) skb->protocol = htons(ETH_P_IP);
347         else {
348                 skb->protocol = ((u16 *) skb->data)[3];
349                 skb_pull(skb,RFC1483LLC_LEN);
350         }
351         DPRINTK("received packet's protocol: %x\n", ntohs(skb->protocol));
352         skb_debug(skb);
353         rtvcc->last_use = jiffies;
354         if (!(rtdev->net_dev.flags & IFF_UP)) { /* sigh, interface is down */
355                 rtdev->stats.rx_dropped++;
356                 dev_kfree_skb(skb);
357                 return;
358         }
359         rtdev->stats.rx_packets++;
360         rtdev->stats.rx_bytes += skb->len;
361         memset(ATM_SKB(skb), 0, sizeof(struct atm_skb_data));
362         netif_rx(skb);
363 }
364
365 /* assign a vcc to a dev
366 Note: we do not have explicit unassign, but look at _push()
367 */
368 static int rt2684_regvcc(struct atm_vcc *atmvcc, unsigned long arg)
369 {
370
371         int err;
372         struct rt2684_vcc *rtvcc;
373         struct sk_buff_head copy;
374         struct sk_buff *skb;
375         struct rt2684_dev *rtdev;
376         struct atm_backend_rt2684 be;
377
378         if (copy_from_user(&be, (void *) arg, sizeof be)) {
379                 return -EFAULT;
380         }
381         write_lock_irq(&devs_lock);
382         /* Find the atmxxx device according to the interface name */
383         rtdev = rt2684_find_dev(&be.ifspec);
384         if (rtdev == NULL) {
385                 printk(KERN_ERR
386                     "rt2684: tried to attach to non-existant device\n");
387                 err = -ENXIO;
388                 goto error;
389         }
390         if (atmvcc->push == NULL) {
391                 err = -EBADFD;
392                 goto error;
393         }
394         if (!list_empty(&rtdev->rtvccs)) {      /* Only 1 VCC/dev right now */
395                 err = -EEXIST;
396                 goto error;
397         }
398         rtvcc = kmalloc(sizeof(struct rt2684_vcc), GFP_KERNEL);
399         if (!rtvcc) {
400                 err = -ENOMEM;
401                 goto error;
402         }
403         memset(rtvcc, 0, sizeof(struct rt2684_vcc));
404         DPRINTK("rt2684_regvcc vcc=%p, encaps=%d, rtvcc=%p\n", atmvcc, be.encaps,
405                 rtvcc);
406         list_add(&rtvcc->rtvccs, &rtdev->rtvccs);
407         write_unlock_irq(&devs_lock);
408         rtvcc->rtdev = rtdev;
409         rtvcc->atmvcc = atmvcc;
410         atmvcc->user_back = rtvcc;
411
412         rtvcc->xoff = 0;
413         //rtvcc->encap = 1;
414         rtvcc->encap = be.encaps;
415         rtvcc->last_use = jiffies;
416         //rtvcc->idle_timeout = timeout*HZ;
417         rtvcc->old_push = atmvcc->push;
418         rtvcc->old_pop = atmvcc->pop;
419         atmvcc->push = rt2684_push;
420         atmvcc->pop = rt2684_pop;
421
422         skb_queue_head_init(&copy);
423         skb_migrate(&atmvcc->sk->sk_receive_queue,&copy);
424         /* re-process everything received between connection setup and MKIP */
425         while ((skb = skb_dequeue(&copy))) {
426                 unsigned int len = skb->len;
427
428                 rt2684_push(atmvcc,skb);
429                 RTPRIV(skb->dev)->stats.rx_packets--;
430                 RTPRIV(skb->dev)->stats.rx_bytes -= len;
431         }
432         return 0;
433
434     error:
435         write_unlock_irq(&devs_lock);
436         return err;
437 }
438
439
440 /* Initialize net device atmxxx */
441 static int rt2684_initdev(struct net_device *dev)
442 {
443         DPRINTK("rt2684_initdev %s\n",dev->name);
444         dev->hard_start_xmit = rt2684_start_xmit;
445         /* sg_xmit ... */
446         dev->hard_header = NULL;
447         dev->rebuild_header = NULL;
448         dev->set_mac_address = NULL;
449         dev->hard_header_parse = NULL;
450         dev->hard_header_cache = NULL;
451         dev->header_cache_update = NULL;
452         dev->change_mtu = NULL;
453         dev->do_ioctl = NULL;
454         dev->get_stats = rt2684_get_stats;
455         dev->type = ARPHRD_PPP; /* We are not using atmarp, so set to PPP */
456         dev->hard_header_len = RFC1483LLC_LEN;
457         dev->mtu = RFC1626_MTU;
458         dev->addr_len = 0;
459         dev->tx_queue_len = 100; /* "normal" queue (packets) */
460             /* When using a "real" qdisc, the qdisc determines the queue */
461             /* length. tx_queue_len is only used for the default case, */
462             /* without any more elaborate queuing. 100 is a reasonable */
463             /* compromise between decent burst-tolerance and protection */
464             /* against memory hogs. */
465         /* Using the same set of flags as PPP */
466         dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
467         return 0;
468 }
469
470 static int rt2684_create(unsigned long arg)
471 {
472         int err;
473         struct rt2684_dev *rtdev;
474         struct atm_newif_rt2684 ni;
475
476         DPRINTK("rt2684_create\n");
477         /*
478          * We track module use by vcc's NOT the devices they're on.  We're
479          * protected here against module death by the kernel_lock, but if
480          * we need to sleep we should make sure that the module doesn't
481          * disappear under us.
482          */
483         if (copy_from_user(&ni, (void *) arg, sizeof ni)) {
484                 return -EFAULT;
485         }
486
487         /* Create atmxxx device */
488         if ((rtdev = kmalloc(sizeof(struct rt2684_dev), GFP_KERNEL)) == NULL) {
489                 return -ENOMEM;
490         }
491         memset(rtdev, 0, sizeof(struct rt2684_dev));
492         INIT_LIST_HEAD(&rtdev->rtvccs);
493
494         write_lock_irq(&devs_lock);
495         rtdev->number = list_empty(&rt2684_devs) ? 1 :
496             list_entry_rtdev(rt2684_devs.prev)->number + 1;
497         list_add_tail(&rtdev->rt2684_devs, &rt2684_devs);
498         write_unlock_irq(&devs_lock);
499
500         if (ni.ifname[0] != '\0') {
501                 memcpy(rtdev->net_dev.name, ni.ifname,
502                     sizeof(rtdev->net_dev.name));
503                 rtdev->net_dev.name[sizeof(rtdev->net_dev.name) - 1] = '\0';
504         } else
505                 sprintf(rtdev->net_dev.name, "atm%d", rtdev->number);
506
507         DPRINTK("registered netdev %s\n", rtdev->net_dev.name);
508
509         rtdev->net_dev.init = rt2684_initdev;
510         spin_lock_init(&rtdev->xoff_lock);
511
512         /* open, stop, do_ioctl ? */
513         err = register_netdev(&rtdev->net_dev);
514         if (err < 0) {
515                 printk(KERN_ERR "rt2684_create: register_netdev failed\n");
516                 write_lock_irq(&devs_lock);
517                 list_del(&rtdev->rt2684_devs);
518                 write_unlock_irq(&devs_lock);
519                 kfree(rtdev);
520                 return err;
521         }
522         return 0;
523 }
524
525 /*
526  * This handles ioctls actually performed on our vcc - we must return
527  * -ENOIOCTLCMD for any unrecognized ioctl
528  *
529  * Called from common.c: atm_backend_t b is used to differentiate
530  * different ioctl hooks.
531  */
532 static int rt2684_ioctl(struct socket *sock, unsigned int cmd,
533         unsigned long arg)
534 {
535         int err;
536         struct atm_vcc *atmvcc = ATM_SD(sock);
537
538         DPRINTK("rt2684_ioctl\n");
539         switch(cmd) {
540         case ATM_SETBACKEND:
541         case ATM_NEWBACKENDIF: {
542                 atm_backend_t b;
543                 err = get_user(b, (atm_backend_t *) arg);
544                 if (err)
545                         return -EFAULT;
546                 if (b != ATM_BACKEND_RT2684)
547                         return -ENOIOCTLCMD;
548                 if (!capable(CAP_NET_ADMIN))
549                         return -EPERM;
550                 if (cmd == ATM_SETBACKEND)
551                         return rt2684_regvcc(atmvcc, arg);
552                 else
553                         return rt2684_create(arg);
554                 }
555 #ifdef CONFIG_ATM_rt2684_IPFILTER
556         case rt2684_SETFILT:
557                 if (atmvcc->push != rt2684_push)
558                         return -ENOIOCTLCMD;
559                 if (!capable(CAP_NET_ADMIN))
560                         return -EPERM;
561                 err = rt2684_setfilt(atmvcc, arg);
562                 return err;
563 #endif /* CONFIG_ATM_rt2684_IPFILTER */
564         }
565         return -ENOIOCTLCMD;
566 }
567
568 static struct atm_ioctl rt2684_ioctl_ops = {
569         .owner  = THIS_MODULE,
570         .ioctl  = rt2684_ioctl,
571 };
572
573 /* Never put more than 256 bytes in at once */
574 static int rt2684_proc_engine(loff_t pos, char *buf)
575 {
576         struct list_head *lhd, *lhc;
577         struct rt2684_dev *rtdev;
578         struct rt2684_vcc *rtvcc;
579         list_for_each(lhd, &rt2684_devs) {
580                 rtdev = list_entry_rtdev(lhd);
581                 if (pos-- == 0)
582                         return sprintf(buf, "dev %.16s: num=%d\n",
583                             rtdev->net_dev.name,
584                             rtdev->number);
585                 list_for_each(lhc, &rtdev->rtvccs) {
586                         rtvcc = list_entry_rtvcc(lhc);
587                         if (pos-- == 0)
588                                 return sprintf(buf, "  vcc %d.%d.%d: encaps=%s"
589 #ifndef FASTER_VERSION
590                                     ", failed copies %u/%u"
591 #endif /* FASTER_VERSION */
592                                     "\n", rtvcc->atmvcc->dev->number,
593                                     rtvcc->atmvcc->vpi, rtvcc->atmvcc->vci,
594                                     (rtvcc->encap == 1) ? "LLC" : "NULL"
595 #ifndef FASTER_VERSION
596                                     , rtvcc->copies_failed
597                                     , rtvcc->copies_needed
598 #endif /* FASTER_VERSION */
599                                     );
600 #ifdef CONFIG_ATM_rt2684_IPFILTER
601 #define b1(var, byte)   ((u8 *) &rtvcc->filter.var)[byte]
602 #define bs(var)         b1(var, 0), b1(var, 1), b1(var, 2), b1(var, 3)
603                         if (rtvcc->filter.netmask != 0 && pos-- == 0)
604                                 return sprintf(buf, "    filter=%d.%d.%d.%d/"
605                                     "%d.%d.%d.%d\n", bs(prefix), bs(netmask));
606 #undef bs
607 #undef b1
608 #endif /* CONFIG_ATM_rt2684_IPFILTER */
609                 }
610         }
611         return 0;
612 }
613
614 static ssize_t rt2684_proc_read(struct file *file, char *buf, size_t count,
615         loff_t *pos)
616 {
617         unsigned long page;
618         int len = 0, x, left;
619         page = __get_free_page(GFP_KERNEL);
620         if (!page)
621                 return -ENOMEM;
622         left = PAGE_SIZE - 256;
623         if (count < left)
624                 left = count;
625         read_lock(&devs_lock);
626         for (;;) {
627                 x = rt2684_proc_engine(*pos, &((char *) page)[len]);
628                 if (x == 0)
629                         break;
630                 if (x > left)
631                         /*
632                          * This should only happen if the user passed in
633                          * a "count" too small for even one line
634                          */
635                         x = -EINVAL;
636                 if (x < 0) {
637                         len = x;
638                         break;
639                 }
640                 len += x;
641                 left -= x;
642                 (*pos)++;
643                 if (left < 256)
644                         break;
645         }
646         read_unlock(&devs_lock);
647         if (len > 0 && copy_to_user(buf, (char *) page, len))
648                 len = -EFAULT;
649         free_page(page);
650         return len;
651 }
652
653 static struct file_operations rt2684_proc_operations = {
654         .read   = rt2684_proc_read,
655 };
656
657 extern struct proc_dir_entry *atm_proc_root;    /* from proc.c */
658
659
660 static int __init rt2684_init(void)
661 {
662 #ifdef CONFIG_PROC_FS
663         struct proc_dir_entry *p;
664         if ((p = create_proc_entry("rt2684", 0, atm_proc_root)) == NULL)
665                 return -ENOMEM;
666         p->proc_fops = &rt2684_proc_operations;
667 #endif  
668         register_atm_ioctl(&rt2684_ioctl_ops);
669         return 0;
670 }
671
672 static void __exit rt2684_exit(void)
673 {
674         struct rt2684_dev *rtdev;
675         
676         deregister_atm_ioctl(&rt2684_ioctl_ops);
677 #ifdef CONFIG_PROC_FS   
678         remove_proc_entry("rt2684", atm_proc_root);
679 #endif  
680         while (!list_empty(&rt2684_devs)) {
681                 rtdev = list_entry_rtdev(rt2684_devs.next);
682                 unregister_netdev(&rtdev->net_dev);
683                 list_del(&rtdev->rt2684_devs);
684                 kfree(rtdev);
685         }
686 }
687
688 module_init(rt2684_init);
689 module_exit(rt2684_exit);
690
691 MODULE_AUTHOR("Song Wang");
692 MODULE_DESCRIPTION("RFC2684 routed protocols over ATM/AAL5 (for IPoA)");
693 MODULE_LICENSE("GPL");