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