blob: b840395ced1d6b115d1cfd489d9b878c7e9cd326 [file] [log] [blame]
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001/*
2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Sjur Brendeland sjur.brandeland@stericsson.com
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00004 * License terms: GNU General Public License (GPL) version 2
5 */
6
Joe Perchesb31fa5b2010-09-05 21:31:11 +00007#define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
8
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00009#include <linux/fs.h>
10#include <linux/init.h>
11#include <linux/module.h>
12#include <linux/sched.h>
13#include <linux/spinlock.h>
14#include <linux/mutex.h>
15#include <linux/list.h>
16#include <linux/wait.h>
17#include <linux/poll.h>
18#include <linux/tcp.h>
19#include <linux/uaccess.h>
Sjur Braendelandbece7b22010-04-28 08:54:38 +000020#include <linux/debugfs.h>
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000021#include <linux/caif/caif_socket.h>
Sjur Braendelandbece7b22010-04-28 08:54:38 +000022#include <asm/atomic.h>
23#include <net/sock.h>
24#include <net/tcp_states.h>
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000025#include <net/caif/caif_layer.h>
26#include <net/caif/caif_dev.h>
27#include <net/caif/cfpkt.h>
28
29MODULE_LICENSE("GPL");
Sjur Braendelandbece7b22010-04-28 08:54:38 +000030MODULE_ALIAS_NETPROTO(AF_CAIF);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000031
Sjur Braendelandbece7b22010-04-28 08:54:38 +000032/*
33 * CAIF state is re-using the TCP socket states.
34 * caif_states stored in sk_state reflect the state as reported by
35 * the CAIF stack, while sk_socket->state is the state of the socket.
36 */
37enum caif_states {
38 CAIF_CONNECTED = TCP_ESTABLISHED,
39 CAIF_CONNECTING = TCP_SYN_SENT,
40 CAIF_DISCONNECTED = TCP_CLOSE
41};
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000042
Sjur Braendelandbece7b22010-04-28 08:54:38 +000043#define TX_FLOW_ON_BIT 1
44#define RX_FLOW_ON_BIT 2
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000045
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000046static struct dentry *debugfsdir;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000047
48#ifdef CONFIG_DEBUG_FS
49struct debug_fs_counter {
Sjur Braendelandbece7b22010-04-28 08:54:38 +000050 atomic_t caif_nr_socks;
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +000051 atomic_t caif_sock_create;
Sjur Braendelandbece7b22010-04-28 08:54:38 +000052 atomic_t num_connect_req;
53 atomic_t num_connect_resp;
54 atomic_t num_connect_fail_resp;
55 atomic_t num_disconnect;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000056 atomic_t num_remote_shutdown_ind;
57 atomic_t num_tx_flow_off_ind;
58 atomic_t num_tx_flow_on_ind;
59 atomic_t num_rx_flow_off;
60 atomic_t num_rx_flow_on;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000061};
Sjur Braendelanda9a8f102010-05-21 02:16:11 +000062static struct debug_fs_counter cnt;
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +000063#define dbfs_atomic_inc(v) atomic_inc_return(v)
64#define dbfs_atomic_dec(v) atomic_dec_return(v)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000065#else
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +000066#define dbfs_atomic_inc(v) 0
67#define dbfs_atomic_dec(v) 0
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000068#endif
69
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000070struct caifsock {
Sjur Braendelandbece7b22010-04-28 08:54:38 +000071 struct sock sk; /* must be first member */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000072 struct cflayer layer;
Sjur Braendelandbece7b22010-04-28 08:54:38 +000073 char name[CAIF_LAYER_NAME_SZ]; /* Used for debugging */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000074 u32 flow_state;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000075 struct caif_connect_request conn_req;
Sjur Braendelandbece7b22010-04-28 08:54:38 +000076 struct mutex readlock;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000077 struct dentry *debugfs_socket_dir;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +000078 int headroom, tailroom, maxframe;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000079};
80
Sjur Braendelandbece7b22010-04-28 08:54:38 +000081static 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}
92
93static void set_rx_flow_off(struct caifsock *cf_sk)
94{
95 clear_bit(RX_FLOW_ON_BIT,
96 (void *) &cf_sk->flow_state);
97}
98
99static void set_rx_flow_on(struct caifsock *cf_sk)
100{
101 set_bit(RX_FLOW_ON_BIT,
102 (void *) &cf_sk->flow_state);
103}
104
105static void set_tx_flow_off(struct caifsock *cf_sk)
106{
107 clear_bit(TX_FLOW_ON_BIT,
108 (void *) &cf_sk->flow_state);
109}
110
111static void set_tx_flow_on(struct caifsock *cf_sk)
112{
113 set_bit(TX_FLOW_ON_BIT,
114 (void *) &cf_sk->flow_state);
115}
116
117static void caif_read_lock(struct sock *sk)
118{
119 struct caifsock *cf_sk;
120 cf_sk = container_of(sk, struct caifsock, sk);
121 mutex_lock(&cf_sk->readlock);
122}
123
124static void caif_read_unlock(struct sock *sk)
125{
126 struct caifsock *cf_sk;
127 cf_sk = container_of(sk, struct caifsock, sk);
128 mutex_unlock(&cf_sk->readlock);
129}
130
Sjur Braendelanda9a8f102010-05-21 02:16:11 +0000131static int sk_rcvbuf_lowwater(struct caifsock *cf_sk)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000132{
133 /* A quarter of full buffer is used a low water mark */
134 return cf_sk->sk.sk_rcvbuf / 4;
135}
136
Sjur Braendelanda9a8f102010-05-21 02:16:11 +0000137static void caif_flow_ctrl(struct sock *sk, int mode)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000138{
139 struct caifsock *cf_sk;
140 cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000141 if (cf_sk->layer.dn && cf_sk->layer.dn->modemcmd)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000142 cf_sk->layer.dn->modemcmd(cf_sk->layer.dn, mode);
143}
144
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 */
Sjur Braendelanda9a8f102010-05-21 02:16:11 +0000149static int caif_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000150{
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);
156
157 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
158 (unsigned)sk->sk_rcvbuf && rx_flow_is_on(cf_sk)) {
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +0000159 if (net_ratelimit())
160 pr_debug("sending flow OFF (queue len = %d %d)\n",
161 atomic_read(&cf_sk->sk.sk_rmem_alloc),
162 sk_rcvbuf_lowwater(cf_sk));
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000163 set_rx_flow_off(cf_sk);
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000164 dbfs_atomic_inc(&cnt.num_rx_flow_off);
165 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_OFF_REQ);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000166 }
167
168 err = sk_filter(sk, skb);
169 if (err)
170 return err;
171 if (!sk_rmem_schedule(sk, skb->truesize) && rx_flow_is_on(cf_sk)) {
172 set_rx_flow_off(cf_sk);
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +0000173 if (net_ratelimit())
174 pr_debug("sending flow OFF due to rmem_schedule\n");
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000175 dbfs_atomic_inc(&cnt.num_rx_flow_off);
176 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_OFF_REQ);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000177 }
178 skb->dev = NULL;
179 skb_set_owner_r(skb, sk);
180 /* Cache the SKB length before we tack it onto the receive
181 * queue. Once it is added it no longer belongs to us and
182 * may be freed by other threads of control pulling packets
183 * from the queue.
184 */
185 skb_len = skb->len;
186 spin_lock_irqsave(&list->lock, flags);
187 if (!sock_flag(sk, SOCK_DEAD))
188 __skb_queue_tail(list, skb);
189 spin_unlock_irqrestore(&list->lock, flags);
190
191 if (!sock_flag(sk, SOCK_DEAD))
192 sk->sk_data_ready(sk, skb_len);
193 else
194 kfree_skb(skb);
195 return 0;
196}
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000197
198/* Packet Receive Callback function called from CAIF Stack */
199static int caif_sktrecv_cb(struct cflayer *layr, struct cfpkt *pkt)
200{
201 struct caifsock *cf_sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000202 struct sk_buff *skb;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000203
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000204 cf_sk = container_of(layr, struct caifsock, layer);
205 skb = cfpkt_tonative(pkt);
206
207 if (unlikely(cf_sk->sk.sk_state != CAIF_CONNECTED)) {
sjur.brandeland@stericsson.com3f874ad2011-05-13 02:44:08 +0000208 kfree_skb(skb);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000209 return 0;
210 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000211 caif_queue_rcv_skb(&cf_sk->sk, skb);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000212 return 0;
213}
214
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000215static void cfsk_hold(struct cflayer *layr)
216{
217 struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
218 sock_hold(&cf_sk->sk);
219}
220
221static void cfsk_put(struct cflayer *layr)
222{
223 struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
224 sock_put(&cf_sk->sk);
225}
226
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000227/* Packet Control Callback function called from CAIF */
228static void caif_ctrl_cb(struct cflayer *layr,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000229 enum caif_ctrlcmd flow,
230 int phyid)
231{
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000232 struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000233 switch (flow) {
234 case CAIF_CTRLCMD_FLOW_ON_IND:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000235 /* OK from modem to start sending again */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000236 dbfs_atomic_inc(&cnt.num_tx_flow_on_ind);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000237 set_tx_flow_on(cf_sk);
238 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000239 break;
240
241 case CAIF_CTRLCMD_FLOW_OFF_IND:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000242 /* Modem asks us to shut up */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000243 dbfs_atomic_inc(&cnt.num_tx_flow_off_ind);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000244 set_tx_flow_off(cf_sk);
245 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000246 break;
247
248 case CAIF_CTRLCMD_INIT_RSP:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000249 /* We're now connected */
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000250 caif_client_register_refcnt(&cf_sk->layer,
251 cfsk_hold, cfsk_put);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000252 dbfs_atomic_inc(&cnt.num_connect_resp);
253 cf_sk->sk.sk_state = CAIF_CONNECTED;
254 set_tx_flow_on(cf_sk);
255 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000256 break;
257
258 case CAIF_CTRLCMD_DEINIT_RSP:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000259 /* We're now disconnected */
260 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
261 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000262 break;
263
264 case CAIF_CTRLCMD_INIT_FAIL_RSP:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000265 /* Connect request failed */
266 dbfs_atomic_inc(&cnt.num_connect_fail_resp);
267 cf_sk->sk.sk_err = ECONNREFUSED;
268 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
269 cf_sk->sk.sk_shutdown = SHUTDOWN_MASK;
270 /*
271 * Socket "standards" seems to require POLLOUT to
272 * be set at connect failure.
273 */
274 set_tx_flow_on(cf_sk);
275 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000276 break;
277
278 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000279 /* Modem has closed this connection, or device is down. */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000280 dbfs_atomic_inc(&cnt.num_remote_shutdown_ind);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000281 cf_sk->sk.sk_shutdown = SHUTDOWN_MASK;
282 cf_sk->sk.sk_err = ECONNRESET;
283 set_rx_flow_on(cf_sk);
284 cf_sk->sk.sk_error_report(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000285 break;
286
287 default:
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000288 pr_debug("Unexpected flow command %d\n", flow);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000289 }
290}
291
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000292static void caif_check_flow_release(struct sock *sk)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000293{
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000294 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
295
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000296 if (rx_flow_is_on(cf_sk))
297 return;
298
299 if (atomic_read(&sk->sk_rmem_alloc) <= sk_rcvbuf_lowwater(cf_sk)) {
300 dbfs_atomic_inc(&cnt.num_rx_flow_on);
301 set_rx_flow_on(cf_sk);
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000302 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_ON_REQ);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000303 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000304}
305
Sjur Braendelanddcda138d2010-05-21 02:16:12 +0000306/*
307 * Copied from unix_dgram_recvmsg, but removed credit checks,
308 * changed locking, address handling and added MSG_TRUNC.
309 */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000310static int caif_seqpkt_recvmsg(struct kiocb *iocb, struct socket *sock,
Sjur Braendelanddcda138d2010-05-21 02:16:12 +0000311 struct msghdr *m, size_t len, int flags)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000312
313{
314 struct sock *sk = sock->sk;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000315 struct sk_buff *skb;
Sjur Braendelanddcda138d2010-05-21 02:16:12 +0000316 int ret;
317 int copylen;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000318
Sjur Braendelanddcda138d2010-05-21 02:16:12 +0000319 ret = -EOPNOTSUPP;
320 if (m->msg_flags&MSG_OOB)
321 goto read_error;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000322
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000323 skb = skb_recv_datagram(sk, flags, 0 , &ret);
324 if (!skb)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000325 goto read_error;
Sjur Braendelanddcda138d2010-05-21 02:16:12 +0000326 copylen = skb->len;
327 if (len < copylen) {
328 m->msg_flags |= MSG_TRUNC;
329 copylen = len;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000330 }
331
Sjur Braendelanddcda138d2010-05-21 02:16:12 +0000332 ret = skb_copy_datagram_iovec(skb, 0, m->msg_iov, copylen);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000333 if (ret)
Sjur Braendelanddcda138d2010-05-21 02:16:12 +0000334 goto out_free;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000335
Sjur Braendelanddcda138d2010-05-21 02:16:12 +0000336 ret = (flags & MSG_TRUNC) ? skb->len : copylen;
337out_free:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000338 skb_free_datagram(sk, skb);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000339 caif_check_flow_release(sk);
Sjur Braendelanddcda138d2010-05-21 02:16:12 +0000340 return ret;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000341
342read_error:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000343 return ret;
344}
345
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000346
347/* Copied from unix_stream_wait_data, identical except for lock call. */
348static long caif_stream_data_wait(struct sock *sk, long timeo)
349{
350 DEFINE_WAIT(wait);
351 lock_sock(sk);
352
353 for (;;) {
354 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
355
356 if (!skb_queue_empty(&sk->sk_receive_queue) ||
357 sk->sk_err ||
358 sk->sk_state != CAIF_CONNECTED ||
359 sock_flag(sk, SOCK_DEAD) ||
360 (sk->sk_shutdown & RCV_SHUTDOWN) ||
361 signal_pending(current) ||
362 !timeo)
363 break;
364
365 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
366 release_sock(sk);
367 timeo = schedule_timeout(timeo);
368 lock_sock(sk);
369 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
370 }
371
372 finish_wait(sk_sleep(sk), &wait);
373 release_sock(sk);
374 return timeo;
375}
376
377
378/*
379 * Copied from unix_stream_recvmsg, but removed credit checks,
380 * changed locking calls, changed address handling.
381 */
382static int caif_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
383 struct msghdr *msg, size_t size,
384 int flags)
385{
386 struct sock *sk = sock->sk;
387 int copied = 0;
388 int target;
389 int err = 0;
390 long timeo;
391
392 err = -EOPNOTSUPP;
393 if (flags&MSG_OOB)
394 goto out;
395
396 msg->msg_namelen = 0;
397
398 /*
399 * Lock the socket to prevent queue disordering
400 * while sleeps in memcpy_tomsg
401 */
402 err = -EAGAIN;
403 if (sk->sk_state == CAIF_CONNECTING)
404 goto out;
405
406 caif_read_lock(sk);
407 target = sock_rcvlowat(sk, flags&MSG_WAITALL, size);
408 timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
409
410 do {
411 int chunk;
412 struct sk_buff *skb;
413
414 lock_sock(sk);
415 skb = skb_dequeue(&sk->sk_receive_queue);
416 caif_check_flow_release(sk);
417
418 if (skb == NULL) {
419 if (copied >= target)
420 goto unlock;
421 /*
422 * POSIX 1003.1g mandates this order.
423 */
424 err = sock_error(sk);
425 if (err)
426 goto unlock;
427 err = -ECONNRESET;
428 if (sk->sk_shutdown & RCV_SHUTDOWN)
429 goto unlock;
430
431 err = -EPIPE;
432 if (sk->sk_state != CAIF_CONNECTED)
433 goto unlock;
434 if (sock_flag(sk, SOCK_DEAD))
435 goto unlock;
436
437 release_sock(sk);
438
439 err = -EAGAIN;
440 if (!timeo)
441 break;
442
443 caif_read_unlock(sk);
444
445 timeo = caif_stream_data_wait(sk, timeo);
446
447 if (signal_pending(current)) {
448 err = sock_intr_errno(timeo);
449 goto out;
450 }
451 caif_read_lock(sk);
452 continue;
453unlock:
454 release_sock(sk);
455 break;
456 }
457 release_sock(sk);
458 chunk = min_t(unsigned int, skb->len, size);
459 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
460 skb_queue_head(&sk->sk_receive_queue, skb);
461 if (copied == 0)
462 copied = -EFAULT;
463 break;
464 }
465 copied += chunk;
466 size -= chunk;
467
468 /* Mark read part of skb as used */
469 if (!(flags & MSG_PEEK)) {
470 skb_pull(skb, chunk);
471
472 /* put the skb back if we didn't use it up. */
473 if (skb->len) {
474 skb_queue_head(&sk->sk_receive_queue, skb);
475 break;
476 }
477 kfree_skb(skb);
478
479 } else {
480 /*
481 * It is questionable, see note in unix_dgram_recvmsg.
482 */
483 /* put message back and return */
484 skb_queue_head(&sk->sk_receive_queue, skb);
485 break;
486 }
487 } while (size);
488 caif_read_unlock(sk);
489
490out:
491 return copied ? : err;
492}
493
494/*
495 * Copied from sock.c:sock_wait_for_wmem, but change to wait for
496 * CAIF flow-on and sock_writable.
497 */
498static long caif_wait_for_flow_on(struct caifsock *cf_sk,
499 int wait_writeable, long timeo, int *err)
500{
501 struct sock *sk = &cf_sk->sk;
502 DEFINE_WAIT(wait);
503 for (;;) {
504 *err = 0;
505 if (tx_flow_is_on(cf_sk) &&
506 (!wait_writeable || sock_writeable(&cf_sk->sk)))
507 break;
508 *err = -ETIMEDOUT;
509 if (!timeo)
510 break;
511 *err = -ERESTARTSYS;
512 if (signal_pending(current))
513 break;
514 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
515 *err = -ECONNRESET;
516 if (sk->sk_shutdown & SHUTDOWN_MASK)
517 break;
518 *err = -sk->sk_err;
519 if (sk->sk_err)
520 break;
521 *err = -EPIPE;
522 if (cf_sk->sk.sk_state != CAIF_CONNECTED)
523 break;
524 timeo = schedule_timeout(timeo);
525 }
526 finish_wait(sk_sleep(sk), &wait);
527 return timeo;
528}
529
530/*
531 * Transmit a SKB. The device may temporarily request re-transmission
532 * by returning EAGAIN.
533 */
534static int transmit_skb(struct sk_buff *skb, struct caifsock *cf_sk,
535 int noblock, long timeo)
536{
537 struct cfpkt *pkt;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000538
539 pkt = cfpkt_fromnative(CAIF_DIR_OUT, skb);
sjur.brandeland@stericsson.com3f874ad2011-05-13 02:44:08 +0000540 memset(skb->cb, 0, sizeof(struct caif_payload_info));
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000541
Sjur Brændeland4dd820c2011-04-11 10:43:51 +0000542 if (cf_sk->layer.dn == NULL)
543 return -EINVAL;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000544
Sjur Brændeland4dd820c2011-04-11 10:43:51 +0000545 return cf_sk->layer.dn->transmit(cf_sk->layer.dn, pkt);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000546}
547
548/* Copied from af_unix:unix_dgram_sendmsg, and adapted to CAIF */
549static int caif_seqpkt_sendmsg(struct kiocb *kiocb, struct socket *sock,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000550 struct msghdr *msg, size_t len)
551{
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000552 struct sock *sk = sock->sk;
553 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000554 int buffer_size;
555 int ret = 0;
556 struct sk_buff *skb = NULL;
557 int noblock;
558 long timeo;
559 caif_assert(cf_sk);
560 ret = sock_error(sk);
561 if (ret)
562 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000563
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000564 ret = -EOPNOTSUPP;
565 if (msg->msg_flags&MSG_OOB)
566 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000567
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000568 ret = -EOPNOTSUPP;
569 if (msg->msg_namelen)
570 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000571
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000572 ret = -EINVAL;
573 if (unlikely(msg->msg_iov->iov_base == NULL))
574 goto err;
575 noblock = msg->msg_flags & MSG_DONTWAIT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000576
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000577 timeo = sock_sndtimeo(sk, noblock);
578 timeo = caif_wait_for_flow_on(container_of(sk, struct caifsock, sk),
579 1, timeo, &ret);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000580
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000581 if (ret)
582 goto err;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000583 ret = -EPIPE;
584 if (cf_sk->sk.sk_state != CAIF_CONNECTED ||
585 sock_flag(sk, SOCK_DEAD) ||
586 (sk->sk_shutdown & RCV_SHUTDOWN))
587 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000588
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000589 /* Error if trying to write more than maximum frame size. */
590 ret = -EMSGSIZE;
591 if (len > cf_sk->maxframe && cf_sk->sk.sk_protocol != CAIFPROTO_RFM)
592 goto err;
593
594 buffer_size = len + cf_sk->headroom + cf_sk->tailroom;
595
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000596 ret = -ENOMEM;
597 skb = sock_alloc_send_skb(sk, buffer_size, noblock, &ret);
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000598
599 if (!skb || skb_tailroom(skb) < buffer_size)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000600 goto err;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000601
602 skb_reserve(skb, cf_sk->headroom);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000603
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000604 ret = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000605
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000606 if (ret)
607 goto err;
608 ret = transmit_skb(skb, cf_sk, noblock, timeo);
609 if (ret < 0)
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000610 /* skb is already freed */
611 return ret;
612
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000613 return len;
614err:
615 kfree_skb(skb);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000616 return ret;
617}
618
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000619/*
620 * Copied from unix_stream_sendmsg and adapted to CAIF:
621 * Changed removed permission handling and added waiting for flow on
622 * and other minor adaptations.
623 */
624static int caif_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
625 struct msghdr *msg, size_t len)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000626{
627 struct sock *sk = sock->sk;
628 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000629 int err, size;
630 struct sk_buff *skb;
631 int sent = 0;
632 long timeo;
633
634 err = -EOPNOTSUPP;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000635 if (unlikely(msg->msg_flags&MSG_OOB))
636 goto out_err;
637
638 if (unlikely(msg->msg_namelen))
639 goto out_err;
640
641 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
642 timeo = caif_wait_for_flow_on(cf_sk, 1, timeo, &err);
643
644 if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN))
645 goto pipe_err;
646
647 while (sent < len) {
648
649 size = len-sent;
650
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000651 if (size > cf_sk->maxframe)
652 size = cf_sk->maxframe;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000653
654 /* If size is more than half of sndbuf, chop up message */
655 if (size > ((sk->sk_sndbuf >> 1) - 64))
656 size = (sk->sk_sndbuf >> 1) - 64;
657
658 if (size > SKB_MAX_ALLOC)
659 size = SKB_MAX_ALLOC;
660
661 skb = sock_alloc_send_skb(sk,
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000662 size + cf_sk->headroom +
663 cf_sk->tailroom,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000664 msg->msg_flags&MSG_DONTWAIT,
665 &err);
666 if (skb == NULL)
667 goto out_err;
668
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000669 skb_reserve(skb, cf_sk->headroom);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000670 /*
671 * If you pass two values to the sock_alloc_send_skb
672 * it tries to grab the large buffer with GFP_NOFS
673 * (which can fail easily), and if it fails grab the
674 * fallback size buffer which is under a page and will
675 * succeed. [Alan]
676 */
677 size = min_t(int, size, skb_tailroom(skb));
678
679 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
680 if (err) {
681 kfree_skb(skb);
682 goto out_err;
683 }
684 err = transmit_skb(skb, cf_sk,
685 msg->msg_flags&MSG_DONTWAIT, timeo);
686 if (err < 0) {
687 kfree_skb(skb);
688 goto pipe_err;
689 }
690 sent += size;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000691 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000692
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000693 return sent;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000694
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000695pipe_err:
696 if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL))
697 send_sig(SIGPIPE, current, 0);
698 err = -EPIPE;
699out_err:
700 return sent ? : err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000701}
702
703static int setsockopt(struct socket *sock,
704 int lvl, int opt, char __user *ov, unsigned int ol)
705{
706 struct sock *sk = sock->sk;
707 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +0000708 int linksel;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000709
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000710 if (cf_sk->sk.sk_socket->state != SS_UNCONNECTED)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000711 return -ENOPROTOOPT;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000712
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000713 switch (opt) {
714 case CAIFSO_LINK_SELECT:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000715 if (ol < sizeof(int))
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000716 return -EINVAL;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000717 if (lvl != SOL_CAIF)
718 goto bad_sol;
719 if (copy_from_user(&linksel, ov, sizeof(int)))
720 return -EINVAL;
721 lock_sock(&(cf_sk->sk));
722 cf_sk->conn_req.link_selector = linksel;
723 release_sock(&cf_sk->sk);
724 return 0;
725
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000726 case CAIFSO_REQ_PARAM:
727 if (lvl != SOL_CAIF)
728 goto bad_sol;
729 if (cf_sk->sk.sk_protocol != CAIFPROTO_UTIL)
730 return -ENOPROTOOPT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000731 lock_sock(&(cf_sk->sk));
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000732 if (ol > sizeof(cf_sk->conn_req.param.data) ||
733 copy_from_user(&cf_sk->conn_req.param.data, ov, ol)) {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000734 release_sock(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000735 return -EINVAL;
736 }
Dan Rosenberg91b5c982011-01-10 16:00:54 -0800737 cf_sk->conn_req.param.size = ol;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000738 release_sock(&cf_sk->sk);
739 return 0;
740
741 default:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000742 return -ENOPROTOOPT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000743 }
744
745 return 0;
746bad_sol:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000747 return -ENOPROTOOPT;
748
749}
750
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000751/*
752 * caif_connect() - Connect a CAIF Socket
753 * Copied and modified af_irda.c:irda_connect().
754 *
755 * Note : by consulting "errno", the user space caller may learn the cause
756 * of the failure. Most of them are visible in the function, others may come
757 * from subroutines called and are listed here :
758 * o -EAFNOSUPPORT: bad socket family or type.
759 * o -ESOCKTNOSUPPORT: bad socket type or protocol
760 * o -EINVAL: bad socket address, or CAIF link type
761 * o -ECONNREFUSED: remote end refused the connection.
762 * o -EINPROGRESS: connect request sent but timed out (or non-blocking)
763 * o -EISCONN: already connected.
764 * o -ETIMEDOUT: Connection timed out (send timeout)
765 * o -ENODEV: No link layer to send request
766 * o -ECONNRESET: Received Shutdown indication or lost link layer
767 * o -ENOMEM: Out of memory
768 *
769 * State Strategy:
770 * o sk_state: holds the CAIF_* protocol state, it's updated by
771 * caif_ctrl_cb.
772 * o sock->state: holds the SS_* socket state and is updated by connect and
773 * disconnect.
774 */
775static int caif_connect(struct socket *sock, struct sockaddr *uaddr,
776 int addr_len, int flags)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000777{
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000778 struct sock *sk = sock->sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000779 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
780 long timeo;
781 int err;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000782 int ifindex, headroom, tailroom;
Eric Dumazet79315062010-10-04 22:42:08 +0000783 unsigned int mtu;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000784 struct net_device *dev;
785
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000786 lock_sock(sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000787
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000788 err = -EAFNOSUPPORT;
789 if (uaddr->sa_family != AF_CAIF)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000790 goto out;
791
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000792 switch (sock->state) {
793 case SS_UNCONNECTED:
794 /* Normal case, a fresh connect */
795 caif_assert(sk->sk_state == CAIF_DISCONNECTED);
796 break;
797 case SS_CONNECTING:
798 switch (sk->sk_state) {
799 case CAIF_CONNECTED:
800 sock->state = SS_CONNECTED;
801 err = -EISCONN;
802 goto out;
803 case CAIF_DISCONNECTED:
804 /* Reconnect allowed */
805 break;
806 case CAIF_CONNECTING:
807 err = -EALREADY;
808 if (flags & O_NONBLOCK)
809 goto out;
810 goto wait_connect;
811 }
812 break;
813 case SS_CONNECTED:
814 caif_assert(sk->sk_state == CAIF_CONNECTED ||
815 sk->sk_state == CAIF_DISCONNECTED);
816 if (sk->sk_shutdown & SHUTDOWN_MASK) {
817 /* Allow re-connect after SHUTDOWN_IND */
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000818 caif_disconnect_client(sock_net(sk), &cf_sk->layer);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000819 break;
820 }
821 /* No reconnect on a seqpacket socket */
822 err = -EISCONN;
823 goto out;
824 case SS_DISCONNECTING:
825 case SS_FREE:
826 caif_assert(1); /*Should never happen */
827 break;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000828 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000829 sk->sk_state = CAIF_DISCONNECTED;
830 sock->state = SS_UNCONNECTED;
831 sk_stream_kill_queues(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000832
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000833 err = -EINVAL;
Dan Carpenterf5d72af2010-05-22 10:43:42 +0000834 if (addr_len != sizeof(struct sockaddr_caif))
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000835 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000836
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000837 memcpy(&cf_sk->conn_req.sockaddr, uaddr,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000838 sizeof(struct sockaddr_caif));
839
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000840 /* Move to connecting socket, start sending Connect Requests */
841 sock->state = SS_CONNECTING;
842 sk->sk_state = CAIF_CONNECTING;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000843
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +0000844 /* Check priority value comming from socket */
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +0000845 /* if priority value is out of range it will be ajusted */
846 if (cf_sk->sk.sk_priority > CAIF_PRIO_MAX)
847 cf_sk->conn_req.priority = CAIF_PRIO_MAX;
848 else if (cf_sk->sk.sk_priority < CAIF_PRIO_MIN)
849 cf_sk->conn_req.priority = CAIF_PRIO_MIN;
850 else
851 cf_sk->conn_req.priority = cf_sk->sk.sk_priority;
852
853 /*ifindex = id of the interface.*/
854 cf_sk->conn_req.ifindex = cf_sk->sk.sk_bound_dev_if;
855
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000856 dbfs_atomic_inc(&cnt.num_connect_req);
857 cf_sk->layer.receive = caif_sktrecv_cb;
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000858
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000859 err = caif_connect_client(sock_net(sk), &cf_sk->conn_req,
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000860 &cf_sk->layer, &ifindex, &headroom, &tailroom);
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000861
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000862 if (err < 0) {
863 cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
864 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
865 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000866 }
Eric Dumazet79315062010-10-04 22:42:08 +0000867
868 err = -ENODEV;
869 rcu_read_lock();
870 dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
871 if (!dev) {
872 rcu_read_unlock();
873 goto out;
874 }
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000875 cf_sk->headroom = LL_RESERVED_SPACE_EXTRA(dev, headroom);
Eric Dumazet79315062010-10-04 22:42:08 +0000876 mtu = dev->mtu;
877 rcu_read_unlock();
878
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000879 cf_sk->tailroom = tailroom;
Eric Dumazet79315062010-10-04 22:42:08 +0000880 cf_sk->maxframe = mtu - (headroom + tailroom);
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000881 if (cf_sk->maxframe < 1) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000882 pr_warn("CAIF Interface MTU too small (%d)\n", dev->mtu);
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +0000883 err = -ENODEV;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000884 goto out;
885 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000886
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000887 err = -EINPROGRESS;
888wait_connect:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000889
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000890 if (sk->sk_state != CAIF_CONNECTED && (flags & O_NONBLOCK))
891 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000892
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000893 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000894
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000895 release_sock(sk);
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000896 err = -ERESTARTSYS;
897 timeo = wait_event_interruptible_timeout(*sk_sleep(sk),
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000898 sk->sk_state != CAIF_CONNECTING,
899 timeo);
900 lock_sock(sk);
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000901 if (timeo < 0)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000902 goto out; /* -ERESTARTSYS */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000903
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000904 err = -ETIMEDOUT;
905 if (timeo == 0 && sk->sk_state != CAIF_CONNECTED)
906 goto out;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000907 if (sk->sk_state != CAIF_CONNECTED) {
908 sock->state = SS_UNCONNECTED;
909 err = sock_error(sk);
910 if (!err)
911 err = -ECONNREFUSED;
912 goto out;
913 }
914 sock->state = SS_CONNECTED;
915 err = 0;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000916out:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000917 release_sock(sk);
918 return err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000919}
920
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000921/*
922 * caif_release() - Disconnect a CAIF Socket
923 * Copied and modified af_irda.c:irda_release().
924 */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000925static int caif_release(struct socket *sock)
926{
927 struct sock *sk = sock->sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000928 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
929 int res = 0;
930
931 if (!sk)
932 return 0;
933
934 set_tx_flow_off(cf_sk);
935
936 /*
937 * Ensure that packets are not queued after this point in time.
938 * caif_queue_rcv_skb checks SOCK_DEAD holding the queue lock,
939 * this ensures no packets when sock is dead.
940 */
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000941 spin_lock_bh(&sk->sk_receive_queue.lock);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000942 sock_set_flag(sk, SOCK_DEAD);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000943 spin_unlock_bh(&sk->sk_receive_queue.lock);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000944 sock->sk = NULL;
945
946 dbfs_atomic_inc(&cnt.num_disconnect);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000947
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +0000948 WARN_ON(IS_ERR(cf_sk->debugfs_socket_dir));
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000949 if (cf_sk->debugfs_socket_dir != NULL)
950 debugfs_remove_recursive(cf_sk->debugfs_socket_dir);
951
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000952 lock_sock(&(cf_sk->sk));
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000953 sk->sk_state = CAIF_DISCONNECTED;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000954 sk->sk_shutdown = SHUTDOWN_MASK;
955
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000956 if (cf_sk->sk.sk_socket->state == SS_CONNECTED ||
957 cf_sk->sk.sk_socket->state == SS_CONNECTING)
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000958 res = caif_disconnect_client(sock_net(sk), &cf_sk->layer);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000959
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000960 cf_sk->sk.sk_socket->state = SS_DISCONNECTING;
961 wake_up_interruptible_poll(sk_sleep(sk), POLLERR|POLLHUP);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000962
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000963 sock_orphan(sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000964 sk_stream_kill_queues(&cf_sk->sk);
965 release_sock(sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000966 sock_put(sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000967 return res;
968}
969
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000970/* Copied from af_unix.c:unix_poll(), added CAIF tx_flow handling */
971static unsigned int caif_poll(struct file *file,
972 struct socket *sock, poll_table *wait)
973{
974 struct sock *sk = sock->sk;
975 unsigned int mask;
976 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
977
978 sock_poll_wait(file, sk_sleep(sk), wait);
979 mask = 0;
980
981 /* exceptional events? */
982 if (sk->sk_err)
983 mask |= POLLERR;
984 if (sk->sk_shutdown == SHUTDOWN_MASK)
985 mask |= POLLHUP;
986 if (sk->sk_shutdown & RCV_SHUTDOWN)
987 mask |= POLLRDHUP;
988
989 /* readable? */
990 if (!skb_queue_empty(&sk->sk_receive_queue) ||
991 (sk->sk_shutdown & RCV_SHUTDOWN))
992 mask |= POLLIN | POLLRDNORM;
993
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000994 /*
995 * we set writable also when the other side has shut down the
996 * connection. This prevents stuck sockets.
997 */
998 if (sock_writeable(sk) && tx_flow_is_on(cf_sk))
999 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1000
1001 return mask;
1002}
1003
1004static const struct proto_ops caif_seqpacket_ops = {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001005 .family = PF_CAIF,
1006 .owner = THIS_MODULE,
1007 .release = caif_release,
1008 .bind = sock_no_bind,
1009 .connect = caif_connect,
1010 .socketpair = sock_no_socketpair,
1011 .accept = sock_no_accept,
1012 .getname = sock_no_getname,
1013 .poll = caif_poll,
1014 .ioctl = sock_no_ioctl,
1015 .listen = sock_no_listen,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001016 .shutdown = sock_no_shutdown,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001017 .setsockopt = setsockopt,
1018 .getsockopt = sock_no_getsockopt,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001019 .sendmsg = caif_seqpkt_sendmsg,
1020 .recvmsg = caif_seqpkt_recvmsg,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001021 .mmap = sock_no_mmap,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001022 .sendpage = sock_no_sendpage,
1023};
1024
1025static const struct proto_ops caif_stream_ops = {
1026 .family = PF_CAIF,
1027 .owner = THIS_MODULE,
1028 .release = caif_release,
1029 .bind = sock_no_bind,
1030 .connect = caif_connect,
1031 .socketpair = sock_no_socketpair,
1032 .accept = sock_no_accept,
1033 .getname = sock_no_getname,
1034 .poll = caif_poll,
1035 .ioctl = sock_no_ioctl,
1036 .listen = sock_no_listen,
1037 .shutdown = sock_no_shutdown,
1038 .setsockopt = setsockopt,
1039 .getsockopt = sock_no_getsockopt,
1040 .sendmsg = caif_stream_sendmsg,
1041 .recvmsg = caif_stream_recvmsg,
1042 .mmap = sock_no_mmap,
1043 .sendpage = sock_no_sendpage,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001044};
1045
1046/* This function is called when a socket is finally destroyed. */
1047static void caif_sock_destructor(struct sock *sk)
1048{
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001049 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001050 caif_assert(!atomic_read(&sk->sk_wmem_alloc));
1051 caif_assert(sk_unhashed(sk));
1052 caif_assert(!sk->sk_socket);
1053 if (!sock_flag(sk, SOCK_DEAD)) {
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +00001054 pr_debug("Attempt to release alive CAIF socket: %p\n", sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001055 return;
1056 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001057 sk_stream_kill_queues(&cf_sk->sk);
1058 dbfs_atomic_dec(&cnt.caif_nr_socks);
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +00001059 caif_free_client(&cf_sk->layer);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001060}
1061
1062static int caif_create(struct net *net, struct socket *sock, int protocol,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001063 int kern)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001064{
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +00001065 int num;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001066 struct sock *sk = NULL;
1067 struct caifsock *cf_sk = NULL;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001068 static struct proto prot = {.name = "PF_CAIF",
1069 .owner = THIS_MODULE,
1070 .obj_size = sizeof(struct caifsock),
1071 };
1072
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001073 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_NET_ADMIN))
1074 return -EPERM;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001075 /*
1076 * The sock->type specifies the socket type to use.
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001077 * The CAIF socket is a packet stream in the sense
1078 * that it is packet based. CAIF trusts the reliability
1079 * of the link, no resending is implemented.
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001080 */
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001081 if (sock->type == SOCK_SEQPACKET)
1082 sock->ops = &caif_seqpacket_ops;
1083 else if (sock->type == SOCK_STREAM)
1084 sock->ops = &caif_stream_ops;
1085 else
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001086 return -ESOCKTNOSUPPORT;
1087
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001088 if (protocol < 0 || protocol >= CAIFPROTO_MAX)
1089 return -EPROTONOSUPPORT;
1090 /*
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001091 * Set the socket state to unconnected. The socket state
1092 * is really not used at all in the net/core or socket.c but the
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001093 * initialization makes sure that sock->state is not uninitialized.
1094 */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001095 sk = sk_alloc(net, PF_CAIF, GFP_KERNEL, &prot);
1096 if (!sk)
1097 return -ENOMEM;
1098
1099 cf_sk = container_of(sk, struct caifsock, sk);
1100
1101 /* Store the protocol */
1102 sk->sk_protocol = (unsigned char) protocol;
1103
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001104 /*
1105 * Lock in order to try to stop someone from opening the socket
1106 * too early.
1107 */
1108 lock_sock(&(cf_sk->sk));
1109
1110 /* Initialize the nozero default sock structure data. */
1111 sock_init_data(sock, sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001112 sk->sk_destruct = caif_sock_destructor;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001113
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001114 mutex_init(&cf_sk->readlock); /* single task reading lock */
1115 cf_sk->layer.ctrlcmd = caif_ctrl_cb;
1116 cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
1117 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001118
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001119 set_tx_flow_off(cf_sk);
1120 set_rx_flow_on(cf_sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001121
1122 /* Set default options on configuration */
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +00001123 cf_sk->sk.sk_priority= CAIF_PRIO_NORMAL;
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001124 cf_sk->conn_req.link_selector = CAIF_LINK_LOW_LATENCY;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001125 cf_sk->conn_req.protocol = protocol;
1126 /* Increase the number of sockets created. */
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001127 dbfs_atomic_inc(&cnt.caif_nr_socks);
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +00001128 num = dbfs_atomic_inc(&cnt.caif_sock_create);
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001129#ifdef CONFIG_DEBUG_FS
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001130 if (!IS_ERR(debugfsdir)) {
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +00001131
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001132 /* Fill in some information concerning the misc socket. */
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +00001133 snprintf(cf_sk->name, sizeof(cf_sk->name), "cfsk%d", num);
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001134
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001135 cf_sk->debugfs_socket_dir =
1136 debugfs_create_dir(cf_sk->name, debugfsdir);
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +00001137
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001138 debugfs_create_u32("sk_state", S_IRUSR | S_IWUSR,
1139 cf_sk->debugfs_socket_dir,
1140 (u32 *) &cf_sk->sk.sk_state);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001141 debugfs_create_u32("flow_state", S_IRUSR | S_IWUSR,
1142 cf_sk->debugfs_socket_dir, &cf_sk->flow_state);
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001143 debugfs_create_u32("sk_rmem_alloc", S_IRUSR | S_IWUSR,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001144 cf_sk->debugfs_socket_dir,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001145 (u32 *) &cf_sk->sk.sk_rmem_alloc);
1146 debugfs_create_u32("sk_wmem_alloc", S_IRUSR | S_IWUSR,
1147 cf_sk->debugfs_socket_dir,
1148 (u32 *) &cf_sk->sk.sk_wmem_alloc);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001149 debugfs_create_u32("identity", S_IRUSR | S_IWUSR,
1150 cf_sk->debugfs_socket_dir,
1151 (u32 *) &cf_sk->layer.id);
1152 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001153#endif
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001154 release_sock(&cf_sk->sk);
1155 return 0;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001156}
1157
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001158
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001159static struct net_proto_family caif_family_ops = {
1160 .family = PF_CAIF,
1161 .create = caif_create,
1162 .owner = THIS_MODULE,
1163};
1164
Sjur Braendelanda9a8f102010-05-21 02:16:11 +00001165static int af_caif_init(void)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001166{
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001167 int err = sock_register(&caif_family_ops);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001168 if (!err)
1169 return err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001170 return 0;
1171}
1172
1173static int __init caif_sktinit_module(void)
1174{
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001175#ifdef CONFIG_DEBUG_FS
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001176 debugfsdir = debugfs_create_dir("caif_sk", NULL);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001177 if (!IS_ERR(debugfsdir)) {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001178 debugfs_create_u32("num_sockets", S_IRUSR | S_IWUSR,
1179 debugfsdir,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001180 (u32 *) &cnt.caif_nr_socks);
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +00001181 debugfs_create_u32("num_create", S_IRUSR | S_IWUSR,
1182 debugfsdir,
1183 (u32 *) &cnt.caif_sock_create);
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001184 debugfs_create_u32("num_connect_req", S_IRUSR | S_IWUSR,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001185 debugfsdir,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001186 (u32 *) &cnt.num_connect_req);
1187 debugfs_create_u32("num_connect_resp", S_IRUSR | S_IWUSR,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001188 debugfsdir,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001189 (u32 *) &cnt.num_connect_resp);
1190 debugfs_create_u32("num_connect_fail_resp", S_IRUSR | S_IWUSR,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001191 debugfsdir,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001192 (u32 *) &cnt.num_connect_fail_resp);
1193 debugfs_create_u32("num_disconnect", S_IRUSR | S_IWUSR,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001194 debugfsdir,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001195 (u32 *) &cnt.num_disconnect);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001196 debugfs_create_u32("num_remote_shutdown_ind",
1197 S_IRUSR | S_IWUSR, debugfsdir,
1198 (u32 *) &cnt.num_remote_shutdown_ind);
1199 debugfs_create_u32("num_tx_flow_off_ind", S_IRUSR | S_IWUSR,
1200 debugfsdir,
1201 (u32 *) &cnt.num_tx_flow_off_ind);
1202 debugfs_create_u32("num_tx_flow_on_ind", S_IRUSR | S_IWUSR,
1203 debugfsdir,
1204 (u32 *) &cnt.num_tx_flow_on_ind);
1205 debugfs_create_u32("num_rx_flow_off", S_IRUSR | S_IWUSR,
1206 debugfsdir,
1207 (u32 *) &cnt.num_rx_flow_off);
1208 debugfs_create_u32("num_rx_flow_on", S_IRUSR | S_IWUSR,
1209 debugfsdir,
1210 (u32 *) &cnt.num_rx_flow_on);
1211 }
1212#endif
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001213 return af_caif_init();
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001214}
1215
1216static void __exit caif_sktexit_module(void)
1217{
1218 sock_unregister(PF_CAIF);
1219 if (debugfsdir != NULL)
1220 debugfs_remove_recursive(debugfsdir);
1221}
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001222module_init(caif_sktinit_module);
1223module_exit(caif_sktexit_module);