blob: 3cc71b9f551756ca63b1299e95d9b6424e5afb72 [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 */
Ying Xue1b784142015-03-02 15:37:48 +0800274static int caif_seqpkt_recvmsg(struct socket *sock, struct msghdr *m,
275 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;
Al Viro3eeff772015-03-14 05:22:21 +0000284 if (flags & MSG_OOB)
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000285 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);
Mark Salyzynb48732e2015-05-26 08:22:19 -0700333
334 if (sock_flag(sk, SOCK_DEAD))
335 break;
336
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000337 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
338 }
339
340 finish_wait(sk_sleep(sk), &wait);
341 release_sock(sk);
342 return timeo;
343}
344
345
346/*
347 * Copied from unix_stream_recvmsg, but removed credit checks,
348 * changed locking calls, changed address handling.
349 */
Ying Xue1b784142015-03-02 15:37:48 +0800350static int caif_stream_recvmsg(struct socket *sock, struct msghdr *msg,
351 size_t size, int flags)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000352{
353 struct sock *sk = sock->sk;
354 int copied = 0;
355 int target;
356 int err = 0;
357 long timeo;
358
359 err = -EOPNOTSUPP;
360 if (flags&MSG_OOB)
361 goto out;
362
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000363 /*
364 * Lock the socket to prevent queue disordering
365 * while sleeps in memcpy_tomsg
366 */
367 err = -EAGAIN;
368 if (sk->sk_state == CAIF_CONNECTING)
369 goto out;
370
371 caif_read_lock(sk);
372 target = sock_rcvlowat(sk, flags&MSG_WAITALL, size);
373 timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
374
375 do {
376 int chunk;
377 struct sk_buff *skb;
378
379 lock_sock(sk);
Mark Salyzynb48732e2015-05-26 08:22:19 -0700380 if (sock_flag(sk, SOCK_DEAD)) {
381 err = -ECONNRESET;
382 goto unlock;
383 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000384 skb = skb_dequeue(&sk->sk_receive_queue);
385 caif_check_flow_release(sk);
386
387 if (skb == NULL) {
388 if (copied >= target)
389 goto unlock;
390 /*
391 * POSIX 1003.1g mandates this order.
392 */
393 err = sock_error(sk);
394 if (err)
395 goto unlock;
396 err = -ECONNRESET;
397 if (sk->sk_shutdown & RCV_SHUTDOWN)
398 goto unlock;
399
400 err = -EPIPE;
401 if (sk->sk_state != CAIF_CONNECTED)
402 goto unlock;
403 if (sock_flag(sk, SOCK_DEAD))
404 goto unlock;
405
406 release_sock(sk);
407
408 err = -EAGAIN;
409 if (!timeo)
410 break;
411
412 caif_read_unlock(sk);
413
414 timeo = caif_stream_data_wait(sk, timeo);
415
416 if (signal_pending(current)) {
417 err = sock_intr_errno(timeo);
418 goto out;
419 }
420 caif_read_lock(sk);
421 continue;
422unlock:
423 release_sock(sk);
424 break;
425 }
426 release_sock(sk);
427 chunk = min_t(unsigned int, skb->len, size);
Al Viro7eab8d92014-04-06 21:51:23 -0400428 if (memcpy_to_msg(msg, skb->data, chunk)) {
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000429 skb_queue_head(&sk->sk_receive_queue, skb);
430 if (copied == 0)
431 copied = -EFAULT;
432 break;
433 }
434 copied += chunk;
435 size -= chunk;
436
437 /* Mark read part of skb as used */
438 if (!(flags & MSG_PEEK)) {
439 skb_pull(skb, chunk);
440
441 /* put the skb back if we didn't use it up. */
442 if (skb->len) {
443 skb_queue_head(&sk->sk_receive_queue, skb);
444 break;
445 }
446 kfree_skb(skb);
447
448 } else {
449 /*
450 * It is questionable, see note in unix_dgram_recvmsg.
451 */
452 /* put message back and return */
453 skb_queue_head(&sk->sk_receive_queue, skb);
454 break;
455 }
456 } while (size);
457 caif_read_unlock(sk);
458
459out:
460 return copied ? : err;
461}
462
463/*
464 * Copied from sock.c:sock_wait_for_wmem, but change to wait for
465 * CAIF flow-on and sock_writable.
466 */
467static long caif_wait_for_flow_on(struct caifsock *cf_sk,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000468 int wait_writeable, long timeo, int *err)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000469{
470 struct sock *sk = &cf_sk->sk;
471 DEFINE_WAIT(wait);
472 for (;;) {
473 *err = 0;
474 if (tx_flow_is_on(cf_sk) &&
475 (!wait_writeable || sock_writeable(&cf_sk->sk)))
476 break;
477 *err = -ETIMEDOUT;
478 if (!timeo)
479 break;
480 *err = -ERESTARTSYS;
481 if (signal_pending(current))
482 break;
483 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
484 *err = -ECONNRESET;
485 if (sk->sk_shutdown & SHUTDOWN_MASK)
486 break;
487 *err = -sk->sk_err;
488 if (sk->sk_err)
489 break;
490 *err = -EPIPE;
491 if (cf_sk->sk.sk_state != CAIF_CONNECTED)
492 break;
493 timeo = schedule_timeout(timeo);
494 }
495 finish_wait(sk_sleep(sk), &wait);
496 return timeo;
497}
498
499/*
500 * Transmit a SKB. The device may temporarily request re-transmission
501 * by returning EAGAIN.
502 */
503static int transmit_skb(struct sk_buff *skb, struct caifsock *cf_sk,
504 int noblock, long timeo)
505{
506 struct cfpkt *pkt;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000507
508 pkt = cfpkt_fromnative(CAIF_DIR_OUT, skb);
sjur.brandeland@stericsson.com3f874ad2011-05-13 02:44:08 +0000509 memset(skb->cb, 0, sizeof(struct caif_payload_info));
Dmitry Tarnyagin44764812012-04-12 08:27:24 +0000510 cfpkt_set_prio(pkt, cf_sk->sk.sk_priority);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000511
Dmitry Tarnyaginba760572012-02-02 01:21:03 +0000512 if (cf_sk->layer.dn == NULL) {
513 kfree_skb(skb);
Sjur Brændeland4dd820c2011-04-11 10:43:51 +0000514 return -EINVAL;
Dmitry Tarnyaginba760572012-02-02 01:21:03 +0000515 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000516
Sjur Brændeland4dd820c2011-04-11 10:43:51 +0000517 return cf_sk->layer.dn->transmit(cf_sk->layer.dn, pkt);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000518}
519
520/* Copied from af_unix:unix_dgram_sendmsg, and adapted to CAIF */
Ying Xue1b784142015-03-02 15:37:48 +0800521static int caif_seqpkt_sendmsg(struct socket *sock, struct msghdr *msg,
522 size_t len)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000523{
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000524 struct sock *sk = sock->sk;
525 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000526 int buffer_size;
527 int ret = 0;
528 struct sk_buff *skb = NULL;
529 int noblock;
530 long timeo;
531 caif_assert(cf_sk);
532 ret = sock_error(sk);
533 if (ret)
534 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000535
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000536 ret = -EOPNOTSUPP;
537 if (msg->msg_flags&MSG_OOB)
538 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000539
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000540 ret = -EOPNOTSUPP;
541 if (msg->msg_namelen)
542 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000543
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000544 ret = -EINVAL;
Al Viroc0371da2014-11-24 10:42:55 -0500545 if (unlikely(msg->msg_iter.iov->iov_base == NULL))
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000546 goto err;
547 noblock = msg->msg_flags & MSG_DONTWAIT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000548
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000549 timeo = sock_sndtimeo(sk, noblock);
550 timeo = caif_wait_for_flow_on(container_of(sk, struct caifsock, sk),
551 1, timeo, &ret);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000552
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000553 if (ret)
554 goto err;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000555 ret = -EPIPE;
556 if (cf_sk->sk.sk_state != CAIF_CONNECTED ||
557 sock_flag(sk, SOCK_DEAD) ||
558 (sk->sk_shutdown & RCV_SHUTDOWN))
559 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000560
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000561 /* Error if trying to write more than maximum frame size. */
562 ret = -EMSGSIZE;
563 if (len > cf_sk->maxframe && cf_sk->sk.sk_protocol != CAIFPROTO_RFM)
564 goto err;
565
566 buffer_size = len + cf_sk->headroom + cf_sk->tailroom;
567
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000568 ret = -ENOMEM;
569 skb = sock_alloc_send_skb(sk, buffer_size, noblock, &ret);
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000570
571 if (!skb || skb_tailroom(skb) < buffer_size)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000572 goto err;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000573
574 skb_reserve(skb, cf_sk->headroom);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000575
Al Viro6ce8e9c2014-04-06 21:25:44 -0400576 ret = memcpy_from_msg(skb_put(skb, len), msg, len);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000577
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000578 if (ret)
579 goto err;
580 ret = transmit_skb(skb, cf_sk, noblock, timeo);
581 if (ret < 0)
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000582 /* skb is already freed */
583 return ret;
584
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000585 return len;
586err:
587 kfree_skb(skb);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000588 return ret;
589}
590
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000591/*
592 * Copied from unix_stream_sendmsg and adapted to CAIF:
593 * Changed removed permission handling and added waiting for flow on
594 * and other minor adaptations.
595 */
Ying Xue1b784142015-03-02 15:37:48 +0800596static int caif_stream_sendmsg(struct socket *sock, struct msghdr *msg,
597 size_t len)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000598{
599 struct sock *sk = sock->sk;
600 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000601 int err, size;
602 struct sk_buff *skb;
603 int sent = 0;
604 long timeo;
605
606 err = -EOPNOTSUPP;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000607 if (unlikely(msg->msg_flags&MSG_OOB))
608 goto out_err;
609
610 if (unlikely(msg->msg_namelen))
611 goto out_err;
612
613 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
614 timeo = caif_wait_for_flow_on(cf_sk, 1, timeo, &err);
615
616 if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN))
617 goto pipe_err;
618
619 while (sent < len) {
620
621 size = len-sent;
622
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000623 if (size > cf_sk->maxframe)
624 size = cf_sk->maxframe;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000625
626 /* If size is more than half of sndbuf, chop up message */
627 if (size > ((sk->sk_sndbuf >> 1) - 64))
628 size = (sk->sk_sndbuf >> 1) - 64;
629
630 if (size > SKB_MAX_ALLOC)
631 size = SKB_MAX_ALLOC;
632
633 skb = sock_alloc_send_skb(sk,
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000634 size + cf_sk->headroom +
635 cf_sk->tailroom,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000636 msg->msg_flags&MSG_DONTWAIT,
637 &err);
638 if (skb == NULL)
639 goto out_err;
640
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000641 skb_reserve(skb, cf_sk->headroom);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000642 /*
643 * If you pass two values to the sock_alloc_send_skb
644 * it tries to grab the large buffer with GFP_NOFS
645 * (which can fail easily), and if it fails grab the
646 * fallback size buffer which is under a page and will
647 * succeed. [Alan]
648 */
649 size = min_t(int, size, skb_tailroom(skb));
650
Al Viro6ce8e9c2014-04-06 21:25:44 -0400651 err = memcpy_from_msg(skb_put(skb, size), msg, size);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000652 if (err) {
653 kfree_skb(skb);
654 goto out_err;
655 }
656 err = transmit_skb(skb, cf_sk,
657 msg->msg_flags&MSG_DONTWAIT, timeo);
Dmitry Tarnyaginba760572012-02-02 01:21:03 +0000658 if (err < 0)
659 /* skb is already freed */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000660 goto pipe_err;
Dmitry Tarnyaginba760572012-02-02 01:21:03 +0000661
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000662 sent += size;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000663 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000664
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000665 return sent;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000666
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000667pipe_err:
668 if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL))
669 send_sig(SIGPIPE, current, 0);
670 err = -EPIPE;
671out_err:
672 return sent ? : err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000673}
674
675static int setsockopt(struct socket *sock,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000676 int lvl, int opt, char __user *ov, unsigned int ol)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000677{
678 struct sock *sk = sock->sk;
679 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +0000680 int linksel;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000681
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000682 if (cf_sk->sk.sk_socket->state != SS_UNCONNECTED)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000683 return -ENOPROTOOPT;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000684
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000685 switch (opt) {
686 case CAIFSO_LINK_SELECT:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000687 if (ol < sizeof(int))
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000688 return -EINVAL;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000689 if (lvl != SOL_CAIF)
690 goto bad_sol;
691 if (copy_from_user(&linksel, ov, sizeof(int)))
692 return -EINVAL;
693 lock_sock(&(cf_sk->sk));
694 cf_sk->conn_req.link_selector = linksel;
695 release_sock(&cf_sk->sk);
696 return 0;
697
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000698 case CAIFSO_REQ_PARAM:
699 if (lvl != SOL_CAIF)
700 goto bad_sol;
701 if (cf_sk->sk.sk_protocol != CAIFPROTO_UTIL)
702 return -ENOPROTOOPT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000703 lock_sock(&(cf_sk->sk));
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000704 if (ol > sizeof(cf_sk->conn_req.param.data) ||
705 copy_from_user(&cf_sk->conn_req.param.data, ov, ol)) {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000706 release_sock(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000707 return -EINVAL;
708 }
Dan Rosenberg91b5c982011-01-10 16:00:54 -0800709 cf_sk->conn_req.param.size = ol;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000710 release_sock(&cf_sk->sk);
711 return 0;
712
713 default:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000714 return -ENOPROTOOPT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000715 }
716
717 return 0;
718bad_sol:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000719 return -ENOPROTOOPT;
720
721}
722
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000723/*
724 * caif_connect() - Connect a CAIF Socket
725 * Copied and modified af_irda.c:irda_connect().
726 *
727 * Note : by consulting "errno", the user space caller may learn the cause
728 * of the failure. Most of them are visible in the function, others may come
729 * from subroutines called and are listed here :
730 * o -EAFNOSUPPORT: bad socket family or type.
731 * o -ESOCKTNOSUPPORT: bad socket type or protocol
732 * o -EINVAL: bad socket address, or CAIF link type
733 * o -ECONNREFUSED: remote end refused the connection.
734 * o -EINPROGRESS: connect request sent but timed out (or non-blocking)
735 * o -EISCONN: already connected.
736 * o -ETIMEDOUT: Connection timed out (send timeout)
737 * o -ENODEV: No link layer to send request
738 * o -ECONNRESET: Received Shutdown indication or lost link layer
739 * o -ENOMEM: Out of memory
740 *
741 * State Strategy:
742 * o sk_state: holds the CAIF_* protocol state, it's updated by
743 * caif_ctrl_cb.
744 * o sock->state: holds the SS_* socket state and is updated by connect and
745 * disconnect.
746 */
747static int caif_connect(struct socket *sock, struct sockaddr *uaddr,
748 int addr_len, int flags)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000749{
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000750 struct sock *sk = sock->sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000751 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
752 long timeo;
753 int err;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000754 int ifindex, headroom, tailroom;
Eric Dumazet79315062010-10-04 22:42:08 +0000755 unsigned int mtu;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000756 struct net_device *dev;
757
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000758 lock_sock(sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000759
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000760 err = -EAFNOSUPPORT;
761 if (uaddr->sa_family != AF_CAIF)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000762 goto out;
763
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000764 switch (sock->state) {
765 case SS_UNCONNECTED:
766 /* Normal case, a fresh connect */
767 caif_assert(sk->sk_state == CAIF_DISCONNECTED);
768 break;
769 case SS_CONNECTING:
770 switch (sk->sk_state) {
771 case CAIF_CONNECTED:
772 sock->state = SS_CONNECTED;
773 err = -EISCONN;
774 goto out;
775 case CAIF_DISCONNECTED:
776 /* Reconnect allowed */
777 break;
778 case CAIF_CONNECTING:
779 err = -EALREADY;
780 if (flags & O_NONBLOCK)
781 goto out;
782 goto wait_connect;
783 }
784 break;
785 case SS_CONNECTED:
786 caif_assert(sk->sk_state == CAIF_CONNECTED ||
787 sk->sk_state == CAIF_DISCONNECTED);
788 if (sk->sk_shutdown & SHUTDOWN_MASK) {
789 /* Allow re-connect after SHUTDOWN_IND */
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000790 caif_disconnect_client(sock_net(sk), &cf_sk->layer);
sjur.brandeland@stericsson.com54e90fb2011-05-22 11:18:51 +0000791 caif_free_client(&cf_sk->layer);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000792 break;
793 }
794 /* No reconnect on a seqpacket socket */
795 err = -EISCONN;
796 goto out;
797 case SS_DISCONNECTING:
798 case SS_FREE:
799 caif_assert(1); /*Should never happen */
800 break;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000801 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000802 sk->sk_state = CAIF_DISCONNECTED;
803 sock->state = SS_UNCONNECTED;
804 sk_stream_kill_queues(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000805
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000806 err = -EINVAL;
Dan Carpenterf5d72af2010-05-22 10:43:42 +0000807 if (addr_len != sizeof(struct sockaddr_caif))
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000808 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000809
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000810 memcpy(&cf_sk->conn_req.sockaddr, uaddr,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000811 sizeof(struct sockaddr_caif));
812
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000813 /* Move to connecting socket, start sending Connect Requests */
814 sock->state = SS_CONNECTING;
815 sk->sk_state = CAIF_CONNECTING;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000816
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +0000817 /* Check priority value comming from socket */
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +0000818 /* if priority value is out of range it will be ajusted */
819 if (cf_sk->sk.sk_priority > CAIF_PRIO_MAX)
820 cf_sk->conn_req.priority = CAIF_PRIO_MAX;
821 else if (cf_sk->sk.sk_priority < CAIF_PRIO_MIN)
822 cf_sk->conn_req.priority = CAIF_PRIO_MIN;
823 else
824 cf_sk->conn_req.priority = cf_sk->sk.sk_priority;
825
826 /*ifindex = id of the interface.*/
827 cf_sk->conn_req.ifindex = cf_sk->sk.sk_bound_dev_if;
828
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000829 cf_sk->layer.receive = caif_sktrecv_cb;
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000830
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000831 err = caif_connect_client(sock_net(sk), &cf_sk->conn_req,
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000832 &cf_sk->layer, &ifindex, &headroom, &tailroom);
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000833
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000834 if (err < 0) {
835 cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
836 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
837 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000838 }
Eric Dumazet79315062010-10-04 22:42:08 +0000839
840 err = -ENODEV;
841 rcu_read_lock();
842 dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
843 if (!dev) {
844 rcu_read_unlock();
845 goto out;
846 }
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000847 cf_sk->headroom = LL_RESERVED_SPACE_EXTRA(dev, headroom);
Eric Dumazet79315062010-10-04 22:42:08 +0000848 mtu = dev->mtu;
849 rcu_read_unlock();
850
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000851 cf_sk->tailroom = tailroom;
Eric Dumazet79315062010-10-04 22:42:08 +0000852 cf_sk->maxframe = mtu - (headroom + tailroom);
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000853 if (cf_sk->maxframe < 1) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000854 pr_warn("CAIF Interface MTU too small (%d)\n", dev->mtu);
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +0000855 err = -ENODEV;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000856 goto out;
857 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000858
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000859 err = -EINPROGRESS;
860wait_connect:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000861
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000862 if (sk->sk_state != CAIF_CONNECTED && (flags & O_NONBLOCK))
863 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000864
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000865 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000866
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000867 release_sock(sk);
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000868 err = -ERESTARTSYS;
869 timeo = wait_event_interruptible_timeout(*sk_sleep(sk),
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000870 sk->sk_state != CAIF_CONNECTING,
871 timeo);
872 lock_sock(sk);
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000873 if (timeo < 0)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000874 goto out; /* -ERESTARTSYS */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000875
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000876 err = -ETIMEDOUT;
877 if (timeo == 0 && sk->sk_state != CAIF_CONNECTED)
878 goto out;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000879 if (sk->sk_state != CAIF_CONNECTED) {
880 sock->state = SS_UNCONNECTED;
881 err = sock_error(sk);
882 if (!err)
883 err = -ECONNREFUSED;
884 goto out;
885 }
886 sock->state = SS_CONNECTED;
887 err = 0;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000888out:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000889 release_sock(sk);
890 return err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000891}
892
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000893/*
894 * caif_release() - Disconnect a CAIF Socket
895 * Copied and modified af_irda.c:irda_release().
896 */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000897static int caif_release(struct socket *sock)
898{
899 struct sock *sk = sock->sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000900 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000901
902 if (!sk)
903 return 0;
904
905 set_tx_flow_off(cf_sk);
906
907 /*
908 * Ensure that packets are not queued after this point in time.
909 * caif_queue_rcv_skb checks SOCK_DEAD holding the queue lock,
910 * this ensures no packets when sock is dead.
911 */
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000912 spin_lock_bh(&sk->sk_receive_queue.lock);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000913 sock_set_flag(sk, SOCK_DEAD);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000914 spin_unlock_bh(&sk->sk_receive_queue.lock);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000915 sock->sk = NULL;
916
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +0000917 WARN_ON(IS_ERR(cf_sk->debugfs_socket_dir));
Fabian Frederickfb0d1642014-06-27 23:07:43 +0200918 debugfs_remove_recursive(cf_sk->debugfs_socket_dir);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000919
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000920 lock_sock(&(cf_sk->sk));
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000921 sk->sk_state = CAIF_DISCONNECTED;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000922 sk->sk_shutdown = SHUTDOWN_MASK;
923
sjur.brandeland@stericsson.com54e90fb2011-05-22 11:18:51 +0000924 caif_disconnect_client(sock_net(sk), &cf_sk->layer);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000925 cf_sk->sk.sk_socket->state = SS_DISCONNECTING;
926 wake_up_interruptible_poll(sk_sleep(sk), POLLERR|POLLHUP);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000927
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000928 sock_orphan(sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000929 sk_stream_kill_queues(&cf_sk->sk);
930 release_sock(sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000931 sock_put(sk);
sjur.brandeland@stericsson.com54e90fb2011-05-22 11:18:51 +0000932 return 0;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000933}
934
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000935/* Copied from af_unix.c:unix_poll(), added CAIF tx_flow handling */
936static unsigned int caif_poll(struct file *file,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000937 struct socket *sock, poll_table *wait)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000938{
939 struct sock *sk = sock->sk;
940 unsigned int mask;
941 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
942
943 sock_poll_wait(file, sk_sleep(sk), wait);
944 mask = 0;
945
946 /* exceptional events? */
947 if (sk->sk_err)
948 mask |= POLLERR;
949 if (sk->sk_shutdown == SHUTDOWN_MASK)
950 mask |= POLLHUP;
951 if (sk->sk_shutdown & RCV_SHUTDOWN)
952 mask |= POLLRDHUP;
953
954 /* readable? */
955 if (!skb_queue_empty(&sk->sk_receive_queue) ||
956 (sk->sk_shutdown & RCV_SHUTDOWN))
957 mask |= POLLIN | POLLRDNORM;
958
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000959 /*
960 * we set writable also when the other side has shut down the
961 * connection. This prevents stuck sockets.
962 */
963 if (sock_writeable(sk) && tx_flow_is_on(cf_sk))
964 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
965
966 return mask;
967}
968
969static const struct proto_ops caif_seqpacket_ops = {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000970 .family = PF_CAIF,
971 .owner = THIS_MODULE,
972 .release = caif_release,
973 .bind = sock_no_bind,
974 .connect = caif_connect,
975 .socketpair = sock_no_socketpair,
976 .accept = sock_no_accept,
977 .getname = sock_no_getname,
978 .poll = caif_poll,
979 .ioctl = sock_no_ioctl,
980 .listen = sock_no_listen,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000981 .shutdown = sock_no_shutdown,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000982 .setsockopt = setsockopt,
983 .getsockopt = sock_no_getsockopt,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000984 .sendmsg = caif_seqpkt_sendmsg,
985 .recvmsg = caif_seqpkt_recvmsg,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000986 .mmap = sock_no_mmap,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000987 .sendpage = sock_no_sendpage,
988};
989
990static const struct proto_ops caif_stream_ops = {
991 .family = PF_CAIF,
992 .owner = THIS_MODULE,
993 .release = caif_release,
994 .bind = sock_no_bind,
995 .connect = caif_connect,
996 .socketpair = sock_no_socketpair,
997 .accept = sock_no_accept,
998 .getname = sock_no_getname,
999 .poll = caif_poll,
1000 .ioctl = sock_no_ioctl,
1001 .listen = sock_no_listen,
1002 .shutdown = sock_no_shutdown,
1003 .setsockopt = setsockopt,
1004 .getsockopt = sock_no_getsockopt,
1005 .sendmsg = caif_stream_sendmsg,
1006 .recvmsg = caif_stream_recvmsg,
1007 .mmap = sock_no_mmap,
1008 .sendpage = sock_no_sendpage,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001009};
1010
1011/* This function is called when a socket is finally destroyed. */
1012static void caif_sock_destructor(struct sock *sk)
1013{
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001014 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001015 caif_assert(!atomic_read(&sk->sk_wmem_alloc));
1016 caif_assert(sk_unhashed(sk));
1017 caif_assert(!sk->sk_socket);
1018 if (!sock_flag(sk, SOCK_DEAD)) {
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +00001019 pr_debug("Attempt to release alive CAIF socket: %p\n", sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001020 return;
1021 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001022 sk_stream_kill_queues(&cf_sk->sk);
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +00001023 caif_free_client(&cf_sk->layer);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001024}
1025
1026static int caif_create(struct net *net, struct socket *sock, int protocol,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +00001027 int kern)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001028{
1029 struct sock *sk = NULL;
1030 struct caifsock *cf_sk = NULL;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001031 static struct proto prot = {.name = "PF_CAIF",
1032 .owner = THIS_MODULE,
1033 .obj_size = sizeof(struct caifsock),
1034 };
1035
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001036 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_NET_ADMIN))
1037 return -EPERM;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001038 /*
1039 * The sock->type specifies the socket type to use.
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001040 * The CAIF socket is a packet stream in the sense
1041 * that it is packet based. CAIF trusts the reliability
1042 * of the link, no resending is implemented.
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001043 */
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001044 if (sock->type == SOCK_SEQPACKET)
1045 sock->ops = &caif_seqpacket_ops;
1046 else if (sock->type == SOCK_STREAM)
1047 sock->ops = &caif_stream_ops;
1048 else
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001049 return -ESOCKTNOSUPPORT;
1050
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001051 if (protocol < 0 || protocol >= CAIFPROTO_MAX)
1052 return -EPROTONOSUPPORT;
1053 /*
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001054 * Set the socket state to unconnected. The socket state
1055 * is really not used at all in the net/core or socket.c but the
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001056 * initialization makes sure that sock->state is not uninitialized.
1057 */
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05001058 sk = sk_alloc(net, PF_CAIF, GFP_KERNEL, &prot, kern);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001059 if (!sk)
1060 return -ENOMEM;
1061
1062 cf_sk = container_of(sk, struct caifsock, sk);
1063
1064 /* Store the protocol */
1065 sk->sk_protocol = (unsigned char) protocol;
1066
Dmitry Tarnyagin44764812012-04-12 08:27:24 +00001067 /* Initialize default priority for well-known cases */
1068 switch (protocol) {
1069 case CAIFPROTO_AT:
1070 sk->sk_priority = TC_PRIO_CONTROL;
1071 break;
1072 case CAIFPROTO_RFM:
1073 sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
1074 break;
1075 default:
1076 sk->sk_priority = TC_PRIO_BESTEFFORT;
1077 }
1078
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001079 /*
1080 * Lock in order to try to stop someone from opening the socket
1081 * too early.
1082 */
1083 lock_sock(&(cf_sk->sk));
1084
1085 /* Initialize the nozero default sock structure data. */
1086 sock_init_data(sock, sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001087 sk->sk_destruct = caif_sock_destructor;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001088
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001089 mutex_init(&cf_sk->readlock); /* single task reading lock */
1090 cf_sk->layer.ctrlcmd = caif_ctrl_cb;
1091 cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
1092 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001093
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001094 set_tx_flow_off(cf_sk);
1095 set_rx_flow_on(cf_sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001096
1097 /* Set default options on configuration */
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001098 cf_sk->conn_req.link_selector = CAIF_LINK_LOW_LATENCY;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001099 cf_sk->conn_req.protocol = protocol;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001100 release_sock(&cf_sk->sk);
1101 return 0;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001102}
1103
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001104
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001105static struct net_proto_family caif_family_ops = {
1106 .family = PF_CAIF,
1107 .create = caif_create,
1108 .owner = THIS_MODULE,
1109};
1110
sjur.brandeland@stericsson.com4a695822012-02-03 04:36:19 +00001111static int __init caif_sktinit_module(void)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001112{
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001113 int err = sock_register(&caif_family_ops);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001114 if (!err)
1115 return err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001116 return 0;
1117}
1118
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001119static void __exit caif_sktexit_module(void)
1120{
1121 sock_unregister(PF_CAIF);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001122}
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001123module_init(caif_sktinit_module);
1124module_exit(caif_sktexit_module);