blob: 2b8892d502f7f67fd140c5b40da28b749359fb4d [file] [log] [blame]
Thomas Gleixneraf873fc2019-05-28 09:57:21 -07001// SPDX-License-Identifier: GPL-2.0-only
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00002/*
3 * Copyright (C) ST-Ericsson AB 2010
sjur.brandeland@stericsson.com26ee65e2013-04-22 23:57:01 +00004 * Author: Sjur Brendeland
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00005 */
6
Joe Perchesb31fa5b2010-09-05 21:31:11 +00007#define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
8
Jakub Kicinskib6459412021-12-28 16:49:13 -08009#include <linux/filter.h>
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000010#include <linux/fs.h>
11#include <linux/init.h>
12#include <linux/module.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010013#include <linux/sched/signal.h>
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000014#include <linux/spinlock.h>
15#include <linux/mutex.h>
16#include <linux/list.h>
17#include <linux/wait.h>
18#include <linux/poll.h>
19#include <linux/tcp.h>
20#include <linux/uaccess.h>
Sjur Braendelandbece7b22010-04-28 08:54:38 +000021#include <linux/debugfs.h>
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000022#include <linux/caif/caif_socket.h>
Dmitry Tarnyagin44764812012-04-12 08:27:24 +000023#include <linux/pkt_sched.h>
Sjur Braendelandbece7b22010-04-28 08:54:38 +000024#include <net/sock.h>
25#include <net/tcp_states.h>
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000026#include <net/caif/caif_layer.h>
27#include <net/caif/caif_dev.h>
28#include <net/caif/cfpkt.h>
29
30MODULE_LICENSE("GPL");
Sjur Braendelandbece7b22010-04-28 08:54:38 +000031MODULE_ALIAS_NETPROTO(AF_CAIF);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000032
Sjur Braendelandbece7b22010-04-28 08:54:38 +000033/*
34 * CAIF state is re-using the TCP socket states.
35 * caif_states stored in sk_state reflect the state as reported by
36 * the CAIF stack, while sk_socket->state is the state of the socket.
37 */
38enum caif_states {
39 CAIF_CONNECTED = TCP_ESTABLISHED,
40 CAIF_CONNECTING = TCP_SYN_SENT,
41 CAIF_DISCONNECTED = TCP_CLOSE
42};
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000043
Sjur Braendelandbece7b22010-04-28 08:54:38 +000044#define TX_FLOW_ON_BIT 1
45#define RX_FLOW_ON_BIT 2
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000046
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000047struct caifsock {
Sjur Braendelandbece7b22010-04-28 08:54:38 +000048 struct sock sk; /* must be first member */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000049 struct cflayer layer;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000050 u32 flow_state;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000051 struct caif_connect_request conn_req;
Sjur Braendelandbece7b22010-04-28 08:54:38 +000052 struct mutex readlock;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000053 struct dentry *debugfs_socket_dir;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +000054 int headroom, tailroom, maxframe;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000055};
56
Sjur Braendelandbece7b22010-04-28 08:54:38 +000057static int rx_flow_is_on(struct caifsock *cf_sk)
58{
59 return test_bit(RX_FLOW_ON_BIT,
60 (void *) &cf_sk->flow_state);
61}
62
63static int tx_flow_is_on(struct caifsock *cf_sk)
64{
65 return test_bit(TX_FLOW_ON_BIT,
66 (void *) &cf_sk->flow_state);
67}
68
69static void set_rx_flow_off(struct caifsock *cf_sk)
70{
71 clear_bit(RX_FLOW_ON_BIT,
72 (void *) &cf_sk->flow_state);
73}
74
75static void set_rx_flow_on(struct caifsock *cf_sk)
76{
77 set_bit(RX_FLOW_ON_BIT,
78 (void *) &cf_sk->flow_state);
79}
80
81static void set_tx_flow_off(struct caifsock *cf_sk)
82{
83 clear_bit(TX_FLOW_ON_BIT,
84 (void *) &cf_sk->flow_state);
85}
86
87static void set_tx_flow_on(struct caifsock *cf_sk)
88{
89 set_bit(TX_FLOW_ON_BIT,
90 (void *) &cf_sk->flow_state);
91}
92
93static void caif_read_lock(struct sock *sk)
94{
95 struct caifsock *cf_sk;
96 cf_sk = container_of(sk, struct caifsock, sk);
97 mutex_lock(&cf_sk->readlock);
98}
99
100static void caif_read_unlock(struct sock *sk)
101{
102 struct caifsock *cf_sk;
103 cf_sk = container_of(sk, struct caifsock, sk);
104 mutex_unlock(&cf_sk->readlock);
105}
106
Sjur Braendelanda9a8f102010-05-21 02:16:11 +0000107static int sk_rcvbuf_lowwater(struct caifsock *cf_sk)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000108{
109 /* A quarter of full buffer is used a low water mark */
110 return cf_sk->sk.sk_rcvbuf / 4;
111}
112
Sjur Braendelanda9a8f102010-05-21 02:16:11 +0000113static void caif_flow_ctrl(struct sock *sk, int mode)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000114{
115 struct caifsock *cf_sk;
116 cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000117 if (cf_sk->layer.dn && cf_sk->layer.dn->modemcmd)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000118 cf_sk->layer.dn->modemcmd(cf_sk->layer.dn, mode);
119}
120
121/*
122 * Copied from sock.c:sock_queue_rcv_skb(), but changed so packets are
123 * not dropped, but CAIF is sending flow off instead.
124 */
Eric Dumazetb8a23e82015-07-17 10:19:23 +0200125static void caif_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000126{
127 int err;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000128 unsigned long flags;
129 struct sk_buff_head *list = &sk->sk_receive_queue;
130 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Eric Dumazetb8a23e82015-07-17 10:19:23 +0200131 bool queued = false;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000132
133 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +0000134 (unsigned int)sk->sk_rcvbuf && rx_flow_is_on(cf_sk)) {
Joe Perchese87cc472012-05-13 21:56:26 +0000135 net_dbg_ratelimited("sending flow OFF (queue len = %d %d)\n",
136 atomic_read(&cf_sk->sk.sk_rmem_alloc),
137 sk_rcvbuf_lowwater(cf_sk));
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000138 set_rx_flow_off(cf_sk);
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000139 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_OFF_REQ);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000140 }
141
142 err = sk_filter(sk, skb);
143 if (err)
Eric Dumazetb8a23e82015-07-17 10:19:23 +0200144 goto out;
145
Mel Gormanc76562b2012-07-31 16:44:41 -0700146 if (!sk_rmem_schedule(sk, skb, skb->truesize) && rx_flow_is_on(cf_sk)) {
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000147 set_rx_flow_off(cf_sk);
Joe Perchese87cc472012-05-13 21:56:26 +0000148 net_dbg_ratelimited("sending flow OFF due to rmem_schedule\n");
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000149 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_OFF_REQ);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000150 }
151 skb->dev = NULL;
152 skb_set_owner_r(skb, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000153 spin_lock_irqsave(&list->lock, flags);
Eric Dumazetb8a23e82015-07-17 10:19:23 +0200154 queued = !sock_flag(sk, SOCK_DEAD);
155 if (queued)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000156 __skb_queue_tail(list, skb);
157 spin_unlock_irqrestore(&list->lock, flags);
Eric Dumazetb8a23e82015-07-17 10:19:23 +0200158out:
159 if (queued)
David S. Miller676d2362014-04-11 16:15:36 -0400160 sk->sk_data_ready(sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000161 else
162 kfree_skb(skb);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000163}
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000164
165/* Packet Receive Callback function called from CAIF Stack */
166static int caif_sktrecv_cb(struct cflayer *layr, struct cfpkt *pkt)
167{
168 struct caifsock *cf_sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000169 struct sk_buff *skb;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000170
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000171 cf_sk = container_of(layr, struct caifsock, layer);
172 skb = cfpkt_tonative(pkt);
173
174 if (unlikely(cf_sk->sk.sk_state != CAIF_CONNECTED)) {
sjur.brandeland@stericsson.com3f874ad2011-05-13 02:44:08 +0000175 kfree_skb(skb);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000176 return 0;
177 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000178 caif_queue_rcv_skb(&cf_sk->sk, skb);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000179 return 0;
180}
181
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000182static void cfsk_hold(struct cflayer *layr)
183{
184 struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
185 sock_hold(&cf_sk->sk);
186}
187
188static void cfsk_put(struct cflayer *layr)
189{
190 struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
191 sock_put(&cf_sk->sk);
192}
193
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000194/* Packet Control Callback function called from CAIF */
195static void caif_ctrl_cb(struct cflayer *layr,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000196 enum caif_ctrlcmd flow,
197 int phyid)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000198{
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000199 struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000200 switch (flow) {
201 case CAIF_CTRLCMD_FLOW_ON_IND:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000202 /* OK from modem to start sending again */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000203 set_tx_flow_on(cf_sk);
204 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000205 break;
206
207 case CAIF_CTRLCMD_FLOW_OFF_IND:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000208 /* Modem asks us to shut up */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000209 set_tx_flow_off(cf_sk);
210 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000211 break;
212
213 case CAIF_CTRLCMD_INIT_RSP:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000214 /* We're now connected */
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000215 caif_client_register_refcnt(&cf_sk->layer,
216 cfsk_hold, cfsk_put);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000217 cf_sk->sk.sk_state = CAIF_CONNECTED;
218 set_tx_flow_on(cf_sk);
Sjur Brændelandeaa8c5f2012-06-24 11:01:36 +0000219 cf_sk->sk.sk_shutdown = 0;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000220 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000221 break;
222
223 case CAIF_CTRLCMD_DEINIT_RSP:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000224 /* We're now disconnected */
225 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
226 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000227 break;
228
229 case CAIF_CTRLCMD_INIT_FAIL_RSP:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000230 /* Connect request failed */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000231 cf_sk->sk.sk_err = ECONNREFUSED;
232 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
233 cf_sk->sk.sk_shutdown = SHUTDOWN_MASK;
234 /*
235 * Socket "standards" seems to require POLLOUT to
236 * be set at connect failure.
237 */
238 set_tx_flow_on(cf_sk);
239 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000240 break;
241
242 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000243 /* Modem has closed this connection, or device is down. */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000244 cf_sk->sk.sk_shutdown = SHUTDOWN_MASK;
245 cf_sk->sk.sk_err = ECONNRESET;
246 set_rx_flow_on(cf_sk);
Alexander Aringe3ae2362021-06-27 18:48:21 -0400247 sk_error_report(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000248 break;
249
250 default:
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000251 pr_debug("Unexpected flow command %d\n", flow);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000252 }
253}
254
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000255static void caif_check_flow_release(struct sock *sk)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000256{
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000257 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
258
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000259 if (rx_flow_is_on(cf_sk))
260 return;
261
262 if (atomic_read(&sk->sk_rmem_alloc) <= sk_rcvbuf_lowwater(cf_sk)) {
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000263 set_rx_flow_on(cf_sk);
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000264 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_ON_REQ);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000265 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000266}
267
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000268/*
269 * Copied from unix_dgram_recvmsg, but removed credit checks,
270 * changed locking, address handling and added MSG_TRUNC.
271 */
Ying Xue1b784142015-03-02 15:37:48 +0800272static int caif_seqpkt_recvmsg(struct socket *sock, struct msghdr *m,
273 size_t len, int flags)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000274
275{
276 struct sock *sk = sock->sk;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000277 struct sk_buff *skb;
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000278 int ret;
279 int copylen;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000280
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000281 ret = -EOPNOTSUPP;
Al Viro3eeff772015-03-14 05:22:21 +0000282 if (flags & MSG_OOB)
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000283 goto read_error;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000284
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000285 skb = skb_recv_datagram(sk, flags, 0 , &ret);
286 if (!skb)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000287 goto read_error;
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000288 copylen = skb->len;
289 if (len < copylen) {
290 m->msg_flags |= MSG_TRUNC;
291 copylen = len;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000292 }
293
David S. Miller51f3d022014-11-05 16:46:40 -0500294 ret = skb_copy_datagram_msg(skb, 0, m, copylen);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000295 if (ret)
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000296 goto out_free;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000297
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000298 ret = (flags & MSG_TRUNC) ? skb->len : copylen;
299out_free:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000300 skb_free_datagram(sk, skb);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000301 caif_check_flow_release(sk);
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000302 return ret;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000303
304read_error:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000305 return ret;
306}
307
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000308
309/* Copied from unix_stream_wait_data, identical except for lock call. */
310static long caif_stream_data_wait(struct sock *sk, long timeo)
311{
312 DEFINE_WAIT(wait);
313 lock_sock(sk);
314
315 for (;;) {
316 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
317
318 if (!skb_queue_empty(&sk->sk_receive_queue) ||
319 sk->sk_err ||
320 sk->sk_state != CAIF_CONNECTED ||
321 sock_flag(sk, SOCK_DEAD) ||
322 (sk->sk_shutdown & RCV_SHUTDOWN) ||
323 signal_pending(current) ||
324 !timeo)
325 break;
326
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800327 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000328 release_sock(sk);
329 timeo = schedule_timeout(timeo);
330 lock_sock(sk);
Mark Salyzynb48732e2015-05-26 08:22:19 -0700331
332 if (sock_flag(sk, SOCK_DEAD))
333 break;
334
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800335 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000336 }
337
338 finish_wait(sk_sleep(sk), &wait);
339 release_sock(sk);
340 return timeo;
341}
342
343
344/*
345 * Copied from unix_stream_recvmsg, but removed credit checks,
346 * changed locking calls, changed address handling.
347 */
Ying Xue1b784142015-03-02 15:37:48 +0800348static int caif_stream_recvmsg(struct socket *sock, struct msghdr *msg,
349 size_t size, int flags)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000350{
351 struct sock *sk = sock->sk;
352 int copied = 0;
353 int target;
354 int err = 0;
355 long timeo;
356
357 err = -EOPNOTSUPP;
358 if (flags&MSG_OOB)
359 goto out;
360
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000361 /*
362 * Lock the socket to prevent queue disordering
363 * while sleeps in memcpy_tomsg
364 */
365 err = -EAGAIN;
366 if (sk->sk_state == CAIF_CONNECTING)
367 goto out;
368
369 caif_read_lock(sk);
370 target = sock_rcvlowat(sk, flags&MSG_WAITALL, size);
371 timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
372
373 do {
374 int chunk;
375 struct sk_buff *skb;
376
377 lock_sock(sk);
Mark Salyzynb48732e2015-05-26 08:22:19 -0700378 if (sock_flag(sk, SOCK_DEAD)) {
379 err = -ECONNRESET;
380 goto unlock;
381 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000382 skb = skb_dequeue(&sk->sk_receive_queue);
383 caif_check_flow_release(sk);
384
385 if (skb == NULL) {
386 if (copied >= target)
387 goto unlock;
388 /*
389 * POSIX 1003.1g mandates this order.
390 */
391 err = sock_error(sk);
392 if (err)
393 goto unlock;
394 err = -ECONNRESET;
395 if (sk->sk_shutdown & RCV_SHUTDOWN)
396 goto unlock;
397
398 err = -EPIPE;
399 if (sk->sk_state != CAIF_CONNECTED)
400 goto unlock;
401 if (sock_flag(sk, SOCK_DEAD))
402 goto unlock;
403
404 release_sock(sk);
405
406 err = -EAGAIN;
407 if (!timeo)
408 break;
409
410 caif_read_unlock(sk);
411
412 timeo = caif_stream_data_wait(sk, timeo);
413
414 if (signal_pending(current)) {
415 err = sock_intr_errno(timeo);
416 goto out;
417 }
418 caif_read_lock(sk);
419 continue;
420unlock:
421 release_sock(sk);
422 break;
423 }
424 release_sock(sk);
425 chunk = min_t(unsigned int, skb->len, size);
Al Viro7eab8d92014-04-06 21:51:23 -0400426 if (memcpy_to_msg(msg, skb->data, chunk)) {
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000427 skb_queue_head(&sk->sk_receive_queue, skb);
428 if (copied == 0)
429 copied = -EFAULT;
430 break;
431 }
432 copied += chunk;
433 size -= chunk;
434
435 /* Mark read part of skb as used */
436 if (!(flags & MSG_PEEK)) {
437 skb_pull(skb, chunk);
438
439 /* put the skb back if we didn't use it up. */
440 if (skb->len) {
441 skb_queue_head(&sk->sk_receive_queue, skb);
442 break;
443 }
444 kfree_skb(skb);
445
446 } else {
447 /*
448 * It is questionable, see note in unix_dgram_recvmsg.
449 */
450 /* put message back and return */
451 skb_queue_head(&sk->sk_receive_queue, skb);
452 break;
453 }
454 } while (size);
455 caif_read_unlock(sk);
456
457out:
458 return copied ? : err;
459}
460
461/*
462 * Copied from sock.c:sock_wait_for_wmem, but change to wait for
463 * CAIF flow-on and sock_writable.
464 */
465static long caif_wait_for_flow_on(struct caifsock *cf_sk,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000466 int wait_writeable, long timeo, int *err)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000467{
468 struct sock *sk = &cf_sk->sk;
469 DEFINE_WAIT(wait);
470 for (;;) {
471 *err = 0;
472 if (tx_flow_is_on(cf_sk) &&
473 (!wait_writeable || sock_writeable(&cf_sk->sk)))
474 break;
475 *err = -ETIMEDOUT;
476 if (!timeo)
477 break;
478 *err = -ERESTARTSYS;
479 if (signal_pending(current))
480 break;
481 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
482 *err = -ECONNRESET;
483 if (sk->sk_shutdown & SHUTDOWN_MASK)
484 break;
485 *err = -sk->sk_err;
486 if (sk->sk_err)
487 break;
488 *err = -EPIPE;
489 if (cf_sk->sk.sk_state != CAIF_CONNECTED)
490 break;
491 timeo = schedule_timeout(timeo);
492 }
493 finish_wait(sk_sleep(sk), &wait);
494 return timeo;
495}
496
497/*
498 * Transmit a SKB. The device may temporarily request re-transmission
499 * by returning EAGAIN.
500 */
501static int transmit_skb(struct sk_buff *skb, struct caifsock *cf_sk,
502 int noblock, long timeo)
503{
504 struct cfpkt *pkt;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000505
506 pkt = cfpkt_fromnative(CAIF_DIR_OUT, skb);
sjur.brandeland@stericsson.com3f874ad2011-05-13 02:44:08 +0000507 memset(skb->cb, 0, sizeof(struct caif_payload_info));
Dmitry Tarnyagin44764812012-04-12 08:27:24 +0000508 cfpkt_set_prio(pkt, cf_sk->sk.sk_priority);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000509
Dmitry Tarnyaginba760572012-02-02 01:21:03 +0000510 if (cf_sk->layer.dn == NULL) {
511 kfree_skb(skb);
Sjur Brændeland4dd820c2011-04-11 10:43:51 +0000512 return -EINVAL;
Dmitry Tarnyaginba760572012-02-02 01:21:03 +0000513 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000514
Sjur Brændeland4dd820c2011-04-11 10:43:51 +0000515 return cf_sk->layer.dn->transmit(cf_sk->layer.dn, pkt);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000516}
517
518/* Copied from af_unix:unix_dgram_sendmsg, and adapted to CAIF */
Ying Xue1b784142015-03-02 15:37:48 +0800519static int caif_seqpkt_sendmsg(struct socket *sock, struct msghdr *msg,
520 size_t len)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000521{
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000522 struct sock *sk = sock->sk;
523 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000524 int buffer_size;
525 int ret = 0;
526 struct sk_buff *skb = NULL;
527 int noblock;
528 long timeo;
529 caif_assert(cf_sk);
530 ret = sock_error(sk);
531 if (ret)
532 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000533
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000534 ret = -EOPNOTSUPP;
535 if (msg->msg_flags&MSG_OOB)
536 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000537
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000538 ret = -EOPNOTSUPP;
539 if (msg->msg_namelen)
540 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000541
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000542 ret = -EINVAL;
Ziyang Xuan991e6342021-07-15 20:22:04 +0800543 if (unlikely(msg->msg_iter.nr_segs == 0) ||
544 unlikely(msg->msg_iter.iov->iov_base == NULL))
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000545 goto err;
546 noblock = msg->msg_flags & MSG_DONTWAIT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000547
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000548 timeo = sock_sndtimeo(sk, noblock);
549 timeo = caif_wait_for_flow_on(container_of(sk, struct caifsock, sk),
550 1, timeo, &ret);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000551
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000552 if (ret)
553 goto err;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000554 ret = -EPIPE;
555 if (cf_sk->sk.sk_state != CAIF_CONNECTED ||
556 sock_flag(sk, SOCK_DEAD) ||
557 (sk->sk_shutdown & RCV_SHUTDOWN))
558 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000559
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000560 /* Error if trying to write more than maximum frame size. */
561 ret = -EMSGSIZE;
562 if (len > cf_sk->maxframe && cf_sk->sk.sk_protocol != CAIFPROTO_RFM)
563 goto err;
564
565 buffer_size = len + cf_sk->headroom + cf_sk->tailroom;
566
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000567 ret = -ENOMEM;
568 skb = sock_alloc_send_skb(sk, buffer_size, noblock, &ret);
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000569
570 if (!skb || skb_tailroom(skb) < buffer_size)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000571 goto err;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000572
573 skb_reserve(skb, cf_sk->headroom);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000574
Al Viro6ce8e9c2014-04-06 21:25:44 -0400575 ret = memcpy_from_msg(skb_put(skb, len), msg, len);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000576
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000577 if (ret)
578 goto err;
579 ret = transmit_skb(skb, cf_sk, noblock, timeo);
580 if (ret < 0)
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000581 /* skb is already freed */
582 return ret;
583
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000584 return len;
585err:
586 kfree_skb(skb);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000587 return ret;
588}
589
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000590/*
591 * Copied from unix_stream_sendmsg and adapted to CAIF:
592 * Changed removed permission handling and added waiting for flow on
593 * and other minor adaptations.
594 */
Ying Xue1b784142015-03-02 15:37:48 +0800595static int caif_stream_sendmsg(struct socket *sock, struct msghdr *msg,
596 size_t len)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000597{
598 struct sock *sk = sock->sk;
599 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000600 int err, size;
601 struct sk_buff *skb;
602 int sent = 0;
603 long timeo;
604
605 err = -EOPNOTSUPP;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000606 if (unlikely(msg->msg_flags&MSG_OOB))
607 goto out_err;
608
609 if (unlikely(msg->msg_namelen))
610 goto out_err;
611
612 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
613 timeo = caif_wait_for_flow_on(cf_sk, 1, timeo, &err);
614
615 if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN))
616 goto pipe_err;
617
618 while (sent < len) {
619
620 size = len-sent;
621
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000622 if (size > cf_sk->maxframe)
623 size = cf_sk->maxframe;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000624
625 /* If size is more than half of sndbuf, chop up message */
626 if (size > ((sk->sk_sndbuf >> 1) - 64))
627 size = (sk->sk_sndbuf >> 1) - 64;
628
629 if (size > SKB_MAX_ALLOC)
630 size = SKB_MAX_ALLOC;
631
632 skb = sock_alloc_send_skb(sk,
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000633 size + cf_sk->headroom +
634 cf_sk->tailroom,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000635 msg->msg_flags&MSG_DONTWAIT,
636 &err);
637 if (skb == NULL)
638 goto out_err;
639
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000640 skb_reserve(skb, cf_sk->headroom);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000641 /*
642 * If you pass two values to the sock_alloc_send_skb
643 * it tries to grab the large buffer with GFP_NOFS
644 * (which can fail easily), and if it fails grab the
645 * fallback size buffer which is under a page and will
646 * succeed. [Alan]
647 */
648 size = min_t(int, size, skb_tailroom(skb));
649
Al Viro6ce8e9c2014-04-06 21:25:44 -0400650 err = memcpy_from_msg(skb_put(skb, size), msg, size);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000651 if (err) {
652 kfree_skb(skb);
653 goto out_err;
654 }
655 err = transmit_skb(skb, cf_sk,
656 msg->msg_flags&MSG_DONTWAIT, timeo);
Dmitry Tarnyaginba760572012-02-02 01:21:03 +0000657 if (err < 0)
658 /* skb is already freed */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000659 goto pipe_err;
Dmitry Tarnyaginba760572012-02-02 01:21:03 +0000660
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000661 sent += size;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000662 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000663
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000664 return sent;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000665
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000666pipe_err:
667 if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL))
668 send_sig(SIGPIPE, current, 0);
669 err = -EPIPE;
670out_err:
671 return sent ? : err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000672}
673
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200674static int setsockopt(struct socket *sock, int lvl, int opt, sockptr_t ov,
675 unsigned int ol)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000676{
677 struct sock *sk = sock->sk;
678 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +0000679 int linksel;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000680
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000681 if (cf_sk->sk.sk_socket->state != SS_UNCONNECTED)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000682 return -ENOPROTOOPT;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000683
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000684 switch (opt) {
685 case CAIFSO_LINK_SELECT:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000686 if (ol < sizeof(int))
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000687 return -EINVAL;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000688 if (lvl != SOL_CAIF)
689 goto bad_sol;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200690 if (copy_from_sockptr(&linksel, ov, sizeof(int)))
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000691 return -EINVAL;
692 lock_sock(&(cf_sk->sk));
693 cf_sk->conn_req.link_selector = linksel;
694 release_sock(&cf_sk->sk);
695 return 0;
696
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000697 case CAIFSO_REQ_PARAM:
698 if (lvl != SOL_CAIF)
699 goto bad_sol;
700 if (cf_sk->sk.sk_protocol != CAIFPROTO_UTIL)
701 return -ENOPROTOOPT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000702 lock_sock(&(cf_sk->sk));
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000703 if (ol > sizeof(cf_sk->conn_req.param.data) ||
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200704 copy_from_sockptr(&cf_sk->conn_req.param.data, ov, ol)) {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000705 release_sock(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000706 return -EINVAL;
707 }
Dan Rosenberg91b5c982011-01-10 16:00:54 -0800708 cf_sk->conn_req.param.size = ol;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000709 release_sock(&cf_sk->sk);
710 return 0;
711
712 default:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000713 return -ENOPROTOOPT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000714 }
715
716 return 0;
717bad_sol:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000718 return -ENOPROTOOPT;
719
720}
721
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000722/*
723 * caif_connect() - Connect a CAIF Socket
724 * Copied and modified af_irda.c:irda_connect().
725 *
726 * Note : by consulting "errno", the user space caller may learn the cause
727 * of the failure. Most of them are visible in the function, others may come
728 * from subroutines called and are listed here :
729 * o -EAFNOSUPPORT: bad socket family or type.
730 * o -ESOCKTNOSUPPORT: bad socket type or protocol
731 * o -EINVAL: bad socket address, or CAIF link type
732 * o -ECONNREFUSED: remote end refused the connection.
733 * o -EINPROGRESS: connect request sent but timed out (or non-blocking)
734 * o -EISCONN: already connected.
735 * o -ETIMEDOUT: Connection timed out (send timeout)
736 * o -ENODEV: No link layer to send request
737 * o -ECONNRESET: Received Shutdown indication or lost link layer
738 * o -ENOMEM: Out of memory
739 *
740 * State Strategy:
741 * o sk_state: holds the CAIF_* protocol state, it's updated by
742 * caif_ctrl_cb.
743 * o sock->state: holds the SS_* socket state and is updated by connect and
744 * disconnect.
745 */
746static int caif_connect(struct socket *sock, struct sockaddr *uaddr,
747 int addr_len, int flags)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000748{
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000749 struct sock *sk = sock->sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000750 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
751 long timeo;
752 int err;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000753 int ifindex, headroom, tailroom;
Eric Dumazet79315062010-10-04 22:42:08 +0000754 unsigned int mtu;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000755 struct net_device *dev;
756
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000757 lock_sock(sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000758
Mateusz Jurczyk20a3d5b2017-06-13 20:06:12 +0200759 err = -EINVAL;
760 if (addr_len < offsetofend(struct sockaddr, sa_family))
761 goto out;
762
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000763 err = -EAFNOSUPPORT;
764 if (uaddr->sa_family != AF_CAIF)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000765 goto out;
766
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000767 switch (sock->state) {
768 case SS_UNCONNECTED:
769 /* Normal case, a fresh connect */
770 caif_assert(sk->sk_state == CAIF_DISCONNECTED);
771 break;
772 case SS_CONNECTING:
773 switch (sk->sk_state) {
774 case CAIF_CONNECTED:
775 sock->state = SS_CONNECTED;
776 err = -EISCONN;
777 goto out;
778 case CAIF_DISCONNECTED:
779 /* Reconnect allowed */
780 break;
781 case CAIF_CONNECTING:
782 err = -EALREADY;
783 if (flags & O_NONBLOCK)
784 goto out;
785 goto wait_connect;
786 }
787 break;
788 case SS_CONNECTED:
789 caif_assert(sk->sk_state == CAIF_CONNECTED ||
790 sk->sk_state == CAIF_DISCONNECTED);
791 if (sk->sk_shutdown & SHUTDOWN_MASK) {
792 /* Allow re-connect after SHUTDOWN_IND */
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000793 caif_disconnect_client(sock_net(sk), &cf_sk->layer);
sjur.brandeland@stericsson.com54e90fb2011-05-22 11:18:51 +0000794 caif_free_client(&cf_sk->layer);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000795 break;
796 }
797 /* No reconnect on a seqpacket socket */
798 err = -EISCONN;
799 goto out;
800 case SS_DISCONNECTING:
801 case SS_FREE:
802 caif_assert(1); /*Should never happen */
803 break;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000804 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000805 sk->sk_state = CAIF_DISCONNECTED;
806 sock->state = SS_UNCONNECTED;
807 sk_stream_kill_queues(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000808
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000809 err = -EINVAL;
Dan Carpenterf5d72af2010-05-22 10:43:42 +0000810 if (addr_len != sizeof(struct sockaddr_caif))
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000811 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000812
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000813 memcpy(&cf_sk->conn_req.sockaddr, uaddr,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000814 sizeof(struct sockaddr_caif));
815
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000816 /* Move to connecting socket, start sending Connect Requests */
817 sock->state = SS_CONNECTING;
818 sk->sk_state = CAIF_CONNECTING;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000819
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +0000820 /* Check priority value comming from socket */
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +0000821 /* if priority value is out of range it will be ajusted */
822 if (cf_sk->sk.sk_priority > CAIF_PRIO_MAX)
823 cf_sk->conn_req.priority = CAIF_PRIO_MAX;
824 else if (cf_sk->sk.sk_priority < CAIF_PRIO_MIN)
825 cf_sk->conn_req.priority = CAIF_PRIO_MIN;
826 else
827 cf_sk->conn_req.priority = cf_sk->sk.sk_priority;
828
829 /*ifindex = id of the interface.*/
830 cf_sk->conn_req.ifindex = cf_sk->sk.sk_bound_dev_if;
831
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000832 cf_sk->layer.receive = caif_sktrecv_cb;
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000833
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000834 err = caif_connect_client(sock_net(sk), &cf_sk->conn_req,
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000835 &cf_sk->layer, &ifindex, &headroom, &tailroom);
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000836
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000837 if (err < 0) {
838 cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
839 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
840 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000841 }
Eric Dumazet79315062010-10-04 22:42:08 +0000842
843 err = -ENODEV;
844 rcu_read_lock();
845 dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
846 if (!dev) {
847 rcu_read_unlock();
848 goto out;
849 }
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000850 cf_sk->headroom = LL_RESERVED_SPACE_EXTRA(dev, headroom);
Eric Dumazet79315062010-10-04 22:42:08 +0000851 mtu = dev->mtu;
852 rcu_read_unlock();
853
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000854 cf_sk->tailroom = tailroom;
Eric Dumazet79315062010-10-04 22:42:08 +0000855 cf_sk->maxframe = mtu - (headroom + tailroom);
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000856 if (cf_sk->maxframe < 1) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000857 pr_warn("CAIF Interface MTU too small (%d)\n", dev->mtu);
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +0000858 err = -ENODEV;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000859 goto out;
860 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000861
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000862 err = -EINPROGRESS;
863wait_connect:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000864
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000865 if (sk->sk_state != CAIF_CONNECTED && (flags & O_NONBLOCK))
866 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000867
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000868 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000869
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000870 release_sock(sk);
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000871 err = -ERESTARTSYS;
872 timeo = wait_event_interruptible_timeout(*sk_sleep(sk),
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000873 sk->sk_state != CAIF_CONNECTING,
874 timeo);
875 lock_sock(sk);
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000876 if (timeo < 0)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000877 goto out; /* -ERESTARTSYS */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000878
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000879 err = -ETIMEDOUT;
880 if (timeo == 0 && sk->sk_state != CAIF_CONNECTED)
881 goto out;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000882 if (sk->sk_state != CAIF_CONNECTED) {
883 sock->state = SS_UNCONNECTED;
884 err = sock_error(sk);
885 if (!err)
886 err = -ECONNREFUSED;
887 goto out;
888 }
889 sock->state = SS_CONNECTED;
890 err = 0;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000891out:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000892 release_sock(sk);
893 return err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000894}
895
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000896/*
897 * caif_release() - Disconnect a CAIF Socket
898 * Copied and modified af_irda.c:irda_release().
899 */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000900static int caif_release(struct socket *sock)
901{
902 struct sock *sk = sock->sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000903 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000904
905 if (!sk)
906 return 0;
907
908 set_tx_flow_off(cf_sk);
909
910 /*
911 * Ensure that packets are not queued after this point in time.
912 * caif_queue_rcv_skb checks SOCK_DEAD holding the queue lock,
913 * this ensures no packets when sock is dead.
914 */
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000915 spin_lock_bh(&sk->sk_receive_queue.lock);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000916 sock_set_flag(sk, SOCK_DEAD);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000917 spin_unlock_bh(&sk->sk_receive_queue.lock);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000918 sock->sk = NULL;
919
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +0000920 WARN_ON(IS_ERR(cf_sk->debugfs_socket_dir));
Fabian Frederickfb0d1642014-06-27 23:07:43 +0200921 debugfs_remove_recursive(cf_sk->debugfs_socket_dir);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000922
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000923 lock_sock(&(cf_sk->sk));
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000924 sk->sk_state = CAIF_DISCONNECTED;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000925 sk->sk_shutdown = SHUTDOWN_MASK;
926
sjur.brandeland@stericsson.com54e90fb2011-05-22 11:18:51 +0000927 caif_disconnect_client(sock_net(sk), &cf_sk->layer);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000928 cf_sk->sk.sk_socket->state = SS_DISCONNECTING;
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800929 wake_up_interruptible_poll(sk_sleep(sk), EPOLLERR|EPOLLHUP);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000930
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000931 sock_orphan(sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000932 sk_stream_kill_queues(&cf_sk->sk);
933 release_sock(sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000934 sock_put(sk);
sjur.brandeland@stericsson.com54e90fb2011-05-22 11:18:51 +0000935 return 0;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000936}
937
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000938/* Copied from af_unix.c:unix_poll(), added CAIF tx_flow handling */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700939static __poll_t caif_poll(struct file *file,
940 struct socket *sock, poll_table *wait)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000941{
942 struct sock *sk = sock->sk;
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700943 __poll_t mask;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000944 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700945
Karsten Graul89ab0662018-10-23 13:40:39 +0200946 sock_poll_wait(file, sock, wait);
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700947 mask = 0;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000948
949 /* exceptional events? */
950 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800951 mask |= EPOLLERR;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000952 if (sk->sk_shutdown == SHUTDOWN_MASK)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800953 mask |= EPOLLHUP;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000954 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800955 mask |= EPOLLRDHUP;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000956
957 /* readable? */
Eric Dumazet3ef7cf52019-10-23 22:44:50 -0700958 if (!skb_queue_empty_lockless(&sk->sk_receive_queue) ||
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000959 (sk->sk_shutdown & RCV_SHUTDOWN))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800960 mask |= EPOLLIN | EPOLLRDNORM;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000961
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000962 /*
963 * we set writable also when the other side has shut down the
964 * connection. This prevents stuck sockets.
965 */
966 if (sock_writeable(sk) && tx_flow_is_on(cf_sk))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800967 mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000968
969 return mask;
970}
971
972static const struct proto_ops caif_seqpacket_ops = {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000973 .family = PF_CAIF,
974 .owner = THIS_MODULE,
975 .release = caif_release,
976 .bind = sock_no_bind,
977 .connect = caif_connect,
978 .socketpair = sock_no_socketpair,
979 .accept = sock_no_accept,
980 .getname = sock_no_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700981 .poll = caif_poll,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000982 .ioctl = sock_no_ioctl,
983 .listen = sock_no_listen,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000984 .shutdown = sock_no_shutdown,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000985 .setsockopt = setsockopt,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000986 .sendmsg = caif_seqpkt_sendmsg,
987 .recvmsg = caif_seqpkt_recvmsg,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000988 .mmap = sock_no_mmap,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000989 .sendpage = sock_no_sendpage,
990};
991
992static const struct proto_ops caif_stream_ops = {
993 .family = PF_CAIF,
994 .owner = THIS_MODULE,
995 .release = caif_release,
996 .bind = sock_no_bind,
997 .connect = caif_connect,
998 .socketpair = sock_no_socketpair,
999 .accept = sock_no_accept,
1000 .getname = sock_no_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001001 .poll = caif_poll,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001002 .ioctl = sock_no_ioctl,
1003 .listen = sock_no_listen,
1004 .shutdown = sock_no_shutdown,
1005 .setsockopt = setsockopt,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001006 .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);
Reshetova, Elena14afee42017-06-30 13:08:00 +03001016 caif_assert(!refcount_read(&sk->sk_wmem_alloc));
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001017 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),
David Windsor93070d32017-08-24 16:58:35 -07001035 .useroffset = offsetof(struct caifsock, conn_req.param),
1036 .usersize = sizeof_field(struct caifsock, conn_req.param)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001037 };
1038
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001039 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_NET_ADMIN))
1040 return -EPERM;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001041 /*
1042 * The sock->type specifies the socket type to use.
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001043 * The CAIF socket is a packet stream in the sense
1044 * that it is packet based. CAIF trusts the reliability
1045 * of the link, no resending is implemented.
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001046 */
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001047 if (sock->type == SOCK_SEQPACKET)
1048 sock->ops = &caif_seqpacket_ops;
1049 else if (sock->type == SOCK_STREAM)
1050 sock->ops = &caif_stream_ops;
1051 else
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001052 return -ESOCKTNOSUPPORT;
1053
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001054 if (protocol < 0 || protocol >= CAIFPROTO_MAX)
1055 return -EPROTONOSUPPORT;
1056 /*
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001057 * Set the socket state to unconnected. The socket state
1058 * is really not used at all in the net/core or socket.c but the
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001059 * initialization makes sure that sock->state is not uninitialized.
1060 */
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05001061 sk = sk_alloc(net, PF_CAIF, GFP_KERNEL, &prot, kern);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001062 if (!sk)
1063 return -ENOMEM;
1064
1065 cf_sk = container_of(sk, struct caifsock, sk);
1066
1067 /* Store the protocol */
1068 sk->sk_protocol = (unsigned char) protocol;
1069
Dmitry Tarnyagin44764812012-04-12 08:27:24 +00001070 /* Initialize default priority for well-known cases */
1071 switch (protocol) {
1072 case CAIFPROTO_AT:
1073 sk->sk_priority = TC_PRIO_CONTROL;
1074 break;
1075 case CAIFPROTO_RFM:
1076 sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
1077 break;
1078 default:
1079 sk->sk_priority = TC_PRIO_BESTEFFORT;
1080 }
1081
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001082 /*
1083 * Lock in order to try to stop someone from opening the socket
1084 * too early.
1085 */
1086 lock_sock(&(cf_sk->sk));
1087
1088 /* Initialize the nozero default sock structure data. */
1089 sock_init_data(sock, sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001090 sk->sk_destruct = caif_sock_destructor;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001091
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001092 mutex_init(&cf_sk->readlock); /* single task reading lock */
1093 cf_sk->layer.ctrlcmd = caif_ctrl_cb;
1094 cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
1095 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001096
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001097 set_tx_flow_off(cf_sk);
1098 set_rx_flow_on(cf_sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001099
1100 /* Set default options on configuration */
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001101 cf_sk->conn_req.link_selector = CAIF_LINK_LOW_LATENCY;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001102 cf_sk->conn_req.protocol = protocol;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001103 release_sock(&cf_sk->sk);
1104 return 0;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001105}
1106
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001107
linzhang173e7832017-05-15 10:26:47 +08001108static const struct net_proto_family caif_family_ops = {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001109 .family = PF_CAIF,
1110 .create = caif_create,
1111 .owner = THIS_MODULE,
1112};
1113
sjur.brandeland@stericsson.com4a695822012-02-03 04:36:19 +00001114static int __init caif_sktinit_module(void)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001115{
Pan Bianc79e1672016-12-04 12:15:44 +08001116 return sock_register(&caif_family_ops);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001117}
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);