blob: 97ed94aa0cbceb3623b91e918fd886f353793aab [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{
93 read_lock(&sk->sk_callback_lock);
Jiri Olsaa57de0b2009-07-08 12:09:13 +000094 if (sk_has_sleeper(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 wake_up(sk->sk_sleep);
96 read_unlock(&sk->sk_callback_lock);
97}
98
99static inline int vcc_writable(struct sock *sk)
100{
101 struct atm_vcc *vcc = atm_sk(sk);
102
103 return (vcc->qos.txtp.max_sdu +
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900104 atomic_read(&sk->sk_wmem_alloc)) <= sk->sk_sndbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
107static void vcc_write_space(struct sock *sk)
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900108{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 read_lock(&sk->sk_callback_lock);
110
111 if (vcc_writable(sk)) {
Jiri Olsaa57de0b2009-07-08 12:09:13 +0000112 if (sk_has_sleeper(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 wake_up_interruptible(sk->sk_sleep);
114
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +0800115 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
117
118 read_unlock(&sk->sk_callback_lock);
119}
120
121static struct proto vcc_proto = {
122 .name = "VCC",
123 .owner = THIS_MODULE,
124 .obj_size = sizeof(struct atm_vcc),
125};
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900126
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700127int vcc_create(struct net *net, struct socket *sock, int protocol, int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 struct sock *sk;
130 struct atm_vcc *vcc;
131
132 sock->sk = NULL;
133 if (sock->type == SOCK_STREAM)
134 return -EINVAL;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700135 sk = sk_alloc(net, family, GFP_KERNEL, &vcc_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if (!sk)
137 return -ENOMEM;
138 sock_init_data(sock, sk);
139 sk->sk_state_change = vcc_def_wakeup;
140 sk->sk_write_space = vcc_write_space;
141
142 vcc = atm_sk(sk);
143 vcc->dev = NULL;
Joe Perchesa8147d72010-01-26 11:40:06 +0000144 memset(&vcc->local, 0, sizeof(struct sockaddr_atmsvc));
145 memset(&vcc->remote, 0, sizeof(struct sockaddr_atmsvc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 vcc->qos.txtp.max_sdu = 1 << 16; /* for meta VCs */
Eric Dumazet81e2a3d2009-06-17 19:06:12 -0700147 atomic_set(&sk->sk_wmem_alloc, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 atomic_set(&sk->sk_rmem_alloc, 0);
149 vcc->push = NULL;
150 vcc->pop = NULL;
151 vcc->push_oam = NULL;
152 vcc->vpi = vcc->vci = 0; /* no VCI/VPI yet */
153 vcc->atm_options = vcc->aal_options = 0;
154 sk->sk_destruct = vcc_sock_destruct;
155 return 0;
156}
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static void vcc_destroy_socket(struct sock *sk)
159{
160 struct atm_vcc *vcc = atm_sk(sk);
161 struct sk_buff *skb;
162
163 set_bit(ATM_VF_CLOSE, &vcc->flags);
164 clear_bit(ATM_VF_READY, &vcc->flags);
165 if (vcc->dev) {
166 if (vcc->dev->ops->close)
167 vcc->dev->ops->close(vcc);
168 if (vcc->push)
169 vcc->push(vcc, NULL); /* atmarpd has no push */
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
Joe Perchesa8147d72010-01-26 11:40:06 +0000172 atm_return(vcc, skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 kfree_skb(skb);
174 }
175
176 module_put(vcc->dev->ops->owner);
177 atm_dev_put(vcc->dev);
178 }
Chas Williams9301e322005-09-28 16:35:01 -0700179
180 vcc_remove_socket(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183int vcc_release(struct socket *sock)
184{
185 struct sock *sk = sock->sk;
186
187 if (sk) {
188 lock_sock(sk);
189 vcc_destroy_socket(sock->sk);
190 release_sock(sk);
191 sock_put(sk);
192 }
193
194 return 0;
195}
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197void vcc_release_async(struct atm_vcc *vcc, int reply)
198{
199 struct sock *sk = sk_atm(vcc);
200
201 set_bit(ATM_VF_CLOSE, &vcc->flags);
202 sk->sk_shutdown |= RCV_SHUTDOWN;
203 sk->sk_err = -reply;
204 clear_bit(ATM_VF_WAITING, &vcc->flags);
205 sk->sk_state_change(sk);
206}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207EXPORT_SYMBOL(vcc_release_async);
208
209
Stanislaw Gruszka64bf69d2005-11-29 16:16:41 -0800210void atm_dev_release_vccs(struct atm_dev *dev)
211{
212 int i;
213
214 write_lock_irq(&vcc_sklist_lock);
215 for (i = 0; i < VCC_HTABLE_SIZE; i++) {
216 struct hlist_head *head = &vcc_hash[i];
217 struct hlist_node *node, *tmp;
218 struct sock *s;
219 struct atm_vcc *vcc;
220
221 sk_for_each_safe(s, node, tmp, head) {
222 vcc = atm_sk(s);
223 if (vcc->dev == dev) {
224 vcc_release_async(vcc, -EPIPE);
225 sk_del_node_init(s);
226 }
227 }
228 }
229 write_unlock_irq(&vcc_sklist_lock);
230}
231
Joe Perchesa8147d72010-01-26 11:40:06 +0000232static int adjust_tp(struct atm_trafprm *tp, unsigned char aal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 int max_sdu;
235
Joe Perchesa8147d72010-01-26 11:40:06 +0000236 if (!tp->traffic_class)
237 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 switch (aal) {
Joe Perchesa8147d72010-01-26 11:40:06 +0000239 case ATM_AAL0:
240 max_sdu = ATM_CELL_SIZE-1;
241 break;
242 case ATM_AAL34:
243 max_sdu = ATM_MAX_AAL34_PDU;
244 break;
245 default:
246 pr_warning("AAL problems ... (%d)\n", aal);
247 /* fall through */
248 case ATM_AAL5:
249 max_sdu = ATM_MAX_AAL5_PDU;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000251 if (!tp->max_sdu)
252 tp->max_sdu = max_sdu;
253 else if (tp->max_sdu > max_sdu)
254 return -EINVAL;
255 if (!tp->max_cdv)
256 tp->max_cdv = ATM_MAX_CDV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return 0;
258}
259
Mitchell Blank Jr61c33e02008-06-17 16:20:06 -0700260static int check_ci(const struct atm_vcc *vcc, short vpi, int vci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Joe Perchesa8147d72010-01-26 11:40:06 +0000262 struct hlist_head *head = &vcc_hash[vci & (VCC_HTABLE_SIZE - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 struct hlist_node *node;
264 struct sock *s;
265 struct atm_vcc *walk;
266
267 sk_for_each(s, node, head) {
268 walk = atm_sk(s);
269 if (walk->dev != vcc->dev)
270 continue;
271 if (test_bit(ATM_VF_ADDR, &walk->flags) && walk->vpi == vpi &&
272 walk->vci == vci && ((walk->qos.txtp.traffic_class !=
273 ATM_NONE && vcc->qos.txtp.traffic_class != ATM_NONE) ||
274 (walk->qos.rxtp.traffic_class != ATM_NONE &&
275 vcc->qos.rxtp.traffic_class != ATM_NONE)))
276 return -EADDRINUSE;
277 }
278
279 /* allow VCCs with same VPI/VCI iff they don't collide on
280 TX/RX (but we may refuse such sharing for other reasons,
281 e.g. if protocol requires to have both channels) */
282
283 return 0;
284}
285
Mitchell Blank Jr61c33e02008-06-17 16:20:06 -0700286static int find_ci(const struct atm_vcc *vcc, short *vpi, int *vci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 static short p; /* poor man's per-device cache */
289 static int c;
290 short old_p;
291 int old_c;
292 int err;
293
294 if (*vpi != ATM_VPI_ANY && *vci != ATM_VCI_ANY) {
295 err = check_ci(vcc, *vpi, *vci);
296 return err;
297 }
298 /* last scan may have left values out of bounds for current device */
299 if (*vpi != ATM_VPI_ANY)
300 p = *vpi;
301 else if (p >= 1 << vcc->dev->ci_range.vpi_bits)
302 p = 0;
303 if (*vci != ATM_VCI_ANY)
304 c = *vci;
305 else if (c < ATM_NOT_RSV_VCI || c >= 1 << vcc->dev->ci_range.vci_bits)
306 c = ATM_NOT_RSV_VCI;
307 old_p = p;
308 old_c = c;
309 do {
310 if (!check_ci(vcc, p, c)) {
311 *vpi = p;
312 *vci = c;
313 return 0;
314 }
315 if (*vci == ATM_VCI_ANY) {
316 c++;
317 if (c >= 1 << vcc->dev->ci_range.vci_bits)
318 c = ATM_NOT_RSV_VCI;
319 }
320 if ((c == ATM_NOT_RSV_VCI || *vci != ATM_VCI_ANY) &&
321 *vpi == ATM_VPI_ANY) {
322 p++;
Joe Perchesa8147d72010-01-26 11:40:06 +0000323 if (p >= 1 << vcc->dev->ci_range.vpi_bits)
324 p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000326 } while (old_p != p || old_c != c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return -EADDRINUSE;
328}
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330static int __vcc_connect(struct atm_vcc *vcc, struct atm_dev *dev, short vpi,
331 int vci)
332{
333 struct sock *sk = sk_atm(vcc);
334 int error;
335
336 if ((vpi != ATM_VPI_UNSPEC && vpi != ATM_VPI_ANY &&
337 vpi >> dev->ci_range.vpi_bits) || (vci != ATM_VCI_UNSPEC &&
338 vci != ATM_VCI_ANY && vci >> dev->ci_range.vci_bits))
339 return -EINVAL;
340 if (vci > 0 && vci < ATM_NOT_RSV_VCI && !capable(CAP_NET_BIND_SERVICE))
341 return -EPERM;
Stanislaw Gruszka64bf69d2005-11-29 16:16:41 -0800342 error = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (!try_module_get(dev->ops->owner))
Stanislaw Gruszka64bf69d2005-11-29 16:16:41 -0800344 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 vcc->dev = dev;
346 write_lock_irq(&vcc_sklist_lock);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900347 if (test_bit(ATM_DF_REMOVED, &dev->flags) ||
Stanislaw Gruszka64bf69d2005-11-29 16:16:41 -0800348 (error = find_ci(vcc, &vpi, &vci))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 write_unlock_irq(&vcc_sklist_lock);
350 goto fail_module_put;
351 }
352 vcc->vpi = vpi;
353 vcc->vci = vci;
354 __vcc_insert_socket(sk);
355 write_unlock_irq(&vcc_sklist_lock);
356 switch (vcc->qos.aal) {
Joe Perchesa8147d72010-01-26 11:40:06 +0000357 case ATM_AAL0:
358 error = atm_init_aal0(vcc);
359 vcc->stats = &dev->stats.aal0;
360 break;
361 case ATM_AAL34:
362 error = atm_init_aal34(vcc);
363 vcc->stats = &dev->stats.aal34;
364 break;
365 case ATM_NO_AAL:
366 /* ATM_AAL5 is also used in the "0 for default" case */
367 vcc->qos.aal = ATM_AAL5;
368 /* fall through */
369 case ATM_AAL5:
370 error = atm_init_aal5(vcc);
371 vcc->stats = &dev->stats.aal5;
372 break;
373 default:
374 error = -EPROTOTYPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000376 if (!error)
377 error = adjust_tp(&vcc->qos.txtp, vcc->qos.aal);
378 if (!error)
379 error = adjust_tp(&vcc->qos.rxtp, vcc->qos.aal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (error)
381 goto fail;
Joe Perches99824462010-01-26 11:40:00 +0000382 pr_debug("VCC %d.%d, AAL %d\n", vpi, vci, vcc->qos.aal);
383 pr_debug(" TX: %d, PCR %d..%d, SDU %d\n",
384 vcc->qos.txtp.traffic_class,
385 vcc->qos.txtp.min_pcr,
386 vcc->qos.txtp.max_pcr,
387 vcc->qos.txtp.max_sdu);
388 pr_debug(" RX: %d, PCR %d..%d, SDU %d\n",
389 vcc->qos.rxtp.traffic_class,
390 vcc->qos.rxtp.min_pcr,
391 vcc->qos.rxtp.max_pcr,
392 vcc->qos.rxtp.max_sdu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 if (dev->ops->open) {
Joe Perchesa8147d72010-01-26 11:40:06 +0000395 error = dev->ops->open(vcc);
396 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 goto fail;
398 }
399 return 0;
400
401fail:
402 vcc_remove_socket(sk);
403fail_module_put:
404 module_put(dev->ops->owner);
405 /* ensure we get dev module ref count correct */
406 vcc->dev = NULL;
407 return error;
408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410int vcc_connect(struct socket *sock, int itf, short vpi, int vci)
411{
412 struct atm_dev *dev;
413 struct atm_vcc *vcc = ATM_SD(sock);
414 int error;
415
Joe Perches99824462010-01-26 11:40:00 +0000416 pr_debug("(vpi %d, vci %d)\n", vpi, vci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (sock->state == SS_CONNECTED)
418 return -EISCONN;
419 if (sock->state != SS_UNCONNECTED)
420 return -EINVAL;
421 if (!(vpi || vci))
422 return -EINVAL;
423
424 if (vpi != ATM_VPI_UNSPEC && vci != ATM_VCI_UNSPEC)
Joe Perchesa8147d72010-01-26 11:40:06 +0000425 clear_bit(ATM_VF_PARTIAL, &vcc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 else
Joe Perchesa8147d72010-01-26 11:40:06 +0000427 if (test_bit(ATM_VF_PARTIAL, &vcc->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return -EINVAL;
Joe Perches99824462010-01-26 11:40:00 +0000429 pr_debug("(TX: cl %d,bw %d-%d,sdu %d; "
430 "RX: cl %d,bw %d-%d,sdu %d,AAL %s%d)\n",
431 vcc->qos.txtp.traffic_class, vcc->qos.txtp.min_pcr,
432 vcc->qos.txtp.max_pcr, vcc->qos.txtp.max_sdu,
433 vcc->qos.rxtp.traffic_class, vcc->qos.rxtp.min_pcr,
Joe Perchesa8147d72010-01-26 11:40:06 +0000434 vcc->qos.rxtp.max_pcr, vcc->qos.rxtp.max_sdu,
Joe Perches99824462010-01-26 11:40:00 +0000435 vcc->qos.aal == ATM_AAL5 ? "" :
436 vcc->qos.aal == ATM_AAL0 ? "" : " ??? code ",
437 vcc->qos.aal == ATM_AAL0 ? 0 : vcc->qos.aal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
439 return -EBADFD;
440 if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
441 vcc->qos.rxtp.traffic_class == ATM_ANYCLASS)
442 return -EINVAL;
Mitchell Blank Jrc9933d02005-11-29 16:13:32 -0800443 if (likely(itf != ATM_ITF_ANY)) {
Joe Perchesa8147d72010-01-26 11:40:06 +0000444 dev = try_then_request_module(atm_dev_lookup(itf),
445 "atm-device-%d", itf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 dev = NULL;
Ingo Molnar57b47a52006-03-20 22:35:41 -0800448 mutex_lock(&atm_dev_mutex);
Mitchell Blank Jrc9933d02005-11-29 16:13:32 -0800449 if (!list_empty(&atm_devs)) {
Joe Perchesa8147d72010-01-26 11:40:06 +0000450 dev = list_entry(atm_devs.next,
451 struct atm_dev, dev_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 atm_dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
Ingo Molnar57b47a52006-03-20 22:35:41 -0800454 mutex_unlock(&atm_dev_mutex);
Mitchell Blank Jrc9933d02005-11-29 16:13:32 -0800455 }
456 if (!dev)
457 return -ENODEV;
458 error = __vcc_connect(vcc, dev, vpi, vci);
459 if (error) {
460 atm_dev_put(dev);
461 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463 if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC)
Joe Perchesa8147d72010-01-26 11:40:06 +0000464 set_bit(ATM_VF_PARTIAL, &vcc->flags);
465 if (test_bit(ATM_VF_READY, &ATM_SD(sock)->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 sock->state = SS_CONNECTED;
467 return 0;
468}
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470int vcc_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
471 size_t size, int flags)
472{
473 struct sock *sk = sock->sk;
474 struct atm_vcc *vcc;
475 struct sk_buff *skb;
476 int copied, error = -EINVAL;
477
478 if (sock->state != SS_CONNECTED)
479 return -ENOTCONN;
480 if (flags & ~MSG_DONTWAIT) /* only handle MSG_DONTWAIT */
481 return -EOPNOTSUPP;
482 vcc = ATM_SD(sock);
Joe Perchesa8147d72010-01-26 11:40:06 +0000483 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
484 test_bit(ATM_VF_CLOSE, &vcc->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 !test_bit(ATM_VF_READY, &vcc->flags))
486 return 0;
487
488 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &error);
489 if (!skb)
490 return error;
491
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900492 copied = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (copied > size) {
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900494 copied = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 msg->msg_flags |= MSG_TRUNC;
496 }
497
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900498 error = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
499 if (error)
500 return error;
Neil Horman3b885782009-10-12 13:26:31 -0700501 sock_recv_ts_and_drops(msg, sk, skb);
Joe Perches99824462010-01-26 11:40:00 +0000502 pr_debug("%d -= %d\n", atomic_read(&sk->sk_rmem_alloc), skb->truesize);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900503 atm_return(vcc, skb->truesize);
504 skb_free_datagram(sk, skb);
505 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508int vcc_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
509 size_t total_len)
510{
511 struct sock *sk = sock->sk;
512 DEFINE_WAIT(wait);
513 struct atm_vcc *vcc;
514 struct sk_buff *skb;
Joe Perchesa8147d72010-01-26 11:40:06 +0000515 int eff, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 const void __user *buff;
517 int size;
518
519 lock_sock(sk);
520 if (sock->state != SS_CONNECTED) {
521 error = -ENOTCONN;
522 goto out;
523 }
524 if (m->msg_name) {
525 error = -EISCONN;
526 goto out;
527 }
528 if (m->msg_iovlen != 1) {
529 error = -ENOSYS; /* fix this later @@@ */
530 goto out;
531 }
532 buff = m->msg_iov->iov_base;
533 size = m->msg_iov->iov_len;
534 vcc = ATM_SD(sock);
535 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
536 test_bit(ATM_VF_CLOSE, &vcc->flags) ||
537 !test_bit(ATM_VF_READY, &vcc->flags)) {
538 error = -EPIPE;
539 send_sig(SIGPIPE, current, 0);
540 goto out;
541 }
542 if (!size) {
543 error = 0;
544 goto out;
545 }
546 if (size < 0 || size > vcc->qos.txtp.max_sdu) {
547 error = -EMSGSIZE;
548 goto out;
549 }
Jesper Juhle49332b2005-05-01 08:59:08 -0700550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 eff = (size+3) & ~3; /* align to word boundary */
552 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
553 error = 0;
Joe Perchesa8147d72010-01-26 11:40:06 +0000554 while (!(skb = alloc_tx(vcc, eff))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (m->msg_flags & MSG_DONTWAIT) {
556 error = -EAGAIN;
557 break;
558 }
559 schedule();
560 if (signal_pending(current)) {
561 error = -ERESTARTSYS;
562 break;
563 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000564 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
565 test_bit(ATM_VF_CLOSE, &vcc->flags) ||
566 !test_bit(ATM_VF_READY, &vcc->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 error = -EPIPE;
568 send_sig(SIGPIPE, current, 0);
569 break;
570 }
571 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
572 }
573 finish_wait(sk->sk_sleep, &wait);
574 if (error)
575 goto out;
576 skb->dev = NULL; /* for paths shared with net_device interfaces */
577 ATM_SKB(skb)->atm_options = vcc->atm_options;
Joe Perchesa8147d72010-01-26 11:40:06 +0000578 if (copy_from_user(skb_put(skb, size), buff, size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 kfree_skb(skb);
580 error = -EFAULT;
581 goto out;
582 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000583 if (eff != size)
584 memset(skb->data + size, 0, eff-size);
585 error = vcc->dev->ops->send(vcc, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 error = error ? error : size;
587out:
588 release_sock(sk);
589 return error;
590}
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592unsigned int vcc_poll(struct file *file, struct socket *sock, poll_table *wait)
593{
594 struct sock *sk = sock->sk;
595 struct atm_vcc *vcc;
596 unsigned int mask;
597
Jiri Olsaa57de0b2009-07-08 12:09:13 +0000598 sock_poll_wait(file, sk->sk_sleep, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 mask = 0;
600
601 vcc = ATM_SD(sock);
602
603 /* exceptional events */
604 if (sk->sk_err)
605 mask = POLLERR;
606
607 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
608 test_bit(ATM_VF_CLOSE, &vcc->flags))
609 mask |= POLLHUP;
610
611 /* readable? */
612 if (!skb_queue_empty(&sk->sk_receive_queue))
613 mask |= POLLIN | POLLRDNORM;
614
615 /* writable? */
616 if (sock->state == SS_CONNECTING &&
617 test_bit(ATM_VF_WAITING, &vcc->flags))
618 return mask;
619
620 if (vcc->qos.txtp.traffic_class != ATM_NONE &&
621 vcc_writable(sk))
622 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
623
624 return mask;
625}
626
Joe Perchesa8147d72010-01-26 11:40:06 +0000627static int atm_change_qos(struct atm_vcc *vcc, struct atm_qos *qos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 int error;
630
631 /*
632 * Don't let the QoS change the already connected AAL type nor the
633 * traffic class.
634 */
635 if (qos->aal != vcc->qos.aal ||
636 qos->rxtp.traffic_class != vcc->qos.rxtp.traffic_class ||
637 qos->txtp.traffic_class != vcc->qos.txtp.traffic_class)
638 return -EINVAL;
Joe Perchesa8147d72010-01-26 11:40:06 +0000639 error = adjust_tp(&qos->txtp, qos->aal);
640 if (!error)
641 error = adjust_tp(&qos->rxtp, qos->aal);
642 if (error)
643 return error;
644 if (!vcc->dev->ops->change_qos)
645 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (sk_atm(vcc)->sk_family == AF_ATMPVC)
Joe Perchesa8147d72010-01-26 11:40:06 +0000647 return vcc->dev->ops->change_qos(vcc, qos, ATM_MF_SET);
648 return svc_change_qos(vcc, qos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
Mitchell Blank Jr61c33e02008-06-17 16:20:06 -0700651static int check_tp(const struct atm_trafprm *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
653 /* @@@ Should be merged with adjust_tp */
Joe Perchesa8147d72010-01-26 11:40:06 +0000654 if (!tp->traffic_class || tp->traffic_class == ATM_ANYCLASS)
655 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (tp->traffic_class != ATM_UBR && !tp->min_pcr && !tp->pcr &&
Joe Perchesa8147d72010-01-26 11:40:06 +0000657 !tp->max_pcr)
658 return -EINVAL;
659 if (tp->min_pcr == ATM_MAX_PCR)
660 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (tp->min_pcr && tp->max_pcr && tp->max_pcr != ATM_MAX_PCR &&
Joe Perchesa8147d72010-01-26 11:40:06 +0000662 tp->min_pcr > tp->max_pcr)
663 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 /*
665 * We allow pcr to be outside [min_pcr,max_pcr], because later
666 * adjustment may still push it in the valid range.
667 */
668 return 0;
669}
670
Mitchell Blank Jr61c33e02008-06-17 16:20:06 -0700671static int check_qos(const struct atm_qos *qos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 int error;
674
675 if (!qos->txtp.traffic_class && !qos->rxtp.traffic_class)
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900676 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (qos->txtp.traffic_class != qos->rxtp.traffic_class &&
678 qos->txtp.traffic_class && qos->rxtp.traffic_class &&
679 qos->txtp.traffic_class != ATM_ANYCLASS &&
Joe Perchesa8147d72010-01-26 11:40:06 +0000680 qos->rxtp.traffic_class != ATM_ANYCLASS)
681 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 error = check_tp(&qos->txtp);
Joe Perchesa8147d72010-01-26 11:40:06 +0000683 if (error)
684 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return check_tp(&qos->rxtp);
686}
687
688int vcc_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700689 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
691 struct atm_vcc *vcc;
692 unsigned long value;
693 int error;
694
695 if (__SO_LEVEL_MATCH(optname, level) && optlen != __SO_SIZE(optname))
696 return -EINVAL;
697
698 vcc = ATM_SD(sock);
699 switch (optname) {
Joe Perchesa8147d72010-01-26 11:40:06 +0000700 case SO_ATMQOS:
701 {
702 struct atm_qos qos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Joe Perchesa8147d72010-01-26 11:40:06 +0000704 if (copy_from_user(&qos, optval, sizeof(qos)))
705 return -EFAULT;
706 error = check_qos(&qos);
707 if (error)
708 return error;
709 if (sock->state == SS_CONNECTED)
710 return atm_change_qos(vcc, &qos);
711 if (sock->state != SS_UNCONNECTED)
712 return -EBADFD;
713 vcc->qos = qos;
714 set_bit(ATM_VF_HASQOS, &vcc->flags);
715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000717 case SO_SETCLP:
718 if (get_user(value, (unsigned long __user *)optval))
719 return -EFAULT;
720 if (value)
721 vcc->atm_options |= ATM_ATMOPT_CLP;
722 else
723 vcc->atm_options &= ~ATM_ATMOPT_CLP;
724 return 0;
725 default:
726 if (level == SOL_SOCKET)
727 return -EINVAL;
728 break;
729 }
730 if (!vcc->dev || !vcc->dev->ops->setsockopt)
731 return -EINVAL;
732 return vcc->dev->ops->setsockopt(vcc, level, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735int vcc_getsockopt(struct socket *sock, int level, int optname,
736 char __user *optval, int __user *optlen)
737{
738 struct atm_vcc *vcc;
739 int len;
740
741 if (get_user(len, optlen))
742 return -EFAULT;
743 if (__SO_LEVEL_MATCH(optname, level) && len != __SO_SIZE(optname))
744 return -EINVAL;
745
746 vcc = ATM_SD(sock);
747 switch (optname) {
Joe Perchesa8147d72010-01-26 11:40:06 +0000748 case SO_ATMQOS:
749 if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
750 return -EINVAL;
751 return copy_to_user(optval, &vcc->qos, sizeof(vcc->qos))
752 ? -EFAULT : 0;
753 case SO_SETCLP:
754 return put_user(vcc->atm_options & ATM_ATMOPT_CLP ? 1 : 0,
755 (unsigned long __user *)optval) ? -EFAULT : 0;
756 case SO_ATMPVC:
757 {
758 struct sockaddr_atmpvc pvc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Joe Perchesa8147d72010-01-26 11:40:06 +0000760 if (!vcc->dev || !test_bit(ATM_VF_ADDR, &vcc->flags))
761 return -ENOTCONN;
762 pvc.sap_family = AF_ATMPVC;
763 pvc.sap_addr.itf = vcc->dev->number;
764 pvc.sap_addr.vpi = vcc->vpi;
765 pvc.sap_addr.vci = vcc->vci;
766 return copy_to_user(optval, &pvc, sizeof(pvc)) ? -EFAULT : 0;
767 }
768 default:
769 if (level == SOL_SOCKET)
770 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 break;
772 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000773 if (!vcc->dev || !vcc->dev->ops->getsockopt)
774 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return vcc->dev->ops->getsockopt(vcc, level, optname, optval, len);
776}
777
778static int __init atm_init(void)
779{
780 int error;
781
Joe Perchesa8147d72010-01-26 11:40:06 +0000782 error = proto_register(&vcc_proto, 0);
783 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 goto out;
Joe Perchesa8147d72010-01-26 11:40:06 +0000785 error = atmpvc_init();
786 if (error < 0) {
Joe Perches99824462010-01-26 11:40:00 +0000787 pr_err("atmpvc_init() failed with %d\n", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 goto out_unregister_vcc_proto;
789 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000790 error = atmsvc_init();
791 if (error < 0) {
Joe Perches99824462010-01-26 11:40:00 +0000792 pr_err("atmsvc_init() failed with %d\n", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 goto out_atmpvc_exit;
794 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000795 error = atm_proc_init();
796 if (error < 0) {
Joe Perches99824462010-01-26 11:40:00 +0000797 pr_err("atm_proc_init() failed with %d\n", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 goto out_atmsvc_exit;
799 }
Joe Perchesa8147d72010-01-26 11:40:06 +0000800 error = atm_sysfs_init();
801 if (error < 0) {
Joe Perches99824462010-01-26 11:40:00 +0000802 pr_err("atm_sysfs_init() failed with %d\n", error);
Roman Kagan656d98b2006-06-29 12:36:34 -0700803 goto out_atmproc_exit;
804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805out:
806 return error;
Roman Kagan656d98b2006-06-29 12:36:34 -0700807out_atmproc_exit:
808 atm_proc_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809out_atmsvc_exit:
810 atmsvc_exit();
811out_atmpvc_exit:
812 atmsvc_exit();
813out_unregister_vcc_proto:
814 proto_unregister(&vcc_proto);
815 goto out;
816}
817
818static void __exit atm_exit(void)
819{
820 atm_proc_exit();
Roman Kagan656d98b2006-06-29 12:36:34 -0700821 atm_sysfs_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 atmsvc_exit();
823 atmpvc_exit();
824 proto_unregister(&vcc_proto);
825}
826
Daniel Walker84ff6022007-02-05 18:04:06 -0800827subsys_initcall(atm_init);
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829module_exit(atm_exit);
830
831MODULE_LICENSE("GPL");
832MODULE_ALIAS_NETPROTO(PF_ATMPVC);
833MODULE_ALIAS_NETPROTO(PF_ATMSVC);