blob: 21f18ea2fce440c1a0c8dcddc335e327dbf9bced [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>
Ingo Molnar3f07c012017-02-08 18:51:30 +010012#include <linux/sched/signal.h>
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000013#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 */
Eric Dumazetb8a23e82015-07-17 10:19:23 +0200124static void 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);
Eric Dumazetb8a23e82015-07-17 10:19:23 +0200130 bool queued = false;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000131
132 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +0000133 (unsigned int)sk->sk_rcvbuf && rx_flow_is_on(cf_sk)) {
Joe Perchese87cc472012-05-13 21:56:26 +0000134 net_dbg_ratelimited("sending flow OFF (queue len = %d %d)\n",
135 atomic_read(&cf_sk->sk.sk_rmem_alloc),
136 sk_rcvbuf_lowwater(cf_sk));
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000137 set_rx_flow_off(cf_sk);
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000138 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_OFF_REQ);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000139 }
140
141 err = sk_filter(sk, skb);
142 if (err)
Eric Dumazetb8a23e82015-07-17 10:19:23 +0200143 goto out;
144
Mel Gormanc76562b2012-07-31 16:44:41 -0700145 if (!sk_rmem_schedule(sk, skb, skb->truesize) && rx_flow_is_on(cf_sk)) {
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000146 set_rx_flow_off(cf_sk);
Joe Perchese87cc472012-05-13 21:56:26 +0000147 net_dbg_ratelimited("sending flow OFF due to rmem_schedule\n");
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000148 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_OFF_REQ);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000149 }
150 skb->dev = NULL;
151 skb_set_owner_r(skb, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000152 spin_lock_irqsave(&list->lock, flags);
Eric Dumazetb8a23e82015-07-17 10:19:23 +0200153 queued = !sock_flag(sk, SOCK_DEAD);
154 if (queued)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000155 __skb_queue_tail(list, skb);
156 spin_unlock_irqrestore(&list->lock, flags);
Eric Dumazetb8a23e82015-07-17 10:19:23 +0200157out:
158 if (queued)
David S. Miller676d2362014-04-11 16:15:36 -0400159 sk->sk_data_ready(sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000160 else
161 kfree_skb(skb);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000162}
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000163
164/* Packet Receive Callback function called from CAIF Stack */
165static int caif_sktrecv_cb(struct cflayer *layr, struct cfpkt *pkt)
166{
167 struct caifsock *cf_sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000168 struct sk_buff *skb;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000169
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000170 cf_sk = container_of(layr, struct caifsock, layer);
171 skb = cfpkt_tonative(pkt);
172
173 if (unlikely(cf_sk->sk.sk_state != CAIF_CONNECTED)) {
sjur.brandeland@stericsson.com3f874ad2011-05-13 02:44:08 +0000174 kfree_skb(skb);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000175 return 0;
176 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000177 caif_queue_rcv_skb(&cf_sk->sk, skb);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000178 return 0;
179}
180
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000181static void cfsk_hold(struct cflayer *layr)
182{
183 struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
184 sock_hold(&cf_sk->sk);
185}
186
187static void cfsk_put(struct cflayer *layr)
188{
189 struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
190 sock_put(&cf_sk->sk);
191}
192
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000193/* Packet Control Callback function called from CAIF */
194static void caif_ctrl_cb(struct cflayer *layr,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000195 enum caif_ctrlcmd flow,
196 int phyid)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000197{
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000198 struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000199 switch (flow) {
200 case CAIF_CTRLCMD_FLOW_ON_IND:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000201 /* OK from modem to start sending again */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000202 set_tx_flow_on(cf_sk);
203 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000204 break;
205
206 case CAIF_CTRLCMD_FLOW_OFF_IND:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000207 /* Modem asks us to shut up */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000208 set_tx_flow_off(cf_sk);
209 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000210 break;
211
212 case CAIF_CTRLCMD_INIT_RSP:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000213 /* We're now connected */
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000214 caif_client_register_refcnt(&cf_sk->layer,
215 cfsk_hold, cfsk_put);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000216 cf_sk->sk.sk_state = CAIF_CONNECTED;
217 set_tx_flow_on(cf_sk);
Sjur Brændelandeaa8c5f2012-06-24 11:01:36 +0000218 cf_sk->sk.sk_shutdown = 0;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000219 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000220 break;
221
222 case CAIF_CTRLCMD_DEINIT_RSP:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000223 /* We're now disconnected */
224 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
225 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000226 break;
227
228 case CAIF_CTRLCMD_INIT_FAIL_RSP:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000229 /* Connect request failed */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000230 cf_sk->sk.sk_err = ECONNREFUSED;
231 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
232 cf_sk->sk.sk_shutdown = SHUTDOWN_MASK;
233 /*
234 * Socket "standards" seems to require POLLOUT to
235 * be set at connect failure.
236 */
237 set_tx_flow_on(cf_sk);
238 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000239 break;
240
241 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000242 /* Modem has closed this connection, or device is down. */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000243 cf_sk->sk.sk_shutdown = SHUTDOWN_MASK;
244 cf_sk->sk.sk_err = ECONNRESET;
245 set_rx_flow_on(cf_sk);
246 cf_sk->sk.sk_error_report(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000247 break;
248
249 default:
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000250 pr_debug("Unexpected flow command %d\n", flow);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000251 }
252}
253
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000254static void caif_check_flow_release(struct sock *sk)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000255{
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000256 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
257
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000258 if (rx_flow_is_on(cf_sk))
259 return;
260
261 if (atomic_read(&sk->sk_rmem_alloc) <= sk_rcvbuf_lowwater(cf_sk)) {
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000262 set_rx_flow_on(cf_sk);
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000263 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_ON_REQ);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000264 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000265}
266
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000267/*
268 * Copied from unix_dgram_recvmsg, but removed credit checks,
269 * changed locking, address handling and added MSG_TRUNC.
270 */
Ying Xue1b784142015-03-02 15:37:48 +0800271static int caif_seqpkt_recvmsg(struct socket *sock, struct msghdr *m,
272 size_t len, int flags)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000273
274{
275 struct sock *sk = sock->sk;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000276 struct sk_buff *skb;
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000277 int ret;
278 int copylen;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000279
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000280 ret = -EOPNOTSUPP;
Al Viro3eeff772015-03-14 05:22:21 +0000281 if (flags & MSG_OOB)
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000282 goto read_error;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000283
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000284 skb = skb_recv_datagram(sk, flags, 0 , &ret);
285 if (!skb)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000286 goto read_error;
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000287 copylen = skb->len;
288 if (len < copylen) {
289 m->msg_flags |= MSG_TRUNC;
290 copylen = len;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000291 }
292
David S. Miller51f3d022014-11-05 16:46:40 -0500293 ret = skb_copy_datagram_msg(skb, 0, m, copylen);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000294 if (ret)
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000295 goto out_free;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000296
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000297 ret = (flags & MSG_TRUNC) ? skb->len : copylen;
298out_free:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000299 skb_free_datagram(sk, skb);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000300 caif_check_flow_release(sk);
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000301 return ret;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000302
303read_error:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000304 return ret;
305}
306
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000307
308/* Copied from unix_stream_wait_data, identical except for lock call. */
309static long caif_stream_data_wait(struct sock *sk, long timeo)
310{
311 DEFINE_WAIT(wait);
312 lock_sock(sk);
313
314 for (;;) {
315 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
316
317 if (!skb_queue_empty(&sk->sk_receive_queue) ||
318 sk->sk_err ||
319 sk->sk_state != CAIF_CONNECTED ||
320 sock_flag(sk, SOCK_DEAD) ||
321 (sk->sk_shutdown & RCV_SHUTDOWN) ||
322 signal_pending(current) ||
323 !timeo)
324 break;
325
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800326 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000327 release_sock(sk);
328 timeo = schedule_timeout(timeo);
329 lock_sock(sk);
Mark Salyzynb48732e2015-05-26 08:22:19 -0700330
331 if (sock_flag(sk, SOCK_DEAD))
332 break;
333
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800334 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000335 }
336
337 finish_wait(sk_sleep(sk), &wait);
338 release_sock(sk);
339 return timeo;
340}
341
342
343/*
344 * Copied from unix_stream_recvmsg, but removed credit checks,
345 * changed locking calls, changed address handling.
346 */
Ying Xue1b784142015-03-02 15:37:48 +0800347static int caif_stream_recvmsg(struct socket *sock, struct msghdr *msg,
348 size_t size, 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);
Mark Salyzynb48732e2015-05-26 08:22:19 -0700377 if (sock_flag(sk, SOCK_DEAD)) {
378 err = -ECONNRESET;
379 goto unlock;
380 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000381 skb = skb_dequeue(&sk->sk_receive_queue);
382 caif_check_flow_release(sk);
383
384 if (skb == NULL) {
385 if (copied >= target)
386 goto unlock;
387 /*
388 * POSIX 1003.1g mandates this order.
389 */
390 err = sock_error(sk);
391 if (err)
392 goto unlock;
393 err = -ECONNRESET;
394 if (sk->sk_shutdown & RCV_SHUTDOWN)
395 goto unlock;
396
397 err = -EPIPE;
398 if (sk->sk_state != CAIF_CONNECTED)
399 goto unlock;
400 if (sock_flag(sk, SOCK_DEAD))
401 goto unlock;
402
403 release_sock(sk);
404
405 err = -EAGAIN;
406 if (!timeo)
407 break;
408
409 caif_read_unlock(sk);
410
411 timeo = caif_stream_data_wait(sk, timeo);
412
413 if (signal_pending(current)) {
414 err = sock_intr_errno(timeo);
415 goto out;
416 }
417 caif_read_lock(sk);
418 continue;
419unlock:
420 release_sock(sk);
421 break;
422 }
423 release_sock(sk);
424 chunk = min_t(unsigned int, skb->len, size);
Al Viro7eab8d92014-04-06 21:51:23 -0400425 if (memcpy_to_msg(msg, skb->data, chunk)) {
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000426 skb_queue_head(&sk->sk_receive_queue, skb);
427 if (copied == 0)
428 copied = -EFAULT;
429 break;
430 }
431 copied += chunk;
432 size -= chunk;
433
434 /* Mark read part of skb as used */
435 if (!(flags & MSG_PEEK)) {
436 skb_pull(skb, chunk);
437
438 /* put the skb back if we didn't use it up. */
439 if (skb->len) {
440 skb_queue_head(&sk->sk_receive_queue, skb);
441 break;
442 }
443 kfree_skb(skb);
444
445 } else {
446 /*
447 * It is questionable, see note in unix_dgram_recvmsg.
448 */
449 /* put message back and return */
450 skb_queue_head(&sk->sk_receive_queue, skb);
451 break;
452 }
453 } while (size);
454 caif_read_unlock(sk);
455
456out:
457 return copied ? : err;
458}
459
460/*
461 * Copied from sock.c:sock_wait_for_wmem, but change to wait for
462 * CAIF flow-on and sock_writable.
463 */
464static long caif_wait_for_flow_on(struct caifsock *cf_sk,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000465 int wait_writeable, long timeo, int *err)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000466{
467 struct sock *sk = &cf_sk->sk;
468 DEFINE_WAIT(wait);
469 for (;;) {
470 *err = 0;
471 if (tx_flow_is_on(cf_sk) &&
472 (!wait_writeable || sock_writeable(&cf_sk->sk)))
473 break;
474 *err = -ETIMEDOUT;
475 if (!timeo)
476 break;
477 *err = -ERESTARTSYS;
478 if (signal_pending(current))
479 break;
480 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
481 *err = -ECONNRESET;
482 if (sk->sk_shutdown & SHUTDOWN_MASK)
483 break;
484 *err = -sk->sk_err;
485 if (sk->sk_err)
486 break;
487 *err = -EPIPE;
488 if (cf_sk->sk.sk_state != CAIF_CONNECTED)
489 break;
490 timeo = schedule_timeout(timeo);
491 }
492 finish_wait(sk_sleep(sk), &wait);
493 return timeo;
494}
495
496/*
497 * Transmit a SKB. The device may temporarily request re-transmission
498 * by returning EAGAIN.
499 */
500static int transmit_skb(struct sk_buff *skb, struct caifsock *cf_sk,
501 int noblock, long timeo)
502{
503 struct cfpkt *pkt;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000504
505 pkt = cfpkt_fromnative(CAIF_DIR_OUT, skb);
sjur.brandeland@stericsson.com3f874ad2011-05-13 02:44:08 +0000506 memset(skb->cb, 0, sizeof(struct caif_payload_info));
Dmitry Tarnyagin44764812012-04-12 08:27:24 +0000507 cfpkt_set_prio(pkt, cf_sk->sk.sk_priority);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000508
Dmitry Tarnyaginba760572012-02-02 01:21:03 +0000509 if (cf_sk->layer.dn == NULL) {
510 kfree_skb(skb);
Sjur Brændeland4dd820c2011-04-11 10:43:51 +0000511 return -EINVAL;
Dmitry Tarnyaginba760572012-02-02 01:21:03 +0000512 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000513
Sjur Brændeland4dd820c2011-04-11 10:43:51 +0000514 return cf_sk->layer.dn->transmit(cf_sk->layer.dn, pkt);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000515}
516
517/* Copied from af_unix:unix_dgram_sendmsg, and adapted to CAIF */
Ying Xue1b784142015-03-02 15:37:48 +0800518static int caif_seqpkt_sendmsg(struct socket *sock, struct msghdr *msg,
519 size_t len)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000520{
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000521 struct sock *sk = sock->sk;
522 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000523 int buffer_size;
524 int ret = 0;
525 struct sk_buff *skb = NULL;
526 int noblock;
527 long timeo;
528 caif_assert(cf_sk);
529 ret = sock_error(sk);
530 if (ret)
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_flags&MSG_OOB)
535 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000536
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000537 ret = -EOPNOTSUPP;
538 if (msg->msg_namelen)
539 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000540
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000541 ret = -EINVAL;
Al Viroc0371da2014-11-24 10:42:55 -0500542 if (unlikely(msg->msg_iter.iov->iov_base == NULL))
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000543 goto err;
544 noblock = msg->msg_flags & MSG_DONTWAIT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000545
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000546 timeo = sock_sndtimeo(sk, noblock);
547 timeo = caif_wait_for_flow_on(container_of(sk, struct caifsock, sk),
548 1, timeo, &ret);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000549
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000550 if (ret)
551 goto err;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000552 ret = -EPIPE;
553 if (cf_sk->sk.sk_state != CAIF_CONNECTED ||
554 sock_flag(sk, SOCK_DEAD) ||
555 (sk->sk_shutdown & RCV_SHUTDOWN))
556 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000557
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000558 /* Error if trying to write more than maximum frame size. */
559 ret = -EMSGSIZE;
560 if (len > cf_sk->maxframe && cf_sk->sk.sk_protocol != CAIFPROTO_RFM)
561 goto err;
562
563 buffer_size = len + cf_sk->headroom + cf_sk->tailroom;
564
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000565 ret = -ENOMEM;
566 skb = sock_alloc_send_skb(sk, buffer_size, noblock, &ret);
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000567
568 if (!skb || skb_tailroom(skb) < buffer_size)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000569 goto err;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000570
571 skb_reserve(skb, cf_sk->headroom);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000572
Al Viro6ce8e9c2014-04-06 21:25:44 -0400573 ret = memcpy_from_msg(skb_put(skb, len), msg, len);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000574
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000575 if (ret)
576 goto err;
577 ret = transmit_skb(skb, cf_sk, noblock, timeo);
578 if (ret < 0)
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000579 /* skb is already freed */
580 return ret;
581
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000582 return len;
583err:
584 kfree_skb(skb);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000585 return ret;
586}
587
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000588/*
589 * Copied from unix_stream_sendmsg and adapted to CAIF:
590 * Changed removed permission handling and added waiting for flow on
591 * and other minor adaptations.
592 */
Ying Xue1b784142015-03-02 15:37:48 +0800593static int caif_stream_sendmsg(struct socket *sock, struct msghdr *msg,
594 size_t len)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000595{
596 struct sock *sk = sock->sk;
597 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000598 int err, size;
599 struct sk_buff *skb;
600 int sent = 0;
601 long timeo;
602
603 err = -EOPNOTSUPP;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000604 if (unlikely(msg->msg_flags&MSG_OOB))
605 goto out_err;
606
607 if (unlikely(msg->msg_namelen))
608 goto out_err;
609
610 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
611 timeo = caif_wait_for_flow_on(cf_sk, 1, timeo, &err);
612
613 if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN))
614 goto pipe_err;
615
616 while (sent < len) {
617
618 size = len-sent;
619
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000620 if (size > cf_sk->maxframe)
621 size = cf_sk->maxframe;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000622
623 /* If size is more than half of sndbuf, chop up message */
624 if (size > ((sk->sk_sndbuf >> 1) - 64))
625 size = (sk->sk_sndbuf >> 1) - 64;
626
627 if (size > SKB_MAX_ALLOC)
628 size = SKB_MAX_ALLOC;
629
630 skb = sock_alloc_send_skb(sk,
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000631 size + cf_sk->headroom +
632 cf_sk->tailroom,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000633 msg->msg_flags&MSG_DONTWAIT,
634 &err);
635 if (skb == NULL)
636 goto out_err;
637
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000638 skb_reserve(skb, cf_sk->headroom);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000639 /*
640 * If you pass two values to the sock_alloc_send_skb
641 * it tries to grab the large buffer with GFP_NOFS
642 * (which can fail easily), and if it fails grab the
643 * fallback size buffer which is under a page and will
644 * succeed. [Alan]
645 */
646 size = min_t(int, size, skb_tailroom(skb));
647
Al Viro6ce8e9c2014-04-06 21:25:44 -0400648 err = memcpy_from_msg(skb_put(skb, size), msg, size);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000649 if (err) {
650 kfree_skb(skb);
651 goto out_err;
652 }
653 err = transmit_skb(skb, cf_sk,
654 msg->msg_flags&MSG_DONTWAIT, timeo);
Dmitry Tarnyaginba760572012-02-02 01:21:03 +0000655 if (err < 0)
656 /* skb is already freed */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000657 goto pipe_err;
Dmitry Tarnyaginba760572012-02-02 01:21:03 +0000658
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000659 sent += size;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000660 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000661
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000662 return sent;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000663
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000664pipe_err:
665 if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL))
666 send_sig(SIGPIPE, current, 0);
667 err = -EPIPE;
668out_err:
669 return sent ? : err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000670}
671
672static int setsockopt(struct socket *sock,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000673 int lvl, int opt, char __user *ov, unsigned int ol)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000674{
675 struct sock *sk = sock->sk;
676 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +0000677 int linksel;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000678
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000679 if (cf_sk->sk.sk_socket->state != SS_UNCONNECTED)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000680 return -ENOPROTOOPT;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000681
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000682 switch (opt) {
683 case CAIFSO_LINK_SELECT:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000684 if (ol < sizeof(int))
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000685 return -EINVAL;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000686 if (lvl != SOL_CAIF)
687 goto bad_sol;
688 if (copy_from_user(&linksel, ov, sizeof(int)))
689 return -EINVAL;
690 lock_sock(&(cf_sk->sk));
691 cf_sk->conn_req.link_selector = linksel;
692 release_sock(&cf_sk->sk);
693 return 0;
694
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000695 case CAIFSO_REQ_PARAM:
696 if (lvl != SOL_CAIF)
697 goto bad_sol;
698 if (cf_sk->sk.sk_protocol != CAIFPROTO_UTIL)
699 return -ENOPROTOOPT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000700 lock_sock(&(cf_sk->sk));
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000701 if (ol > sizeof(cf_sk->conn_req.param.data) ||
702 copy_from_user(&cf_sk->conn_req.param.data, ov, ol)) {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000703 release_sock(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000704 return -EINVAL;
705 }
Dan Rosenberg91b5c982011-01-10 16:00:54 -0800706 cf_sk->conn_req.param.size = ol;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000707 release_sock(&cf_sk->sk);
708 return 0;
709
710 default:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000711 return -ENOPROTOOPT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000712 }
713
714 return 0;
715bad_sol:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000716 return -ENOPROTOOPT;
717
718}
719
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000720/*
721 * caif_connect() - Connect a CAIF Socket
722 * Copied and modified af_irda.c:irda_connect().
723 *
724 * Note : by consulting "errno", the user space caller may learn the cause
725 * of the failure. Most of them are visible in the function, others may come
726 * from subroutines called and are listed here :
727 * o -EAFNOSUPPORT: bad socket family or type.
728 * o -ESOCKTNOSUPPORT: bad socket type or protocol
729 * o -EINVAL: bad socket address, or CAIF link type
730 * o -ECONNREFUSED: remote end refused the connection.
731 * o -EINPROGRESS: connect request sent but timed out (or non-blocking)
732 * o -EISCONN: already connected.
733 * o -ETIMEDOUT: Connection timed out (send timeout)
734 * o -ENODEV: No link layer to send request
735 * o -ECONNRESET: Received Shutdown indication or lost link layer
736 * o -ENOMEM: Out of memory
737 *
738 * State Strategy:
739 * o sk_state: holds the CAIF_* protocol state, it's updated by
740 * caif_ctrl_cb.
741 * o sock->state: holds the SS_* socket state and is updated by connect and
742 * disconnect.
743 */
744static int caif_connect(struct socket *sock, struct sockaddr *uaddr,
745 int addr_len, int flags)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000746{
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000747 struct sock *sk = sock->sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000748 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
749 long timeo;
750 int err;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000751 int ifindex, headroom, tailroom;
Eric Dumazet79315062010-10-04 22:42:08 +0000752 unsigned int mtu;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000753 struct net_device *dev;
754
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000755 lock_sock(sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000756
Mateusz Jurczyk20a3d5b2017-06-13 20:06:12 +0200757 err = -EINVAL;
758 if (addr_len < offsetofend(struct sockaddr, sa_family))
759 goto out;
760
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000761 err = -EAFNOSUPPORT;
762 if (uaddr->sa_family != AF_CAIF)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000763 goto out;
764
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000765 switch (sock->state) {
766 case SS_UNCONNECTED:
767 /* Normal case, a fresh connect */
768 caif_assert(sk->sk_state == CAIF_DISCONNECTED);
769 break;
770 case SS_CONNECTING:
771 switch (sk->sk_state) {
772 case CAIF_CONNECTED:
773 sock->state = SS_CONNECTED;
774 err = -EISCONN;
775 goto out;
776 case CAIF_DISCONNECTED:
777 /* Reconnect allowed */
778 break;
779 case CAIF_CONNECTING:
780 err = -EALREADY;
781 if (flags & O_NONBLOCK)
782 goto out;
783 goto wait_connect;
784 }
785 break;
786 case SS_CONNECTED:
787 caif_assert(sk->sk_state == CAIF_CONNECTED ||
788 sk->sk_state == CAIF_DISCONNECTED);
789 if (sk->sk_shutdown & SHUTDOWN_MASK) {
790 /* Allow re-connect after SHUTDOWN_IND */
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000791 caif_disconnect_client(sock_net(sk), &cf_sk->layer);
sjur.brandeland@stericsson.com54e90fb2011-05-22 11:18:51 +0000792 caif_free_client(&cf_sk->layer);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000793 break;
794 }
795 /* No reconnect on a seqpacket socket */
796 err = -EISCONN;
797 goto out;
798 case SS_DISCONNECTING:
799 case SS_FREE:
800 caif_assert(1); /*Should never happen */
801 break;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000802 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000803 sk->sk_state = CAIF_DISCONNECTED;
804 sock->state = SS_UNCONNECTED;
805 sk_stream_kill_queues(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000806
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000807 err = -EINVAL;
Dan Carpenterf5d72af2010-05-22 10:43:42 +0000808 if (addr_len != sizeof(struct sockaddr_caif))
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000809 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000810
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000811 memcpy(&cf_sk->conn_req.sockaddr, uaddr,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000812 sizeof(struct sockaddr_caif));
813
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000814 /* Move to connecting socket, start sending Connect Requests */
815 sock->state = SS_CONNECTING;
816 sk->sk_state = CAIF_CONNECTING;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000817
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +0000818 /* Check priority value comming from socket */
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +0000819 /* if priority value is out of range it will be ajusted */
820 if (cf_sk->sk.sk_priority > CAIF_PRIO_MAX)
821 cf_sk->conn_req.priority = CAIF_PRIO_MAX;
822 else if (cf_sk->sk.sk_priority < CAIF_PRIO_MIN)
823 cf_sk->conn_req.priority = CAIF_PRIO_MIN;
824 else
825 cf_sk->conn_req.priority = cf_sk->sk.sk_priority;
826
827 /*ifindex = id of the interface.*/
828 cf_sk->conn_req.ifindex = cf_sk->sk.sk_bound_dev_if;
829
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000830 cf_sk->layer.receive = caif_sktrecv_cb;
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000831
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000832 err = caif_connect_client(sock_net(sk), &cf_sk->conn_req,
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000833 &cf_sk->layer, &ifindex, &headroom, &tailroom);
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000834
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000835 if (err < 0) {
836 cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
837 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
838 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000839 }
Eric Dumazet79315062010-10-04 22:42:08 +0000840
841 err = -ENODEV;
842 rcu_read_lock();
843 dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
844 if (!dev) {
845 rcu_read_unlock();
846 goto out;
847 }
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000848 cf_sk->headroom = LL_RESERVED_SPACE_EXTRA(dev, headroom);
Eric Dumazet79315062010-10-04 22:42:08 +0000849 mtu = dev->mtu;
850 rcu_read_unlock();
851
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000852 cf_sk->tailroom = tailroom;
Eric Dumazet79315062010-10-04 22:42:08 +0000853 cf_sk->maxframe = mtu - (headroom + tailroom);
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000854 if (cf_sk->maxframe < 1) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000855 pr_warn("CAIF Interface MTU too small (%d)\n", dev->mtu);
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +0000856 err = -ENODEV;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000857 goto out;
858 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000859
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000860 err = -EINPROGRESS;
861wait_connect:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000862
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000863 if (sk->sk_state != CAIF_CONNECTED && (flags & O_NONBLOCK))
864 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000865
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000866 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000867
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000868 release_sock(sk);
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000869 err = -ERESTARTSYS;
870 timeo = wait_event_interruptible_timeout(*sk_sleep(sk),
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000871 sk->sk_state != CAIF_CONNECTING,
872 timeo);
873 lock_sock(sk);
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000874 if (timeo < 0)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000875 goto out; /* -ERESTARTSYS */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000876
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000877 err = -ETIMEDOUT;
878 if (timeo == 0 && sk->sk_state != CAIF_CONNECTED)
879 goto out;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000880 if (sk->sk_state != CAIF_CONNECTED) {
881 sock->state = SS_UNCONNECTED;
882 err = sock_error(sk);
883 if (!err)
884 err = -ECONNREFUSED;
885 goto out;
886 }
887 sock->state = SS_CONNECTED;
888 err = 0;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000889out:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000890 release_sock(sk);
891 return err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000892}
893
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000894/*
895 * caif_release() - Disconnect a CAIF Socket
896 * Copied and modified af_irda.c:irda_release().
897 */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000898static int caif_release(struct socket *sock)
899{
900 struct sock *sk = sock->sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000901 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000902
903 if (!sk)
904 return 0;
905
906 set_tx_flow_off(cf_sk);
907
908 /*
909 * Ensure that packets are not queued after this point in time.
910 * caif_queue_rcv_skb checks SOCK_DEAD holding the queue lock,
911 * this ensures no packets when sock is dead.
912 */
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000913 spin_lock_bh(&sk->sk_receive_queue.lock);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000914 sock_set_flag(sk, SOCK_DEAD);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000915 spin_unlock_bh(&sk->sk_receive_queue.lock);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000916 sock->sk = NULL;
917
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +0000918 WARN_ON(IS_ERR(cf_sk->debugfs_socket_dir));
Fabian Frederickfb0d1642014-06-27 23:07:43 +0200919 debugfs_remove_recursive(cf_sk->debugfs_socket_dir);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000920
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000921 lock_sock(&(cf_sk->sk));
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000922 sk->sk_state = CAIF_DISCONNECTED;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000923 sk->sk_shutdown = SHUTDOWN_MASK;
924
sjur.brandeland@stericsson.com54e90fb2011-05-22 11:18:51 +0000925 caif_disconnect_client(sock_net(sk), &cf_sk->layer);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000926 cf_sk->sk.sk_socket->state = SS_DISCONNECTING;
927 wake_up_interruptible_poll(sk_sleep(sk), POLLERR|POLLHUP);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000928
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000929 sock_orphan(sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000930 sk_stream_kill_queues(&cf_sk->sk);
931 release_sock(sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000932 sock_put(sk);
sjur.brandeland@stericsson.com54e90fb2011-05-22 11:18:51 +0000933 return 0;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000934}
935
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000936/* Copied from af_unix.c:unix_poll(), added CAIF tx_flow handling */
937static unsigned int caif_poll(struct file *file,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000938 struct socket *sock, poll_table *wait)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000939{
940 struct sock *sk = sock->sk;
941 unsigned int mask;
942 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
943
944 sock_poll_wait(file, sk_sleep(sk), wait);
945 mask = 0;
946
947 /* exceptional events? */
948 if (sk->sk_err)
949 mask |= POLLERR;
950 if (sk->sk_shutdown == SHUTDOWN_MASK)
951 mask |= POLLHUP;
952 if (sk->sk_shutdown & RCV_SHUTDOWN)
953 mask |= POLLRDHUP;
954
955 /* readable? */
956 if (!skb_queue_empty(&sk->sk_receive_queue) ||
957 (sk->sk_shutdown & RCV_SHUTDOWN))
958 mask |= POLLIN | POLLRDNORM;
959
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000960 /*
961 * we set writable also when the other side has shut down the
962 * connection. This prevents stuck sockets.
963 */
964 if (sock_writeable(sk) && tx_flow_is_on(cf_sk))
965 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
966
967 return mask;
968}
969
970static const struct proto_ops caif_seqpacket_ops = {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000971 .family = PF_CAIF,
972 .owner = THIS_MODULE,
973 .release = caif_release,
974 .bind = sock_no_bind,
975 .connect = caif_connect,
976 .socketpair = sock_no_socketpair,
977 .accept = sock_no_accept,
978 .getname = sock_no_getname,
979 .poll = caif_poll,
980 .ioctl = sock_no_ioctl,
981 .listen = sock_no_listen,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000982 .shutdown = sock_no_shutdown,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000983 .setsockopt = setsockopt,
984 .getsockopt = sock_no_getsockopt,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000985 .sendmsg = caif_seqpkt_sendmsg,
986 .recvmsg = caif_seqpkt_recvmsg,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000987 .mmap = sock_no_mmap,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000988 .sendpage = sock_no_sendpage,
989};
990
991static const struct proto_ops caif_stream_ops = {
992 .family = PF_CAIF,
993 .owner = THIS_MODULE,
994 .release = caif_release,
995 .bind = sock_no_bind,
996 .connect = caif_connect,
997 .socketpair = sock_no_socketpair,
998 .accept = sock_no_accept,
999 .getname = sock_no_getname,
1000 .poll = caif_poll,
1001 .ioctl = sock_no_ioctl,
1002 .listen = sock_no_listen,
1003 .shutdown = sock_no_shutdown,
1004 .setsockopt = setsockopt,
1005 .getsockopt = sock_no_getsockopt,
1006 .sendmsg = caif_stream_sendmsg,
1007 .recvmsg = caif_stream_recvmsg,
1008 .mmap = sock_no_mmap,
1009 .sendpage = sock_no_sendpage,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001010};
1011
1012/* This function is called when a socket is finally destroyed. */
1013static void caif_sock_destructor(struct sock *sk)
1014{
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001015 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001016 caif_assert(!atomic_read(&sk->sk_wmem_alloc));
1017 caif_assert(sk_unhashed(sk));
1018 caif_assert(!sk->sk_socket);
1019 if (!sock_flag(sk, SOCK_DEAD)) {
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +00001020 pr_debug("Attempt to release alive CAIF socket: %p\n", sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001021 return;
1022 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001023 sk_stream_kill_queues(&cf_sk->sk);
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +00001024 caif_free_client(&cf_sk->layer);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001025}
1026
1027static int caif_create(struct net *net, struct socket *sock, int protocol,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +00001028 int kern)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001029{
1030 struct sock *sk = NULL;
1031 struct caifsock *cf_sk = NULL;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001032 static struct proto prot = {.name = "PF_CAIF",
1033 .owner = THIS_MODULE,
1034 .obj_size = sizeof(struct caifsock),
1035 };
1036
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001037 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_NET_ADMIN))
1038 return -EPERM;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001039 /*
1040 * The sock->type specifies the socket type to use.
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001041 * The CAIF socket is a packet stream in the sense
1042 * that it is packet based. CAIF trusts the reliability
1043 * of the link, no resending is implemented.
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001044 */
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001045 if (sock->type == SOCK_SEQPACKET)
1046 sock->ops = &caif_seqpacket_ops;
1047 else if (sock->type == SOCK_STREAM)
1048 sock->ops = &caif_stream_ops;
1049 else
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001050 return -ESOCKTNOSUPPORT;
1051
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001052 if (protocol < 0 || protocol >= CAIFPROTO_MAX)
1053 return -EPROTONOSUPPORT;
1054 /*
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001055 * Set the socket state to unconnected. The socket state
1056 * is really not used at all in the net/core or socket.c but the
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001057 * initialization makes sure that sock->state is not uninitialized.
1058 */
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05001059 sk = sk_alloc(net, PF_CAIF, GFP_KERNEL, &prot, kern);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001060 if (!sk)
1061 return -ENOMEM;
1062
1063 cf_sk = container_of(sk, struct caifsock, sk);
1064
1065 /* Store the protocol */
1066 sk->sk_protocol = (unsigned char) protocol;
1067
Dmitry Tarnyagin44764812012-04-12 08:27:24 +00001068 /* Initialize default priority for well-known cases */
1069 switch (protocol) {
1070 case CAIFPROTO_AT:
1071 sk->sk_priority = TC_PRIO_CONTROL;
1072 break;
1073 case CAIFPROTO_RFM:
1074 sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
1075 break;
1076 default:
1077 sk->sk_priority = TC_PRIO_BESTEFFORT;
1078 }
1079
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001080 /*
1081 * Lock in order to try to stop someone from opening the socket
1082 * too early.
1083 */
1084 lock_sock(&(cf_sk->sk));
1085
1086 /* Initialize the nozero default sock structure data. */
1087 sock_init_data(sock, sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001088 sk->sk_destruct = caif_sock_destructor;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001089
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001090 mutex_init(&cf_sk->readlock); /* single task reading lock */
1091 cf_sk->layer.ctrlcmd = caif_ctrl_cb;
1092 cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
1093 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001094
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001095 set_tx_flow_off(cf_sk);
1096 set_rx_flow_on(cf_sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001097
1098 /* Set default options on configuration */
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001099 cf_sk->conn_req.link_selector = CAIF_LINK_LOW_LATENCY;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001100 cf_sk->conn_req.protocol = protocol;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001101 release_sock(&cf_sk->sk);
1102 return 0;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001103}
1104
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001105
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001106static struct net_proto_family caif_family_ops = {
1107 .family = PF_CAIF,
1108 .create = caif_create,
1109 .owner = THIS_MODULE,
1110};
1111
sjur.brandeland@stericsson.com4a695822012-02-03 04:36:19 +00001112static int __init caif_sktinit_module(void)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001113{
Pan Bianc79e1672016-12-04 12:15:44 +08001114 return sock_register(&caif_family_ops);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001115}
1116
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001117static void __exit caif_sktexit_module(void)
1118{
1119 sock_unregister(PF_CAIF);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001120}
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001121module_init(caif_sktinit_module);
1122module_exit(caif_sktexit_module);