blob: b43feb1a3995f77a75d0c445859ab70030c2f477 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* net/atm/common.c - ATM sockets (common part for PVC and SVC) */
2
3/* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
4
Joe Perches99824462010-01-26 11:40:00 +00005#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
Linus Torvalds1da177e2005-04-16 15:20:36 -07006
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/module.h>
8#include <linux/kmod.h>
9#include <linux/net.h> /* struct socket, struct proto_ops */
10#include <linux/atm.h> /* ATM stuff */
11#include <linux/atmdev.h>
12#include <linux/socket.h> /* SOL_SOCKET */
13#include <linux/errno.h> /* error codes */
14#include <linux/capability.h>
Jesper Juhle49332b2005-05-01 08:59:08 -070015#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/sched.h>
17#include <linux/time.h> /* struct timeval */
18#include <linux/skbuff.h>
19#include <linux/bitops.h>
20#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <net/sock.h> /* struct sock */
Joe Perchesa8147d72010-01-26 11:40:06 +000023#include <linux/uaccess.h>
24#include <linux/poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28#include "resources.h" /* atm_find_dev */
29#include "common.h" /* prototypes */
30#include "protocols.h" /* atm_init_<transport> */
31#include "addr.h" /* address registry */
32#include "signaling.h" /* for WAITING and sigd_attach */
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034struct hlist_head vcc_hash[VCC_HTABLE_SIZE];
Joe Perchesa8147d72010-01-26 11:40:06 +000035EXPORT_SYMBOL(vcc_hash);
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037DEFINE_RWLOCK(vcc_sklist_lock);
Joe Perchesa8147d72010-01-26 11:40:06 +000038EXPORT_SYMBOL(vcc_sklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40static void __vcc_insert_socket(struct sock *sk)
41{
42 struct atm_vcc *vcc = atm_sk(sk);
Joe Perchesa8147d72010-01-26 11:40:06 +000043 struct hlist_head *head = &vcc_hash[vcc->vci & (VCC_HTABLE_SIZE - 1)];
Eric Dumazet81c3d542005-10-03 14:13:38 -070044 sk->sk_hash = vcc->vci & (VCC_HTABLE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 sk_add_node(sk, head);
46}
47
48void vcc_insert_socket(struct sock *sk)
49{
50 write_lock_irq(&vcc_sklist_lock);
51 __vcc_insert_socket(sk);
52 write_unlock_irq(&vcc_sklist_lock);
53}
Joe Perchesa8147d72010-01-26 11:40:06 +000054EXPORT_SYMBOL(vcc_insert_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56static void vcc_remove_socket(struct sock *sk)
57{
58 write_lock_irq(&vcc_sklist_lock);
59 sk_del_node_init(sk);
60 write_unlock_irq(&vcc_sklist_lock);
61}
62
Joe Perchesa8147d72010-01-26 11:40:06 +000063static struct sk_buff *alloc_tx(struct atm_vcc *vcc, unsigned int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 struct sk_buff *skb;
66 struct sock *sk = sk_atm(vcc);
67
Eric Dumazet81e2a3d2009-06-17 19:06:12 -070068 if (sk_wmem_alloc_get(sk) && !atm_may_send(vcc, size)) {
Stephen Hemminger52240062007-08-28 15:22:09 -070069 pr_debug("Sorry: wmem_alloc = %d, size = %d, sndbuf = %d\n",
Joe Perches99824462010-01-26 11:40:00 +000070 sk_wmem_alloc_get(sk), size, sk->sk_sndbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return NULL;
72 }
Eric Dumazet81e2a3d2009-06-17 19:06:12 -070073 while (!(skb = alloc_skb(size, GFP_KERNEL)))
74 schedule();
Joe Perchesa8147d72010-01-26 11:40:06 +000075 pr_debug("%d += %d\n", sk_wmem_alloc_get(sk), skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 atomic_add(skb->truesize, &sk->sk_wmem_alloc);
77 return skb;
78}
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static void vcc_sock_destruct(struct sock *sk)
81{
82 if (atomic_read(&sk->sk_rmem_alloc))
Joe Perchesa8147d72010-01-26 11:40:06 +000083 printk(KERN_DEBUG "%s: rmem leakage (%d bytes) detected.\n",
84 __func__, atomic_read(&sk->sk_rmem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 if (atomic_read(&sk->sk_wmem_alloc))
Joe Perchesa8147d72010-01-26 11:40:06 +000087 printk(KERN_DEBUG "%s: wmem leakage (%d bytes) detected.\n",
88 __func__, atomic_read(&sk->sk_wmem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
91static void vcc_def_wakeup(struct sock *sk)
92{
Eric Dumazet43815482010-04-29 11:01:49 +000093 struct socket_wq *wq;
94
95 rcu_read_lock();
96 wq = rcu_dereference(sk->sk_wq);
97 if (wq_has_sleeper(wq))
98 wake_up(&wq->wait);
99 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
102static inline int vcc_writable(struct sock *sk)
103{
104 struct atm_vcc *vcc = atm_sk(sk);
105
106 return (vcc->qos.txtp.max_sdu +
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900107 atomic_read(&sk->sk_wmem_alloc)) <= sk->sk_sndbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
110static void vcc_write_space(struct sock *sk)
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900111{
Eric Dumazet43815482010-04-29 11:01:49 +0000112 struct socket_wq *wq;
113
114 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 if (vcc_writable(sk)) {
Eric Dumazet43815482010-04-29 11:01:49 +0000117 wq = rcu_dereference(sk->sk_wq);
118 if (wq_has_sleeper(wq))
119 wake_up_interruptible(&wq->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +0800121 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123
Eric Dumazet43815482010-04-29 11:01:49 +0000124 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
127static struct proto vcc_proto = {
128 .name = "VCC",
129 .owner = THIS_MODULE,
130 .obj_size = sizeof(struct atm_vcc),
131};
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900132
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700133int vcc_create(struct net *net, struct socket *sock, int protocol, int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 struct sock *sk;
136 struct atm_vcc *vcc;
137
138 sock->sk = NULL;
139 if (sock->type == SOCK_STREAM)
140 return -EINVAL;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700141 sk = sk_alloc(net, family, GFP_KERNEL, &vcc_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (!sk)
143 return -ENOMEM;
144 sock_init_data(sock, sk);
145 sk->sk_state_change = vcc_def_wakeup;
146 sk->sk_write_space = vcc_write_space;
147
148 vcc = atm_sk(sk);
149 vcc->dev = NULL;
Joe Perchesa8147d72010-01-26 11:40:06 +0000150 memset(&vcc->local, 0, sizeof(struct sockaddr_atmsvc));
151 memset(&vcc->remote, 0, sizeof(struct sockaddr_atmsvc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 vcc->qos.txtp.max_sdu = 1 << 16; /* for meta VCs */
Eric Dumazet81e2a3d2009-06-17 19:06:12 -0700153 atomic_set(&sk->sk_wmem_alloc, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 atomic_set(&sk->sk_rmem_alloc, 0);
155 vcc->push = NULL;
156 vcc->pop = NULL;
157 vcc->push_oam = NULL;
158 vcc->vpi = vcc->vci = 0; /* no VCI/VPI yet */
159 vcc->atm_options = vcc->aal_options = 0;
160 sk->sk_destruct = vcc_sock_destruct;
161 return 0;
162}
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static void vcc_destroy_socket(struct sock *sk)
165{
166 struct atm_vcc *vcc = atm_sk(sk);
167 struct sk_buff *skb;
168
169 set_bit(ATM_VF_CLOSE, &vcc->flags);
170 clear_bit(ATM_VF_READY, &vcc->flags);
171 if (vcc->dev) {
172 if (vcc->dev->ops->close)
173 vcc->dev->ops->close(vcc);
174 if (vcc->push)
175 vcc->push(vcc, NULL); /* atmarpd has no push */
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
Joe Perchesa8147d72010-01-26 11:40:06 +0000178 atm_return(vcc, skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 kfree_skb(skb);
180 }
181
182 module_put(vcc->dev->ops->owner);
183 atm_dev_put(vcc->dev);
184 }
Chas Williams9301e322005-09-28 16:35:01 -0700185
186 vcc_remove_socket(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189int vcc_release(struct socket *sock)
190{
191 struct sock *sk = sock->sk;
192
193 if (sk) {
194 lock_sock(sk);
195 vcc_destroy_socket(sock->sk);
196 release_sock(sk);
197 sock_put(sk);
198 }
199
200 return 0;
201}
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203void vcc_release_async(struct atm_vcc *vcc, int reply)
204{
205 struct sock *sk = sk_atm(vcc);
206
207 set_bit(ATM_VF_CLOSE, &vcc->flags);
208 sk->sk_shutdown |= RCV_SHUTDOWN;
209 sk->sk_err = -reply;
210 clear_bit(ATM_VF_WAITING, &vcc->flags);
211 sk->sk_state_change(sk);
212}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213EXPORT_SYMBOL(vcc_release_async);
214
215
Stanislaw Gruszka64bf69d2005-11-29 16:16:41 -0800216void atm_dev_release_vccs(struct atm_dev *dev)
217{
218 int i;
219
220 write_lock_irq(&vcc_sklist_lock);
221 for (i = 0; i < VCC_HTABLE_SIZE; i++) {
222 struct hlist_head *head = &vcc_hash[i];
223 struct hlist_node *node, *tmp;
224 struct sock *s;
225 struct atm_vcc *vcc;
226
227 sk_for_each_safe(s, node, tmp, head) {
228 vcc = atm_sk(s);
229 if (vcc->dev == dev) {
230 vcc_release_async(vcc, -EPIPE);
231 sk_del_node_init(s);
232 }
233 }
234 }
235 write_unlock_irq(&vcc_sklist_lock);
236}
237
Joe Perchesa8147d72010-01-26 11:40:06 +0000238static int adjust_tp(struct atm_trafprm *tp, unsigned char aal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 int max_sdu;
241
Joe Perchesa8147d72010-01-26 11:40:06 +0000242 if (!tp->traffic_class)
243 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 switch (aal) {
Joe Perchesa8147d72010-01-26 11:40:06 +0000245 case ATM_AAL0:
246 max_sdu = ATM_CELL_SIZE-1;
247 break;
248 case ATM_AAL34:
249 max_sdu = ATM_MAX_AAL34_PDU;
250 break;
251 default:
252 pr_warning("AAL problems ... (%d)\n", aal);
253 /* fall through */
254 case ATM_AAL5:
255 max_sdu = ATM_MAX_AAL5_PDU;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000257 if (!tp->max_sdu)
258 tp->max_sdu = max_sdu;
259 else if (tp->max_sdu > max_sdu)
260 return -EINVAL;
261 if (!tp->max_cdv)
262 tp->max_cdv = ATM_MAX_CDV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return 0;
264}
265
Mitchell Blank Jr61c33e02008-06-17 16:20:06 -0700266static int check_ci(const struct atm_vcc *vcc, short vpi, int vci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Joe Perchesa8147d72010-01-26 11:40:06 +0000268 struct hlist_head *head = &vcc_hash[vci & (VCC_HTABLE_SIZE - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 struct hlist_node *node;
270 struct sock *s;
271 struct atm_vcc *walk;
272
273 sk_for_each(s, node, head) {
274 walk = atm_sk(s);
275 if (walk->dev != vcc->dev)
276 continue;
277 if (test_bit(ATM_VF_ADDR, &walk->flags) && walk->vpi == vpi &&
278 walk->vci == vci && ((walk->qos.txtp.traffic_class !=
279 ATM_NONE && vcc->qos.txtp.traffic_class != ATM_NONE) ||
280 (walk->qos.rxtp.traffic_class != ATM_NONE &&
281 vcc->qos.rxtp.traffic_class != ATM_NONE)))
282 return -EADDRINUSE;
283 }
284
285 /* allow VCCs with same VPI/VCI iff they don't collide on
286 TX/RX (but we may refuse such sharing for other reasons,
287 e.g. if protocol requires to have both channels) */
288
289 return 0;
290}
291
Mitchell Blank Jr61c33e02008-06-17 16:20:06 -0700292static int find_ci(const struct atm_vcc *vcc, short *vpi, int *vci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 static short p; /* poor man's per-device cache */
295 static int c;
296 short old_p;
297 int old_c;
298 int err;
299
300 if (*vpi != ATM_VPI_ANY && *vci != ATM_VCI_ANY) {
301 err = check_ci(vcc, *vpi, *vci);
302 return err;
303 }
304 /* last scan may have left values out of bounds for current device */
305 if (*vpi != ATM_VPI_ANY)
306 p = *vpi;
307 else if (p >= 1 << vcc->dev->ci_range.vpi_bits)
308 p = 0;
309 if (*vci != ATM_VCI_ANY)
310 c = *vci;
311 else if (c < ATM_NOT_RSV_VCI || c >= 1 << vcc->dev->ci_range.vci_bits)
312 c = ATM_NOT_RSV_VCI;
313 old_p = p;
314 old_c = c;
315 do {
316 if (!check_ci(vcc, p, c)) {
317 *vpi = p;
318 *vci = c;
319 return 0;
320 }
321 if (*vci == ATM_VCI_ANY) {
322 c++;
323 if (c >= 1 << vcc->dev->ci_range.vci_bits)
324 c = ATM_NOT_RSV_VCI;
325 }
326 if ((c == ATM_NOT_RSV_VCI || *vci != ATM_VCI_ANY) &&
327 *vpi == ATM_VPI_ANY) {
328 p++;
Joe Perchesa8147d72010-01-26 11:40:06 +0000329 if (p >= 1 << vcc->dev->ci_range.vpi_bits)
330 p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000332 } while (old_p != p || old_c != c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return -EADDRINUSE;
334}
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336static int __vcc_connect(struct atm_vcc *vcc, struct atm_dev *dev, short vpi,
337 int vci)
338{
339 struct sock *sk = sk_atm(vcc);
340 int error;
341
342 if ((vpi != ATM_VPI_UNSPEC && vpi != ATM_VPI_ANY &&
343 vpi >> dev->ci_range.vpi_bits) || (vci != ATM_VCI_UNSPEC &&
344 vci != ATM_VCI_ANY && vci >> dev->ci_range.vci_bits))
345 return -EINVAL;
346 if (vci > 0 && vci < ATM_NOT_RSV_VCI && !capable(CAP_NET_BIND_SERVICE))
347 return -EPERM;
Stanislaw Gruszka64bf69d2005-11-29 16:16:41 -0800348 error = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (!try_module_get(dev->ops->owner))
Stanislaw Gruszka64bf69d2005-11-29 16:16:41 -0800350 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 vcc->dev = dev;
352 write_lock_irq(&vcc_sklist_lock);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900353 if (test_bit(ATM_DF_REMOVED, &dev->flags) ||
Stanislaw Gruszka64bf69d2005-11-29 16:16:41 -0800354 (error = find_ci(vcc, &vpi, &vci))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 write_unlock_irq(&vcc_sklist_lock);
356 goto fail_module_put;
357 }
358 vcc->vpi = vpi;
359 vcc->vci = vci;
360 __vcc_insert_socket(sk);
361 write_unlock_irq(&vcc_sklist_lock);
362 switch (vcc->qos.aal) {
Joe Perchesa8147d72010-01-26 11:40:06 +0000363 case ATM_AAL0:
364 error = atm_init_aal0(vcc);
365 vcc->stats = &dev->stats.aal0;
366 break;
367 case ATM_AAL34:
368 error = atm_init_aal34(vcc);
369 vcc->stats = &dev->stats.aal34;
370 break;
371 case ATM_NO_AAL:
372 /* ATM_AAL5 is also used in the "0 for default" case */
373 vcc->qos.aal = ATM_AAL5;
374 /* fall through */
375 case ATM_AAL5:
376 error = atm_init_aal5(vcc);
377 vcc->stats = &dev->stats.aal5;
378 break;
379 default:
380 error = -EPROTOTYPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000382 if (!error)
383 error = adjust_tp(&vcc->qos.txtp, vcc->qos.aal);
384 if (!error)
385 error = adjust_tp(&vcc->qos.rxtp, vcc->qos.aal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (error)
387 goto fail;
Joe Perches99824462010-01-26 11:40:00 +0000388 pr_debug("VCC %d.%d, AAL %d\n", vpi, vci, vcc->qos.aal);
389 pr_debug(" TX: %d, PCR %d..%d, SDU %d\n",
390 vcc->qos.txtp.traffic_class,
391 vcc->qos.txtp.min_pcr,
392 vcc->qos.txtp.max_pcr,
393 vcc->qos.txtp.max_sdu);
394 pr_debug(" RX: %d, PCR %d..%d, SDU %d\n",
395 vcc->qos.rxtp.traffic_class,
396 vcc->qos.rxtp.min_pcr,
397 vcc->qos.rxtp.max_pcr,
398 vcc->qos.rxtp.max_sdu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 if (dev->ops->open) {
Joe Perchesa8147d72010-01-26 11:40:06 +0000401 error = dev->ops->open(vcc);
402 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 goto fail;
404 }
405 return 0;
406
407fail:
408 vcc_remove_socket(sk);
409fail_module_put:
410 module_put(dev->ops->owner);
411 /* ensure we get dev module ref count correct */
412 vcc->dev = NULL;
413 return error;
414}
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416int vcc_connect(struct socket *sock, int itf, short vpi, int vci)
417{
418 struct atm_dev *dev;
419 struct atm_vcc *vcc = ATM_SD(sock);
420 int error;
421
Joe Perches99824462010-01-26 11:40:00 +0000422 pr_debug("(vpi %d, vci %d)\n", vpi, vci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (sock->state == SS_CONNECTED)
424 return -EISCONN;
425 if (sock->state != SS_UNCONNECTED)
426 return -EINVAL;
427 if (!(vpi || vci))
428 return -EINVAL;
429
430 if (vpi != ATM_VPI_UNSPEC && vci != ATM_VCI_UNSPEC)
Joe Perchesa8147d72010-01-26 11:40:06 +0000431 clear_bit(ATM_VF_PARTIAL, &vcc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 else
Joe Perchesa8147d72010-01-26 11:40:06 +0000433 if (test_bit(ATM_VF_PARTIAL, &vcc->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return -EINVAL;
Joe Perches99824462010-01-26 11:40:00 +0000435 pr_debug("(TX: cl %d,bw %d-%d,sdu %d; "
436 "RX: cl %d,bw %d-%d,sdu %d,AAL %s%d)\n",
437 vcc->qos.txtp.traffic_class, vcc->qos.txtp.min_pcr,
438 vcc->qos.txtp.max_pcr, vcc->qos.txtp.max_sdu,
439 vcc->qos.rxtp.traffic_class, vcc->qos.rxtp.min_pcr,
Joe Perchesa8147d72010-01-26 11:40:06 +0000440 vcc->qos.rxtp.max_pcr, vcc->qos.rxtp.max_sdu,
Joe Perches99824462010-01-26 11:40:00 +0000441 vcc->qos.aal == ATM_AAL5 ? "" :
442 vcc->qos.aal == ATM_AAL0 ? "" : " ??? code ",
443 vcc->qos.aal == ATM_AAL0 ? 0 : vcc->qos.aal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
445 return -EBADFD;
446 if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
447 vcc->qos.rxtp.traffic_class == ATM_ANYCLASS)
448 return -EINVAL;
Mitchell Blank Jrc9933d02005-11-29 16:13:32 -0800449 if (likely(itf != ATM_ITF_ANY)) {
Joe Perchesa8147d72010-01-26 11:40:06 +0000450 dev = try_then_request_module(atm_dev_lookup(itf),
451 "atm-device-%d", itf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 dev = NULL;
Ingo Molnar57b47a52006-03-20 22:35:41 -0800454 mutex_lock(&atm_dev_mutex);
Mitchell Blank Jrc9933d02005-11-29 16:13:32 -0800455 if (!list_empty(&atm_devs)) {
Joe Perchesa8147d72010-01-26 11:40:06 +0000456 dev = list_entry(atm_devs.next,
457 struct atm_dev, dev_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 atm_dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
Ingo Molnar57b47a52006-03-20 22:35:41 -0800460 mutex_unlock(&atm_dev_mutex);
Mitchell Blank Jrc9933d02005-11-29 16:13:32 -0800461 }
462 if (!dev)
463 return -ENODEV;
464 error = __vcc_connect(vcc, dev, vpi, vci);
465 if (error) {
466 atm_dev_put(dev);
467 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469 if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC)
Joe Perchesa8147d72010-01-26 11:40:06 +0000470 set_bit(ATM_VF_PARTIAL, &vcc->flags);
471 if (test_bit(ATM_VF_READY, &ATM_SD(sock)->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 sock->state = SS_CONNECTED;
473 return 0;
474}
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476int vcc_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
477 size_t size, int flags)
478{
479 struct sock *sk = sock->sk;
480 struct atm_vcc *vcc;
481 struct sk_buff *skb;
482 int copied, error = -EINVAL;
483
484 if (sock->state != SS_CONNECTED)
485 return -ENOTCONN;
486 if (flags & ~MSG_DONTWAIT) /* only handle MSG_DONTWAIT */
487 return -EOPNOTSUPP;
488 vcc = ATM_SD(sock);
Joe Perchesa8147d72010-01-26 11:40:06 +0000489 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
490 test_bit(ATM_VF_CLOSE, &vcc->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 !test_bit(ATM_VF_READY, &vcc->flags))
492 return 0;
493
494 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &error);
495 if (!skb)
496 return error;
497
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900498 copied = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (copied > size) {
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900500 copied = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 msg->msg_flags |= MSG_TRUNC;
502 }
503
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900504 error = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
505 if (error)
506 return error;
Neil Horman3b885782009-10-12 13:26:31 -0700507 sock_recv_ts_and_drops(msg, sk, skb);
Joe Perches99824462010-01-26 11:40:00 +0000508 pr_debug("%d -= %d\n", atomic_read(&sk->sk_rmem_alloc), skb->truesize);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900509 atm_return(vcc, skb->truesize);
510 skb_free_datagram(sk, skb);
511 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514int vcc_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
515 size_t total_len)
516{
517 struct sock *sk = sock->sk;
518 DEFINE_WAIT(wait);
519 struct atm_vcc *vcc;
520 struct sk_buff *skb;
Joe Perchesa8147d72010-01-26 11:40:06 +0000521 int eff, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 const void __user *buff;
523 int size;
524
525 lock_sock(sk);
526 if (sock->state != SS_CONNECTED) {
527 error = -ENOTCONN;
528 goto out;
529 }
530 if (m->msg_name) {
531 error = -EISCONN;
532 goto out;
533 }
534 if (m->msg_iovlen != 1) {
535 error = -ENOSYS; /* fix this later @@@ */
536 goto out;
537 }
538 buff = m->msg_iov->iov_base;
539 size = m->msg_iov->iov_len;
540 vcc = ATM_SD(sock);
541 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
542 test_bit(ATM_VF_CLOSE, &vcc->flags) ||
543 !test_bit(ATM_VF_READY, &vcc->flags)) {
544 error = -EPIPE;
545 send_sig(SIGPIPE, current, 0);
546 goto out;
547 }
548 if (!size) {
549 error = 0;
550 goto out;
551 }
552 if (size < 0 || size > vcc->qos.txtp.max_sdu) {
553 error = -EMSGSIZE;
554 goto out;
555 }
Jesper Juhle49332b2005-05-01 08:59:08 -0700556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 eff = (size+3) & ~3; /* align to word boundary */
Eric Dumazetaa395142010-04-20 13:03:51 +0000558 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 error = 0;
Joe Perchesa8147d72010-01-26 11:40:06 +0000560 while (!(skb = alloc_tx(vcc, eff))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (m->msg_flags & MSG_DONTWAIT) {
562 error = -EAGAIN;
563 break;
564 }
565 schedule();
566 if (signal_pending(current)) {
567 error = -ERESTARTSYS;
568 break;
569 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000570 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
571 test_bit(ATM_VF_CLOSE, &vcc->flags) ||
572 !test_bit(ATM_VF_READY, &vcc->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 error = -EPIPE;
574 send_sig(SIGPIPE, current, 0);
575 break;
576 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000577 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000579 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (error)
581 goto out;
582 skb->dev = NULL; /* for paths shared with net_device interfaces */
583 ATM_SKB(skb)->atm_options = vcc->atm_options;
Joe Perchesa8147d72010-01-26 11:40:06 +0000584 if (copy_from_user(skb_put(skb, size), buff, size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 kfree_skb(skb);
586 error = -EFAULT;
587 goto out;
588 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000589 if (eff != size)
590 memset(skb->data + size, 0, eff-size);
591 error = vcc->dev->ops->send(vcc, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 error = error ? error : size;
593out:
594 release_sock(sk);
595 return error;
596}
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598unsigned int vcc_poll(struct file *file, struct socket *sock, poll_table *wait)
599{
600 struct sock *sk = sock->sk;
601 struct atm_vcc *vcc;
602 unsigned int mask;
603
Eric Dumazetaa395142010-04-20 13:03:51 +0000604 sock_poll_wait(file, sk_sleep(sk), wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 mask = 0;
606
607 vcc = ATM_SD(sock);
608
609 /* exceptional events */
610 if (sk->sk_err)
611 mask = POLLERR;
612
613 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
614 test_bit(ATM_VF_CLOSE, &vcc->flags))
615 mask |= POLLHUP;
616
617 /* readable? */
618 if (!skb_queue_empty(&sk->sk_receive_queue))
619 mask |= POLLIN | POLLRDNORM;
620
621 /* writable? */
622 if (sock->state == SS_CONNECTING &&
623 test_bit(ATM_VF_WAITING, &vcc->flags))
624 return mask;
625
626 if (vcc->qos.txtp.traffic_class != ATM_NONE &&
627 vcc_writable(sk))
628 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
629
630 return mask;
631}
632
Joe Perchesa8147d72010-01-26 11:40:06 +0000633static int atm_change_qos(struct atm_vcc *vcc, struct atm_qos *qos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
635 int error;
636
637 /*
638 * Don't let the QoS change the already connected AAL type nor the
639 * traffic class.
640 */
641 if (qos->aal != vcc->qos.aal ||
642 qos->rxtp.traffic_class != vcc->qos.rxtp.traffic_class ||
643 qos->txtp.traffic_class != vcc->qos.txtp.traffic_class)
644 return -EINVAL;
Joe Perchesa8147d72010-01-26 11:40:06 +0000645 error = adjust_tp(&qos->txtp, qos->aal);
646 if (!error)
647 error = adjust_tp(&qos->rxtp, qos->aal);
648 if (error)
649 return error;
650 if (!vcc->dev->ops->change_qos)
651 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (sk_atm(vcc)->sk_family == AF_ATMPVC)
Joe Perchesa8147d72010-01-26 11:40:06 +0000653 return vcc->dev->ops->change_qos(vcc, qos, ATM_MF_SET);
654 return svc_change_qos(vcc, qos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
Mitchell Blank Jr61c33e02008-06-17 16:20:06 -0700657static int check_tp(const struct atm_trafprm *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 /* @@@ Should be merged with adjust_tp */
Joe Perchesa8147d72010-01-26 11:40:06 +0000660 if (!tp->traffic_class || tp->traffic_class == ATM_ANYCLASS)
661 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (tp->traffic_class != ATM_UBR && !tp->min_pcr && !tp->pcr &&
Joe Perchesa8147d72010-01-26 11:40:06 +0000663 !tp->max_pcr)
664 return -EINVAL;
665 if (tp->min_pcr == ATM_MAX_PCR)
666 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (tp->min_pcr && tp->max_pcr && tp->max_pcr != ATM_MAX_PCR &&
Joe Perchesa8147d72010-01-26 11:40:06 +0000668 tp->min_pcr > tp->max_pcr)
669 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 /*
671 * We allow pcr to be outside [min_pcr,max_pcr], because later
672 * adjustment may still push it in the valid range.
673 */
674 return 0;
675}
676
Mitchell Blank Jr61c33e02008-06-17 16:20:06 -0700677static int check_qos(const struct atm_qos *qos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
679 int error;
680
681 if (!qos->txtp.traffic_class && !qos->rxtp.traffic_class)
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900682 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (qos->txtp.traffic_class != qos->rxtp.traffic_class &&
684 qos->txtp.traffic_class && qos->rxtp.traffic_class &&
685 qos->txtp.traffic_class != ATM_ANYCLASS &&
Joe Perchesa8147d72010-01-26 11:40:06 +0000686 qos->rxtp.traffic_class != ATM_ANYCLASS)
687 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 error = check_tp(&qos->txtp);
Joe Perchesa8147d72010-01-26 11:40:06 +0000689 if (error)
690 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return check_tp(&qos->rxtp);
692}
693
694int vcc_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700695 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
697 struct atm_vcc *vcc;
698 unsigned long value;
699 int error;
700
701 if (__SO_LEVEL_MATCH(optname, level) && optlen != __SO_SIZE(optname))
702 return -EINVAL;
703
704 vcc = ATM_SD(sock);
705 switch (optname) {
Joe Perchesa8147d72010-01-26 11:40:06 +0000706 case SO_ATMQOS:
707 {
708 struct atm_qos qos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Joe Perchesa8147d72010-01-26 11:40:06 +0000710 if (copy_from_user(&qos, optval, sizeof(qos)))
711 return -EFAULT;
712 error = check_qos(&qos);
713 if (error)
714 return error;
715 if (sock->state == SS_CONNECTED)
716 return atm_change_qos(vcc, &qos);
717 if (sock->state != SS_UNCONNECTED)
718 return -EBADFD;
719 vcc->qos = qos;
720 set_bit(ATM_VF_HASQOS, &vcc->flags);
721 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000723 case SO_SETCLP:
724 if (get_user(value, (unsigned long __user *)optval))
725 return -EFAULT;
726 if (value)
727 vcc->atm_options |= ATM_ATMOPT_CLP;
728 else
729 vcc->atm_options &= ~ATM_ATMOPT_CLP;
730 return 0;
731 default:
732 if (level == SOL_SOCKET)
733 return -EINVAL;
734 break;
735 }
736 if (!vcc->dev || !vcc->dev->ops->setsockopt)
737 return -EINVAL;
738 return vcc->dev->ops->setsockopt(vcc, level, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741int vcc_getsockopt(struct socket *sock, int level, int optname,
742 char __user *optval, int __user *optlen)
743{
744 struct atm_vcc *vcc;
745 int len;
746
747 if (get_user(len, optlen))
748 return -EFAULT;
749 if (__SO_LEVEL_MATCH(optname, level) && len != __SO_SIZE(optname))
750 return -EINVAL;
751
752 vcc = ATM_SD(sock);
753 switch (optname) {
Joe Perchesa8147d72010-01-26 11:40:06 +0000754 case SO_ATMQOS:
755 if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
756 return -EINVAL;
757 return copy_to_user(optval, &vcc->qos, sizeof(vcc->qos))
758 ? -EFAULT : 0;
759 case SO_SETCLP:
760 return put_user(vcc->atm_options & ATM_ATMOPT_CLP ? 1 : 0,
761 (unsigned long __user *)optval) ? -EFAULT : 0;
762 case SO_ATMPVC:
763 {
764 struct sockaddr_atmpvc pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Joe Perchesa8147d72010-01-26 11:40:06 +0000766 if (!vcc->dev || !test_bit(ATM_VF_ADDR, &vcc->flags))
767 return -ENOTCONN;
768 pvc.sap_family = AF_ATMPVC;
769 pvc.sap_addr.itf = vcc->dev->number;
770 pvc.sap_addr.vpi = vcc->vpi;
771 pvc.sap_addr.vci = vcc->vci;
772 return copy_to_user(optval, &pvc, sizeof(pvc)) ? -EFAULT : 0;
773 }
774 default:
775 if (level == SOL_SOCKET)
776 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 break;
778 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000779 if (!vcc->dev || !vcc->dev->ops->getsockopt)
780 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return vcc->dev->ops->getsockopt(vcc, level, optname, optval, len);
782}
783
784static int __init atm_init(void)
785{
786 int error;
787
Joe Perchesa8147d72010-01-26 11:40:06 +0000788 error = proto_register(&vcc_proto, 0);
789 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 goto out;
Joe Perchesa8147d72010-01-26 11:40:06 +0000791 error = atmpvc_init();
792 if (error < 0) {
Joe Perches99824462010-01-26 11:40:00 +0000793 pr_err("atmpvc_init() failed with %d\n", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 goto out_unregister_vcc_proto;
795 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000796 error = atmsvc_init();
797 if (error < 0) {
Joe Perches99824462010-01-26 11:40:00 +0000798 pr_err("atmsvc_init() failed with %d\n", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 goto out_atmpvc_exit;
800 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000801 error = atm_proc_init();
802 if (error < 0) {
Joe Perches99824462010-01-26 11:40:00 +0000803 pr_err("atm_proc_init() failed with %d\n", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 goto out_atmsvc_exit;
805 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000806 error = atm_sysfs_init();
807 if (error < 0) {
Joe Perches99824462010-01-26 11:40:00 +0000808 pr_err("atm_sysfs_init() failed with %d\n", error);
Roman Kagan656d98b2006-06-29 12:36:34 -0700809 goto out_atmproc_exit;
810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811out:
812 return error;
Roman Kagan656d98b2006-06-29 12:36:34 -0700813out_atmproc_exit:
814 atm_proc_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815out_atmsvc_exit:
816 atmsvc_exit();
817out_atmpvc_exit:
818 atmsvc_exit();
819out_unregister_vcc_proto:
820 proto_unregister(&vcc_proto);
821 goto out;
822}
823
824static void __exit atm_exit(void)
825{
826 atm_proc_exit();
Roman Kagan656d98b2006-06-29 12:36:34 -0700827 atm_sysfs_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 atmsvc_exit();
829 atmpvc_exit();
830 proto_unregister(&vcc_proto);
831}
832
Daniel Walker84ff6022007-02-05 18:04:06 -0800833subsys_initcall(atm_init);
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835module_exit(atm_exit);
836
837MODULE_LICENSE("GPL");
838MODULE_ALIAS_NETPROTO(PF_ATMPVC);
839MODULE_ALIAS_NETPROTO(PF_ATMSVC);