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