aboutsummaryrefslogtreecommitdiffstats
path: root/net/caif
diff options
context:
space:
mode:
authorSjur Braendeland <sjur.brandeland@stericsson.com>2010-04-28 04:54:38 -0400
committerDavid S. Miller <davem@davemloft.net>2010-04-28 15:55:14 -0400
commitbece7b2398d073d11b2e352405a3ecd3a1e39c60 (patch)
tree645d3207b985dc7b1b139cfd857ed6de273655ef /net/caif
parent8d545c8f958f5f433c50a00762ce1f231ed56eee (diff)
caif: Rewritten socket implementation
Changes: This is a complete re-write of the socket layer. Making the socket implementation more aligned with the other socket layers and using more of the support functions available in sock.c. Lots of code is copied from af_unix (and some from af_irda). Non-blocking mode should be working as well. Signed-off-by: Sjur Braendeland <sjur.brandeland@stericsson.com> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net/caif')
-rw-r--r--net/caif/caif_socket.c1795
1 files changed, 828 insertions, 967 deletions
diff --git a/net/caif/caif_socket.c b/net/caif/caif_socket.c
index d455375789fb..c3a70c5c893a 100644
--- a/net/caif/caif_socket.c
+++ b/net/caif/caif_socket.c
@@ -1,7 +1,6 @@
1/* 1/*
2 * Copyright (C) ST-Ericsson AB 2010 2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Sjur Brendeland sjur.brandeland@stericsson.com 3 * Author: Sjur Brendeland sjur.brandeland@stericsson.com
4 * Per Sigmond per.sigmond@stericsson.com
5 * License terms: GNU General Public License (GPL) version 2 4 * License terms: GNU General Public License (GPL) version 2
6 */ 5 */
7 6
@@ -16,91 +15,52 @@
16#include <linux/poll.h> 15#include <linux/poll.h>
17#include <linux/tcp.h> 16#include <linux/tcp.h>
18#include <linux/uaccess.h> 17#include <linux/uaccess.h>
19#include <asm/atomic.h> 18#include <linux/mutex.h>
20 19#include <linux/debugfs.h>
21#include <linux/caif/caif_socket.h> 20#include <linux/caif/caif_socket.h>
21#include <asm/atomic.h>
22#include <net/sock.h>
23#include <net/tcp_states.h>
22#include <net/caif/caif_layer.h> 24#include <net/caif/caif_layer.h>
23#include <net/caif/caif_dev.h> 25#include <net/caif/caif_dev.h>
24#include <net/caif/cfpkt.h> 26#include <net/caif/cfpkt.h>
25 27
26MODULE_LICENSE("GPL"); 28MODULE_LICENSE("GPL");
29MODULE_ALIAS_NETPROTO(AF_CAIF);
30
31#define CAIF_DEF_SNDBUF (CAIF_MAX_PAYLOAD_SIZE*10)
32#define CAIF_DEF_RCVBUF (CAIF_MAX_PAYLOAD_SIZE*100)
33
34/*
35 * CAIF state is re-using the TCP socket states.
36 * caif_states stored in sk_state reflect the state as reported by
37 * the CAIF stack, while sk_socket->state is the state of the socket.
38 */
39enum caif_states {
40 CAIF_CONNECTED = TCP_ESTABLISHED,
41 CAIF_CONNECTING = TCP_SYN_SENT,
42 CAIF_DISCONNECTED = TCP_CLOSE
43};
44
45#define TX_FLOW_ON_BIT 1
46#define RX_FLOW_ON_BIT 2
27 47
28#define CHNL_SKT_READ_QUEUE_HIGH 200
29#define CHNL_SKT_READ_QUEUE_LOW 100
30
31static int caif_sockbuf_size = 40000;
32static atomic_t caif_nr_socks = ATOMIC_INIT(0);
33
34#define CONN_STATE_OPEN_BIT 1
35#define CONN_STATE_PENDING_BIT 2
36#define CONN_STATE_PEND_DESTROY_BIT 3
37#define CONN_REMOTE_SHUTDOWN_BIT 4
38
39#define TX_FLOW_ON_BIT 1
40#define RX_FLOW_ON_BIT 2
41
42#define STATE_IS_OPEN(cf_sk) test_bit(CONN_STATE_OPEN_BIT,\
43 (void *) &(cf_sk)->conn_state)
44#define STATE_IS_REMOTE_SHUTDOWN(cf_sk) test_bit(CONN_REMOTE_SHUTDOWN_BIT,\
45 (void *) &(cf_sk)->conn_state)
46#define STATE_IS_PENDING(cf_sk) test_bit(CONN_STATE_PENDING_BIT,\
47 (void *) &(cf_sk)->conn_state)
48#define STATE_IS_PENDING_DESTROY(cf_sk) test_bit(CONN_STATE_PEND_DESTROY_BIT,\
49 (void *) &(cf_sk)->conn_state)
50
51#define SET_STATE_PENDING_DESTROY(cf_sk) set_bit(CONN_STATE_PEND_DESTROY_BIT,\
52 (void *) &(cf_sk)->conn_state)
53#define SET_STATE_OPEN(cf_sk) set_bit(CONN_STATE_OPEN_BIT,\
54 (void *) &(cf_sk)->conn_state)
55#define SET_STATE_CLOSED(cf_sk) clear_bit(CONN_STATE_OPEN_BIT,\
56 (void *) &(cf_sk)->conn_state)
57#define SET_PENDING_ON(cf_sk) set_bit(CONN_STATE_PENDING_BIT,\
58 (void *) &(cf_sk)->conn_state)
59#define SET_PENDING_OFF(cf_sk) clear_bit(CONN_STATE_PENDING_BIT,\
60 (void *) &(cf_sk)->conn_state)
61#define SET_REMOTE_SHUTDOWN(cf_sk) set_bit(CONN_REMOTE_SHUTDOWN_BIT,\
62 (void *) &(cf_sk)->conn_state)
63
64#define SET_REMOTE_SHUTDOWN_OFF(dev) clear_bit(CONN_REMOTE_SHUTDOWN_BIT,\
65 (void *) &(dev)->conn_state)
66#define RX_FLOW_IS_ON(cf_sk) test_bit(RX_FLOW_ON_BIT,\
67 (void *) &(cf_sk)->flow_state)
68#define TX_FLOW_IS_ON(cf_sk) test_bit(TX_FLOW_ON_BIT,\
69 (void *) &(cf_sk)->flow_state)
70
71#define SET_RX_FLOW_OFF(cf_sk) clear_bit(RX_FLOW_ON_BIT,\
72 (void *) &(cf_sk)->flow_state)
73#define SET_RX_FLOW_ON(cf_sk) set_bit(RX_FLOW_ON_BIT,\
74 (void *) &(cf_sk)->flow_state)
75#define SET_TX_FLOW_OFF(cf_sk) clear_bit(TX_FLOW_ON_BIT,\
76 (void *) &(cf_sk)->flow_state)
77#define SET_TX_FLOW_ON(cf_sk) set_bit(TX_FLOW_ON_BIT,\
78 (void *) &(cf_sk)->flow_state)
79
80#define SKT_READ_FLAG 0x01
81#define SKT_WRITE_FLAG 0x02
82static struct dentry *debugfsdir; 48static struct dentry *debugfsdir;
83#include <linux/debugfs.h>
84 49
85#ifdef CONFIG_DEBUG_FS 50#ifdef CONFIG_DEBUG_FS
86struct debug_fs_counter { 51struct debug_fs_counter {
87 atomic_t num_open; 52 atomic_t caif_nr_socks;
88 atomic_t num_close; 53 atomic_t num_connect_req;
89 atomic_t num_init; 54 atomic_t num_connect_resp;
90 atomic_t num_init_resp; 55 atomic_t num_connect_fail_resp;
91 atomic_t num_init_fail_resp; 56 atomic_t num_disconnect;
92 atomic_t num_deinit;
93 atomic_t num_deinit_resp;
94 atomic_t num_remote_shutdown_ind; 57 atomic_t num_remote_shutdown_ind;
95 atomic_t num_tx_flow_off_ind; 58 atomic_t num_tx_flow_off_ind;
96 atomic_t num_tx_flow_on_ind; 59 atomic_t num_tx_flow_on_ind;
97 atomic_t num_rx_flow_off; 60 atomic_t num_rx_flow_off;
98 atomic_t num_rx_flow_on; 61 atomic_t num_rx_flow_on;
99 atomic_t skb_in_use;
100 atomic_t skb_alloc;
101 atomic_t skb_free;
102}; 62};
103static struct debug_fs_counter cnt; 63struct debug_fs_counter cnt;
104#define dbfs_atomic_inc(v) atomic_inc(v) 64#define dbfs_atomic_inc(v) atomic_inc(v)
105#define dbfs_atomic_dec(v) atomic_dec(v) 65#define dbfs_atomic_dec(v) atomic_dec(v)
106#else 66#else
@@ -108,624 +68,666 @@ static struct debug_fs_counter cnt;
108#define dbfs_atomic_dec(v) 68#define dbfs_atomic_dec(v)
109#endif 69#endif
110 70
111/* The AF_CAIF socket */
112struct caifsock { 71struct caifsock {
113 /* NOTE: sk has to be the first member */ 72 struct sock sk; /* must be first member */
114 struct sock sk;
115 struct cflayer layer; 73 struct cflayer layer;
116 char name[CAIF_LAYER_NAME_SZ]; 74 char name[CAIF_LAYER_NAME_SZ]; /* Used for debugging */
117 u32 conn_state;
118 u32 flow_state; 75 u32 flow_state;
119 struct cfpktq *pktq;
120 int file_mode;
121 struct caif_connect_request conn_req; 76 struct caif_connect_request conn_req;
122 int read_queue_len; 77 struct mutex readlock;
123 /* protect updates of read_queue_len */
124 spinlock_t read_queue_len_lock;
125 struct dentry *debugfs_socket_dir; 78 struct dentry *debugfs_socket_dir;
126}; 79};
127 80
128static void drain_queue(struct caifsock *cf_sk); 81static int rx_flow_is_on(struct caifsock *cf_sk)
82{
83 return test_bit(RX_FLOW_ON_BIT,
84 (void *) &cf_sk->flow_state);
85}
86
87static int tx_flow_is_on(struct caifsock *cf_sk)
88{
89 return test_bit(TX_FLOW_ON_BIT,
90 (void *) &cf_sk->flow_state);
91}
129 92
130/* Packet Receive Callback function called from CAIF Stack */ 93static void set_rx_flow_off(struct caifsock *cf_sk)
131static int caif_sktrecv_cb(struct cflayer *layr, struct cfpkt *pkt)
132{ 94{
133 struct caifsock *cf_sk; 95 clear_bit(RX_FLOW_ON_BIT,
134 int read_queue_high; 96 (void *) &cf_sk->flow_state);
135 cf_sk = container_of(layr, struct caifsock, layer); 97}
136 98
137 if (!STATE_IS_OPEN(cf_sk)) { 99static void set_rx_flow_on(struct caifsock *cf_sk)
138 /*FIXME: This should be allowed finally!*/ 100{
139 pr_debug("CAIF: %s(): called after close request\n", __func__); 101 set_bit(RX_FLOW_ON_BIT,
140 cfpkt_destroy(pkt); 102 (void *) &cf_sk->flow_state);
141 return 0; 103}
142 }
143 /* NOTE: This function may be called in Tasklet context! */
144 104
145 /* The queue has its own lock */ 105static void set_tx_flow_off(struct caifsock *cf_sk)
146 cfpkt_queue(cf_sk->pktq, pkt, 0); 106{
107 clear_bit(TX_FLOW_ON_BIT,
108 (void *) &cf_sk->flow_state);
109}
147 110
148 spin_lock(&cf_sk->read_queue_len_lock); 111static void set_tx_flow_on(struct caifsock *cf_sk)
149 cf_sk->read_queue_len++; 112{
113 set_bit(TX_FLOW_ON_BIT,
114 (void *) &cf_sk->flow_state);
115}
150 116
151 read_queue_high = (cf_sk->read_queue_len > CHNL_SKT_READ_QUEUE_HIGH); 117static void caif_read_lock(struct sock *sk)
152 spin_unlock(&cf_sk->read_queue_len_lock); 118{
119 struct caifsock *cf_sk;
120 cf_sk = container_of(sk, struct caifsock, sk);
121 mutex_lock(&cf_sk->readlock);
122}
153 123
154 if (RX_FLOW_IS_ON(cf_sk) && read_queue_high) { 124static void caif_read_unlock(struct sock *sk)
155 dbfs_atomic_inc(&cnt.num_rx_flow_off); 125{
156 SET_RX_FLOW_OFF(cf_sk); 126 struct caifsock *cf_sk;
127 cf_sk = container_of(sk, struct caifsock, sk);
128 mutex_unlock(&cf_sk->readlock);
129}
157 130
158 /* Send flow off (NOTE: must not sleep) */ 131int sk_rcvbuf_lowwater(struct caifsock *cf_sk)
159 pr_debug("CAIF: %s():" 132{
160 " sending flow OFF (queue len = %d)\n", 133 /* A quarter of full buffer is used a low water mark */
161 __func__, 134 return cf_sk->sk.sk_rcvbuf / 4;
162 cf_sk->read_queue_len); 135}
163 caif_assert(cf_sk->layer.dn);
164 caif_assert(cf_sk->layer.dn->ctrlcmd);
165 136
166 (void) cf_sk->layer.dn->modemcmd(cf_sk->layer.dn, 137void caif_flow_ctrl(struct sock *sk, int mode)
167 CAIF_MODEMCMD_FLOW_OFF_REQ); 138{
168 } 139 struct caifsock *cf_sk;
140 cf_sk = container_of(sk, struct caifsock, sk);
141 if (cf_sk->layer.dn)
142 cf_sk->layer.dn->modemcmd(cf_sk->layer.dn, mode);
143}
169 144
170 /* Signal reader that data is available. */ 145/*
146 * Copied from sock.c:sock_queue_rcv_skb(), but changed so packets are
147 * not dropped, but CAIF is sending flow off instead.
148 */
149int caif_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
150{
151 int err;
152 int skb_len;
153 unsigned long flags;
154 struct sk_buff_head *list = &sk->sk_receive_queue;
155 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
171 156
172 wake_up_interruptible(sk_sleep(&cf_sk->sk)); 157 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
158 (unsigned)sk->sk_rcvbuf && rx_flow_is_on(cf_sk)) {
159 trace_printk("CAIF: %s():"
160 " sending flow OFF (queue len = %d %d)\n",
161 __func__,
162 atomic_read(&cf_sk->sk.sk_rmem_alloc),
163 sk_rcvbuf_lowwater(cf_sk));
164 set_rx_flow_off(cf_sk);
165 if (cf_sk->layer.dn)
166 cf_sk->layer.dn->modemcmd(cf_sk->layer.dn,
167 CAIF_MODEMCMD_FLOW_OFF_REQ);
168 }
173 169
170 err = sk_filter(sk, skb);
171 if (err)
172 return err;
173 if (!sk_rmem_schedule(sk, skb->truesize) && rx_flow_is_on(cf_sk)) {
174 set_rx_flow_off(cf_sk);
175 trace_printk("CAIF: %s():"
176 " sending flow OFF due to rmem_schedule\n",
177 __func__);
178 if (cf_sk->layer.dn)
179 cf_sk->layer.dn->modemcmd(cf_sk->layer.dn,
180 CAIF_MODEMCMD_FLOW_OFF_REQ);
181 }
182 skb->dev = NULL;
183 skb_set_owner_r(skb, sk);
184 /* Cache the SKB length before we tack it onto the receive
185 * queue. Once it is added it no longer belongs to us and
186 * may be freed by other threads of control pulling packets
187 * from the queue.
188 */
189 skb_len = skb->len;
190 spin_lock_irqsave(&list->lock, flags);
191 if (!sock_flag(sk, SOCK_DEAD))
192 __skb_queue_tail(list, skb);
193 spin_unlock_irqrestore(&list->lock, flags);
194
195 if (!sock_flag(sk, SOCK_DEAD))
196 sk->sk_data_ready(sk, skb_len);
197 else
198 kfree_skb(skb);
174 return 0; 199 return 0;
175} 200}
176 201
177/* Packet Flow Control Callback function called from CAIF */ 202/* Packet Receive Callback function called from CAIF Stack */
178static void caif_sktflowctrl_cb(struct cflayer *layr, 203static int caif_sktrecv_cb(struct cflayer *layr, struct cfpkt *pkt)
179 enum caif_ctrlcmd flow,
180 int phyid)
181{ 204{
182 struct caifsock *cf_sk; 205 struct caifsock *cf_sk;
183 206 struct sk_buff *skb;
184 /* NOTE: This function may be called in Tasklet context! */
185 pr_debug("CAIF: %s(): flowctrl func called: %s.\n",
186 __func__,
187 flow == CAIF_CTRLCMD_FLOW_ON_IND ? "ON" :
188 flow == CAIF_CTRLCMD_FLOW_OFF_IND ? "OFF" :
189 flow == CAIF_CTRLCMD_INIT_RSP ? "INIT_RSP" :
190 flow == CAIF_CTRLCMD_DEINIT_RSP ? "DEINIT_RSP" :
191 flow == CAIF_CTRLCMD_INIT_FAIL_RSP ? "INIT_FAIL_RSP" :
192 flow ==
193 CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND ? "REMOTE_SHUTDOWN" :
194 "UKNOWN CTRL COMMAND");
195
196 if (layr == NULL)
197 return;
198 207
199 cf_sk = container_of(layr, struct caifsock, layer); 208 cf_sk = container_of(layr, struct caifsock, layer);
209 skb = cfpkt_tonative(pkt);
210
211 if (unlikely(cf_sk->sk.sk_state != CAIF_CONNECTED)) {
212 cfpkt_destroy(pkt);
213 return 0;
214 }
215 caif_queue_rcv_skb(&cf_sk->sk, skb);
216 return 0;
217}
200 218
219/* Packet Control Callback function called from CAIF */
220static void caif_ctrl_cb(struct cflayer *layr,
221 enum caif_ctrlcmd flow,
222 int phyid)
223{
224 struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
201 switch (flow) { 225 switch (flow) {
202 case CAIF_CTRLCMD_FLOW_ON_IND: 226 case CAIF_CTRLCMD_FLOW_ON_IND:
227 /* OK from modem to start sending again */
203 dbfs_atomic_inc(&cnt.num_tx_flow_on_ind); 228 dbfs_atomic_inc(&cnt.num_tx_flow_on_ind);
204 /* Signal reader that data is available. */ 229 set_tx_flow_on(cf_sk);
205 SET_TX_FLOW_ON(cf_sk); 230 cf_sk->sk.sk_state_change(&cf_sk->sk);
206 wake_up_interruptible(sk_sleep(&cf_sk->sk));
207 break; 231 break;
208 232
209 case CAIF_CTRLCMD_FLOW_OFF_IND: 233 case CAIF_CTRLCMD_FLOW_OFF_IND:
234 /* Modem asks us to shut up */
210 dbfs_atomic_inc(&cnt.num_tx_flow_off_ind); 235 dbfs_atomic_inc(&cnt.num_tx_flow_off_ind);
211 SET_TX_FLOW_OFF(cf_sk); 236 set_tx_flow_off(cf_sk);
237 cf_sk->sk.sk_state_change(&cf_sk->sk);
212 break; 238 break;
213 239
214 case CAIF_CTRLCMD_INIT_RSP: 240 case CAIF_CTRLCMD_INIT_RSP:
215 dbfs_atomic_inc(&cnt.num_init_resp); 241 /* We're now connected */
216 /* Signal reader that data is available. */ 242 dbfs_atomic_inc(&cnt.num_connect_resp);
217 caif_assert(STATE_IS_OPEN(cf_sk)); 243 cf_sk->sk.sk_state = CAIF_CONNECTED;
218 SET_PENDING_OFF(cf_sk); 244 set_tx_flow_on(cf_sk);
219 SET_TX_FLOW_ON(cf_sk); 245 cf_sk->sk.sk_state_change(&cf_sk->sk);
220 wake_up_interruptible(sk_sleep(&cf_sk->sk));
221 break; 246 break;
222 247
223 case CAIF_CTRLCMD_DEINIT_RSP: 248 case CAIF_CTRLCMD_DEINIT_RSP:
224 dbfs_atomic_inc(&cnt.num_deinit_resp); 249 /* We're now disconnected */
225 caif_assert(!STATE_IS_OPEN(cf_sk)); 250 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
226 SET_PENDING_OFF(cf_sk); 251 cf_sk->sk.sk_state_change(&cf_sk->sk);
227 if (!STATE_IS_PENDING_DESTROY(cf_sk)) { 252 cfcnfg_release_adap_layer(&cf_sk->layer);
228 if (sk_sleep(&cf_sk->sk) != NULL)
229 wake_up_interruptible(sk_sleep(&cf_sk->sk));
230 }
231 dbfs_atomic_inc(&cnt.num_deinit);
232 sock_put(&cf_sk->sk);
233 break; 253 break;
234 254
235 case CAIF_CTRLCMD_INIT_FAIL_RSP: 255 case CAIF_CTRLCMD_INIT_FAIL_RSP:
236 dbfs_atomic_inc(&cnt.num_init_fail_resp); 256 /* Connect request failed */
237 caif_assert(STATE_IS_OPEN(cf_sk)); 257 dbfs_atomic_inc(&cnt.num_connect_fail_resp);
238 SET_STATE_CLOSED(cf_sk); 258 cf_sk->sk.sk_err = ECONNREFUSED;
239 SET_PENDING_OFF(cf_sk); 259 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
240 SET_TX_FLOW_OFF(cf_sk); 260 cf_sk->sk.sk_shutdown = SHUTDOWN_MASK;
241 wake_up_interruptible(sk_sleep(&cf_sk->sk)); 261 /*
262 * Socket "standards" seems to require POLLOUT to
263 * be set at connect failure.
264 */
265 set_tx_flow_on(cf_sk);
266 cf_sk->sk.sk_state_change(&cf_sk->sk);
242 break; 267 break;
243 268
244 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND: 269 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND:
270 /* Modem has closed this connection, or device is down. */
245 dbfs_atomic_inc(&cnt.num_remote_shutdown_ind); 271 dbfs_atomic_inc(&cnt.num_remote_shutdown_ind);
246 SET_REMOTE_SHUTDOWN(cf_sk); 272 cf_sk->sk.sk_shutdown = SHUTDOWN_MASK;
247 /* Use sk_shutdown to indicate remote shutdown indication */ 273 cf_sk->sk.sk_err = ECONNRESET;
248 cf_sk->sk.sk_shutdown |= RCV_SHUTDOWN; 274 set_rx_flow_on(cf_sk);
249 cf_sk->file_mode = 0; 275 cf_sk->sk.sk_error_report(&cf_sk->sk);
250 wake_up_interruptible(sk_sleep(&cf_sk->sk));
251 break; 276 break;
252 277
253 default: 278 default:
254 pr_debug("CAIF: %s(): Unexpected flow command %d\n", 279 pr_debug("CAIF: %s(): Unexpected flow command %d\n",
255 __func__, flow); 280 __func__, flow);
256 } 281 }
257} 282}
258 283
259static void skb_destructor(struct sk_buff *skb) 284static void caif_check_flow_release(struct sock *sk)
260{ 285{
261 dbfs_atomic_inc(&cnt.skb_free); 286 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
262 dbfs_atomic_dec(&cnt.skb_in_use);
263}
264 287
288 if (cf_sk->layer.dn == NULL || cf_sk->layer.dn->modemcmd == NULL)
289 return;
290 if (rx_flow_is_on(cf_sk))
291 return;
265 292
266static int caif_recvmsg(struct kiocb *iocb, struct socket *sock, 293 if (atomic_read(&sk->sk_rmem_alloc) <= sk_rcvbuf_lowwater(cf_sk)) {
294 dbfs_atomic_inc(&cnt.num_rx_flow_on);
295 set_rx_flow_on(cf_sk);
296 cf_sk->layer.dn->modemcmd(cf_sk->layer.dn,
297 CAIF_MODEMCMD_FLOW_ON_REQ);
298 }
299}
300/*
301 * Copied from sock.c:sock_queue_rcv_skb(), and added check that user buffer
302 * has sufficient size.
303 */
304
305static int caif_seqpkt_recvmsg(struct kiocb *iocb, struct socket *sock,
267 struct msghdr *m, size_t buf_len, int flags) 306 struct msghdr *m, size_t buf_len, int flags)
268 307
269{ 308{
270 struct sock *sk = sock->sk; 309 struct sock *sk = sock->sk;
271 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
272 struct cfpkt *pkt = NULL;
273 size_t len;
274 int result;
275 struct sk_buff *skb; 310 struct sk_buff *skb;
276 ssize_t ret = -EIO; 311 int ret = 0;
277 int read_queue_low; 312 int len;
278
279 if (cf_sk == NULL) {
280 pr_debug("CAIF: %s(): private_data not set!\n",
281 __func__);
282 ret = -EBADFD;
283 goto read_error;
284 }
285
286 /* Don't do multiple iovec entries yet */
287 if (m->msg_iovlen != 1)
288 return -EOPNOTSUPP;
289 313
290 if (unlikely(!buf_len)) 314 if (unlikely(!buf_len))
291 return -EINVAL; 315 return -EINVAL;
292 316
293 lock_sock(&(cf_sk->sk)); 317 skb = skb_recv_datagram(sk, flags, 0 , &ret);
294 318 if (!skb)
295 caif_assert(cf_sk->pktq);
296
297 if (!STATE_IS_OPEN(cf_sk)) {
298 /* Socket is closed or closing. */
299 if (!STATE_IS_PENDING(cf_sk)) {
300 pr_debug("CAIF: %s(): socket is closed (by remote)\n",
301 __func__);
302 ret = -EPIPE;
303 } else {
304 pr_debug("CAIF: %s(): socket is closing..\n", __func__);
305 ret = -EBADF;
306 }
307 goto read_error; 319 goto read_error;
308 }
309 /* Socket is open or opening. */
310 if (STATE_IS_PENDING(cf_sk)) {
311 pr_debug("CAIF: %s(): socket is opening...\n", __func__);
312
313 if (flags & MSG_DONTWAIT) {
314 /* We can't block. */
315 pr_debug("CAIF: %s():state pending and MSG_DONTWAIT\n",
316 __func__);
317 ret = -EAGAIN;
318 goto read_error;
319 }
320 320
321 len = skb->len;
322
323 if (skb && skb->len > buf_len && !(flags & MSG_PEEK)) {
324 len = buf_len;
321 /* 325 /*
322 * Blocking mode; state is pending and we need to wait 326 * Push skb back on receive queue if buffer too small.
323 * for its conclusion. 327 * This has a built-in race where multi-threaded receive
328 * may get packet in wrong order, but multiple read does
329 * not really guarantee ordered delivery anyway.
330 * Let's optimize for speed without taking locks.
324 */ 331 */
325 release_sock(&cf_sk->sk);
326
327 result =
328 wait_event_interruptible(*sk_sleep(&cf_sk->sk),
329 !STATE_IS_PENDING(cf_sk));
330 332
331 lock_sock(&(cf_sk->sk)); 333 skb_queue_head(&sk->sk_receive_queue, skb);
332 334 ret = -EMSGSIZE;
333 if (result == -ERESTARTSYS) { 335 goto read_error;
334 pr_debug("CAIF: %s(): wait_event_interruptible"
335 " woken by a signal (1)", __func__);
336 ret = -ERESTARTSYS;
337 goto read_error;
338 }
339 } 336 }
340 337
341 if (STATE_IS_REMOTE_SHUTDOWN(cf_sk) || 338 ret = skb_copy_datagram_iovec(skb, 0, m->msg_iov, len);
342 !STATE_IS_OPEN(cf_sk) || 339 if (ret)
343 STATE_IS_PENDING(cf_sk)) {
344
345 pr_debug("CAIF: %s(): socket closed\n",
346 __func__);
347 ret = -ESHUTDOWN;
348 goto read_error; 340 goto read_error;
349 }
350 341
351 /* 342 skb_free_datagram(sk, skb);
352 * Block if we don't have any received buffers.
353 * The queue has its own lock.
354 */
355 while ((pkt = cfpkt_qpeek(cf_sk->pktq)) == NULL) {
356 343
357 if (flags & MSG_DONTWAIT) { 344 caif_check_flow_release(sk);
358 pr_debug("CAIF: %s(): MSG_DONTWAIT\n", __func__);
359 ret = -EAGAIN;
360 goto read_error;
361 }
362 trace_printk("CAIF: %s() wait_event\n", __func__);
363 345
364 /* Let writers in. */ 346 return len;
365 release_sock(&cf_sk->sk);
366 347
367 /* Block reader until data arrives or socket is closed. */ 348read_error:
368 if (wait_event_interruptible(*sk_sleep(&cf_sk->sk), 349 return ret;
369 cfpkt_qpeek(cf_sk->pktq) 350}
370 || STATE_IS_REMOTE_SHUTDOWN(cf_sk)
371 || !STATE_IS_OPEN(cf_sk)) ==
372 -ERESTARTSYS) {
373 pr_debug("CAIF: %s():"
374 " wait_event_interruptible woken by "
375 "a signal, signal_pending(current) = %d\n",
376 __func__,
377 signal_pending(current));
378 return -ERESTARTSYS;
379 }
380 351
381 trace_printk("CAIF: %s() awake\n", __func__);
382 if (STATE_IS_REMOTE_SHUTDOWN(cf_sk)) {
383 pr_debug("CAIF: %s(): "
384 "received remote_shutdown indication\n",
385 __func__);
386 ret = -ESHUTDOWN;
387 goto read_error_no_unlock;
388 }
389 352
390 /* I want to be alone on cf_sk (except status and queue). */ 353/* Copied from unix_stream_wait_data, identical except for lock call. */
391 lock_sock(&(cf_sk->sk)); 354static long caif_stream_data_wait(struct sock *sk, long timeo)
355{
356 DEFINE_WAIT(wait);
357 lock_sock(sk);
358
359 for (;;) {
360 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
361
362 if (!skb_queue_empty(&sk->sk_receive_queue) ||
363 sk->sk_err ||
364 sk->sk_state != CAIF_CONNECTED ||
365 sock_flag(sk, SOCK_DEAD) ||
366 (sk->sk_shutdown & RCV_SHUTDOWN) ||
367 signal_pending(current) ||
368 !timeo)
369 break;
392 370
393 if (!STATE_IS_OPEN(cf_sk)) { 371 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
394 /* Someone closed the link, report error. */ 372 release_sock(sk);
395 pr_debug("CAIF: %s(): remote end shutdown!\n", 373 timeo = schedule_timeout(timeo);
396 __func__); 374 lock_sock(sk);
397 ret = -EPIPE; 375 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
398 goto read_error;
399 }
400 } 376 }
401 377
402 /* The queue has its own lock. */ 378 finish_wait(sk_sleep(sk), &wait);
403 len = cfpkt_getlen(pkt); 379 release_sock(sk);
404 380 return timeo;
405 /* Check max length that can be copied. */ 381}
406 if (len <= buf_len)
407 pkt = cfpkt_dequeue(cf_sk->pktq);
408 else {
409 pr_debug("CAIF: %s(): user buffer too small (%ld,%ld)\n",
410 __func__, (long) len, (long) buf_len);
411 if (sock->type == SOCK_SEQPACKET) {
412 ret = -EMSGSIZE;
413 goto read_error;
414 }
415 len = buf_len;
416 }
417 382
418 383
419 spin_lock(&cf_sk->read_queue_len_lock); 384/*
420 cf_sk->read_queue_len--; 385 * Copied from unix_stream_recvmsg, but removed credit checks,
421 read_queue_low = (cf_sk->read_queue_len < CHNL_SKT_READ_QUEUE_LOW); 386 * changed locking calls, changed address handling.
422 spin_unlock(&cf_sk->read_queue_len_lock); 387 */
388static int caif_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
389 struct msghdr *msg, size_t size,
390 int flags)
391{
392 struct sock *sk = sock->sk;
393 int copied = 0;
394 int target;
395 int err = 0;
396 long timeo;
423 397
424 if (!RX_FLOW_IS_ON(cf_sk) && read_queue_low) { 398 err = -EOPNOTSUPP;
425 dbfs_atomic_inc(&cnt.num_rx_flow_on); 399 if (flags&MSG_OOB)
426 SET_RX_FLOW_ON(cf_sk); 400 goto out;
427 401
428 /* Send flow on. */ 402 msg->msg_namelen = 0;
429 pr_debug("CAIF: %s(): sending flow ON (queue len = %d)\n",
430 __func__, cf_sk->read_queue_len);
431 caif_assert(cf_sk->layer.dn);
432 caif_assert(cf_sk->layer.dn->ctrlcmd);
433 (void) cf_sk->layer.dn->modemcmd(cf_sk->layer.dn,
434 CAIF_MODEMCMD_FLOW_ON_REQ);
435 403
436 caif_assert(cf_sk->read_queue_len >= 0); 404 /*
437 } 405 * Lock the socket to prevent queue disordering
406 * while sleeps in memcpy_tomsg
407 */
408 err = -EAGAIN;
409 if (sk->sk_state == CAIF_CONNECTING)
410 goto out;
438 411
439 skb = cfpkt_tonative(pkt); 412 caif_read_lock(sk);
440 result = skb_copy_datagram_iovec(skb, 0, m->msg_iov, len); 413 target = sock_rcvlowat(sk, flags&MSG_WAITALL, size);
441 skb_pull(skb, len); 414 timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
442 415
443 if (result) { 416 do {
444 pr_debug("CAIF: %s(): copy to_iovec failed\n", __func__); 417 int chunk;
445 cfpkt_destroy(pkt); 418 struct sk_buff *skb;
446 ret = -EFAULT;
447 goto read_error;
448 }
449 419
450 /* Free packet and remove from queue */ 420 lock_sock(sk);
451 if (skb->len == 0) 421 skb = skb_dequeue(&sk->sk_receive_queue);
452 skb_free_datagram(sk, skb); 422 caif_check_flow_release(sk);
453 423
454 /* Let the others in. */ 424 if (skb == NULL) {
455 release_sock(&cf_sk->sk); 425 if (copied >= target)
456 return len; 426 goto unlock;
427 /*
428 * POSIX 1003.1g mandates this order.
429 */
430 err = sock_error(sk);
431 if (err)
432 goto unlock;
433 err = -ECONNRESET;
434 if (sk->sk_shutdown & RCV_SHUTDOWN)
435 goto unlock;
457 436
458read_error: 437 err = -EPIPE;
459 release_sock(&cf_sk->sk); 438 if (sk->sk_state != CAIF_CONNECTED)
460read_error_no_unlock: 439 goto unlock;
461 return ret; 440 if (sock_flag(sk, SOCK_DEAD))
462} 441 goto unlock;
463 442
464/* Send a signal as a consequence of sendmsg, sendto or caif_sendmsg. */ 443 release_sock(sk);
465static int caif_sendmsg(struct kiocb *kiocb, struct socket *sock,
466 struct msghdr *msg, size_t len)
467{
468 444
469 struct sock *sk = sock->sk; 445 err = -EAGAIN;
470 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk); 446 if (!timeo)
471 size_t payload_size = msg->msg_iov->iov_len; 447 break;
472 struct cfpkt *pkt = NULL;
473 struct caif_payload_info info;
474 unsigned char *txbuf;
475 ssize_t ret = -EIO;
476 int result;
477 struct sk_buff *skb;
478 caif_assert(msg->msg_iovlen == 1);
479 448
480 if (cf_sk == NULL) { 449 caif_read_unlock(sk);
481 pr_debug("CAIF: %s(): private_data not set!\n",
482 __func__);
483 ret = -EBADFD;
484 goto write_error_no_unlock;
485 }
486 450
487 if (unlikely(msg->msg_iov->iov_base == NULL)) { 451 timeo = caif_stream_data_wait(sk, timeo);
488 pr_warning("CAIF: %s(): Buffer is NULL.\n", __func__);
489 ret = -EINVAL;
490 goto write_error_no_unlock;
491 }
492 452
493 if (payload_size > CAIF_MAX_PAYLOAD_SIZE) { 453 if (signal_pending(current)) {
494 pr_debug("CAIF: %s(): buffer too long\n", __func__); 454 err = sock_intr_errno(timeo);
495 if (sock->type == SOCK_SEQPACKET) { 455 goto out;
496 ret = -EINVAL; 456 }
497 goto write_error_no_unlock; 457 caif_read_lock(sk);
458 continue;
459unlock:
460 release_sock(sk);
461 break;
498 } 462 }
499 payload_size = CAIF_MAX_PAYLOAD_SIZE; 463 release_sock(sk);
500 } 464 chunk = min_t(unsigned int, skb->len, size);
465 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
466 skb_queue_head(&sk->sk_receive_queue, skb);
467 if (copied == 0)
468 copied = -EFAULT;
469 break;
470 }
471 copied += chunk;
472 size -= chunk;
501 473
502 /* I want to be alone on cf_sk (except status and queue) */ 474 /* Mark read part of skb as used */
503 lock_sock(&(cf_sk->sk)); 475 if (!(flags & MSG_PEEK)) {
476 skb_pull(skb, chunk);
504 477
505 caif_assert(cf_sk->pktq); 478 /* put the skb back if we didn't use it up. */
479 if (skb->len) {
480 skb_queue_head(&sk->sk_receive_queue, skb);
481 break;
482 }
483 kfree_skb(skb);
506 484
507 if (!STATE_IS_OPEN(cf_sk)) {
508 /* Socket is closed or closing */
509 if (!STATE_IS_PENDING(cf_sk)) {
510 pr_debug("CAIF: %s(): socket is closed (by remote)\n",
511 __func__);
512 ret = -EPIPE;
513 } else { 485 } else {
514 pr_debug("CAIF: %s(): socket is closing...\n", 486 /*
515 __func__); 487 * It is questionable, see note in unix_dgram_recvmsg.
516 ret = -EBADF; 488 */
517 } 489 /* put message back and return */
518 goto write_error; 490 skb_queue_head(&sk->sk_receive_queue, skb);
519 } 491 break;
520
521 /* Socket is open or opening */
522 if (STATE_IS_PENDING(cf_sk)) {
523 pr_debug("CAIF: %s(): socket is opening...\n", __func__);
524
525 if (msg->msg_flags & MSG_DONTWAIT) {
526 /* We can't block */
527 trace_printk("CAIF: %s():state pending:"
528 "state=MSG_DONTWAIT\n", __func__);
529 ret = -EAGAIN;
530 goto write_error;
531 } 492 }
532 /* Let readers in */ 493 } while (size);
533 release_sock(&cf_sk->sk); 494 caif_read_unlock(sk);
534
535 /*
536 * Blocking mode; state is pending and we need to wait
537 * for its conclusion.
538 */
539 result =
540 wait_event_interruptible(*sk_sleep(&cf_sk->sk),
541 !STATE_IS_PENDING(cf_sk));
542 /* I want to be alone on cf_sk (except status and queue) */
543 lock_sock(&(cf_sk->sk));
544 495
545 if (result == -ERESTARTSYS) { 496out:
546 pr_debug("CAIF: %s(): wait_event_interruptible" 497 return copied ? : err;
547 " woken by a signal (1)", __func__); 498}
548 ret = -ERESTARTSYS;
549 goto write_error;
550 }
551 }
552 if (STATE_IS_REMOTE_SHUTDOWN(cf_sk) ||
553 !STATE_IS_OPEN(cf_sk) ||
554 STATE_IS_PENDING(cf_sk)) {
555 499
556 pr_debug("CAIF: %s(): socket closed\n", 500/*
557 __func__); 501 * Copied from sock.c:sock_wait_for_wmem, but change to wait for
558 ret = -ESHUTDOWN; 502 * CAIF flow-on and sock_writable.
559 goto write_error; 503 */
504static long caif_wait_for_flow_on(struct caifsock *cf_sk,
505 int wait_writeable, long timeo, int *err)
506{
507 struct sock *sk = &cf_sk->sk;
508 DEFINE_WAIT(wait);
509 for (;;) {
510 *err = 0;
511 if (tx_flow_is_on(cf_sk) &&
512 (!wait_writeable || sock_writeable(&cf_sk->sk)))
513 break;
514 *err = -ETIMEDOUT;
515 if (!timeo)
516 break;
517 *err = -ERESTARTSYS;
518 if (signal_pending(current))
519 break;
520 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
521 *err = -ECONNRESET;
522 if (sk->sk_shutdown & SHUTDOWN_MASK)
523 break;
524 *err = -sk->sk_err;
525 if (sk->sk_err)
526 break;
527 *err = -EPIPE;
528 if (cf_sk->sk.sk_state != CAIF_CONNECTED)
529 break;
530 timeo = schedule_timeout(timeo);
560 } 531 }
532 finish_wait(sk_sleep(sk), &wait);
533 return timeo;
534}
561 535
562 if (!TX_FLOW_IS_ON(cf_sk)) { 536/*
537 * Transmit a SKB. The device may temporarily request re-transmission
538 * by returning EAGAIN.
539 */
540static int transmit_skb(struct sk_buff *skb, struct caifsock *cf_sk,
541 int noblock, long timeo)
542{
543 struct cfpkt *pkt;
544 int ret, loopcnt = 0;
563 545
564 /* Flow is off. Check non-block flag */ 546 pkt = cfpkt_fromnative(CAIF_DIR_OUT, skb);
565 if (msg->msg_flags & MSG_DONTWAIT) { 547 memset(cfpkt_info(pkt), 0, sizeof(struct caif_payload_info));
566 trace_printk("CAIF: %s(): MSG_DONTWAIT and tx flow off", 548 do {
567 __func__);
568 ret = -EAGAIN;
569 goto write_error;
570 }
571 549
572 /* release lock before waiting */ 550 ret = -ETIMEDOUT;
573 release_sock(&cf_sk->sk);
574 551
575 /* Wait until flow is on or socket is closed */ 552 /* Slight paranoia, probably not needed. */
576 if (wait_event_interruptible(*sk_sleep(&cf_sk->sk), 553 if (unlikely(loopcnt++ > 1000)) {
577 TX_FLOW_IS_ON(cf_sk) 554 pr_warning("CAIF: %s(): transmit retries failed,"
578 || !STATE_IS_OPEN(cf_sk) 555 " error = %d\n", __func__, ret);
579 || STATE_IS_REMOTE_SHUTDOWN(cf_sk) 556 break;
580 ) == -ERESTARTSYS) {
581 pr_debug("CAIF: %s():"
582 " wait_event_interruptible woken by a signal",
583 __func__);
584 ret = -ERESTARTSYS;
585 goto write_error_no_unlock;
586 } 557 }
587 558
588 /* I want to be alone on cf_sk (except status and queue) */ 559 if (cf_sk->layer.dn != NULL)
589 lock_sock(&(cf_sk->sk)); 560 ret = cf_sk->layer.dn->transmit(cf_sk->layer.dn, pkt);
590 561 if (likely(ret >= 0))
591 if (!STATE_IS_OPEN(cf_sk)) { 562 break;
592 /* someone closed the link, report error */ 563 /* if transmit return -EAGAIN, then retry */
593 pr_debug("CAIF: %s(): remote end shutdown!\n", 564 if (noblock && ret == -EAGAIN)
594 __func__); 565 break;
595 ret = -EPIPE; 566 timeo = caif_wait_for_flow_on(cf_sk, 0, timeo, &ret);
596 goto write_error; 567 if (signal_pending(current)) {
568 ret = sock_intr_errno(timeo);
569 break;
597 } 570 }
598 571 if (ret)
599 if (STATE_IS_REMOTE_SHUTDOWN(cf_sk)) { 572 break;
600 pr_debug("CAIF: %s(): " 573 if (cf_sk->sk.sk_state != CAIF_CONNECTED ||
601 "received remote_shutdown indication\n", 574 sock_flag(&cf_sk->sk, SOCK_DEAD) ||
602 __func__); 575 (cf_sk->sk.sk_shutdown & RCV_SHUTDOWN)) {
603 ret = -ESHUTDOWN; 576 ret = -EPIPE;
604 goto write_error; 577 cf_sk->sk.sk_err = EPIPE;
578 break;
605 } 579 }
606 } 580 } while (ret == -EAGAIN);
581 return ret;
582}
607 583
608 pkt = cfpkt_create(payload_size); 584/* Copied from af_unix:unix_dgram_sendmsg, and adapted to CAIF */
609 skb = (struct sk_buff *)pkt; 585static int caif_seqpkt_sendmsg(struct kiocb *kiocb, struct socket *sock,
610 skb->destructor = skb_destructor; 586 struct msghdr *msg, size_t len)
611 skb->sk = sk; 587{
612 dbfs_atomic_inc(&cnt.skb_alloc); 588 struct sock *sk = sock->sk;
613 dbfs_atomic_inc(&cnt.skb_in_use); 589 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
614 if (cfpkt_raw_append(pkt, (void **) &txbuf, payload_size) < 0) { 590 int buffer_size;
615 pr_debug("CAIF: %s(): cfpkt_raw_append failed\n", __func__); 591 int ret = 0;
616 cfpkt_destroy(pkt); 592 struct sk_buff *skb = NULL;
617 ret = -EINVAL; 593 int noblock;
618 goto write_error; 594 long timeo;
619 } 595 caif_assert(cf_sk);
596 ret = sock_error(sk);
597 if (ret)
598 goto err;
599
600 ret = -EOPNOTSUPP;
601 if (msg->msg_flags&MSG_OOB)
602 goto err;
603
604 ret = -EOPNOTSUPP;
605 if (msg->msg_namelen)
606 goto err;
607
608 ret = -EINVAL;
609 if (unlikely(msg->msg_iov->iov_base == NULL))
610 goto err;
611 noblock = msg->msg_flags & MSG_DONTWAIT;
612
613 buffer_size = len + CAIF_NEEDED_HEADROOM + CAIF_NEEDED_TAILROOM;
614
615 ret = -EMSGSIZE;
616 if (buffer_size > CAIF_MAX_PAYLOAD_SIZE)
617 goto err;
618
619 timeo = sock_sndtimeo(sk, noblock);
620 timeo = caif_wait_for_flow_on(container_of(sk, struct caifsock, sk),
621 1, timeo, &ret);
622
623 ret = -EPIPE;
624 if (cf_sk->sk.sk_state != CAIF_CONNECTED ||
625 sock_flag(sk, SOCK_DEAD) ||
626 (sk->sk_shutdown & RCV_SHUTDOWN))
627 goto err;
628
629 ret = -ENOMEM;
630 skb = sock_alloc_send_skb(sk, buffer_size, noblock, &ret);
631 if (!skb)
632 goto err;
633 skb_reserve(skb, CAIF_NEEDED_HEADROOM);
634
635 ret = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
636
637 if (ret)
638 goto err;
639 ret = transmit_skb(skb, cf_sk, noblock, timeo);
640 if (ret < 0)
641 goto err;
642 return len;
643err:
644 kfree_skb(skb);
645 return ret;
646}
620 647
621 /* Copy data into buffer. */ 648/*
622 if (copy_from_user(txbuf, msg->msg_iov->iov_base, payload_size)) { 649 * Copied from unix_stream_sendmsg and adapted to CAIF:
623 pr_debug("CAIF: %s(): copy_from_user returned non zero.\n", 650 * Changed removed permission handling and added waiting for flow on
624 __func__); 651 * and other minor adaptations.
625 cfpkt_destroy(pkt); 652 */
626 ret = -EINVAL; 653static int caif_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
627 goto write_error; 654 struct msghdr *msg, size_t len)
628 } 655{
629 memset(&info, 0, sizeof(info)); 656 struct sock *sk = sock->sk;
657 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
658 int err, size;
659 struct sk_buff *skb;
660 int sent = 0;
661 long timeo;
630 662
631 /* Send the packet down the stack. */ 663 err = -EOPNOTSUPP;
632 caif_assert(cf_sk->layer.dn);
633 caif_assert(cf_sk->layer.dn->transmit);
634 664
635 do { 665 if (unlikely(msg->msg_flags&MSG_OOB))
636 ret = cf_sk->layer.dn->transmit(cf_sk->layer.dn, pkt); 666 goto out_err;
637 667
638 if (likely((ret >= 0) || (ret != -EAGAIN))) 668 if (unlikely(msg->msg_namelen))
639 break; 669 goto out_err;
640 670
641 /* EAGAIN - retry */ 671 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
642 if (msg->msg_flags & MSG_DONTWAIT) { 672 timeo = caif_wait_for_flow_on(cf_sk, 1, timeo, &err);
643 pr_debug("CAIF: %s(): NONBLOCK and transmit failed,"
644 " error = %ld\n", __func__, (long) ret);
645 ret = -EAGAIN;
646 goto write_error;
647 }
648 673
649 /* Let readers in */ 674 if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN))
650 release_sock(&cf_sk->sk); 675 goto pipe_err;
651 676
652 /* Wait until flow is on or socket is closed */ 677 while (sent < len) {
653 if (wait_event_interruptible(*sk_sleep(&cf_sk->sk),
654 TX_FLOW_IS_ON(cf_sk)
655 || !STATE_IS_OPEN(cf_sk)
656 || STATE_IS_REMOTE_SHUTDOWN(cf_sk)
657 ) == -ERESTARTSYS) {
658 pr_debug("CAIF: %s(): wait_event_interruptible"
659 " woken by a signal", __func__);
660 ret = -ERESTARTSYS;
661 goto write_error_no_unlock;
662 }
663 678
664 /* I want to be alone on cf_sk (except status and queue) */ 679 size = len-sent;
665 lock_sock(&(cf_sk->sk));
666 680
667 } while (ret == -EAGAIN); 681 if (size > CAIF_MAX_PAYLOAD_SIZE)
682 size = CAIF_MAX_PAYLOAD_SIZE;
668 683
669 if (ret < 0) { 684 /* If size is more than half of sndbuf, chop up message */
670 cfpkt_destroy(pkt); 685 if (size > ((sk->sk_sndbuf >> 1) - 64))
671 pr_debug("CAIF: %s(): transmit failed, error = %ld\n", 686 size = (sk->sk_sndbuf >> 1) - 64;
672 __func__, (long) ret);
673 687
674 goto write_error; 688 if (size > SKB_MAX_ALLOC)
675 } 689 size = SKB_MAX_ALLOC;
676 690
677 release_sock(&cf_sk->sk); 691 skb = sock_alloc_send_skb(sk,
678 return payload_size; 692 size + CAIF_NEEDED_HEADROOM
693 + CAIF_NEEDED_TAILROOM,
694 msg->msg_flags&MSG_DONTWAIT,
695 &err);
696 if (skb == NULL)
697 goto out_err;
679 698
680write_error: 699 skb_reserve(skb, CAIF_NEEDED_HEADROOM);
681 release_sock(&cf_sk->sk); 700 /*
682write_error_no_unlock: 701 * If you pass two values to the sock_alloc_send_skb
683 return ret; 702 * it tries to grab the large buffer with GFP_NOFS
684} 703 * (which can fail easily), and if it fails grab the
704 * fallback size buffer which is under a page and will
705 * succeed. [Alan]
706 */
707 size = min_t(int, size, skb_tailroom(skb));
685 708
686static unsigned int caif_poll(struct file *file, struct socket *sock, 709 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
687 poll_table *wait) 710 if (err) {
688{ 711 kfree_skb(skb);
689 struct sock *sk = sock->sk; 712 goto out_err;
690 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk); 713 }
691 u32 mask = 0; 714 err = transmit_skb(skb, cf_sk,
692 poll_wait(file, sk_sleep(sk), wait); 715 msg->msg_flags&MSG_DONTWAIT, timeo);
693 lock_sock(&(cf_sk->sk)); 716 if (err < 0) {
694 if (!STATE_IS_OPEN(cf_sk)) { 717 kfree_skb(skb);
695 if (!STATE_IS_PENDING(cf_sk)) 718 goto pipe_err;
696 mask |= POLLHUP; 719 }
697 } else { 720 sent += size;
698 if (cfpkt_qpeek(cf_sk->pktq) != NULL)
699 mask |= (POLLIN | POLLRDNORM);
700 if (TX_FLOW_IS_ON(cf_sk))
701 mask |= (POLLOUT | POLLWRNORM);
702 } 721 }
703 release_sock(&cf_sk->sk);
704 trace_printk("CAIF: %s(): poll mask=0x%04x\n",
705 __func__, mask);
706 return mask;
707}
708
709static void drain_queue(struct caifsock *cf_sk)
710{
711 struct cfpkt *pkt = NULL;
712
713 /* Empty the queue */
714 do {
715 /* The queue has its own lock */
716 if (!cf_sk->pktq)
717 break;
718
719 pkt = cfpkt_dequeue(cf_sk->pktq);
720 if (!pkt)
721 break;
722 pr_debug("CAIF: %s(): freeing packet from read queue\n",
723 __func__);
724 cfpkt_destroy(pkt);
725 722
726 } while (1); 723 return sent;
727 724
728 cf_sk->read_queue_len = 0; 725pipe_err:
726 if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL))
727 send_sig(SIGPIPE, current, 0);
728 err = -EPIPE;
729out_err:
730 return sent ? : err;
729} 731}
730 732
731static int setsockopt(struct socket *sock, 733static int setsockopt(struct socket *sock,
@@ -736,19 +738,13 @@ static int setsockopt(struct socket *sock,
736 int prio, linksel; 738 int prio, linksel;
737 struct ifreq ifreq; 739 struct ifreq ifreq;
738 740
739 if (STATE_IS_OPEN(cf_sk)) { 741 if (cf_sk->sk.sk_socket->state != SS_UNCONNECTED)
740 pr_debug("CAIF: %s(): setsockopt "
741 "cannot be done on a connected socket\n",
742 __func__);
743 return -ENOPROTOOPT; 742 return -ENOPROTOOPT;
744 } 743
745 switch (opt) { 744 switch (opt) {
746 case CAIFSO_LINK_SELECT: 745 case CAIFSO_LINK_SELECT:
747 if (ol < sizeof(int)) { 746 if (ol < sizeof(int))
748 pr_debug("CAIF: %s(): setsockopt"
749 " CAIFSO_CHANNEL_CONFIG bad size\n", __func__);
750 return -EINVAL; 747 return -EINVAL;
751 }
752 if (lvl != SOL_CAIF) 748 if (lvl != SOL_CAIF)
753 goto bad_sol; 749 goto bad_sol;
754 if (copy_from_user(&linksel, ov, sizeof(int))) 750 if (copy_from_user(&linksel, ov, sizeof(int)))
@@ -761,28 +757,20 @@ static int setsockopt(struct socket *sock,
761 case SO_PRIORITY: 757 case SO_PRIORITY:
762 if (lvl != SOL_SOCKET) 758 if (lvl != SOL_SOCKET)
763 goto bad_sol; 759 goto bad_sol;
764 if (ol < sizeof(int)) { 760 if (ol < sizeof(int))
765 pr_debug("CAIF: %s(): setsockopt"
766 " SO_PRIORITY bad size\n", __func__);
767 return -EINVAL; 761 return -EINVAL;
768 }
769 if (copy_from_user(&prio, ov, sizeof(int))) 762 if (copy_from_user(&prio, ov, sizeof(int)))
770 return -EINVAL; 763 return -EINVAL;
771 lock_sock(&(cf_sk->sk)); 764 lock_sock(&(cf_sk->sk));
772 cf_sk->conn_req.priority = prio; 765 cf_sk->conn_req.priority = prio;
773 pr_debug("CAIF: %s(): Setting sockopt priority=%d\n", __func__,
774 cf_sk->conn_req.priority);
775 release_sock(&cf_sk->sk); 766 release_sock(&cf_sk->sk);
776 return 0; 767 return 0;
777 768
778 case SO_BINDTODEVICE: 769 case SO_BINDTODEVICE:
779 if (lvl != SOL_SOCKET) 770 if (lvl != SOL_SOCKET)
780 goto bad_sol; 771 goto bad_sol;
781 if (ol < sizeof(struct ifreq)) { 772 if (ol < sizeof(struct ifreq))
782 pr_debug("CAIF: %s(): setsockopt"
783 " SO_PRIORITY bad size\n", __func__);
784 return -EINVAL; 773 return -EINVAL;
785 }
786 if (copy_from_user(&ifreq, ov, sizeof(ifreq))) 774 if (copy_from_user(&ifreq, ov, sizeof(ifreq)))
787 return -EFAULT; 775 return -EFAULT;
788 lock_sock(&(cf_sk->sk)); 776 lock_sock(&(cf_sk->sk));
@@ -798,359 +786,275 @@ static int setsockopt(struct socket *sock,
798 goto bad_sol; 786 goto bad_sol;
799 if (cf_sk->sk.sk_protocol != CAIFPROTO_UTIL) 787 if (cf_sk->sk.sk_protocol != CAIFPROTO_UTIL)
800 return -ENOPROTOOPT; 788 return -ENOPROTOOPT;
801 if (ol > sizeof(cf_sk->conn_req.param.data))
802 goto req_param_bad_size;
803
804 lock_sock(&(cf_sk->sk)); 789 lock_sock(&(cf_sk->sk));
805 cf_sk->conn_req.param.size = ol; 790 cf_sk->conn_req.param.size = ol;
806 if (copy_from_user(&cf_sk->conn_req.param.data, ov, ol)) { 791 if (ol > sizeof(cf_sk->conn_req.param.data) ||
792 copy_from_user(&cf_sk->conn_req.param.data, ov, ol)) {
807 release_sock(&cf_sk->sk); 793 release_sock(&cf_sk->sk);
808req_param_bad_size:
809 pr_debug("CAIF: %s(): setsockopt"
810 " CAIFSO_CHANNEL_CONFIG bad size\n", __func__);
811 return -EINVAL; 794 return -EINVAL;
812 } 795 }
813
814 release_sock(&cf_sk->sk); 796 release_sock(&cf_sk->sk);
815 return 0; 797 return 0;
816 798
817 default: 799 default:
818 pr_debug("CAIF: %s(): unhandled option %d\n", __func__, opt); 800 return -ENOPROTOOPT;
819 return -EINVAL;
820 } 801 }
821 802
822 return 0; 803 return 0;
823bad_sol: 804bad_sol:
824 pr_debug("CAIF: %s(): setsockopt bad level\n", __func__);
825 return -ENOPROTOOPT; 805 return -ENOPROTOOPT;
826 806
827} 807}
828 808
829static int caif_connect(struct socket *sock, struct sockaddr *uservaddr, 809/*
830 int sockaddr_len, int flags) 810 * caif_connect() - Connect a CAIF Socket
811 * Copied and modified af_irda.c:irda_connect().
812 *
813 * Note : by consulting "errno", the user space caller may learn the cause
814 * of the failure. Most of them are visible in the function, others may come
815 * from subroutines called and are listed here :
816 * o -EAFNOSUPPORT: bad socket family or type.
817 * o -ESOCKTNOSUPPORT: bad socket type or protocol
818 * o -EINVAL: bad socket address, or CAIF link type
819 * o -ECONNREFUSED: remote end refused the connection.
820 * o -EINPROGRESS: connect request sent but timed out (or non-blocking)
821 * o -EISCONN: already connected.
822 * o -ETIMEDOUT: Connection timed out (send timeout)
823 * o -ENODEV: No link layer to send request
824 * o -ECONNRESET: Received Shutdown indication or lost link layer
825 * o -ENOMEM: Out of memory
826 *
827 * State Strategy:
828 * o sk_state: holds the CAIF_* protocol state, it's updated by
829 * caif_ctrl_cb.
830 * o sock->state: holds the SS_* socket state and is updated by connect and
831 * disconnect.
832 */
833static int caif_connect(struct socket *sock, struct sockaddr *uaddr,
834 int addr_len, int flags)
831{ 835{
832 struct caifsock *cf_sk = NULL;
833 int result = -1;
834 int mode = 0;
835 int ret = -EIO;
836 struct sock *sk = sock->sk; 836 struct sock *sk = sock->sk;
837 BUG_ON(sk == NULL); 837 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
838 838 long timeo;
839 cf_sk = container_of(sk, struct caifsock, sk); 839 int err;
840 840 lock_sock(sk);
841 trace_printk("CAIF: %s(): cf_sk=%p OPEN=%d, TX_FLOW=%d, RX_FLOW=%d\n",
842 __func__, cf_sk,
843 STATE_IS_OPEN(cf_sk),
844 TX_FLOW_IS_ON(cf_sk), RX_FLOW_IS_ON(cf_sk));
845
846 841
847 if (sock->type == SOCK_SEQPACKET || sock->type == SOCK_STREAM) 842 err = -EAFNOSUPPORT;
848 sock->state = SS_CONNECTING; 843 if (uaddr->sa_family != AF_CAIF)
849 else
850 goto out; 844 goto out;
851 845
852 /* I want to be alone on cf_sk (except status and queue) */ 846 err = -ESOCKTNOSUPPORT;
853 lock_sock(&(cf_sk->sk)); 847 if (unlikely(!(sk->sk_type == SOCK_STREAM &&
854 848 cf_sk->sk.sk_protocol == CAIFPROTO_AT) &&
855 if (sockaddr_len != sizeof(struct sockaddr_caif)) { 849 sk->sk_type != SOCK_SEQPACKET))
856 pr_debug("CAIF: %s(): Bad address len (%ld,%lu)\n", 850 goto out;
857 __func__, (long) sockaddr_len, 851 switch (sock->state) {
858 (long unsigned) sizeof(struct sockaddr_caif)); 852 case SS_UNCONNECTED:
859 ret = -EINVAL; 853 /* Normal case, a fresh connect */
860 goto open_error; 854 caif_assert(sk->sk_state == CAIF_DISCONNECTED);
855 break;
856 case SS_CONNECTING:
857 switch (sk->sk_state) {
858 case CAIF_CONNECTED:
859 sock->state = SS_CONNECTED;
860 err = -EISCONN;
861 goto out;
862 case CAIF_DISCONNECTED:
863 /* Reconnect allowed */
864 break;
865 case CAIF_CONNECTING:
866 err = -EALREADY;
867 if (flags & O_NONBLOCK)
868 goto out;
869 goto wait_connect;
870 }
871 break;
872 case SS_CONNECTED:
873 caif_assert(sk->sk_state == CAIF_CONNECTED ||
874 sk->sk_state == CAIF_DISCONNECTED);
875 if (sk->sk_shutdown & SHUTDOWN_MASK) {
876 /* Allow re-connect after SHUTDOWN_IND */
877 caif_disconnect_client(&cf_sk->layer);
878 break;
879 }
880 /* No reconnect on a seqpacket socket */
881 err = -EISCONN;
882 goto out;
883 case SS_DISCONNECTING:
884 case SS_FREE:
885 caif_assert(1); /*Should never happen */
886 break;
861 } 887 }
888 sk->sk_state = CAIF_DISCONNECTED;
889 sock->state = SS_UNCONNECTED;
890 sk_stream_kill_queues(&cf_sk->sk);
862 891
863 if (uservaddr->sa_family != AF_CAIF) { 892 err = -EINVAL;
864 pr_debug("CAIF: %s(): Bad address family (%d)\n", 893 if (addr_len != sizeof(struct sockaddr_caif) ||
865 __func__, uservaddr->sa_family); 894 !uaddr)
866 ret = -EAFNOSUPPORT; 895 goto out;
867 goto open_error;
868 }
869 896
870 memcpy(&cf_sk->conn_req.sockaddr, uservaddr, 897 memcpy(&cf_sk->conn_req.sockaddr, uaddr,
871 sizeof(struct sockaddr_caif)); 898 sizeof(struct sockaddr_caif));
872 899
873 dbfs_atomic_inc(&cnt.num_open); 900 /* Move to connecting socket, start sending Connect Requests */
874 mode = SKT_READ_FLAG | SKT_WRITE_FLAG; 901 sock->state = SS_CONNECTING;
875 902 sk->sk_state = CAIF_CONNECTING;
876 /* If socket is not open, make sure socket is in fully closed state */ 903
877 if (!STATE_IS_OPEN(cf_sk)) { 904 dbfs_atomic_inc(&cnt.num_connect_req);
878 /* Has link close response been received (if we ever sent it)?*/ 905 cf_sk->layer.receive = caif_sktrecv_cb;
879 if (STATE_IS_PENDING(cf_sk)) { 906 err = caif_connect_client(&cf_sk->conn_req,
880 /* 907 &cf_sk->layer);
881 * Still waiting for close response from remote. 908 if (err < 0) {
882 * If opened non-blocking, report "would block" 909 cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
883 */ 910 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
884 if (flags & O_NONBLOCK) { 911 goto out;
885 pr_debug("CAIF: %s(): O_NONBLOCK"
886 " && close pending\n", __func__);
887 ret = -EAGAIN;
888 goto open_error;
889 }
890
891 pr_debug("CAIF: %s(): Wait for close response"
892 " from remote...\n", __func__);
893
894 release_sock(&cf_sk->sk);
895
896 /*
897 * Blocking mode; close is pending and we need to wait
898 * for its conclusion.
899 */
900 result =
901 wait_event_interruptible(*sk_sleep(&cf_sk->sk),
902 !STATE_IS_PENDING(cf_sk));
903
904 lock_sock(&(cf_sk->sk));
905 if (result == -ERESTARTSYS) {
906 pr_debug("CAIF: %s(): wait_event_interruptible"
907 "woken by a signal (1)", __func__);
908 ret = -ERESTARTSYS;
909 goto open_error;
910 }
911 }
912 } 912 }
913 913
914 /* socket is now either closed, pending open or open */ 914 err = -EINPROGRESS;
915 if (STATE_IS_OPEN(cf_sk) && !STATE_IS_PENDING(cf_sk)) { 915wait_connect:
916 /* Open */
917 pr_debug("CAIF: %s(): Socket is already opened (cf_sk=%p)"
918 " check access f_flags = 0x%x file_mode = 0x%x\n",
919 __func__, cf_sk, mode, cf_sk->file_mode);
920
921 } else {
922 /* We are closed or pending open.
923 * If closed: send link setup
924 * If pending open: link setup already sent (we could have been
925 * interrupted by a signal last time)
926 */
927 if (!STATE_IS_OPEN(cf_sk)) {
928 /* First opening of file; connect lower layers: */
929 /* Drain queue (very unlikely) */
930 drain_queue(cf_sk);
931
932 cf_sk->layer.receive = caif_sktrecv_cb;
933 SET_STATE_OPEN(cf_sk);
934 SET_PENDING_ON(cf_sk);
935
936 /* Register this channel. */
937 result =
938 caif_connect_client(&cf_sk->conn_req,
939 &cf_sk->layer);
940 if (result < 0) {
941 pr_debug("CAIF: %s(): can't register channel\n",
942 __func__);
943 ret = -EIO;
944 SET_STATE_CLOSED(cf_sk);
945 SET_PENDING_OFF(cf_sk);
946 goto open_error;
947 }
948 dbfs_atomic_inc(&cnt.num_init);
949 }
950
951 /* If opened non-blocking, report "success".
952 */
953 if (flags & O_NONBLOCK) {
954 pr_debug("CAIF: %s(): O_NONBLOCK success\n",
955 __func__);
956 ret = -EINPROGRESS;
957 cf_sk->sk.sk_err = -EINPROGRESS;
958 goto open_error;
959 }
960
961 trace_printk("CAIF: %s(): Wait for connect response\n",
962 __func__);
963 916
964 /* release lock before waiting */ 917 if (sk->sk_state != CAIF_CONNECTED && (flags & O_NONBLOCK))
965 release_sock(&cf_sk->sk); 918 goto out;
966
967 result =
968 wait_event_interruptible(*sk_sleep(&cf_sk->sk),
969 !STATE_IS_PENDING(cf_sk));
970
971 lock_sock(&(cf_sk->sk));
972
973 if (result == -ERESTARTSYS) {
974 pr_debug("CAIF: %s(): wait_event_interruptible"
975 "woken by a signal (2)", __func__);
976 ret = -ERESTARTSYS;
977 goto open_error;
978 }
979
980 if (!STATE_IS_OPEN(cf_sk)) {
981 /* Lower layers said "no" */
982 pr_debug("CAIF: %s(): Closed received\n", __func__);
983 ret = -EPIPE;
984 goto open_error;
985 }
986 919
987 trace_printk("CAIF: %s(): Connect received\n", __func__); 920 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
921
922 release_sock(sk);
923 err = wait_event_interruptible_timeout(*sk_sleep(sk),
924 sk->sk_state != CAIF_CONNECTING,
925 timeo);
926 lock_sock(sk);
927 if (err < 0)
928 goto out; /* -ERESTARTSYS */
929 if (err == 0 && sk->sk_state != CAIF_CONNECTED) {
930 err = -ETIMEDOUT;
931 goto out;
988 } 932 }
989 /* Open is ok */
990 cf_sk->file_mode |= mode;
991 933
992 trace_printk("CAIF: %s(): Connected - file mode = %x\n", 934 if (sk->sk_state != CAIF_CONNECTED) {
993 __func__, cf_sk->file_mode); 935 sock->state = SS_UNCONNECTED;
994 936 err = sock_error(sk);
995 release_sock(&cf_sk->sk); 937 if (!err)
996 return 0; 938 err = -ECONNREFUSED;
997open_error: 939 goto out;
998 sock->state = SS_UNCONNECTED; 940 }
999 release_sock(&cf_sk->sk); 941 sock->state = SS_CONNECTED;
942 err = 0;
1000out: 943out:
1001 return ret; 944 release_sock(sk);
945 return err;
1002} 946}
1003 947
1004static int caif_shutdown(struct socket *sock, int how) 948
949/*
950 * caif_release() - Disconnect a CAIF Socket
951 * Copied and modified af_irda.c:irda_release().
952 */
953static int caif_release(struct socket *sock)
1005{ 954{
1006 struct caifsock *cf_sk = NULL;
1007 int result = 0;
1008 int tx_flow_state_was_on;
1009 struct sock *sk = sock->sk; 955 struct sock *sk = sock->sk;
956 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
957 int res = 0;
1010 958
1011 trace_printk("CAIF: %s(): enter\n", __func__); 959 if (!sk)
1012 pr_debug("f_flags=%x\n", sock->file->f_flags); 960 return 0;
1013
1014 if (how != SHUT_RDWR)
1015 return -EOPNOTSUPP;
1016
1017 cf_sk = container_of(sk, struct caifsock, sk);
1018 if (cf_sk == NULL) {
1019 pr_debug("CAIF: %s(): COULD NOT FIND SOCKET\n", __func__);
1020 return -EBADF;
1021 }
1022
1023 /* I want to be alone on cf_sk (except status queue) */
1024 lock_sock(&(cf_sk->sk));
1025 sock_hold(&cf_sk->sk);
1026
1027 /* IS_CLOSED have double meaning:
1028 * 1) Spontanous Remote Shutdown Request.
1029 * 2) Ack on a channel teardown(disconnect)
1030 * Must clear bit in case we previously received
1031 * remote shudown request.
1032 */
1033 if (STATE_IS_OPEN(cf_sk) && !STATE_IS_PENDING(cf_sk)) {
1034 SET_STATE_CLOSED(cf_sk);
1035 SET_PENDING_ON(cf_sk);
1036 tx_flow_state_was_on = TX_FLOW_IS_ON(cf_sk);
1037 SET_TX_FLOW_OFF(cf_sk);
1038
1039 /* Hold the socket until DEINIT_RSP is received */
1040 sock_hold(&cf_sk->sk);
1041 result = caif_disconnect_client(&cf_sk->layer);
1042
1043 if (result < 0) {
1044 pr_debug("CAIF: %s(): "
1045 "caif_disconnect_client() failed\n",
1046 __func__);
1047 SET_STATE_CLOSED(cf_sk);
1048 SET_PENDING_OFF(cf_sk);
1049 SET_TX_FLOW_OFF(cf_sk);
1050 release_sock(&cf_sk->sk);
1051 sock_put(&cf_sk->sk);
1052 return -EIO;
1053 }
1054 961
1055 } 962 set_tx_flow_off(cf_sk);
1056 if (STATE_IS_REMOTE_SHUTDOWN(cf_sk)) {
1057 SET_PENDING_OFF(cf_sk);
1058 SET_REMOTE_SHUTDOWN_OFF(cf_sk);
1059 }
1060 963
1061 /* 964 /*
1062 * Socket is no longer in state pending close, 965 * Ensure that packets are not queued after this point in time.
1063 * and we can release the reference. 966 * caif_queue_rcv_skb checks SOCK_DEAD holding the queue lock,
967 * this ensures no packets when sock is dead.
1064 */ 968 */
969 spin_lock(&sk->sk_receive_queue.lock);
970 sock_set_flag(sk, SOCK_DEAD);
971 spin_unlock(&sk->sk_receive_queue.lock);
972 sock->sk = NULL;
1065 973
1066 dbfs_atomic_inc(&cnt.num_close); 974 dbfs_atomic_inc(&cnt.num_disconnect);
1067 drain_queue(cf_sk);
1068 SET_RX_FLOW_ON(cf_sk);
1069 cf_sk->file_mode = 0;
1070 sock_put(&cf_sk->sk);
1071 release_sock(&cf_sk->sk);
1072 if (!result && (sock->file->f_flags & O_NONBLOCK)) {
1073 pr_debug("nonblocking shutdown returing -EAGAIN\n");
1074 return -EAGAIN;
1075 } else
1076 return result;
1077}
1078
1079static ssize_t caif_sock_no_sendpage(struct socket *sock,
1080 struct page *page,
1081 int offset, size_t size, int flags)
1082{
1083 return -EOPNOTSUPP;
1084}
1085
1086/* This function is called as part of close. */
1087static int caif_release(struct socket *sock)
1088{
1089 struct sock *sk = sock->sk;
1090 struct caifsock *cf_sk = NULL;
1091 int res;
1092 caif_assert(sk != NULL);
1093 cf_sk = container_of(sk, struct caifsock, sk);
1094 975
1095 if (cf_sk->debugfs_socket_dir != NULL) 976 if (cf_sk->debugfs_socket_dir != NULL)
1096 debugfs_remove_recursive(cf_sk->debugfs_socket_dir); 977 debugfs_remove_recursive(cf_sk->debugfs_socket_dir);
1097 978
1098 res = caif_shutdown(sock, SHUT_RDWR);
1099 if (res && res != -EINPROGRESS)
1100 return res;
1101
1102 /*
1103 * FIXME: Shutdown should probably be possible to do async
1104 * without flushing queues, allowing reception of frames while
1105 * waiting for DEINIT_IND.
1106 * Release should always block, to allow secure decoupling of
1107 * CAIF stack.
1108 */
1109 if (!(sock->file->f_flags & O_NONBLOCK)) {
1110 res = wait_event_interruptible(*sk_sleep(&cf_sk->sk),
1111 !STATE_IS_PENDING(cf_sk));
1112
1113 if (res == -ERESTARTSYS) {
1114 pr_debug("CAIF: %s(): wait_event_interruptible"
1115 "woken by a signal (1)", __func__);
1116 }
1117 }
1118 lock_sock(&(cf_sk->sk)); 979 lock_sock(&(cf_sk->sk));
980 sk->sk_state = CAIF_DISCONNECTED;
981 sk->sk_shutdown = SHUTDOWN_MASK;
1119 982
1120 sock->sk = NULL; 983 if (cf_sk->sk.sk_socket->state == SS_CONNECTED ||
984 cf_sk->sk.sk_socket->state == SS_CONNECTING)
985 res = caif_disconnect_client(&cf_sk->layer);
1121 986
1122 /* Detach the socket from its process context by making it orphan. */ 987 cf_sk->sk.sk_socket->state = SS_DISCONNECTING;
1123 sock_orphan(sk); 988 wake_up_interruptible_poll(sk_sleep(sk), POLLERR|POLLHUP);
1124 989
1125 /* 990 sock_orphan(sk);
1126 * Setting SHUTDOWN_MASK means that both send and receive are shutdown 991 cf_sk->layer.dn = NULL;
1127 * for the socket. 992 sk_stream_kill_queues(&cf_sk->sk);
1128 */ 993 release_sock(sk);
1129 sk->sk_shutdown = SHUTDOWN_MASK; 994 sock_put(sk);
995 return res;
996}
1130 997
1131 /* 998/* Copied from af_unix.c:unix_poll(), added CAIF tx_flow handling */
1132 * Set the socket state to closed, the TCP_CLOSE macro is used when 999static unsigned int caif_poll(struct file *file,
1133 * closing any socket. 1000 struct socket *sock, poll_table *wait)
1134 */ 1001{
1002 struct sock *sk = sock->sk;
1003 unsigned int mask;
1004 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
1135 1005
1136 /* Flush out this sockets receive queue. */ 1006 sock_poll_wait(file, sk_sleep(sk), wait);
1137 drain_queue(cf_sk); 1007 mask = 0;
1138 1008
1139 /* Finally release the socket. */ 1009 /* exceptional events? */
1140 SET_STATE_PENDING_DESTROY(cf_sk); 1010 if (sk->sk_err)
1011 mask |= POLLERR;
1012 if (sk->sk_shutdown == SHUTDOWN_MASK)
1013 mask |= POLLHUP;
1014 if (sk->sk_shutdown & RCV_SHUTDOWN)
1015 mask |= POLLRDHUP;
1141 1016
1142 release_sock(&cf_sk->sk); 1017 /* readable? */
1018 if (!skb_queue_empty(&sk->sk_receive_queue) ||
1019 (sk->sk_shutdown & RCV_SHUTDOWN))
1020 mask |= POLLIN | POLLRDNORM;
1143 1021
1144 sock_put(sk); 1022 /* Connection-based need to check for termination and startup */
1023 if (sk->sk_state == CAIF_DISCONNECTED)
1024 mask |= POLLHUP;
1145 1025
1146 /* 1026 /*
1147 * The rest of the cleanup will be handled from the 1027 * we set writable also when the other side has shut down the
1148 * caif_sock_destructor 1028 * connection. This prevents stuck sockets.
1149 */ 1029 */
1150 return res; 1030 if (sock_writeable(sk) && tx_flow_is_on(cf_sk))
1031 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1032
1033 return mask;
1151} 1034}
1152 1035
1153static const struct proto_ops caif_ops = { 1036static const struct proto_ops caif_seqpacket_ops = {
1037 .family = PF_CAIF,
1038 .owner = THIS_MODULE,
1039 .release = caif_release,
1040 .bind = sock_no_bind,
1041 .connect = caif_connect,
1042 .socketpair = sock_no_socketpair,
1043 .accept = sock_no_accept,
1044 .getname = sock_no_getname,
1045 .poll = caif_poll,
1046 .ioctl = sock_no_ioctl,
1047 .listen = sock_no_listen,
1048 .shutdown = sock_no_shutdown,
1049 .setsockopt = setsockopt,
1050 .getsockopt = sock_no_getsockopt,
1051 .sendmsg = caif_seqpkt_sendmsg,
1052 .recvmsg = caif_seqpkt_recvmsg,
1053 .mmap = sock_no_mmap,
1054 .sendpage = sock_no_sendpage,
1055};
1056
1057static const struct proto_ops caif_stream_ops = {
1154 .family = PF_CAIF, 1058 .family = PF_CAIF,
1155 .owner = THIS_MODULE, 1059 .owner = THIS_MODULE,
1156 .release = caif_release, 1060 .release = caif_release,
@@ -1162,73 +1066,62 @@ static const struct proto_ops caif_ops = {
1162 .poll = caif_poll, 1066 .poll = caif_poll,
1163 .ioctl = sock_no_ioctl, 1067 .ioctl = sock_no_ioctl,
1164 .listen = sock_no_listen, 1068 .listen = sock_no_listen,
1165 .shutdown = caif_shutdown, 1069 .shutdown = sock_no_shutdown,
1166 .setsockopt = setsockopt, 1070 .setsockopt = setsockopt,
1167 .getsockopt = sock_no_getsockopt, 1071 .getsockopt = sock_no_getsockopt,
1168 .sendmsg = caif_sendmsg, 1072 .sendmsg = caif_stream_sendmsg,
1169 .recvmsg = caif_recvmsg, 1073 .recvmsg = caif_stream_recvmsg,
1170 .mmap = sock_no_mmap, 1074 .mmap = sock_no_mmap,
1171 .sendpage = caif_sock_no_sendpage, 1075 .sendpage = sock_no_sendpage,
1172}; 1076};
1173 1077
1174/* This function is called when a socket is finally destroyed. */ 1078/* This function is called when a socket is finally destroyed. */
1175static void caif_sock_destructor(struct sock *sk) 1079static void caif_sock_destructor(struct sock *sk)
1176{ 1080{
1177 struct caifsock *cf_sk = NULL; 1081 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
1178 cf_sk = container_of(sk, struct caifsock, sk);
1179 /* Error checks. */
1180 caif_assert(!atomic_read(&sk->sk_wmem_alloc)); 1082 caif_assert(!atomic_read(&sk->sk_wmem_alloc));
1181 caif_assert(sk_unhashed(sk)); 1083 caif_assert(sk_unhashed(sk));
1182 caif_assert(!sk->sk_socket); 1084 caif_assert(!sk->sk_socket);
1183 if (!sock_flag(sk, SOCK_DEAD)) { 1085 if (!sock_flag(sk, SOCK_DEAD)) {
1184 pr_debug("CAIF: %s(): 0x%p", __func__, sk); 1086 pr_info("Attempt to release alive CAIF socket: %p\n", sk);
1185 return; 1087 return;
1186 } 1088 }
1187 1089 sk_stream_kill_queues(&cf_sk->sk);
1188 if (STATE_IS_OPEN(cf_sk)) { 1090 dbfs_atomic_dec(&cnt.caif_nr_socks);
1189 pr_debug("CAIF: %s(): socket is opened (cf_sk=%p)"
1190 " file_mode = 0x%x\n", __func__,
1191 cf_sk, cf_sk->file_mode);
1192 return;
1193 }
1194 drain_queue(cf_sk);
1195 kfree(cf_sk->pktq);
1196
1197 trace_printk("CAIF: %s(): caif_sock_destructor: Removing socket %s\n",
1198 __func__, cf_sk->name);
1199 atomic_dec(&caif_nr_socks);
1200} 1091}
1201 1092
1202static int caif_create(struct net *net, struct socket *sock, int protocol, 1093static int caif_create(struct net *net, struct socket *sock, int protocol,
1203 int kern) 1094 int kern)
1204{ 1095{
1205 struct sock *sk = NULL; 1096 struct sock *sk = NULL;
1206 struct caifsock *cf_sk = NULL; 1097 struct caifsock *cf_sk = NULL;
1207 int result = 0;
1208 static struct proto prot = {.name = "PF_CAIF", 1098 static struct proto prot = {.name = "PF_CAIF",
1209 .owner = THIS_MODULE, 1099 .owner = THIS_MODULE,
1210 .obj_size = sizeof(struct caifsock), 1100 .obj_size = sizeof(struct caifsock),
1211 }; 1101 };
1212 1102
1103 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_NET_ADMIN))
1104 return -EPERM;
1213 /* 1105 /*
1214 * The sock->type specifies the socket type to use. 1106 * The sock->type specifies the socket type to use.
1215 * in SEQPACKET mode packet boundaries are enforced. 1107 * The CAIF socket is a packet stream in the sense
1108 * that it is packet based. CAIF trusts the reliability
1109 * of the link, no resending is implemented.
1216 */ 1110 */
1217 if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_STREAM) 1111 if (sock->type == SOCK_SEQPACKET)
1112 sock->ops = &caif_seqpacket_ops;
1113 else if (sock->type == SOCK_STREAM)
1114 sock->ops = &caif_stream_ops;
1115 else
1218 return -ESOCKTNOSUPPORT; 1116 return -ESOCKTNOSUPPORT;
1219 1117
1220 if (net != &init_net)
1221 return -EAFNOSUPPORT;
1222
1223 if (protocol < 0 || protocol >= CAIFPROTO_MAX) 1118 if (protocol < 0 || protocol >= CAIFPROTO_MAX)
1224 return -EPROTONOSUPPORT; 1119 return -EPROTONOSUPPORT;
1225 /* 1120 /*
1226 * Set the socket state to unconnected. The socket state is really 1121 * Set the socket state to unconnected. The socket state
1227 * not used at all in the net/core or socket.c but the 1122 * is really not used at all in the net/core or socket.c but the
1228 * initialization makes sure that sock->state is not uninitialized. 1123 * initialization makes sure that sock->state is not uninitialized.
1229 */ 1124 */
1230 sock->state = SS_UNCONNECTED;
1231
1232 sk = sk_alloc(net, PF_CAIF, GFP_KERNEL, &prot); 1125 sk = sk_alloc(net, PF_CAIF, GFP_KERNEL, &prot);
1233 if (!sk) 1126 if (!sk)
1234 return -ENOMEM; 1127 return -ENOMEM;
@@ -1238,11 +1131,9 @@ static int caif_create(struct net *net, struct socket *sock, int protocol,
1238 /* Store the protocol */ 1131 /* Store the protocol */
1239 sk->sk_protocol = (unsigned char) protocol; 1132 sk->sk_protocol = (unsigned char) protocol;
1240 1133
1241 spin_lock_init(&cf_sk->read_queue_len_lock); 1134 /* Sendbuf dictates the amount of outbound packets not yet sent */
1242 1135 sk->sk_sndbuf = CAIF_DEF_SNDBUF;
1243 /* Fill in some information concerning the misc socket. */ 1136 sk->sk_rcvbuf = CAIF_DEF_RCVBUF;
1244 snprintf(cf_sk->name, sizeof(cf_sk->name), "cf_sk%d",
1245 atomic_read(&caif_nr_socks));
1246 1137
1247 /* 1138 /*
1248 * Lock in order to try to stop someone from opening the socket 1139 * Lock in order to try to stop someone from opening the socket
@@ -1252,108 +1143,85 @@ static int caif_create(struct net *net, struct socket *sock, int protocol,
1252 1143
1253 /* Initialize the nozero default sock structure data. */ 1144 /* Initialize the nozero default sock structure data. */
1254 sock_init_data(sock, sk); 1145 sock_init_data(sock, sk);
1255 sock->ops = &caif_ops;
1256 sk->sk_destruct = caif_sock_destructor; 1146 sk->sk_destruct = caif_sock_destructor;
1257 sk->sk_sndbuf = caif_sockbuf_size;
1258 sk->sk_rcvbuf = caif_sockbuf_size;
1259 1147
1260 cf_sk->pktq = cfpktq_create(); 1148 mutex_init(&cf_sk->readlock); /* single task reading lock */
1149 cf_sk->layer.ctrlcmd = caif_ctrl_cb;
1150 cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
1151 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
1261 1152
1262 if (!cf_sk->pktq) { 1153 set_tx_flow_off(cf_sk);
1263 pr_err("CAIF: %s(): queue create failed.\n", __func__); 1154 set_rx_flow_on(cf_sk);
1264 result = -ENOMEM;
1265 release_sock(&cf_sk->sk);
1266 goto err_failed;
1267 }
1268 cf_sk->layer.ctrlcmd = caif_sktflowctrl_cb;
1269 SET_STATE_CLOSED(cf_sk);
1270 SET_PENDING_OFF(cf_sk);
1271 SET_TX_FLOW_OFF(cf_sk);
1272 SET_RX_FLOW_ON(cf_sk);
1273 1155
1274 /* Set default options on configuration */ 1156 /* Set default options on configuration */
1275 cf_sk->conn_req.priority = CAIF_PRIO_NORMAL; 1157 cf_sk->conn_req.priority = CAIF_PRIO_NORMAL;
1276 cf_sk->conn_req.link_selector = CAIF_LINK_HIGH_BANDW; 1158 cf_sk->conn_req.link_selector = CAIF_LINK_LOW_LATENCY;
1277 cf_sk->conn_req.protocol = protocol; 1159 cf_sk->conn_req.protocol = protocol;
1278 /* Increase the number of sockets created. */ 1160 /* Increase the number of sockets created. */
1279 atomic_inc(&caif_nr_socks); 1161 dbfs_atomic_inc(&cnt.caif_nr_socks);
1162#ifdef CONFIG_DEBUG_FS
1280 if (!IS_ERR(debugfsdir)) { 1163 if (!IS_ERR(debugfsdir)) {
1164 /* Fill in some information concerning the misc socket. */
1165 snprintf(cf_sk->name, sizeof(cf_sk->name), "cfsk%d",
1166 atomic_read(&cnt.caif_nr_socks));
1167
1281 cf_sk->debugfs_socket_dir = 1168 cf_sk->debugfs_socket_dir =
1282 debugfs_create_dir(cf_sk->name, debugfsdir); 1169 debugfs_create_dir(cf_sk->name, debugfsdir);
1283 debugfs_create_u32("conn_state", S_IRUSR | S_IWUSR, 1170 debugfs_create_u32("sk_state", S_IRUSR | S_IWUSR,
1284 cf_sk->debugfs_socket_dir, &cf_sk->conn_state); 1171 cf_sk->debugfs_socket_dir,
1172 (u32 *) &cf_sk->sk.sk_state);
1285 debugfs_create_u32("flow_state", S_IRUSR | S_IWUSR, 1173 debugfs_create_u32("flow_state", S_IRUSR | S_IWUSR,
1286 cf_sk->debugfs_socket_dir, &cf_sk->flow_state); 1174 cf_sk->debugfs_socket_dir, &cf_sk->flow_state);
1287 debugfs_create_u32("read_queue_len", S_IRUSR | S_IWUSR, 1175 debugfs_create_u32("sk_rmem_alloc", S_IRUSR | S_IWUSR,
1176 cf_sk->debugfs_socket_dir,
1177 (u32 *) &cf_sk->sk.sk_rmem_alloc);
1178 debugfs_create_u32("sk_wmem_alloc", S_IRUSR | S_IWUSR,
1288 cf_sk->debugfs_socket_dir, 1179 cf_sk->debugfs_socket_dir,
1289 (u32 *) &cf_sk->read_queue_len); 1180 (u32 *) &cf_sk->sk.sk_wmem_alloc);
1290 debugfs_create_u32("identity", S_IRUSR | S_IWUSR, 1181 debugfs_create_u32("identity", S_IRUSR | S_IWUSR,
1291 cf_sk->debugfs_socket_dir, 1182 cf_sk->debugfs_socket_dir,
1292 (u32 *) &cf_sk->layer.id); 1183 (u32 *) &cf_sk->layer.id);
1293 } 1184 }
1185#endif
1294 release_sock(&cf_sk->sk); 1186 release_sock(&cf_sk->sk);
1295 return 0; 1187 return 0;
1296err_failed:
1297 sk_free(sk);
1298 return result;
1299} 1188}
1300 1189
1190
1301static struct net_proto_family caif_family_ops = { 1191static struct net_proto_family caif_family_ops = {
1302 .family = PF_CAIF, 1192 .family = PF_CAIF,
1303 .create = caif_create, 1193 .create = caif_create,
1304 .owner = THIS_MODULE, 1194 .owner = THIS_MODULE,
1305}; 1195};
1306 1196
1307static int af_caif_init(void) 1197int af_caif_init(void)
1308{ 1198{
1309 int err; 1199 int err = sock_register(&caif_family_ops);
1310 err = sock_register(&caif_family_ops);
1311
1312 if (!err) 1200 if (!err)
1313 return err; 1201 return err;
1314
1315 return 0; 1202 return 0;
1316} 1203}
1317 1204
1318static int __init caif_sktinit_module(void) 1205static int __init caif_sktinit_module(void)
1319{ 1206{
1320 int stat;
1321#ifdef CONFIG_DEBUG_FS 1207#ifdef CONFIG_DEBUG_FS
1322 debugfsdir = debugfs_create_dir("chnl_skt", NULL); 1208 debugfsdir = debugfs_create_dir("caif_sk", NULL);
1323 if (!IS_ERR(debugfsdir)) { 1209 if (!IS_ERR(debugfsdir)) {
1324 debugfs_create_u32("skb_inuse", S_IRUSR | S_IWUSR,
1325 debugfsdir,
1326 (u32 *) &cnt.skb_in_use);
1327 debugfs_create_u32("skb_alloc", S_IRUSR | S_IWUSR,
1328 debugfsdir,
1329 (u32 *) &cnt.skb_alloc);
1330 debugfs_create_u32("skb_free", S_IRUSR | S_IWUSR,
1331 debugfsdir,
1332 (u32 *) &cnt.skb_free);
1333 debugfs_create_u32("num_sockets", S_IRUSR | S_IWUSR, 1210 debugfs_create_u32("num_sockets", S_IRUSR | S_IWUSR,
1334 debugfsdir, 1211 debugfsdir,
1335 (u32 *) &caif_nr_socks); 1212 (u32 *) &cnt.caif_nr_socks);
1336 debugfs_create_u32("num_open", S_IRUSR | S_IWUSR, 1213 debugfs_create_u32("num_connect_req", S_IRUSR | S_IWUSR,
1337 debugfsdir, 1214 debugfsdir,
1338 (u32 *) &cnt.num_open); 1215 (u32 *) &cnt.num_connect_req);
1339 debugfs_create_u32("num_close", S_IRUSR | S_IWUSR, 1216 debugfs_create_u32("num_connect_resp", S_IRUSR | S_IWUSR,
1340 debugfsdir, 1217 debugfsdir,
1341 (u32 *) &cnt.num_close); 1218 (u32 *) &cnt.num_connect_resp);
1342 debugfs_create_u32("num_init", S_IRUSR | S_IWUSR, 1219 debugfs_create_u32("num_connect_fail_resp", S_IRUSR | S_IWUSR,
1343 debugfsdir, 1220 debugfsdir,
1344 (u32 *) &cnt.num_init); 1221 (u32 *) &cnt.num_connect_fail_resp);
1345 debugfs_create_u32("num_init_resp", S_IRUSR | S_IWUSR, 1222 debugfs_create_u32("num_disconnect", S_IRUSR | S_IWUSR,
1346 debugfsdir, 1223 debugfsdir,
1347 (u32 *) &cnt.num_init_resp); 1224 (u32 *) &cnt.num_disconnect);
1348 debugfs_create_u32("num_init_fail_resp", S_IRUSR | S_IWUSR,
1349 debugfsdir,
1350 (u32 *) &cnt.num_init_fail_resp);
1351 debugfs_create_u32("num_deinit", S_IRUSR | S_IWUSR,
1352 debugfsdir,
1353 (u32 *) &cnt.num_deinit);
1354 debugfs_create_u32("num_deinit_resp", S_IRUSR | S_IWUSR,
1355 debugfsdir,
1356 (u32 *) &cnt.num_deinit_resp);
1357 debugfs_create_u32("num_remote_shutdown_ind", 1225 debugfs_create_u32("num_remote_shutdown_ind",
1358 S_IRUSR | S_IWUSR, debugfsdir, 1226 S_IRUSR | S_IWUSR, debugfsdir,
1359 (u32 *) &cnt.num_remote_shutdown_ind); 1227 (u32 *) &cnt.num_remote_shutdown_ind);
@@ -1371,13 +1239,7 @@ static int __init caif_sktinit_module(void)
1371 (u32 *) &cnt.num_rx_flow_on); 1239 (u32 *) &cnt.num_rx_flow_on);
1372 } 1240 }
1373#endif 1241#endif
1374 stat = af_caif_init(); 1242 return af_caif_init();
1375 if (stat) {
1376 pr_err("CAIF: %s(): Failed to initialize CAIF socket layer.",
1377 __func__);
1378 return stat;
1379 }
1380 return 0;
1381} 1243}
1382 1244
1383static void __exit caif_sktexit_module(void) 1245static void __exit caif_sktexit_module(void)
@@ -1386,6 +1248,5 @@ static void __exit caif_sktexit_module(void)
1386 if (debugfsdir != NULL) 1248 if (debugfsdir != NULL)
1387 debugfs_remove_recursive(debugfsdir); 1249 debugfs_remove_recursive(debugfsdir);
1388} 1250}
1389
1390module_init(caif_sktinit_module); 1251module_init(caif_sktinit_module);
1391module_exit(caif_sktexit_module); 1252module_exit(caif_sktexit_module);