blob: c458adcbc1770d8b2cf1d2e4352cbb1698d894b9 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/* net/atm/svc.c - ATM SVC sockets */
3
4/* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
5
Joe Perches99824462010-01-26 11:40:00 +00006#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
8#include <linux/string.h>
9#include <linux/net.h> /* struct socket, struct proto_ops */
10#include <linux/errno.h> /* error codes */
11#include <linux/kernel.h> /* printk */
12#include <linux/skbuff.h>
13#include <linux/wait.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010014#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/fcntl.h> /* O_NONBLOCK */
16#include <linux/init.h>
17#include <linux/atm.h> /* ATM stuff */
18#include <linux/atmsap.h>
19#include <linux/atmsvc.h>
20#include <linux/atmdev.h>
21#include <linux/bitops.h>
22#include <net/sock.h> /* for sock_no_* */
Joe Perchesb7d93712010-01-26 11:40:18 +000023#include <linux/uaccess.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040024#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include "resources.h"
27#include "common.h" /* common for PVCs and SVCs */
28#include "signaling.h"
29#include "addr.h"
30
Joe Perchesb7d93712010-01-26 11:40:18 +000031static int svc_create(struct net *net, struct socket *sock, int protocol,
32 int kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/*
35 * Note: since all this is still nicely synchronized with the signaling demon,
36 * there's no need to protect sleep loops with clis. If signaling is
37 * moved into the kernel, that would change.
38 */
39
40
Joe Perchesb7d93712010-01-26 11:40:18 +000041static int svc_shutdown(struct socket *sock, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
43 return 0;
44}
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static void svc_disconnect(struct atm_vcc *vcc)
47{
48 DEFINE_WAIT(wait);
49 struct sk_buff *skb;
50 struct sock *sk = sk_atm(vcc);
51
Joe Perchesb7d93712010-01-26 11:40:18 +000052 pr_debug("%p\n", vcc);
53 if (test_bit(ATM_VF_REGIS, &vcc->flags)) {
Joe Perchesb7d93712010-01-26 11:40:18 +000054 sigd_enq(vcc, as_close, NULL, NULL, NULL);
chas williams - CONTRACTORde713b52014-08-12 08:12:26 -040055 for (;;) {
56 prepare_to_wait(sk_sleep(sk), &wait, TASK_UNINTERRUPTIBLE);
57 if (test_bit(ATM_VF_RELEASED, &vcc->flags) || !sigd)
58 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 }
Eric Dumazetaa395142010-04-20 13:03:51 +000061 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 }
63 /* beware - socket is still in use by atmsigd until the last
64 as_indicate has been answered */
65 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
66 atm_return(vcc, skb->truesize);
Stephen Hemminger52240062007-08-28 15:22:09 -070067 pr_debug("LISTEN REL\n");
Joe Perchesb7d93712010-01-26 11:40:18 +000068 sigd_enq2(NULL, as_reject, vcc, NULL, NULL, &vcc->qos, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 dev_kfree_skb(skb);
70 }
71 clear_bit(ATM_VF_REGIS, &vcc->flags);
72 /* ... may retry later */
73}
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static int svc_release(struct socket *sock)
76{
77 struct sock *sk = sock->sk;
78 struct atm_vcc *vcc;
79
Joe Perchesb7d93712010-01-26 11:40:18 +000080 if (sk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 vcc = ATM_SD(sock);
Joe Perches99824462010-01-26 11:40:00 +000082 pr_debug("%p\n", vcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 clear_bit(ATM_VF_READY, &vcc->flags);
Joe Perchesb7d93712010-01-26 11:40:18 +000084 /*
85 * VCC pointer is used as a reference,
86 * so we must not free it (thereby subjecting it to re-use)
87 * before all pending connections are closed
88 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 svc_disconnect(vcc);
90 vcc_release(sock);
91 }
92 return 0;
93}
94
Joe Perchesb7d93712010-01-26 11:40:18 +000095static int svc_bind(struct socket *sock, struct sockaddr *sockaddr,
96 int sockaddr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 DEFINE_WAIT(wait);
99 struct sock *sk = sock->sk;
100 struct sockaddr_atmsvc *addr;
101 struct atm_vcc *vcc;
102 int error;
103
104 if (sockaddr_len != sizeof(struct sockaddr_atmsvc))
105 return -EINVAL;
106 lock_sock(sk);
107 if (sock->state == SS_CONNECTED) {
108 error = -EISCONN;
109 goto out;
110 }
111 if (sock->state != SS_UNCONNECTED) {
112 error = -EINVAL;
113 goto out;
114 }
115 vcc = ATM_SD(sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 addr = (struct sockaddr_atmsvc *) sockaddr;
117 if (addr->sas_family != AF_ATMSVC) {
118 error = -EAFNOSUPPORT;
119 goto out;
120 }
Joe Perchesb7d93712010-01-26 11:40:18 +0000121 clear_bit(ATM_VF_BOUND, &vcc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 /* failing rebind will kill old binding */
123 /* @@@ check memory (de)allocation on rebind */
Joe Perchesb7d93712010-01-26 11:40:18 +0000124 if (!test_bit(ATM_VF_HASQOS, &vcc->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 error = -EBADFD;
126 goto out;
127 }
128 vcc->local = *addr;
129 set_bit(ATM_VF_WAITING, &vcc->flags);
Joe Perchesb7d93712010-01-26 11:40:18 +0000130 sigd_enq(vcc, as_bind, NULL, NULL, &vcc->local);
chas williams - CONTRACTORde713b52014-08-12 08:12:26 -0400131 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +0000132 prepare_to_wait(sk_sleep(sk), &wait, TASK_UNINTERRUPTIBLE);
chas williams - CONTRACTORde713b52014-08-12 08:12:26 -0400133 if (!test_bit(ATM_VF_WAITING, &vcc->flags) || !sigd)
134 break;
135 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000137 finish_wait(sk_sleep(sk), &wait);
Joe Perchesb7d93712010-01-26 11:40:18 +0000138 clear_bit(ATM_VF_REGIS, &vcc->flags); /* doesn't count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if (!sigd) {
140 error = -EUNATCH;
141 goto out;
142 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900143 if (!sk->sk_err)
Joe Perchesb7d93712010-01-26 11:40:18 +0000144 set_bit(ATM_VF_BOUND, &vcc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 error = -sk->sk_err;
146out:
147 release_sock(sk);
148 return error;
149}
150
Joe Perchesb7d93712010-01-26 11:40:18 +0000151static int svc_connect(struct socket *sock, struct sockaddr *sockaddr,
152 int sockaddr_len, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 DEFINE_WAIT(wait);
155 struct sock *sk = sock->sk;
156 struct sockaddr_atmsvc *addr;
157 struct atm_vcc *vcc = ATM_SD(sock);
158 int error;
159
Joe Perchesb7d93712010-01-26 11:40:18 +0000160 pr_debug("%p\n", vcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 lock_sock(sk);
162 if (sockaddr_len != sizeof(struct sockaddr_atmsvc)) {
163 error = -EINVAL;
164 goto out;
165 }
166
167 switch (sock->state) {
168 default:
169 error = -EINVAL;
170 goto out;
171 case SS_CONNECTED:
172 error = -EISCONN;
173 goto out;
174 case SS_CONNECTING:
175 if (test_bit(ATM_VF_WAITING, &vcc->flags)) {
176 error = -EALREADY;
177 goto out;
178 }
179 sock->state = SS_UNCONNECTED;
180 if (sk->sk_err) {
181 error = -sk->sk_err;
182 goto out;
183 }
184 break;
185 case SS_UNCONNECTED:
186 addr = (struct sockaddr_atmsvc *) sockaddr;
187 if (addr->sas_family != AF_ATMSVC) {
188 error = -EAFNOSUPPORT;
189 goto out;
190 }
191 if (!test_bit(ATM_VF_HASQOS, &vcc->flags)) {
192 error = -EBADFD;
193 goto out;
194 }
195 if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
196 vcc->qos.rxtp.traffic_class == ATM_ANYCLASS) {
197 error = -EINVAL;
198 goto out;
199 }
200 if (!vcc->qos.txtp.traffic_class &&
201 !vcc->qos.rxtp.traffic_class) {
202 error = -EINVAL;
203 goto out;
204 }
205 vcc->remote = *addr;
206 set_bit(ATM_VF_WAITING, &vcc->flags);
Joe Perchesb7d93712010-01-26 11:40:18 +0000207 sigd_enq(vcc, as_connect, NULL, NULL, &vcc->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (flags & O_NONBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 sock->state = SS_CONNECTING;
210 error = -EINPROGRESS;
211 goto out;
212 }
213 error = 0;
chas williams - CONTRACTORde713b52014-08-12 08:12:26 -0400214 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
216 schedule();
217 if (!signal_pending(current)) {
Eric Dumazetaa395142010-04-20 13:03:51 +0000218 prepare_to_wait(sk_sleep(sk), &wait,
Joe Perchesb7d93712010-01-26 11:40:18 +0000219 TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 continue;
221 }
Stephen Hemminger52240062007-08-28 15:22:09 -0700222 pr_debug("*ABORT*\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 /*
224 * This is tricky:
225 * Kernel ---close--> Demon
226 * Kernel <--close--- Demon
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900227 * or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 * Kernel ---close--> Demon
229 * Kernel <--error--- Demon
230 * or
231 * Kernel ---close--> Demon
232 * Kernel <--okay---- Demon
233 * Kernel <--close--- Demon
234 */
Joe Perchesb7d93712010-01-26 11:40:18 +0000235 sigd_enq(vcc, as_close, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 while (test_bit(ATM_VF_WAITING, &vcc->flags) && sigd) {
Eric Dumazetaa395142010-04-20 13:03:51 +0000237 prepare_to_wait(sk_sleep(sk), &wait,
Joe Perchesb7d93712010-01-26 11:40:18 +0000238 TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 schedule();
240 }
241 if (!sk->sk_err)
Joe Perchesb7d93712010-01-26 11:40:18 +0000242 while (!test_bit(ATM_VF_RELEASED, &vcc->flags) &&
243 sigd) {
Eric Dumazetaa395142010-04-20 13:03:51 +0000244 prepare_to_wait(sk_sleep(sk), &wait,
Joe Perchesb7d93712010-01-26 11:40:18 +0000245 TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 schedule();
247 }
Joe Perchesb7d93712010-01-26 11:40:18 +0000248 clear_bit(ATM_VF_REGIS, &vcc->flags);
249 clear_bit(ATM_VF_RELEASED, &vcc->flags);
250 clear_bit(ATM_VF_CLOSE, &vcc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 /* we're gone now but may connect later */
252 error = -EINTR;
253 break;
254 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000255 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (error)
257 goto out;
258 if (!sigd) {
259 error = -EUNATCH;
260 goto out;
261 }
262 if (sk->sk_err) {
263 error = -sk->sk_err;
264 goto out;
265 }
266 }
Paul Bolle391296c2014-05-27 17:07:12 +0200267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 vcc->qos.txtp.max_pcr = SELECT_TOP_PCR(vcc->qos.txtp);
269 vcc->qos.txtp.pcr = 0;
270 vcc->qos.txtp.min_pcr = 0;
Paul Bolle391296c2014-05-27 17:07:12 +0200271
Joe Perchesb7d93712010-01-26 11:40:18 +0000272 error = vcc_connect(sock, vcc->itf, vcc->vpi, vcc->vci);
273 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 sock->state = SS_CONNECTED;
275 else
Joe Perchesb7d93712010-01-26 11:40:18 +0000276 (void)svc_disconnect(vcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277out:
278 release_sock(sk);
279 return error;
280}
281
Joe Perchesb7d93712010-01-26 11:40:18 +0000282static int svc_listen(struct socket *sock, int backlog)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 DEFINE_WAIT(wait);
285 struct sock *sk = sock->sk;
286 struct atm_vcc *vcc = ATM_SD(sock);
287 int error;
288
Joe Perches99824462010-01-26 11:40:00 +0000289 pr_debug("%p\n", vcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 lock_sock(sk);
291 /* let server handle listen on unbound sockets */
Joe Perchesb7d93712010-01-26 11:40:18 +0000292 if (test_bit(ATM_VF_SESSION, &vcc->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 error = -EINVAL;
294 goto out;
295 }
Chas Williams17b24b32008-12-04 14:58:13 -0800296 if (test_bit(ATM_VF_LISTEN, &vcc->flags)) {
297 error = -EADDRINUSE;
298 goto out;
Joe Perchesb7d93712010-01-26 11:40:18 +0000299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 set_bit(ATM_VF_WAITING, &vcc->flags);
Joe Perchesb7d93712010-01-26 11:40:18 +0000301 sigd_enq(vcc, as_listen, NULL, NULL, &vcc->local);
chas williams - CONTRACTORde713b52014-08-12 08:12:26 -0400302 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +0000303 prepare_to_wait(sk_sleep(sk), &wait, TASK_UNINTERRUPTIBLE);
chas williams - CONTRACTORde713b52014-08-12 08:12:26 -0400304 if (!test_bit(ATM_VF_WAITING, &vcc->flags) || !sigd)
305 break;
306 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000308 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (!sigd) {
310 error = -EUNATCH;
311 goto out;
312 }
Joe Perchesb7d93712010-01-26 11:40:18 +0000313 set_bit(ATM_VF_LISTEN, &vcc->flags);
Chas Williams17b24b32008-12-04 14:58:13 -0800314 vcc_insert_socket(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 sk->sk_max_ack_backlog = backlog > 0 ? backlog : ATM_BACKLOG_DEFAULT;
316 error = -sk->sk_err;
317out:
318 release_sock(sk);
319 return error;
320}
321
David Howellscdfbabf2017-03-09 08:09:05 +0000322static int svc_accept(struct socket *sock, struct socket *newsock, int flags,
323 bool kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 struct sock *sk = sock->sk;
326 struct sk_buff *skb;
327 struct atmsvc_msg *msg;
328 struct atm_vcc *old_vcc = ATM_SD(sock);
329 struct atm_vcc *new_vcc;
330 int error;
331
332 lock_sock(sk);
333
David Howellscdfbabf2017-03-09 08:09:05 +0000334 error = svc_create(sock_net(sk), newsock, 0, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (error)
336 goto out;
337
338 new_vcc = ATM_SD(newsock);
339
Joe Perches99824462010-01-26 11:40:00 +0000340 pr_debug("%p -> %p\n", old_vcc, new_vcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 while (1) {
342 DEFINE_WAIT(wait);
343
Eric Dumazetaa395142010-04-20 13:03:51 +0000344 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 while (!(skb = skb_dequeue(&sk->sk_receive_queue)) &&
346 sigd) {
Joe Perchesb7d93712010-01-26 11:40:18 +0000347 if (test_bit(ATM_VF_RELEASED, &old_vcc->flags))
348 break;
349 if (test_bit(ATM_VF_CLOSE, &old_vcc->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 error = -sk->sk_err;
351 break;
352 }
353 if (flags & O_NONBLOCK) {
354 error = -EAGAIN;
355 break;
356 }
357 release_sock(sk);
358 schedule();
359 lock_sock(sk);
360 if (signal_pending(current)) {
361 error = -ERESTARTSYS;
362 break;
363 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000364 prepare_to_wait(sk_sleep(sk), &wait,
Joe Perchesb7d93712010-01-26 11:40:18 +0000365 TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000367 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (error)
369 goto out;
370 if (!skb) {
371 error = -EUNATCH;
372 goto out;
373 }
Joe Perchesb7d93712010-01-26 11:40:18 +0000374 msg = (struct atmsvc_msg *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 new_vcc->qos = msg->qos;
Joe Perchesb7d93712010-01-26 11:40:18 +0000376 set_bit(ATM_VF_HASQOS, &new_vcc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 new_vcc->remote = msg->svc;
378 new_vcc->local = msg->local;
379 new_vcc->sap = msg->sap;
380 error = vcc_connect(newsock, msg->pvc.sap_addr.itf,
Joe Perchesb7d93712010-01-26 11:40:18 +0000381 msg->pvc.sap_addr.vpi,
382 msg->pvc.sap_addr.vci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 dev_kfree_skb(skb);
384 sk->sk_ack_backlog--;
385 if (error) {
Joe Perchesb7d93712010-01-26 11:40:18 +0000386 sigd_enq2(NULL, as_reject, old_vcc, NULL, NULL,
387 &old_vcc->qos, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 error = error == -EAGAIN ? -EBUSY : error;
389 goto out;
390 }
391 /* wait should be short, so we ignore the non-blocking flag */
392 set_bit(ATM_VF_WAITING, &new_vcc->flags);
Joe Perchesb7d93712010-01-26 11:40:18 +0000393 sigd_enq(new_vcc, as_accept, old_vcc, NULL, NULL);
chas williams - CONTRACTORde713b52014-08-12 08:12:26 -0400394 for (;;) {
395 prepare_to_wait(sk_sleep(sk_atm(new_vcc)), &wait,
396 TASK_UNINTERRUPTIBLE);
397 if (!test_bit(ATM_VF_WAITING, &new_vcc->flags) || !sigd)
398 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 release_sock(sk);
400 schedule();
401 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000403 finish_wait(sk_sleep(sk_atm(new_vcc)), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (!sigd) {
405 error = -EUNATCH;
406 goto out;
407 }
408 if (!sk_atm(new_vcc)->sk_err)
409 break;
410 if (sk_atm(new_vcc)->sk_err != ERESTARTSYS) {
411 error = -sk_atm(new_vcc)->sk_err;
412 goto out;
413 }
414 }
415 newsock->state = SS_CONNECTED;
416out:
417 release_sock(sk);
418 return error;
419}
420
Joe Perchesb7d93712010-01-26 11:40:18 +0000421static int svc_getname(struct socket *sock, struct sockaddr *sockaddr,
422 int *sockaddr_len, int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 struct sockaddr_atmsvc *addr;
425
426 *sockaddr_len = sizeof(struct sockaddr_atmsvc);
427 addr = (struct sockaddr_atmsvc *) sockaddr;
Joe Perchesb7d93712010-01-26 11:40:18 +0000428 memcpy(addr, peer ? &ATM_SD(sock)->remote : &ATM_SD(sock)->local,
429 sizeof(struct sockaddr_atmsvc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return 0;
431}
432
Joe Perchesb7d93712010-01-26 11:40:18 +0000433int svc_change_qos(struct atm_vcc *vcc, struct atm_qos *qos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
435 struct sock *sk = sk_atm(vcc);
436 DEFINE_WAIT(wait);
437
438 set_bit(ATM_VF_WAITING, &vcc->flags);
Joe Perchesb7d93712010-01-26 11:40:18 +0000439 sigd_enq2(vcc, as_modify, NULL, NULL, &vcc->local, qos, 0);
chas williams - CONTRACTORde713b52014-08-12 08:12:26 -0400440 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +0000441 prepare_to_wait(sk_sleep(sk), &wait, TASK_UNINTERRUPTIBLE);
chas williams - CONTRACTORde713b52014-08-12 08:12:26 -0400442 if (!test_bit(ATM_VF_WAITING, &vcc->flags) ||
443 test_bit(ATM_VF_RELEASED, &vcc->flags) || !sigd) {
444 break;
445 }
446 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000448 finish_wait(sk_sleep(sk), &wait);
Joe Perchesb7d93712010-01-26 11:40:18 +0000449 if (!sigd)
450 return -EUNATCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return -sk->sk_err;
452}
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454static int svc_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700455 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 struct sock *sk = sock->sk;
458 struct atm_vcc *vcc = ATM_SD(sock);
459 int value, error = 0;
460
461 lock_sock(sk);
462 switch (optname) {
Joe Perchesb7d93712010-01-26 11:40:18 +0000463 case SO_ATMSAP:
464 if (level != SOL_ATM || optlen != sizeof(struct atm_sap)) {
465 error = -EINVAL;
466 goto out;
467 }
468 if (copy_from_user(&vcc->sap, optval, optlen)) {
469 error = -EFAULT;
470 goto out;
471 }
472 set_bit(ATM_VF_HASSAP, &vcc->flags);
473 break;
474 case SO_MULTIPOINT:
475 if (level != SOL_ATM || optlen != sizeof(int)) {
476 error = -EINVAL;
477 goto out;
478 }
479 if (get_user(value, (int __user *)optval)) {
480 error = -EFAULT;
481 goto out;
482 }
483 if (value == 1)
484 set_bit(ATM_VF_SESSION, &vcc->flags);
485 else if (value == 0)
486 clear_bit(ATM_VF_SESSION, &vcc->flags);
487 else
488 error = -EINVAL;
489 break;
490 default:
491 error = vcc_setsockopt(sock, level, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493
494out:
495 release_sock(sk);
496 return error;
497}
498
Joe Perchesb7d93712010-01-26 11:40:18 +0000499static int svc_getsockopt(struct socket *sock, int level, int optname,
500 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 struct sock *sk = sock->sk;
503 int error = 0, len;
504
505 lock_sock(sk);
506 if (!__SO_LEVEL_MATCH(optname, level) || optname != SO_ATMSAP) {
507 error = vcc_getsockopt(sock, level, optname, optval, optlen);
508 goto out;
509 }
510 if (get_user(len, optlen)) {
511 error = -EFAULT;
512 goto out;
513 }
514 if (len != sizeof(struct atm_sap)) {
515 error = -EINVAL;
516 goto out;
517 }
518 if (copy_to_user(optval, &ATM_SD(sock)->sap, sizeof(struct atm_sap))) {
519 error = -EFAULT;
520 goto out;
521 }
522out:
523 release_sock(sk);
524 return error;
525}
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527static int svc_addparty(struct socket *sock, struct sockaddr *sockaddr,
528 int sockaddr_len, int flags)
529{
530 DEFINE_WAIT(wait);
531 struct sock *sk = sock->sk;
532 struct atm_vcc *vcc = ATM_SD(sock);
533 int error;
534
535 lock_sock(sk);
536 set_bit(ATM_VF_WAITING, &vcc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 sigd_enq(vcc, as_addparty, NULL, NULL,
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900538 (struct sockaddr_atmsvc *) sockaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (flags & O_NONBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 error = -EINPROGRESS;
541 goto out;
542 }
Joe Perches99824462010-01-26 11:40:00 +0000543 pr_debug("added wait queue\n");
chas williams - CONTRACTORde713b52014-08-12 08:12:26 -0400544 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +0000545 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
chas williams - CONTRACTORde713b52014-08-12 08:12:26 -0400546 if (!test_bit(ATM_VF_WAITING, &vcc->flags) || !sigd)
547 break;
548 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000550 finish_wait(sk_sleep(sk), &wait);
Stefan Hajnoczic6852932016-05-18 17:42:13 -0700551 error = -xchg(&sk->sk_err_soft, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552out:
553 release_sock(sk);
554 return error;
555}
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557static int svc_dropparty(struct socket *sock, int ep_ref)
558{
559 DEFINE_WAIT(wait);
560 struct sock *sk = sock->sk;
561 struct atm_vcc *vcc = ATM_SD(sock);
562 int error;
563
564 lock_sock(sk);
565 set_bit(ATM_VF_WAITING, &vcc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 sigd_enq2(vcc, as_dropparty, NULL, NULL, NULL, NULL, ep_ref);
chas williams - CONTRACTORde713b52014-08-12 08:12:26 -0400567 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +0000568 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
chas williams - CONTRACTORde713b52014-08-12 08:12:26 -0400569 if (!test_bit(ATM_VF_WAITING, &vcc->flags) || !sigd)
570 break;
571 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
Eric Dumazetaa395142010-04-20 13:03:51 +0000573 finish_wait(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (!sigd) {
575 error = -EUNATCH;
576 goto out;
577 }
Stefan Hajnoczic6852932016-05-18 17:42:13 -0700578 error = -xchg(&sk->sk_err_soft, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579out:
580 release_sock(sk);
581 return error;
582}
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584static int svc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
585{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900586 int error, ep_ref;
587 struct sockaddr_atmsvc sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 struct atm_vcc *vcc = ATM_SD(sock);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 switch (cmd) {
Joe Perchesb7d93712010-01-26 11:40:18 +0000591 case ATM_ADDPARTY:
592 if (!test_bit(ATM_VF_SESSION, &vcc->flags))
593 return -EINVAL;
594 if (copy_from_user(&sa, (void __user *) arg, sizeof(sa)))
595 return -EFAULT;
596 error = svc_addparty(sock, (struct sockaddr *)&sa, sizeof(sa),
597 0);
598 break;
599 case ATM_DROPPARTY:
600 if (!test_bit(ATM_VF_SESSION, &vcc->flags))
601 return -EINVAL;
602 if (copy_from_user(&ep_ref, (void __user *) arg, sizeof(int)))
603 return -EFAULT;
604 error = svc_dropparty(sock, ep_ref);
605 break;
606 default:
607 error = vcc_ioctl(sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609
610 return error;
611}
612
David Woodhouse8865c412008-12-03 22:12:38 -0800613#ifdef CONFIG_COMPAT
Joe Perchesb7d93712010-01-26 11:40:18 +0000614static int svc_compat_ioctl(struct socket *sock, unsigned int cmd,
615 unsigned long arg)
David Woodhouse8865c412008-12-03 22:12:38 -0800616{
617 /* The definition of ATM_ADDPARTY uses the size of struct atm_iobuf.
618 But actually it takes a struct sockaddr_atmsvc, which doesn't need
619 compat handling. So all we have to do is fix up cmd... */
620 if (cmd == COMPAT_ATM_ADDPARTY)
621 cmd = ATM_ADDPARTY;
622
623 if (cmd == ATM_ADDPARTY || cmd == ATM_DROPPARTY)
624 return svc_ioctl(sock, cmd, arg);
625 else
626 return vcc_compat_ioctl(sock, cmd, arg);
627}
628#endif /* CONFIG_COMPAT */
629
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800630static const struct proto_ops svc_proto_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 .family = PF_ATMSVC,
632 .owner = THIS_MODULE,
633
634 .release = svc_release,
635 .bind = svc_bind,
636 .connect = svc_connect,
637 .socketpair = sock_no_socketpair,
638 .accept = svc_accept,
639 .getname = svc_getname,
640 .poll = vcc_poll,
641 .ioctl = svc_ioctl,
David Woodhouse8865c412008-12-03 22:12:38 -0800642#ifdef CONFIG_COMPAT
643 .compat_ioctl = svc_compat_ioctl,
644#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 .listen = svc_listen,
646 .shutdown = svc_shutdown,
647 .setsockopt = svc_setsockopt,
648 .getsockopt = svc_getsockopt,
649 .sendmsg = vcc_sendmsg,
650 .recvmsg = vcc_recvmsg,
651 .mmap = sock_no_mmap,
652 .sendpage = sock_no_sendpage,
653};
654
655
Eric Paris3f378b62009-11-05 22:18:14 -0800656static int svc_create(struct net *net, struct socket *sock, int protocol,
657 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 int error;
660
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800661 if (!net_eq(net, &init_net))
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700662 return -EAFNOSUPPORT;
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 sock->ops = &svc_proto_ops;
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500665 error = vcc_create(net, sock, protocol, AF_ATMSVC, kern);
Joe Perchesb7d93712010-01-26 11:40:18 +0000666 if (error)
667 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 ATM_SD(sock)->local.sas_family = AF_ATMSVC;
669 ATM_SD(sock)->remote.sas_family = AF_ATMSVC;
670 return 0;
671}
672
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +0000673static const struct net_proto_family svc_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 .family = PF_ATMSVC,
675 .create = svc_create,
676 .owner = THIS_MODULE,
677};
678
679
680/*
681 * Initialize the ATM SVC protocol family
682 */
683
684int __init atmsvc_init(void)
685{
686 return sock_register(&svc_family_ops);
687}
688
689void atmsvc_exit(void)
690{
691 sock_unregister(PF_ATMSVC);
692}