blob: 5f8a1d222720c61ecf8b04ae077834e638a04f8d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* net/atm/clip.c - RFC1577 Classical IP over ATM */
2
3/* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
4
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#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>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080020#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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>
Randy Dunlap4bdbf6c2006-07-03 19:47:27 -070026#include <linux/poison.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <net/atmclip.h>
42
43
44#if 0
45#define DPRINTK(format,args...) printk(format,##args)
46#else
47#define DPRINTK(format,args...)
48#endif
49
50
51static struct net_device *clip_devs;
52static struct atm_vcc *atmarpd;
53static struct neigh_table clip_tbl;
54static struct timer_list idle_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Al Viro30d492d2006-11-14 21:11:29 -080056static int to_atmarpd(enum atmarp_ctrl_type type, int itf, __be32 ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 struct sock *sk;
59 struct atmarp_ctrl *ctrl;
60 struct sk_buff *skb;
61
Stephen Hemmingere49e76d2006-04-14 15:59:37 -070062 DPRINTK("to_atmarpd(%d)\n", type);
63 if (!atmarpd)
64 return -EUNATCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 skb = alloc_skb(sizeof(struct atmarp_ctrl),GFP_ATOMIC);
Stephen Hemmingere49e76d2006-04-14 15:59:37 -070066 if (!skb)
67 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 ctrl = (struct atmarp_ctrl *) skb_put(skb,sizeof(struct atmarp_ctrl));
69 ctrl->type = type;
70 ctrl->itf_num = itf;
71 ctrl->ip = ip;
Stephen Hemmingere49e76d2006-04-14 15:59:37 -070072 atm_force_charge(atmarpd, skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 sk = sk_atm(atmarpd);
75 skb_queue_tail(&sk->sk_receive_queue, skb);
76 sk->sk_data_ready(sk, skb->len);
77 return 0;
78}
79
Stephen Hemmingere49e76d2006-04-14 15:59:37 -070080static void link_vcc(struct clip_vcc *clip_vcc, struct atmarp_entry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Stephen Hemmingere49e76d2006-04-14 15:59:37 -070082 DPRINTK("link_vcc %p to entry %p (neigh %p)\n", clip_vcc, entry,
83 entry->neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 clip_vcc->entry = entry;
Stephen Hemmingere49e76d2006-04-14 15:59:37 -070085 clip_vcc->xoff = 0; /* @@@ may overrun buffer by one packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 clip_vcc->next = entry->vccs;
87 entry->vccs = clip_vcc;
88 entry->neigh->used = jiffies;
89}
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static 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) {
Stephen Hemmingere49e76d2006-04-14 15:59:37 -070097 printk(KERN_CRIT "!clip_vcc->entry (clip_vcc %p)\n", clip_vcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return;
99 }
Herbert Xu932ff272006-06-09 12:20:56 -0700100 netif_tx_lock_bh(entry->neigh->dev); /* block clip_start_xmit() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 entry->neigh->used = jiffies;
102 for (walk = &entry->vccs; *walk; walk = &(*walk)->next)
103 if (*walk == clip_vcc) {
104 int error;
105
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700106 *walk = clip_vcc->next; /* atomic */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 clip_vcc->entry = NULL;
108 if (clip_vcc->xoff)
109 netif_wake_queue(entry->neigh->dev);
110 if (entry->vccs)
111 goto out;
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700112 entry->expires = jiffies - 1;
113 /* force resolution or expiration */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 error = neigh_update(entry->neigh, NULL, NUD_NONE,
115 NEIGH_UPDATE_F_ADMIN);
116 if (error)
117 printk(KERN_CRIT "unlink_clip_vcc: "
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700118 "neigh_update failed with %d\n", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 goto out;
120 }
121 printk(KERN_CRIT "ATMARP: unlink_clip_vcc failed (entry %p, vcc "
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700122 "0x%p)\n", entry, clip_vcc);
123 out:
Herbert Xu932ff272006-06-09 12:20:56 -0700124 netif_tx_unlock_bh(entry->neigh->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
127/* The neighbour entry n->lock is held. */
128static 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
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700152 while ((skb = skb_dequeue(&n->arp_queue)) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 dev_kfree_skb(skb);
154
155 return 0;
156 }
157
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700158 DPRINTK("expired neigh %p\n", n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return 1;
160}
161
162static void idle_timer_check(unsigned long dummy)
163{
164 write_lock(&clip_tbl.lock);
165 __neigh_for_each_release(&clip_tbl, neigh_check_cb);
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700166 mod_timer(&idle_timer, jiffies + CLIP_CHECK_INTERVAL * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 write_unlock(&clip_tbl.lock);
168}
169
170static 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;
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700176 if (!vcc || !atm_charge(vcc, skb->truesize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 dev_kfree_skb_any(skb);
178 return 0;
179 }
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700180 DPRINTK("pushing to %p\n", vcc);
181 DPRINTK("using %p\n", CLIP_VCC(vcc)->old_push);
182 CLIP_VCC(vcc)->old_push(vcc, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return 0;
184}
185
186static 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,
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700192 0x00
193};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700195static void clip_push(struct atm_vcc *vcc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 struct clip_vcc *clip_vcc = CLIP_VCC(vcc);
198
199 DPRINTK("clip push\n");
200 if (!skb) {
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700201 DPRINTK("removing VCC %p\n", clip_vcc);
202 if (clip_vcc->entry)
203 unlink_clip_vcc(clip_vcc);
204 clip_vcc->old_push(vcc, NULL); /* pass on the bad news */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 kfree(clip_vcc);
206 return;
207 }
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700208 atm_return(vcc, skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 skb->dev = clip_vcc->entry ? clip_vcc->entry->neigh->dev : clip_devs;
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700210 /* clip_vcc->entry == NULL if we don't have an IP address yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (!skb->dev) {
212 dev_kfree_skb_any(skb);
213 return;
214 }
215 ATM_SKB(skb)->vcc = vcc;
216 skb->mac.raw = skb->data;
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700217 if (!clip_vcc->encap
218 || skb->len < RFC1483LLC_LEN
219 || memcmp(skb->data, llc_oui, sizeof (llc_oui)))
220 skb->protocol = htons(ETH_P_IP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 else {
Al Viro30d492d2006-11-14 21:11:29 -0800222 skb->protocol = ((__be16 *) skb->data)[3];
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700223 skb_pull(skb, RFC1483LLC_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (skb->protocol == htons(ETH_P_ARP)) {
225 PRIV(skb->dev)->stats.rx_packets++;
226 PRIV(skb->dev)->stats.rx_bytes += skb->len;
227 clip_arp_rcv(skb);
228 return;
229 }
230 }
231 clip_vcc->last_use = jiffies;
232 PRIV(skb->dev)->stats.rx_packets++;
233 PRIV(skb->dev)->stats.rx_bytes += skb->len;
234 memset(ATM_SKB(skb), 0, sizeof(struct atm_skb_data));
235 netif_rx(skb);
236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238/*
239 * Note: these spinlocks _must_not_ block on non-SMP. The only goal is that
240 * clip_pop is atomic with respect to the critical section in clip_start_xmit.
241 */
242
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700243static void clip_pop(struct atm_vcc *vcc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 struct clip_vcc *clip_vcc = CLIP_VCC(vcc);
246 struct net_device *dev = skb->dev;
247 int old;
248 unsigned long flags;
249
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700250 DPRINTK("clip_pop(vcc %p)\n", vcc);
251 clip_vcc->old_pop(vcc, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 /* skb->dev == NULL in outbound ARP packets */
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700253 if (!dev)
254 return;
255 spin_lock_irqsave(&PRIV(dev)->xoff_lock, flags);
256 if (atm_may_send(vcc, 0)) {
257 old = xchg(&clip_vcc->xoff, 0);
258 if (old)
259 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700261 spin_unlock_irqrestore(&PRIV(dev)->xoff_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264static void clip_neigh_destroy(struct neighbour *neigh)
265{
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700266 DPRINTK("clip_neigh_destroy (neigh %p)\n", neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (NEIGH2ENTRY(neigh)->vccs)
268 printk(KERN_CRIT "clip_neigh_destroy: vccs != NULL !!!\n");
Randy Dunlap4bdbf6c2006-07-03 19:47:27 -0700269 NEIGH2ENTRY(neigh)->vccs = (void *) NEIGHBOR_DEAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700272static void clip_neigh_solicit(struct neighbour *neigh, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700274 DPRINTK("clip_neigh_solicit (neigh %p, skb %p)\n", neigh, skb);
275 to_atmarpd(act_need, PRIV(neigh->dev)->number, NEIGH2ENTRY(neigh)->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700278static void clip_neigh_error(struct neighbour *neigh, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280#ifndef CONFIG_ATM_CLIP_NO_ICMP
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700281 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282#endif
283 kfree_skb(skb);
284}
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286static struct neigh_ops clip_neigh_ops = {
287 .family = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 .solicit = clip_neigh_solicit,
289 .error_report = clip_neigh_error,
290 .output = dev_queue_xmit,
291 .connected_output = dev_queue_xmit,
292 .hh_output = dev_queue_xmit,
293 .queue_xmit = dev_queue_xmit,
294};
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296static int clip_constructor(struct neighbour *neigh)
297{
298 struct atmarp_entry *entry = NEIGH2ENTRY(neigh);
299 struct net_device *dev = neigh->dev;
300 struct in_device *in_dev;
301 struct neigh_parms *parms;
302
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700303 DPRINTK("clip_constructor (neigh %p, entry %p)\n", neigh, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 neigh->type = inet_addr_type(entry->ip);
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700305 if (neigh->type != RTN_UNICAST)
306 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -0700309 in_dev = __in_dev_get_rcu(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (!in_dev) {
311 rcu_read_unlock();
312 return -EINVAL;
313 }
314
315 parms = in_dev->arp_parms;
316 __neigh_parms_put(neigh->parms);
317 neigh->parms = neigh_parms_clone(parms);
318 rcu_read_unlock();
319
320 neigh->ops = &clip_neigh_ops;
321 neigh->output = neigh->nud_state & NUD_VALID ?
322 neigh->ops->connected_output : neigh->ops->output;
323 entry->neigh = neigh;
324 entry->vccs = NULL;
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700325 entry->expires = jiffies - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return 0;
327}
328
329static u32 clip_hash(const void *pkey, const struct net_device *dev)
330{
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700331 return jhash_2words(*(u32 *) pkey, dev->ifindex, clip_tbl.hash_rnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
334static struct neigh_table clip_tbl = {
335 .family = AF_INET,
336 .entry_size = sizeof(struct neighbour)+sizeof(struct atmarp_entry),
337 .key_len = 4,
338 .hash = clip_hash,
339 .constructor = clip_constructor,
340 .id = "clip_arp_cache",
341
342 /* parameters are copied from ARP ... */
343 .parms = {
344 .tbl = &clip_tbl,
David S. Milleraa837b52006-03-20 22:30:23 -0800345 .neigh_destructor = clip_neigh_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 .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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365/* @@@ copy bh locking from arp.c -- need to bh-enable atm code before */
366
367/*
368 * We play with the resolve flag: 0 and 1 have the usual meaning, but -1 means
369 * to allocate the neighbour entry but not to ask atmarpd for resolution. Also,
370 * don't increment the usage count. This is used to create entries in
371 * clip_setentry.
372 */
373
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700374static int clip_encap(struct atm_vcc *vcc, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 CLIP_VCC(vcc)->encap = mode;
377 return 0;
378}
379
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700380static int clip_start_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 struct clip_priv *clip_priv = PRIV(dev);
383 struct atmarp_entry *entry;
384 struct atm_vcc *vcc;
385 int old;
386 unsigned long flags;
387
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700388 DPRINTK("clip_start_xmit (skb %p)\n", skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (!skb->dst) {
390 printk(KERN_ERR "clip_start_xmit: skb->dst == NULL\n");
391 dev_kfree_skb(skb);
392 clip_priv->stats.tx_dropped++;
393 return 0;
394 }
395 if (!skb->dst->neighbour) {
396#if 0
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700397 skb->dst->neighbour = clip_find_neighbour(skb->dst, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (!skb->dst->neighbour) {
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700399 dev_kfree_skb(skb); /* lost that one */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 clip_priv->stats.tx_dropped++;
401 return 0;
402 }
403#endif
404 printk(KERN_ERR "clip_start_xmit: NO NEIGHBOUR !\n");
405 dev_kfree_skb(skb);
406 clip_priv->stats.tx_dropped++;
407 return 0;
408 }
409 entry = NEIGH2ENTRY(skb->dst->neighbour);
410 if (!entry->vccs) {
411 if (time_after(jiffies, entry->expires)) {
412 /* should be resolved */
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700413 entry->expires = jiffies + ATMARP_RETRY_DELAY * HZ;
414 to_atmarpd(act_need, PRIV(dev)->number, entry->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416 if (entry->neigh->arp_queue.qlen < ATMARP_MAX_UNRES_PACKETS)
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700417 skb_queue_tail(&entry->neigh->arp_queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 else {
419 dev_kfree_skb(skb);
420 clip_priv->stats.tx_dropped++;
421 }
422 return 0;
423 }
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700424 DPRINTK("neigh %p, vccs %p\n", entry, entry->vccs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 ATM_SKB(skb)->vcc = vcc = entry->vccs->vcc;
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700426 DPRINTK("using neighbour %p, vcc %p\n", skb->dst->neighbour, vcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (entry->vccs->encap) {
428 void *here;
429
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700430 here = skb_push(skb, RFC1483LLC_LEN);
431 memcpy(here, llc_oui, sizeof(llc_oui));
Al Viro30d492d2006-11-14 21:11:29 -0800432 ((__be16 *) here)[3] = skb->protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434 atomic_add(skb->truesize, &sk_atm(vcc)->sk_wmem_alloc);
435 ATM_SKB(skb)->atm_options = vcc->atm_options;
436 entry->vccs->last_use = jiffies;
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700437 DPRINTK("atm_skb(%p)->vcc(%p)->dev(%p)\n", skb, vcc, vcc->dev);
438 old = xchg(&entry->vccs->xoff, 1); /* assume XOFF ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (old) {
440 printk(KERN_WARNING "clip_start_xmit: XOFF->XOFF transition\n");
441 return 0;
442 }
443 clip_priv->stats.tx_packets++;
444 clip_priv->stats.tx_bytes += skb->len;
Stephen Hemminger5ff765f2006-04-14 16:00:59 -0700445 vcc->send(vcc, skb);
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700446 if (atm_may_send(vcc, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 entry->vccs->xoff = 0;
448 return 0;
449 }
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700450 spin_lock_irqsave(&clip_priv->xoff_lock, flags);
451 netif_stop_queue(dev); /* XOFF -> throttle immediately */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 barrier();
453 if (!entry->vccs->xoff)
454 netif_start_queue(dev);
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700455 /* Oh, we just raced with clip_pop. netif_start_queue should be
456 good enough, because nothing should really be asleep because
457 of the brief netif_stop_queue. If this isn't true or if it
458 changes, use netif_wake_queue instead. */
459 spin_unlock_irqrestore(&clip_priv->xoff_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return 0;
461}
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463static struct net_device_stats *clip_get_stats(struct net_device *dev)
464{
465 return &PRIV(dev)->stats;
466}
467
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700468static int clip_mkip(struct atm_vcc *vcc, int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 struct clip_vcc *clip_vcc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 struct sk_buff *skb;
David S. Millerc40a27f2006-11-30 21:05:23 -0800472 struct sk_buff_head *rq;
473 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700475 if (!vcc->push)
476 return -EBADFD;
477 clip_vcc = kmalloc(sizeof(struct clip_vcc), GFP_KERNEL);
478 if (!clip_vcc)
479 return -ENOMEM;
480 DPRINTK("mkip clip_vcc %p vcc %p\n", clip_vcc, vcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 clip_vcc->vcc = vcc;
482 vcc->user_back = clip_vcc;
483 set_bit(ATM_VF_IS_CLIP, &vcc->flags);
484 clip_vcc->entry = NULL;
485 clip_vcc->xoff = 0;
486 clip_vcc->encap = 1;
487 clip_vcc->last_use = jiffies;
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700488 clip_vcc->idle_timeout = timeout * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 clip_vcc->old_push = vcc->push;
490 clip_vcc->old_pop = vcc->pop;
491 vcc->push = clip_push;
492 vcc->pop = clip_pop;
David S. Millerc40a27f2006-11-30 21:05:23 -0800493
494 rq = &sk_atm(vcc)->sk_receive_queue;
495
496 spin_lock_irqsave(&rq->lock, flags);
497 if (skb_queue_empty(rq)) {
498 skb = NULL;
499 } else {
500 /* NULL terminate the list. */
501 rq->prev->next = NULL;
502 skb = rq->next;
503 }
504 rq->prev = rq->next = (struct sk_buff *)rq;
505 rq->qlen = 0;
506 spin_unlock_irqrestore(&rq->lock, flags);
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 /* re-process everything received between connection setup and MKIP */
David S. Millerc40a27f2006-11-30 21:05:23 -0800509 while (skb) {
510 struct sk_buff *next = skb->next;
511
512 skb->next = skb->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (!clip_devs) {
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700514 atm_return(vcc, skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 kfree_skb(skb);
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700516 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 unsigned int len = skb->len;
518
YOSHIFUJI Hideakife261092006-09-18 06:37:58 -0700519 skb_get(skb);
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700520 clip_push(vcc, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 PRIV(skb->dev)->stats.rx_packets--;
522 PRIV(skb->dev)->stats.rx_bytes -= len;
YOSHIFUJI Hideakife261092006-09-18 06:37:58 -0700523 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
David S. Millerc40a27f2006-11-30 21:05:23 -0800525
526 skb = next;
527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return 0;
529}
530
Al Viro30d492d2006-11-14 21:11:29 -0800531static int clip_setentry(struct atm_vcc *vcc, __be32 ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 struct neighbour *neigh;
534 struct atmarp_entry *entry;
535 int error;
536 struct clip_vcc *clip_vcc;
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700537 struct flowi fl = { .nl_u = { .ip4_u = { .daddr = ip, .tos = 1}} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 struct rtable *rt;
539
540 if (vcc->push != clip_push) {
541 printk(KERN_WARNING "clip_setentry: non-CLIP VCC\n");
542 return -EBADF;
543 }
544 clip_vcc = CLIP_VCC(vcc);
545 if (!ip) {
546 if (!clip_vcc->entry) {
547 printk(KERN_ERR "hiding hidden ATMARP entry\n");
548 return 0;
549 }
550 DPRINTK("setentry: remove\n");
551 unlink_clip_vcc(clip_vcc);
552 return 0;
553 }
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700554 error = ip_route_output_key(&rt, &fl);
555 if (error)
556 return error;
557 neigh = __neigh_lookup(&clip_tbl, &ip, rt->u.dst.dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 ip_rt_put(rt);
559 if (!neigh)
560 return -ENOMEM;
561 entry = NEIGH2ENTRY(neigh);
562 if (entry != clip_vcc->entry) {
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700563 if (!clip_vcc->entry)
564 DPRINTK("setentry: add\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 else {
566 DPRINTK("setentry: update\n");
567 unlink_clip_vcc(clip_vcc);
568 }
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700569 link_vcc(clip_vcc, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700571 error = neigh_update(neigh, llc_oui, NUD_PERMANENT,
572 NEIGH_UPDATE_F_OVERRIDE | NEIGH_UPDATE_F_ADMIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 neigh_release(neigh);
574 return error;
575}
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577static void clip_setup(struct net_device *dev)
578{
579 dev->hard_start_xmit = clip_start_xmit;
580 /* sg_xmit ... */
581 dev->get_stats = clip_get_stats;
582 dev->type = ARPHRD_ATM;
583 dev->hard_header_len = RFC1483LLC_LEN;
584 dev->mtu = RFC1626_MTU;
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700585 dev->tx_queue_len = 100; /* "normal" queue (packets) */
586 /* When using a "real" qdisc, the qdisc determines the queue */
587 /* length. tx_queue_len is only used for the default case, */
588 /* without any more elaborate queuing. 100 is a reasonable */
589 /* compromise between decent burst-tolerance and protection */
590 /* against memory hogs. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593static int clip_create(int number)
594{
595 struct net_device *dev;
596 struct clip_priv *clip_priv;
597 int error;
598
599 if (number != -1) {
600 for (dev = clip_devs; dev; dev = PRIV(dev)->next)
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700601 if (PRIV(dev)->number == number)
602 return -EEXIST;
603 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 number = 0;
605 for (dev = clip_devs; dev; dev = PRIV(dev)->next)
606 if (PRIV(dev)->number >= number)
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700607 number = PRIV(dev)->number + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609 dev = alloc_netdev(sizeof(struct clip_priv), "", clip_setup);
610 if (!dev)
611 return -ENOMEM;
612 clip_priv = PRIV(dev);
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700613 sprintf(dev->name, "atm%d", number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 spin_lock_init(&clip_priv->xoff_lock);
615 clip_priv->number = number;
616 error = register_netdev(dev);
617 if (error) {
618 free_netdev(dev);
619 return error;
620 }
621 clip_priv->next = clip_devs;
622 clip_devs = dev;
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700623 DPRINTK("registered (net:%s)\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return number;
625}
626
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700627static int clip_device_event(struct notifier_block *this, unsigned long event,
Stephen Hemmingerf3a05922006-04-14 15:07:27 -0700628 void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Stephen Hemmingerf3a05922006-04-14 15:07:27 -0700630 struct net_device *dev = arg;
631
632 if (event == NETDEV_UNREGISTER) {
633 neigh_ifdown(&clip_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return NOTIFY_DONE;
Stephen Hemmingerf3a05922006-04-14 15:07:27 -0700635 }
636
637 /* ignore non-CLIP devices */
638 if (dev->type != ARPHRD_ATM || dev->hard_start_xmit != clip_start_xmit)
639 return NOTIFY_DONE;
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 switch (event) {
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700642 case NETDEV_UP:
643 DPRINTK("clip_device_event NETDEV_UP\n");
Stephen Hemminger5ff765f2006-04-14 16:00:59 -0700644 to_atmarpd(act_up, PRIV(dev)->number, 0);
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700645 break;
646 case NETDEV_GOING_DOWN:
647 DPRINTK("clip_device_event NETDEV_DOWN\n");
Stephen Hemminger5ff765f2006-04-14 16:00:59 -0700648 to_atmarpd(act_down, PRIV(dev)->number, 0);
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700649 break;
650 case NETDEV_CHANGE:
651 case NETDEV_CHANGEMTU:
652 DPRINTK("clip_device_event NETDEV_CHANGE*\n");
Stephen Hemminger5ff765f2006-04-14 16:00:59 -0700653 to_atmarpd(act_change, PRIV(dev)->number, 0);
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700654 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656 return NOTIFY_DONE;
657}
658
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700659static int clip_inet_event(struct notifier_block *this, unsigned long event,
660 void *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
662 struct in_device *in_dev;
663
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700664 in_dev = ((struct in_ifaddr *)ifa)->ifa_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (!in_dev || !in_dev->dev) {
666 printk(KERN_WARNING "clip_inet_event: no device\n");
667 return NOTIFY_DONE;
668 }
669 /*
670 * Transitions are of the down-change-up type, so it's sufficient to
671 * handle the change on up.
672 */
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700673 if (event != NETDEV_UP)
674 return NOTIFY_DONE;
675 return clip_device_event(this, NETDEV_CHANGE, in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
678
679static struct notifier_block clip_dev_notifier = {
Stephen Hemminger5ff765f2006-04-14 16:00:59 -0700680 .notifier_call = clip_device_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681};
682
683
684
685static struct notifier_block clip_inet_notifier = {
Stephen Hemminger5ff765f2006-04-14 16:00:59 -0700686 .notifier_call = clip_inet_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687};
688
689
690
691static void atmarpd_close(struct atm_vcc *vcc)
692{
693 DPRINTK("atmarpd_close\n");
Stephen Hemmingerf3a05922006-04-14 15:07:27 -0700694
695 rtnl_lock();
696 atmarpd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 skb_queue_purge(&sk_atm(vcc)->sk_receive_queue);
Stephen Hemmingerf3a05922006-04-14 15:07:27 -0700698 rtnl_unlock();
699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 DPRINTK("(done)\n");
701 module_put(THIS_MODULE);
702}
703
704
705static struct atmdev_ops atmarpd_dev_ops = {
706 .close = atmarpd_close
707};
708
709
710static struct atm_dev atmarpd_dev = {
711 .ops = &atmarpd_dev_ops,
712 .type = "arpd",
713 .number = 999,
714 .lock = SPIN_LOCK_UNLOCKED
715};
716
717
718static int atm_init_atmarp(struct atm_vcc *vcc)
719{
Stephen Hemmingerf3a05922006-04-14 15:07:27 -0700720 rtnl_lock();
721 if (atmarpd) {
722 rtnl_unlock();
723 return -EADDRINUSE;
724 }
725
Stephen Hemminger2d907392006-04-14 15:56:02 -0700726 mod_timer(&idle_timer, jiffies+CLIP_CHECK_INTERVAL*HZ);
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 atmarpd = vcc;
729 set_bit(ATM_VF_META,&vcc->flags);
730 set_bit(ATM_VF_READY,&vcc->flags);
731 /* allow replies and avoid getting closed if signaling dies */
732 vcc->dev = &atmarpd_dev;
733 vcc_insert_socket(sk_atm(vcc));
734 vcc->push = NULL;
735 vcc->pop = NULL; /* crash */
736 vcc->push_oam = NULL; /* crash */
Stephen Hemmingerf3a05922006-04-14 15:07:27 -0700737 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return 0;
739}
740
741static int clip_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
742{
743 struct atm_vcc *vcc = ATM_SD(sock);
744 int err = 0;
745
746 switch (cmd) {
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700747 case SIOCMKCLIP:
748 case ATMARPD_CTRL:
749 case ATMARP_MKIP:
750 case ATMARP_SETENTRY:
751 case ATMARP_ENCAP:
752 if (!capable(CAP_NET_ADMIN))
753 return -EPERM;
754 break;
755 default:
756 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758
759 switch (cmd) {
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700760 case SIOCMKCLIP:
761 err = clip_create(arg);
762 break;
763 case ATMARPD_CTRL:
764 err = atm_init_atmarp(vcc);
765 if (!err) {
766 sock->state = SS_CONNECTED;
767 __module_get(THIS_MODULE);
768 }
769 break;
770 case ATMARP_MKIP:
771 err = clip_mkip(vcc, arg);
772 break;
773 case ATMARP_SETENTRY:
Al Viro30d492d2006-11-14 21:11:29 -0800774 err = clip_setentry(vcc, (__force __be32)arg);
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700775 break;
776 case ATMARP_ENCAP:
777 err = clip_encap(vcc, arg);
778 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780 return err;
781}
782
783static struct atm_ioctl clip_ioctl_ops = {
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700784 .owner = THIS_MODULE,
785 .ioctl = clip_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786};
787
788#ifdef CONFIG_PROC_FS
789
790static void svc_addr(struct seq_file *seq, struct sockaddr_atmsvc *addr)
791{
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700792 static int code[] = { 1, 2, 10, 6, 1, 0 };
793 static int e164[] = { 1, 8, 4, 6, 1, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 if (*addr->sas_addr.pub) {
796 seq_printf(seq, "%s", addr->sas_addr.pub);
797 if (*addr->sas_addr.prv)
798 seq_putc(seq, '+');
799 } else if (!*addr->sas_addr.prv) {
800 seq_printf(seq, "%s", "(none)");
801 return;
802 }
803 if (*addr->sas_addr.prv) {
804 unsigned char *prv = addr->sas_addr.prv;
805 int *fields;
806 int i, j;
807
808 fields = *prv == ATM_AFI_E164 ? e164 : code;
809 for (i = 0; fields[i]; i++) {
810 for (j = fields[i]; j; j--)
811 seq_printf(seq, "%02X", *prv++);
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700812 if (fields[i + 1])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 seq_putc(seq, '.');
814 }
815 }
816}
817
818/* This means the neighbour entry has no attached VCC objects. */
819#define SEQ_NO_VCC_TOKEN ((void *) 2)
820
821static void atmarp_info(struct seq_file *seq, struct net_device *dev,
822 struct atmarp_entry *entry, struct clip_vcc *clip_vcc)
823{
824 unsigned long exp;
825 char buf[17];
826 int svc, llc, off;
827
828 svc = ((clip_vcc == SEQ_NO_VCC_TOKEN) ||
829 (sk_atm(clip_vcc->vcc)->sk_family == AF_ATMSVC));
830
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700831 llc = ((clip_vcc == SEQ_NO_VCC_TOKEN) || clip_vcc->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 if (clip_vcc == SEQ_NO_VCC_TOKEN)
834 exp = entry->neigh->used;
835 else
836 exp = clip_vcc->last_use;
837
838 exp = (jiffies - exp) / HZ;
839
840 seq_printf(seq, "%-6s%-4s%-4s%5ld ",
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700841 dev->name, svc ? "SVC" : "PVC", llc ? "LLC" : "NULL", exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
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,
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700859 clip_vcc->vcc->vpi, clip_vcc->vcc->vci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 } else {
861 svc_addr(seq, &clip_vcc->vcc->remote);
862 seq_putc(seq, '\n');
863 }
864}
865
866struct clip_seq_state {
867 /* This member must be first. */
868 struct neigh_seq_state ns;
869
870 /* Local to clip specific iteration. */
871 struct clip_vcc *vcc;
872};
873
874static struct clip_vcc *clip_seq_next_vcc(struct atmarp_entry *e,
875 struct clip_vcc *curr)
876{
877 if (!curr) {
878 curr = e->vccs;
879 if (!curr)
880 return SEQ_NO_VCC_TOKEN;
881 return curr;
882 }
883 if (curr == SEQ_NO_VCC_TOKEN)
884 return NULL;
885
886 curr = curr->next;
887
888 return curr;
889}
890
891static void *clip_seq_vcc_walk(struct clip_seq_state *state,
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700892 struct atmarp_entry *e, loff_t * pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894 struct clip_vcc *vcc = state->vcc;
895
896 vcc = clip_seq_next_vcc(e, vcc);
897 if (vcc && pos != NULL) {
898 while (*pos) {
899 vcc = clip_seq_next_vcc(e, vcc);
900 if (!vcc)
901 break;
902 --(*pos);
903 }
904 }
905 state->vcc = vcc;
906
907 return vcc;
908}
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910static void *clip_seq_sub_iter(struct neigh_seq_state *_state,
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700911 struct neighbour *n, loff_t * pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700913 struct clip_seq_state *state = (struct clip_seq_state *)_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 return clip_seq_vcc_walk(state, NEIGH2ENTRY(n), pos);
916}
917
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700918static void *clip_seq_start(struct seq_file *seq, loff_t * pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
920 return neigh_seq_start(seq, pos, &clip_tbl, NEIGH_SEQ_NEIGH_ONLY);
921}
922
923static int clip_seq_show(struct seq_file *seq, void *v)
924{
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700925 static char atm_arp_banner[] =
926 "IPitf TypeEncp Idle IP address ATM address\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 if (v == SEQ_START_TOKEN) {
929 seq_puts(seq, atm_arp_banner);
930 } else {
931 struct clip_seq_state *state = seq->private;
932 struct neighbour *n = v;
933 struct clip_vcc *vcc = state->vcc;
934
935 atmarp_info(seq, n->dev, NEIGH2ENTRY(n), vcc);
936 }
Stephen Hemmingere49e76d2006-04-14 15:59:37 -0700937 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
940static struct seq_operations arp_seq_ops = {
941 .start = clip_seq_start,
942 .next = neigh_seq_next,
943 .stop = neigh_seq_stop,
944 .show = clip_seq_show,
945};
946
947static int arp_seq_open(struct inode *inode, struct file *file)
948{
949 struct clip_seq_state *state;
950 struct seq_file *seq;
951 int rc = -EAGAIN;
952
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700953 state = kzalloc(sizeof(*state), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if (!state) {
955 rc = -ENOMEM;
956 goto out_kfree;
957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 state->ns.neigh_sub_iter = clip_seq_sub_iter;
959
960 rc = seq_open(file, &arp_seq_ops);
961 if (rc)
962 goto out_kfree;
963
964 seq = file->private_data;
965 seq->private = state;
966out:
967 return rc;
968
969out_kfree:
970 kfree(state);
971 goto out;
972}
973
974static struct file_operations arp_seq_fops = {
975 .open = arp_seq_open,
976 .read = seq_read,
977 .llseek = seq_lseek,
978 .release = seq_release_private,
979 .owner = THIS_MODULE
980};
981#endif
982
983static int __init atm_clip_init(void)
984{
Simon Kelleybd89efc2006-05-12 14:56:08 -0700985 neigh_table_init_no_netlink(&clip_tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 clip_tbl_hook = &clip_tbl;
988 register_atm_ioctl(&clip_ioctl_ops);
Stephen Hemmingerf3a05922006-04-14 15:07:27 -0700989 register_netdevice_notifier(&clip_dev_notifier);
990 register_inetaddr_notifier(&clip_inet_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Stephen Hemminger2d907392006-04-14 15:56:02 -0700992 setup_timer(&idle_timer, idle_timer_check, 0);
993
Adrian Bunk24781732006-07-09 12:13:18 -0700994#ifdef CONFIG_PROC_FS
995 {
996 struct proc_dir_entry *p;
997
998 p = create_proc_entry("arp", S_IRUGO, atm_proc_root);
999 if (p)
1000 p->proc_fops = &arp_seq_fops;
1001 }
1002#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 return 0;
1005}
1006
1007static void __exit atm_clip_exit(void)
1008{
1009 struct net_device *dev, *next;
1010
1011 remove_proc_entry("arp", atm_proc_root);
1012
Stephen Hemmingerf3a05922006-04-14 15:07:27 -07001013 unregister_inetaddr_notifier(&clip_inet_notifier);
1014 unregister_netdevice_notifier(&clip_dev_notifier);
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 deregister_atm_ioctl(&clip_ioctl_ops);
1017
1018 /* First, stop the idle timer, so it stops banging
1019 * on the table.
1020 */
Stephen Hemminger2d907392006-04-14 15:56:02 -07001021 del_timer_sync(&idle_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 /* Next, purge the table, so that the device
1024 * unregister loop below does not hang due to
1025 * device references remaining in the table.
1026 */
1027 neigh_ifdown(&clip_tbl, NULL);
1028
1029 dev = clip_devs;
1030 while (dev) {
1031 next = PRIV(dev)->next;
1032 unregister_netdev(dev);
1033 free_netdev(dev);
1034 dev = next;
1035 }
1036
1037 /* Now it is safe to fully shutdown whole table. */
1038 neigh_table_clear(&clip_tbl);
1039
1040 clip_tbl_hook = NULL;
1041}
1042
1043module_init(atm_clip_init);
1044module_exit(atm_clip_exit);
Stephen Hemminger4909e4882006-04-14 16:01:26 -07001045MODULE_AUTHOR("Werner Almesberger");
1046MODULE_DESCRIPTION("Classical/IP over ATM interface");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047MODULE_LICENSE("GPL");