cleanup
[linux-2.4.21-pre4.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/netdevice.h>
11 #include <linux/skbuff.h>
12 #include <linux/wait.h>
13 #include <linux/timer.h>
14 #include <linux/if_arp.h> /* for some manifest constants */
15 #include <linux/notifier.h>
16 #include <linux/atm.h>
17 #include <linux/atmdev.h>
18 #include <linux/atmclip.h>
19 #include <linux/atmarp.h>
20 #include <linux/ip.h> /* for net/route.h */
21 #include <linux/in.h> /* for struct sockaddr_in */
22 #include <linux/if.h> /* for IFF_UP */
23 #include <linux/inetdevice.h>
24 #include <linux/bitops.h>
25 #include <net/route.h> /* for struct rtable and routing */
26 #include <net/icmp.h> /* icmp_send */
27 #include <asm/param.h> /* for HZ */
28 #include <asm/byteorder.h> /* for htons etc. */
29 #include <asm/system.h> /* save/restore_flags */
30 #include <asm/uaccess.h>
31 #include <asm/atomic.h>
32
33 #include "common.h"
34 #include "resources.h"
35 #include "ipcommon.h"
36 #include <net/atmclip.h>
37
38
39 #if 0
40 #define DPRINTK(format,args...) printk(format,##args)
41 #else
42 #define DPRINTK(format,args...)
43 #endif
44
45
46 struct net_device *clip_devs = NULL;
47 struct atm_vcc *atmarpd = NULL;
48 static struct timer_list idle_timer;
49 static int start_timer = 1;
50
51
52 static int to_atmarpd(enum atmarp_ctrl_type type,int itf,unsigned long ip)
53 {
54         struct atmarp_ctrl *ctrl;
55         struct sk_buff *skb;
56
57         DPRINTK("to_atmarpd(%d)\n",type);
58         if (!atmarpd) return -EUNATCH;
59         skb = alloc_skb(sizeof(struct atmarp_ctrl),GFP_ATOMIC);
60         if (!skb) return -ENOMEM;
61         ctrl = (struct atmarp_ctrl *) skb_put(skb,sizeof(struct atmarp_ctrl));
62         ctrl->type = type;
63         ctrl->itf_num = itf;
64         ctrl->ip = ip;
65         atm_force_charge(atmarpd,skb->truesize);
66         skb_queue_tail(&atmarpd->recvq,skb);
67         wake_up(&atmarpd->sleep);
68         return 0;
69 }
70
71
72 static void link_vcc(struct clip_vcc *clip_vcc,struct atmarp_entry *entry)
73 {
74         DPRINTK("link_vcc %p to entry %p (neigh %p)\n",clip_vcc,entry,
75             entry->neigh);
76         clip_vcc->entry = entry;
77         clip_vcc->xoff = 0; /* @@@ may overrun buffer by one packet */
78         clip_vcc->next = entry->vccs;
79         entry->vccs = clip_vcc;
80         entry->neigh->used = jiffies;
81 }
82
83
84 static void unlink_clip_vcc(struct clip_vcc *clip_vcc)
85 {
86         struct atmarp_entry *entry = clip_vcc->entry;
87         struct clip_vcc **walk;
88
89         if (!entry) {
90                 printk(KERN_CRIT "!clip_vcc->entry (clip_vcc %p)\n",clip_vcc);
91                 return;
92         }
93         entry->neigh->used = jiffies;
94         for (walk = &entry->vccs; *walk; walk = &(*walk)->next)
95                 if (*walk == clip_vcc) {
96                         int error;
97
98                         *walk = clip_vcc->next; /* atomic */
99                         clip_vcc->entry = NULL;
100                         if (clip_vcc->xoff)
101                                 netif_wake_queue(entry->neigh->dev);
102                         if (entry->vccs) return;
103                         entry->expires = jiffies-1;
104                                 /* force resolution or expiration */
105                         error = neigh_update(entry->neigh,NULL,NUD_NONE,0,0);
106                         if (error)
107                                 printk(KERN_CRIT "unlink_clip_vcc: "
108                                     "neigh_update failed with %d\n",error);
109                         return;
110                 }
111         printk(KERN_CRIT "ATMARP: unlink_clip_vcc failed (entry %p, vcc "
112           "0x%p)\n",entry,clip_vcc);
113 }
114
115
116 static void idle_timer_check(unsigned long dummy)
117 {
118         int i;
119
120         /*DPRINTK("idle_timer_check\n");*/
121         write_lock(&clip_tbl.lock);
122         for (i = 0; i <= NEIGH_HASHMASK; i++) {
123                 struct neighbour **np;
124
125                 for (np = &clip_tbl.hash_buckets[i]; *np;) {
126                         struct neighbour *n = *np;
127                         struct atmarp_entry *entry = NEIGH2ENTRY(n);
128                         struct clip_vcc *clip_vcc;
129
130                         for (clip_vcc = entry->vccs; clip_vcc;
131                             clip_vcc = clip_vcc->next)
132                                 if (clip_vcc->idle_timeout &&
133                                     time_after(jiffies, clip_vcc->last_use+
134                                     clip_vcc->idle_timeout)) {
135                                         DPRINTK("releasing vcc %p->%p of "
136                                             "entry %p\n",clip_vcc,clip_vcc->vcc,
137                                             entry);
138                                         atm_async_release_vcc(clip_vcc->vcc,
139                                             -ETIMEDOUT);
140                                 }
141                         if (entry->vccs ||
142                             time_before(jiffies, entry->expires)) {
143                                 np = &n->next;
144                                 continue;
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                                 while ((skb = skb_dequeue(&n->arp_queue)) !=
152                                      NULL) 
153                                         dev_kfree_skb(skb);
154                                 np = &n->next;
155                                 continue;
156                         }
157                         *np = n->next;
158                         DPRINTK("expired neigh %p\n",n);
159                         n->dead = 1;
160                         neigh_release(n);
161                 }
162         }
163         mod_timer(&idle_timer, jiffies+CLIP_CHECK_INTERVAL*HZ);
164         write_unlock(&clip_tbl.lock);
165 }
166
167
168 static int clip_arp_rcv(struct sk_buff *skb)
169 {
170         struct atm_vcc *vcc;
171
172         DPRINTK("clip_arp_rcv\n");
173         vcc = ATM_SKB(skb)->vcc;
174         if (!vcc || !atm_charge(vcc,skb->truesize)) {
175                 dev_kfree_skb_any(skb);
176                 return 0;
177         }
178         DPRINTK("pushing to %p\n",vcc);
179         DPRINTK("using %p\n",CLIP_VCC(vcc)->old_push);
180         CLIP_VCC(vcc)->old_push(vcc,skb);
181         return 0;
182 }
183
184
185 void clip_push(struct atm_vcc *vcc,struct sk_buff *skb)
186 {
187         struct clip_vcc *clip_vcc = CLIP_VCC(vcc);
188
189         DPRINTK("clip push\n");
190         if (!skb) {
191                 DPRINTK("removing VCC %p\n",clip_vcc);
192                 if (clip_vcc->entry) unlink_clip_vcc(clip_vcc);
193                 clip_vcc->old_push(vcc,NULL); /* pass on the bad news */
194                 kfree(clip_vcc);
195                 return;
196         }
197         atm_return(vcc,skb->truesize);
198         skb->dev = clip_vcc->entry ? clip_vcc->entry->neigh->dev : clip_devs;
199                 /* clip_vcc->entry == NULL if we don't have an IP address yet */
200         if (!skb->dev) {
201                 dev_kfree_skb_any(skb);
202                 return;
203         }
204         ATM_SKB(skb)->vcc = vcc;
205         skb->mac.raw = skb->data;
206         if (!clip_vcc->encap || skb->len < RFC1483LLC_LEN || memcmp(skb->data,
207             llc_oui,sizeof(llc_oui))) skb->protocol = htons(ETH_P_IP);
208         else {
209                 skb->protocol = ((u16 *) skb->data)[3];
210                 skb_pull(skb,RFC1483LLC_LEN);
211                 if (skb->protocol == htons(ETH_P_ARP)) {
212                         PRIV(skb->dev)->stats.rx_packets++;
213                         PRIV(skb->dev)->stats.rx_bytes += skb->len;
214                         clip_arp_rcv(skb);
215                         return;
216                 }
217         }
218         clip_vcc->last_use = jiffies;
219         PRIV(skb->dev)->stats.rx_packets++;
220         PRIV(skb->dev)->stats.rx_bytes += skb->len;
221         netif_rx(skb);
222 }
223
224
225 /*
226  * Note: these spinlocks _must_not_ block on non-SMP. The only goal is that
227  * clip_pop is atomic with respect to the critical section in clip_start_xmit.
228  */
229
230
231 static void clip_pop(struct atm_vcc *vcc,struct sk_buff *skb)
232 {
233         struct clip_vcc *clip_vcc = CLIP_VCC(vcc);
234         struct net_device *dev = skb->dev;
235         int old;
236         unsigned long flags;
237
238         DPRINTK("clip_pop(vcc %p)\n",vcc);
239         clip_vcc->old_pop(vcc,skb);
240         /* skb->dev == NULL in outbound ARP packets */
241         if (!dev) return;
242         spin_lock_irqsave(&PRIV(dev)->xoff_lock,flags);
243         if (atm_may_send(vcc,0)) {
244                 old = xchg(&clip_vcc->xoff,0);
245                 if (old) netif_wake_queue(dev);
246         }
247         spin_unlock_irqrestore(&PRIV(dev)->xoff_lock,flags);
248 }
249
250
251 static void clip_neigh_destroy(struct neighbour *neigh)
252 {
253         DPRINTK("clip_neigh_destroy (neigh %p)\n",neigh);
254         if (NEIGH2ENTRY(neigh)->vccs)
255                 printk(KERN_CRIT "clip_neigh_destroy: vccs != NULL !!!\n");
256         NEIGH2ENTRY(neigh)->vccs = (void *) 0xdeadbeef;
257 }
258
259
260 static void clip_neigh_solicit(struct neighbour *neigh,struct sk_buff *skb)
261 {
262         DPRINTK("clip_neigh_solicit (neigh %p, skb %p)\n",neigh,skb);
263         to_atmarpd(act_need,PRIV(neigh->dev)->number,NEIGH2ENTRY(neigh)->ip);
264 }
265
266
267 static void clip_neigh_error(struct neighbour *neigh,struct sk_buff *skb)
268 {
269 #ifndef CONFIG_ATM_CLIP_NO_ICMP
270         icmp_send(skb,ICMP_DEST_UNREACH,ICMP_HOST_UNREACH,0);
271 #endif
272         kfree_skb(skb);
273 }
274
275
276 static struct neigh_ops clip_neigh_ops = {
277         family:                 AF_INET,
278         destructor:             clip_neigh_destroy,
279         solicit:                clip_neigh_solicit,
280         error_report:           clip_neigh_error,
281         output:                 dev_queue_xmit,
282         connected_output:       dev_queue_xmit,
283         hh_output:              dev_queue_xmit,
284         queue_xmit:             dev_queue_xmit,
285 };
286
287
288 static int clip_constructor(struct neighbour *neigh)
289 {
290         struct atmarp_entry *entry = NEIGH2ENTRY(neigh);
291         struct net_device *dev = neigh->dev;
292         struct in_device *in_dev = dev->ip_ptr;
293
294         DPRINTK("clip_constructor (neigh %p, entry %p)\n",neigh,entry);
295         if (!in_dev) return -EINVAL;
296         neigh->type = inet_addr_type(entry->ip);
297         if (neigh->type != RTN_UNICAST) return -EINVAL;
298         if (in_dev->arp_parms) neigh->parms = in_dev->arp_parms;
299         neigh->ops = &clip_neigh_ops;
300         neigh->output = neigh->nud_state & NUD_VALID ?
301             neigh->ops->connected_output : neigh->ops->output;
302         entry->neigh = neigh;
303         entry->vccs = NULL;
304         entry->expires = jiffies-1;
305         return 0;
306 }
307
308 static u32 clip_hash(const void *pkey, const struct net_device *dev)
309 {
310         u32 hash_val;
311
312         hash_val = *(u32*)pkey;
313         hash_val ^= (hash_val>>16);
314         hash_val ^= hash_val>>8;
315         hash_val ^= hash_val>>3;
316         hash_val = (hash_val^dev->ifindex)&NEIGH_HASHMASK;
317
318         return hash_val;
319 }
320
321
322 struct neigh_table clip_tbl = {
323         NULL,                   /* next */
324         AF_INET,                /* family */
325         sizeof(struct neighbour)+sizeof(struct atmarp_entry), /* entry_size */
326         4,                      /* key_len */
327         clip_hash,
328         clip_constructor,       /* constructor */
329         NULL,                   /* pconstructor */
330         NULL,                   /* pdestructor */
331         NULL,                   /* proxy_redo */
332         "clip_arp_cache",
333         {                       /* neigh_parms */
334                 NULL,           /* next */
335                 NULL,           /* neigh_setup */
336                 &clip_tbl,      /* tbl */
337                 0,              /* entries */
338                 NULL,           /* priv */
339                 NULL,           /* sysctl_table */
340                 30*HZ,          /* base_reachable_time */
341                 1*HZ,           /* retrans_time */
342                 60*HZ,          /* gc_staletime */
343                 30*HZ,          /* reachable_time */
344                 5*HZ,           /* delay_probe_time */
345                 3,              /* queue_len */
346                 3,              /* ucast_probes */
347                 0,              /* app_probes */
348                 3,              /* mcast_probes */
349                 1*HZ,           /* anycast_delay */
350                 (8*HZ)/10,      /* proxy_delay */
351                 1*HZ,           /* proxy_qlen */
352                 64              /* locktime */
353         },
354         30*HZ,128,512,1024      /* copied from ARP ... */
355 };
356
357
358 /* @@@ copy bh locking from arp.c -- need to bh-enable atm code before */
359
360 /*
361  * We play with the resolve flag: 0 and 1 have the usual meaning, but -1 means
362  * to allocate the neighbour entry but not to ask atmarpd for resolution. Also,
363  * don't increment the usage count. This is used to create entries in
364  * clip_setentry.
365  */
366
367
368 int clip_encap(struct atm_vcc *vcc,int mode)
369 {
370         CLIP_VCC(vcc)->encap = mode;
371         return 0;
372 }
373
374
375 static int clip_start_xmit(struct sk_buff *skb,struct net_device *dev)
376 {
377         struct clip_priv *clip_priv = PRIV(dev);
378         struct atmarp_entry *entry;
379         struct atm_vcc *vcc;
380         int old;
381         unsigned long flags;
382
383         DPRINTK("clip_start_xmit (skb %p)\n",skb);
384         if (!skb->dst) {
385                 printk(KERN_ERR "clip_start_xmit: skb->dst == NULL\n");
386                 dev_kfree_skb(skb);
387                 clip_priv->stats.tx_dropped++;
388                 return 0;
389         }
390         if (!skb->dst->neighbour) {
391 #if 0
392                 skb->dst->neighbour = clip_find_neighbour(skb->dst,1);
393                 if (!skb->dst->neighbour) {
394                         dev_kfree_skb(skb); /* lost that one */
395                         clip_priv->stats.tx_dropped++;
396                         return 0;
397                 }
398 #endif
399                 printk(KERN_ERR "clip_start_xmit: NO NEIGHBOUR !\n");
400                 dev_kfree_skb(skb);
401                 clip_priv->stats.tx_dropped++;
402                 return 0;
403         }
404         entry = NEIGH2ENTRY(skb->dst->neighbour);
405         if (!entry->vccs) {
406                 if (time_after(jiffies, entry->expires)) {
407                         /* should be resolved */
408                         entry->expires = jiffies+ATMARP_RETRY_DELAY*HZ;
409                         to_atmarpd(act_need,PRIV(dev)->number,entry->ip);
410                 }
411                 if (entry->neigh->arp_queue.qlen < ATMARP_MAX_UNRES_PACKETS)
412                         skb_queue_tail(&entry->neigh->arp_queue,skb);
413                 else {
414                         dev_kfree_skb(skb);
415                         clip_priv->stats.tx_dropped++;
416                 }
417                 return 0;
418         }
419         DPRINTK("neigh %p, vccs %p\n",entry,entry->vccs);
420         ATM_SKB(skb)->vcc = vcc = entry->vccs->vcc;
421         DPRINTK("using neighbour %p, vcc %p\n",skb->dst->neighbour,vcc);
422         if (entry->vccs->encap) {
423                 void *here;
424
425                 here = skb_push(skb,RFC1483LLC_LEN);
426                 memcpy(here,llc_oui,sizeof(llc_oui));
427                 ((u16 *) here)[3] = skb->protocol;
428         }
429         atomic_add(skb->truesize,&vcc->tx_inuse);
430         ATM_SKB(skb)->iovcnt = 0;
431         ATM_SKB(skb)->atm_options = vcc->atm_options;
432         entry->vccs->last_use = jiffies;
433         DPRINTK("atm_skb(%p)->vcc(%p)->dev(%p)\n",skb,vcc,vcc->dev);
434         old = xchg(&entry->vccs->xoff,1); /* assume XOFF ... */
435         if (old) {
436                 printk(KERN_WARNING "clip_start_xmit: XOFF->XOFF transition\n");
437                 return 0;
438         }
439         clip_priv->stats.tx_packets++;
440         clip_priv->stats.tx_bytes += skb->len;
441         (void) vcc->send(vcc,skb);
442         if (atm_may_send(vcc,0)) {
443                 entry->vccs->xoff = 0;
444                 return 0;
445         }
446         spin_lock_irqsave(&clip_priv->xoff_lock,flags);
447         netif_stop_queue(dev); /* XOFF -> throttle immediately */
448         barrier();
449         if (!entry->vccs->xoff)
450                 netif_start_queue(dev);
451                 /* Oh, we just raced with clip_pop. netif_start_queue should be
452                    good enough, because nothing should really be asleep because
453                    of the brief netif_stop_queue. If this isn't true or if it
454                    changes, use netif_wake_queue instead. */
455         spin_unlock_irqrestore(&clip_priv->xoff_lock,flags);
456         return 0;
457 }
458
459
460 static struct net_device_stats *clip_get_stats(struct net_device *dev)
461 {
462         return &PRIV(dev)->stats;
463 }
464
465
466 int clip_mkip(struct atm_vcc *vcc,int timeout)
467 {
468         struct clip_vcc *clip_vcc;
469         struct sk_buff_head copy;
470         struct sk_buff *skb;
471
472         if (!vcc->push) return -EBADFD;
473         clip_vcc = kmalloc(sizeof(struct clip_vcc),GFP_KERNEL);
474         if (!clip_vcc) return -ENOMEM;
475         DPRINTK("mkip clip_vcc %p vcc %p\n",clip_vcc,vcc);
476         clip_vcc->vcc = vcc;
477         vcc->user_back = clip_vcc;
478         clip_vcc->entry = NULL;
479         clip_vcc->xoff = 0;
480         clip_vcc->encap = 1;
481         clip_vcc->last_use = jiffies;
482         clip_vcc->idle_timeout = timeout*HZ;
483         clip_vcc->old_push = vcc->push;
484         clip_vcc->old_pop = vcc->pop;
485         vcc->push = clip_push;
486         vcc->pop = clip_pop;
487         skb_queue_head_init(&copy);
488         skb_migrate(&vcc->recvq,&copy);
489         /* re-process everything received between connection setup and MKIP */
490         while ((skb = skb_dequeue(&copy)))
491                 if (!clip_devs) {
492                         atm_return(vcc,skb->truesize);
493                         kfree_skb(skb);
494                 }
495                 else {
496                         unsigned int len = skb->len;
497
498                         clip_push(vcc,skb);
499                         PRIV(skb->dev)->stats.rx_packets--;
500                         PRIV(skb->dev)->stats.rx_bytes -= len;
501                 }
502         return 0;
503 }
504
505
506 int clip_setentry(struct atm_vcc *vcc,u32 ip)
507 {
508         struct neighbour *neigh;
509         struct atmarp_entry *entry;
510         int error;
511         struct clip_vcc *clip_vcc;
512         struct rtable *rt;
513
514         if (vcc->push != clip_push) {
515                 printk(KERN_WARNING "clip_setentry: non-CLIP VCC\n");
516                 return -EBADF;
517         }
518         clip_vcc = CLIP_VCC(vcc);
519         if (!ip) {
520                 if (!clip_vcc->entry) {
521                         printk(KERN_ERR "hiding hidden ATMARP entry\n");
522                         return 0;
523                 }
524                 DPRINTK("setentry: remove\n");
525                 unlink_clip_vcc(clip_vcc);
526                 return 0;
527         }
528         error = ip_route_output(&rt,ip,0,1,0);
529         if (error) return error;
530         neigh = __neigh_lookup(&clip_tbl,&ip,rt->u.dst.dev,1);
531         ip_rt_put(rt);
532         if (!neigh)
533                 return -ENOMEM;
534         entry = NEIGH2ENTRY(neigh);
535         if (entry != clip_vcc->entry) {
536                 if (!clip_vcc->entry) DPRINTK("setentry: add\n");
537                 else {
538                         DPRINTK("setentry: update\n");
539                         unlink_clip_vcc(clip_vcc);
540                 }
541                 link_vcc(clip_vcc,entry);
542         }
543         error = neigh_update(neigh,llc_oui,NUD_PERMANENT,1,0);
544         neigh_release(neigh);
545         return error;
546 }
547
548
549 static int clip_init(struct net_device *dev)
550 {
551         DPRINTK("clip_init %s\n",dev->name);
552         dev->hard_start_xmit = clip_start_xmit;
553         /* sg_xmit ... */
554         dev->hard_header = NULL;
555         dev->rebuild_header = NULL;
556         dev->set_mac_address = NULL;
557         dev->hard_header_parse = NULL;
558         dev->hard_header_cache = NULL;
559         dev->header_cache_update = NULL;
560         dev->change_mtu = NULL;
561         dev->do_ioctl = NULL;
562         dev->get_stats = clip_get_stats;
563         dev->type = ARPHRD_ATM;
564         dev->hard_header_len = RFC1483LLC_LEN;
565         dev->mtu = RFC1626_MTU;
566         dev->addr_len = 0;
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         dev->flags = 0;
574         return 0;
575 }
576
577
578 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 = kmalloc(sizeof(struct net_device)+sizeof(struct clip_priv),
595             GFP_KERNEL); 
596         if (!dev) return -ENOMEM;
597         memset(dev,0,sizeof(struct net_device)+sizeof(struct clip_priv));
598         clip_priv = PRIV(dev);
599         sprintf(dev->name,"atm%d",number);
600         dev->init = clip_init;
601         spin_lock_init(&clip_priv->xoff_lock);
602         clip_priv->number = number;
603         error = register_netdev(dev);
604         if (error) {
605                 kfree(dev);
606                 return error;
607         }
608         clip_priv->next = clip_devs;
609         clip_devs = dev;
610         DPRINTK("registered (net:%s)\n",dev->name);
611         return number;
612 }
613
614
615 static int clip_device_event(struct notifier_block *this,unsigned long event,
616     void *dev)
617 {
618         /* ignore non-CLIP devices */
619         if (((struct net_device *) dev)->type != ARPHRD_ATM ||
620             ((struct net_device *) dev)->init != clip_init)
621                 return NOTIFY_DONE;
622         switch (event) {
623                 case NETDEV_UP:
624                         DPRINTK("clip_device_event NETDEV_UP\n");
625                         (void) to_atmarpd(act_up,PRIV(dev)->number,0);
626                         break;
627                 case NETDEV_GOING_DOWN:
628                         DPRINTK("clip_device_event NETDEV_DOWN\n");
629                         (void) to_atmarpd(act_down,PRIV(dev)->number,0);
630                         break;
631                 case NETDEV_CHANGE:
632                 case NETDEV_CHANGEMTU:
633                         DPRINTK("clip_device_event NETDEV_CHANGE*\n");
634                         (void) to_atmarpd(act_change,PRIV(dev)->number,0);
635                         break;
636                 case NETDEV_REBOOT:
637                 case NETDEV_REGISTER:
638                 case NETDEV_DOWN:
639                         DPRINTK("clip_device_event %ld\n",event);
640                         /* ignore */
641                         break;
642                 default:
643                         printk(KERN_WARNING "clip_device_event: unknown event "
644                             "%ld\n",event);
645                         break;
646         }
647         return NOTIFY_DONE;
648 }
649
650
651 static int clip_inet_event(struct notifier_block *this,unsigned long event,
652     void *ifa)
653 {
654         struct in_device *in_dev;
655
656         in_dev = ((struct in_ifaddr *) ifa)->ifa_dev;
657         if (!in_dev || !in_dev->dev) {
658                 printk(KERN_WARNING "clip_inet_event: no device\n");
659                 return NOTIFY_DONE;
660         }
661         /*
662          * Transitions are of the down-change-up type, so it's sufficient to
663          * handle the change on up.
664          */
665         if (event != NETDEV_UP) return NOTIFY_DONE;
666         return clip_device_event(this,NETDEV_CHANGE,in_dev->dev);
667 }
668
669
670 static struct notifier_block clip_dev_notifier = {
671         clip_device_event,
672         NULL,
673         0
674 };
675
676
677
678 static struct notifier_block clip_inet_notifier = {
679         clip_inet_event,
680         NULL,
681         0
682 };
683
684
685
686 static void atmarpd_close(struct atm_vcc *vcc)
687 {
688         DPRINTK("atmarpd_close\n");
689         atmarpd = NULL; /* assumed to be atomic */
690         barrier();
691         unregister_inetaddr_notifier(&clip_inet_notifier);
692         unregister_netdevice_notifier(&clip_dev_notifier);
693         if (skb_peek(&vcc->recvq))
694                 printk(KERN_ERR "atmarpd_close: closing with requests "
695                     "pending\n");
696         skb_queue_purge(&vcc->recvq);
697         DPRINTK("(done)\n");
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         &atmarpd_dev_ops,
708         NULL,           /* no PHY */
709         "arpd",         /* type */
710         999,            /* dummy device number */
711         NULL,NULL,      /* pretend not to have any VCCs */
712         NULL,NULL,      /* no data */
713         { 0 },          /* no flags */
714         NULL,           /* no local address */
715         { 0 }           /* no ESI, no statistics */
716 };
717
718
719 int atm_init_atmarp(struct atm_vcc *vcc)
720 {
721         struct net_device *dev;
722
723         if (atmarpd) return -EADDRINUSE;
724         if (start_timer) {
725                 start_timer = 0;
726                 init_timer(&idle_timer);
727                 idle_timer.expires = jiffies+CLIP_CHECK_INTERVAL*HZ;
728                 idle_timer.function = idle_timer_check;
729                 add_timer(&idle_timer);
730         }
731         atmarpd = vcc;
732         set_bit(ATM_VF_META,&vcc->flags);
733         set_bit(ATM_VF_READY,&vcc->flags);
734             /* allow replies and avoid getting closed if signaling dies */
735         bind_vcc(vcc,&atmarpd_dev);
736         vcc->push = NULL;
737         vcc->pop = NULL; /* crash */
738         vcc->push_oam = NULL; /* crash */
739         if (register_netdevice_notifier(&clip_dev_notifier))
740                 printk(KERN_ERR "register_netdevice_notifier failed\n");
741         if (register_inetaddr_notifier(&clip_inet_notifier))
742                 printk(KERN_ERR "register_inetaddr_notifier failed\n");
743         for (dev = clip_devs; dev; dev = PRIV(dev)->next)
744                 if (dev->flags & IFF_UP)
745                         (void) to_atmarpd(act_up,PRIV(dev)->number,0);
746         return 0;
747 }
748
749
750 void atm_clip_init(void)
751 {
752         clip_tbl.lock = RW_LOCK_UNLOCKED;
753         clip_tbl.kmem_cachep = kmem_cache_create(clip_tbl.id,
754             clip_tbl.entry_size, 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
755 }