blob: 05a41c7ec304e4256dca41b6b111bc1a267598b5 [file] [log] [blame]
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001/*
2 * Copyright (C) ST-Ericsson AB 2010
sjur.brandeland@stericsson.com26ee65e2013-04-22 23:57:01 +00003 * Author: Sjur Brendeland
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00004 * License terms: GNU General Public License (GPL) version 2
5 */
6
Joe Perchesb31fa5b2010-09-05 21:31:11 +00007#define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
8
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00009#include <linux/fs.h>
10#include <linux/init.h>
11#include <linux/module.h>
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>
Dmitry Tarnyagin44764812012-04-12 08:27:24 +000022#include <linux/pkt_sched.h>
Sjur Braendelandbece7b22010-04-28 08:54:38 +000023#include <net/sock.h>
24#include <net/tcp_states.h>
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000025#include <net/caif/caif_layer.h>
26#include <net/caif/caif_dev.h>
27#include <net/caif/cfpkt.h>
28
29MODULE_LICENSE("GPL");
Sjur Braendelandbece7b22010-04-28 08:54:38 +000030MODULE_ALIAS_NETPROTO(AF_CAIF);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000031
Sjur Braendelandbece7b22010-04-28 08:54:38 +000032/*
33 * CAIF state is re-using the TCP socket states.
34 * caif_states stored in sk_state reflect the state as reported by
35 * the CAIF stack, while sk_socket->state is the state of the socket.
36 */
37enum caif_states {
38 CAIF_CONNECTED = TCP_ESTABLISHED,
39 CAIF_CONNECTING = TCP_SYN_SENT,
40 CAIF_DISCONNECTED = TCP_CLOSE
41};
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000042
Sjur Braendelandbece7b22010-04-28 08:54:38 +000043#define TX_FLOW_ON_BIT 1
44#define RX_FLOW_ON_BIT 2
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000045
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000046struct caifsock {
Sjur Braendelandbece7b22010-04-28 08:54:38 +000047 struct sock sk; /* must be first member */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000048 struct cflayer layer;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000049 u32 flow_state;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000050 struct caif_connect_request conn_req;
Sjur Braendelandbece7b22010-04-28 08:54:38 +000051 struct mutex readlock;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000052 struct dentry *debugfs_socket_dir;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +000053 int headroom, tailroom, maxframe;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000054};
55
Sjur Braendelandbece7b22010-04-28 08:54:38 +000056static int rx_flow_is_on(struct caifsock *cf_sk)
57{
58 return test_bit(RX_FLOW_ON_BIT,
59 (void *) &cf_sk->flow_state);
60}
61
62static int tx_flow_is_on(struct caifsock *cf_sk)
63{
64 return test_bit(TX_FLOW_ON_BIT,
65 (void *) &cf_sk->flow_state);
66}
67
68static void set_rx_flow_off(struct caifsock *cf_sk)
69{
70 clear_bit(RX_FLOW_ON_BIT,
71 (void *) &cf_sk->flow_state);
72}
73
74static void set_rx_flow_on(struct caifsock *cf_sk)
75{
76 set_bit(RX_FLOW_ON_BIT,
77 (void *) &cf_sk->flow_state);
78}
79
80static void set_tx_flow_off(struct caifsock *cf_sk)
81{
82 clear_bit(TX_FLOW_ON_BIT,
83 (void *) &cf_sk->flow_state);
84}
85
86static void set_tx_flow_on(struct caifsock *cf_sk)
87{
88 set_bit(TX_FLOW_ON_BIT,
89 (void *) &cf_sk->flow_state);
90}
91
92static void caif_read_lock(struct sock *sk)
93{
94 struct caifsock *cf_sk;
95 cf_sk = container_of(sk, struct caifsock, sk);
96 mutex_lock(&cf_sk->readlock);
97}
98
99static void caif_read_unlock(struct sock *sk)
100{
101 struct caifsock *cf_sk;
102 cf_sk = container_of(sk, struct caifsock, sk);
103 mutex_unlock(&cf_sk->readlock);
104}
105
Sjur Braendelanda9a8f102010-05-21 02:16:11 +0000106static int sk_rcvbuf_lowwater(struct caifsock *cf_sk)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000107{
108 /* A quarter of full buffer is used a low water mark */
109 return cf_sk->sk.sk_rcvbuf / 4;
110}
111
Sjur Braendelanda9a8f102010-05-21 02:16:11 +0000112static void caif_flow_ctrl(struct sock *sk, int mode)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000113{
114 struct caifsock *cf_sk;
115 cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000116 if (cf_sk->layer.dn && cf_sk->layer.dn->modemcmd)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000117 cf_sk->layer.dn->modemcmd(cf_sk->layer.dn, mode);
118}
119
120/*
121 * Copied from sock.c:sock_queue_rcv_skb(), but changed so packets are
122 * not dropped, but CAIF is sending flow off instead.
123 */
Sjur Braendelanda9a8f102010-05-21 02:16:11 +0000124static int caif_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000125{
126 int err;
127 int skb_len;
128 unsigned long flags;
129 struct sk_buff_head *list = &sk->sk_receive_queue;
130 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
131
132 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
Eric Dumazet95c96172012-04-15 05:58:06 +0000133 (unsigned int)sk->sk_rcvbuf && rx_flow_is_on(cf_sk)) {
Joe Perchese87cc472012-05-13 21:56:26 +0000134 net_dbg_ratelimited("sending flow OFF (queue len = %d %d)\n",
135 atomic_read(&cf_sk->sk.sk_rmem_alloc),
136 sk_rcvbuf_lowwater(cf_sk));
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000137 set_rx_flow_off(cf_sk);
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000138 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_OFF_REQ);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000139 }
140
141 err = sk_filter(sk, skb);
142 if (err)
143 return err;
Mel Gormanc76562b2012-07-31 16:44:41 -0700144 if (!sk_rmem_schedule(sk, skb, skb->truesize) && rx_flow_is_on(cf_sk)) {
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000145 set_rx_flow_off(cf_sk);
Joe Perchese87cc472012-05-13 21:56:26 +0000146 net_dbg_ratelimited("sending flow OFF due to rmem_schedule\n");
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000147 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_OFF_REQ);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000148 }
149 skb->dev = NULL;
150 skb_set_owner_r(skb, sk);
151 /* Cache the SKB length before we tack it onto the receive
152 * queue. Once it is added it no longer belongs to us and
153 * may be freed by other threads of control pulling packets
154 * from the queue.
155 */
156 skb_len = skb->len;
157 spin_lock_irqsave(&list->lock, flags);
158 if (!sock_flag(sk, SOCK_DEAD))
159 __skb_queue_tail(list, skb);
160 spin_unlock_irqrestore(&list->lock, flags);
161
162 if (!sock_flag(sk, SOCK_DEAD))
163 sk->sk_data_ready(sk, skb_len);
164 else
165 kfree_skb(skb);
166 return 0;
167}
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000168
169/* Packet Receive Callback function called from CAIF Stack */
170static int caif_sktrecv_cb(struct cflayer *layr, struct cfpkt *pkt)
171{
172 struct caifsock *cf_sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000173 struct sk_buff *skb;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000174
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000175 cf_sk = container_of(layr, struct caifsock, layer);
176 skb = cfpkt_tonative(pkt);
177
178 if (unlikely(cf_sk->sk.sk_state != CAIF_CONNECTED)) {
sjur.brandeland@stericsson.com3f874ad2011-05-13 02:44:08 +0000179 kfree_skb(skb);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000180 return 0;
181 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000182 caif_queue_rcv_skb(&cf_sk->sk, skb);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000183 return 0;
184}
185
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000186static void cfsk_hold(struct cflayer *layr)
187{
188 struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
189 sock_hold(&cf_sk->sk);
190}
191
192static void cfsk_put(struct cflayer *layr)
193{
194 struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
195 sock_put(&cf_sk->sk);
196}
197
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000198/* Packet Control Callback function called from CAIF */
199static void caif_ctrl_cb(struct cflayer *layr,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000200 enum caif_ctrlcmd flow,
201 int phyid)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000202{
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000203 struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000204 switch (flow) {
205 case CAIF_CTRLCMD_FLOW_ON_IND:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000206 /* OK from modem to start sending again */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000207 set_tx_flow_on(cf_sk);
208 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000209 break;
210
211 case CAIF_CTRLCMD_FLOW_OFF_IND:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000212 /* Modem asks us to shut up */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000213 set_tx_flow_off(cf_sk);
214 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000215 break;
216
217 case CAIF_CTRLCMD_INIT_RSP:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000218 /* We're now connected */
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000219 caif_client_register_refcnt(&cf_sk->layer,
220 cfsk_hold, cfsk_put);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000221 cf_sk->sk.sk_state = CAIF_CONNECTED;
222 set_tx_flow_on(cf_sk);
Sjur Brændelandeaa8c5f2012-06-24 11:01:36 +0000223 cf_sk->sk.sk_shutdown = 0;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000224 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000225 break;
226
227 case CAIF_CTRLCMD_DEINIT_RSP:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000228 /* We're now disconnected */
229 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
230 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000231 break;
232
233 case CAIF_CTRLCMD_INIT_FAIL_RSP:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000234 /* Connect request failed */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000235 cf_sk->sk.sk_err = ECONNREFUSED;
236 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
237 cf_sk->sk.sk_shutdown = SHUTDOWN_MASK;
238 /*
239 * Socket "standards" seems to require POLLOUT to
240 * be set at connect failure.
241 */
242 set_tx_flow_on(cf_sk);
243 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000244 break;
245
246 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000247 /* Modem has closed this connection, or device is down. */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000248 cf_sk->sk.sk_shutdown = SHUTDOWN_MASK;
249 cf_sk->sk.sk_err = ECONNRESET;
250 set_rx_flow_on(cf_sk);
251 cf_sk->sk.sk_error_report(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000252 break;
253
254 default:
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000255 pr_debug("Unexpected flow command %d\n", flow);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000256 }
257}
258
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000259static void caif_check_flow_release(struct sock *sk)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000260{
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000261 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
262
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000263 if (rx_flow_is_on(cf_sk))
264 return;
265
266 if (atomic_read(&sk->sk_rmem_alloc) <= sk_rcvbuf_lowwater(cf_sk)) {
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000267 set_rx_flow_on(cf_sk);
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000268 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_ON_REQ);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000269 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000270}
271
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000272/*
273 * Copied from unix_dgram_recvmsg, but removed credit checks,
274 * changed locking, address handling and added MSG_TRUNC.
275 */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000276static int caif_seqpkt_recvmsg(struct kiocb *iocb, struct socket *sock,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000277 struct msghdr *m, size_t len, int flags)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000278
279{
280 struct sock *sk = sock->sk;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000281 struct sk_buff *skb;
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000282 int ret;
283 int copylen;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000284
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000285 ret = -EOPNOTSUPP;
286 if (m->msg_flags&MSG_OOB)
287 goto read_error;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000288
Mathias Krause2d6fbfe2013-04-07 01:51:52 +0000289 m->msg_namelen = 0;
290
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000291 skb = skb_recv_datagram(sk, flags, 0 , &ret);
292 if (!skb)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000293 goto read_error;
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000294 copylen = skb->len;
295 if (len < copylen) {
296 m->msg_flags |= MSG_TRUNC;
297 copylen = len;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000298 }
299
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000300 ret = skb_copy_datagram_iovec(skb, 0, m->msg_iov, copylen);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000301 if (ret)
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000302 goto out_free;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000303
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000304 ret = (flags & MSG_TRUNC) ? skb->len : copylen;
305out_free:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000306 skb_free_datagram(sk, skb);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000307 caif_check_flow_release(sk);
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000308 return ret;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000309
310read_error:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000311 return ret;
312}
313
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000314
315/* Copied from unix_stream_wait_data, identical except for lock call. */
316static long caif_stream_data_wait(struct sock *sk, long timeo)
317{
318 DEFINE_WAIT(wait);
319 lock_sock(sk);
320
321 for (;;) {
322 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
323
324 if (!skb_queue_empty(&sk->sk_receive_queue) ||
325 sk->sk_err ||
326 sk->sk_state != CAIF_CONNECTED ||
327 sock_flag(sk, SOCK_DEAD) ||
328 (sk->sk_shutdown & RCV_SHUTDOWN) ||
329 signal_pending(current) ||
330 !timeo)
331 break;
332
333 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
334 release_sock(sk);
335 timeo = schedule_timeout(timeo);
336 lock_sock(sk);
337 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
338 }
339
340 finish_wait(sk_sleep(sk), &wait);
341 release_sock(sk);
342 return timeo;
343}
344
345
346/*
347 * Copied from unix_stream_recvmsg, but removed credit checks,
348 * changed locking calls, changed address handling.
349 */
350static int caif_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000351 struct msghdr *msg, size_t size,
352 int flags)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000353{
354 struct sock *sk = sock->sk;
355 int copied = 0;
356 int target;
357 int err = 0;
358 long timeo;
359
360 err = -EOPNOTSUPP;
361 if (flags&MSG_OOB)
362 goto out;
363
364 msg->msg_namelen = 0;
365
366 /*
367 * Lock the socket to prevent queue disordering
368 * while sleeps in memcpy_tomsg
369 */
370 err = -EAGAIN;
371 if (sk->sk_state == CAIF_CONNECTING)
372 goto out;
373
374 caif_read_lock(sk);
375 target = sock_rcvlowat(sk, flags&MSG_WAITALL, size);
376 timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
377
378 do {
379 int chunk;
380 struct sk_buff *skb;
381
382 lock_sock(sk);
383 skb = skb_dequeue(&sk->sk_receive_queue);
384 caif_check_flow_release(sk);
385
386 if (skb == NULL) {
387 if (copied >= target)
388 goto unlock;
389 /*
390 * POSIX 1003.1g mandates this order.
391 */
392 err = sock_error(sk);
393 if (err)
394 goto unlock;
395 err = -ECONNRESET;
396 if (sk->sk_shutdown & RCV_SHUTDOWN)
397 goto unlock;
398
399 err = -EPIPE;
400 if (sk->sk_state != CAIF_CONNECTED)
401 goto unlock;
402 if (sock_flag(sk, SOCK_DEAD))
403 goto unlock;
404
405 release_sock(sk);
406
407 err = -EAGAIN;
408 if (!timeo)
409 break;
410
411 caif_read_unlock(sk);
412
413 timeo = caif_stream_data_wait(sk, timeo);
414
415 if (signal_pending(current)) {
416 err = sock_intr_errno(timeo);
417 goto out;
418 }
419 caif_read_lock(sk);
420 continue;
421unlock:
422 release_sock(sk);
423 break;
424 }
425 release_sock(sk);
426 chunk = min_t(unsigned int, skb->len, size);
427 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
428 skb_queue_head(&sk->sk_receive_queue, skb);
429 if (copied == 0)
430 copied = -EFAULT;
431 break;
432 }
433 copied += chunk;
434 size -= chunk;
435
436 /* Mark read part of skb as used */
437 if (!(flags & MSG_PEEK)) {
438 skb_pull(skb, chunk);
439
440 /* put the skb back if we didn't use it up. */
441 if (skb->len) {
442 skb_queue_head(&sk->sk_receive_queue, skb);
443 break;
444 }
445 kfree_skb(skb);
446
447 } else {
448 /*
449 * It is questionable, see note in unix_dgram_recvmsg.
450 */
451 /* put message back and return */
452 skb_queue_head(&sk->sk_receive_queue, skb);
453 break;
454 }
455 } while (size);
456 caif_read_unlock(sk);
457
458out:
459 return copied ? : err;
460}
461
462/*
463 * Copied from sock.c:sock_wait_for_wmem, but change to wait for
464 * CAIF flow-on and sock_writable.
465 */
466static long caif_wait_for_flow_on(struct caifsock *cf_sk,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000467 int wait_writeable, long timeo, int *err)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000468{
469 struct sock *sk = &cf_sk->sk;
470 DEFINE_WAIT(wait);
471 for (;;) {
472 *err = 0;
473 if (tx_flow_is_on(cf_sk) &&
474 (!wait_writeable || sock_writeable(&cf_sk->sk)))
475 break;
476 *err = -ETIMEDOUT;
477 if (!timeo)
478 break;
479 *err = -ERESTARTSYS;
480 if (signal_pending(current))
481 break;
482 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
483 *err = -ECONNRESET;
484 if (sk->sk_shutdown & SHUTDOWN_MASK)
485 break;
486 *err = -sk->sk_err;
487 if (sk->sk_err)
488 break;
489 *err = -EPIPE;
490 if (cf_sk->sk.sk_state != CAIF_CONNECTED)
491 break;
492 timeo = schedule_timeout(timeo);
493 }
494 finish_wait(sk_sleep(sk), &wait);
495 return timeo;
496}
497
498/*
499 * Transmit a SKB. The device may temporarily request re-transmission
500 * by returning EAGAIN.
501 */
502static int transmit_skb(struct sk_buff *skb, struct caifsock *cf_sk,
503 int noblock, long timeo)
504{
505 struct cfpkt *pkt;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000506
507 pkt = cfpkt_fromnative(CAIF_DIR_OUT, skb);
sjur.brandeland@stericsson.com3f874ad2011-05-13 02:44:08 +0000508 memset(skb->cb, 0, sizeof(struct caif_payload_info));
Dmitry Tarnyagin44764812012-04-12 08:27:24 +0000509 cfpkt_set_prio(pkt, cf_sk->sk.sk_priority);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000510
Dmitry Tarnyaginba760572012-02-02 01:21:03 +0000511 if (cf_sk->layer.dn == NULL) {
512 kfree_skb(skb);
Sjur Brændeland4dd820c2011-04-11 10:43:51 +0000513 return -EINVAL;
Dmitry Tarnyaginba760572012-02-02 01:21:03 +0000514 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000515
Sjur Brændeland4dd820c2011-04-11 10:43:51 +0000516 return cf_sk->layer.dn->transmit(cf_sk->layer.dn, pkt);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000517}
518
519/* Copied from af_unix:unix_dgram_sendmsg, and adapted to CAIF */
520static int caif_seqpkt_sendmsg(struct kiocb *kiocb, struct socket *sock,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000521 struct msghdr *msg, size_t len)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000522{
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000523 struct sock *sk = sock->sk;
524 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000525 int buffer_size;
526 int ret = 0;
527 struct sk_buff *skb = NULL;
528 int noblock;
529 long timeo;
530 caif_assert(cf_sk);
531 ret = sock_error(sk);
532 if (ret)
533 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000534
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000535 ret = -EOPNOTSUPP;
536 if (msg->msg_flags&MSG_OOB)
537 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000538
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000539 ret = -EOPNOTSUPP;
540 if (msg->msg_namelen)
541 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000542
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000543 ret = -EINVAL;
544 if (unlikely(msg->msg_iov->iov_base == NULL))
545 goto err;
546 noblock = msg->msg_flags & MSG_DONTWAIT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000547
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000548 timeo = sock_sndtimeo(sk, noblock);
549 timeo = caif_wait_for_flow_on(container_of(sk, struct caifsock, sk),
550 1, timeo, &ret);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000551
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000552 if (ret)
553 goto err;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000554 ret = -EPIPE;
555 if (cf_sk->sk.sk_state != CAIF_CONNECTED ||
556 sock_flag(sk, SOCK_DEAD) ||
557 (sk->sk_shutdown & RCV_SHUTDOWN))
558 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000559
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000560 /* Error if trying to write more than maximum frame size. */
561 ret = -EMSGSIZE;
562 if (len > cf_sk->maxframe && cf_sk->sk.sk_protocol != CAIFPROTO_RFM)
563 goto err;
564
565 buffer_size = len + cf_sk->headroom + cf_sk->tailroom;
566
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000567 ret = -ENOMEM;
568 skb = sock_alloc_send_skb(sk, buffer_size, noblock, &ret);
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000569
570 if (!skb || skb_tailroom(skb) < buffer_size)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000571 goto err;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000572
573 skb_reserve(skb, cf_sk->headroom);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000574
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000575 ret = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000576
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000577 if (ret)
578 goto err;
579 ret = transmit_skb(skb, cf_sk, noblock, timeo);
580 if (ret < 0)
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000581 /* skb is already freed */
582 return ret;
583
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000584 return len;
585err:
586 kfree_skb(skb);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000587 return ret;
588}
589
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000590/*
591 * Copied from unix_stream_sendmsg and adapted to CAIF:
592 * Changed removed permission handling and added waiting for flow on
593 * and other minor adaptations.
594 */
595static int caif_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000596 struct msghdr *msg, size_t len)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000597{
598 struct sock *sk = sock->sk;
599 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000600 int err, size;
601 struct sk_buff *skb;
602 int sent = 0;
603 long timeo;
604
605 err = -EOPNOTSUPP;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000606 if (unlikely(msg->msg_flags&MSG_OOB))
607 goto out_err;
608
609 if (unlikely(msg->msg_namelen))
610 goto out_err;
611
612 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
613 timeo = caif_wait_for_flow_on(cf_sk, 1, timeo, &err);
614
615 if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN))
616 goto pipe_err;
617
618 while (sent < len) {
619
620 size = len-sent;
621
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000622 if (size > cf_sk->maxframe)
623 size = cf_sk->maxframe;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000624
625 /* If size is more than half of sndbuf, chop up message */
626 if (size > ((sk->sk_sndbuf >> 1) - 64))
627 size = (sk->sk_sndbuf >> 1) - 64;
628
629 if (size > SKB_MAX_ALLOC)
630 size = SKB_MAX_ALLOC;
631
632 skb = sock_alloc_send_skb(sk,
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000633 size + cf_sk->headroom +
634 cf_sk->tailroom,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000635 msg->msg_flags&MSG_DONTWAIT,
636 &err);
637 if (skb == NULL)
638 goto out_err;
639
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000640 skb_reserve(skb, cf_sk->headroom);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000641 /*
642 * If you pass two values to the sock_alloc_send_skb
643 * it tries to grab the large buffer with GFP_NOFS
644 * (which can fail easily), and if it fails grab the
645 * fallback size buffer which is under a page and will
646 * succeed. [Alan]
647 */
648 size = min_t(int, size, skb_tailroom(skb));
649
650 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
651 if (err) {
652 kfree_skb(skb);
653 goto out_err;
654 }
655 err = transmit_skb(skb, cf_sk,
656 msg->msg_flags&MSG_DONTWAIT, timeo);
Dmitry Tarnyaginba760572012-02-02 01:21:03 +0000657 if (err < 0)
658 /* skb is already freed */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000659 goto pipe_err;
Dmitry Tarnyaginba760572012-02-02 01:21:03 +0000660
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000661 sent += size;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000662 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000663
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000664 return sent;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000665
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000666pipe_err:
667 if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL))
668 send_sig(SIGPIPE, current, 0);
669 err = -EPIPE;
670out_err:
671 return sent ? : err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000672}
673
674static int setsockopt(struct socket *sock,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000675 int lvl, int opt, char __user *ov, unsigned int ol)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000676{
677 struct sock *sk = sock->sk;
678 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +0000679 int linksel;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000680
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000681 if (cf_sk->sk.sk_socket->state != SS_UNCONNECTED)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000682 return -ENOPROTOOPT;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000683
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000684 switch (opt) {
685 case CAIFSO_LINK_SELECT:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000686 if (ol < sizeof(int))
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000687 return -EINVAL;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000688 if (lvl != SOL_CAIF)
689 goto bad_sol;
690 if (copy_from_user(&linksel, ov, sizeof(int)))
691 return -EINVAL;
692 lock_sock(&(cf_sk->sk));
693 cf_sk->conn_req.link_selector = linksel;
694 release_sock(&cf_sk->sk);
695 return 0;
696
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000697 case CAIFSO_REQ_PARAM:
698 if (lvl != SOL_CAIF)
699 goto bad_sol;
700 if (cf_sk->sk.sk_protocol != CAIFPROTO_UTIL)
701 return -ENOPROTOOPT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000702 lock_sock(&(cf_sk->sk));
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000703 if (ol > sizeof(cf_sk->conn_req.param.data) ||
704 copy_from_user(&cf_sk->conn_req.param.data, ov, ol)) {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000705 release_sock(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000706 return -EINVAL;
707 }
Dan Rosenberg91b5c982011-01-10 16:00:54 -0800708 cf_sk->conn_req.param.size = ol;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000709 release_sock(&cf_sk->sk);
710 return 0;
711
712 default:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000713 return -ENOPROTOOPT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000714 }
715
716 return 0;
717bad_sol:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000718 return -ENOPROTOOPT;
719
720}
721
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000722/*
723 * caif_connect() - Connect a CAIF Socket
724 * Copied and modified af_irda.c:irda_connect().
725 *
726 * Note : by consulting "errno", the user space caller may learn the cause
727 * of the failure. Most of them are visible in the function, others may come
728 * from subroutines called and are listed here :
729 * o -EAFNOSUPPORT: bad socket family or type.
730 * o -ESOCKTNOSUPPORT: bad socket type or protocol
731 * o -EINVAL: bad socket address, or CAIF link type
732 * o -ECONNREFUSED: remote end refused the connection.
733 * o -EINPROGRESS: connect request sent but timed out (or non-blocking)
734 * o -EISCONN: already connected.
735 * o -ETIMEDOUT: Connection timed out (send timeout)
736 * o -ENODEV: No link layer to send request
737 * o -ECONNRESET: Received Shutdown indication or lost link layer
738 * o -ENOMEM: Out of memory
739 *
740 * State Strategy:
741 * o sk_state: holds the CAIF_* protocol state, it's updated by
742 * caif_ctrl_cb.
743 * o sock->state: holds the SS_* socket state and is updated by connect and
744 * disconnect.
745 */
746static int caif_connect(struct socket *sock, struct sockaddr *uaddr,
747 int addr_len, int flags)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000748{
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000749 struct sock *sk = sock->sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000750 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
751 long timeo;
752 int err;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000753 int ifindex, headroom, tailroom;
Eric Dumazet79315062010-10-04 22:42:08 +0000754 unsigned int mtu;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000755 struct net_device *dev;
756
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000757 lock_sock(sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000758
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000759 err = -EAFNOSUPPORT;
760 if (uaddr->sa_family != AF_CAIF)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000761 goto out;
762
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000763 switch (sock->state) {
764 case SS_UNCONNECTED:
765 /* Normal case, a fresh connect */
766 caif_assert(sk->sk_state == CAIF_DISCONNECTED);
767 break;
768 case SS_CONNECTING:
769 switch (sk->sk_state) {
770 case CAIF_CONNECTED:
771 sock->state = SS_CONNECTED;
772 err = -EISCONN;
773 goto out;
774 case CAIF_DISCONNECTED:
775 /* Reconnect allowed */
776 break;
777 case CAIF_CONNECTING:
778 err = -EALREADY;
779 if (flags & O_NONBLOCK)
780 goto out;
781 goto wait_connect;
782 }
783 break;
784 case SS_CONNECTED:
785 caif_assert(sk->sk_state == CAIF_CONNECTED ||
786 sk->sk_state == CAIF_DISCONNECTED);
787 if (sk->sk_shutdown & SHUTDOWN_MASK) {
788 /* Allow re-connect after SHUTDOWN_IND */
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000789 caif_disconnect_client(sock_net(sk), &cf_sk->layer);
sjur.brandeland@stericsson.com54e90fb2011-05-22 11:18:51 +0000790 caif_free_client(&cf_sk->layer);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000791 break;
792 }
793 /* No reconnect on a seqpacket socket */
794 err = -EISCONN;
795 goto out;
796 case SS_DISCONNECTING:
797 case SS_FREE:
798 caif_assert(1); /*Should never happen */
799 break;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000800 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000801 sk->sk_state = CAIF_DISCONNECTED;
802 sock->state = SS_UNCONNECTED;
803 sk_stream_kill_queues(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000804
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000805 err = -EINVAL;
Dan Carpenterf5d72af2010-05-22 10:43:42 +0000806 if (addr_len != sizeof(struct sockaddr_caif))
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000807 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000808
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000809 memcpy(&cf_sk->conn_req.sockaddr, uaddr,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000810 sizeof(struct sockaddr_caif));
811
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000812 /* Move to connecting socket, start sending Connect Requests */
813 sock->state = SS_CONNECTING;
814 sk->sk_state = CAIF_CONNECTING;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000815
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +0000816 /* Check priority value comming from socket */
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +0000817 /* if priority value is out of range it will be ajusted */
818 if (cf_sk->sk.sk_priority > CAIF_PRIO_MAX)
819 cf_sk->conn_req.priority = CAIF_PRIO_MAX;
820 else if (cf_sk->sk.sk_priority < CAIF_PRIO_MIN)
821 cf_sk->conn_req.priority = CAIF_PRIO_MIN;
822 else
823 cf_sk->conn_req.priority = cf_sk->sk.sk_priority;
824
825 /*ifindex = id of the interface.*/
826 cf_sk->conn_req.ifindex = cf_sk->sk.sk_bound_dev_if;
827
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000828 cf_sk->layer.receive = caif_sktrecv_cb;
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000829
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000830 err = caif_connect_client(sock_net(sk), &cf_sk->conn_req,
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000831 &cf_sk->layer, &ifindex, &headroom, &tailroom);
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000832
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000833 if (err < 0) {
834 cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
835 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
836 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000837 }
Eric Dumazet79315062010-10-04 22:42:08 +0000838
839 err = -ENODEV;
840 rcu_read_lock();
841 dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
842 if (!dev) {
843 rcu_read_unlock();
844 goto out;
845 }
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000846 cf_sk->headroom = LL_RESERVED_SPACE_EXTRA(dev, headroom);
Eric Dumazet79315062010-10-04 22:42:08 +0000847 mtu = dev->mtu;
848 rcu_read_unlock();
849
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000850 cf_sk->tailroom = tailroom;
Eric Dumazet79315062010-10-04 22:42:08 +0000851 cf_sk->maxframe = mtu - (headroom + tailroom);
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000852 if (cf_sk->maxframe < 1) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000853 pr_warn("CAIF Interface MTU too small (%d)\n", dev->mtu);
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +0000854 err = -ENODEV;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000855 goto out;
856 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000857
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000858 err = -EINPROGRESS;
859wait_connect:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000860
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000861 if (sk->sk_state != CAIF_CONNECTED && (flags & O_NONBLOCK))
862 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000863
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000864 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000865
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000866 release_sock(sk);
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000867 err = -ERESTARTSYS;
868 timeo = wait_event_interruptible_timeout(*sk_sleep(sk),
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000869 sk->sk_state != CAIF_CONNECTING,
870 timeo);
871 lock_sock(sk);
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000872 if (timeo < 0)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000873 goto out; /* -ERESTARTSYS */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000874
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000875 err = -ETIMEDOUT;
876 if (timeo == 0 && sk->sk_state != CAIF_CONNECTED)
877 goto out;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000878 if (sk->sk_state != CAIF_CONNECTED) {
879 sock->state = SS_UNCONNECTED;
880 err = sock_error(sk);
881 if (!err)
882 err = -ECONNREFUSED;
883 goto out;
884 }
885 sock->state = SS_CONNECTED;
886 err = 0;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000887out:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000888 release_sock(sk);
889 return err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000890}
891
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000892/*
893 * caif_release() - Disconnect a CAIF Socket
894 * Copied and modified af_irda.c:irda_release().
895 */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000896static int caif_release(struct socket *sock)
897{
898 struct sock *sk = sock->sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000899 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000900
901 if (!sk)
902 return 0;
903
904 set_tx_flow_off(cf_sk);
905
906 /*
907 * Ensure that packets are not queued after this point in time.
908 * caif_queue_rcv_skb checks SOCK_DEAD holding the queue lock,
909 * this ensures no packets when sock is dead.
910 */
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000911 spin_lock_bh(&sk->sk_receive_queue.lock);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000912 sock_set_flag(sk, SOCK_DEAD);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000913 spin_unlock_bh(&sk->sk_receive_queue.lock);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000914 sock->sk = NULL;
915
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +0000916 WARN_ON(IS_ERR(cf_sk->debugfs_socket_dir));
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000917 if (cf_sk->debugfs_socket_dir != NULL)
918 debugfs_remove_recursive(cf_sk->debugfs_socket_dir);
919
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000920 lock_sock(&(cf_sk->sk));
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000921 sk->sk_state = CAIF_DISCONNECTED;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000922 sk->sk_shutdown = SHUTDOWN_MASK;
923
sjur.brandeland@stericsson.com54e90fb2011-05-22 11:18:51 +0000924 caif_disconnect_client(sock_net(sk), &cf_sk->layer);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000925 cf_sk->sk.sk_socket->state = SS_DISCONNECTING;
926 wake_up_interruptible_poll(sk_sleep(sk), POLLERR|POLLHUP);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000927
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000928 sock_orphan(sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000929 sk_stream_kill_queues(&cf_sk->sk);
930 release_sock(sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000931 sock_put(sk);
sjur.brandeland@stericsson.com54e90fb2011-05-22 11:18:51 +0000932 return 0;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000933}
934
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000935/* Copied from af_unix.c:unix_poll(), added CAIF tx_flow handling */
936static unsigned int caif_poll(struct file *file,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +0000937 struct socket *sock, poll_table *wait)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000938{
939 struct sock *sk = sock->sk;
940 unsigned int mask;
941 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
942
943 sock_poll_wait(file, sk_sleep(sk), wait);
944 mask = 0;
945
946 /* exceptional events? */
947 if (sk->sk_err)
948 mask |= POLLERR;
949 if (sk->sk_shutdown == SHUTDOWN_MASK)
950 mask |= POLLHUP;
951 if (sk->sk_shutdown & RCV_SHUTDOWN)
952 mask |= POLLRDHUP;
953
954 /* readable? */
955 if (!skb_queue_empty(&sk->sk_receive_queue) ||
956 (sk->sk_shutdown & RCV_SHUTDOWN))
957 mask |= POLLIN | POLLRDNORM;
958
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000959 /*
960 * we set writable also when the other side has shut down the
961 * connection. This prevents stuck sockets.
962 */
963 if (sock_writeable(sk) && tx_flow_is_on(cf_sk))
964 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
965
966 return mask;
967}
968
969static const struct proto_ops caif_seqpacket_ops = {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000970 .family = PF_CAIF,
971 .owner = THIS_MODULE,
972 .release = caif_release,
973 .bind = sock_no_bind,
974 .connect = caif_connect,
975 .socketpair = sock_no_socketpair,
976 .accept = sock_no_accept,
977 .getname = sock_no_getname,
978 .poll = caif_poll,
979 .ioctl = sock_no_ioctl,
980 .listen = sock_no_listen,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000981 .shutdown = sock_no_shutdown,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000982 .setsockopt = setsockopt,
983 .getsockopt = sock_no_getsockopt,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000984 .sendmsg = caif_seqpkt_sendmsg,
985 .recvmsg = caif_seqpkt_recvmsg,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000986 .mmap = sock_no_mmap,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000987 .sendpage = sock_no_sendpage,
988};
989
990static const struct proto_ops caif_stream_ops = {
991 .family = PF_CAIF,
992 .owner = THIS_MODULE,
993 .release = caif_release,
994 .bind = sock_no_bind,
995 .connect = caif_connect,
996 .socketpair = sock_no_socketpair,
997 .accept = sock_no_accept,
998 .getname = sock_no_getname,
999 .poll = caif_poll,
1000 .ioctl = sock_no_ioctl,
1001 .listen = sock_no_listen,
1002 .shutdown = sock_no_shutdown,
1003 .setsockopt = setsockopt,
1004 .getsockopt = sock_no_getsockopt,
1005 .sendmsg = caif_stream_sendmsg,
1006 .recvmsg = caif_stream_recvmsg,
1007 .mmap = sock_no_mmap,
1008 .sendpage = sock_no_sendpage,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001009};
1010
1011/* This function is called when a socket is finally destroyed. */
1012static void caif_sock_destructor(struct sock *sk)
1013{
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001014 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001015 caif_assert(!atomic_read(&sk->sk_wmem_alloc));
1016 caif_assert(sk_unhashed(sk));
1017 caif_assert(!sk->sk_socket);
1018 if (!sock_flag(sk, SOCK_DEAD)) {
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +00001019 pr_debug("Attempt to release alive CAIF socket: %p\n", sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001020 return;
1021 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001022 sk_stream_kill_queues(&cf_sk->sk);
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +00001023 caif_free_client(&cf_sk->layer);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001024}
1025
1026static int caif_create(struct net *net, struct socket *sock, int protocol,
Silviu-Mihai Popescu3bffc472013-03-06 19:39:57 +00001027 int kern)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001028{
1029 struct sock *sk = NULL;
1030 struct caifsock *cf_sk = NULL;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001031 static struct proto prot = {.name = "PF_CAIF",
1032 .owner = THIS_MODULE,
1033 .obj_size = sizeof(struct caifsock),
1034 };
1035
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001036 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_NET_ADMIN))
1037 return -EPERM;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001038 /*
1039 * The sock->type specifies the socket type to use.
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001040 * The CAIF socket is a packet stream in the sense
1041 * that it is packet based. CAIF trusts the reliability
1042 * of the link, no resending is implemented.
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001043 */
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001044 if (sock->type == SOCK_SEQPACKET)
1045 sock->ops = &caif_seqpacket_ops;
1046 else if (sock->type == SOCK_STREAM)
1047 sock->ops = &caif_stream_ops;
1048 else
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001049 return -ESOCKTNOSUPPORT;
1050
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001051 if (protocol < 0 || protocol >= CAIFPROTO_MAX)
1052 return -EPROTONOSUPPORT;
1053 /*
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001054 * Set the socket state to unconnected. The socket state
1055 * is really not used at all in the net/core or socket.c but the
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001056 * initialization makes sure that sock->state is not uninitialized.
1057 */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001058 sk = sk_alloc(net, PF_CAIF, GFP_KERNEL, &prot);
1059 if (!sk)
1060 return -ENOMEM;
1061
1062 cf_sk = container_of(sk, struct caifsock, sk);
1063
1064 /* Store the protocol */
1065 sk->sk_protocol = (unsigned char) protocol;
1066
Dmitry Tarnyagin44764812012-04-12 08:27:24 +00001067 /* Initialize default priority for well-known cases */
1068 switch (protocol) {
1069 case CAIFPROTO_AT:
1070 sk->sk_priority = TC_PRIO_CONTROL;
1071 break;
1072 case CAIFPROTO_RFM:
1073 sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
1074 break;
1075 default:
1076 sk->sk_priority = TC_PRIO_BESTEFFORT;
1077 }
1078
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001079 /*
1080 * Lock in order to try to stop someone from opening the socket
1081 * too early.
1082 */
1083 lock_sock(&(cf_sk->sk));
1084
1085 /* Initialize the nozero default sock structure data. */
1086 sock_init_data(sock, sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001087 sk->sk_destruct = caif_sock_destructor;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001088
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001089 mutex_init(&cf_sk->readlock); /* single task reading lock */
1090 cf_sk->layer.ctrlcmd = caif_ctrl_cb;
1091 cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
1092 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001093
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001094 set_tx_flow_off(cf_sk);
1095 set_rx_flow_on(cf_sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001096
1097 /* Set default options on configuration */
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001098 cf_sk->conn_req.link_selector = CAIF_LINK_LOW_LATENCY;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001099 cf_sk->conn_req.protocol = protocol;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001100 release_sock(&cf_sk->sk);
1101 return 0;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001102}
1103
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001104
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001105static struct net_proto_family caif_family_ops = {
1106 .family = PF_CAIF,
1107 .create = caif_create,
1108 .owner = THIS_MODULE,
1109};
1110
sjur.brandeland@stericsson.com4a695822012-02-03 04:36:19 +00001111static int __init caif_sktinit_module(void)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001112{
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001113 int err = sock_register(&caif_family_ops);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001114 if (!err)
1115 return err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001116 return 0;
1117}
1118
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001119static void __exit caif_sktexit_module(void)
1120{
1121 sock_unregister(PF_CAIF);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001122}
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001123module_init(caif_sktinit_module);
1124module_exit(caif_sktexit_module);