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