1 /* net/atm/clip.c - RFC1577 Classical IP over ATM */
3 /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
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/netdevice.h>
11 #include <linux/skbuff.h>
12 #include <linux/wait.h>
13 #include <linux/timer.h>
14 #include <linux/if_arp.h> /* for some manifest constants */
15 #include <linux/notifier.h>
16 #include <linux/atm.h>
17 #include <linux/atmdev.h>
18 #include <linux/atmclip.h>
19 #include <linux/atmarp.h>
20 #include <linux/ip.h> /* for net/route.h */
21 #include <linux/in.h> /* for struct sockaddr_in */
22 #include <linux/if.h> /* for IFF_UP */
23 #include <linux/inetdevice.h>
24 #include <linux/bitops.h>
25 #include <net/route.h> /* for struct rtable and routing */
26 #include <net/icmp.h> /* icmp_send */
27 #include <asm/param.h> /* for HZ */
28 #include <asm/byteorder.h> /* for htons etc. */
29 #include <asm/system.h> /* save/restore_flags */
30 #include <asm/uaccess.h>
31 #include <asm/atomic.h>
34 #include "resources.h"
36 #include <net/atmclip.h>
40 #define DPRINTK(format,args...) printk(format,##args)
42 #define DPRINTK(format,args...)
46 struct net_device *clip_devs = NULL;
47 struct atm_vcc *atmarpd = NULL;
48 static struct timer_list idle_timer;
49 static int start_timer = 1;
52 static int to_atmarpd(enum atmarp_ctrl_type type,int itf,unsigned long ip)
54 struct atmarp_ctrl *ctrl;
57 DPRINTK("to_atmarpd(%d)\n",type);
58 if (!atmarpd) return -EUNATCH;
59 skb = alloc_skb(sizeof(struct atmarp_ctrl),GFP_ATOMIC);
60 if (!skb) return -ENOMEM;
61 ctrl = (struct atmarp_ctrl *) skb_put(skb,sizeof(struct atmarp_ctrl));
65 atm_force_charge(atmarpd,skb->truesize);
66 skb_queue_tail(&atmarpd->recvq,skb);
67 wake_up(&atmarpd->sleep);
72 static void link_vcc(struct clip_vcc *clip_vcc,struct atmarp_entry *entry)
74 DPRINTK("link_vcc %p to entry %p (neigh %p)\n",clip_vcc,entry,
76 clip_vcc->entry = entry;
77 clip_vcc->xoff = 0; /* @@@ may overrun buffer by one packet */
78 clip_vcc->next = entry->vccs;
79 entry->vccs = clip_vcc;
80 entry->neigh->used = jiffies;
84 static void unlink_clip_vcc(struct clip_vcc *clip_vcc)
86 struct atmarp_entry *entry = clip_vcc->entry;
87 struct clip_vcc **walk;
90 printk(KERN_CRIT "!clip_vcc->entry (clip_vcc %p)\n",clip_vcc);
93 entry->neigh->used = jiffies;
94 for (walk = &entry->vccs; *walk; walk = &(*walk)->next)
95 if (*walk == clip_vcc) {
98 *walk = clip_vcc->next; /* atomic */
99 clip_vcc->entry = NULL;
101 netif_wake_queue(entry->neigh->dev);
102 if (entry->vccs) return;
103 entry->expires = jiffies-1;
104 /* force resolution or expiration */
105 error = neigh_update(entry->neigh,NULL,NUD_NONE,0,0);
107 printk(KERN_CRIT "unlink_clip_vcc: "
108 "neigh_update failed with %d\n",error);
111 printk(KERN_CRIT "ATMARP: unlink_clip_vcc failed (entry %p, vcc "
112 "0x%p)\n",entry,clip_vcc);
116 static void idle_timer_check(unsigned long dummy)
120 /*DPRINTK("idle_timer_check\n");*/
121 write_lock(&clip_tbl.lock);
122 for (i = 0; i <= NEIGH_HASHMASK; i++) {
123 struct neighbour **np;
125 for (np = &clip_tbl.hash_buckets[i]; *np;) {
126 struct neighbour *n = *np;
127 struct atmarp_entry *entry = NEIGH2ENTRY(n);
128 struct clip_vcc *clip_vcc;
130 for (clip_vcc = entry->vccs; clip_vcc;
131 clip_vcc = clip_vcc->next)
132 if (clip_vcc->idle_timeout &&
133 time_after(jiffies, clip_vcc->last_use+
134 clip_vcc->idle_timeout)) {
135 DPRINTK("releasing vcc %p->%p of "
136 "entry %p\n",clip_vcc,clip_vcc->vcc,
138 atm_async_release_vcc(clip_vcc->vcc,
142 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));
151 while ((skb = skb_dequeue(&n->arp_queue)) !=
158 DPRINTK("expired neigh %p\n",n);
163 mod_timer(&idle_timer, jiffies+CLIP_CHECK_INTERVAL*HZ);
164 write_unlock(&clip_tbl.lock);
168 static int clip_arp_rcv(struct sk_buff *skb)
172 DPRINTK("clip_arp_rcv\n");
173 vcc = ATM_SKB(skb)->vcc;
174 if (!vcc || !atm_charge(vcc,skb->truesize)) {
175 dev_kfree_skb_any(skb);
178 DPRINTK("pushing to %p\n",vcc);
179 DPRINTK("using %p\n",CLIP_VCC(vcc)->old_push);
180 CLIP_VCC(vcc)->old_push(vcc,skb);
185 void clip_push(struct atm_vcc *vcc,struct sk_buff *skb)
187 struct clip_vcc *clip_vcc = CLIP_VCC(vcc);
189 DPRINTK("clip push\n");
191 DPRINTK("removing VCC %p\n",clip_vcc);
192 if (clip_vcc->entry) unlink_clip_vcc(clip_vcc);
193 clip_vcc->old_push(vcc,NULL); /* pass on the bad news */
197 atm_return(vcc,skb->truesize);
198 skb->dev = clip_vcc->entry ? clip_vcc->entry->neigh->dev : clip_devs;
199 /* clip_vcc->entry == NULL if we don't have an IP address yet */
201 dev_kfree_skb_any(skb);
204 ATM_SKB(skb)->vcc = vcc;
205 skb->mac.raw = skb->data;
206 if (!clip_vcc->encap || skb->len < RFC1483LLC_LEN || memcmp(skb->data,
207 llc_oui,sizeof(llc_oui))) skb->protocol = htons(ETH_P_IP);
209 skb->protocol = ((u16 *) skb->data)[3];
210 skb_pull(skb,RFC1483LLC_LEN);
211 if (skb->protocol == htons(ETH_P_ARP)) {
212 PRIV(skb->dev)->stats.rx_packets++;
213 PRIV(skb->dev)->stats.rx_bytes += skb->len;
218 clip_vcc->last_use = jiffies;
219 PRIV(skb->dev)->stats.rx_packets++;
220 PRIV(skb->dev)->stats.rx_bytes += skb->len;
226 * Note: these spinlocks _must_not_ block on non-SMP. The only goal is that
227 * clip_pop is atomic with respect to the critical section in clip_start_xmit.
231 static void clip_pop(struct atm_vcc *vcc,struct sk_buff *skb)
233 struct clip_vcc *clip_vcc = CLIP_VCC(vcc);
234 struct net_device *dev = skb->dev;
238 DPRINTK("clip_pop(vcc %p)\n",vcc);
239 clip_vcc->old_pop(vcc,skb);
240 /* skb->dev == NULL in outbound ARP packets */
242 spin_lock_irqsave(&PRIV(dev)->xoff_lock,flags);
243 if (atm_may_send(vcc,0)) {
244 old = xchg(&clip_vcc->xoff,0);
245 if (old) netif_wake_queue(dev);
247 spin_unlock_irqrestore(&PRIV(dev)->xoff_lock,flags);
251 static void clip_neigh_destroy(struct neighbour *neigh)
253 DPRINTK("clip_neigh_destroy (neigh %p)\n",neigh);
254 if (NEIGH2ENTRY(neigh)->vccs)
255 printk(KERN_CRIT "clip_neigh_destroy: vccs != NULL !!!\n");
256 NEIGH2ENTRY(neigh)->vccs = (void *) 0xdeadbeef;
260 static void clip_neigh_solicit(struct neighbour *neigh,struct sk_buff *skb)
262 DPRINTK("clip_neigh_solicit (neigh %p, skb %p)\n",neigh,skb);
263 to_atmarpd(act_need,PRIV(neigh->dev)->number,NEIGH2ENTRY(neigh)->ip);
267 static void clip_neigh_error(struct neighbour *neigh,struct sk_buff *skb)
269 #ifndef CONFIG_ATM_CLIP_NO_ICMP
270 icmp_send(skb,ICMP_DEST_UNREACH,ICMP_HOST_UNREACH,0);
276 static struct neigh_ops clip_neigh_ops = {
278 destructor: clip_neigh_destroy,
279 solicit: clip_neigh_solicit,
280 error_report: clip_neigh_error,
281 output: dev_queue_xmit,
282 connected_output: dev_queue_xmit,
283 hh_output: dev_queue_xmit,
284 queue_xmit: dev_queue_xmit,
288 static int clip_constructor(struct neighbour *neigh)
290 struct atmarp_entry *entry = NEIGH2ENTRY(neigh);
291 struct net_device *dev = neigh->dev;
292 struct in_device *in_dev = dev->ip_ptr;
294 DPRINTK("clip_constructor (neigh %p, entry %p)\n",neigh,entry);
295 if (!in_dev) return -EINVAL;
296 neigh->type = inet_addr_type(entry->ip);
297 if (neigh->type != RTN_UNICAST) return -EINVAL;
298 if (in_dev->arp_parms) neigh->parms = in_dev->arp_parms;
299 neigh->ops = &clip_neigh_ops;
300 neigh->output = neigh->nud_state & NUD_VALID ?
301 neigh->ops->connected_output : neigh->ops->output;
302 entry->neigh = neigh;
304 entry->expires = jiffies-1;
308 static u32 clip_hash(const void *pkey, const struct net_device *dev)
312 hash_val = *(u32*)pkey;
313 hash_val ^= (hash_val>>16);
314 hash_val ^= hash_val>>8;
315 hash_val ^= hash_val>>3;
316 hash_val = (hash_val^dev->ifindex)&NEIGH_HASHMASK;
322 struct neigh_table clip_tbl = {
324 AF_INET, /* family */
325 sizeof(struct neighbour)+sizeof(struct atmarp_entry), /* entry_size */
328 clip_constructor, /* constructor */
329 NULL, /* pconstructor */
330 NULL, /* pdestructor */
331 NULL, /* proxy_redo */
335 NULL, /* neigh_setup */
339 NULL, /* sysctl_table */
340 30*HZ, /* base_reachable_time */
341 1*HZ, /* retrans_time */
342 60*HZ, /* gc_staletime */
343 30*HZ, /* reachable_time */
344 5*HZ, /* delay_probe_time */
346 3, /* ucast_probes */
348 3, /* mcast_probes */
349 1*HZ, /* anycast_delay */
350 (8*HZ)/10, /* proxy_delay */
351 1*HZ, /* proxy_qlen */
354 30*HZ,128,512,1024 /* copied from ARP ... */
358 /* @@@ copy bh locking from arp.c -- need to bh-enable atm code before */
361 * We play with the resolve flag: 0 and 1 have the usual meaning, but -1 means
362 * to allocate the neighbour entry but not to ask atmarpd for resolution. Also,
363 * don't increment the usage count. This is used to create entries in
368 int clip_encap(struct atm_vcc *vcc,int mode)
370 CLIP_VCC(vcc)->encap = mode;
375 static int clip_start_xmit(struct sk_buff *skb,struct net_device *dev)
377 struct clip_priv *clip_priv = PRIV(dev);
378 struct atmarp_entry *entry;
383 DPRINTK("clip_start_xmit (skb %p)\n",skb);
385 printk(KERN_ERR "clip_start_xmit: skb->dst == NULL\n");
387 clip_priv->stats.tx_dropped++;
390 if (!skb->dst->neighbour) {
392 skb->dst->neighbour = clip_find_neighbour(skb->dst,1);
393 if (!skb->dst->neighbour) {
394 dev_kfree_skb(skb); /* lost that one */
395 clip_priv->stats.tx_dropped++;
399 printk(KERN_ERR "clip_start_xmit: NO NEIGHBOUR !\n");
401 clip_priv->stats.tx_dropped++;
404 entry = NEIGH2ENTRY(skb->dst->neighbour);
406 if (time_after(jiffies, entry->expires)) {
407 /* should be resolved */
408 entry->expires = jiffies+ATMARP_RETRY_DELAY*HZ;
409 to_atmarpd(act_need,PRIV(dev)->number,entry->ip);
411 if (entry->neigh->arp_queue.qlen < ATMARP_MAX_UNRES_PACKETS)
412 skb_queue_tail(&entry->neigh->arp_queue,skb);
415 clip_priv->stats.tx_dropped++;
419 DPRINTK("neigh %p, vccs %p\n",entry,entry->vccs);
420 ATM_SKB(skb)->vcc = vcc = entry->vccs->vcc;
421 DPRINTK("using neighbour %p, vcc %p\n",skb->dst->neighbour,vcc);
422 if (entry->vccs->encap) {
425 here = skb_push(skb,RFC1483LLC_LEN);
426 memcpy(here,llc_oui,sizeof(llc_oui));
427 ((u16 *) here)[3] = skb->protocol;
429 atomic_add(skb->truesize,&vcc->tx_inuse);
430 ATM_SKB(skb)->iovcnt = 0;
431 ATM_SKB(skb)->atm_options = vcc->atm_options;
432 entry->vccs->last_use = jiffies;
433 DPRINTK("atm_skb(%p)->vcc(%p)->dev(%p)\n",skb,vcc,vcc->dev);
434 old = xchg(&entry->vccs->xoff,1); /* assume XOFF ... */
436 printk(KERN_WARNING "clip_start_xmit: XOFF->XOFF transition\n");
439 clip_priv->stats.tx_packets++;
440 clip_priv->stats.tx_bytes += skb->len;
441 (void) vcc->send(vcc,skb);
442 if (atm_may_send(vcc,0)) {
443 entry->vccs->xoff = 0;
446 spin_lock_irqsave(&clip_priv->xoff_lock,flags);
447 netif_stop_queue(dev); /* XOFF -> throttle immediately */
449 if (!entry->vccs->xoff)
450 netif_start_queue(dev);
451 /* Oh, we just raced with clip_pop. netif_start_queue should be
452 good enough, because nothing should really be asleep because
453 of the brief netif_stop_queue. If this isn't true or if it
454 changes, use netif_wake_queue instead. */
455 spin_unlock_irqrestore(&clip_priv->xoff_lock,flags);
460 static struct net_device_stats *clip_get_stats(struct net_device *dev)
462 return &PRIV(dev)->stats;
466 int clip_mkip(struct atm_vcc *vcc,int timeout)
468 struct clip_vcc *clip_vcc;
469 struct sk_buff_head copy;
472 if (!vcc->push) return -EBADFD;
473 clip_vcc = kmalloc(sizeof(struct clip_vcc),GFP_KERNEL);
474 if (!clip_vcc) return -ENOMEM;
475 DPRINTK("mkip clip_vcc %p vcc %p\n",clip_vcc,vcc);
477 vcc->user_back = clip_vcc;
478 clip_vcc->entry = NULL;
481 clip_vcc->last_use = jiffies;
482 clip_vcc->idle_timeout = timeout*HZ;
483 clip_vcc->old_push = vcc->push;
484 clip_vcc->old_pop = vcc->pop;
485 vcc->push = clip_push;
487 skb_queue_head_init(©);
488 skb_migrate(&vcc->recvq,©);
489 /* re-process everything received between connection setup and MKIP */
490 while ((skb = skb_dequeue(©)))
492 atm_return(vcc,skb->truesize);
496 unsigned int len = skb->len;
499 PRIV(skb->dev)->stats.rx_packets--;
500 PRIV(skb->dev)->stats.rx_bytes -= len;
506 int clip_setentry(struct atm_vcc *vcc,u32 ip)
508 struct neighbour *neigh;
509 struct atmarp_entry *entry;
511 struct clip_vcc *clip_vcc;
514 if (vcc->push != clip_push) {
515 printk(KERN_WARNING "clip_setentry: non-CLIP VCC\n");
518 clip_vcc = CLIP_VCC(vcc);
520 if (!clip_vcc->entry) {
521 printk(KERN_ERR "hiding hidden ATMARP entry\n");
524 DPRINTK("setentry: remove\n");
525 unlink_clip_vcc(clip_vcc);
528 error = ip_route_output(&rt,ip,0,1,0);
529 if (error) return error;
530 neigh = __neigh_lookup(&clip_tbl,&ip,rt->u.dst.dev,1);
534 entry = NEIGH2ENTRY(neigh);
535 if (entry != clip_vcc->entry) {
536 if (!clip_vcc->entry) DPRINTK("setentry: add\n");
538 DPRINTK("setentry: update\n");
539 unlink_clip_vcc(clip_vcc);
541 link_vcc(clip_vcc,entry);
543 error = neigh_update(neigh,llc_oui,NUD_PERMANENT,1,0);
544 neigh_release(neigh);
549 static int clip_init(struct net_device *dev)
551 DPRINTK("clip_init %s\n",dev->name);
552 dev->hard_start_xmit = clip_start_xmit;
554 dev->hard_header = NULL;
555 dev->rebuild_header = NULL;
556 dev->set_mac_address = NULL;
557 dev->hard_header_parse = NULL;
558 dev->hard_header_cache = NULL;
559 dev->header_cache_update = NULL;
560 dev->change_mtu = NULL;
561 dev->do_ioctl = NULL;
562 dev->get_stats = clip_get_stats;
563 dev->type = ARPHRD_ATM;
564 dev->hard_header_len = RFC1483LLC_LEN;
565 dev->mtu = RFC1626_MTU;
567 dev->tx_queue_len = 100; /* "normal" queue (packets) */
568 /* When using a "real" qdisc, the qdisc determines the queue */
569 /* length. tx_queue_len is only used for the default case, */
570 /* without any more elaborate queuing. 100 is a reasonable */
571 /* compromise between decent burst-tolerance and protection */
572 /* against memory hogs. */
578 int clip_create(int number)
580 struct net_device *dev;
581 struct clip_priv *clip_priv;
585 for (dev = clip_devs; dev; dev = PRIV(dev)->next)
586 if (PRIV(dev)->number == number) return -EEXIST;
590 for (dev = clip_devs; dev; dev = PRIV(dev)->next)
591 if (PRIV(dev)->number >= number)
592 number = PRIV(dev)->number+1;
594 dev = kmalloc(sizeof(struct net_device)+sizeof(struct clip_priv),
596 if (!dev) return -ENOMEM;
597 memset(dev,0,sizeof(struct net_device)+sizeof(struct clip_priv));
598 clip_priv = PRIV(dev);
599 sprintf(dev->name,"atm%d",number);
600 dev->init = clip_init;
601 spin_lock_init(&clip_priv->xoff_lock);
602 clip_priv->number = number;
603 error = register_netdev(dev);
608 clip_priv->next = clip_devs;
610 DPRINTK("registered (net:%s)\n",dev->name);
615 static int clip_device_event(struct notifier_block *this,unsigned long event,
618 /* ignore non-CLIP devices */
619 if (((struct net_device *) dev)->type != ARPHRD_ATM ||
620 ((struct net_device *) dev)->init != clip_init)
624 DPRINTK("clip_device_event NETDEV_UP\n");
625 (void) to_atmarpd(act_up,PRIV(dev)->number,0);
627 case NETDEV_GOING_DOWN:
628 DPRINTK("clip_device_event NETDEV_DOWN\n");
629 (void) to_atmarpd(act_down,PRIV(dev)->number,0);
632 case NETDEV_CHANGEMTU:
633 DPRINTK("clip_device_event NETDEV_CHANGE*\n");
634 (void) to_atmarpd(act_change,PRIV(dev)->number,0);
637 case NETDEV_REGISTER:
639 DPRINTK("clip_device_event %ld\n",event);
643 printk(KERN_WARNING "clip_device_event: unknown event "
651 static int clip_inet_event(struct notifier_block *this,unsigned long event,
654 struct in_device *in_dev;
656 in_dev = ((struct in_ifaddr *) ifa)->ifa_dev;
657 if (!in_dev || !in_dev->dev) {
658 printk(KERN_WARNING "clip_inet_event: no device\n");
662 * Transitions are of the down-change-up type, so it's sufficient to
663 * handle the change on up.
665 if (event != NETDEV_UP) return NOTIFY_DONE;
666 return clip_device_event(this,NETDEV_CHANGE,in_dev->dev);
670 static struct notifier_block clip_dev_notifier = {
678 static struct notifier_block clip_inet_notifier = {
686 static void atmarpd_close(struct atm_vcc *vcc)
688 DPRINTK("atmarpd_close\n");
689 atmarpd = NULL; /* assumed to be atomic */
691 unregister_inetaddr_notifier(&clip_inet_notifier);
692 unregister_netdevice_notifier(&clip_dev_notifier);
693 if (skb_peek(&vcc->recvq))
694 printk(KERN_ERR "atmarpd_close: closing with requests "
696 skb_queue_purge(&vcc->recvq);
701 static struct atmdev_ops atmarpd_dev_ops = {
702 close: atmarpd_close,
706 static struct atm_dev atmarpd_dev = {
710 999, /* dummy device number */
711 NULL,NULL, /* pretend not to have any VCCs */
712 NULL,NULL, /* no data */
713 { 0 }, /* no flags */
714 NULL, /* no local address */
715 { 0 } /* no ESI, no statistics */
719 int atm_init_atmarp(struct atm_vcc *vcc)
721 struct net_device *dev;
723 if (atmarpd) return -EADDRINUSE;
726 init_timer(&idle_timer);
727 idle_timer.expires = jiffies+CLIP_CHECK_INTERVAL*HZ;
728 idle_timer.function = idle_timer_check;
729 add_timer(&idle_timer);
732 set_bit(ATM_VF_META,&vcc->flags);
733 set_bit(ATM_VF_READY,&vcc->flags);
734 /* allow replies and avoid getting closed if signaling dies */
735 bind_vcc(vcc,&atmarpd_dev);
737 vcc->pop = NULL; /* crash */
738 vcc->push_oam = NULL; /* crash */
739 if (register_netdevice_notifier(&clip_dev_notifier))
740 printk(KERN_ERR "register_netdevice_notifier failed\n");
741 if (register_inetaddr_notifier(&clip_inet_notifier))
742 printk(KERN_ERR "register_inetaddr_notifier failed\n");
743 for (dev = clip_devs; dev; dev = PRIV(dev)->next)
744 if (dev->flags & IFF_UP)
745 (void) to_atmarpd(act_up,PRIV(dev)->number,0);
750 void atm_clip_init(void)
752 clip_tbl.lock = RW_LOCK_UNLOCKED;
753 clip_tbl.kmem_cachep = kmem_cache_create(clip_tbl.id,
754 clip_tbl.entry_size, 0, SLAB_HWCACHE_ALIGN, NULL, NULL);