1 /* net/atm/clip.c - RFC1577 Classical IP over ATM */
3 /* Written 1995-1999 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 <net/route.h> /* for struct rtable and routing */
25 #include <net/icmp.h> /* icmp_send */
26 #include <asm/param.h> /* for HZ */
27 #include <asm/byteorder.h> /* for htons etc. */
28 #include <asm/system.h> /* save/restore_flags */
29 #include <asm/uaccess.h>
30 #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
->next
= entry
->vccs
;
78 entry
->vccs
= clip_vcc
;
79 entry
->neigh
->used
= jiffies
;
83 static void unlink_clip_vcc(struct clip_vcc
*clip_vcc
)
85 struct atmarp_entry
*entry
= clip_vcc
->entry
;
86 struct clip_vcc
**walk
;
89 printk(KERN_CRIT
"!clip_vcc->entry (clip_vcc %p)\n",clip_vcc
);
92 entry
->neigh
->used
= jiffies
;
93 for (walk
= &entry
->vccs
; *walk
; walk
= &(*walk
)->next
)
94 if (*walk
== clip_vcc
) {
97 *walk
= clip_vcc
->next
; /* atomic */
98 clip_vcc
->entry
= NULL
;
99 if (entry
->vccs
) return;
100 entry
->expires
= jiffies
-1;
101 /* force resolution or expiration */
102 error
= neigh_update(entry
->neigh
,NULL
,NUD_NONE
,0,0);
104 printk(KERN_CRIT
"unlink_clip_vcc: "
105 "neigh_update failed with %d\n",error
);
108 printk(KERN_CRIT
"ATMARP: unlink_clip_vcc failed (entry %p, vcc "
109 "0x%p)\n",entry
,clip_vcc
);
113 static void idle_timer_check(unsigned long dummy
)
117 /*DPRINTK("idle_timer_check\n");*/
118 write_lock(&clip_tbl
.lock
);
119 for (i
= 0; i
<= NEIGH_HASHMASK
; i
++) {
120 struct neighbour
**np
;
122 for (np
= &clip_tbl
.hash_buckets
[i
]; *np
;) {
123 struct neighbour
*n
= *np
;
124 struct atmarp_entry
*entry
= NEIGH2ENTRY(n
);
125 struct clip_vcc
*clip_vcc
;
127 for (clip_vcc
= entry
->vccs
; clip_vcc
;
128 clip_vcc
= clip_vcc
->next
)
129 if (clip_vcc
->idle_timeout
&&
130 time_after(jiffies
, clip_vcc
->last_use
+
131 clip_vcc
->idle_timeout
)) {
132 DPRINTK("releasing vcc %p->%p of "
133 "entry %p\n",clip_vcc
,clip_vcc
->vcc
,
135 atm_async_release_vcc(clip_vcc
->vcc
,
139 time_before(jiffies
, entry
->expires
)) {
143 if (atomic_read(&n
->refcnt
) > 1) {
146 DPRINTK("destruction postponed with ref %d\n",
147 atomic_read(&n
->refcnt
));
148 while ((skb
= skb_dequeue(&n
->arp_queue
)) !=
155 DPRINTK("expired neigh %p\n",n
);
160 mod_timer(&idle_timer
, jiffies
+CLIP_CHECK_INTERVAL
*HZ
);
161 write_unlock(&clip_tbl
.lock
);
165 static int clip_arp_rcv(struct sk_buff
*skb
)
169 DPRINTK("clip_arp_rcv\n");
170 vcc
= ATM_SKB(skb
)->vcc
;
171 if (!vcc
|| !atm_charge(vcc
,skb
->truesize
)) {
175 DPRINTK("pushing to %p\n",vcc
);
176 DPRINTK("using %p\n",CLIP_VCC(vcc
)->old_push
);
177 CLIP_VCC(vcc
)->old_push(vcc
,skb
);
182 void clip_push(struct atm_vcc
*vcc
,struct sk_buff
*skb
)
184 struct clip_vcc
*clip_vcc
= CLIP_VCC(vcc
);
186 DPRINTK("clip push\n");
188 DPRINTK("removing VCC %p\n",clip_vcc
);
189 if (clip_vcc
->entry
) unlink_clip_vcc(clip_vcc
);
190 clip_vcc
->old_push(vcc
,NULL
); /* pass on the bad news */
194 atm_return(vcc
,skb
->truesize
);
195 skb
->dev
= clip_vcc
->entry
? clip_vcc
->entry
->neigh
->dev
: clip_devs
;
196 /* clip_vcc->entry == NULL if we don't have an IP address yet */
201 ATM_SKB(skb
)->vcc
= vcc
;
202 skb
->mac
.raw
= skb
->data
;
203 if (!clip_vcc
->encap
|| skb
->len
< RFC1483LLC_LEN
|| memcmp(skb
->data
,
204 llc_oui
,sizeof(llc_oui
))) skb
->protocol
= htons(ETH_P_IP
);
206 skb
->protocol
= ((u16
*) skb
->data
)[3];
207 skb_pull(skb
,RFC1483LLC_LEN
);
208 if (skb
->protocol
== htons(ETH_P_ARP
)) {
209 PRIV(skb
->dev
)->stats
.rx_packets
++;
210 PRIV(skb
->dev
)->stats
.rx_bytes
+= skb
->len
;
215 clip_vcc
->last_use
= jiffies
;
216 PRIV(skb
->dev
)->stats
.rx_packets
++;
217 PRIV(skb
->dev
)->stats
.rx_bytes
+= skb
->len
;
222 static void clip_neigh_destroy(struct neighbour
*neigh
)
224 DPRINTK("clip_neigh_destroy (neigh %p)\n",neigh
);
225 if (NEIGH2ENTRY(neigh
)->vccs
)
226 printk(KERN_CRIT
"clip_neigh_destroy: vccs != NULL !!!\n");
227 NEIGH2ENTRY(neigh
)->vccs
= (void *) 0xdeadbeef;
231 static void clip_neigh_solicit(struct neighbour
*neigh
,struct sk_buff
*skb
)
233 DPRINTK("clip_neigh_solicit (neigh %p, skb %p)\n",neigh
,skb
);
234 to_atmarpd(act_need
,PRIV(neigh
->dev
)->number
,NEIGH2ENTRY(neigh
)->ip
);
238 static void clip_neigh_error(struct neighbour
*neigh
,struct sk_buff
*skb
)
240 #ifndef CONFIG_ATM_CLIP_NO_ICMP
241 icmp_send(skb
,ICMP_DEST_UNREACH
,ICMP_HOST_UNREACH
,0);
247 static struct neigh_ops clip_neigh_ops
= {
248 AF_INET
, /* family */
249 clip_neigh_destroy
, /* destructor */
250 clip_neigh_solicit
, /* solicit */
251 clip_neigh_error
, /* error_report */
252 dev_queue_xmit
, /* output */
253 dev_queue_xmit
, /* connected_output */
254 dev_queue_xmit
, /* hh_output */
255 dev_queue_xmit
/* queue_xmit */
259 static int clip_constructor(struct neighbour
*neigh
)
261 struct atmarp_entry
*entry
= NEIGH2ENTRY(neigh
);
262 struct net_device
*dev
= neigh
->dev
;
263 struct in_device
*in_dev
= dev
->ip_ptr
;
265 DPRINTK("clip_constructor (neigh %p, entry %p)\n",neigh
,entry
);
266 if (!in_dev
) return -EINVAL
;
267 neigh
->type
= inet_addr_type(entry
->ip
);
268 if (neigh
->type
!= RTN_UNICAST
) return -EINVAL
;
269 if (in_dev
->arp_parms
) neigh
->parms
= in_dev
->arp_parms
;
270 neigh
->ops
= &clip_neigh_ops
;
271 neigh
->output
= neigh
->nud_state
& NUD_VALID
?
272 neigh
->ops
->connected_output
: neigh
->ops
->output
;
273 entry
->neigh
= neigh
;
275 entry
->expires
= jiffies
-1;
279 static u32
clip_hash(const void *pkey
, const struct net_device
*dev
)
283 hash_val
= *(u32
*)pkey
;
284 hash_val
^= (hash_val
>>16);
285 hash_val
^= hash_val
>>8;
286 hash_val
^= hash_val
>>3;
287 hash_val
= (hash_val
^dev
->ifindex
)&NEIGH_HASHMASK
;
293 struct neigh_table clip_tbl
= {
295 AF_INET
, /* family */
296 sizeof(struct neighbour
)+sizeof(struct atmarp_entry
), /* entry_size */
299 clip_constructor
, /* constructor */
300 NULL
, /* pconstructor */
301 NULL
, /* pdestructor */
302 NULL
, /* proxy_redo */
306 NULL
, /* neigh_setup */
310 NULL
, /* sysctl_table */
311 30*HZ
, /* base_reachable_time */
312 1*HZ
, /* retrans_time */
313 60*HZ
, /* gc_staletime */
314 30*HZ
, /* reachable_time */
315 5*HZ
, /* delay_probe_time */
317 3, /* ucast_probes */
319 3, /* mcast_probes */
320 1*HZ
, /* anycast_delay */
321 (8*HZ
)/10, /* proxy_delay */
322 1*HZ
, /* proxy_qlen */
325 30*HZ
,128,512,1024 /* copied from ARP ... */
329 /* @@@ copy bh locking from arp.c -- need to bh-enable atm code before */
332 * We play with the resolve flag: 0 and 1 have the usual meaning, but -1 means
333 * to allocate the neighbour entry but not to ask atmarpd for resolution. Also,
334 * don't increment the usage count. This is used to create entries in
339 int clip_encap(struct atm_vcc
*vcc
,int mode
)
341 CLIP_VCC(vcc
)->encap
= mode
;
346 static int clip_start_xmit(struct sk_buff
*skb
,struct net_device
*dev
)
348 struct atmarp_entry
*entry
;
350 DPRINTK("clip_start_xmit (skb %p)\n",skb
);
352 printk(KERN_ERR
"clip_start_xmit: skb->dst == NULL\n");
356 if (!skb
->dst
->neighbour
) {
358 skb
->dst
->neighbour
= clip_find_neighbour(skb
->dst
,1);
359 if (!skb
->dst
->neighbour
) {
360 dev_kfree_skb(skb
); /* lost that one */
361 PRIV(dev
)->stats
.tx_dropped
++;
365 printk("clip_start_xmit: NO NEIGHBOUR !\n");
368 entry
= NEIGH2ENTRY(skb
->dst
->neighbour
);
370 if (time_after(jiffies
, entry
->expires
)) {
371 /* should be resolved */
372 entry
->expires
= jiffies
+ATMARP_RETRY_DELAY
*HZ
;
373 to_atmarpd(act_need
,PRIV(dev
)->number
,entry
->ip
);
375 if (entry
->neigh
->arp_queue
.qlen
< ATMARP_MAX_UNRES_PACKETS
)
376 skb_queue_tail(&entry
->neigh
->arp_queue
,skb
);
379 PRIV(dev
)->stats
.tx_dropped
++;
383 DPRINTK("neigh %p, vccs %p\n",entry
,entry
->vccs
);
384 ATM_SKB(skb
)->vcc
= entry
->vccs
->vcc
;
385 DPRINTK("using neighbour %p, vcc %p\n",skb
->dst
->neighbour
,
387 if (entry
->vccs
->encap
) {
390 here
= skb_push(skb
,RFC1483LLC_LEN
);
391 memcpy(here
,llc_oui
,sizeof(llc_oui
));
392 ((u16
*) here
)[3] = skb
->protocol
;
394 atomic_add(skb
->truesize
,&ATM_SKB(skb
)->vcc
->tx_inuse
);
395 ATM_SKB(skb
)->iovcnt
= 0;
396 ATM_SKB(skb
)->atm_options
= ATM_SKB(skb
)->vcc
->atm_options
;
397 entry
->vccs
->last_use
= jiffies
;
398 DPRINTK("atm_skb(%p)->vcc(%p)->dev(%p)\n",skb
,ATM_SKB(skb
)->vcc
,
399 ATM_SKB(skb
)->vcc
->dev
);
400 PRIV(dev
)->stats
.tx_packets
++;
401 PRIV(dev
)->stats
.tx_bytes
+= skb
->len
;
402 (void) ATM_SKB(skb
)->vcc
->dev
->ops
->send(ATM_SKB(skb
)->vcc
,skb
);
407 static struct net_device_stats
*clip_get_stats(struct net_device
*dev
)
409 return &PRIV(dev
)->stats
;
413 int clip_mkip(struct atm_vcc
*vcc
,int timeout
)
415 struct clip_vcc
*clip_vcc
;
416 struct sk_buff_head copy
;
420 if (!vcc
->push
) return -EBADFD
;
421 clip_vcc
= kmalloc(sizeof(struct clip_vcc
),GFP_KERNEL
);
422 if (!clip_vcc
) return -ENOMEM
;
423 DPRINTK("mkip clip_vcc %p vcc %p\n",clip_vcc
,vcc
);
425 vcc
->user_back
= clip_vcc
;
426 clip_vcc
->entry
= NULL
;
428 clip_vcc
->last_use
= jiffies
;
429 clip_vcc
->idle_timeout
= timeout
*HZ
;
430 clip_vcc
->old_push
= vcc
->push
;
433 vcc
->push
= clip_push
;
434 skb_migrate(&vcc
->recvq
,©
);
435 restore_flags(flags
);
436 /* re-process everything received between connection setup and MKIP */
437 while ((skb
= skb_dequeue(©
)))
439 atm_return(vcc
,skb
->truesize
);
443 unsigned int len
= skb
->len
;
446 PRIV(skb
->dev
)->stats
.rx_packets
--;
447 PRIV(skb
->dev
)->stats
.rx_bytes
-= len
;
453 int clip_setentry(struct atm_vcc
*vcc
,u32 ip
)
455 struct neighbour
*neigh
;
456 struct atmarp_entry
*entry
;
458 struct clip_vcc
*clip_vcc
;
461 if (vcc
->push
!= clip_push
) {
462 printk(KERN_WARNING
"clip_setentry: non-CLIP VCC\n");
465 clip_vcc
= CLIP_VCC(vcc
);
467 if (!clip_vcc
->entry
) {
468 printk(KERN_ERR
"hiding hidden ATMARP entry\n");
471 DPRINTK("setentry: remove\n");
472 unlink_clip_vcc(clip_vcc
);
475 error
= ip_route_output(&rt
,ip
,0,1,0);
476 if (error
) return error
;
477 neigh
= __neigh_lookup(&clip_tbl
,&ip
,rt
->u
.dst
.dev
,1);
481 entry
= NEIGH2ENTRY(neigh
);
482 if (entry
!= clip_vcc
->entry
) {
483 if (!clip_vcc
->entry
) DPRINTK("setentry: add\n");
485 DPRINTK("setentry: update\n");
486 unlink_clip_vcc(clip_vcc
);
488 link_vcc(clip_vcc
,entry
);
490 error
= neigh_update(neigh
,llc_oui
,NUD_PERMANENT
,1,0);
491 neigh_release(neigh
);
496 static int clip_init(struct net_device
*dev
)
498 DPRINTK("clip_init %s\n",dev
->name
);
499 dev
->hard_start_xmit
= clip_start_xmit
;
501 dev
->hard_header
= NULL
;
502 dev
->rebuild_header
= NULL
;
503 dev
->set_mac_address
= NULL
;
504 dev
->hard_header_parse
= NULL
;
505 dev
->hard_header_cache
= NULL
;
506 dev
->header_cache_update
= NULL
;
507 dev
->change_mtu
= NULL
;
508 dev
->do_ioctl
= NULL
;
509 dev
->get_stats
= clip_get_stats
;
510 dev
->type
= ARPHRD_ATM
;
511 dev
->hard_header_len
= RFC1483LLC_LEN
;
512 dev
->mtu
= RFC1626_MTU
;
514 dev
->tx_queue_len
= 0;
516 dev_init_buffers(dev
); /* is this ever supposed to be used ? */
521 int clip_create(int number
)
523 struct net_device
*dev
;
527 for (dev
= clip_devs
; dev
; dev
= PRIV(dev
)->next
)
528 if (PRIV(dev
)->number
== number
) return -EEXIST
;
532 for (dev
= clip_devs
; dev
; dev
= PRIV(dev
)->next
)
533 if (PRIV(dev
)->number
>= number
)
534 number
= PRIV(dev
)->number
+1;
536 dev
= kmalloc(sizeof(struct net_device
)+sizeof(struct clip_priv
),
538 if (!dev
) return -ENOMEM
;
539 memset(dev
,0,sizeof(struct net_device
)+sizeof(struct clip_priv
));
540 dev
->name
= PRIV(dev
)->name
;
541 sprintf(dev
->name
,"atm%d",number
);
542 dev
->init
= clip_init
;
543 PRIV(dev
)->number
= number
;
544 error
= register_netdev(dev
);
549 PRIV(dev
)->next
= clip_devs
;
551 DPRINTK("registered (net:%s)\n",dev
->name
);
556 static int clip_device_event(struct notifier_block
*this,unsigned long event
,
559 /* ignore non-CLIP devices */
560 if (((struct net_device
*) dev
)->type
!= ARPHRD_ATM
||
561 ((struct net_device
*) dev
)->init
!= clip_init
)
565 DPRINTK("clip_device_event NETDEV_UP\n");
566 (void) to_atmarpd(act_up
,PRIV(dev
)->number
,0);
569 DPRINTK("clip_device_event NETDEV_DOWN\n");
570 (void) to_atmarpd(act_down
,PRIV(dev
)->number
,0);
573 case NETDEV_CHANGEMTU
:
574 DPRINTK("clip_device_event NETDEV_CHANGE*\n");
575 (void) to_atmarpd(act_change
,PRIV(dev
)->number
,0);
578 case NETDEV_REGISTER
:
579 DPRINTK("clip_device_event %ld\n",event
);
583 printk(KERN_WARNING
"clip_device_event: unknown event "
591 static int clip_inet_event(struct notifier_block
*this,unsigned long event
,
594 struct in_device
*in_dev
;
596 in_dev
= ((struct in_ifaddr
*) ifa
)->ifa_dev
;
597 if (!in_dev
|| !in_dev
->dev
) {
598 printk(KERN_WARNING
"clip_inet_event: no device\n");
602 * Transitions are of the down-change-up type, so it's sufficient to
603 * handle the change on up.
605 if (event
!= NETDEV_UP
) return NOTIFY_DONE
;
606 return clip_device_event(this,NETDEV_CHANGE
,in_dev
->dev
);
610 static struct notifier_block clip_dev_notifier
= {
618 static struct notifier_block clip_inet_notifier
= {
626 static void atmarpd_close(struct atm_vcc
*vcc
)
630 DPRINTK("atmarpd_close\n");
631 atmarpd
= NULL
; /* assumed to be atomic */
633 unregister_inetaddr_notifier(&clip_inet_notifier
);
634 unregister_netdevice_notifier(&clip_dev_notifier
);
635 if (skb_peek(&vcc
->recvq
))
636 printk(KERN_ERR
"atmarpd_close: closing with requests "
638 while ((skb
= skb_dequeue(&vcc
->recvq
))) kfree_skb(skb
);
643 static struct atmdev_ops atmarpd_dev_ops
= {
644 NULL
, /* no dev_close */
646 atmarpd_close
, /* close */
648 NULL
, /* no getsockopt */
649 NULL
, /* no setsockopt */
651 NULL
, /* no sg_send */
652 NULL
, /* no send_oam */
653 NULL
, /* no phy_put */
654 NULL
, /* no phy_get */
655 NULL
, /* no feedback */
656 NULL
, /* no change_qos */
657 NULL
/* no free_rx_skb */
661 static struct atm_dev atmarpd_dev
= {
665 999, /* dummy device number */
666 NULL
,NULL
, /* pretend not to have any VCCs */
667 NULL
,NULL
, /* no data */
669 NULL
, /* no local address */
670 { 0 } /* no ESI, no statistics */
674 int atm_init_atmarp(struct atm_vcc
*vcc
)
676 struct net_device
*dev
;
678 if (atmarpd
) return -EADDRINUSE
;
681 init_timer(&idle_timer
);
682 idle_timer
.expires
= jiffies
+CLIP_CHECK_INTERVAL
*HZ
;
683 idle_timer
.function
= idle_timer_check
;
684 add_timer(&idle_timer
);
687 vcc
->flags
|= ATM_VF_READY
| ATM_VF_META
;
688 /* allow replies and avoid getting closed if signaling dies */
689 bind_vcc(vcc
,&atmarpd_dev
);
691 vcc
->pop
= NULL
; /* crash */
692 vcc
->push_oam
= NULL
; /* crash */
693 if (register_netdevice_notifier(&clip_dev_notifier
))
694 printk(KERN_ERR
"register_netdevice_notifier failed\n");
695 if (register_inetaddr_notifier(&clip_inet_notifier
))
696 printk(KERN_ERR
"register_inetaddr_notifier failed\n");
697 for (dev
= clip_devs
; dev
; dev
= PRIV(dev
)->next
)
698 if (dev
->flags
& IFF_UP
)
699 (void) to_atmarpd(act_up
,PRIV(dev
)->number
,0);
704 void atm_clip_init(void)
706 clip_tbl
.lock
= RW_LOCK_UNLOCKED
;
707 clip_tbl
.kmem_cachep
= kmem_cache_create(clip_tbl
.id
,
708 clip_tbl
.entry_size
, 0, SLAB_HWCACHE_ALIGN
, NULL
, NULL
);