2 * net/atm/rt2684.c - RFC1577 Classical IP over ATM
4 * shrinked version: only handles encapsulation
7 * Song Wang (songw@broadcom.com)
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>
21 #include <linux/rtnetlink.h>
22 #include <linux/atmrt2684.h>
23 #include <linux/atmclip.h>
30 #define DPRINTK(format, args...) printk(KERN_DEBUG "rt2684: " format, ##args)
32 #define DPRINTK(format, args...)
36 static void skb_debug(const struct sk_buff *skb)
39 char buf[NUM2PRINT * 3 + 1]; /* 3 chars per byte */
41 for (i = 0; i < skb->len && i < NUM2PRINT; i++) {
42 sprintf(buf + i * 3, "%2.2x ", 0xff & skb->data[i]);
44 printk(KERN_DEBUG "rt2684: skb: %s\n", buf);
47 #define skb_debug(skb) do {} while (0)
51 struct atm_vcc *atmvcc;
52 struct rt2684_dev *rtdev;
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;
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 */
74 struct net_device net_dev;
75 struct list_head rt2684_devs;
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) */
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
90 static rwlock_t devs_lock = RW_LOCK_UNLOCKED;
92 static LIST_HEAD(rt2684_devs);
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 */
102 static inline struct rt2684_dev *RTPRIV(const struct net_device *net_dev)
104 return (struct rt2684_dev *) ((char *) (net_dev) -
105 (unsigned long) (&((struct rt2684_dev *) 0)->net_dev));
108 static inline struct rt2684_dev *list_entry_rtdev(const struct list_head *le)
110 return list_entry(le, struct rt2684_dev, rt2684_devs);
113 static inline struct rt2684_vcc *RT2684_VCC(const struct atm_vcc *atmvcc)
115 return (struct rt2684_vcc *) (atmvcc->user_back);
118 static inline struct rt2684_vcc *list_entry_rtvcc(const struct list_head *le)
120 return list_entry(le, struct rt2684_vcc, rtvccs);
123 /* Caller should hold read_lock(&devs_lock) */
124 static struct rt2684_dev *rt2684_find_dev(const struct rt2684_if_spec *s)
126 struct list_head *lh;
127 struct rt2684_dev *rtdev;
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)
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))
148 static inline struct rt2684_vcc *pick_outgoing_vcc(struct sk_buff *skb,
149 struct rt2684_dev *rtdev)
151 return list_empty(&rtdev->rtvccs) ? NULL :
152 list_entry_rtvcc(rtdev->rtvccs.next); /* 1 vcc/dev right now */
156 * Send a packet out a particular vcc.
158 static int rt2684_start_xmit(struct sk_buff *skb, struct net_device *dev)
160 struct rt2684_dev *rtdev = RTPRIV(dev);
161 struct rt2684_vcc *rtvcc;
162 struct atm_vcc *atmvcc;
166 DPRINTK("rt2684_start_xmit, skb->dst=%p\n", skb->dst);
167 read_lock(&devs_lock);
168 rtvcc = pick_outgoing_vcc(skb, rtdev);
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); */
175 read_unlock(&devs_lock);
182 here = skb_push(skb,RFC1483LLC_LEN);
183 memcpy(here,llc_oui,sizeof(llc_oui));
184 ((u16 *) here)[3] = skb->protocol;
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.
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 ... */
204 printk(KERN_WARNING "rt2684_start_xmit: XOFF->XOFF transition\n");
207 rtdev->stats.tx_packets++;
208 rtdev->stats.tx_bytes += skb->len;
209 if( atmvcc->send(atmvcc, skb) != 0 )
210 rtdev->stats.tx_dropped++;
212 if (atm_may_send(atmvcc,0)) {
216 spin_lock_irqsave(&rtdev->xoff_lock,flags);
217 netif_stop_queue(dev); /* XOFF -> throttle immediately */
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);
227 read_unlock(&devs_lock);
231 static struct net_device_stats *rt2684_get_stats(struct net_device *dev)
233 DPRINTK("rt2684_get_stats\n");
234 return &RTPRIV(dev)->stats;
237 #ifdef CONFIG_ATM_rt2684_IPFILTER
238 /* this IOCTL is experimental. */
239 static int rt2684_setfilt(struct atm_vcc *atmvcc, unsigned long arg)
241 struct rt2684_vcc *rtvcc;
242 struct rt2684_filter_set fs;
244 if (copy_from_user(&fs, (void *) arg, sizeof fs))
246 if (fs.ifspec.method != RT2684_FIND_BYNOTHING) {
248 * This is really a per-vcc thing, but we can also search
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 */
258 rtvcc = list_entry_rtvcc(rtdev->rtvccs.next);
259 read_unlock(&devs_lock);
263 rtvcc = rt2684_VCC(atmvcc);
264 memcpy(&rtvcc->filter, &fs.filter, sizeof(rtvcc->filter));
268 /* Returns 1 if packet should be dropped */
270 packet_fails_filter(u16 type, struct rt2684_vcc *rtvcc, struct sk_buff *skb)
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)
278 if (type == __constant_htons(ETH_P_ARP))
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?
285 #endif /* CONFIG_ATM_rt2684_IPFILTER */
287 static void rt2684_close_vcc(struct rt2684_vcc *rtvcc)
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 */
298 static void rt2684_pop(struct atm_vcc *atmvcc,struct sk_buff *skb)
300 struct rt2684_vcc *rtvcc = RT2684_VCC(atmvcc);
301 struct rt2684_dev *rtdev = rtvcc->rtdev;
302 struct net_device *dev = skb->dev;
306 DPRINTK("rt2684_pop(vcc %p)\n",atmvcc);
307 rtvcc->old_pop(atmvcc,skb);
308 /* skb->dev == NULL in outbound ARP packets */
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);
315 spin_unlock_irqrestore(&rtdev->xoff_lock,flags);
318 /* when AAL5 PDU comes in: */
319 static void rt2684_push(struct atm_vcc *atmvcc, struct sk_buff *skb)
321 struct rt2684_vcc *rtvcc = RT2684_VCC(atmvcc);
322 struct rt2684_dev *rtdev = rtvcc->rtdev;
324 DPRINTK("rt2684_push\n");
326 if (skb == NULL) { /* skb==NULL means VCC is being destroyed */
327 rt2684_close_vcc(rtvcc);
331 #if defined(CONFIG_MIPS_BRCM)
332 // skb->__unused=FROM_WAN;
335 atm_return(atmvcc, skb->truesize);
336 DPRINTK("skb from rtdev %p\n", rtdev);
338 skb->dev = &rtdev->net_dev;
340 dev_kfree_skb_any(skb);
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);
348 skb->protocol = ((u16 *) skb->data)[3];
349 skb_pull(skb,RFC1483LLC_LEN);
351 DPRINTK("received packet's protocol: %x\n", ntohs(skb->protocol));
353 rtvcc->last_use = jiffies;
354 if (!(rtdev->net_dev.flags & IFF_UP)) { /* sigh, interface is down */
355 rtdev->stats.rx_dropped++;
359 rtdev->stats.rx_packets++;
360 rtdev->stats.rx_bytes += skb->len;
361 memset(ATM_SKB(skb), 0, sizeof(struct atm_skb_data));
365 /* assign a vcc to a dev
366 Note: we do not have explicit unassign, but look at _push()
368 static int rt2684_regvcc(struct atm_vcc *atmvcc, unsigned long arg)
372 struct rt2684_vcc *rtvcc;
373 struct sk_buff_head copy;
375 struct rt2684_dev *rtdev;
376 struct atm_backend_rt2684 be;
378 if (copy_from_user(&be, (void *) arg, sizeof be)) {
381 write_lock_irq(&devs_lock);
382 /* Find the atmxxx device according to the interface name */
383 rtdev = rt2684_find_dev(&be.ifspec);
386 "rt2684: tried to attach to non-existant device\n");
390 if (atmvcc->push == NULL) {
394 if (!list_empty(&rtdev->rtvccs)) { /* Only 1 VCC/dev right now */
398 rtvcc = kmalloc(sizeof(struct rt2684_vcc), GFP_KERNEL);
403 memset(rtvcc, 0, sizeof(struct rt2684_vcc));
404 DPRINTK("rt2684_regvcc vcc=%p, encaps=%d, rtvcc=%p\n", atmvcc, be.encaps,
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;
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;
422 skb_queue_head_init(©);
423 skb_migrate(&atmvcc->sk->sk_receive_queue,©);
424 /* re-process everything received between connection setup and MKIP */
425 while ((skb = skb_dequeue(©))) {
426 unsigned int len = skb->len;
428 rt2684_push(atmvcc,skb);
429 RTPRIV(skb->dev)->stats.rx_packets--;
430 RTPRIV(skb->dev)->stats.rx_bytes -= len;
435 write_unlock_irq(&devs_lock);
440 /* Initialize net device atmxxx */
441 static int rt2684_initdev(struct net_device *dev)
443 DPRINTK("rt2684_initdev %s\n",dev->name);
444 dev->hard_start_xmit = rt2684_start_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;
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;
470 static int rt2684_create(unsigned long arg)
473 struct rt2684_dev *rtdev;
474 struct atm_newif_rt2684 ni;
476 DPRINTK("rt2684_create\n");
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.
483 if (copy_from_user(&ni, (void *) arg, sizeof ni)) {
487 /* Create atmxxx device */
488 if ((rtdev = kmalloc(sizeof(struct rt2684_dev), GFP_KERNEL)) == NULL) {
491 memset(rtdev, 0, sizeof(struct rt2684_dev));
492 INIT_LIST_HEAD(&rtdev->rtvccs);
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);
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';
505 sprintf(rtdev->net_dev.name, "atm%d", rtdev->number);
507 DPRINTK("registered netdev %s\n", rtdev->net_dev.name);
509 rtdev->net_dev.init = rt2684_initdev;
510 spin_lock_init(&rtdev->xoff_lock);
512 /* open, stop, do_ioctl ? */
513 err = register_netdev(&rtdev->net_dev);
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);
526 * This handles ioctls actually performed on our vcc - we must return
527 * -ENOIOCTLCMD for any unrecognized ioctl
529 * Called from common.c: atm_backend_t b is used to differentiate
530 * different ioctl hooks.
532 static int rt2684_ioctl(struct socket *sock, unsigned int cmd,
536 struct atm_vcc *atmvcc = ATM_SD(sock);
538 DPRINTK("rt2684_ioctl\n");
541 case ATM_NEWBACKENDIF: {
543 err = get_user(b, (atm_backend_t *) arg);
546 if (b != ATM_BACKEND_RT2684)
548 if (!capable(CAP_NET_ADMIN))
550 if (cmd == ATM_SETBACKEND)
551 return rt2684_regvcc(atmvcc, arg);
553 return rt2684_create(arg);
555 #ifdef CONFIG_ATM_rt2684_IPFILTER
557 if (atmvcc->push != rt2684_push)
559 if (!capable(CAP_NET_ADMIN))
561 err = rt2684_setfilt(atmvcc, arg);
563 #endif /* CONFIG_ATM_rt2684_IPFILTER */
568 static struct atm_ioctl rt2684_ioctl_ops = {
569 .owner = THIS_MODULE,
570 .ioctl = rt2684_ioctl,
573 /* Never put more than 256 bytes in at once */
574 static int rt2684_proc_engine(loff_t pos, char *buf)
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);
582 return sprintf(buf, "dev %.16s: num=%d\n",
585 list_for_each(lhc, &rtdev->rtvccs) {
586 rtvcc = list_entry_rtvcc(lhc);
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 */
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));
608 #endif /* CONFIG_ATM_rt2684_IPFILTER */
614 static ssize_t rt2684_proc_read(struct file *file, char *buf, size_t count,
618 int len = 0, x, left;
619 page = __get_free_page(GFP_KERNEL);
622 left = PAGE_SIZE - 256;
625 read_lock(&devs_lock);
627 x = rt2684_proc_engine(*pos, &((char *) page)[len]);
632 * This should only happen if the user passed in
633 * a "count" too small for even one line
646 read_unlock(&devs_lock);
647 if (len > 0 && copy_to_user(buf, (char *) page, len))
653 static struct file_operations rt2684_proc_operations = {
654 .read = rt2684_proc_read,
657 extern struct proc_dir_entry *atm_proc_root; /* from proc.c */
660 static int __init rt2684_init(void)
662 #ifdef CONFIG_PROC_FS
663 struct proc_dir_entry *p;
664 if ((p = create_proc_entry("rt2684", 0, atm_proc_root)) == NULL)
666 p->proc_fops = &rt2684_proc_operations;
668 register_atm_ioctl(&rt2684_ioctl_ops);
672 static void __exit rt2684_exit(void)
674 struct rt2684_dev *rtdev;
676 deregister_atm_ioctl(&rt2684_ioctl_ops);
677 #ifdef CONFIG_PROC_FS
678 remove_proc_entry("rt2684", atm_proc_root);
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);
688 module_init(rt2684_init);
689 module_exit(rt2684_exit);
691 MODULE_AUTHOR("Song Wang");
692 MODULE_DESCRIPTION("RFC2684 routed protocols over ATM/AAL5 (for IPoA)");
693 MODULE_LICENSE("GPL");