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