ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / atm / clip.c
1 /* net/atm/clip.c - RFC1577 Classical IP over ATM */
2
3 /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
4
5
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/module.h>
11 #include <linux/init.h>
12 #include <linux/netdevice.h>
13 #include <linux/skbuff.h>
14 #include <linux/wait.h>
15 #include <linux/timer.h>
16 #include <linux/if_arp.h> /* for some manifest constants */
17 #include <linux/notifier.h>
18 #include <linux/atm.h>
19 #include <linux/atmdev.h>
20 #include <linux/atmclip.h>
21 #include <linux/atmarp.h>
22 #include <linux/ip.h> /* for net/route.h */
23 #include <linux/in.h> /* for struct sockaddr_in */
24 #include <linux/if.h> /* for IFF_UP */
25 #include <linux/inetdevice.h>
26 #include <linux/bitops.h>
27 #include <linux/proc_fs.h>
28 #include <linux/seq_file.h>
29 #include <net/route.h> /* for struct rtable and routing */
30 #include <net/icmp.h> /* icmp_send */
31 #include <asm/param.h> /* for HZ */
32 #include <asm/byteorder.h> /* for htons etc. */
33 #include <asm/system.h> /* save/restore_flags */
34 #include <asm/uaccess.h>
35 #include <asm/atomic.h>
36
37 #include "common.h"
38 #include "resources.h"
39 #include "ipcommon.h"
40 #include <net/atmclip.h>
41
42
43 #if 0
44 #define DPRINTK(format,args...) printk(format,##args)
45 #else
46 #define DPRINTK(format,args...)
47 #endif
48
49
50 struct net_device *clip_devs = NULL;
51 struct atm_vcc *atmarpd = NULL;
52 static struct neigh_table clip_tbl;
53 static struct timer_list idle_timer;
54 static int start_timer = 1;
55
56
57 static int to_atmarpd(enum atmarp_ctrl_type type,int itf,unsigned long ip)
58 {
59         struct atmarp_ctrl *ctrl;
60         struct sk_buff *skb;
61
62         DPRINTK("to_atmarpd(%d)\n",type);
63         if (!atmarpd) return -EUNATCH;
64         skb = alloc_skb(sizeof(struct atmarp_ctrl),GFP_ATOMIC);
65         if (!skb) return -ENOMEM;
66         ctrl = (struct atmarp_ctrl *) skb_put(skb,sizeof(struct atmarp_ctrl));
67         ctrl->type = type;
68         ctrl->itf_num = itf;
69         ctrl->ip = ip;
70         atm_force_charge(atmarpd,skb->truesize);
71         skb_queue_tail(&atmarpd->sk->sk_receive_queue, skb);
72         atmarpd->sk->sk_data_ready(atmarpd->sk, skb->len);
73         return 0;
74 }
75
76
77 static void link_vcc(struct clip_vcc *clip_vcc,struct atmarp_entry *entry)
78 {
79         DPRINTK("link_vcc %p to entry %p (neigh %p)\n",clip_vcc,entry,
80             entry->neigh);
81         clip_vcc->entry = entry;
82         clip_vcc->xoff = 0; /* @@@ may overrun buffer by one packet */
83         clip_vcc->next = entry->vccs;
84         entry->vccs = clip_vcc;
85         entry->neigh->used = jiffies;
86 }
87
88
89 static void unlink_clip_vcc(struct clip_vcc *clip_vcc)
90 {
91         struct atmarp_entry *entry = clip_vcc->entry;
92         struct clip_vcc **walk;
93
94         if (!entry) {
95                 printk(KERN_CRIT "!clip_vcc->entry (clip_vcc %p)\n",clip_vcc);
96                 return;
97         }
98         spin_lock_bh(&entry->neigh->dev->xmit_lock);    /* block clip_start_xmit() */
99         entry->neigh->used = jiffies;
100         for (walk = &entry->vccs; *walk; walk = &(*walk)->next)
101                 if (*walk == clip_vcc) {
102                         int error;
103
104                         *walk = clip_vcc->next; /* atomic */
105                         clip_vcc->entry = NULL;
106                         if (clip_vcc->xoff)
107                                 netif_wake_queue(entry->neigh->dev);
108                         if (entry->vccs)
109                                 goto out;
110                         entry->expires = jiffies-1;
111                                 /* force resolution or expiration */
112                         error = neigh_update(entry->neigh,NULL,NUD_NONE,0,0);
113                         if (error)
114                                 printk(KERN_CRIT "unlink_clip_vcc: "
115                                     "neigh_update failed with %d\n",error);
116                         goto out;
117                 }
118         printk(KERN_CRIT "ATMARP: unlink_clip_vcc failed (entry %p, vcc "
119           "0x%p)\n",entry,clip_vcc);
120 out:
121         spin_unlock_bh(&entry->neigh->dev->xmit_lock);
122 }
123
124
125 static void idle_timer_check(unsigned long dummy)
126 {
127         int i;
128
129         /*DPRINTK("idle_timer_check\n");*/
130         write_lock(&clip_tbl.lock);
131         for (i = 0; i <= NEIGH_HASHMASK; i++) {
132                 struct neighbour **np;
133
134                 for (np = &clip_tbl.hash_buckets[i]; *np;) {
135                         struct neighbour *n = *np;
136                         struct atmarp_entry *entry = NEIGH2ENTRY(n);
137                         struct clip_vcc *clip_vcc;
138
139                         write_lock(&n->lock);
140
141                         for (clip_vcc = entry->vccs; clip_vcc;
142                             clip_vcc = clip_vcc->next)
143                                 if (clip_vcc->idle_timeout &&
144                                     time_after(jiffies, clip_vcc->last_use+
145                                     clip_vcc->idle_timeout)) {
146                                         DPRINTK("releasing vcc %p->%p of "
147                                             "entry %p\n",clip_vcc,clip_vcc->vcc,
148                                             entry);
149                                         vcc_release_async(clip_vcc->vcc,
150                                                           -ETIMEDOUT);
151                                 }
152                         if (entry->vccs ||
153                             time_before(jiffies, entry->expires)) {
154                                 np = &n->next;
155                                 write_unlock(&n->lock);
156                                 continue;
157                         }
158                         if (atomic_read(&n->refcnt) > 1) {
159                                 struct sk_buff *skb;
160
161                                 DPRINTK("destruction postponed with ref %d\n",
162                                     atomic_read(&n->refcnt));
163                                 while ((skb = skb_dequeue(&n->arp_queue)) !=
164                                      NULL) 
165                                         dev_kfree_skb(skb);
166                                 np = &n->next;
167                                 write_unlock(&n->lock);
168                                 continue;
169                         }
170                         *np = n->next;
171                         DPRINTK("expired neigh %p\n",n);
172                         n->dead = 1;
173                         write_unlock(&n->lock);
174                         neigh_release(n);
175                 }
176         }
177         mod_timer(&idle_timer, jiffies+CLIP_CHECK_INTERVAL*HZ);
178         write_unlock(&clip_tbl.lock);
179 }
180
181
182 static int clip_arp_rcv(struct sk_buff *skb)
183 {
184         struct atm_vcc *vcc;
185
186         DPRINTK("clip_arp_rcv\n");
187         vcc = ATM_SKB(skb)->vcc;
188         if (!vcc || !atm_charge(vcc,skb->truesize)) {
189                 dev_kfree_skb_any(skb);
190                 return 0;
191         }
192         DPRINTK("pushing to %p\n",vcc);
193         DPRINTK("using %p\n",CLIP_VCC(vcc)->old_push);
194         CLIP_VCC(vcc)->old_push(vcc,skb);
195         return 0;
196 }
197
198 static const unsigned char llc_oui[] = {
199         0xaa,   /* DSAP: non-ISO */
200         0xaa,   /* SSAP: non-ISO */
201         0x03,   /* Ctrl: Unnumbered Information Command PDU */
202         0x00,   /* OUI: EtherType */
203         0x00,
204         0x00 };
205
206 static void clip_push(struct atm_vcc *vcc,struct sk_buff *skb)
207 {
208         struct clip_vcc *clip_vcc = CLIP_VCC(vcc);
209
210         DPRINTK("clip push\n");
211         if (!skb) {
212                 DPRINTK("removing VCC %p\n",clip_vcc);
213                 if (clip_vcc->entry) unlink_clip_vcc(clip_vcc);
214                 clip_vcc->old_push(vcc,NULL); /* pass on the bad news */
215                 kfree(clip_vcc);
216                 return;
217         }
218         atm_return(vcc,skb->truesize);
219         skb->dev = clip_vcc->entry ? clip_vcc->entry->neigh->dev : clip_devs;
220                 /* clip_vcc->entry == NULL if we don't have an IP address yet */
221         if (!skb->dev) {
222                 dev_kfree_skb_any(skb);
223                 return;
224         }
225         ATM_SKB(skb)->vcc = vcc;
226         skb->mac.raw = skb->data;
227         if (!clip_vcc->encap || skb->len < RFC1483LLC_LEN || memcmp(skb->data,
228             llc_oui,sizeof(llc_oui))) skb->protocol = htons(ETH_P_IP);
229         else {
230                 skb->protocol = ((u16 *) skb->data)[3];
231                 skb_pull(skb,RFC1483LLC_LEN);
232                 if (skb->protocol == htons(ETH_P_ARP)) {
233                         PRIV(skb->dev)->stats.rx_packets++;
234                         PRIV(skb->dev)->stats.rx_bytes += skb->len;
235                         clip_arp_rcv(skb);
236                         return;
237                 }
238         }
239         clip_vcc->last_use = jiffies;
240         PRIV(skb->dev)->stats.rx_packets++;
241         PRIV(skb->dev)->stats.rx_bytes += skb->len;
242         memset(ATM_SKB(skb), 0, sizeof(struct atm_skb_data));
243         netif_rx(skb);
244 }
245
246
247 /*
248  * Note: these spinlocks _must_not_ block on non-SMP. The only goal is that
249  * clip_pop is atomic with respect to the critical section in clip_start_xmit.
250  */
251
252
253 static void clip_pop(struct atm_vcc *vcc,struct sk_buff *skb)
254 {
255         struct clip_vcc *clip_vcc = CLIP_VCC(vcc);
256         struct net_device *dev = skb->dev;
257         int old;
258         unsigned long flags;
259
260         DPRINTK("clip_pop(vcc %p)\n",vcc);
261         clip_vcc->old_pop(vcc,skb);
262         /* skb->dev == NULL in outbound ARP packets */
263         if (!dev) return;
264         spin_lock_irqsave(&PRIV(dev)->xoff_lock,flags);
265         if (atm_may_send(vcc,0)) {
266                 old = xchg(&clip_vcc->xoff,0);
267                 if (old) netif_wake_queue(dev);
268         }
269         spin_unlock_irqrestore(&PRIV(dev)->xoff_lock,flags);
270 }
271
272
273 static void clip_neigh_destroy(struct neighbour *neigh)
274 {
275         DPRINTK("clip_neigh_destroy (neigh %p)\n",neigh);
276         if (NEIGH2ENTRY(neigh)->vccs)
277                 printk(KERN_CRIT "clip_neigh_destroy: vccs != NULL !!!\n");
278         NEIGH2ENTRY(neigh)->vccs = (void *) 0xdeadbeef;
279 }
280
281
282 static void clip_neigh_solicit(struct neighbour *neigh,struct sk_buff *skb)
283 {
284         DPRINTK("clip_neigh_solicit (neigh %p, skb %p)\n",neigh,skb);
285         to_atmarpd(act_need,PRIV(neigh->dev)->number,NEIGH2ENTRY(neigh)->ip);
286 }
287
288
289 static void clip_neigh_error(struct neighbour *neigh,struct sk_buff *skb)
290 {
291 #ifndef CONFIG_ATM_CLIP_NO_ICMP
292         icmp_send(skb,ICMP_DEST_UNREACH,ICMP_HOST_UNREACH,0);
293 #endif
294         kfree_skb(skb);
295 }
296
297
298 static struct neigh_ops clip_neigh_ops = {
299         .family =               AF_INET,
300         .destructor =           clip_neigh_destroy,
301         .solicit =              clip_neigh_solicit,
302         .error_report =         clip_neigh_error,
303         .output =               dev_queue_xmit,
304         .connected_output =     dev_queue_xmit,
305         .hh_output =            dev_queue_xmit,
306         .queue_xmit =           dev_queue_xmit,
307 };
308
309
310 static int clip_constructor(struct neighbour *neigh)
311 {
312         struct atmarp_entry *entry = NEIGH2ENTRY(neigh);
313         struct net_device *dev = neigh->dev;
314         struct in_device *in_dev = dev->ip_ptr;
315
316         DPRINTK("clip_constructor (neigh %p, entry %p)\n",neigh,entry);
317         if (!in_dev) return -EINVAL;
318         neigh->type = inet_addr_type(entry->ip);
319         if (neigh->type != RTN_UNICAST) return -EINVAL;
320         if (in_dev->arp_parms) neigh->parms = in_dev->arp_parms;
321         neigh->ops = &clip_neigh_ops;
322         neigh->output = neigh->nud_state & NUD_VALID ?
323             neigh->ops->connected_output : neigh->ops->output;
324         entry->neigh = neigh;
325         entry->vccs = NULL;
326         entry->expires = jiffies-1;
327         return 0;
328 }
329
330 static u32 clip_hash(const void *pkey, const struct net_device *dev)
331 {
332         u32 hash_val;
333
334         hash_val = *(u32*)pkey;
335         hash_val ^= (hash_val>>16);
336         hash_val ^= hash_val>>8;
337         hash_val ^= hash_val>>3;
338         hash_val = (hash_val^dev->ifindex)&NEIGH_HASHMASK;
339
340         return hash_val;
341 }
342
343 static struct neigh_table clip_tbl = {
344         .family         = AF_INET,
345         .entry_size     = sizeof(struct neighbour)+sizeof(struct atmarp_entry),
346         .key_len        = 4,
347         .hash           = clip_hash,
348         .constructor    = clip_constructor,
349         .id             = "clip_arp_cache",
350
351         /* parameters are copied from ARP ... */
352         .parms = {
353                 .tbl                    = &clip_tbl,
354                 .base_reachable_time    = 30 * HZ,
355                 .retrans_time           = 1 * HZ,
356                 .gc_staletime           = 60 * HZ,
357                 .reachable_time         = 30 * HZ,
358                 .delay_probe_time       = 5 * HZ,
359                 .queue_len              = 3,
360                 .ucast_probes           = 3,
361                 .mcast_probes           = 3,
362                 .anycast_delay          = 1 * HZ,
363                 .proxy_delay            = (8 * HZ) / 10,
364                 .proxy_qlen             = 64,
365                 .locktime               = 1 * HZ,
366         },
367         .gc_interval    = 30 * HZ,
368         .gc_thresh1     = 128,
369         .gc_thresh2     = 512,
370         .gc_thresh3     = 1024,
371 };
372
373
374 /* @@@ copy bh locking from arp.c -- need to bh-enable atm code before */
375
376 /*
377  * We play with the resolve flag: 0 and 1 have the usual meaning, but -1 means
378  * to allocate the neighbour entry but not to ask atmarpd for resolution. Also,
379  * don't increment the usage count. This is used to create entries in
380  * clip_setentry.
381  */
382
383
384 static int clip_encap(struct atm_vcc *vcc,int mode)
385 {
386         CLIP_VCC(vcc)->encap = mode;
387         return 0;
388 }
389
390
391 static int clip_start_xmit(struct sk_buff *skb,struct net_device *dev)
392 {
393         struct clip_priv *clip_priv = PRIV(dev);
394         struct atmarp_entry *entry;
395         struct atm_vcc *vcc;
396         int old;
397         unsigned long flags;
398
399         DPRINTK("clip_start_xmit (skb %p)\n",skb);
400         if (!skb->dst) {
401                 printk(KERN_ERR "clip_start_xmit: skb->dst == NULL\n");
402                 dev_kfree_skb(skb);
403                 clip_priv->stats.tx_dropped++;
404                 return 0;
405         }
406         if (!skb->dst->neighbour) {
407 #if 0
408                 skb->dst->neighbour = clip_find_neighbour(skb->dst,1);
409                 if (!skb->dst->neighbour) {
410                         dev_kfree_skb(skb); /* lost that one */
411                         clip_priv->stats.tx_dropped++;
412                         return 0;
413                 }
414 #endif
415                 printk(KERN_ERR "clip_start_xmit: NO NEIGHBOUR !\n");
416                 dev_kfree_skb(skb);
417                 clip_priv->stats.tx_dropped++;
418                 return 0;
419         }
420         entry = NEIGH2ENTRY(skb->dst->neighbour);
421         if (!entry->vccs) {
422                 if (time_after(jiffies, entry->expires)) {
423                         /* should be resolved */
424                         entry->expires = jiffies+ATMARP_RETRY_DELAY*HZ;
425                         to_atmarpd(act_need,PRIV(dev)->number,entry->ip);
426                 }
427                 if (entry->neigh->arp_queue.qlen < ATMARP_MAX_UNRES_PACKETS)
428                         skb_queue_tail(&entry->neigh->arp_queue,skb);
429                 else {
430                         dev_kfree_skb(skb);
431                         clip_priv->stats.tx_dropped++;
432                 }
433                 return 0;
434         }
435         DPRINTK("neigh %p, vccs %p\n",entry,entry->vccs);
436         ATM_SKB(skb)->vcc = vcc = entry->vccs->vcc;
437         DPRINTK("using neighbour %p, vcc %p\n",skb->dst->neighbour,vcc);
438         if (entry->vccs->encap) {
439                 void *here;
440
441                 here = skb_push(skb,RFC1483LLC_LEN);
442                 memcpy(here,llc_oui,sizeof(llc_oui));
443                 ((u16 *) here)[3] = skb->protocol;
444         }
445         atomic_add(skb->truesize, &vcc->sk->sk_wmem_alloc);
446         ATM_SKB(skb)->atm_options = vcc->atm_options;
447         entry->vccs->last_use = jiffies;
448         DPRINTK("atm_skb(%p)->vcc(%p)->dev(%p)\n",skb,vcc,vcc->dev);
449         old = xchg(&entry->vccs->xoff,1); /* assume XOFF ... */
450         if (old) {
451                 printk(KERN_WARNING "clip_start_xmit: XOFF->XOFF transition\n");
452                 return 0;
453         }
454         clip_priv->stats.tx_packets++;
455         clip_priv->stats.tx_bytes += skb->len;
456         (void) vcc->send(vcc,skb);
457         if (atm_may_send(vcc,0)) {
458                 entry->vccs->xoff = 0;
459                 return 0;
460         }
461         spin_lock_irqsave(&clip_priv->xoff_lock,flags);
462         netif_stop_queue(dev); /* XOFF -> throttle immediately */
463         barrier();
464         if (!entry->vccs->xoff)
465                 netif_start_queue(dev);
466                 /* Oh, we just raced with clip_pop. netif_start_queue should be
467                    good enough, because nothing should really be asleep because
468                    of the brief netif_stop_queue. If this isn't true or if it
469                    changes, use netif_wake_queue instead. */
470         spin_unlock_irqrestore(&clip_priv->xoff_lock,flags);
471         return 0;
472 }
473
474
475 static struct net_device_stats *clip_get_stats(struct net_device *dev)
476 {
477         return &PRIV(dev)->stats;
478 }
479
480
481 static int clip_mkip(struct atm_vcc *vcc,int timeout)
482 {
483         struct clip_vcc *clip_vcc;
484         struct sk_buff_head copy;
485         struct sk_buff *skb;
486
487         if (!vcc->push) return -EBADFD;
488         clip_vcc = kmalloc(sizeof(struct clip_vcc),GFP_KERNEL);
489         if (!clip_vcc) return -ENOMEM;
490         DPRINTK("mkip clip_vcc %p vcc %p\n",clip_vcc,vcc);
491         clip_vcc->vcc = vcc;
492         vcc->user_back = clip_vcc;
493         set_bit(ATM_VF_IS_CLIP, &vcc->flags);
494         clip_vcc->entry = NULL;
495         clip_vcc->xoff = 0;
496         clip_vcc->encap = 1;
497         clip_vcc->last_use = jiffies;
498         clip_vcc->idle_timeout = timeout*HZ;
499         clip_vcc->old_push = vcc->push;
500         clip_vcc->old_pop = vcc->pop;
501         vcc->push = clip_push;
502         vcc->pop = clip_pop;
503         skb_queue_head_init(&copy);
504         skb_migrate(&vcc->sk->sk_receive_queue, &copy);
505         /* re-process everything received between connection setup and MKIP */
506         while ((skb = skb_dequeue(&copy)))
507                 if (!clip_devs) {
508                         atm_return(vcc,skb->truesize);
509                         kfree_skb(skb);
510                 }
511                 else {
512                         unsigned int len = skb->len;
513
514                         clip_push(vcc,skb);
515                         PRIV(skb->dev)->stats.rx_packets--;
516                         PRIV(skb->dev)->stats.rx_bytes -= len;
517                 }
518         return 0;
519 }
520
521
522 static int clip_setentry(struct atm_vcc *vcc,u32 ip)
523 {
524         struct neighbour *neigh;
525         struct atmarp_entry *entry;
526         int error;
527         struct clip_vcc *clip_vcc;
528         struct flowi fl = { .nl_u = { .ip4_u = { .daddr = ip, .tos = 1 } } };
529         struct rtable *rt;
530
531         if (vcc->push != clip_push) {
532                 printk(KERN_WARNING "clip_setentry: non-CLIP VCC\n");
533                 return -EBADF;
534         }
535         clip_vcc = CLIP_VCC(vcc);
536         if (!ip) {
537                 if (!clip_vcc->entry) {
538                         printk(KERN_ERR "hiding hidden ATMARP entry\n");
539                         return 0;
540                 }
541                 DPRINTK("setentry: remove\n");
542                 unlink_clip_vcc(clip_vcc);
543                 return 0;
544         }
545         error = ip_route_output_key(&rt,&fl);
546         if (error) return error;
547         neigh = __neigh_lookup(&clip_tbl,&ip,rt->u.dst.dev,1);
548         ip_rt_put(rt);
549         if (!neigh)
550                 return -ENOMEM;
551         entry = NEIGH2ENTRY(neigh);
552         if (entry != clip_vcc->entry) {
553                 if (!clip_vcc->entry) DPRINTK("setentry: add\n");
554                 else {
555                         DPRINTK("setentry: update\n");
556                         unlink_clip_vcc(clip_vcc);
557                 }
558                 link_vcc(clip_vcc,entry);
559         }
560         error = neigh_update(neigh,llc_oui,NUD_PERMANENT,1,0);
561         neigh_release(neigh);
562         return error;
563 }
564
565
566 static void clip_setup(struct net_device *dev)
567 {
568         dev->hard_start_xmit = clip_start_xmit;
569         /* sg_xmit ... */
570         dev->get_stats = clip_get_stats;
571         dev->type = ARPHRD_ATM;
572         dev->hard_header_len = RFC1483LLC_LEN;
573         dev->mtu = RFC1626_MTU;
574         dev->tx_queue_len = 100; /* "normal" queue (packets) */
575             /* When using a "real" qdisc, the qdisc determines the queue */
576             /* length. tx_queue_len is only used for the default case, */
577             /* without any more elaborate queuing. 100 is a reasonable */
578             /* compromise between decent burst-tolerance and protection */
579             /* against memory hogs. */
580 }
581
582
583 static int clip_create(int number)
584 {
585         struct net_device *dev;
586         struct clip_priv *clip_priv;
587         int error;
588
589         if (number != -1) {
590                 for (dev = clip_devs; dev; dev = PRIV(dev)->next)
591                         if (PRIV(dev)->number == number) return -EEXIST;
592         }
593         else {
594                 number = 0;
595                 for (dev = clip_devs; dev; dev = PRIV(dev)->next)
596                         if (PRIV(dev)->number >= number)
597                                 number = PRIV(dev)->number+1;
598         }
599         dev = alloc_netdev(sizeof(struct clip_priv), "", clip_setup);
600         if (!dev)
601                 return -ENOMEM;
602         clip_priv = PRIV(dev);
603         sprintf(dev->name,"atm%d",number);
604         spin_lock_init(&clip_priv->xoff_lock);
605         clip_priv->number = number;
606         error = register_netdev(dev);
607         if (error) {
608                 free_netdev(dev);
609                 return error;
610         }
611         clip_priv->next = clip_devs;
612         clip_devs = dev;
613         DPRINTK("registered (net:%s)\n",dev->name);
614         return number;
615 }
616
617
618 static int clip_device_event(struct notifier_block *this,unsigned long event,
619     void *dev)
620 {
621         /* ignore non-CLIP devices */
622         if (((struct net_device *) dev)->type != ARPHRD_ATM ||
623             ((struct net_device *) dev)->hard_start_xmit != clip_start_xmit)
624                 return NOTIFY_DONE;
625         switch (event) {
626                 case NETDEV_UP:
627                         DPRINTK("clip_device_event NETDEV_UP\n");
628                         (void) to_atmarpd(act_up,PRIV(dev)->number,0);
629                         break;
630                 case NETDEV_GOING_DOWN:
631                         DPRINTK("clip_device_event NETDEV_DOWN\n");
632                         (void) to_atmarpd(act_down,PRIV(dev)->number,0);
633                         break;
634                 case NETDEV_CHANGE:
635                 case NETDEV_CHANGEMTU:
636                         DPRINTK("clip_device_event NETDEV_CHANGE*\n");
637                         (void) to_atmarpd(act_change,PRIV(dev)->number,0);
638                         break;
639                 case NETDEV_REBOOT:
640                 case NETDEV_REGISTER:
641                 case NETDEV_DOWN:
642                         DPRINTK("clip_device_event %ld\n",event);
643                         /* ignore */
644                         break;
645                 default:
646                         printk(KERN_WARNING "clip_device_event: unknown event "
647                             "%ld\n",event);
648                         break;
649         }
650         return NOTIFY_DONE;
651 }
652
653
654 static int clip_inet_event(struct notifier_block *this,unsigned long event,
655     void *ifa)
656 {
657         struct in_device *in_dev;
658
659         in_dev = ((struct in_ifaddr *) ifa)->ifa_dev;
660         if (!in_dev || !in_dev->dev) {
661                 printk(KERN_WARNING "clip_inet_event: no device\n");
662                 return NOTIFY_DONE;
663         }
664         /*
665          * Transitions are of the down-change-up type, so it's sufficient to
666          * handle the change on up.
667          */
668         if (event != NETDEV_UP) return NOTIFY_DONE;
669         return clip_device_event(this,NETDEV_CHANGE,in_dev->dev);
670 }
671
672
673 static struct notifier_block clip_dev_notifier = {
674         clip_device_event,
675         NULL,
676         0
677 };
678
679
680
681 static struct notifier_block clip_inet_notifier = {
682         clip_inet_event,
683         NULL,
684         0
685 };
686
687
688
689 static void atmarpd_close(struct atm_vcc *vcc)
690 {
691         DPRINTK("atmarpd_close\n");
692         atmarpd = NULL; /* assumed to be atomic */
693         barrier();
694         unregister_inetaddr_notifier(&clip_inet_notifier);
695         unregister_netdevice_notifier(&clip_dev_notifier);
696         if (skb_peek(&vcc->sk->sk_receive_queue))
697                 printk(KERN_ERR "atmarpd_close: closing with requests "
698                     "pending\n");
699         skb_queue_purge(&vcc->sk->sk_receive_queue);
700         DPRINTK("(done)\n");
701         module_put(THIS_MODULE);
702 }
703
704
705 static struct atmdev_ops atmarpd_dev_ops = {
706         .close = atmarpd_close
707 };
708
709
710 static struct atm_dev atmarpd_dev = {
711         .ops =                  &atmarpd_dev_ops,
712         .type =                 "arpd",
713         .number =               999,
714         .lock =                 SPIN_LOCK_UNLOCKED
715 };
716
717
718 static int atm_init_atmarp(struct atm_vcc *vcc)
719 {
720         if (atmarpd) return -EADDRINUSE;
721         if (start_timer) {
722                 start_timer = 0;
723                 init_timer(&idle_timer);
724                 idle_timer.expires = jiffies+CLIP_CHECK_INTERVAL*HZ;
725                 idle_timer.function = idle_timer_check;
726                 add_timer(&idle_timer);
727         }
728         atmarpd = vcc;
729         set_bit(ATM_VF_META,&vcc->flags);
730         set_bit(ATM_VF_READY,&vcc->flags);
731             /* allow replies and avoid getting closed if signaling dies */
732         vcc->dev = &atmarpd_dev;
733         vcc_insert_socket(vcc->sk);
734         vcc->push = NULL;
735         vcc->pop = NULL; /* crash */
736         vcc->push_oam = NULL; /* crash */
737         if (register_netdevice_notifier(&clip_dev_notifier))
738                 printk(KERN_ERR "register_netdevice_notifier failed\n");
739         if (register_inetaddr_notifier(&clip_inet_notifier))
740                 printk(KERN_ERR "register_inetaddr_notifier failed\n");
741         return 0;
742 }
743
744 static int clip_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
745 {
746         struct atm_vcc *vcc = ATM_SD(sock);
747         int err = 0;
748
749         switch (cmd) {
750                 case SIOCMKCLIP:
751                 case ATMARPD_CTRL:
752                 case ATMARP_MKIP:
753                 case ATMARP_SETENTRY:
754                 case ATMARP_ENCAP:
755                         if (!capable(CAP_NET_ADMIN))
756                                 return -EPERM;
757                         break;
758                 default:
759                         return -ENOIOCTLCMD;
760         }
761
762         switch (cmd) {
763                 case SIOCMKCLIP:
764                         err = clip_create(arg);
765                         break;
766                 case ATMARPD_CTRL:
767                         err = atm_init_atmarp(vcc);
768                         if (!err) {
769                                 sock->state = SS_CONNECTED;
770                                 __module_get(THIS_MODULE);
771                         }
772                         break;
773                 case ATMARP_MKIP:
774                         err = clip_mkip(vcc ,arg);
775                         break;
776                 case ATMARP_SETENTRY:
777                         err = clip_setentry(vcc, arg);
778                         break;
779                 case ATMARP_ENCAP:
780                         err = clip_encap(vcc, arg);
781                         break;
782         }
783         return err;
784 }
785
786 static struct atm_ioctl clip_ioctl_ops = {
787         .owner  = THIS_MODULE,
788         .ioctl  = clip_ioctl,
789 };
790
791 #ifdef CONFIG_PROC_FS
792
793 static void svc_addr(struct seq_file *seq, struct sockaddr_atmsvc *addr)
794 {
795         static int code[] = { 1,2,10,6,1,0 };
796         static int e164[] = { 1,8,4,6,1,0 };
797
798         if (*addr->sas_addr.pub) {
799                 seq_printf(seq, "%s", addr->sas_addr.pub);
800                 if (*addr->sas_addr.prv)
801                         seq_putc(seq, '+');
802         } else if (!*addr->sas_addr.prv) {
803                 seq_printf(seq, "%s", "(none)");
804                 return;
805         }
806         if (*addr->sas_addr.prv) {
807                 unsigned char *prv = addr->sas_addr.prv;
808                 int *fields;
809                 int i, j;
810
811                 fields = *prv == ATM_AFI_E164 ? e164 : code;
812                 for (i = 0; fields[i]; i++) {
813                         for (j = fields[i]; j; j--)
814                                 seq_printf(seq, "%02X", *prv++);
815                         if (fields[i+1])
816                                 seq_putc(seq, '.');
817                 }
818         }
819 }
820
821 static void atmarp_info(struct seq_file *seq, struct net_device *dev,
822                         struct atmarp_entry *entry, struct clip_vcc *clip_vcc)
823 {
824         char buf[17];
825         int svc, off;
826
827         svc = !clip_vcc || clip_vcc->vcc->sk->sk_family == AF_ATMSVC;
828         seq_printf(seq, "%-6s%-4s%-4s%5ld ", dev->name, svc ? "SVC" : "PVC",
829             !clip_vcc || clip_vcc->encap ? "LLC" : "NULL",
830             (jiffies-(clip_vcc ? clip_vcc->last_use : entry->neigh->used))/HZ);
831
832         off = scnprintf(buf, sizeof(buf) - 1, "%d.%d.%d.%d", NIPQUAD(entry->ip));
833         while (off < 16)
834                 buf[off++] = ' ';
835         buf[off] = '\0';
836         seq_printf(seq, "%s", buf);
837
838         if (!clip_vcc) {
839                 if (time_before(jiffies, entry->expires))
840                         seq_printf(seq, "(resolving)\n");
841                 else
842                         seq_printf(seq, "(expired, ref %d)\n",
843                                    atomic_read(&entry->neigh->refcnt));
844         } else if (!svc) {
845                 seq_printf(seq, "%d.%d.%d\n", clip_vcc->vcc->dev->number,
846                            clip_vcc->vcc->vpi, clip_vcc->vcc->vci);
847         } else {
848                 svc_addr(seq, &clip_vcc->vcc->remote);
849                 seq_putc(seq, '\n');
850         }
851 }
852
853 struct arp_state {
854         int bucket;
855         struct neighbour *n;
856         struct clip_vcc *vcc;
857 };
858   
859 static void *arp_vcc_walk(struct arp_state *state,
860                           struct atmarp_entry *e, loff_t *l)
861 {
862         struct clip_vcc *vcc = state->vcc;
863
864         if (!vcc)
865                 vcc = e->vccs;
866         if (vcc == (void *)1) {
867                 vcc = e->vccs;
868                 --*l;
869         }
870         for (; vcc; vcc = vcc->next) {
871                 if (--*l < 0)
872                         break;
873         }
874         state->vcc = vcc;
875         return (*l < 0) ? state : NULL;
876 }
877   
878 static void *arp_get_idx(struct arp_state *state, loff_t l)
879 {
880         void *v = NULL;
881
882         for (; state->bucket <= NEIGH_HASHMASK; state->bucket++) {
883                 for (; state->n; state->n = state->n->next) {
884                         v = arp_vcc_walk(state, NEIGH2ENTRY(state->n), &l);
885                         if (v)
886                                 goto done;
887                 }
888                 state->n = clip_tbl.hash_buckets[state->bucket + 1];
889         }
890 done:
891         return v;
892 }
893
894 static void *arp_seq_start(struct seq_file *seq, loff_t *pos)
895 {
896         struct arp_state *state = seq->private;
897         void *ret = (void *)1;
898
899         read_lock_bh(&clip_tbl.lock);
900         state->bucket = 0;
901         state->n = clip_tbl.hash_buckets[0];
902         state->vcc = (void *)1;
903         if (*pos)
904                 ret = arp_get_idx(state, *pos);
905         return ret;
906 }
907
908 static void arp_seq_stop(struct seq_file *seq, void *v)
909 {
910         struct arp_state *state = seq->private;
911
912         if (state->bucket != -1)
913                 read_unlock_bh(&clip_tbl.lock);
914 }
915
916 static void *arp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
917 {
918         struct arp_state *state = seq->private;
919
920         v = arp_get_idx(state, 1);
921         *pos += !!PTR_ERR(v);
922         return v;
923 }
924
925 static int arp_seq_show(struct seq_file *seq, void *v)
926 {
927         static char atm_arp_banner[] = 
928                 "IPitf TypeEncp Idle IP address      ATM address\n";
929
930         if (v == (void *)1)
931                 seq_puts(seq, atm_arp_banner);
932         else {
933                 struct arp_state *state = seq->private;
934                 struct neighbour *n = state->n; 
935                 struct clip_vcc *vcc = state->vcc;
936
937                 atmarp_info(seq, n->dev, NEIGH2ENTRY(n), vcc);
938         }
939         return 0;
940 }
941
942 static struct seq_operations arp_seq_ops = {
943         .start  = arp_seq_start,
944         .next   = arp_seq_next,
945         .stop   = arp_seq_stop,
946         .show   = arp_seq_show,
947 };
948
949 static int arp_seq_open(struct inode *inode, struct file *file)
950 {
951         struct arp_state *state;
952         struct seq_file *seq;
953         int rc = -EAGAIN;
954
955         state = kmalloc(sizeof(*state), GFP_KERNEL);
956         if (!state) {
957                 rc = -ENOMEM;
958                 goto out_kfree;
959         }
960
961         rc = seq_open(file, &arp_seq_ops);
962         if (rc)
963                 goto out_kfree;
964
965         seq = file->private_data;
966         seq->private = state;
967 out:
968         return rc;
969
970 out_kfree:
971         kfree(state);
972         goto out;
973 }
974
975 static int arp_seq_release(struct inode *inode, struct file *file)
976 {
977         return seq_release_private(inode, file);
978 }
979
980 static struct file_operations arp_seq_fops = {
981         .open           = arp_seq_open,
982         .read           = seq_read,
983         .llseek         = seq_lseek,
984         .release        = arp_seq_release,
985         .owner          = THIS_MODULE
986 };
987 #endif
988
989 static int __init atm_clip_init(void)
990 {
991         /* we should use neigh_table_init() */
992         clip_tbl.lock = RW_LOCK_UNLOCKED;
993         clip_tbl.kmem_cachep = kmem_cache_create(clip_tbl.id,
994             clip_tbl.entry_size, 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
995
996         if (!clip_tbl.kmem_cachep)
997                 return -ENOMEM;
998
999         /* so neigh_ifdown() doesn't complain */
1000         clip_tbl.proxy_timer.data = 0;
1001         clip_tbl.proxy_timer.function = 0;
1002         init_timer(&clip_tbl.proxy_timer);
1003         skb_queue_head_init(&clip_tbl.proxy_queue);
1004
1005         clip_tbl_hook = &clip_tbl;
1006         register_atm_ioctl(&clip_ioctl_ops);
1007
1008 #ifdef CONFIG_PROC_FS
1009 {
1010         struct proc_dir_entry *p;
1011
1012         p = create_proc_entry("arp", S_IRUGO, atm_proc_root);
1013         if (p)
1014                 p->proc_fops = &arp_seq_fops;
1015 }
1016 #endif
1017
1018         return 0;
1019 }
1020
1021 static void __exit atm_clip_exit(void)
1022 {
1023         struct net_device *dev, *next;
1024
1025         remove_proc_entry("arp", atm_proc_root);
1026
1027         deregister_atm_ioctl(&clip_ioctl_ops);
1028
1029         neigh_ifdown(&clip_tbl, NULL);
1030         dev = clip_devs;
1031         while (dev) {
1032                 next = PRIV(dev)->next;
1033                 unregister_netdev(dev);
1034                 free_netdev(dev);
1035                 dev = next;
1036         }
1037         if (start_timer == 0) del_timer(&idle_timer);
1038
1039         kmem_cache_destroy(clip_tbl.kmem_cachep);
1040
1041         clip_tbl_hook = NULL;
1042 }
1043
1044 module_init(atm_clip_init);
1045 module_exit(atm_clip_exit);
1046
1047 MODULE_LICENSE("GPL");