blob: 5589de7086af4eca7634e786918600a81cf6b09c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* net/atm/svc.c - ATM SVC sockets */
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
7#include <linux/string.h>
8#include <linux/net.h> /* struct socket, struct proto_ops */
9#include <linux/errno.h> /* error codes */
10#include <linux/kernel.h> /* printk */
11#include <linux/skbuff.h>
12#include <linux/wait.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010013#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/fcntl.h> /* O_NONBLOCK */
15#include <linux/init.h>
16#include <linux/atm.h> /* ATM stuff */
17#include <linux/atmsap.h>
18#include <linux/atmsvc.h>
19#include <linux/atmdev.h>
20#include <linux/bitops.h>
21#include <net/sock.h> /* for sock_no_* */
Joe Perchesb7d93712010-01-26 11:40:18 +000022#include <linux/uaccess.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040023#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include "resources.h"
26#include "common.h" /* common for PVCs and SVCs */
27#include "signaling.h"
28#include "addr.h"
29
Joe Perchesb7d93712010-01-26 11:40:18 +000030static int svc_create(struct net *net, struct socket *sock, int protocol,
31 int kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/*
34 * Note: since all this is still nicely synchronized with the signaling demon,
35 * there's no need to protect sleep loops with clis. If signaling is
36 * moved into the kernel, that would change.
37 */
38
39
Joe Perchesb7d93712010-01-26 11:40:18 +000040static int svc_shutdown(struct socket *sock, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 return 0;
43}
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static void svc_disconnect(struct atm_vcc *vcc)
46{
47 DEFINE_WAIT(wait);
48 struct sk_buff *skb;
49 struct sock *sk = sk_atm(vcc);
50
Joe Perchesb7d93712010-01-26 11:40:18 +000051 pr_debug("%p\n", vcc);
52 if (test_bit(ATM_VF_REGIS, &vcc->flags)) {
Joe Perchesb7d93712010-01-26 11:40:18 +000053 sigd_enq(vcc, as_close, NULL, NULL, NULL);
chas williams - CONTRACTORde713b52014-08-12 08:12:26 -040054 for (;;) {
55 prepare_to_wait(sk_sleep(sk), &wait, TASK_UNINTERRUPTIBLE);
56 if (test_bit(ATM_VF_RELEASED, &vcc->flags) || !sigd)
57 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 }
Eric Dumazetaa395142010-04-20 13:03:51 +000060 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 }
62 /* beware - socket is still in use by atmsigd until the last
63 as_indicate has been answered */
64 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
65 atm_return(vcc, skb->truesize);
Stephen Hemminger52240062007-08-28 15:22:09 -070066 pr_debug("LISTEN REL\n");
Joe Perchesb7d93712010-01-26 11:40:18 +000067 sigd_enq2(NULL, as_reject, vcc, NULL, NULL, &vcc->qos, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 dev_kfree_skb(skb);
69 }
70 clear_bit(ATM_VF_REGIS, &vcc->flags);
71 /* ... may retry later */
72}
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static int svc_release(struct socket *sock)
75{
76 struct sock *sk = sock->sk;
77 struct atm_vcc *vcc;
78
Joe Perchesb7d93712010-01-26 11:40:18 +000079 if (sk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 vcc = ATM_SD(sock);
Joe Perches99824462010-01-26 11:40:00 +000081 pr_debug("%p\n", vcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 clear_bit(ATM_VF_READY, &vcc->flags);
Joe Perchesb7d93712010-01-26 11:40:18 +000083 /*
84 * VCC pointer is used as a reference,
85 * so we must not free it (thereby subjecting it to re-use)
86 * before all pending connections are closed
87 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 svc_disconnect(vcc);
89 vcc_release(sock);
90 }
91 return 0;
92}
93
Joe Perchesb7d93712010-01-26 11:40:18 +000094static int svc_bind(struct socket *sock, struct sockaddr *sockaddr,
95 int sockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 DEFINE_WAIT(wait);
98 struct sock *sk = sock->sk;
99 struct sockaddr_atmsvc *addr;
100 struct atm_vcc *vcc;
101 int error;
102
103 if (sockaddr_len != sizeof(struct sockaddr_atmsvc))
104 return -EINVAL;
105 lock_sock(sk);
106 if (sock->state == SS_CONNECTED) {
107 error = -EISCONN;
108 goto out;
109 }
110 if (sock->state != SS_UNCONNECTED) {
111 error = -EINVAL;
112 goto out;
113 }
114 vcc = ATM_SD(sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 addr = (struct sockaddr_atmsvc *) sockaddr;
116 if (addr->sas_family != AF_ATMSVC) {
117 error = -EAFNOSUPPORT;
118 goto out;
119 }
Joe Perchesb7d93712010-01-26 11:40:18 +0000120 clear_bit(ATM_VF_BOUND, &vcc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 /* failing rebind will kill old binding */
122 /* @@@ check memory (de)allocation on rebind */
Joe Perchesb7d93712010-01-26 11:40:18 +0000123 if (!test_bit(ATM_VF_HASQOS, &vcc->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 error = -EBADFD;
125 goto out;
126 }
127 vcc->local = *addr;
128 set_bit(ATM_VF_WAITING, &vcc->flags);
Joe Perchesb7d93712010-01-26 11:40:18 +0000129 sigd_enq(vcc, as_bind, NULL, NULL, &vcc->local);
chas williams - CONTRACTORde713b52014-08-12 08:12:26 -0400130 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +0000131 prepare_to_wait(sk_sleep(sk), &wait, TASK_UNINTERRUPTIBLE);
chas williams - CONTRACTORde713b52014-08-12 08:12:26 -0400132 if (!test_bit(ATM_VF_WAITING, &vcc->flags) || !sigd)
133 break;
134 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000136 finish_wait(sk_sleep(sk), &wait);
Joe Perchesb7d93712010-01-26 11:40:18 +0000137 clear_bit(ATM_VF_REGIS, &vcc->flags); /* doesn't count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (!sigd) {
139 error = -EUNATCH;
140 goto out;
141 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900142 if (!sk->sk_err)
Joe Perchesb7d93712010-01-26 11:40:18 +0000143 set_bit(ATM_VF_BOUND, &vcc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 error = -sk->sk_err;
145out:
146 release_sock(sk);
147 return error;
148}
149
Joe Perchesb7d93712010-01-26 11:40:18 +0000150static int svc_connect(struct socket *sock, struct sockaddr *sockaddr,
151 int sockaddr_len, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 DEFINE_WAIT(wait);
154 struct sock *sk = sock->sk;
155 struct sockaddr_atmsvc *addr;
156 struct atm_vcc *vcc = ATM_SD(sock);
157 int error;
158
Joe Perchesb7d93712010-01-26 11:40:18 +0000159 pr_debug("%p\n", vcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 lock_sock(sk);
161 if (sockaddr_len != sizeof(struct sockaddr_atmsvc)) {
162 error = -EINVAL;
163 goto out;
164 }
165
166 switch (sock->state) {
167 default:
168 error = -EINVAL;
169 goto out;
170 case SS_CONNECTED:
171 error = -EISCONN;
172 goto out;
173 case SS_CONNECTING:
174 if (test_bit(ATM_VF_WAITING, &vcc->flags)) {
175 error = -EALREADY;
176 goto out;
177 }
178 sock->state = SS_UNCONNECTED;
179 if (sk->sk_err) {
180 error = -sk->sk_err;
181 goto out;
182 }
183 break;
184 case SS_UNCONNECTED:
185 addr = (struct sockaddr_atmsvc *) sockaddr;
186 if (addr->sas_family != AF_ATMSVC) {
187 error = -EAFNOSUPPORT;
188 goto out;
189 }
190 if (!test_bit(ATM_VF_HASQOS, &vcc->flags)) {
191 error = -EBADFD;
192 goto out;
193 }
194 if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
195 vcc->qos.rxtp.traffic_class == ATM_ANYCLASS) {
196 error = -EINVAL;
197 goto out;
198 }
199 if (!vcc->qos.txtp.traffic_class &&
200 !vcc->qos.rxtp.traffic_class) {
201 error = -EINVAL;
202 goto out;
203 }
204 vcc->remote = *addr;
205 set_bit(ATM_VF_WAITING, &vcc->flags);
Joe Perchesb7d93712010-01-26 11:40:18 +0000206 sigd_enq(vcc, as_connect, NULL, NULL, &vcc->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (flags & O_NONBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 sock->state = SS_CONNECTING;
209 error = -EINPROGRESS;
210 goto out;
211 }
212 error = 0;
chas williams - CONTRACTORde713b52014-08-12 08:12:26 -0400213 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
215 schedule();
216 if (!signal_pending(current)) {
Eric Dumazetaa395142010-04-20 13:03:51 +0000217 prepare_to_wait(sk_sleep(sk), &wait,
Joe Perchesb7d93712010-01-26 11:40:18 +0000218 TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 continue;
220 }
Stephen Hemminger52240062007-08-28 15:22:09 -0700221 pr_debug("*ABORT*\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 /*
223 * This is tricky:
224 * Kernel ---close--> Demon
225 * Kernel <--close--- Demon
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900226 * or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 * Kernel ---close--> Demon
228 * Kernel <--error--- Demon
229 * or
230 * Kernel ---close--> Demon
231 * Kernel <--okay---- Demon
232 * Kernel <--close--- Demon
233 */
Joe Perchesb7d93712010-01-26 11:40:18 +0000234 sigd_enq(vcc, as_close, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
Eric Dumazetaa395142010-04-20 13:03:51 +0000236 prepare_to_wait(sk_sleep(sk), &wait,
Joe Perchesb7d93712010-01-26 11:40:18 +0000237 TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 schedule();
239 }
240 if (!sk->sk_err)
Joe Perchesb7d93712010-01-26 11:40:18 +0000241 while (!test_bit(ATM_VF_RELEASED, &vcc->flags) &&
242 sigd) {
Eric Dumazetaa395142010-04-20 13:03:51 +0000243 prepare_to_wait(sk_sleep(sk), &wait,
Joe Perchesb7d93712010-01-26 11:40:18 +0000244 TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 schedule();
246 }
Joe Perchesb7d93712010-01-26 11:40:18 +0000247 clear_bit(ATM_VF_REGIS, &vcc->flags);
248 clear_bit(ATM_VF_RELEASED, &vcc->flags);
249 clear_bit(ATM_VF_CLOSE, &vcc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 /* we're gone now but may connect later */
251 error = -EINTR;
252 break;
253 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000254 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (error)
256 goto out;
257 if (!sigd) {
258 error = -EUNATCH;
259 goto out;
260 }
261 if (sk->sk_err) {
262 error = -sk->sk_err;
263 goto out;
264 }
265 }
Paul Bolle391296c2014-05-27 17:07:12 +0200266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 vcc->qos.txtp.max_pcr = SELECT_TOP_PCR(vcc->qos.txtp);
268 vcc->qos.txtp.pcr = 0;
269 vcc->qos.txtp.min_pcr = 0;
Paul Bolle391296c2014-05-27 17:07:12 +0200270
Joe Perchesb7d93712010-01-26 11:40:18 +0000271 error = vcc_connect(sock, vcc->itf, vcc->vpi, vcc->vci);
272 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 sock->state = SS_CONNECTED;
274 else
Joe Perchesb7d93712010-01-26 11:40:18 +0000275 (void)svc_disconnect(vcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276out:
277 release_sock(sk);
278 return error;
279}
280
Joe Perchesb7d93712010-01-26 11:40:18 +0000281static int svc_listen(struct socket *sock, int backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 DEFINE_WAIT(wait);
284 struct sock *sk = sock->sk;
285 struct atm_vcc *vcc = ATM_SD(sock);
286 int error;
287
Joe Perches99824462010-01-26 11:40:00 +0000288 pr_debug("%p\n", vcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 lock_sock(sk);
290 /* let server handle listen on unbound sockets */
Joe Perchesb7d93712010-01-26 11:40:18 +0000291 if (test_bit(ATM_VF_SESSION, &vcc->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 error = -EINVAL;
293 goto out;
294 }
Chas Williams17b24b32008-12-04 14:58:13 -0800295 if (test_bit(ATM_VF_LISTEN, &vcc->flags)) {
296 error = -EADDRINUSE;
297 goto out;
Joe Perchesb7d93712010-01-26 11:40:18 +0000298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 set_bit(ATM_VF_WAITING, &vcc->flags);
Joe Perchesb7d93712010-01-26 11:40:18 +0000300 sigd_enq(vcc, as_listen, NULL, NULL, &vcc->local);
chas williams - CONTRACTORde713b52014-08-12 08:12:26 -0400301 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +0000302 prepare_to_wait(sk_sleep(sk), &wait, TASK_UNINTERRUPTIBLE);
chas williams - CONTRACTORde713b52014-08-12 08:12:26 -0400303 if (!test_bit(ATM_VF_WAITING, &vcc->flags) || !sigd)
304 break;
305 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000307 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (!sigd) {
309 error = -EUNATCH;
310 goto out;
311 }
Joe Perchesb7d93712010-01-26 11:40:18 +0000312 set_bit(ATM_VF_LISTEN, &vcc->flags);
Chas Williams17b24b32008-12-04 14:58:13 -0800313 vcc_insert_socket(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 sk->sk_max_ack_backlog = backlog > 0 ? backlog : ATM_BACKLOG_DEFAULT;
315 error = -sk->sk_err;
316out:
317 release_sock(sk);
318 return error;
319}
320
David Howellscdfbabf2017-03-09 08:09:05 +0000321static int svc_accept(struct socket *sock, struct socket *newsock, int flags,
322 bool kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 struct sock *sk = sock->sk;
325 struct sk_buff *skb;
326 struct atmsvc_msg *msg;
327 struct atm_vcc *old_vcc = ATM_SD(sock);
328 struct atm_vcc *new_vcc;
329 int error;
330
331 lock_sock(sk);
332
David Howellscdfbabf2017-03-09 08:09:05 +0000333 error = svc_create(sock_net(sk), newsock, 0, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (error)
335 goto out;
336
337 new_vcc = ATM_SD(newsock);
338
Joe Perches99824462010-01-26 11:40:00 +0000339 pr_debug("%p -> %p\n", old_vcc, new_vcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 while (1) {
341 DEFINE_WAIT(wait);
342
Eric Dumazetaa395142010-04-20 13:03:51 +0000343 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 while (!(skb = skb_dequeue(&sk->sk_receive_queue)) &&
345 sigd) {
Joe Perchesb7d93712010-01-26 11:40:18 +0000346 if (test_bit(ATM_VF_RELEASED, &old_vcc->flags))
347 break;
348 if (test_bit(ATM_VF_CLOSE, &old_vcc->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 error = -sk->sk_err;
350 break;
351 }
352 if (flags & O_NONBLOCK) {
353 error = -EAGAIN;
354 break;
355 }
356 release_sock(sk);
357 schedule();
358 lock_sock(sk);
359 if (signal_pending(current)) {
360 error = -ERESTARTSYS;
361 break;
362 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000363 prepare_to_wait(sk_sleep(sk), &wait,
Joe Perchesb7d93712010-01-26 11:40:18 +0000364 TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000366 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (error)
368 goto out;
369 if (!skb) {
370 error = -EUNATCH;
371 goto out;
372 }
Joe Perchesb7d93712010-01-26 11:40:18 +0000373 msg = (struct atmsvc_msg *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 new_vcc->qos = msg->qos;
Joe Perchesb7d93712010-01-26 11:40:18 +0000375 set_bit(ATM_VF_HASQOS, &new_vcc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 new_vcc->remote = msg->svc;
377 new_vcc->local = msg->local;
378 new_vcc->sap = msg->sap;
379 error = vcc_connect(newsock, msg->pvc.sap_addr.itf,
Joe Perchesb7d93712010-01-26 11:40:18 +0000380 msg->pvc.sap_addr.vpi,
381 msg->pvc.sap_addr.vci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 dev_kfree_skb(skb);
383 sk->sk_ack_backlog--;
384 if (error) {
Joe Perchesb7d93712010-01-26 11:40:18 +0000385 sigd_enq2(NULL, as_reject, old_vcc, NULL, NULL,
386 &old_vcc->qos, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 error = error == -EAGAIN ? -EBUSY : error;
388 goto out;
389 }
390 /* wait should be short, so we ignore the non-blocking flag */
391 set_bit(ATM_VF_WAITING, &new_vcc->flags);
Joe Perchesb7d93712010-01-26 11:40:18 +0000392 sigd_enq(new_vcc, as_accept, old_vcc, NULL, NULL);
chas williams - CONTRACTORde713b52014-08-12 08:12:26 -0400393 for (;;) {
394 prepare_to_wait(sk_sleep(sk_atm(new_vcc)), &wait,
395 TASK_UNINTERRUPTIBLE);
396 if (!test_bit(ATM_VF_WAITING, &new_vcc->flags) || !sigd)
397 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 release_sock(sk);
399 schedule();
400 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000402 finish_wait(sk_sleep(sk_atm(new_vcc)), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (!sigd) {
404 error = -EUNATCH;
405 goto out;
406 }
407 if (!sk_atm(new_vcc)->sk_err)
408 break;
409 if (sk_atm(new_vcc)->sk_err != ERESTARTSYS) {
410 error = -sk_atm(new_vcc)->sk_err;
411 goto out;
412 }
413 }
414 newsock->state = SS_CONNECTED;
415out:
416 release_sock(sk);
417 return error;
418}
419
Joe Perchesb7d93712010-01-26 11:40:18 +0000420static int svc_getname(struct socket *sock, struct sockaddr *sockaddr,
421 int *sockaddr_len, int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 struct sockaddr_atmsvc *addr;
424
425 *sockaddr_len = sizeof(struct sockaddr_atmsvc);
426 addr = (struct sockaddr_atmsvc *) sockaddr;
Joe Perchesb7d93712010-01-26 11:40:18 +0000427 memcpy(addr, peer ? &ATM_SD(sock)->remote : &ATM_SD(sock)->local,
428 sizeof(struct sockaddr_atmsvc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return 0;
430}
431
Joe Perchesb7d93712010-01-26 11:40:18 +0000432int svc_change_qos(struct atm_vcc *vcc, struct atm_qos *qos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 struct sock *sk = sk_atm(vcc);
435 DEFINE_WAIT(wait);
436
437 set_bit(ATM_VF_WAITING, &vcc->flags);
Joe Perchesb7d93712010-01-26 11:40:18 +0000438 sigd_enq2(vcc, as_modify, NULL, NULL, &vcc->local, qos, 0);
chas williams - CONTRACTORde713b52014-08-12 08:12:26 -0400439 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +0000440 prepare_to_wait(sk_sleep(sk), &wait, TASK_UNINTERRUPTIBLE);
chas williams - CONTRACTORde713b52014-08-12 08:12:26 -0400441 if (!test_bit(ATM_VF_WAITING, &vcc->flags) ||
442 test_bit(ATM_VF_RELEASED, &vcc->flags) || !sigd) {
443 break;
444 }
445 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000447 finish_wait(sk_sleep(sk), &wait);
Joe Perchesb7d93712010-01-26 11:40:18 +0000448 if (!sigd)
449 return -EUNATCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return -sk->sk_err;
451}
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453static int svc_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700454 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 struct sock *sk = sock->sk;
457 struct atm_vcc *vcc = ATM_SD(sock);
458 int value, error = 0;
459
460 lock_sock(sk);
461 switch (optname) {
Joe Perchesb7d93712010-01-26 11:40:18 +0000462 case SO_ATMSAP:
463 if (level != SOL_ATM || optlen != sizeof(struct atm_sap)) {
464 error = -EINVAL;
465 goto out;
466 }
467 if (copy_from_user(&vcc->sap, optval, optlen)) {
468 error = -EFAULT;
469 goto out;
470 }
471 set_bit(ATM_VF_HASSAP, &vcc->flags);
472 break;
473 case SO_MULTIPOINT:
474 if (level != SOL_ATM || optlen != sizeof(int)) {
475 error = -EINVAL;
476 goto out;
477 }
478 if (get_user(value, (int __user *)optval)) {
479 error = -EFAULT;
480 goto out;
481 }
482 if (value == 1)
483 set_bit(ATM_VF_SESSION, &vcc->flags);
484 else if (value == 0)
485 clear_bit(ATM_VF_SESSION, &vcc->flags);
486 else
487 error = -EINVAL;
488 break;
489 default:
490 error = vcc_setsockopt(sock, level, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492
493out:
494 release_sock(sk);
495 return error;
496}
497
Joe Perchesb7d93712010-01-26 11:40:18 +0000498static int svc_getsockopt(struct socket *sock, int level, int optname,
499 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 struct sock *sk = sock->sk;
502 int error = 0, len;
503
504 lock_sock(sk);
505 if (!__SO_LEVEL_MATCH(optname, level) || optname != SO_ATMSAP) {
506 error = vcc_getsockopt(sock, level, optname, optval, optlen);
507 goto out;
508 }
509 if (get_user(len, optlen)) {
510 error = -EFAULT;
511 goto out;
512 }
513 if (len != sizeof(struct atm_sap)) {
514 error = -EINVAL;
515 goto out;
516 }
517 if (copy_to_user(optval, &ATM_SD(sock)->sap, sizeof(struct atm_sap))) {
518 error = -EFAULT;
519 goto out;
520 }
521out:
522 release_sock(sk);
523 return error;
524}
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526static int svc_addparty(struct socket *sock, struct sockaddr *sockaddr,
527 int sockaddr_len, int flags)
528{
529 DEFINE_WAIT(wait);
530 struct sock *sk = sock->sk;
531 struct atm_vcc *vcc = ATM_SD(sock);
532 int error;
533
534 lock_sock(sk);
535 set_bit(ATM_VF_WAITING, &vcc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 sigd_enq(vcc, as_addparty, NULL, NULL,
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900537 (struct sockaddr_atmsvc *) sockaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (flags & O_NONBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 error = -EINPROGRESS;
540 goto out;
541 }
Joe Perches99824462010-01-26 11:40:00 +0000542 pr_debug("added wait queue\n");
chas williams - CONTRACTORde713b52014-08-12 08:12:26 -0400543 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +0000544 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
chas williams - CONTRACTORde713b52014-08-12 08:12:26 -0400545 if (!test_bit(ATM_VF_WAITING, &vcc->flags) || !sigd)
546 break;
547 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000549 finish_wait(sk_sleep(sk), &wait);
Stefan Hajnoczic6852932016-05-18 17:42:13 -0700550 error = -xchg(&sk->sk_err_soft, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551out:
552 release_sock(sk);
553 return error;
554}
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556static int svc_dropparty(struct socket *sock, int ep_ref)
557{
558 DEFINE_WAIT(wait);
559 struct sock *sk = sock->sk;
560 struct atm_vcc *vcc = ATM_SD(sock);
561 int error;
562
563 lock_sock(sk);
564 set_bit(ATM_VF_WAITING, &vcc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 sigd_enq2(vcc, as_dropparty, NULL, NULL, NULL, NULL, ep_ref);
chas williams - CONTRACTORde713b52014-08-12 08:12:26 -0400566 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +0000567 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
chas williams - CONTRACTORde713b52014-08-12 08:12:26 -0400568 if (!test_bit(ATM_VF_WAITING, &vcc->flags) || !sigd)
569 break;
570 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000572 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (!sigd) {
574 error = -EUNATCH;
575 goto out;
576 }
Stefan Hajnoczic6852932016-05-18 17:42:13 -0700577 error = -xchg(&sk->sk_err_soft, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578out:
579 release_sock(sk);
580 return error;
581}
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583static int svc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
584{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900585 int error, ep_ref;
586 struct sockaddr_atmsvc sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 struct atm_vcc *vcc = ATM_SD(sock);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 switch (cmd) {
Joe Perchesb7d93712010-01-26 11:40:18 +0000590 case ATM_ADDPARTY:
591 if (!test_bit(ATM_VF_SESSION, &vcc->flags))
592 return -EINVAL;
593 if (copy_from_user(&sa, (void __user *) arg, sizeof(sa)))
594 return -EFAULT;
595 error = svc_addparty(sock, (struct sockaddr *)&sa, sizeof(sa),
596 0);
597 break;
598 case ATM_DROPPARTY:
599 if (!test_bit(ATM_VF_SESSION, &vcc->flags))
600 return -EINVAL;
601 if (copy_from_user(&ep_ref, (void __user *) arg, sizeof(int)))
602 return -EFAULT;
603 error = svc_dropparty(sock, ep_ref);
604 break;
605 default:
606 error = vcc_ioctl(sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608
609 return error;
610}
611
David Woodhouse8865c412008-12-03 22:12:38 -0800612#ifdef CONFIG_COMPAT
Joe Perchesb7d93712010-01-26 11:40:18 +0000613static int svc_compat_ioctl(struct socket *sock, unsigned int cmd,
614 unsigned long arg)
David Woodhouse8865c412008-12-03 22:12:38 -0800615{
616 /* The definition of ATM_ADDPARTY uses the size of struct atm_iobuf.
617 But actually it takes a struct sockaddr_atmsvc, which doesn't need
618 compat handling. So all we have to do is fix up cmd... */
619 if (cmd == COMPAT_ATM_ADDPARTY)
620 cmd = ATM_ADDPARTY;
621
622 if (cmd == ATM_ADDPARTY || cmd == ATM_DROPPARTY)
623 return svc_ioctl(sock, cmd, arg);
624 else
625 return vcc_compat_ioctl(sock, cmd, arg);
626}
627#endif /* CONFIG_COMPAT */
628
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800629static const struct proto_ops svc_proto_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 .family = PF_ATMSVC,
631 .owner = THIS_MODULE,
632
633 .release = svc_release,
634 .bind = svc_bind,
635 .connect = svc_connect,
636 .socketpair = sock_no_socketpair,
637 .accept = svc_accept,
638 .getname = svc_getname,
639 .poll = vcc_poll,
640 .ioctl = svc_ioctl,
David Woodhouse8865c412008-12-03 22:12:38 -0800641#ifdef CONFIG_COMPAT
642 .compat_ioctl = svc_compat_ioctl,
643#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 .listen = svc_listen,
645 .shutdown = svc_shutdown,
646 .setsockopt = svc_setsockopt,
647 .getsockopt = svc_getsockopt,
648 .sendmsg = vcc_sendmsg,
649 .recvmsg = vcc_recvmsg,
650 .mmap = sock_no_mmap,
651 .sendpage = sock_no_sendpage,
652};
653
654
Eric Paris3f378b62009-11-05 22:18:14 -0800655static int svc_create(struct net *net, struct socket *sock, int protocol,
656 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658 int error;
659
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800660 if (!net_eq(net, &init_net))
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700661 return -EAFNOSUPPORT;
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 sock->ops = &svc_proto_ops;
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500664 error = vcc_create(net, sock, protocol, AF_ATMSVC, kern);
Joe Perchesb7d93712010-01-26 11:40:18 +0000665 if (error)
666 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 ATM_SD(sock)->local.sas_family = AF_ATMSVC;
668 ATM_SD(sock)->remote.sas_family = AF_ATMSVC;
669 return 0;
670}
671
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +0000672static const struct net_proto_family svc_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 .family = PF_ATMSVC,
674 .create = svc_create,
675 .owner = THIS_MODULE,
676};
677
678
679/*
680 * Initialize the ATM SVC protocol family
681 */
682
683int __init atmsvc_init(void)
684{
685 return sock_register(&svc_family_ops);
686}
687
688void atmsvc_exit(void)
689{
690 sock_unregister(PF_ATMSVC);
691}