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