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