File Index
Files and directories
Sign in
FILE

clip.c

File preview with syntax highlighting.

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