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