1 /* net/atm/clip.c - RFC1577 Classical IP over ATM */
3 /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA
6 * Harald Welte <laforge@gnumonks.org>:
7 * - backport DaveM's generalized neighbour cache from 2.6.9-rcX */
10 #include <linux/config.h>
11 #include <linux/string.h>
12 #include <linux/errno.h>
13 #include <linux/kernel.h> /* for UINT_MAX */
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/netdevice.h>
17 #include <linux/skbuff.h>
18 #include <linux/wait.h>
19 #include <linux/timer.h>
20 #include <linux/if_arp.h> /* for some manifest constants */
21 #include <linux/notifier.h>
22 #include <linux/atm.h>
23 #include <linux/atmdev.h>
24 #include <linux/atmclip.h>
25 #include <linux/atmarp.h>
26 #include <linux/ip.h> /* for net/route.h */
27 #include <linux/in.h> /* for struct sockaddr_in */
28 #include <linux/if.h> /* for IFF_UP */
29 #include <linux/inetdevice.h>
30 #include <linux/bitops.h>
31 #include <linux/jhash.h>
32 #include <net/route.h> /* for struct rtable and routing */
33 #include <net/icmp.h> /* icmp_send */
34 #include <asm/param.h> /* for HZ */
35 #include <asm/byteorder.h> /* for htons etc. */
36 #include <asm/system.h> /* save/restore_flags */
37 #include <asm/uaccess.h>
38 #include <asm/atomic.h>
41 #include "resources.h"
43 #include <net/atmclip.h>
47 #define DPRINTK(format,args...) printk(format,##args)
49 #define DPRINTK(format,args...)
53 struct net_device *clip_devs = NULL;
54 struct atm_vcc *atmarpd = NULL;
55 static struct neigh_table clip_tbl;
56 static struct timer_list idle_timer;
57 static int start_timer = 1;
60 static int to_atmarpd(enum atmarp_ctrl_type type,int itf,unsigned long ip)
62 struct atmarp_ctrl *ctrl;
65 DPRINTK("to_atmarpd(%d)\n",type);
66 if (!atmarpd) return -EUNATCH;
67 skb = alloc_skb(sizeof(struct atmarp_ctrl),GFP_ATOMIC);
68 if (!skb) return -ENOMEM;
69 ctrl = (struct atmarp_ctrl *) skb_put(skb,sizeof(struct atmarp_ctrl));
73 atm_force_charge(atmarpd,skb->truesize);
74 skb_queue_tail(&atmarpd->sk->receive_queue,skb);
75 wake_up(&atmarpd->sleep);
80 static void link_vcc(struct clip_vcc *clip_vcc,struct atmarp_entry *entry)
82 DPRINTK("link_vcc %p to entry %p (neigh %p)\n",clip_vcc,entry,
84 clip_vcc->entry = entry;
85 clip_vcc->xoff = 0; /* @@@ may overrun buffer by one packet */
86 clip_vcc->next = entry->vccs;
87 entry->vccs = clip_vcc;
88 entry->neigh->used = jiffies;
92 static void unlink_clip_vcc(struct clip_vcc *clip_vcc)
94 struct atmarp_entry *entry = clip_vcc->entry;
95 struct clip_vcc **walk;
98 printk(KERN_CRIT "!clip_vcc->entry (clip_vcc %p)\n",clip_vcc);
101 spin_lock_bh(&entry->neigh->dev->xmit_lock); /* block clip_start_xmit() */
102 entry->neigh->used = jiffies;
103 for (walk = &entry->vccs; *walk; walk = &(*walk)->next)
104 if (*walk == clip_vcc) {
107 *walk = clip_vcc->next; /* atomic */
108 clip_vcc->entry = NULL;
110 netif_wake_queue(entry->neigh->dev);
113 entry->expires = jiffies-1;
114 /* force resolution or expiration */
115 error = neigh_update(entry->neigh,NULL,NUD_NONE,0,0);
117 printk(KERN_CRIT "unlink_clip_vcc: "
118 "neigh_update failed with %d\n",error);
121 printk(KERN_CRIT "ATMARP: unlink_clip_vcc failed (entry %p, vcc "
122 "0x%p)\n",entry,clip_vcc);
124 spin_unlock_bh(&entry->neigh->dev->xmit_lock);
127 /* The neighbour entry n->lock is held. */
128 static int neigh_check_cb(struct neighbour *n)
130 struct atmarp_entry *entry = NEIGH2ENTRY(n);
133 for (cv = entry->vccs; cv; cv = cv->next) {
134 unsigned long exp = cv->last_use + cv->idle_timeout;
136 if (cv->idle_timeout && time_after(jiffies, exp)) {
137 DPRINTK("releasing vcc %p->%p of entry %p\n",
139 vcc_release_async(cv->vcc, -ETIMEDOUT);
143 if (entry->vccs || time_before(jiffies, entry->expires))
146 if (atomic_read(&n->refcnt) > 1) {
149 DPRINTK("destruction postponed with ref %d\n",
150 atomic_read(&n->refcnt));
152 while ((skb = skb_dequeue(&n->arp_queue)) != NULL)
158 DPRINTK("expired neigh %p\n",n);
162 static void idle_timer_check(unsigned long dummy)
164 write_lock(&clip_tbl.lock);
165 __neigh_for_each_release(&clip_tbl, neigh_check_cb);
166 mod_timer(&idle_timer, jiffies+CLIP_CHECK_INTERVAL*HZ);
167 write_unlock(&clip_tbl.lock);
170 static int clip_arp_rcv(struct sk_buff *skb)
174 DPRINTK("clip_arp_rcv\n");
175 vcc = ATM_SKB(skb)->vcc;
176 if (!vcc || !atm_charge(vcc,skb->truesize)) {
177 dev_kfree_skb_any(skb);
180 DPRINTK("pushing to %p\n",vcc);
181 DPRINTK("using %p\n",CLIP_VCC(vcc)->old_push);
182 CLIP_VCC(vcc)->old_push(vcc,skb);
187 static void clip_push(struct atm_vcc *vcc,struct sk_buff *skb)
189 struct clip_vcc *clip_vcc = CLIP_VCC(vcc);
191 DPRINTK("clip push\n");
193 DPRINTK("removing VCC %p\n",clip_vcc);
194 if (clip_vcc->entry) unlink_clip_vcc(clip_vcc);
195 clip_vcc->old_push(vcc,NULL); /* pass on the bad news */
199 atm_return(vcc,skb->truesize);
200 skb->dev = clip_vcc->entry ? clip_vcc->entry->neigh->dev : clip_devs;
201 /* clip_vcc->entry == NULL if we don't have an IP address yet */
203 dev_kfree_skb_any(skb);
206 ATM_SKB(skb)->vcc = vcc;
207 skb->mac.raw = skb->data;
208 if (!clip_vcc->encap || skb->len < RFC1483LLC_LEN || memcmp(skb->data,
209 llc_oui,sizeof(llc_oui))) skb->protocol = htons(ETH_P_IP);
211 skb->protocol = ((u16 *) skb->data)[3];
212 skb_pull(skb,RFC1483LLC_LEN);
213 if (skb->protocol == htons(ETH_P_ARP)) {
214 PRIV(skb->dev)->stats.rx_packets++;
215 PRIV(skb->dev)->stats.rx_bytes += skb->len;
220 clip_vcc->last_use = jiffies;
221 PRIV(skb->dev)->stats.rx_packets++;
222 PRIV(skb->dev)->stats.rx_bytes += skb->len;
223 memset(ATM_SKB(skb), 0, sizeof(struct atm_skb_data));
229 * Note: these spinlocks _must_not_ block on non-SMP. The only goal is that
230 * clip_pop is atomic with respect to the critical section in clip_start_xmit.
234 static void clip_pop(struct atm_vcc *vcc,struct sk_buff *skb)
236 struct clip_vcc *clip_vcc = CLIP_VCC(vcc);
237 struct net_device *dev = skb->dev;
241 DPRINTK("clip_pop(vcc %p)\n",vcc);
242 clip_vcc->old_pop(vcc,skb);
243 /* skb->dev == NULL in outbound ARP packets */
245 spin_lock_irqsave(&PRIV(dev)->xoff_lock,flags);
246 if (atm_may_send(vcc,0)) {
247 old = xchg(&clip_vcc->xoff,0);
248 if (old) netif_wake_queue(dev);
250 spin_unlock_irqrestore(&PRIV(dev)->xoff_lock,flags);
254 static void clip_neigh_destroy(struct neighbour *neigh)
256 DPRINTK("clip_neigh_destroy (neigh %p)\n",neigh);
257 if (NEIGH2ENTRY(neigh)->vccs)
258 printk(KERN_CRIT "clip_neigh_destroy: vccs != NULL !!!\n");
259 NEIGH2ENTRY(neigh)->vccs = (void *) 0xdeadbeef;
263 static void clip_neigh_solicit(struct neighbour *neigh,struct sk_buff *skb)
265 DPRINTK("clip_neigh_solicit (neigh %p, skb %p)\n",neigh,skb);
266 to_atmarpd(act_need,PRIV(neigh->dev)->number,NEIGH2ENTRY(neigh)->ip);
270 static void clip_neigh_error(struct neighbour *neigh,struct sk_buff *skb)
272 #ifndef CONFIG_ATM_CLIP_NO_ICMP
273 icmp_send(skb,ICMP_DEST_UNREACH,ICMP_HOST_UNREACH,0);
279 static struct neigh_ops clip_neigh_ops = {
281 destructor: clip_neigh_destroy,
282 solicit: clip_neigh_solicit,
283 error_report: clip_neigh_error,
284 output: dev_queue_xmit,
285 connected_output: dev_queue_xmit,
286 hh_output: dev_queue_xmit,
287 queue_xmit: dev_queue_xmit,
291 static int clip_constructor(struct neighbour *neigh)
293 struct atmarp_entry *entry = NEIGH2ENTRY(neigh);
294 struct net_device *dev = neigh->dev;
295 struct in_device *in_dev = dev->ip_ptr;
297 DPRINTK("clip_constructor (neigh %p, entry %p)\n",neigh,entry);
298 if (!in_dev) return -EINVAL;
299 neigh->type = inet_addr_type(entry->ip);
300 if (neigh->type != RTN_UNICAST) return -EINVAL;
301 if (in_dev->arp_parms) neigh->parms = in_dev->arp_parms;
302 neigh->ops = &clip_neigh_ops;
303 neigh->output = neigh->nud_state & NUD_VALID ?
304 neigh->ops->connected_output : neigh->ops->output;
305 entry->neigh = neigh;
307 entry->expires = jiffies-1;
311 static u32 clip_hash(const void *pkey, const struct net_device *dev)
313 return jhash_2words(*(u32 *)pkey, dev->ifindex, clip_tbl.hash_rnd);
317 static struct neigh_table clip_tbl = {
319 AF_INET, /* family */
320 sizeof(struct neighbour)+sizeof(struct atmarp_entry), /* entry_size */
323 clip_constructor, /* constructor */
324 NULL, /* pconstructor */
325 NULL, /* pdestructor */
326 NULL, /* proxy_redo */
330 NULL, /* neigh_setup */
334 NULL, /* sysctl_table */
335 30*HZ, /* base_reachable_time */
336 1*HZ, /* retrans_time */
337 60*HZ, /* gc_staletime */
338 30*HZ, /* reachable_time */
339 5*HZ, /* delay_probe_time */
341 3, /* ucast_probes */
343 3, /* mcast_probes */
344 1*HZ, /* anycast_delay */
345 (8*HZ)/10, /* proxy_delay */
346 1*HZ, /* proxy_qlen */
349 30*HZ,128,512,1024 /* copied from ARP ... */
353 /* @@@ copy bh locking from arp.c -- need to bh-enable atm code before */
356 * We play with the resolve flag: 0 and 1 have the usual meaning, but -1 means
357 * to allocate the neighbour entry but not to ask atmarpd for resolution. Also,
358 * don't increment the usage count. This is used to create entries in
363 static int clip_encap(struct atm_vcc *vcc,int mode)
365 CLIP_VCC(vcc)->encap = mode;
370 static int clip_start_xmit(struct sk_buff *skb,struct net_device *dev)
372 struct clip_priv *clip_priv = PRIV(dev);
373 struct atmarp_entry *entry;
378 DPRINTK("clip_start_xmit (skb %p)\n",skb);
380 printk(KERN_ERR "clip_start_xmit: skb->dst == NULL\n");
382 clip_priv->stats.tx_dropped++;
385 if (!skb->dst->neighbour) {
387 skb->dst->neighbour = clip_find_neighbour(skb->dst,1);
388 if (!skb->dst->neighbour) {
389 dev_kfree_skb(skb); /* lost that one */
390 clip_priv->stats.tx_dropped++;
394 printk(KERN_ERR "clip_start_xmit: NO NEIGHBOUR !\n");
396 clip_priv->stats.tx_dropped++;
399 entry = NEIGH2ENTRY(skb->dst->neighbour);
401 if (time_after(jiffies, entry->expires)) {
402 /* should be resolved */
403 entry->expires = jiffies+ATMARP_RETRY_DELAY*HZ;
404 to_atmarpd(act_need,PRIV(dev)->number,entry->ip);
406 if (entry->neigh->arp_queue.qlen < ATMARP_MAX_UNRES_PACKETS)
407 skb_queue_tail(&entry->neigh->arp_queue,skb);
410 clip_priv->stats.tx_dropped++;
414 DPRINTK("neigh %p, vccs %p\n",entry,entry->vccs);
415 ATM_SKB(skb)->vcc = vcc = entry->vccs->vcc;
416 DPRINTK("using neighbour %p, vcc %p\n",skb->dst->neighbour,vcc);
417 if (entry->vccs->encap) {
420 here = skb_push(skb,RFC1483LLC_LEN);
421 memcpy(here,llc_oui,sizeof(llc_oui));
422 ((u16 *) here)[3] = skb->protocol;
424 atomic_add(skb->truesize,&vcc->sk->wmem_alloc);
425 ATM_SKB(skb)->atm_options = vcc->atm_options;
426 entry->vccs->last_use = jiffies;
427 DPRINTK("atm_skb(%p)->vcc(%p)->dev(%p)\n",skb,vcc,vcc->dev);
428 old = xchg(&entry->vccs->xoff,1); /* assume XOFF ... */
430 printk(KERN_WARNING "clip_start_xmit: XOFF->XOFF transition\n");
433 clip_priv->stats.tx_packets++;
434 clip_priv->stats.tx_bytes += skb->len;
435 (void) vcc->send(vcc,skb);
436 if (atm_may_send(vcc,0)) {
437 entry->vccs->xoff = 0;
440 spin_lock_irqsave(&clip_priv->xoff_lock,flags);
441 netif_stop_queue(dev); /* XOFF -> throttle immediately */
443 if (!entry->vccs->xoff)
444 netif_start_queue(dev);
445 /* Oh, we just raced with clip_pop. netif_start_queue should be
446 good enough, because nothing should really be asleep because
447 of the brief netif_stop_queue. If this isn't true or if it
448 changes, use netif_wake_queue instead. */
449 spin_unlock_irqrestore(&clip_priv->xoff_lock,flags);
454 static struct net_device_stats *clip_get_stats(struct net_device *dev)
456 return &PRIV(dev)->stats;
460 static int clip_mkip(struct atm_vcc *vcc,int timeout)
462 struct clip_vcc *clip_vcc;
463 struct sk_buff_head copy;
466 if (!vcc->push) return -EBADFD;
467 clip_vcc = kmalloc(sizeof(struct clip_vcc),GFP_KERNEL);
468 if (!clip_vcc) return -ENOMEM;
469 DPRINTK("mkip clip_vcc %p vcc %p\n",clip_vcc,vcc);
471 vcc->user_back = clip_vcc;
472 clip_vcc->entry = NULL;
475 clip_vcc->last_use = jiffies;
476 clip_vcc->idle_timeout = timeout*HZ;
477 clip_vcc->old_push = vcc->push;
478 clip_vcc->old_pop = vcc->pop;
479 vcc->push = clip_push;
481 skb_queue_head_init(©);
482 skb_migrate(&vcc->sk->receive_queue,©);
483 /* re-process everything received between connection setup and MKIP */
484 while ((skb = skb_dequeue(©)))
486 atm_return(vcc,skb->truesize);
490 unsigned int len = skb->len;
494 PRIV(skb->dev)->stats.rx_packets--;
495 PRIV(skb->dev)->stats.rx_bytes -= len;
502 static int clip_setentry(struct atm_vcc *vcc,u32 ip)
504 struct neighbour *neigh;
505 struct atmarp_entry *entry;
507 struct clip_vcc *clip_vcc;
510 if (vcc->push != clip_push) {
511 printk(KERN_WARNING "clip_setentry: non-CLIP VCC\n");
514 clip_vcc = CLIP_VCC(vcc);
516 if (!clip_vcc->entry) {
517 printk(KERN_ERR "hiding hidden ATMARP entry\n");
520 DPRINTK("setentry: remove\n");
521 unlink_clip_vcc(clip_vcc);
524 error = ip_route_output(&rt,ip,0,1,0);
525 if (error) return error;
526 neigh = __neigh_lookup(&clip_tbl,&ip,rt->u.dst.dev,1);
530 entry = NEIGH2ENTRY(neigh);
531 if (entry != clip_vcc->entry) {
532 if (!clip_vcc->entry) DPRINTK("setentry: add\n");
534 DPRINTK("setentry: update\n");
535 unlink_clip_vcc(clip_vcc);
537 link_vcc(clip_vcc,entry);
539 error = neigh_update(neigh,llc_oui,NUD_PERMANENT,1,0);
540 neigh_release(neigh);
545 static int clip_init(struct net_device *dev)
547 DPRINTK("clip_init %s\n",dev->name);
548 dev->hard_start_xmit = clip_start_xmit;
550 dev->hard_header = NULL;
551 dev->rebuild_header = NULL;
552 dev->set_mac_address = NULL;
553 dev->hard_header_parse = NULL;
554 dev->hard_header_cache = NULL;
555 dev->header_cache_update = NULL;
556 dev->change_mtu = NULL;
557 dev->do_ioctl = NULL;
558 dev->get_stats = clip_get_stats;
559 dev->type = ARPHRD_ATM;
560 dev->hard_header_len = RFC1483LLC_LEN;
561 dev->mtu = RFC1626_MTU;
563 dev->tx_queue_len = 100; /* "normal" queue (packets) */
564 /* When using a "real" qdisc, the qdisc determines the queue */
565 /* length. tx_queue_len is only used for the default case, */
566 /* without any more elaborate queuing. 100 is a reasonable */
567 /* compromise between decent burst-tolerance and protection */
568 /* against memory hogs. */
574 static int clip_create(int number)
576 struct net_device *dev;
577 struct clip_priv *clip_priv;
581 for (dev = clip_devs; dev; dev = PRIV(dev)->next)
582 if (PRIV(dev)->number == number) return -EEXIST;
586 for (dev = clip_devs; dev; dev = PRIV(dev)->next)
587 if (PRIV(dev)->number >= number)
588 number = PRIV(dev)->number+1;
590 dev = kmalloc(sizeof(struct net_device)+sizeof(struct clip_priv),
592 if (!dev) return -ENOMEM;
593 memset(dev,0,sizeof(struct net_device)+sizeof(struct clip_priv));
594 clip_priv = PRIV(dev);
595 sprintf(dev->name,"atm%d",number);
596 dev->init = clip_init;
597 spin_lock_init(&clip_priv->xoff_lock);
598 clip_priv->number = number;
599 error = register_netdev(dev);
604 clip_priv->next = clip_devs;
606 DPRINTK("registered (net:%s)\n",dev->name);
611 static int clip_device_event(struct notifier_block *this,unsigned long event,
614 /* ignore non-CLIP devices */
615 if (((struct net_device *) dev)->type != ARPHRD_ATM ||
616 ((struct net_device *) dev)->init != clip_init)
620 DPRINTK("clip_device_event NETDEV_UP\n");
621 (void) to_atmarpd(act_up,PRIV(dev)->number,0);
623 case NETDEV_GOING_DOWN:
624 DPRINTK("clip_device_event NETDEV_DOWN\n");
625 (void) to_atmarpd(act_down,PRIV(dev)->number,0);
628 case NETDEV_CHANGEMTU:
629 DPRINTK("clip_device_event NETDEV_CHANGE*\n");
630 (void) to_atmarpd(act_change,PRIV(dev)->number,0);
633 case NETDEV_REGISTER:
635 DPRINTK("clip_device_event %ld\n",event);
639 printk(KERN_WARNING "clip_device_event: unknown event "
647 static int clip_inet_event(struct notifier_block *this,unsigned long event,
650 struct in_device *in_dev;
652 in_dev = ((struct in_ifaddr *) ifa)->ifa_dev;
653 if (!in_dev || !in_dev->dev) {
654 printk(KERN_WARNING "clip_inet_event: no device\n");
658 * Transitions are of the down-change-up type, so it's sufficient to
659 * handle the change on up.
661 if (event != NETDEV_UP) return NOTIFY_DONE;
662 return clip_device_event(this,NETDEV_CHANGE,in_dev->dev);
666 static struct notifier_block clip_dev_notifier = {
674 static struct notifier_block clip_inet_notifier = {
682 static void atmarpd_close(struct atm_vcc *vcc)
684 DPRINTK("atmarpd_close\n");
685 atmarpd = NULL; /* assumed to be atomic */
687 unregister_inetaddr_notifier(&clip_inet_notifier);
688 unregister_netdevice_notifier(&clip_dev_notifier);
689 if (skb_peek(&vcc->sk->receive_queue))
690 printk(KERN_ERR "atmarpd_close: closing with requests "
692 skb_queue_purge(&vcc->sk->receive_queue);
698 static struct atmdev_ops atmarpd_dev_ops = {
699 .close = atmarpd_close,
703 static struct atm_dev atmarpd_dev = {
704 .ops = &atmarpd_dev_ops,
707 .lock = SPIN_LOCK_UNLOCKED
711 static int atm_init_atmarp(struct atm_vcc *vcc)
713 struct net_device *dev;
715 if (atmarpd) return -EADDRINUSE;
718 init_timer(&idle_timer);
719 idle_timer.expires = jiffies+CLIP_CHECK_INTERVAL*HZ;
720 idle_timer.function = idle_timer_check;
721 add_timer(&idle_timer);
724 set_bit(ATM_VF_META,&vcc->flags);
725 set_bit(ATM_VF_READY,&vcc->flags);
726 /* allow replies and avoid getting closed if signaling dies */
727 vcc->dev = &atmarpd_dev;
728 vcc_insert_socket(vcc->sk);
730 vcc->pop = NULL; /* crash */
731 vcc->push_oam = NULL; /* crash */
732 if (register_netdevice_notifier(&clip_dev_notifier))
733 printk(KERN_ERR "register_netdevice_notifier failed\n");
734 if (register_inetaddr_notifier(&clip_inet_notifier))
735 printk(KERN_ERR "register_inetaddr_notifier failed\n");
736 for (dev = clip_devs; dev; dev = PRIV(dev)->next)
737 if (dev->flags & IFF_UP)
738 (void) to_atmarpd(act_up,PRIV(dev)->number,0);
743 static struct atm_clip_ops __atm_clip_ops = {
744 .clip_create = clip_create,
745 .clip_mkip = clip_mkip,
746 .clip_setentry = clip_setentry,
747 .clip_encap = clip_encap,
748 .clip_push = clip_push,
749 .atm_init_atmarp = atm_init_atmarp,
753 static int __init atm_clip_init(void)
755 neigh_table_init(&clip_tbl);
757 clip_tbl_hook = &clip_tbl;
758 atm_clip_ops_set(&__atm_clip_ops);
763 static void __exit atm_clip_exit(void)
765 struct net_device *dev, *next;
767 atm_clip_ops_set(NULL);
769 /* First, stop the idle timer, so it stops banging
772 if (start_timer == 0)
773 del_timer(&idle_timer);
775 /* Next, purge the table, so that the device
776 * unregister loop below does not hang due to
777 * device references remaining in the table.
779 neigh_ifdown(&clip_tbl, NULL);
783 next = PRIV(dev)->next;
784 unregister_netdev(dev);
789 /* Now it is safe to fully shutdown whole table. */
790 neigh_table_clear(&clip_tbl);
792 clip_tbl_hook = NULL;
795 module_init(atm_clip_init);
796 module_exit(atm_clip_exit);
798 MODULE_LICENSE("GPL");