File: /usr/src/linux/net/atm/common.c

1     /* net/atm/common.c - ATM sockets (common part for PVC and SVC) */
2     
3     /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
4     
5     
6     #include <linux/config.h>
7     #include <linux/module.h>
8     #include <linux/kmod.h>
9     #include <linux/net.h>		/* struct socket, struct net_proto, struct
10     				   proto_ops */
11     #include <linux/atm.h>		/* ATM stuff */
12     #include <linux/atmdev.h>
13     #include <linux/atmclip.h>	/* CLIP_*ENCAP */
14     #include <linux/atmarp.h>	/* manifest constants */
15     #include <linux/sonet.h>	/* for ioctls */
16     #include <linux/socket.h>	/* SOL_SOCKET */
17     #include <linux/errno.h>	/* error codes */
18     #include <linux/capability.h>
19     #include <linux/mm.h>		/* verify_area */
20     #include <linux/sched.h>
21     #include <linux/time.h>		/* struct timeval */
22     #include <linux/skbuff.h>
23     #include <linux/bitops.h>
24     #include <net/sock.h>		/* struct sock */
25     
26     #include <asm/uaccess.h>
27     #include <asm/atomic.h>
28     #include <asm/poll.h>
29     #include <asm/ioctls.h>
30     
31     #if defined(CONFIG_ATM_LANE) || defined(CONFIG_ATM_LANE_MODULE)
32     #include <linux/atmlec.h>
33     #include "lec.h"
34     #include "lec_arpc.h"
35     struct atm_lane_ops atm_lane_ops;
36     #endif
37     #ifdef CONFIG_ATM_LANE_MODULE
38     EXPORT_SYMBOL(atm_lane_ops);
39     #endif
40     
41     #if defined(CONFIG_ATM_MPOA) || defined(CONFIG_ATM_MPOA_MODULE)
42     #include <linux/atmmpc.h>
43     #include "mpc.h"
44     struct atm_mpoa_ops atm_mpoa_ops;
45     #endif
46     #ifdef CONFIG_ATM_MPOA_MODULE
47     EXPORT_SYMBOL(atm_mpoa_ops);
48     #ifndef CONFIG_ATM_LANE_MODULE
49     EXPORT_SYMBOL(atm_lane_ops);
50     #endif
51     #endif
52     
53     #if defined(CONFIG_ATM_TCP) || defined(CONFIG_ATM_TCP_MODULE)
54     #include <linux/atm_tcp.h>
55     #ifdef CONFIG_ATM_TCP_MODULE
56     struct atm_tcp_ops atm_tcp_ops;
57     EXPORT_SYMBOL(atm_tcp_ops);
58     #endif
59     #endif
60     
61     #include "resources.h"		/* atm_find_dev */
62     #include "common.h"		/* prototypes */
63     #include "protocols.h"		/* atm_init_<transport> */
64     #include "addr.h"		/* address registry */
65     #ifdef CONFIG_ATM_CLIP
66     #include <net/atmclip.h>	/* for clip_create */
67     #endif
68     #include "signaling.h"		/* for WAITING and sigd_attach */
69     
70     
71     #if 0
72     #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
73     #else
74     #define DPRINTK(format,args...)
75     #endif
76     
77     spinlock_t atm_dev_lock = SPIN_LOCK_UNLOCKED;
78     
79     static struct sk_buff *alloc_tx(struct atm_vcc *vcc,unsigned int size)
80     {
81     	struct sk_buff *skb;
82     
83     	if (atomic_read(&vcc->tx_inuse) && !atm_may_send(vcc,size)) {
84     		DPRINTK("Sorry: tx_inuse = %d, size = %d, sndbuf = %d\n",
85     		    atomic_read(&vcc->tx_inuse),size,vcc->sk->sndbuf);
86     		return NULL;
87     	}
88     	while (!(skb = alloc_skb(size,GFP_KERNEL))) schedule();
89     	DPRINTK("AlTx %d += %d\n",atomic_read(&vcc->tx_inuse),skb->truesize);
90     	atomic_add(skb->truesize+ATM_PDU_OVHD,&vcc->tx_inuse);
91     	return skb;
92     }
93     
94     
95     int atm_create(struct socket *sock,int protocol,int family)
96     {
97     	struct sock *sk;
98     	struct atm_vcc *vcc;
99     
100     	sock->sk = NULL;
101     	if (sock->type == SOCK_STREAM) return -EINVAL;
102     	if (!(sk = alloc_atm_vcc_sk(family))) return -ENOMEM;
103     	vcc = sk->protinfo.af_atm;
104     	memset(&vcc->flags,0,sizeof(vcc->flags));
105     	vcc->dev = NULL;
106     	vcc->family = sock->ops->family;
107     	vcc->alloc_tx = alloc_tx;
108     	vcc->callback = NULL;
109     	memset(&vcc->local,0,sizeof(struct sockaddr_atmsvc));
110     	memset(&vcc->remote,0,sizeof(struct sockaddr_atmsvc));
111     	vcc->qos.txtp.max_sdu = 1 << 16; /* for meta VCs */
112     	atomic_set(&vcc->tx_inuse,0);
113     	atomic_set(&vcc->rx_inuse,0);
114     	vcc->push = NULL;
115     	vcc->pop = NULL;
116     	vcc->push_oam = NULL;
117     	vcc->vpi = vcc->vci = 0; /* no VCI/VPI yet */
118     	vcc->atm_options = vcc->aal_options = 0;
119     	vcc->timestamp.tv_sec = vcc->timestamp.tv_usec = 0;
120     	init_waitqueue_head(&vcc->sleep);
121     	skb_queue_head_init(&vcc->recvq);
122     	skb_queue_head_init(&vcc->listenq);
123     	sk->sleep = &vcc->sleep;
124     	sock->sk = sk;
125     	return 0;
126     }
127     
128     
129     void atm_release_vcc_sk(struct sock *sk,int free_sk)
130     {
131     	struct atm_vcc *vcc;
132     	struct sk_buff *skb;
133     
134     	vcc = sk->protinfo.af_atm;
135     	clear_bit(ATM_VF_READY,&vcc->flags);
136     	if (vcc->dev) {
137     		if (vcc->dev->ops->close) vcc->dev->ops->close(vcc);
138     		if (vcc->push) vcc->push(vcc,NULL); /* atmarpd has no push */
139     		while ((skb = skb_dequeue(&vcc->recvq))) {
140     			atm_return(vcc,skb->truesize);
141     			if (vcc->dev->ops->free_rx_skb)
142     				vcc->dev->ops->free_rx_skb(vcc,skb);
143     			else kfree_skb(skb);
144     		}
145     		spin_lock (&atm_dev_lock);	
146     		fops_put (vcc->dev->ops);
147     		if (atomic_read(&vcc->rx_inuse))
148     			printk(KERN_WARNING "atm_release_vcc: strange ... "
149     			    "rx_inuse == %d after closing\n",
150     			    atomic_read(&vcc->rx_inuse));
151     		bind_vcc(vcc,NULL);
152     	} else
153     		spin_lock (&atm_dev_lock);	
154     
155     	if (free_sk) free_atm_vcc_sk(sk);
156     
157     	spin_unlock (&atm_dev_lock);
158     }
159     
160     
161     int atm_release(struct socket *sock)
162     {
163     	if (sock->sk)
164     		atm_release_vcc_sk(sock->sk,1);
165     	return 0;
166     }
167     
168     
169     void atm_async_release_vcc(struct atm_vcc *vcc,int reply)
170     {
171     	set_bit(ATM_VF_CLOSE,&vcc->flags);
172     	vcc->reply = reply;
173     	wake_up(&vcc->sleep);
174     }
175     
176     
177     EXPORT_SYMBOL(atm_async_release_vcc);
178     
179     
180     static int adjust_tp(struct atm_trafprm *tp,unsigned char aal)
181     {
182     	int max_sdu;
183     
184     	if (!tp->traffic_class) return 0;
185     	switch (aal) {
186     		case ATM_AAL0:
187     			max_sdu = ATM_CELL_SIZE-1;
188     			break;
189     		case ATM_AAL34:
190     			max_sdu = ATM_MAX_AAL34_PDU;
191     			break;
192     		default:
193     			printk(KERN_WARNING "ATM: AAL problems ... "
194     			    "(%d)\n",aal);
195     			/* fall through */
196     		case ATM_AAL5:
197     			max_sdu = ATM_MAX_AAL5_PDU;
198     	}
199     	if (!tp->max_sdu) tp->max_sdu = max_sdu;
200     	else if (tp->max_sdu > max_sdu) return -EINVAL;
201     	if (!tp->max_cdv) tp->max_cdv = ATM_MAX_CDV;
202     	return 0;
203     }
204     
205     
206     static int atm_do_connect_dev(struct atm_vcc *vcc,struct atm_dev *dev,int vpi,
207         int vci)
208     {
209     	int error;
210     
211     	if ((vpi != ATM_VPI_UNSPEC && vpi != ATM_VPI_ANY &&
212     	    vpi >> dev->ci_range.vpi_bits) || (vci != ATM_VCI_UNSPEC &&
213     	    vci != ATM_VCI_ANY && vci >> dev->ci_range.vci_bits))
214     		return -EINVAL;
215     	if (vci > 0 && vci < ATM_NOT_RSV_VCI && !capable(CAP_NET_BIND_SERVICE))
216     		return -EPERM;
217     	error = 0;
218     	bind_vcc(vcc,dev);
219     	switch (vcc->qos.aal) {
220     		case ATM_AAL0:
221     			error = atm_init_aal0(vcc);
222     			vcc->stats = &dev->stats.aal0;
223     			break;
224     		case ATM_AAL34:
225     			error = atm_init_aal34(vcc);
226     			vcc->stats = &dev->stats.aal34;
227     			break;
228     		case ATM_NO_AAL:
229     			/* ATM_AAL5 is also used in the "0 for default" case */
230     			vcc->qos.aal = ATM_AAL5;
231     			/* fall through */
232     		case ATM_AAL5:
233     			error = atm_init_aal5(vcc);
234     			vcc->stats = &dev->stats.aal5;
235     			break;
236     		default:
237     			error = -EPROTOTYPE;
238     	}
239     	if (!error) error = adjust_tp(&vcc->qos.txtp,vcc->qos.aal);
240     	if (!error) error = adjust_tp(&vcc->qos.rxtp,vcc->qos.aal);
241     	if (error) {
242     		bind_vcc(vcc,NULL);
243     		return error;
244     	}
245     	DPRINTK("VCC %d.%d, AAL %d\n",vpi,vci,vcc->qos.aal);
246     	DPRINTK("  TX: %d, PCR %d..%d, SDU %d\n",vcc->qos.txtp.traffic_class,
247     	    vcc->qos.txtp.min_pcr,vcc->qos.txtp.max_pcr,vcc->qos.txtp.max_sdu);
248     	DPRINTK("  RX: %d, PCR %d..%d, SDU %d\n",vcc->qos.rxtp.traffic_class,
249     	    vcc->qos.rxtp.min_pcr,vcc->qos.rxtp.max_pcr,vcc->qos.rxtp.max_sdu);
250     	fops_get (dev->ops);
251     	if (dev->ops->open) {
252     		error = dev->ops->open(vcc,vpi,vci);
253     		if (error) {
254     			fops_put (dev->ops);
255     			bind_vcc(vcc,NULL);
256     			return error;
257     		}
258     	}
259     	return 0;
260     }
261     
262     
263     static int atm_do_connect(struct atm_vcc *vcc,int itf,int vpi,int vci)
264     {
265     	struct atm_dev *dev;
266     	int return_val;
267     
268     	spin_lock (&atm_dev_lock);
269     	dev = atm_find_dev(itf);
270     	if (!dev)
271     		return_val =  -ENODEV;
272     	else
273     		return_val = atm_do_connect_dev(vcc,dev,vpi,vci);
274     
275     	spin_unlock (&atm_dev_lock);
276     
277     	return return_val;
278     }
279     
280     
281     int atm_connect_vcc(struct atm_vcc *vcc,int itf,short vpi,int vci)
282     {
283     	if (vpi != ATM_VPI_UNSPEC && vci != ATM_VCI_UNSPEC)
284     		clear_bit(ATM_VF_PARTIAL,&vcc->flags);
285     	else if (test_bit(ATM_VF_PARTIAL,&vcc->flags)) return -EINVAL;
286     	printk(KERN_DEBUG "atm_connect (TX: cl %d,bw %d-%d,sdu %d; "
287     	    "RX: cl %d,bw %d-%d,sdu %d,AAL %s%d)\n",
288     	    vcc->qos.txtp.traffic_class,vcc->qos.txtp.min_pcr,
289     	    vcc->qos.txtp.max_pcr,vcc->qos.txtp.max_sdu,
290     	    vcc->qos.rxtp.traffic_class,vcc->qos.rxtp.min_pcr,
291     	    vcc->qos.rxtp.max_pcr,vcc->qos.rxtp.max_sdu,
292     	    vcc->qos.aal == ATM_AAL5 ? "" : vcc->qos.aal == ATM_AAL0 ? "" :
293     	    " ??? code ",vcc->qos.aal == ATM_AAL0 ? 0 : vcc->qos.aal);
294     	if (!test_bit(ATM_VF_HASQOS,&vcc->flags)) return -EBADFD;
295     	if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
296     	    vcc->qos.rxtp.traffic_class == ATM_ANYCLASS)
297     		return -EINVAL;
298     	if (itf != ATM_ITF_ANY) {
299     		int error;
300     
301     		error = atm_do_connect(vcc,itf,vpi,vci);
302     		if (error) return error;
303     	}
304     	else {
305     		struct atm_dev *dev;
306     
307     		spin_lock (&atm_dev_lock);
308     		for (dev = atm_devs; dev; dev = dev->next)
309     			if (!atm_do_connect_dev(vcc,dev,vpi,vci)) break;
310     		spin_unlock (&atm_dev_lock);
311     		if (!dev) return -ENODEV;
312     	}
313     	if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC)
314     		set_bit(ATM_VF_PARTIAL,&vcc->flags);
315     	return 0;
316     }
317     
318     
319     int atm_connect(struct socket *sock,int itf,short vpi,int vci)
320     {
321     	int error;
322     
323     	DPRINTK("atm_connect (vpi %d, vci %d)\n",vpi,vci);
324     	if (sock->state == SS_CONNECTED) return -EISCONN;
325     	if (sock->state != SS_UNCONNECTED) return -EINVAL;
326     	if (!(vpi || vci)) return -EINVAL;
327     	error = atm_connect_vcc(ATM_SD(sock),itf,vpi,vci);
328     	if (error) return error;
329     	if (test_bit(ATM_VF_READY,&ATM_SD(sock)->flags))
330     		sock->state = SS_CONNECTED;
331     	return 0;
332     }
333     
334     
335     int atm_recvmsg(struct socket *sock,struct msghdr *m,int total_len,
336         int flags,struct scm_cookie *scm)
337     {
338     	DECLARE_WAITQUEUE(wait,current);
339     	struct atm_vcc *vcc;
340     	struct sk_buff *skb;
341     	int eff_len,error;
342     	void *buff;
343     	int size;
344     
345     	if (sock->state != SS_CONNECTED) return -ENOTCONN;
346     	if (flags & ~MSG_DONTWAIT) return -EOPNOTSUPP;
347     	if (m->msg_iovlen != 1) return -ENOSYS; /* fix this later @@@ */
348     	buff = m->msg_iov->iov_base;
349     	size = m->msg_iov->iov_len;
350     	vcc = ATM_SD(sock);
351     	add_wait_queue(&vcc->sleep,&wait);
352     	set_current_state(TASK_INTERRUPTIBLE);
353     	error = 1; /* <= 0 is error */
354     	while (!(skb = skb_dequeue(&vcc->recvq))) {
355     		if (test_bit(ATM_VF_RELEASED,&vcc->flags) ||
356     		    test_bit(ATM_VF_CLOSE,&vcc->flags)) {
357     			error = vcc->reply;
358     			break;
359     		}
360     		if (!test_bit(ATM_VF_READY,&vcc->flags)) {
361     			error = 0;
362     			break;
363     		}
364     		if (flags & MSG_DONTWAIT) {
365     			error = -EAGAIN;
366     			break;
367     		}
368     		schedule();
369     		set_current_state(TASK_INTERRUPTIBLE);
370     		if (signal_pending(current)) {
371     			error = -ERESTARTSYS;
372     			break;
373     		}
374     	}
375     	set_current_state(TASK_RUNNING);
376     	remove_wait_queue(&vcc->sleep,&wait);
377     	if (error <= 0) return error;
378     	vcc->timestamp = skb->stamp;
379     	eff_len = skb->len > size ? size : skb->len;
380     	if (skb->len > size) /* Not fit ?  Report it... */
381     		m->msg_flags |= MSG_TRUNC;
382     	if (vcc->dev->ops->feedback)
383     		vcc->dev->ops->feedback(vcc,skb,(unsigned long) skb->data,
384     		    (unsigned long) buff,eff_len);
385     	DPRINTK("RcvM %d -= %d\n",atomic_read(&vcc->rx_inuse),skb->truesize);
386     	atm_return(vcc,skb->truesize);
387     	if (ATM_SKB(skb)->iovcnt) { /* @@@ hack */
388     		/* iovcnt set, use scatter-gather for receive */
389     		int el, cnt;
390     		struct iovec *iov = (struct iovec *)skb->data;
391     		unsigned char *p = (unsigned char *)buff;
392     
393     		el = eff_len;
394     		error = 0;
395     		for (cnt = 0; (cnt < ATM_SKB(skb)->iovcnt) && el; cnt++) {
396     /*printk("s-g???: %p -> %p (%d)\n",iov->iov_base,p,iov->iov_len);*/
397     			error = copy_to_user(p,iov->iov_base,
398     			    (iov->iov_len > el) ? el : iov->iov_len) ?
399     			    -EFAULT : 0;
400     			if (error) break;
401     			p += iov->iov_len;
402     			el -= (iov->iov_len > el)?el:iov->iov_len;
403     			iov++;
404     		}
405     		if (!vcc->dev->ops->free_rx_skb) kfree_skb(skb);
406     		else vcc->dev->ops->free_rx_skb(vcc, skb);
407     		return error ? error : eff_len;
408     	}
409     	error = copy_to_user(buff,skb->data,eff_len) ? -EFAULT : 0;
410     	if (!vcc->dev->ops->free_rx_skb) kfree_skb(skb);
411     	else vcc->dev->ops->free_rx_skb(vcc, skb);
412     	return error ? error : eff_len;
413     }
414     
415     
416     int atm_sendmsg(struct socket *sock,struct msghdr *m,int total_len,
417         struct scm_cookie *scm)
418     {
419     	DECLARE_WAITQUEUE(wait,current);
420     	struct atm_vcc *vcc;
421     	struct sk_buff *skb;
422     	int eff,error;
423     	const void *buff;
424     	int size;
425     
426     	if (sock->state != SS_CONNECTED) return -ENOTCONN;
427     	if (m->msg_name) return -EISCONN;
428     	if (m->msg_iovlen != 1) return -ENOSYS; /* fix this later @@@ */
429     	buff = m->msg_iov->iov_base;
430     	size = m->msg_iov->iov_len;
431     	vcc = ATM_SD(sock);
432     	if (test_bit(ATM_VF_RELEASED,&vcc->flags) ||
433     	    test_bit(ATM_VF_CLOSE,&vcc->flags))
434     		return vcc->reply;
435     	if (!test_bit(ATM_VF_READY,&vcc->flags)) return -EPIPE;
436     	if (!size) return 0;
437     	if (size < 0 || size > vcc->qos.txtp.max_sdu) return -EMSGSIZE;
438     	/* verify_area is done by net/socket.c */
439     	eff = (size+3) & ~3; /* align to word boundary */
440     	add_wait_queue(&vcc->sleep,&wait);
441     	set_current_state(TASK_INTERRUPTIBLE);
442     	error = 0;
443     	while (!(skb = vcc->alloc_tx(vcc,eff))) {
444     		if (m->msg_flags & MSG_DONTWAIT) {
445     			error = -EAGAIN;
446     			break;
447     		}
448     		schedule();
449     		set_current_state(TASK_INTERRUPTIBLE);
450     		if (signal_pending(current)) {
451     			error = -ERESTARTSYS;
452     			break;
453     		}
454     		if (test_bit(ATM_VF_RELEASED,&vcc->flags) ||
455     		    test_bit(ATM_VF_CLOSE,&vcc->flags)) {
456     			error = vcc->reply;
457     			break;
458     		}
459     		if (!test_bit(ATM_VF_READY,&vcc->flags)) {
460     			error = -EPIPE;
461     			break;
462     		}
463     	}
464     	set_current_state(TASK_RUNNING);
465     	remove_wait_queue(&vcc->sleep,&wait);
466     	if (error) return error;
467     	skb->dev = NULL; /* for paths shared with net_device interfaces */
468     	ATM_SKB(skb)->iovcnt = 0;
469     	ATM_SKB(skb)->atm_options = vcc->atm_options;
470     	if (copy_from_user(skb_put(skb,size),buff,size)) {
471     		kfree_skb(skb);
472     		return -EFAULT;
473     	}
474     	if (eff != size) memset(skb->data+size,0,eff-size);
475     	error = vcc->dev->ops->send(vcc,skb);
476     	return error ? error : size;
477     }
478     
479     
480     unsigned int atm_poll(struct file *file,struct socket *sock,poll_table *wait)
481     {
482     	struct atm_vcc *vcc;
483     	unsigned int mask;
484     
485     	vcc = ATM_SD(sock);
486     	poll_wait(file,&vcc->sleep,wait);
487     	mask = 0;
488     	if (skb_peek(&vcc->recvq) || skb_peek(&vcc->listenq))
489     		mask |= POLLIN | POLLRDNORM;
490     	if (test_bit(ATM_VF_RELEASED,&vcc->flags) ||
491     	    test_bit(ATM_VF_CLOSE,&vcc->flags))
492     		mask |= POLLHUP;
493     	if (sock->state != SS_CONNECTING) {
494     		if (vcc->qos.txtp.traffic_class != ATM_NONE &&
495     		    vcc->qos.txtp.max_sdu+atomic_read(&vcc->tx_inuse)+
496     		    ATM_PDU_OVHD <= vcc->sk->sndbuf)
497     			mask |= POLLOUT | POLLWRNORM;
498     	}
499     	else if (vcc->reply != WAITING) {
500     			mask |= POLLOUT | POLLWRNORM;
501     			if (vcc->reply) mask |= POLLERR;
502     		}
503     	return mask;
504     }
505     
506     
507     static void copy_aal_stats(struct k_atm_aal_stats *from,
508         struct atm_aal_stats *to)
509     {
510     #define __HANDLE_ITEM(i) to->i = atomic_read(&from->i)
511     	__AAL_STAT_ITEMS
512     #undef __HANDLE_ITEM
513     }
514     
515     
516     static void subtract_aal_stats(struct k_atm_aal_stats *from,
517         struct atm_aal_stats *to)
518     {
519     #define __HANDLE_ITEM(i) atomic_sub(to->i,&from->i)
520     	__AAL_STAT_ITEMS
521     #undef __HANDLE_ITEM
522     }
523     
524     
525     static int fetch_stats(struct atm_dev *dev,struct atm_dev_stats *arg,int zero)
526     {
527     	struct atm_dev_stats tmp;
528     	int error = 0;
529     
530     	copy_aal_stats(&dev->stats.aal0,&tmp.aal0);
531     	copy_aal_stats(&dev->stats.aal34,&tmp.aal34);
532     	copy_aal_stats(&dev->stats.aal5,&tmp.aal5);
533     	if (arg) error = copy_to_user(arg,&tmp,sizeof(tmp));
534     	if (zero && !error) {
535     		subtract_aal_stats(&dev->stats.aal0,&tmp.aal0);
536     		subtract_aal_stats(&dev->stats.aal34,&tmp.aal34);
537     		subtract_aal_stats(&dev->stats.aal5,&tmp.aal5);
538     	}
539     	return error ? -EFAULT : 0;
540     }
541     
542     
543     int atm_ioctl(struct socket *sock,unsigned int cmd,unsigned long arg)
544     {
545     	struct atm_dev *dev;
546     	struct atm_vcc *vcc;
547     	int *tmp_buf, *tmp_p;
548     	void *buf;
549     	int error,len,size,number, ret_val;
550     
551     	ret_val = 0;
552     	spin_lock (&atm_dev_lock);
553     	vcc = ATM_SD(sock);
554     	switch (cmd) {
555     		case SIOCOUTQ:
556     			if (sock->state != SS_CONNECTED ||
557     			    !test_bit(ATM_VF_READY,&vcc->flags)) {
558     				ret_val =  -EINVAL;
559     				goto done;
560     			}
561     			ret_val =  put_user(vcc->sk->sndbuf-
562     			    atomic_read(&vcc->tx_inuse)-ATM_PDU_OVHD,
563     			    (int *) arg) ? -EFAULT : 0;
564     			goto done;
565     		case SIOCINQ:
566     			{
567     				struct sk_buff *skb;
568     
569     				if (sock->state != SS_CONNECTED) {
570     					ret_val = -EINVAL;
571     					goto done;
572     				}
573     				skb = skb_peek(&vcc->recvq);
574     				ret_val = put_user(skb ? skb->len : 0,(int *) arg)
575     				    ? -EFAULT : 0;
576     				goto done;
577     			}
578     		case ATM_GETNAMES:
579     			if (get_user(buf,
580     				     &((struct atm_iobuf *) arg)->buffer)) {
581     				ret_val = -EFAULT;
582     				goto done;
583     			}
584     			if (get_user(len,
585     				     &((struct atm_iobuf *) arg)->length)) {
586     				ret_val = -EFAULT;
587     				goto done;
588     			}
589     			size = 0;
590     			for (dev = atm_devs; dev; dev = dev->next)
591     				size += sizeof(int);
592     			if (size > len) {
593     				ret_val = -E2BIG;
594     				goto done;
595     			}
596     			tmp_buf = kmalloc(size,GFP_KERNEL);
597     			if (!tmp_buf) {
598     				ret_val = -ENOMEM;
599     				goto done;
600     			}
601     			tmp_p = tmp_buf;
602     			for (dev = atm_devs; dev; dev = dev->next)
603     				*tmp_p++ = dev->number;
604     		        ret_val = ((copy_to_user(buf, tmp_buf, size)) ||
605     			    put_user(size, &((struct atm_iobuf *) arg)->length)
606     			    ) ? -EFAULT : 0;
607     			kfree(tmp_buf);
608     			goto done;
609     		case SIOCGSTAMP: /* borrowed from IP */
610     			if (!vcc->timestamp.tv_sec) {
611     				ret_val = -ENOENT;
612     				goto done;
613     			}
614     			vcc->timestamp.tv_sec += vcc->timestamp.tv_usec/1000000;
615     			vcc->timestamp.tv_usec %= 1000000;
616     			ret_val = copy_to_user((void *) arg,&vcc->timestamp,
617     			    sizeof(struct timeval)) ? -EFAULT : 0;
618     			goto done;
619     		case ATM_SETSC:
620     			printk(KERN_WARNING "ATM_SETSC is obsolete\n");
621     			ret_val = 0;
622     			goto done;
623     		case ATMSIGD_CTRL:
624     			if (!capable(CAP_NET_ADMIN)) {
625     				ret_val = -EPERM;
626     				goto done;
627     			}
628     			/*
629     			 * The user/kernel protocol for exchanging signalling
630     			 * info uses kernel pointers as opaque references,
631     			 * so the holder of the file descriptor can scribble
632     			 * on the kernel... so we should make sure that we
633     			 * have the same privledges that /proc/kcore needs
634     			 */
635     			if (!capable(CAP_SYS_RAWIO)) {
636     				ret_val = -EPERM;
637     				goto done;
638     			}
639     			error = sigd_attach(vcc);
640     			if (!error) sock->state = SS_CONNECTED;
641     			ret_val = error;
642     			goto done;
643     #ifdef CONFIG_ATM_CLIP
644     		case SIOCMKCLIP:
645     			if (!capable(CAP_NET_ADMIN))
646     				ret_val = -EPERM;
647     			else 
648     				ret_val = clip_create(arg);
649     			goto done;
650     		case ATMARPD_CTRL:
651     			if (!capable(CAP_NET_ADMIN)) {
652     				ret_val = -EPERM;
653     				goto done;
654     			}
655     			error = atm_init_atmarp(vcc);
656     			if (!error) sock->state = SS_CONNECTED;
657     			ret_val = error;
658     			goto done;
659     		case ATMARP_MKIP:
660     			if (!capable(CAP_NET_ADMIN)) 
661     				ret_val = -EPERM;
662     			else 
663     				ret_val = clip_mkip(vcc,arg);
664     			goto done;
665     		case ATMARP_SETENTRY:
666     			if (!capable(CAP_NET_ADMIN)) 
667     				ret_val = -EPERM;
668     			else
669     				ret_val = clip_setentry(vcc,arg);
670     			goto done;
671     		case ATMARP_ENCAP:
672     			if (!capable(CAP_NET_ADMIN)) 
673     				ret_val = -EPERM;
674     			else
675     				ret_val = clip_encap(vcc,arg);
676     			goto done;
677     #endif
678     #if defined(CONFIG_ATM_LANE) || defined(CONFIG_ATM_LANE_MODULE)
679                     case ATMLEC_CTRL:
680                             if (!capable(CAP_NET_ADMIN)) {
681     				ret_val = -EPERM;
682     				goto done;
683     			}
684                             if (atm_lane_ops.lecd_attach == NULL)
685     				atm_lane_init();
686                             if (atm_lane_ops.lecd_attach == NULL) { /* try again */
687     				ret_val = -ENOSYS;
688     				goto done;
689     			}
690     			error = atm_lane_ops.lecd_attach(vcc, (int)arg);
691     			if (error >= 0) sock->state = SS_CONNECTED;
692     			ret_val =  error;
693     			goto done;
694                     case ATMLEC_MCAST:
695     			if (!capable(CAP_NET_ADMIN))
696     				ret_val = -EPERM;
697     			else
698     				ret_val = atm_lane_ops.mcast_attach(vcc, (int)arg);
699     			goto done;
700                     case ATMLEC_DATA:
701     			if (!capable(CAP_NET_ADMIN))
702     				ret_val = -EPERM;
703     			else
704     				ret_val = atm_lane_ops.vcc_attach(vcc, (void*)arg);
705     			goto done;
706     #endif
707     #if defined(CONFIG_ATM_MPOA) || defined(CONFIG_ATM_MPOA_MODULE)
708     		case ATMMPC_CTRL:
709     			if (!capable(CAP_NET_ADMIN)) {
710     				ret_val = -EPERM;
711     				goto done;
712     			}
713     			if (atm_mpoa_ops.mpoad_attach == NULL)
714                                     atm_mpoa_init();
715     			if (atm_mpoa_ops.mpoad_attach == NULL) { /* try again */
716     				ret_val = -ENOSYS;
717     				goto done;
718     			}
719     			error = atm_mpoa_ops.mpoad_attach(vcc, (int)arg);
720     			if (error >= 0) sock->state = SS_CONNECTED;
721     			ret_val = error;
722     			goto done;
723     		case ATMMPC_DATA:
724     			if (!capable(CAP_NET_ADMIN)) 
725     				ret_val = -EPERM;
726     			else
727     				ret_val = atm_mpoa_ops.vcc_attach(vcc, arg);
728     			goto done;
729     #endif
730     #if defined(CONFIG_ATM_TCP) || defined(CONFIG_ATM_TCP_MODULE)
731     		case SIOCSIFATMTCP:
732     			if (!capable(CAP_NET_ADMIN)) {
733     				ret_val = -EPERM;
734     				goto done;
735     			}
736     			if (!atm_tcp_ops.attach) {
737     				ret_val = -ENOPKG;
738     				goto done;
739     			}
740     			fops_get (&atm_tcp_ops);
741     			error = atm_tcp_ops.attach(vcc,(int) arg);
742     			if (error >= 0) sock->state = SS_CONNECTED;
743     			else            fops_put (&atm_tcp_ops);
744     			ret_val = error;
745     			goto done;
746     		case ATMTCP_CREATE:
747     			if (!capable(CAP_NET_ADMIN)) {
748     				ret_val = -EPERM;
749     				goto done;
750     			}
751     			if (!atm_tcp_ops.create_persistent) {
752     				ret_val = -ENOPKG;
753     				goto done;
754     			}
755     			error = atm_tcp_ops.create_persistent((int) arg);
756     			if (error < 0) fops_put (&atm_tcp_ops);
757     			ret_val = error;
758     			goto done;
759     		case ATMTCP_REMOVE:
760     			if (!capable(CAP_NET_ADMIN)) {
761     				ret_val = -EPERM;
762     				goto done;
763     			}
764     			if (!atm_tcp_ops.remove_persistent) {
765     				ret_val = -ENOPKG;
766     				goto done;
767     			}
768     			error = atm_tcp_ops.remove_persistent((int) arg);
769     			fops_put (&atm_tcp_ops);
770     			ret_val = error;
771     			goto done;
772     #endif
773     		default:
774     			break;
775     	}
776     	if (get_user(buf,&((struct atmif_sioc *) arg)->arg)) {
777     		ret_val = -EFAULT;
778     		goto done;
779     	}
780     	if (get_user(len,&((struct atmif_sioc *) arg)->length)) {
781     		ret_val = -EFAULT;
782     		goto done;
783     	}
784     	if (get_user(number,&((struct atmif_sioc *) arg)->number)) {
785     		ret_val = -EFAULT;
786     		goto done;
787     	}
788     	if (!(dev = atm_find_dev(number))) {
789     		ret_val = -ENODEV;
790     		goto done;
791     	}
792     	
793     	size = 0;
794     	switch (cmd) {
795     		case ATM_GETTYPE:
796     			size = strlen(dev->type)+1;
797     			if (copy_to_user(buf,dev->type,size)) {
798     				ret_val = -EFAULT;
799     				goto done;
800     			}
801     			break;
802     		case ATM_GETESI:
803     			size = ESI_LEN;
804     			if (copy_to_user(buf,dev->esi,size)) {
805     				ret_val = -EFAULT;
806     				goto done;
807     			}
808     			break;
809     		case ATM_SETESI:
810     			{
811     				int i;
812     
813     				for (i = 0; i < ESI_LEN; i++)
814     					if (dev->esi[i]) {
815     						ret_val = -EEXIST;
816     						goto done;
817     					}
818     			}
819     			/* fall through */
820     		case ATM_SETESIF:
821     			{
822     				unsigned char esi[ESI_LEN];
823     
824     				if (!capable(CAP_NET_ADMIN)) {
825     					ret_val = -EPERM;
826     					goto done;
827     				}
828     				if (copy_from_user(esi,buf,ESI_LEN)) {
829     					ret_val = -EFAULT;
830     					goto done;
831     				}
832     				memcpy(dev->esi,esi,ESI_LEN);
833     				ret_val =  ESI_LEN;
834     				goto done;
835     			}
836     		case ATM_GETSTATZ:
837     			if (!capable(CAP_NET_ADMIN)) {
838     				ret_val = -EPERM;
839     				goto done;
840     			}
841     			/* fall through */
842     		case ATM_GETSTAT:
843     			size = sizeof(struct atm_dev_stats);
844     			error = fetch_stats(dev,buf,cmd == ATM_GETSTATZ);
845     			if (error) {
846     				ret_val = error;
847     				goto done;
848     			}
849     			break;
850     		case ATM_GETCIRANGE:
851     			size = sizeof(struct atm_cirange);
852     			if (copy_to_user(buf,&dev->ci_range,size)) {
853     				ret_val = -EFAULT;
854     				goto done;
855     			}
856     			break;
857     		case ATM_GETLINKRATE:
858     			size = sizeof(int);
859     			if (copy_to_user(buf,&dev->link_rate,size)) {
860     				ret_val = -EFAULT;
861     				goto done;
862     			}
863     			break;
864     		case ATM_RSTADDR:
865     			if (!capable(CAP_NET_ADMIN)) {
866     				ret_val = -EPERM;
867     				goto done;
868     			}
869     			atm_reset_addr(dev);
870     			break;
871     		case ATM_ADDADDR:
872     		case ATM_DELADDR:
873     			if (!capable(CAP_NET_ADMIN)) {
874     				ret_val = -EPERM;
875     				goto done;
876     			}
877     			{
878     				struct sockaddr_atmsvc addr;
879     
880     				if (copy_from_user(&addr,buf,sizeof(addr))) {
881     					ret_val = -EFAULT;
882     					goto done;
883     				}
884     				if (cmd == ATM_ADDADDR)
885     					ret_val = atm_add_addr(dev,&addr);
886     				else
887     					ret_val = atm_del_addr(dev,&addr);
888     				goto done;
889     			}
890     		case ATM_GETADDR:
891     			size = atm_get_addr(dev,buf,len);
892     			if (size < 0)
893     				ret_val = size;
894     			else
895     			/* may return 0, but later on size == 0 means "don't
896     			   write the length" */
897     				ret_val = put_user(size,
898     						   &((struct atmif_sioc *) arg)->length) ? -EFAULT : 0;
899     			goto done;
900     		case ATM_SETLOOP:
901     			if (__ATM_LM_XTRMT((int) (long) buf) &&
902     			    __ATM_LM_XTLOC((int) (long) buf) >
903     			    __ATM_LM_XTRMT((int) (long) buf)) {
904     				ret_val = -EINVAL;
905     				goto done;
906     			}
907     			/* fall through */
908     		case ATM_SETCIRANGE:
909     		case SONET_GETSTATZ:
910     		case SONET_SETDIAG:
911     		case SONET_CLRDIAG:
912     		case SONET_SETFRAMING:
913     			if (!capable(CAP_NET_ADMIN)) {
914     				ret_val = -EPERM;
915     				goto done;
916     			}
917     			/* fall through */
918     		default:
919     			if (!dev->ops->ioctl) {
920     				ret_val = -EINVAL;
921     				goto done;
922     			}
923     			size = dev->ops->ioctl(dev,cmd,buf);
924     			if (size < 0) {
925     				ret_val = (size == -ENOIOCTLCMD ? -EINVAL : size);
926     				goto done;
927     			}
928     	}
929     	
930     	if (size)
931     		ret_val =  put_user(size,&((struct atmif_sioc *) arg)->length) ?
932     			-EFAULT : 0;
933     
934      done:
935     	spin_unlock (&atm_dev_lock); 
936     	return ret_val;
937     }
938     
939     
940     static int atm_change_qos(struct atm_vcc *vcc,struct atm_qos *qos)
941     {
942     	int error;
943     
944     	/*
945     	 * Don't let the QoS change the already connected AAL type nor the
946     	 * traffic class.
947     	 */
948     	if (qos->aal != vcc->qos.aal ||
949     	    qos->rxtp.traffic_class != vcc->qos.rxtp.traffic_class ||
950     	    qos->txtp.traffic_class != vcc->qos.txtp.traffic_class)
951     		return -EINVAL;
952     	error = adjust_tp(&qos->txtp,qos->aal);
953     	if (!error) error = adjust_tp(&qos->rxtp,qos->aal);
954     	if (error) return error;
955     	if (!vcc->dev->ops->change_qos) return -EOPNOTSUPP;
956     	if (vcc->family == AF_ATMPVC)
957     		return vcc->dev->ops->change_qos(vcc,qos,ATM_MF_SET);
958     	return svc_change_qos(vcc,qos);
959     }
960     
961     
962     static int check_tp(struct atm_trafprm *tp)
963     {
964     	/* @@@ Should be merged with adjust_tp */
965     	if (!tp->traffic_class || tp->traffic_class == ATM_ANYCLASS) return 0;
966     	if (tp->traffic_class != ATM_UBR && !tp->min_pcr && !tp->pcr &&
967     	    !tp->max_pcr) return -EINVAL;
968     	if (tp->min_pcr == ATM_MAX_PCR) return -EINVAL;
969     	if (tp->min_pcr && tp->max_pcr && tp->max_pcr != ATM_MAX_PCR &&
970     	    tp->min_pcr > tp->max_pcr) return -EINVAL;
971     	/*
972     	 * We allow pcr to be outside [min_pcr,max_pcr], because later
973     	 * adjustment may still push it in the valid range.
974     	 */
975     	return 0;
976     }
977     
978     
979     static int check_qos(struct atm_qos *qos)
980     {
981     	int error;
982     
983     	if (!qos->txtp.traffic_class && !qos->rxtp.traffic_class)
984                     return -EINVAL;
985     	if (qos->txtp.traffic_class != qos->rxtp.traffic_class &&
986     	    qos->txtp.traffic_class && qos->rxtp.traffic_class &&
987     	    qos->txtp.traffic_class != ATM_ANYCLASS &&
988     	    qos->rxtp.traffic_class != ATM_ANYCLASS) return -EINVAL;
989     	error = check_tp(&qos->txtp);
990     	if (error) return error;
991     	return check_tp(&qos->rxtp);
992     }
993     
994     
995     static int atm_do_setsockopt(struct socket *sock,int level,int optname,
996         void *optval,int optlen)
997     {
998     	struct atm_vcc *vcc;
999     	unsigned long value;
1000     	int error;
1001     
1002     	vcc = ATM_SD(sock);
1003     	switch (optname) {
1004     		case SO_ATMQOS:
1005     			{
1006     				struct atm_qos qos;
1007     
1008     				if (copy_from_user(&qos,optval,sizeof(qos)))
1009     					return -EFAULT;
1010     				error = check_qos(&qos);
1011     				if (error) return error;
1012     				if (sock->state == SS_CONNECTED)
1013     					return atm_change_qos(vcc,&qos);
1014     				if (sock->state != SS_UNCONNECTED)
1015     					return -EBADFD;
1016     				vcc->qos = qos;
1017     				set_bit(ATM_VF_HASQOS,&vcc->flags);
1018     				return 0;
1019     			}
1020     		case SO_SETCLP:
1021     			if (get_user(value,(unsigned long *) optval))
1022     				return -EFAULT;
1023     			if (value) vcc->atm_options |= ATM_ATMOPT_CLP;
1024     			else vcc->atm_options &= ~ATM_ATMOPT_CLP;
1025     			return 0;
1026     		default:
1027     			if (level == SOL_SOCKET) return -EINVAL;
1028     			break;
1029     	}
1030     	if (!vcc->dev || !vcc->dev->ops->setsockopt) return -EINVAL;
1031     	return vcc->dev->ops->setsockopt(vcc,level,optname,optval,optlen);
1032     }
1033     
1034     
1035     static int atm_do_getsockopt(struct socket *sock,int level,int optname,
1036         void *optval,int optlen)
1037     {
1038     	struct atm_vcc *vcc;
1039     
1040     	vcc = ATM_SD(sock);
1041     	switch (optname) {
1042     		case SO_ATMQOS:
1043     			if (!test_bit(ATM_VF_HASQOS,&vcc->flags))
1044     				return -EINVAL;
1045     			return copy_to_user(optval,&vcc->qos,sizeof(vcc->qos)) ?
1046     			    -EFAULT : 0;
1047     		case SO_SETCLP:
1048     			return put_user(vcc->atm_options & ATM_ATMOPT_CLP ? 1 :
1049     			  0,(unsigned long *) optval) ? -EFAULT : 0;
1050     		case SO_ATMPVC:
1051     			{
1052     				struct sockaddr_atmpvc pvc;
1053     
1054     				if (!vcc->dev ||
1055     				    !test_bit(ATM_VF_ADDR,&vcc->flags))
1056     					return -ENOTCONN;
1057     				pvc.sap_family = AF_ATMPVC;
1058     				pvc.sap_addr.itf = vcc->dev->number;
1059     				pvc.sap_addr.vpi = vcc->vpi;
1060     				pvc.sap_addr.vci = vcc->vci;
1061     				return copy_to_user(optval,&pvc,sizeof(pvc)) ?
1062     				    -EFAULT : 0;
1063     			}
1064     		default:
1065     			if (level == SOL_SOCKET) return -EINVAL;
1066     			break;
1067     	}
1068     	if (!vcc->dev || !vcc->dev->ops->getsockopt) return -EINVAL;
1069     	return vcc->dev->ops->getsockopt(vcc,level,optname,optval,optlen);
1070     }
1071     
1072     
1073     int atm_setsockopt(struct socket *sock,int level,int optname,char *optval,
1074         int optlen)
1075     {
1076     	if (__SO_LEVEL_MATCH(optname, level) && optlen != __SO_SIZE(optname))
1077     		return -EINVAL;
1078     	return atm_do_setsockopt(sock,level,optname,optval,optlen);
1079     }
1080     
1081     
1082     int atm_getsockopt(struct socket *sock,int level,int optname,
1083         char *optval,int *optlen)
1084     {
1085     	int len;
1086     
1087     	if (get_user(len,optlen)) return -EFAULT;
1088     	if (__SO_LEVEL_MATCH(optname, level) && len != __SO_SIZE(optname))
1089     		return -EINVAL;
1090     	return atm_do_getsockopt(sock,level,optname,optval,len);
1091     }
1092     
1093     
1094     /*
1095      * lane_mpoa_init.c: A couple of helper functions
1096      * to make modular LANE and MPOA client easier to implement
1097      */
1098     
1099     /*
1100      * This is how it goes:
1101      *
1102      * if xxxx is not compiled as module, call atm_xxxx_init_ops()
1103      *    from here
1104      * else call atm_mpoa_init_ops() from init_module() within
1105      *    the kernel when xxxx module is loaded
1106      *
1107      * In either case function pointers in struct atm_xxxx_ops
1108      * are initialized to their correct values. Either they
1109      * point to functions in the module or in the kernel
1110      */
1111      
1112     extern struct atm_mpoa_ops atm_mpoa_ops; /* in common.c */
1113     extern struct atm_lane_ops atm_lane_ops; /* in common.c */
1114     
1115     #if defined(CONFIG_ATM_MPOA) || defined(CONFIG_ATM_MPOA_MODULE)
1116     void atm_mpoa_init(void)
1117     {
1118     #ifndef CONFIG_ATM_MPOA_MODULE /* not module */
1119             atm_mpoa_init_ops(&atm_mpoa_ops);
1120     #else
1121     	request_module("mpoa");
1122     #endif
1123     
1124             return;
1125     }
1126     #endif
1127     
1128     #if defined(CONFIG_ATM_LANE) || defined(CONFIG_ATM_LANE_MODULE)
1129     #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
1130     struct net_bridge_fdb_entry *(*br_fdb_get_hook)(struct net_bridge *br,
1131     						unsigned char *addr) = NULL;
1132     void (*br_fdb_put_hook)(struct net_bridge_fdb_entry *ent) = NULL;
1133     #if defined(CONFIG_ATM_LANE_MODULE) || defined(CONFIG_BRIDGE_MODULE)
1134     EXPORT_SYMBOL(br_fdb_get_hook);
1135     EXPORT_SYMBOL(br_fdb_put_hook);
1136     #endif /* defined(CONFIG_ATM_LANE_MODULE) || defined(CONFIG_BRIDGE_MODULE) */
1137     #endif /* defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE) */
1138     
1139     void atm_lane_init(void)
1140     {
1141     #ifndef CONFIG_ATM_LANE_MODULE /* not module */
1142             atm_lane_init_ops(&atm_lane_ops);
1143     #else
1144     	request_module("lec");
1145     #endif
1146     
1147             return;
1148     }        
1149     #endif
1150