blob: 653db759eb7e873037066327309c0dd35d098369 [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;
51 atomic_t num_connect_req;
52 atomic_t num_connect_resp;
53 atomic_t num_connect_fail_resp;
54 atomic_t num_disconnect;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000055 atomic_t num_remote_shutdown_ind;
56 atomic_t num_tx_flow_off_ind;
57 atomic_t num_tx_flow_on_ind;
58 atomic_t num_rx_flow_off;
59 atomic_t num_rx_flow_on;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000060};
Sjur Braendelanda9a8f102010-05-21 02:16:11 +000061static struct debug_fs_counter cnt;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000062#define dbfs_atomic_inc(v) atomic_inc(v)
63#define dbfs_atomic_dec(v) atomic_dec(v)
64#else
65#define dbfs_atomic_inc(v)
66#define dbfs_atomic_dec(v)
67#endif
68
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000069struct caifsock {
Sjur Braendelandbece7b22010-04-28 08:54:38 +000070 struct sock sk; /* must be first member */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000071 struct cflayer layer;
Sjur Braendelandbece7b22010-04-28 08:54:38 +000072 char name[CAIF_LAYER_NAME_SZ]; /* Used for debugging */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000073 u32 flow_state;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000074 struct caif_connect_request conn_req;
Sjur Braendelandbece7b22010-04-28 08:54:38 +000075 struct mutex readlock;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000076 struct dentry *debugfs_socket_dir;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +000077 int headroom, tailroom, maxframe;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000078};
79
Sjur Braendelandbece7b22010-04-28 08:54:38 +000080static int rx_flow_is_on(struct caifsock *cf_sk)
81{
82 return test_bit(RX_FLOW_ON_BIT,
83 (void *) &cf_sk->flow_state);
84}
85
86static int tx_flow_is_on(struct caifsock *cf_sk)
87{
88 return test_bit(TX_FLOW_ON_BIT,
89 (void *) &cf_sk->flow_state);
90}
91
92static void set_rx_flow_off(struct caifsock *cf_sk)
93{
94 clear_bit(RX_FLOW_ON_BIT,
95 (void *) &cf_sk->flow_state);
96}
97
98static void set_rx_flow_on(struct caifsock *cf_sk)
99{
100 set_bit(RX_FLOW_ON_BIT,
101 (void *) &cf_sk->flow_state);
102}
103
104static void set_tx_flow_off(struct caifsock *cf_sk)
105{
106 clear_bit(TX_FLOW_ON_BIT,
107 (void *) &cf_sk->flow_state);
108}
109
110static void set_tx_flow_on(struct caifsock *cf_sk)
111{
112 set_bit(TX_FLOW_ON_BIT,
113 (void *) &cf_sk->flow_state);
114}
115
116static void caif_read_lock(struct sock *sk)
117{
118 struct caifsock *cf_sk;
119 cf_sk = container_of(sk, struct caifsock, sk);
120 mutex_lock(&cf_sk->readlock);
121}
122
123static void caif_read_unlock(struct sock *sk)
124{
125 struct caifsock *cf_sk;
126 cf_sk = container_of(sk, struct caifsock, sk);
127 mutex_unlock(&cf_sk->readlock);
128}
129
Sjur Braendelanda9a8f102010-05-21 02:16:11 +0000130static int sk_rcvbuf_lowwater(struct caifsock *cf_sk)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000131{
132 /* A quarter of full buffer is used a low water mark */
133 return cf_sk->sk.sk_rcvbuf / 4;
134}
135
Sjur Braendelanda9a8f102010-05-21 02:16:11 +0000136static void caif_flow_ctrl(struct sock *sk, int mode)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000137{
138 struct caifsock *cf_sk;
139 cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000140 if (cf_sk->layer.dn && cf_sk->layer.dn->modemcmd)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000141 cf_sk->layer.dn->modemcmd(cf_sk->layer.dn, mode);
142}
143
144/*
145 * Copied from sock.c:sock_queue_rcv_skb(), but changed so packets are
146 * not dropped, but CAIF is sending flow off instead.
147 */
Sjur Braendelanda9a8f102010-05-21 02:16:11 +0000148static int caif_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000149{
150 int err;
151 int skb_len;
152 unsigned long flags;
153 struct sk_buff_head *list = &sk->sk_receive_queue;
154 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
155
156 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
157 (unsigned)sk->sk_rcvbuf && rx_flow_is_on(cf_sk)) {
Sjur Braendelandb04367d2010-09-21 11:44:44 +0000158 pr_debug("sending flow OFF (queue len = %d %d)\n",
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000159 atomic_read(&cf_sk->sk.sk_rmem_alloc),
160 sk_rcvbuf_lowwater(cf_sk));
161 set_rx_flow_off(cf_sk);
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000162 dbfs_atomic_inc(&cnt.num_rx_flow_off);
163 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_OFF_REQ);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000164 }
165
166 err = sk_filter(sk, skb);
167 if (err)
168 return err;
169 if (!sk_rmem_schedule(sk, skb->truesize) && rx_flow_is_on(cf_sk)) {
170 set_rx_flow_off(cf_sk);
Sjur Braendelandb04367d2010-09-21 11:44:44 +0000171 pr_debug("sending flow OFF due to rmem_schedule\n");
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000172 dbfs_atomic_inc(&cnt.num_rx_flow_off);
173 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_OFF_REQ);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000174 }
175 skb->dev = NULL;
176 skb_set_owner_r(skb, sk);
177 /* Cache the SKB length before we tack it onto the receive
178 * queue. Once it is added it no longer belongs to us and
179 * may be freed by other threads of control pulling packets
180 * from the queue.
181 */
182 skb_len = skb->len;
183 spin_lock_irqsave(&list->lock, flags);
184 if (!sock_flag(sk, SOCK_DEAD))
185 __skb_queue_tail(list, skb);
186 spin_unlock_irqrestore(&list->lock, flags);
187
188 if (!sock_flag(sk, SOCK_DEAD))
189 sk->sk_data_ready(sk, skb_len);
190 else
191 kfree_skb(skb);
192 return 0;
193}
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000194
195/* Packet Receive Callback function called from CAIF Stack */
196static int caif_sktrecv_cb(struct cflayer *layr, struct cfpkt *pkt)
197{
198 struct caifsock *cf_sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000199 struct sk_buff *skb;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000200
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000201 cf_sk = container_of(layr, struct caifsock, layer);
202 skb = cfpkt_tonative(pkt);
203
204 if (unlikely(cf_sk->sk.sk_state != CAIF_CONNECTED)) {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000205 cfpkt_destroy(pkt);
206 return 0;
207 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000208 caif_queue_rcv_skb(&cf_sk->sk, skb);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000209 return 0;
210}
211
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000212static void cfsk_hold(struct cflayer *layr)
213{
214 struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
215 sock_hold(&cf_sk->sk);
216}
217
218static void cfsk_put(struct cflayer *layr)
219{
220 struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
221 sock_put(&cf_sk->sk);
222}
223
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000224/* Packet Control Callback function called from CAIF */
225static void caif_ctrl_cb(struct cflayer *layr,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000226 enum caif_ctrlcmd flow,
227 int phyid)
228{
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000229 struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000230 switch (flow) {
231 case CAIF_CTRLCMD_FLOW_ON_IND:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000232 /* OK from modem to start sending again */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000233 dbfs_atomic_inc(&cnt.num_tx_flow_on_ind);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000234 set_tx_flow_on(cf_sk);
235 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000236 break;
237
238 case CAIF_CTRLCMD_FLOW_OFF_IND:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000239 /* Modem asks us to shut up */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000240 dbfs_atomic_inc(&cnt.num_tx_flow_off_ind);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000241 set_tx_flow_off(cf_sk);
242 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000243 break;
244
245 case CAIF_CTRLCMD_INIT_RSP:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000246 /* We're now connected */
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000247 caif_client_register_refcnt(&cf_sk->layer,
248 cfsk_hold, cfsk_put);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000249 dbfs_atomic_inc(&cnt.num_connect_resp);
250 cf_sk->sk.sk_state = CAIF_CONNECTED;
251 set_tx_flow_on(cf_sk);
252 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000253 break;
254
255 case CAIF_CTRLCMD_DEINIT_RSP:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000256 /* We're now disconnected */
257 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
258 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000259 break;
260
261 case CAIF_CTRLCMD_INIT_FAIL_RSP:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000262 /* Connect request failed */
263 dbfs_atomic_inc(&cnt.num_connect_fail_resp);
264 cf_sk->sk.sk_err = ECONNREFUSED;
265 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
266 cf_sk->sk.sk_shutdown = SHUTDOWN_MASK;
267 /*
268 * Socket "standards" seems to require POLLOUT to
269 * be set at connect failure.
270 */
271 set_tx_flow_on(cf_sk);
272 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000273 break;
274
275 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000276 /* Modem has closed this connection, or device is down. */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000277 dbfs_atomic_inc(&cnt.num_remote_shutdown_ind);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000278 cf_sk->sk.sk_shutdown = SHUTDOWN_MASK;
279 cf_sk->sk.sk_err = ECONNRESET;
280 set_rx_flow_on(cf_sk);
281 cf_sk->sk.sk_error_report(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000282 break;
283
284 default:
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000285 pr_debug("Unexpected flow command %d\n", flow);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000286 }
287}
288
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000289static void caif_check_flow_release(struct sock *sk)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000290{
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000291 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
292
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000293 if (rx_flow_is_on(cf_sk))
294 return;
295
296 if (atomic_read(&sk->sk_rmem_alloc) <= sk_rcvbuf_lowwater(cf_sk)) {
297 dbfs_atomic_inc(&cnt.num_rx_flow_on);
298 set_rx_flow_on(cf_sk);
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000299 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_ON_REQ);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000300 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000301}
302
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000303/*
304 * Copied from unix_dgram_recvmsg, but removed credit checks,
305 * changed locking, address handling and added MSG_TRUNC.
306 */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000307static int caif_seqpkt_recvmsg(struct kiocb *iocb, struct socket *sock,
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000308 struct msghdr *m, size_t len, int flags)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000309
310{
311 struct sock *sk = sock->sk;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000312 struct sk_buff *skb;
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000313 int ret;
314 int copylen;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000315
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000316 ret = -EOPNOTSUPP;
317 if (m->msg_flags&MSG_OOB)
318 goto read_error;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000319
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000320 skb = skb_recv_datagram(sk, flags, 0 , &ret);
321 if (!skb)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000322 goto read_error;
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000323 copylen = skb->len;
324 if (len < copylen) {
325 m->msg_flags |= MSG_TRUNC;
326 copylen = len;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000327 }
328
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000329 ret = skb_copy_datagram_iovec(skb, 0, m->msg_iov, copylen);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000330 if (ret)
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000331 goto out_free;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000332
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000333 ret = (flags & MSG_TRUNC) ? skb->len : copylen;
334out_free:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000335 skb_free_datagram(sk, skb);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000336 caif_check_flow_release(sk);
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000337 return ret;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000338
339read_error:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000340 return ret;
341}
342
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000343
344/* Copied from unix_stream_wait_data, identical except for lock call. */
345static long caif_stream_data_wait(struct sock *sk, long timeo)
346{
347 DEFINE_WAIT(wait);
348 lock_sock(sk);
349
350 for (;;) {
351 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
352
353 if (!skb_queue_empty(&sk->sk_receive_queue) ||
354 sk->sk_err ||
355 sk->sk_state != CAIF_CONNECTED ||
356 sock_flag(sk, SOCK_DEAD) ||
357 (sk->sk_shutdown & RCV_SHUTDOWN) ||
358 signal_pending(current) ||
359 !timeo)
360 break;
361
362 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
363 release_sock(sk);
364 timeo = schedule_timeout(timeo);
365 lock_sock(sk);
366 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
367 }
368
369 finish_wait(sk_sleep(sk), &wait);
370 release_sock(sk);
371 return timeo;
372}
373
374
375/*
376 * Copied from unix_stream_recvmsg, but removed credit checks,
377 * changed locking calls, changed address handling.
378 */
379static int caif_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
380 struct msghdr *msg, size_t size,
381 int flags)
382{
383 struct sock *sk = sock->sk;
384 int copied = 0;
385 int target;
386 int err = 0;
387 long timeo;
388
389 err = -EOPNOTSUPP;
390 if (flags&MSG_OOB)
391 goto out;
392
393 msg->msg_namelen = 0;
394
395 /*
396 * Lock the socket to prevent queue disordering
397 * while sleeps in memcpy_tomsg
398 */
399 err = -EAGAIN;
400 if (sk->sk_state == CAIF_CONNECTING)
401 goto out;
402
403 caif_read_lock(sk);
404 target = sock_rcvlowat(sk, flags&MSG_WAITALL, size);
405 timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
406
407 do {
408 int chunk;
409 struct sk_buff *skb;
410
411 lock_sock(sk);
412 skb = skb_dequeue(&sk->sk_receive_queue);
413 caif_check_flow_release(sk);
414
415 if (skb == NULL) {
416 if (copied >= target)
417 goto unlock;
418 /*
419 * POSIX 1003.1g mandates this order.
420 */
421 err = sock_error(sk);
422 if (err)
423 goto unlock;
424 err = -ECONNRESET;
425 if (sk->sk_shutdown & RCV_SHUTDOWN)
426 goto unlock;
427
428 err = -EPIPE;
429 if (sk->sk_state != CAIF_CONNECTED)
430 goto unlock;
431 if (sock_flag(sk, SOCK_DEAD))
432 goto unlock;
433
434 release_sock(sk);
435
436 err = -EAGAIN;
437 if (!timeo)
438 break;
439
440 caif_read_unlock(sk);
441
442 timeo = caif_stream_data_wait(sk, timeo);
443
444 if (signal_pending(current)) {
445 err = sock_intr_errno(timeo);
446 goto out;
447 }
448 caif_read_lock(sk);
449 continue;
450unlock:
451 release_sock(sk);
452 break;
453 }
454 release_sock(sk);
455 chunk = min_t(unsigned int, skb->len, size);
456 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
457 skb_queue_head(&sk->sk_receive_queue, skb);
458 if (copied == 0)
459 copied = -EFAULT;
460 break;
461 }
462 copied += chunk;
463 size -= chunk;
464
465 /* Mark read part of skb as used */
466 if (!(flags & MSG_PEEK)) {
467 skb_pull(skb, chunk);
468
469 /* put the skb back if we didn't use it up. */
470 if (skb->len) {
471 skb_queue_head(&sk->sk_receive_queue, skb);
472 break;
473 }
474 kfree_skb(skb);
475
476 } else {
477 /*
478 * It is questionable, see note in unix_dgram_recvmsg.
479 */
480 /* put message back and return */
481 skb_queue_head(&sk->sk_receive_queue, skb);
482 break;
483 }
484 } while (size);
485 caif_read_unlock(sk);
486
487out:
488 return copied ? : err;
489}
490
491/*
492 * Copied from sock.c:sock_wait_for_wmem, but change to wait for
493 * CAIF flow-on and sock_writable.
494 */
495static long caif_wait_for_flow_on(struct caifsock *cf_sk,
496 int wait_writeable, long timeo, int *err)
497{
498 struct sock *sk = &cf_sk->sk;
499 DEFINE_WAIT(wait);
500 for (;;) {
501 *err = 0;
502 if (tx_flow_is_on(cf_sk) &&
503 (!wait_writeable || sock_writeable(&cf_sk->sk)))
504 break;
505 *err = -ETIMEDOUT;
506 if (!timeo)
507 break;
508 *err = -ERESTARTSYS;
509 if (signal_pending(current))
510 break;
511 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
512 *err = -ECONNRESET;
513 if (sk->sk_shutdown & SHUTDOWN_MASK)
514 break;
515 *err = -sk->sk_err;
516 if (sk->sk_err)
517 break;
518 *err = -EPIPE;
519 if (cf_sk->sk.sk_state != CAIF_CONNECTED)
520 break;
521 timeo = schedule_timeout(timeo);
522 }
523 finish_wait(sk_sleep(sk), &wait);
524 return timeo;
525}
526
527/*
528 * Transmit a SKB. The device may temporarily request re-transmission
529 * by returning EAGAIN.
530 */
531static int transmit_skb(struct sk_buff *skb, struct caifsock *cf_sk,
532 int noblock, long timeo)
533{
534 struct cfpkt *pkt;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000535
536 pkt = cfpkt_fromnative(CAIF_DIR_OUT, skb);
537 memset(cfpkt_info(pkt), 0, sizeof(struct caif_payload_info));
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000538
Sjur Brændeland4dd820c2011-04-11 10:43:51 +0000539 if (cf_sk->layer.dn == NULL)
540 return -EINVAL;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000541
Sjur Brændeland4dd820c2011-04-11 10:43:51 +0000542 return cf_sk->layer.dn->transmit(cf_sk->layer.dn, pkt);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000543}
544
545/* Copied from af_unix:unix_dgram_sendmsg, and adapted to CAIF */
546static int caif_seqpkt_sendmsg(struct kiocb *kiocb, struct socket *sock,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000547 struct msghdr *msg, size_t len)
548{
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000549 struct sock *sk = sock->sk;
550 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000551 int buffer_size;
552 int ret = 0;
553 struct sk_buff *skb = NULL;
554 int noblock;
555 long timeo;
556 caif_assert(cf_sk);
557 ret = sock_error(sk);
558 if (ret)
559 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000560
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000561 ret = -EOPNOTSUPP;
562 if (msg->msg_flags&MSG_OOB)
563 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000564
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000565 ret = -EOPNOTSUPP;
566 if (msg->msg_namelen)
567 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000568
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000569 ret = -EINVAL;
570 if (unlikely(msg->msg_iov->iov_base == NULL))
571 goto err;
572 noblock = msg->msg_flags & MSG_DONTWAIT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000573
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000574 timeo = sock_sndtimeo(sk, noblock);
575 timeo = caif_wait_for_flow_on(container_of(sk, struct caifsock, sk),
576 1, timeo, &ret);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000577
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000578 if (ret)
579 goto err;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000580 ret = -EPIPE;
581 if (cf_sk->sk.sk_state != CAIF_CONNECTED ||
582 sock_flag(sk, SOCK_DEAD) ||
583 (sk->sk_shutdown & RCV_SHUTDOWN))
584 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000585
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000586 /* Error if trying to write more than maximum frame size. */
587 ret = -EMSGSIZE;
588 if (len > cf_sk->maxframe && cf_sk->sk.sk_protocol != CAIFPROTO_RFM)
589 goto err;
590
591 buffer_size = len + cf_sk->headroom + cf_sk->tailroom;
592
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000593 ret = -ENOMEM;
594 skb = sock_alloc_send_skb(sk, buffer_size, noblock, &ret);
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000595
596 if (!skb || skb_tailroom(skb) < buffer_size)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000597 goto err;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000598
599 skb_reserve(skb, cf_sk->headroom);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000600
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000601 ret = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000602
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000603 if (ret)
604 goto err;
605 ret = transmit_skb(skb, cf_sk, noblock, timeo);
606 if (ret < 0)
607 goto err;
608 return len;
609err:
610 kfree_skb(skb);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000611 return ret;
612}
613
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000614/*
615 * Copied from unix_stream_sendmsg and adapted to CAIF:
616 * Changed removed permission handling and added waiting for flow on
617 * and other minor adaptations.
618 */
619static int caif_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
620 struct msghdr *msg, size_t len)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000621{
622 struct sock *sk = sock->sk;
623 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000624 int err, size;
625 struct sk_buff *skb;
626 int sent = 0;
627 long timeo;
628
629 err = -EOPNOTSUPP;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000630 if (unlikely(msg->msg_flags&MSG_OOB))
631 goto out_err;
632
633 if (unlikely(msg->msg_namelen))
634 goto out_err;
635
636 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
637 timeo = caif_wait_for_flow_on(cf_sk, 1, timeo, &err);
638
639 if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN))
640 goto pipe_err;
641
642 while (sent < len) {
643
644 size = len-sent;
645
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000646 if (size > cf_sk->maxframe)
647 size = cf_sk->maxframe;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000648
649 /* If size is more than half of sndbuf, chop up message */
650 if (size > ((sk->sk_sndbuf >> 1) - 64))
651 size = (sk->sk_sndbuf >> 1) - 64;
652
653 if (size > SKB_MAX_ALLOC)
654 size = SKB_MAX_ALLOC;
655
656 skb = sock_alloc_send_skb(sk,
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000657 size + cf_sk->headroom +
658 cf_sk->tailroom,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000659 msg->msg_flags&MSG_DONTWAIT,
660 &err);
661 if (skb == NULL)
662 goto out_err;
663
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000664 skb_reserve(skb, cf_sk->headroom);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000665 /*
666 * If you pass two values to the sock_alloc_send_skb
667 * it tries to grab the large buffer with GFP_NOFS
668 * (which can fail easily), and if it fails grab the
669 * fallback size buffer which is under a page and will
670 * succeed. [Alan]
671 */
672 size = min_t(int, size, skb_tailroom(skb));
673
674 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
675 if (err) {
676 kfree_skb(skb);
677 goto out_err;
678 }
679 err = transmit_skb(skb, cf_sk,
680 msg->msg_flags&MSG_DONTWAIT, timeo);
681 if (err < 0) {
682 kfree_skb(skb);
683 goto pipe_err;
684 }
685 sent += size;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000686 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000687
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000688 return sent;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000689
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000690pipe_err:
691 if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL))
692 send_sig(SIGPIPE, current, 0);
693 err = -EPIPE;
694out_err:
695 return sent ? : err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000696}
697
698static int setsockopt(struct socket *sock,
699 int lvl, int opt, char __user *ov, unsigned int ol)
700{
701 struct sock *sk = sock->sk;
702 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +0000703 int linksel;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000704
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000705 if (cf_sk->sk.sk_socket->state != SS_UNCONNECTED)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000706 return -ENOPROTOOPT;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000707
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000708 switch (opt) {
709 case CAIFSO_LINK_SELECT:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000710 if (ol < sizeof(int))
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000711 return -EINVAL;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000712 if (lvl != SOL_CAIF)
713 goto bad_sol;
714 if (copy_from_user(&linksel, ov, sizeof(int)))
715 return -EINVAL;
716 lock_sock(&(cf_sk->sk));
717 cf_sk->conn_req.link_selector = linksel;
718 release_sock(&cf_sk->sk);
719 return 0;
720
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000721 case CAIFSO_REQ_PARAM:
722 if (lvl != SOL_CAIF)
723 goto bad_sol;
724 if (cf_sk->sk.sk_protocol != CAIFPROTO_UTIL)
725 return -ENOPROTOOPT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000726 lock_sock(&(cf_sk->sk));
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000727 if (ol > sizeof(cf_sk->conn_req.param.data) ||
728 copy_from_user(&cf_sk->conn_req.param.data, ov, ol)) {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000729 release_sock(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000730 return -EINVAL;
731 }
Dan Rosenberg91b5c982011-01-10 16:00:54 -0800732 cf_sk->conn_req.param.size = ol;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000733 release_sock(&cf_sk->sk);
734 return 0;
735
736 default:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000737 return -ENOPROTOOPT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000738 }
739
740 return 0;
741bad_sol:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000742 return -ENOPROTOOPT;
743
744}
745
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000746/*
747 * caif_connect() - Connect a CAIF Socket
748 * Copied and modified af_irda.c:irda_connect().
749 *
750 * Note : by consulting "errno", the user space caller may learn the cause
751 * of the failure. Most of them are visible in the function, others may come
752 * from subroutines called and are listed here :
753 * o -EAFNOSUPPORT: bad socket family or type.
754 * o -ESOCKTNOSUPPORT: bad socket type or protocol
755 * o -EINVAL: bad socket address, or CAIF link type
756 * o -ECONNREFUSED: remote end refused the connection.
757 * o -EINPROGRESS: connect request sent but timed out (or non-blocking)
758 * o -EISCONN: already connected.
759 * o -ETIMEDOUT: Connection timed out (send timeout)
760 * o -ENODEV: No link layer to send request
761 * o -ECONNRESET: Received Shutdown indication or lost link layer
762 * o -ENOMEM: Out of memory
763 *
764 * State Strategy:
765 * o sk_state: holds the CAIF_* protocol state, it's updated by
766 * caif_ctrl_cb.
767 * o sock->state: holds the SS_* socket state and is updated by connect and
768 * disconnect.
769 */
770static int caif_connect(struct socket *sock, struct sockaddr *uaddr,
771 int addr_len, int flags)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000772{
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000773 struct sock *sk = sock->sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000774 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
775 long timeo;
776 int err;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000777 int ifindex, headroom, tailroom;
Eric Dumazet79315062010-10-04 22:42:08 +0000778 unsigned int mtu;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000779 struct net_device *dev;
780
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000781 lock_sock(sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000782
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000783 err = -EAFNOSUPPORT;
784 if (uaddr->sa_family != AF_CAIF)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000785 goto out;
786
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000787 switch (sock->state) {
788 case SS_UNCONNECTED:
789 /* Normal case, a fresh connect */
790 caif_assert(sk->sk_state == CAIF_DISCONNECTED);
791 break;
792 case SS_CONNECTING:
793 switch (sk->sk_state) {
794 case CAIF_CONNECTED:
795 sock->state = SS_CONNECTED;
796 err = -EISCONN;
797 goto out;
798 case CAIF_DISCONNECTED:
799 /* Reconnect allowed */
800 break;
801 case CAIF_CONNECTING:
802 err = -EALREADY;
803 if (flags & O_NONBLOCK)
804 goto out;
805 goto wait_connect;
806 }
807 break;
808 case SS_CONNECTED:
809 caif_assert(sk->sk_state == CAIF_CONNECTED ||
810 sk->sk_state == CAIF_DISCONNECTED);
811 if (sk->sk_shutdown & SHUTDOWN_MASK) {
812 /* Allow re-connect after SHUTDOWN_IND */
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000813 caif_disconnect_client(sock_net(sk), &cf_sk->layer);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000814 break;
815 }
816 /* No reconnect on a seqpacket socket */
817 err = -EISCONN;
818 goto out;
819 case SS_DISCONNECTING:
820 case SS_FREE:
821 caif_assert(1); /*Should never happen */
822 break;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000823 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000824 sk->sk_state = CAIF_DISCONNECTED;
825 sock->state = SS_UNCONNECTED;
826 sk_stream_kill_queues(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000827
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000828 err = -EINVAL;
Dan Carpenterf5d72af2010-05-22 10:43:42 +0000829 if (addr_len != sizeof(struct sockaddr_caif))
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000830 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000831
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000832 memcpy(&cf_sk->conn_req.sockaddr, uaddr,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000833 sizeof(struct sockaddr_caif));
834
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000835 /* Move to connecting socket, start sending Connect Requests */
836 sock->state = SS_CONNECTING;
837 sk->sk_state = CAIF_CONNECTING;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000838
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300839 /* Check priority value coming from socket */
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +0000840 /* if priority value is out of range it will be ajusted */
841 if (cf_sk->sk.sk_priority > CAIF_PRIO_MAX)
842 cf_sk->conn_req.priority = CAIF_PRIO_MAX;
843 else if (cf_sk->sk.sk_priority < CAIF_PRIO_MIN)
844 cf_sk->conn_req.priority = CAIF_PRIO_MIN;
845 else
846 cf_sk->conn_req.priority = cf_sk->sk.sk_priority;
847
848 /*ifindex = id of the interface.*/
849 cf_sk->conn_req.ifindex = cf_sk->sk.sk_bound_dev_if;
850
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000851 dbfs_atomic_inc(&cnt.num_connect_req);
852 cf_sk->layer.receive = caif_sktrecv_cb;
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000853
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000854 err = caif_connect_client(sock_net(sk), &cf_sk->conn_req,
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000855 &cf_sk->layer, &ifindex, &headroom, &tailroom);
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000856
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000857 if (err < 0) {
858 cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
859 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
860 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000861 }
Eric Dumazet79315062010-10-04 22:42:08 +0000862
863 err = -ENODEV;
864 rcu_read_lock();
865 dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
866 if (!dev) {
867 rcu_read_unlock();
868 goto out;
869 }
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000870 cf_sk->headroom = LL_RESERVED_SPACE_EXTRA(dev, headroom);
Eric Dumazet79315062010-10-04 22:42:08 +0000871 mtu = dev->mtu;
872 rcu_read_unlock();
873
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000874 cf_sk->tailroom = tailroom;
Eric Dumazet79315062010-10-04 22:42:08 +0000875 cf_sk->maxframe = mtu - (headroom + tailroom);
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000876 if (cf_sk->maxframe < 1) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000877 pr_warn("CAIF Interface MTU too small (%d)\n", dev->mtu);
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +0000878 err = -ENODEV;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000879 goto out;
880 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000881
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000882 err = -EINPROGRESS;
883wait_connect:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000884
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000885 if (sk->sk_state != CAIF_CONNECTED && (flags & O_NONBLOCK))
886 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000887
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000888 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000889
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000890 release_sock(sk);
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000891 err = -ERESTARTSYS;
892 timeo = wait_event_interruptible_timeout(*sk_sleep(sk),
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000893 sk->sk_state != CAIF_CONNECTING,
894 timeo);
895 lock_sock(sk);
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000896 if (timeo < 0)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000897 goto out; /* -ERESTARTSYS */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000898
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000899 err = -ETIMEDOUT;
900 if (timeo == 0 && sk->sk_state != CAIF_CONNECTED)
901 goto out;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000902 if (sk->sk_state != CAIF_CONNECTED) {
903 sock->state = SS_UNCONNECTED;
904 err = sock_error(sk);
905 if (!err)
906 err = -ECONNREFUSED;
907 goto out;
908 }
909 sock->state = SS_CONNECTED;
910 err = 0;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000911out:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000912 release_sock(sk);
913 return err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000914}
915
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000916/*
917 * caif_release() - Disconnect a CAIF Socket
918 * Copied and modified af_irda.c:irda_release().
919 */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000920static int caif_release(struct socket *sock)
921{
922 struct sock *sk = sock->sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000923 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
924 int res = 0;
925
926 if (!sk)
927 return 0;
928
929 set_tx_flow_off(cf_sk);
930
931 /*
932 * Ensure that packets are not queued after this point in time.
933 * caif_queue_rcv_skb checks SOCK_DEAD holding the queue lock,
934 * this ensures no packets when sock is dead.
935 */
936 spin_lock(&sk->sk_receive_queue.lock);
937 sock_set_flag(sk, SOCK_DEAD);
938 spin_unlock(&sk->sk_receive_queue.lock);
939 sock->sk = NULL;
940
941 dbfs_atomic_inc(&cnt.num_disconnect);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000942
943 if (cf_sk->debugfs_socket_dir != NULL)
944 debugfs_remove_recursive(cf_sk->debugfs_socket_dir);
945
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000946 lock_sock(&(cf_sk->sk));
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000947 sk->sk_state = CAIF_DISCONNECTED;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000948 sk->sk_shutdown = SHUTDOWN_MASK;
949
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000950 if (cf_sk->sk.sk_socket->state == SS_CONNECTED ||
951 cf_sk->sk.sk_socket->state == SS_CONNECTING)
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000952 res = caif_disconnect_client(sock_net(sk), &cf_sk->layer);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000953
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000954 cf_sk->sk.sk_socket->state = SS_DISCONNECTING;
955 wake_up_interruptible_poll(sk_sleep(sk), POLLERR|POLLHUP);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000956
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000957 sock_orphan(sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000958 sk_stream_kill_queues(&cf_sk->sk);
959 release_sock(sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000960 sock_put(sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000961 return res;
962}
963
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000964/* Copied from af_unix.c:unix_poll(), added CAIF tx_flow handling */
965static unsigned int caif_poll(struct file *file,
966 struct socket *sock, poll_table *wait)
967{
968 struct sock *sk = sock->sk;
969 unsigned int mask;
970 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
971
972 sock_poll_wait(file, sk_sleep(sk), wait);
973 mask = 0;
974
975 /* exceptional events? */
976 if (sk->sk_err)
977 mask |= POLLERR;
978 if (sk->sk_shutdown == SHUTDOWN_MASK)
979 mask |= POLLHUP;
980 if (sk->sk_shutdown & RCV_SHUTDOWN)
981 mask |= POLLRDHUP;
982
983 /* readable? */
984 if (!skb_queue_empty(&sk->sk_receive_queue) ||
985 (sk->sk_shutdown & RCV_SHUTDOWN))
986 mask |= POLLIN | POLLRDNORM;
987
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000988 /*
989 * we set writable also when the other side has shut down the
990 * connection. This prevents stuck sockets.
991 */
992 if (sock_writeable(sk) && tx_flow_is_on(cf_sk))
993 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
994
995 return mask;
996}
997
998static const struct proto_ops caif_seqpacket_ops = {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000999 .family = PF_CAIF,
1000 .owner = THIS_MODULE,
1001 .release = caif_release,
1002 .bind = sock_no_bind,
1003 .connect = caif_connect,
1004 .socketpair = sock_no_socketpair,
1005 .accept = sock_no_accept,
1006 .getname = sock_no_getname,
1007 .poll = caif_poll,
1008 .ioctl = sock_no_ioctl,
1009 .listen = sock_no_listen,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001010 .shutdown = sock_no_shutdown,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001011 .setsockopt = setsockopt,
1012 .getsockopt = sock_no_getsockopt,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001013 .sendmsg = caif_seqpkt_sendmsg,
1014 .recvmsg = caif_seqpkt_recvmsg,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001015 .mmap = sock_no_mmap,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001016 .sendpage = sock_no_sendpage,
1017};
1018
1019static const struct proto_ops caif_stream_ops = {
1020 .family = PF_CAIF,
1021 .owner = THIS_MODULE,
1022 .release = caif_release,
1023 .bind = sock_no_bind,
1024 .connect = caif_connect,
1025 .socketpair = sock_no_socketpair,
1026 .accept = sock_no_accept,
1027 .getname = sock_no_getname,
1028 .poll = caif_poll,
1029 .ioctl = sock_no_ioctl,
1030 .listen = sock_no_listen,
1031 .shutdown = sock_no_shutdown,
1032 .setsockopt = setsockopt,
1033 .getsockopt = sock_no_getsockopt,
1034 .sendmsg = caif_stream_sendmsg,
1035 .recvmsg = caif_stream_recvmsg,
1036 .mmap = sock_no_mmap,
1037 .sendpage = sock_no_sendpage,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001038};
1039
1040/* This function is called when a socket is finally destroyed. */
1041static void caif_sock_destructor(struct sock *sk)
1042{
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001043 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001044 caif_assert(!atomic_read(&sk->sk_wmem_alloc));
1045 caif_assert(sk_unhashed(sk));
1046 caif_assert(!sk->sk_socket);
1047 if (!sock_flag(sk, SOCK_DEAD)) {
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001048 pr_info("Attempt to release alive CAIF socket: %p\n", sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001049 return;
1050 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001051 sk_stream_kill_queues(&cf_sk->sk);
1052 dbfs_atomic_dec(&cnt.caif_nr_socks);
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +00001053 caif_free_client(&cf_sk->layer);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001054}
1055
1056static int caif_create(struct net *net, struct socket *sock, int protocol,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001057 int kern)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001058{
1059 struct sock *sk = NULL;
1060 struct caifsock *cf_sk = NULL;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001061 static struct proto prot = {.name = "PF_CAIF",
1062 .owner = THIS_MODULE,
1063 .obj_size = sizeof(struct caifsock),
1064 };
1065
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001066 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_NET_ADMIN))
1067 return -EPERM;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001068 /*
1069 * The sock->type specifies the socket type to use.
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001070 * The CAIF socket is a packet stream in the sense
1071 * that it is packet based. CAIF trusts the reliability
1072 * of the link, no resending is implemented.
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001073 */
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001074 if (sock->type == SOCK_SEQPACKET)
1075 sock->ops = &caif_seqpacket_ops;
1076 else if (sock->type == SOCK_STREAM)
1077 sock->ops = &caif_stream_ops;
1078 else
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001079 return -ESOCKTNOSUPPORT;
1080
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001081 if (protocol < 0 || protocol >= CAIFPROTO_MAX)
1082 return -EPROTONOSUPPORT;
1083 /*
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001084 * Set the socket state to unconnected. The socket state
1085 * is really not used at all in the net/core or socket.c but the
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001086 * initialization makes sure that sock->state is not uninitialized.
1087 */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001088 sk = sk_alloc(net, PF_CAIF, GFP_KERNEL, &prot);
1089 if (!sk)
1090 return -ENOMEM;
1091
1092 cf_sk = container_of(sk, struct caifsock, sk);
1093
1094 /* Store the protocol */
1095 sk->sk_protocol = (unsigned char) protocol;
1096
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001097 /*
1098 * Lock in order to try to stop someone from opening the socket
1099 * too early.
1100 */
1101 lock_sock(&(cf_sk->sk));
1102
1103 /* Initialize the nozero default sock structure data. */
1104 sock_init_data(sock, sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001105 sk->sk_destruct = caif_sock_destructor;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001106
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001107 mutex_init(&cf_sk->readlock); /* single task reading lock */
1108 cf_sk->layer.ctrlcmd = caif_ctrl_cb;
1109 cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
1110 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001111
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001112 set_tx_flow_off(cf_sk);
1113 set_rx_flow_on(cf_sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001114
1115 /* Set default options on configuration */
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +00001116 cf_sk->sk.sk_priority= CAIF_PRIO_NORMAL;
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001117 cf_sk->conn_req.link_selector = CAIF_LINK_LOW_LATENCY;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001118 cf_sk->conn_req.protocol = protocol;
1119 /* Increase the number of sockets created. */
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001120 dbfs_atomic_inc(&cnt.caif_nr_socks);
1121#ifdef CONFIG_DEBUG_FS
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001122 if (!IS_ERR(debugfsdir)) {
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001123 /* Fill in some information concerning the misc socket. */
1124 snprintf(cf_sk->name, sizeof(cf_sk->name), "cfsk%d",
1125 atomic_read(&cnt.caif_nr_socks));
1126
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001127 cf_sk->debugfs_socket_dir =
1128 debugfs_create_dir(cf_sk->name, debugfsdir);
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001129 debugfs_create_u32("sk_state", S_IRUSR | S_IWUSR,
1130 cf_sk->debugfs_socket_dir,
1131 (u32 *) &cf_sk->sk.sk_state);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001132 debugfs_create_u32("flow_state", S_IRUSR | S_IWUSR,
1133 cf_sk->debugfs_socket_dir, &cf_sk->flow_state);
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001134 debugfs_create_u32("sk_rmem_alloc", S_IRUSR | S_IWUSR,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001135 cf_sk->debugfs_socket_dir,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001136 (u32 *) &cf_sk->sk.sk_rmem_alloc);
1137 debugfs_create_u32("sk_wmem_alloc", S_IRUSR | S_IWUSR,
1138 cf_sk->debugfs_socket_dir,
1139 (u32 *) &cf_sk->sk.sk_wmem_alloc);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001140 debugfs_create_u32("identity", S_IRUSR | S_IWUSR,
1141 cf_sk->debugfs_socket_dir,
1142 (u32 *) &cf_sk->layer.id);
1143 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001144#endif
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001145 release_sock(&cf_sk->sk);
1146 return 0;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001147}
1148
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001149
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001150static struct net_proto_family caif_family_ops = {
1151 .family = PF_CAIF,
1152 .create = caif_create,
1153 .owner = THIS_MODULE,
1154};
1155
Sjur Braendelanda9a8f102010-05-21 02:16:11 +00001156static int af_caif_init(void)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001157{
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001158 int err = sock_register(&caif_family_ops);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001159 if (!err)
1160 return err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001161 return 0;
1162}
1163
1164static int __init caif_sktinit_module(void)
1165{
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001166#ifdef CONFIG_DEBUG_FS
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001167 debugfsdir = debugfs_create_dir("caif_sk", NULL);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001168 if (!IS_ERR(debugfsdir)) {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001169 debugfs_create_u32("num_sockets", S_IRUSR | S_IWUSR,
1170 debugfsdir,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001171 (u32 *) &cnt.caif_nr_socks);
1172 debugfs_create_u32("num_connect_req", S_IRUSR | S_IWUSR,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001173 debugfsdir,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001174 (u32 *) &cnt.num_connect_req);
1175 debugfs_create_u32("num_connect_resp", S_IRUSR | S_IWUSR,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001176 debugfsdir,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001177 (u32 *) &cnt.num_connect_resp);
1178 debugfs_create_u32("num_connect_fail_resp", S_IRUSR | S_IWUSR,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001179 debugfsdir,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001180 (u32 *) &cnt.num_connect_fail_resp);
1181 debugfs_create_u32("num_disconnect", S_IRUSR | S_IWUSR,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001182 debugfsdir,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001183 (u32 *) &cnt.num_disconnect);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001184 debugfs_create_u32("num_remote_shutdown_ind",
1185 S_IRUSR | S_IWUSR, debugfsdir,
1186 (u32 *) &cnt.num_remote_shutdown_ind);
1187 debugfs_create_u32("num_tx_flow_off_ind", S_IRUSR | S_IWUSR,
1188 debugfsdir,
1189 (u32 *) &cnt.num_tx_flow_off_ind);
1190 debugfs_create_u32("num_tx_flow_on_ind", S_IRUSR | S_IWUSR,
1191 debugfsdir,
1192 (u32 *) &cnt.num_tx_flow_on_ind);
1193 debugfs_create_u32("num_rx_flow_off", S_IRUSR | S_IWUSR,
1194 debugfsdir,
1195 (u32 *) &cnt.num_rx_flow_off);
1196 debugfs_create_u32("num_rx_flow_on", S_IRUSR | S_IWUSR,
1197 debugfsdir,
1198 (u32 *) &cnt.num_rx_flow_on);
1199 }
1200#endif
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001201 return af_caif_init();
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001202}
1203
1204static void __exit caif_sktexit_module(void)
1205{
1206 sock_unregister(PF_CAIF);
1207 if (debugfsdir != NULL)
1208 debugfs_remove_recursive(debugfsdir);
1209}
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001210module_init(caif_sktinit_module);
1211module_exit(caif_sktexit_module);