blob: ac618b0b8a4f52933a238b83234fc09c1187b310 [file] [log] [blame]
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001/*
2 * Copyright (C) ST-Ericsson AB 2010
sjur.brandeland@stericsson.com26ee65e2013-04-22 23:57:01 +00003 * Author: Sjur Brendeland
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00004 * License terms: GNU General Public License (GPL) version 2
5 */
6
Joe Perchesb31fa5b2010-09-05 21:31:11 +00007#define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
8
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00009#include <linux/fs.h>
10#include <linux/init.h>
11#include <linux/module.h>
12#include <linux/sched.h>
13#include <linux/spinlock.h>
14#include <linux/mutex.h>
15#include <linux/list.h>
16#include <linux/wait.h>
17#include <linux/poll.h>
18#include <linux/tcp.h>
19#include <linux/uaccess.h>
Sjur Braendelandbece7b22010-04-28 08:54:38 +000020#include <linux/debugfs.h>
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000021#include <linux/caif/caif_socket.h>
Dmitry Tarnyagin44764812012-04-12 08:27:24 +000022#include <linux/pkt_sched.h>
Sjur Braendelandbece7b22010-04-28 08:54:38 +000023#include <net/sock.h>
24#include <net/tcp_states.h>
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000025#include <net/caif/caif_layer.h>
26#include <net/caif/caif_dev.h>
27#include <net/caif/cfpkt.h>
28
29MODULE_LICENSE("GPL");
Sjur Braendelandbece7b22010-04-28 08:54:38 +000030MODULE_ALIAS_NETPROTO(AF_CAIF);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000031
Sjur Braendelandbece7b22010-04-28 08:54:38 +000032/*
33 * CAIF state is re-using the TCP socket states.
34 * caif_states stored in sk_state reflect the state as reported by
35 * the CAIF stack, while sk_socket->state is the state of the socket.
36 */
37enum caif_states {
38 CAIF_CONNECTED = TCP_ESTABLISHED,
39 CAIF_CONNECTING = TCP_SYN_SENT,
40 CAIF_DISCONNECTED = TCP_CLOSE
41};
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000042
Sjur Braendelandbece7b22010-04-28 08:54:38 +000043#define TX_FLOW_ON_BIT 1
44#define RX_FLOW_ON_BIT 2
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000045
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000046struct caifsock {
Sjur Braendelandbece7b22010-04-28 08:54:38 +000047 struct sock sk; /* must be first member */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000048 struct cflayer layer;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000049 u32 flow_state;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000050 struct caif_connect_request conn_req;
Sjur Braendelandbece7b22010-04-28 08:54:38 +000051 struct mutex readlock;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000052 struct dentry *debugfs_socket_dir;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +000053 int headroom, tailroom, maxframe;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000054};
55
Sjur Braendelandbece7b22010-04-28 08:54:38 +000056static int rx_flow_is_on(struct caifsock *cf_sk)
57{
58 return test_bit(RX_FLOW_ON_BIT,
59 (void *) &cf_sk->flow_state);
60}
61
62static int tx_flow_is_on(struct caifsock *cf_sk)
63{
64 return test_bit(TX_FLOW_ON_BIT,
65 (void *) &cf_sk->flow_state);
66}
67
68static void set_rx_flow_off(struct caifsock *cf_sk)
69{
70 clear_bit(RX_FLOW_ON_BIT,
71 (void *) &cf_sk->flow_state);
72}
73
74static void set_rx_flow_on(struct caifsock *cf_sk)
75{
76 set_bit(RX_FLOW_ON_BIT,
77 (void *) &cf_sk->flow_state);
78}
79
80static void set_tx_flow_off(struct caifsock *cf_sk)
81{
82 clear_bit(TX_FLOW_ON_BIT,
83 (void *) &cf_sk->flow_state);
84}
85
86static void set_tx_flow_on(struct caifsock *cf_sk)
87{
88 set_bit(TX_FLOW_ON_BIT,
89 (void *) &cf_sk->flow_state);
90}
91
92static void caif_read_lock(struct sock *sk)
93{
94 struct caifsock *cf_sk;
95 cf_sk = container_of(sk, struct caifsock, sk);
96 mutex_lock(&cf_sk->readlock);
97}
98
99static void caif_read_unlock(struct sock *sk)
100{
101 struct caifsock *cf_sk;
102 cf_sk = container_of(sk, struct caifsock, sk);
103 mutex_unlock(&cf_sk->readlock);
104}
105
Sjur Braendelanda9a8f102010-05-21 02:16:11 +0000106static int sk_rcvbuf_lowwater(struct caifsock *cf_sk)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000107{
108 /* A quarter of full buffer is used a low water mark */
109 return cf_sk->sk.sk_rcvbuf / 4;
110}
111
Sjur Braendelanda9a8f102010-05-21 02:16:11 +0000112static void caif_flow_ctrl(struct sock *sk, int mode)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000113{
114 struct caifsock *cf_sk;
115 cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000116 if (cf_sk->layer.dn && cf_sk->layer.dn->modemcmd)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000117 cf_sk->layer.dn->modemcmd(cf_sk->layer.dn, mode);
118}
119
120/*
121 * Copied from sock.c:sock_queue_rcv_skb(), but changed so packets are
122 * not dropped, but CAIF is sending flow off instead.
123 */
Sjur Braendelanda9a8f102010-05-21 02:16:11 +0000124static int caif_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000125{
126 int err;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000127 unsigned long flags;
128 struct sk_buff_head *list = &sk->sk_receive_queue;
129 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
130
131 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +0000132 (unsigned int)sk->sk_rcvbuf && rx_flow_is_on(cf_sk)) {
Joe Perchese87cc472012-05-13 21:56:26 +0000133 net_dbg_ratelimited("sending flow OFF (queue len = %d %d)\n",
134 atomic_read(&cf_sk->sk.sk_rmem_alloc),
135 sk_rcvbuf_lowwater(cf_sk));
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000136 set_rx_flow_off(cf_sk);
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000137 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_OFF_REQ);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000138 }
139
140 err = sk_filter(sk, skb);
141 if (err)
142 return err;
Mel Gormanc76562b2012-07-31 16:44:41 -0700143 if (!sk_rmem_schedule(sk, skb, skb->truesize) && rx_flow_is_on(cf_sk)) {
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000144 set_rx_flow_off(cf_sk);
Joe Perchese87cc472012-05-13 21:56:26 +0000145 net_dbg_ratelimited("sending flow OFF due to rmem_schedule\n");
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000146 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_OFF_REQ);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000147 }
148 skb->dev = NULL;
149 skb_set_owner_r(skb, sk);
150 /* Cache the SKB length before we tack it onto the receive
151 * queue. Once it is added it no longer belongs to us and
152 * may be freed by other threads of control pulling packets
153 * from the queue.
154 */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000155 spin_lock_irqsave(&list->lock, flags);
156 if (!sock_flag(sk, SOCK_DEAD))
157 __skb_queue_tail(list, skb);
158 spin_unlock_irqrestore(&list->lock, flags);
159
160 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -0400161 sk->sk_data_ready(sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000162 else
163 kfree_skb(skb);
164 return 0;
165}
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000166
167/* Packet Receive Callback function called from CAIF Stack */
168static int caif_sktrecv_cb(struct cflayer *layr, struct cfpkt *pkt)
169{
170 struct caifsock *cf_sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000171 struct sk_buff *skb;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000172
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000173 cf_sk = container_of(layr, struct caifsock, layer);
174 skb = cfpkt_tonative(pkt);
175
176 if (unlikely(cf_sk->sk.sk_state != CAIF_CONNECTED)) {
sjur.brandeland@stericsson.com3f874ad2011-05-13 02:44:08 +0000177 kfree_skb(skb);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000178 return 0;
179 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000180 caif_queue_rcv_skb(&cf_sk->sk, skb);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000181 return 0;
182}
183
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000184static void cfsk_hold(struct cflayer *layr)
185{
186 struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
187 sock_hold(&cf_sk->sk);
188}
189
190static void cfsk_put(struct cflayer *layr)
191{
192 struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
193 sock_put(&cf_sk->sk);
194}
195
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000196/* Packet Control Callback function called from CAIF */
197static void caif_ctrl_cb(struct cflayer *layr,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000198 enum caif_ctrlcmd flow,
199 int phyid)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000200{
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000201 struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000202 switch (flow) {
203 case CAIF_CTRLCMD_FLOW_ON_IND:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000204 /* OK from modem to start sending again */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000205 set_tx_flow_on(cf_sk);
206 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000207 break;
208
209 case CAIF_CTRLCMD_FLOW_OFF_IND:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000210 /* Modem asks us to shut up */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000211 set_tx_flow_off(cf_sk);
212 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000213 break;
214
215 case CAIF_CTRLCMD_INIT_RSP:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000216 /* We're now connected */
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000217 caif_client_register_refcnt(&cf_sk->layer,
218 cfsk_hold, cfsk_put);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000219 cf_sk->sk.sk_state = CAIF_CONNECTED;
220 set_tx_flow_on(cf_sk);
Sjur Brændelandeaa8c5f2012-06-24 11:01:36 +0000221 cf_sk->sk.sk_shutdown = 0;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000222 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000223 break;
224
225 case CAIF_CTRLCMD_DEINIT_RSP:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000226 /* We're now disconnected */
227 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
228 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000229 break;
230
231 case CAIF_CTRLCMD_INIT_FAIL_RSP:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000232 /* Connect request failed */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000233 cf_sk->sk.sk_err = ECONNREFUSED;
234 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
235 cf_sk->sk.sk_shutdown = SHUTDOWN_MASK;
236 /*
237 * Socket "standards" seems to require POLLOUT to
238 * be set at connect failure.
239 */
240 set_tx_flow_on(cf_sk);
241 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000242 break;
243
244 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000245 /* Modem has closed this connection, or device is down. */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000246 cf_sk->sk.sk_shutdown = SHUTDOWN_MASK;
247 cf_sk->sk.sk_err = ECONNRESET;
248 set_rx_flow_on(cf_sk);
249 cf_sk->sk.sk_error_report(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000250 break;
251
252 default:
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000253 pr_debug("Unexpected flow command %d\n", flow);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000254 }
255}
256
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000257static void caif_check_flow_release(struct sock *sk)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000258{
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000259 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
260
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000261 if (rx_flow_is_on(cf_sk))
262 return;
263
264 if (atomic_read(&sk->sk_rmem_alloc) <= sk_rcvbuf_lowwater(cf_sk)) {
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000265 set_rx_flow_on(cf_sk);
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000266 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_ON_REQ);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000267 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000268}
269
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000270/*
271 * Copied from unix_dgram_recvmsg, but removed credit checks,
272 * changed locking, address handling and added MSG_TRUNC.
273 */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000274static int caif_seqpkt_recvmsg(struct kiocb *iocb, struct socket *sock,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000275 struct msghdr *m, size_t len, int flags)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000276
277{
278 struct sock *sk = sock->sk;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000279 struct sk_buff *skb;
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000280 int ret;
281 int copylen;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000282
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000283 ret = -EOPNOTSUPP;
284 if (m->msg_flags&MSG_OOB)
285 goto read_error;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000286
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000287 skb = skb_recv_datagram(sk, flags, 0 , &ret);
288 if (!skb)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000289 goto read_error;
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000290 copylen = skb->len;
291 if (len < copylen) {
292 m->msg_flags |= MSG_TRUNC;
293 copylen = len;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000294 }
295
David S. Miller51f3d022014-11-05 16:46:40 -0500296 ret = skb_copy_datagram_msg(skb, 0, m, copylen);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000297 if (ret)
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000298 goto out_free;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000299
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000300 ret = (flags & MSG_TRUNC) ? skb->len : copylen;
301out_free:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000302 skb_free_datagram(sk, skb);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000303 caif_check_flow_release(sk);
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000304 return ret;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000305
306read_error:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000307 return ret;
308}
309
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000310
311/* Copied from unix_stream_wait_data, identical except for lock call. */
312static long caif_stream_data_wait(struct sock *sk, long timeo)
313{
314 DEFINE_WAIT(wait);
315 lock_sock(sk);
316
317 for (;;) {
318 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
319
320 if (!skb_queue_empty(&sk->sk_receive_queue) ||
321 sk->sk_err ||
322 sk->sk_state != CAIF_CONNECTED ||
323 sock_flag(sk, SOCK_DEAD) ||
324 (sk->sk_shutdown & RCV_SHUTDOWN) ||
325 signal_pending(current) ||
326 !timeo)
327 break;
328
329 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
330 release_sock(sk);
331 timeo = schedule_timeout(timeo);
332 lock_sock(sk);
333 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
334 }
335
336 finish_wait(sk_sleep(sk), &wait);
337 release_sock(sk);
338 return timeo;
339}
340
341
342/*
343 * Copied from unix_stream_recvmsg, but removed credit checks,
344 * changed locking calls, changed address handling.
345 */
346static int caif_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000347 struct msghdr *msg, size_t size,
348 int flags)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000349{
350 struct sock *sk = sock->sk;
351 int copied = 0;
352 int target;
353 int err = 0;
354 long timeo;
355
356 err = -EOPNOTSUPP;
357 if (flags&MSG_OOB)
358 goto out;
359
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000360 /*
361 * Lock the socket to prevent queue disordering
362 * while sleeps in memcpy_tomsg
363 */
364 err = -EAGAIN;
365 if (sk->sk_state == CAIF_CONNECTING)
366 goto out;
367
368 caif_read_lock(sk);
369 target = sock_rcvlowat(sk, flags&MSG_WAITALL, size);
370 timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
371
372 do {
373 int chunk;
374 struct sk_buff *skb;
375
376 lock_sock(sk);
377 skb = skb_dequeue(&sk->sk_receive_queue);
378 caif_check_flow_release(sk);
379
380 if (skb == NULL) {
381 if (copied >= target)
382 goto unlock;
383 /*
384 * POSIX 1003.1g mandates this order.
385 */
386 err = sock_error(sk);
387 if (err)
388 goto unlock;
389 err = -ECONNRESET;
390 if (sk->sk_shutdown & RCV_SHUTDOWN)
391 goto unlock;
392
393 err = -EPIPE;
394 if (sk->sk_state != CAIF_CONNECTED)
395 goto unlock;
396 if (sock_flag(sk, SOCK_DEAD))
397 goto unlock;
398
399 release_sock(sk);
400
401 err = -EAGAIN;
402 if (!timeo)
403 break;
404
405 caif_read_unlock(sk);
406
407 timeo = caif_stream_data_wait(sk, timeo);
408
409 if (signal_pending(current)) {
410 err = sock_intr_errno(timeo);
411 goto out;
412 }
413 caif_read_lock(sk);
414 continue;
415unlock:
416 release_sock(sk);
417 break;
418 }
419 release_sock(sk);
420 chunk = min_t(unsigned int, skb->len, size);
Al Viro7eab8d92014-04-06 21:51:23 -0400421 if (memcpy_to_msg(msg, skb->data, chunk)) {
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000422 skb_queue_head(&sk->sk_receive_queue, skb);
423 if (copied == 0)
424 copied = -EFAULT;
425 break;
426 }
427 copied += chunk;
428 size -= chunk;
429
430 /* Mark read part of skb as used */
431 if (!(flags & MSG_PEEK)) {
432 skb_pull(skb, chunk);
433
434 /* put the skb back if we didn't use it up. */
435 if (skb->len) {
436 skb_queue_head(&sk->sk_receive_queue, skb);
437 break;
438 }
439 kfree_skb(skb);
440
441 } else {
442 /*
443 * It is questionable, see note in unix_dgram_recvmsg.
444 */
445 /* put message back and return */
446 skb_queue_head(&sk->sk_receive_queue, skb);
447 break;
448 }
449 } while (size);
450 caif_read_unlock(sk);
451
452out:
453 return copied ? : err;
454}
455
456/*
457 * Copied from sock.c:sock_wait_for_wmem, but change to wait for
458 * CAIF flow-on and sock_writable.
459 */
460static long caif_wait_for_flow_on(struct caifsock *cf_sk,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000461 int wait_writeable, long timeo, int *err)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000462{
463 struct sock *sk = &cf_sk->sk;
464 DEFINE_WAIT(wait);
465 for (;;) {
466 *err = 0;
467 if (tx_flow_is_on(cf_sk) &&
468 (!wait_writeable || sock_writeable(&cf_sk->sk)))
469 break;
470 *err = -ETIMEDOUT;
471 if (!timeo)
472 break;
473 *err = -ERESTARTSYS;
474 if (signal_pending(current))
475 break;
476 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
477 *err = -ECONNRESET;
478 if (sk->sk_shutdown & SHUTDOWN_MASK)
479 break;
480 *err = -sk->sk_err;
481 if (sk->sk_err)
482 break;
483 *err = -EPIPE;
484 if (cf_sk->sk.sk_state != CAIF_CONNECTED)
485 break;
486 timeo = schedule_timeout(timeo);
487 }
488 finish_wait(sk_sleep(sk), &wait);
489 return timeo;
490}
491
492/*
493 * Transmit a SKB. The device may temporarily request re-transmission
494 * by returning EAGAIN.
495 */
496static int transmit_skb(struct sk_buff *skb, struct caifsock *cf_sk,
497 int noblock, long timeo)
498{
499 struct cfpkt *pkt;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000500
501 pkt = cfpkt_fromnative(CAIF_DIR_OUT, skb);
sjur.brandeland@stericsson.com3f874ad2011-05-13 02:44:08 +0000502 memset(skb->cb, 0, sizeof(struct caif_payload_info));
Dmitry Tarnyagin44764812012-04-12 08:27:24 +0000503 cfpkt_set_prio(pkt, cf_sk->sk.sk_priority);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000504
Dmitry Tarnyaginba760572012-02-02 01:21:03 +0000505 if (cf_sk->layer.dn == NULL) {
506 kfree_skb(skb);
Sjur Brændeland4dd820c2011-04-11 10:43:51 +0000507 return -EINVAL;
Dmitry Tarnyaginba760572012-02-02 01:21:03 +0000508 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000509
Sjur Brændeland4dd820c2011-04-11 10:43:51 +0000510 return cf_sk->layer.dn->transmit(cf_sk->layer.dn, pkt);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000511}
512
513/* Copied from af_unix:unix_dgram_sendmsg, and adapted to CAIF */
514static int caif_seqpkt_sendmsg(struct kiocb *kiocb, struct socket *sock,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000515 struct msghdr *msg, size_t len)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000516{
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000517 struct sock *sk = sock->sk;
518 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000519 int buffer_size;
520 int ret = 0;
521 struct sk_buff *skb = NULL;
522 int noblock;
523 long timeo;
524 caif_assert(cf_sk);
525 ret = sock_error(sk);
526 if (ret)
527 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000528
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000529 ret = -EOPNOTSUPP;
530 if (msg->msg_flags&MSG_OOB)
531 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000532
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000533 ret = -EOPNOTSUPP;
534 if (msg->msg_namelen)
535 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000536
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000537 ret = -EINVAL;
538 if (unlikely(msg->msg_iov->iov_base == NULL))
539 goto err;
540 noblock = msg->msg_flags & MSG_DONTWAIT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000541
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000542 timeo = sock_sndtimeo(sk, noblock);
543 timeo = caif_wait_for_flow_on(container_of(sk, struct caifsock, sk),
544 1, timeo, &ret);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000545
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000546 if (ret)
547 goto err;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000548 ret = -EPIPE;
549 if (cf_sk->sk.sk_state != CAIF_CONNECTED ||
550 sock_flag(sk, SOCK_DEAD) ||
551 (sk->sk_shutdown & RCV_SHUTDOWN))
552 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000553
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000554 /* Error if trying to write more than maximum frame size. */
555 ret = -EMSGSIZE;
556 if (len > cf_sk->maxframe && cf_sk->sk.sk_protocol != CAIFPROTO_RFM)
557 goto err;
558
559 buffer_size = len + cf_sk->headroom + cf_sk->tailroom;
560
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000561 ret = -ENOMEM;
562 skb = sock_alloc_send_skb(sk, buffer_size, noblock, &ret);
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000563
564 if (!skb || skb_tailroom(skb) < buffer_size)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000565 goto err;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000566
567 skb_reserve(skb, cf_sk->headroom);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000568
Al Viro6ce8e9c2014-04-06 21:25:44 -0400569 ret = memcpy_from_msg(skb_put(skb, len), msg, len);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000570
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000571 if (ret)
572 goto err;
573 ret = transmit_skb(skb, cf_sk, noblock, timeo);
574 if (ret < 0)
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000575 /* skb is already freed */
576 return ret;
577
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000578 return len;
579err:
580 kfree_skb(skb);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000581 return ret;
582}
583
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000584/*
585 * Copied from unix_stream_sendmsg and adapted to CAIF:
586 * Changed removed permission handling and added waiting for flow on
587 * and other minor adaptations.
588 */
589static int caif_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000590 struct msghdr *msg, size_t len)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000591{
592 struct sock *sk = sock->sk;
593 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000594 int err, size;
595 struct sk_buff *skb;
596 int sent = 0;
597 long timeo;
598
599 err = -EOPNOTSUPP;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000600 if (unlikely(msg->msg_flags&MSG_OOB))
601 goto out_err;
602
603 if (unlikely(msg->msg_namelen))
604 goto out_err;
605
606 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
607 timeo = caif_wait_for_flow_on(cf_sk, 1, timeo, &err);
608
609 if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN))
610 goto pipe_err;
611
612 while (sent < len) {
613
614 size = len-sent;
615
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000616 if (size > cf_sk->maxframe)
617 size = cf_sk->maxframe;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000618
619 /* If size is more than half of sndbuf, chop up message */
620 if (size > ((sk->sk_sndbuf >> 1) - 64))
621 size = (sk->sk_sndbuf >> 1) - 64;
622
623 if (size > SKB_MAX_ALLOC)
624 size = SKB_MAX_ALLOC;
625
626 skb = sock_alloc_send_skb(sk,
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000627 size + cf_sk->headroom +
628 cf_sk->tailroom,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000629 msg->msg_flags&MSG_DONTWAIT,
630 &err);
631 if (skb == NULL)
632 goto out_err;
633
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000634 skb_reserve(skb, cf_sk->headroom);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000635 /*
636 * If you pass two values to the sock_alloc_send_skb
637 * it tries to grab the large buffer with GFP_NOFS
638 * (which can fail easily), and if it fails grab the
639 * fallback size buffer which is under a page and will
640 * succeed. [Alan]
641 */
642 size = min_t(int, size, skb_tailroom(skb));
643
Al Viro6ce8e9c2014-04-06 21:25:44 -0400644 err = memcpy_from_msg(skb_put(skb, size), msg, size);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000645 if (err) {
646 kfree_skb(skb);
647 goto out_err;
648 }
649 err = transmit_skb(skb, cf_sk,
650 msg->msg_flags&MSG_DONTWAIT, timeo);
Dmitry Tarnyaginba760572012-02-02 01:21:03 +0000651 if (err < 0)
652 /* skb is already freed */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000653 goto pipe_err;
Dmitry Tarnyaginba760572012-02-02 01:21:03 +0000654
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000655 sent += size;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000656 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000657
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000658 return sent;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000659
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000660pipe_err:
661 if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL))
662 send_sig(SIGPIPE, current, 0);
663 err = -EPIPE;
664out_err:
665 return sent ? : err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000666}
667
668static int setsockopt(struct socket *sock,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000669 int lvl, int opt, char __user *ov, unsigned int ol)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000670{
671 struct sock *sk = sock->sk;
672 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +0000673 int linksel;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000674
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000675 if (cf_sk->sk.sk_socket->state != SS_UNCONNECTED)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000676 return -ENOPROTOOPT;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000677
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000678 switch (opt) {
679 case CAIFSO_LINK_SELECT:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000680 if (ol < sizeof(int))
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000681 return -EINVAL;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000682 if (lvl != SOL_CAIF)
683 goto bad_sol;
684 if (copy_from_user(&linksel, ov, sizeof(int)))
685 return -EINVAL;
686 lock_sock(&(cf_sk->sk));
687 cf_sk->conn_req.link_selector = linksel;
688 release_sock(&cf_sk->sk);
689 return 0;
690
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000691 case CAIFSO_REQ_PARAM:
692 if (lvl != SOL_CAIF)
693 goto bad_sol;
694 if (cf_sk->sk.sk_protocol != CAIFPROTO_UTIL)
695 return -ENOPROTOOPT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000696 lock_sock(&(cf_sk->sk));
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000697 if (ol > sizeof(cf_sk->conn_req.param.data) ||
698 copy_from_user(&cf_sk->conn_req.param.data, ov, ol)) {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000699 release_sock(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000700 return -EINVAL;
701 }
Dan Rosenberg91b5c982011-01-10 16:00:54 -0800702 cf_sk->conn_req.param.size = ol;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000703 release_sock(&cf_sk->sk);
704 return 0;
705
706 default:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000707 return -ENOPROTOOPT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000708 }
709
710 return 0;
711bad_sol:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000712 return -ENOPROTOOPT;
713
714}
715
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000716/*
717 * caif_connect() - Connect a CAIF Socket
718 * Copied and modified af_irda.c:irda_connect().
719 *
720 * Note : by consulting "errno", the user space caller may learn the cause
721 * of the failure. Most of them are visible in the function, others may come
722 * from subroutines called and are listed here :
723 * o -EAFNOSUPPORT: bad socket family or type.
724 * o -ESOCKTNOSUPPORT: bad socket type or protocol
725 * o -EINVAL: bad socket address, or CAIF link type
726 * o -ECONNREFUSED: remote end refused the connection.
727 * o -EINPROGRESS: connect request sent but timed out (or non-blocking)
728 * o -EISCONN: already connected.
729 * o -ETIMEDOUT: Connection timed out (send timeout)
730 * o -ENODEV: No link layer to send request
731 * o -ECONNRESET: Received Shutdown indication or lost link layer
732 * o -ENOMEM: Out of memory
733 *
734 * State Strategy:
735 * o sk_state: holds the CAIF_* protocol state, it's updated by
736 * caif_ctrl_cb.
737 * o sock->state: holds the SS_* socket state and is updated by connect and
738 * disconnect.
739 */
740static int caif_connect(struct socket *sock, struct sockaddr *uaddr,
741 int addr_len, int flags)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000742{
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000743 struct sock *sk = sock->sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000744 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
745 long timeo;
746 int err;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000747 int ifindex, headroom, tailroom;
Eric Dumazet79315062010-10-04 22:42:08 +0000748 unsigned int mtu;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000749 struct net_device *dev;
750
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000751 lock_sock(sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000752
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000753 err = -EAFNOSUPPORT;
754 if (uaddr->sa_family != AF_CAIF)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000755 goto out;
756
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000757 switch (sock->state) {
758 case SS_UNCONNECTED:
759 /* Normal case, a fresh connect */
760 caif_assert(sk->sk_state == CAIF_DISCONNECTED);
761 break;
762 case SS_CONNECTING:
763 switch (sk->sk_state) {
764 case CAIF_CONNECTED:
765 sock->state = SS_CONNECTED;
766 err = -EISCONN;
767 goto out;
768 case CAIF_DISCONNECTED:
769 /* Reconnect allowed */
770 break;
771 case CAIF_CONNECTING:
772 err = -EALREADY;
773 if (flags & O_NONBLOCK)
774 goto out;
775 goto wait_connect;
776 }
777 break;
778 case SS_CONNECTED:
779 caif_assert(sk->sk_state == CAIF_CONNECTED ||
780 sk->sk_state == CAIF_DISCONNECTED);
781 if (sk->sk_shutdown & SHUTDOWN_MASK) {
782 /* Allow re-connect after SHUTDOWN_IND */
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000783 caif_disconnect_client(sock_net(sk), &cf_sk->layer);
sjur.brandeland@stericsson.com54e90fb2011-05-22 11:18:51 +0000784 caif_free_client(&cf_sk->layer);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000785 break;
786 }
787 /* No reconnect on a seqpacket socket */
788 err = -EISCONN;
789 goto out;
790 case SS_DISCONNECTING:
791 case SS_FREE:
792 caif_assert(1); /*Should never happen */
793 break;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000794 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000795 sk->sk_state = CAIF_DISCONNECTED;
796 sock->state = SS_UNCONNECTED;
797 sk_stream_kill_queues(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000798
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000799 err = -EINVAL;
Dan Carpenterf5d72af2010-05-22 10:43:42 +0000800 if (addr_len != sizeof(struct sockaddr_caif))
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000801 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000802
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000803 memcpy(&cf_sk->conn_req.sockaddr, uaddr,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000804 sizeof(struct sockaddr_caif));
805
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000806 /* Move to connecting socket, start sending Connect Requests */
807 sock->state = SS_CONNECTING;
808 sk->sk_state = CAIF_CONNECTING;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000809
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +0000810 /* Check priority value comming from socket */
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +0000811 /* if priority value is out of range it will be ajusted */
812 if (cf_sk->sk.sk_priority > CAIF_PRIO_MAX)
813 cf_sk->conn_req.priority = CAIF_PRIO_MAX;
814 else if (cf_sk->sk.sk_priority < CAIF_PRIO_MIN)
815 cf_sk->conn_req.priority = CAIF_PRIO_MIN;
816 else
817 cf_sk->conn_req.priority = cf_sk->sk.sk_priority;
818
819 /*ifindex = id of the interface.*/
820 cf_sk->conn_req.ifindex = cf_sk->sk.sk_bound_dev_if;
821
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000822 cf_sk->layer.receive = caif_sktrecv_cb;
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000823
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000824 err = caif_connect_client(sock_net(sk), &cf_sk->conn_req,
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000825 &cf_sk->layer, &ifindex, &headroom, &tailroom);
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000826
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000827 if (err < 0) {
828 cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
829 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
830 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000831 }
Eric Dumazet79315062010-10-04 22:42:08 +0000832
833 err = -ENODEV;
834 rcu_read_lock();
835 dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
836 if (!dev) {
837 rcu_read_unlock();
838 goto out;
839 }
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000840 cf_sk->headroom = LL_RESERVED_SPACE_EXTRA(dev, headroom);
Eric Dumazet79315062010-10-04 22:42:08 +0000841 mtu = dev->mtu;
842 rcu_read_unlock();
843
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000844 cf_sk->tailroom = tailroom;
Eric Dumazet79315062010-10-04 22:42:08 +0000845 cf_sk->maxframe = mtu - (headroom + tailroom);
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000846 if (cf_sk->maxframe < 1) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000847 pr_warn("CAIF Interface MTU too small (%d)\n", dev->mtu);
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +0000848 err = -ENODEV;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000849 goto out;
850 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000851
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000852 err = -EINPROGRESS;
853wait_connect:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000854
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000855 if (sk->sk_state != CAIF_CONNECTED && (flags & O_NONBLOCK))
856 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000857
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000858 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000859
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000860 release_sock(sk);
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000861 err = -ERESTARTSYS;
862 timeo = wait_event_interruptible_timeout(*sk_sleep(sk),
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000863 sk->sk_state != CAIF_CONNECTING,
864 timeo);
865 lock_sock(sk);
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000866 if (timeo < 0)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000867 goto out; /* -ERESTARTSYS */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000868
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000869 err = -ETIMEDOUT;
870 if (timeo == 0 && sk->sk_state != CAIF_CONNECTED)
871 goto out;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000872 if (sk->sk_state != CAIF_CONNECTED) {
873 sock->state = SS_UNCONNECTED;
874 err = sock_error(sk);
875 if (!err)
876 err = -ECONNREFUSED;
877 goto out;
878 }
879 sock->state = SS_CONNECTED;
880 err = 0;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000881out:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000882 release_sock(sk);
883 return err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000884}
885
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000886/*
887 * caif_release() - Disconnect a CAIF Socket
888 * Copied and modified af_irda.c:irda_release().
889 */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000890static int caif_release(struct socket *sock)
891{
892 struct sock *sk = sock->sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000893 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000894
895 if (!sk)
896 return 0;
897
898 set_tx_flow_off(cf_sk);
899
900 /*
901 * Ensure that packets are not queued after this point in time.
902 * caif_queue_rcv_skb checks SOCK_DEAD holding the queue lock,
903 * this ensures no packets when sock is dead.
904 */
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000905 spin_lock_bh(&sk->sk_receive_queue.lock);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000906 sock_set_flag(sk, SOCK_DEAD);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000907 spin_unlock_bh(&sk->sk_receive_queue.lock);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000908 sock->sk = NULL;
909
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +0000910 WARN_ON(IS_ERR(cf_sk->debugfs_socket_dir));
Fabian Frederickfb0d1642014-06-27 23:07:43 +0200911 debugfs_remove_recursive(cf_sk->debugfs_socket_dir);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000912
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000913 lock_sock(&(cf_sk->sk));
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000914 sk->sk_state = CAIF_DISCONNECTED;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000915 sk->sk_shutdown = SHUTDOWN_MASK;
916
sjur.brandeland@stericsson.com54e90fb2011-05-22 11:18:51 +0000917 caif_disconnect_client(sock_net(sk), &cf_sk->layer);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000918 cf_sk->sk.sk_socket->state = SS_DISCONNECTING;
919 wake_up_interruptible_poll(sk_sleep(sk), POLLERR|POLLHUP);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000920
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000921 sock_orphan(sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000922 sk_stream_kill_queues(&cf_sk->sk);
923 release_sock(sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000924 sock_put(sk);
sjur.brandeland@stericsson.com54e90fb2011-05-22 11:18:51 +0000925 return 0;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000926}
927
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000928/* Copied from af_unix.c:unix_poll(), added CAIF tx_flow handling */
929static unsigned int caif_poll(struct file *file,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000930 struct socket *sock, poll_table *wait)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000931{
932 struct sock *sk = sock->sk;
933 unsigned int mask;
934 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
935
936 sock_poll_wait(file, sk_sleep(sk), wait);
937 mask = 0;
938
939 /* exceptional events? */
940 if (sk->sk_err)
941 mask |= POLLERR;
942 if (sk->sk_shutdown == SHUTDOWN_MASK)
943 mask |= POLLHUP;
944 if (sk->sk_shutdown & RCV_SHUTDOWN)
945 mask |= POLLRDHUP;
946
947 /* readable? */
948 if (!skb_queue_empty(&sk->sk_receive_queue) ||
949 (sk->sk_shutdown & RCV_SHUTDOWN))
950 mask |= POLLIN | POLLRDNORM;
951
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000952 /*
953 * we set writable also when the other side has shut down the
954 * connection. This prevents stuck sockets.
955 */
956 if (sock_writeable(sk) && tx_flow_is_on(cf_sk))
957 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
958
959 return mask;
960}
961
962static const struct proto_ops caif_seqpacket_ops = {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000963 .family = PF_CAIF,
964 .owner = THIS_MODULE,
965 .release = caif_release,
966 .bind = sock_no_bind,
967 .connect = caif_connect,
968 .socketpair = sock_no_socketpair,
969 .accept = sock_no_accept,
970 .getname = sock_no_getname,
971 .poll = caif_poll,
972 .ioctl = sock_no_ioctl,
973 .listen = sock_no_listen,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000974 .shutdown = sock_no_shutdown,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000975 .setsockopt = setsockopt,
976 .getsockopt = sock_no_getsockopt,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000977 .sendmsg = caif_seqpkt_sendmsg,
978 .recvmsg = caif_seqpkt_recvmsg,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000979 .mmap = sock_no_mmap,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000980 .sendpage = sock_no_sendpage,
981};
982
983static const struct proto_ops caif_stream_ops = {
984 .family = PF_CAIF,
985 .owner = THIS_MODULE,
986 .release = caif_release,
987 .bind = sock_no_bind,
988 .connect = caif_connect,
989 .socketpair = sock_no_socketpair,
990 .accept = sock_no_accept,
991 .getname = sock_no_getname,
992 .poll = caif_poll,
993 .ioctl = sock_no_ioctl,
994 .listen = sock_no_listen,
995 .shutdown = sock_no_shutdown,
996 .setsockopt = setsockopt,
997 .getsockopt = sock_no_getsockopt,
998 .sendmsg = caif_stream_sendmsg,
999 .recvmsg = caif_stream_recvmsg,
1000 .mmap = sock_no_mmap,
1001 .sendpage = sock_no_sendpage,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001002};
1003
1004/* This function is called when a socket is finally destroyed. */
1005static void caif_sock_destructor(struct sock *sk)
1006{
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001007 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001008 caif_assert(!atomic_read(&sk->sk_wmem_alloc));
1009 caif_assert(sk_unhashed(sk));
1010 caif_assert(!sk->sk_socket);
1011 if (!sock_flag(sk, SOCK_DEAD)) {
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +00001012 pr_debug("Attempt to release alive CAIF socket: %p\n", sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001013 return;
1014 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001015 sk_stream_kill_queues(&cf_sk->sk);
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +00001016 caif_free_client(&cf_sk->layer);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001017}
1018
1019static int caif_create(struct net *net, struct socket *sock, int protocol,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +00001020 int kern)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001021{
1022 struct sock *sk = NULL;
1023 struct caifsock *cf_sk = NULL;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001024 static struct proto prot = {.name = "PF_CAIF",
1025 .owner = THIS_MODULE,
1026 .obj_size = sizeof(struct caifsock),
1027 };
1028
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001029 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_NET_ADMIN))
1030 return -EPERM;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001031 /*
1032 * The sock->type specifies the socket type to use.
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001033 * The CAIF socket is a packet stream in the sense
1034 * that it is packet based. CAIF trusts the reliability
1035 * of the link, no resending is implemented.
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001036 */
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001037 if (sock->type == SOCK_SEQPACKET)
1038 sock->ops = &caif_seqpacket_ops;
1039 else if (sock->type == SOCK_STREAM)
1040 sock->ops = &caif_stream_ops;
1041 else
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001042 return -ESOCKTNOSUPPORT;
1043
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001044 if (protocol < 0 || protocol >= CAIFPROTO_MAX)
1045 return -EPROTONOSUPPORT;
1046 /*
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001047 * Set the socket state to unconnected. The socket state
1048 * is really not used at all in the net/core or socket.c but the
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001049 * initialization makes sure that sock->state is not uninitialized.
1050 */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001051 sk = sk_alloc(net, PF_CAIF, GFP_KERNEL, &prot);
1052 if (!sk)
1053 return -ENOMEM;
1054
1055 cf_sk = container_of(sk, struct caifsock, sk);
1056
1057 /* Store the protocol */
1058 sk->sk_protocol = (unsigned char) protocol;
1059
Dmitry Tarnyagin44764812012-04-12 08:27:24 +00001060 /* Initialize default priority for well-known cases */
1061 switch (protocol) {
1062 case CAIFPROTO_AT:
1063 sk->sk_priority = TC_PRIO_CONTROL;
1064 break;
1065 case CAIFPROTO_RFM:
1066 sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
1067 break;
1068 default:
1069 sk->sk_priority = TC_PRIO_BESTEFFORT;
1070 }
1071
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001072 /*
1073 * Lock in order to try to stop someone from opening the socket
1074 * too early.
1075 */
1076 lock_sock(&(cf_sk->sk));
1077
1078 /* Initialize the nozero default sock structure data. */
1079 sock_init_data(sock, sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001080 sk->sk_destruct = caif_sock_destructor;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001081
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001082 mutex_init(&cf_sk->readlock); /* single task reading lock */
1083 cf_sk->layer.ctrlcmd = caif_ctrl_cb;
1084 cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
1085 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001086
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001087 set_tx_flow_off(cf_sk);
1088 set_rx_flow_on(cf_sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001089
1090 /* Set default options on configuration */
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001091 cf_sk->conn_req.link_selector = CAIF_LINK_LOW_LATENCY;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001092 cf_sk->conn_req.protocol = protocol;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001093 release_sock(&cf_sk->sk);
1094 return 0;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001095}
1096
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001097
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001098static struct net_proto_family caif_family_ops = {
1099 .family = PF_CAIF,
1100 .create = caif_create,
1101 .owner = THIS_MODULE,
1102};
1103
sjur.brandeland@stericsson.com4a695822012-02-03 04:36:19 +00001104static int __init caif_sktinit_module(void)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001105{
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001106 int err = sock_register(&caif_family_ops);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001107 if (!err)
1108 return err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001109 return 0;
1110}
1111
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001112static void __exit caif_sktexit_module(void)
1113{
1114 sock_unregister(PF_CAIF);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001115}
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001116module_init(caif_sktinit_module);
1117module_exit(caif_sktexit_module);