blob: 095259f839023a99a2127d1d4173bb29737471df [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>
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,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000200 enum caif_ctrlcmd flow,
201 int phyid)
202{
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,
Sjur Braendelanddcda1382010-05-21 02:16:12 +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
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000289 skb = skb_recv_datagram(sk, flags, 0 , &ret);
290 if (!skb)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000291 goto read_error;
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000292 copylen = skb->len;
293 if (len < copylen) {
294 m->msg_flags |= MSG_TRUNC;
295 copylen = len;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000296 }
297
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000298 ret = skb_copy_datagram_iovec(skb, 0, m->msg_iov, copylen);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000299 if (ret)
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000300 goto out_free;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000301
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000302 ret = (flags & MSG_TRUNC) ? skb->len : copylen;
303out_free:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000304 skb_free_datagram(sk, skb);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000305 caif_check_flow_release(sk);
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000306 return ret;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000307
308read_error:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000309 return ret;
310}
311
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000312
313/* Copied from unix_stream_wait_data, identical except for lock call. */
314static long caif_stream_data_wait(struct sock *sk, long timeo)
315{
316 DEFINE_WAIT(wait);
317 lock_sock(sk);
318
319 for (;;) {
320 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
321
322 if (!skb_queue_empty(&sk->sk_receive_queue) ||
323 sk->sk_err ||
324 sk->sk_state != CAIF_CONNECTED ||
325 sock_flag(sk, SOCK_DEAD) ||
326 (sk->sk_shutdown & RCV_SHUTDOWN) ||
327 signal_pending(current) ||
328 !timeo)
329 break;
330
331 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
332 release_sock(sk);
333 timeo = schedule_timeout(timeo);
334 lock_sock(sk);
335 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
336 }
337
338 finish_wait(sk_sleep(sk), &wait);
339 release_sock(sk);
340 return timeo;
341}
342
343
344/*
345 * Copied from unix_stream_recvmsg, but removed credit checks,
346 * changed locking calls, changed address handling.
347 */
348static int caif_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
349 struct msghdr *msg, size_t size,
350 int flags)
351{
352 struct sock *sk = sock->sk;
353 int copied = 0;
354 int target;
355 int err = 0;
356 long timeo;
357
358 err = -EOPNOTSUPP;
359 if (flags&MSG_OOB)
360 goto out;
361
362 msg->msg_namelen = 0;
363
364 /*
365 * Lock the socket to prevent queue disordering
366 * while sleeps in memcpy_tomsg
367 */
368 err = -EAGAIN;
369 if (sk->sk_state == CAIF_CONNECTING)
370 goto out;
371
372 caif_read_lock(sk);
373 target = sock_rcvlowat(sk, flags&MSG_WAITALL, size);
374 timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
375
376 do {
377 int chunk;
378 struct sk_buff *skb;
379
380 lock_sock(sk);
381 skb = skb_dequeue(&sk->sk_receive_queue);
382 caif_check_flow_release(sk);
383
384 if (skb == NULL) {
385 if (copied >= target)
386 goto unlock;
387 /*
388 * POSIX 1003.1g mandates this order.
389 */
390 err = sock_error(sk);
391 if (err)
392 goto unlock;
393 err = -ECONNRESET;
394 if (sk->sk_shutdown & RCV_SHUTDOWN)
395 goto unlock;
396
397 err = -EPIPE;
398 if (sk->sk_state != CAIF_CONNECTED)
399 goto unlock;
400 if (sock_flag(sk, SOCK_DEAD))
401 goto unlock;
402
403 release_sock(sk);
404
405 err = -EAGAIN;
406 if (!timeo)
407 break;
408
409 caif_read_unlock(sk);
410
411 timeo = caif_stream_data_wait(sk, timeo);
412
413 if (signal_pending(current)) {
414 err = sock_intr_errno(timeo);
415 goto out;
416 }
417 caif_read_lock(sk);
418 continue;
419unlock:
420 release_sock(sk);
421 break;
422 }
423 release_sock(sk);
424 chunk = min_t(unsigned int, skb->len, size);
425 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
426 skb_queue_head(&sk->sk_receive_queue, skb);
427 if (copied == 0)
428 copied = -EFAULT;
429 break;
430 }
431 copied += chunk;
432 size -= chunk;
433
434 /* Mark read part of skb as used */
435 if (!(flags & MSG_PEEK)) {
436 skb_pull(skb, chunk);
437
438 /* put the skb back if we didn't use it up. */
439 if (skb->len) {
440 skb_queue_head(&sk->sk_receive_queue, skb);
441 break;
442 }
443 kfree_skb(skb);
444
445 } else {
446 /*
447 * It is questionable, see note in unix_dgram_recvmsg.
448 */
449 /* put message back and return */
450 skb_queue_head(&sk->sk_receive_queue, skb);
451 break;
452 }
453 } while (size);
454 caif_read_unlock(sk);
455
456out:
457 return copied ? : err;
458}
459
460/*
461 * Copied from sock.c:sock_wait_for_wmem, but change to wait for
462 * CAIF flow-on and sock_writable.
463 */
464static long caif_wait_for_flow_on(struct caifsock *cf_sk,
465 int wait_writeable, long timeo, int *err)
466{
467 struct sock *sk = &cf_sk->sk;
468 DEFINE_WAIT(wait);
469 for (;;) {
470 *err = 0;
471 if (tx_flow_is_on(cf_sk) &&
472 (!wait_writeable || sock_writeable(&cf_sk->sk)))
473 break;
474 *err = -ETIMEDOUT;
475 if (!timeo)
476 break;
477 *err = -ERESTARTSYS;
478 if (signal_pending(current))
479 break;
480 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
481 *err = -ECONNRESET;
482 if (sk->sk_shutdown & SHUTDOWN_MASK)
483 break;
484 *err = -sk->sk_err;
485 if (sk->sk_err)
486 break;
487 *err = -EPIPE;
488 if (cf_sk->sk.sk_state != CAIF_CONNECTED)
489 break;
490 timeo = schedule_timeout(timeo);
491 }
492 finish_wait(sk_sleep(sk), &wait);
493 return timeo;
494}
495
496/*
497 * Transmit a SKB. The device may temporarily request re-transmission
498 * by returning EAGAIN.
499 */
500static int transmit_skb(struct sk_buff *skb, struct caifsock *cf_sk,
501 int noblock, long timeo)
502{
503 struct cfpkt *pkt;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000504
505 pkt = cfpkt_fromnative(CAIF_DIR_OUT, skb);
sjur.brandeland@stericsson.com3f874ad2011-05-13 02:44:08 +0000506 memset(skb->cb, 0, sizeof(struct caif_payload_info));
Dmitry Tarnyagin44764812012-04-12 08:27:24 +0000507 cfpkt_set_prio(pkt, cf_sk->sk.sk_priority);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000508
Dmitry Tarnyaginba760572012-02-02 01:21:03 +0000509 if (cf_sk->layer.dn == NULL) {
510 kfree_skb(skb);
Sjur Brændeland4dd820c2011-04-11 10:43:51 +0000511 return -EINVAL;
Dmitry Tarnyaginba760572012-02-02 01:21:03 +0000512 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000513
Sjur Brændeland4dd820c2011-04-11 10:43:51 +0000514 return cf_sk->layer.dn->transmit(cf_sk->layer.dn, pkt);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000515}
516
517/* Copied from af_unix:unix_dgram_sendmsg, and adapted to CAIF */
518static int caif_seqpkt_sendmsg(struct kiocb *kiocb, struct socket *sock,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000519 struct msghdr *msg, size_t len)
520{
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000521 struct sock *sk = sock->sk;
522 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000523 int buffer_size;
524 int ret = 0;
525 struct sk_buff *skb = NULL;
526 int noblock;
527 long timeo;
528 caif_assert(cf_sk);
529 ret = sock_error(sk);
530 if (ret)
531 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000532
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000533 ret = -EOPNOTSUPP;
534 if (msg->msg_flags&MSG_OOB)
535 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000536
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000537 ret = -EOPNOTSUPP;
538 if (msg->msg_namelen)
539 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000540
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000541 ret = -EINVAL;
542 if (unlikely(msg->msg_iov->iov_base == NULL))
543 goto err;
544 noblock = msg->msg_flags & MSG_DONTWAIT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000545
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000546 timeo = sock_sndtimeo(sk, noblock);
547 timeo = caif_wait_for_flow_on(container_of(sk, struct caifsock, sk),
548 1, timeo, &ret);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000549
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000550 if (ret)
551 goto err;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000552 ret = -EPIPE;
553 if (cf_sk->sk.sk_state != CAIF_CONNECTED ||
554 sock_flag(sk, SOCK_DEAD) ||
555 (sk->sk_shutdown & RCV_SHUTDOWN))
556 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000557
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000558 /* Error if trying to write more than maximum frame size. */
559 ret = -EMSGSIZE;
560 if (len > cf_sk->maxframe && cf_sk->sk.sk_protocol != CAIFPROTO_RFM)
561 goto err;
562
563 buffer_size = len + cf_sk->headroom + cf_sk->tailroom;
564
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000565 ret = -ENOMEM;
566 skb = sock_alloc_send_skb(sk, buffer_size, noblock, &ret);
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000567
568 if (!skb || skb_tailroom(skb) < buffer_size)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000569 goto err;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000570
571 skb_reserve(skb, cf_sk->headroom);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000572
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000573 ret = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000574
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000575 if (ret)
576 goto err;
577 ret = transmit_skb(skb, cf_sk, noblock, timeo);
578 if (ret < 0)
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000579 /* skb is already freed */
580 return ret;
581
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000582 return len;
583err:
584 kfree_skb(skb);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000585 return ret;
586}
587
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000588/*
589 * Copied from unix_stream_sendmsg and adapted to CAIF:
590 * Changed removed permission handling and added waiting for flow on
591 * and other minor adaptations.
592 */
593static int caif_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
594 struct msghdr *msg, size_t len)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000595{
596 struct sock *sk = sock->sk;
597 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000598 int err, size;
599 struct sk_buff *skb;
600 int sent = 0;
601 long timeo;
602
603 err = -EOPNOTSUPP;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000604 if (unlikely(msg->msg_flags&MSG_OOB))
605 goto out_err;
606
607 if (unlikely(msg->msg_namelen))
608 goto out_err;
609
610 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
611 timeo = caif_wait_for_flow_on(cf_sk, 1, timeo, &err);
612
613 if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN))
614 goto pipe_err;
615
616 while (sent < len) {
617
618 size = len-sent;
619
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000620 if (size > cf_sk->maxframe)
621 size = cf_sk->maxframe;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000622
623 /* If size is more than half of sndbuf, chop up message */
624 if (size > ((sk->sk_sndbuf >> 1) - 64))
625 size = (sk->sk_sndbuf >> 1) - 64;
626
627 if (size > SKB_MAX_ALLOC)
628 size = SKB_MAX_ALLOC;
629
630 skb = sock_alloc_send_skb(sk,
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000631 size + cf_sk->headroom +
632 cf_sk->tailroom,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000633 msg->msg_flags&MSG_DONTWAIT,
634 &err);
635 if (skb == NULL)
636 goto out_err;
637
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000638 skb_reserve(skb, cf_sk->headroom);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000639 /*
640 * If you pass two values to the sock_alloc_send_skb
641 * it tries to grab the large buffer with GFP_NOFS
642 * (which can fail easily), and if it fails grab the
643 * fallback size buffer which is under a page and will
644 * succeed. [Alan]
645 */
646 size = min_t(int, size, skb_tailroom(skb));
647
648 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
649 if (err) {
650 kfree_skb(skb);
651 goto out_err;
652 }
653 err = transmit_skb(skb, cf_sk,
654 msg->msg_flags&MSG_DONTWAIT, timeo);
Dmitry Tarnyaginba760572012-02-02 01:21:03 +0000655 if (err < 0)
656 /* skb is already freed */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000657 goto pipe_err;
Dmitry Tarnyaginba760572012-02-02 01:21:03 +0000658
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000659 sent += size;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000660 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000661
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000662 return sent;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000663
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000664pipe_err:
665 if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL))
666 send_sig(SIGPIPE, current, 0);
667 err = -EPIPE;
668out_err:
669 return sent ? : err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000670}
671
672static int setsockopt(struct socket *sock,
673 int lvl, int opt, char __user *ov, unsigned int ol)
674{
675 struct sock *sk = sock->sk;
676 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +0000677 int linksel;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000678
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000679 if (cf_sk->sk.sk_socket->state != SS_UNCONNECTED)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000680 return -ENOPROTOOPT;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000681
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000682 switch (opt) {
683 case CAIFSO_LINK_SELECT:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000684 if (ol < sizeof(int))
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000685 return -EINVAL;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000686 if (lvl != SOL_CAIF)
687 goto bad_sol;
688 if (copy_from_user(&linksel, ov, sizeof(int)))
689 return -EINVAL;
690 lock_sock(&(cf_sk->sk));
691 cf_sk->conn_req.link_selector = linksel;
692 release_sock(&cf_sk->sk);
693 return 0;
694
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000695 case CAIFSO_REQ_PARAM:
696 if (lvl != SOL_CAIF)
697 goto bad_sol;
698 if (cf_sk->sk.sk_protocol != CAIFPROTO_UTIL)
699 return -ENOPROTOOPT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000700 lock_sock(&(cf_sk->sk));
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000701 if (ol > sizeof(cf_sk->conn_req.param.data) ||
702 copy_from_user(&cf_sk->conn_req.param.data, ov, ol)) {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000703 release_sock(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000704 return -EINVAL;
705 }
Dan Rosenberg91b5c982011-01-10 16:00:54 -0800706 cf_sk->conn_req.param.size = ol;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000707 release_sock(&cf_sk->sk);
708 return 0;
709
710 default:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000711 return -ENOPROTOOPT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000712 }
713
714 return 0;
715bad_sol:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000716 return -ENOPROTOOPT;
717
718}
719
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000720/*
721 * caif_connect() - Connect a CAIF Socket
722 * Copied and modified af_irda.c:irda_connect().
723 *
724 * Note : by consulting "errno", the user space caller may learn the cause
725 * of the failure. Most of them are visible in the function, others may come
726 * from subroutines called and are listed here :
727 * o -EAFNOSUPPORT: bad socket family or type.
728 * o -ESOCKTNOSUPPORT: bad socket type or protocol
729 * o -EINVAL: bad socket address, or CAIF link type
730 * o -ECONNREFUSED: remote end refused the connection.
731 * o -EINPROGRESS: connect request sent but timed out (or non-blocking)
732 * o -EISCONN: already connected.
733 * o -ETIMEDOUT: Connection timed out (send timeout)
734 * o -ENODEV: No link layer to send request
735 * o -ECONNRESET: Received Shutdown indication or lost link layer
736 * o -ENOMEM: Out of memory
737 *
738 * State Strategy:
739 * o sk_state: holds the CAIF_* protocol state, it's updated by
740 * caif_ctrl_cb.
741 * o sock->state: holds the SS_* socket state and is updated by connect and
742 * disconnect.
743 */
744static int caif_connect(struct socket *sock, struct sockaddr *uaddr,
745 int addr_len, int flags)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000746{
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000747 struct sock *sk = sock->sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000748 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
749 long timeo;
750 int err;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000751 int ifindex, headroom, tailroom;
Eric Dumazet79315062010-10-04 22:42:08 +0000752 unsigned int mtu;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000753 struct net_device *dev;
754
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000755 lock_sock(sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000756
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000757 err = -EAFNOSUPPORT;
758 if (uaddr->sa_family != AF_CAIF)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000759 goto out;
760
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000761 switch (sock->state) {
762 case SS_UNCONNECTED:
763 /* Normal case, a fresh connect */
764 caif_assert(sk->sk_state == CAIF_DISCONNECTED);
765 break;
766 case SS_CONNECTING:
767 switch (sk->sk_state) {
768 case CAIF_CONNECTED:
769 sock->state = SS_CONNECTED;
770 err = -EISCONN;
771 goto out;
772 case CAIF_DISCONNECTED:
773 /* Reconnect allowed */
774 break;
775 case CAIF_CONNECTING:
776 err = -EALREADY;
777 if (flags & O_NONBLOCK)
778 goto out;
779 goto wait_connect;
780 }
781 break;
782 case SS_CONNECTED:
783 caif_assert(sk->sk_state == CAIF_CONNECTED ||
784 sk->sk_state == CAIF_DISCONNECTED);
785 if (sk->sk_shutdown & SHUTDOWN_MASK) {
786 /* Allow re-connect after SHUTDOWN_IND */
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000787 caif_disconnect_client(sock_net(sk), &cf_sk->layer);
sjur.brandeland@stericsson.com54e90fb2011-05-22 11:18:51 +0000788 caif_free_client(&cf_sk->layer);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000789 break;
790 }
791 /* No reconnect on a seqpacket socket */
792 err = -EISCONN;
793 goto out;
794 case SS_DISCONNECTING:
795 case SS_FREE:
796 caif_assert(1); /*Should never happen */
797 break;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000798 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000799 sk->sk_state = CAIF_DISCONNECTED;
800 sock->state = SS_UNCONNECTED;
801 sk_stream_kill_queues(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000802
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000803 err = -EINVAL;
Dan Carpenterf5d72af2010-05-22 10:43:42 +0000804 if (addr_len != sizeof(struct sockaddr_caif))
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000805 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000806
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000807 memcpy(&cf_sk->conn_req.sockaddr, uaddr,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000808 sizeof(struct sockaddr_caif));
809
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000810 /* Move to connecting socket, start sending Connect Requests */
811 sock->state = SS_CONNECTING;
812 sk->sk_state = CAIF_CONNECTING;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000813
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +0000814 /* Check priority value comming from socket */
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +0000815 /* if priority value is out of range it will be ajusted */
816 if (cf_sk->sk.sk_priority > CAIF_PRIO_MAX)
817 cf_sk->conn_req.priority = CAIF_PRIO_MAX;
818 else if (cf_sk->sk.sk_priority < CAIF_PRIO_MIN)
819 cf_sk->conn_req.priority = CAIF_PRIO_MIN;
820 else
821 cf_sk->conn_req.priority = cf_sk->sk.sk_priority;
822
823 /*ifindex = id of the interface.*/
824 cf_sk->conn_req.ifindex = cf_sk->sk.sk_bound_dev_if;
825
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000826 cf_sk->layer.receive = caif_sktrecv_cb;
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000827
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000828 err = caif_connect_client(sock_net(sk), &cf_sk->conn_req,
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000829 &cf_sk->layer, &ifindex, &headroom, &tailroom);
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000830
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000831 if (err < 0) {
832 cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
833 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
834 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000835 }
Eric Dumazet79315062010-10-04 22:42:08 +0000836
837 err = -ENODEV;
838 rcu_read_lock();
839 dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
840 if (!dev) {
841 rcu_read_unlock();
842 goto out;
843 }
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000844 cf_sk->headroom = LL_RESERVED_SPACE_EXTRA(dev, headroom);
Eric Dumazet79315062010-10-04 22:42:08 +0000845 mtu = dev->mtu;
846 rcu_read_unlock();
847
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000848 cf_sk->tailroom = tailroom;
Eric Dumazet79315062010-10-04 22:42:08 +0000849 cf_sk->maxframe = mtu - (headroom + tailroom);
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000850 if (cf_sk->maxframe < 1) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000851 pr_warn("CAIF Interface MTU too small (%d)\n", dev->mtu);
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +0000852 err = -ENODEV;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000853 goto out;
854 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000855
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000856 err = -EINPROGRESS;
857wait_connect:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000858
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000859 if (sk->sk_state != CAIF_CONNECTED && (flags & O_NONBLOCK))
860 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000861
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000862 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000863
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000864 release_sock(sk);
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000865 err = -ERESTARTSYS;
866 timeo = wait_event_interruptible_timeout(*sk_sleep(sk),
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000867 sk->sk_state != CAIF_CONNECTING,
868 timeo);
869 lock_sock(sk);
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000870 if (timeo < 0)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000871 goto out; /* -ERESTARTSYS */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000872
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000873 err = -ETIMEDOUT;
874 if (timeo == 0 && sk->sk_state != CAIF_CONNECTED)
875 goto out;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000876 if (sk->sk_state != CAIF_CONNECTED) {
877 sock->state = SS_UNCONNECTED;
878 err = sock_error(sk);
879 if (!err)
880 err = -ECONNREFUSED;
881 goto out;
882 }
883 sock->state = SS_CONNECTED;
884 err = 0;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000885out:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000886 release_sock(sk);
887 return err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000888}
889
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000890/*
891 * caif_release() - Disconnect a CAIF Socket
892 * Copied and modified af_irda.c:irda_release().
893 */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000894static int caif_release(struct socket *sock)
895{
896 struct sock *sk = sock->sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000897 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000898
899 if (!sk)
900 return 0;
901
902 set_tx_flow_off(cf_sk);
903
904 /*
905 * Ensure that packets are not queued after this point in time.
906 * caif_queue_rcv_skb checks SOCK_DEAD holding the queue lock,
907 * this ensures no packets when sock is dead.
908 */
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000909 spin_lock_bh(&sk->sk_receive_queue.lock);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000910 sock_set_flag(sk, SOCK_DEAD);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000911 spin_unlock_bh(&sk->sk_receive_queue.lock);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000912 sock->sk = NULL;
913
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +0000914 WARN_ON(IS_ERR(cf_sk->debugfs_socket_dir));
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000915 if (cf_sk->debugfs_socket_dir != NULL)
916 debugfs_remove_recursive(cf_sk->debugfs_socket_dir);
917
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000918 lock_sock(&(cf_sk->sk));
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000919 sk->sk_state = CAIF_DISCONNECTED;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000920 sk->sk_shutdown = SHUTDOWN_MASK;
921
sjur.brandeland@stericsson.com54e90fb2011-05-22 11:18:51 +0000922 caif_disconnect_client(sock_net(sk), &cf_sk->layer);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000923 cf_sk->sk.sk_socket->state = SS_DISCONNECTING;
924 wake_up_interruptible_poll(sk_sleep(sk), POLLERR|POLLHUP);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000925
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000926 sock_orphan(sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000927 sk_stream_kill_queues(&cf_sk->sk);
928 release_sock(sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000929 sock_put(sk);
sjur.brandeland@stericsson.com54e90fb2011-05-22 11:18:51 +0000930 return 0;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000931}
932
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000933/* Copied from af_unix.c:unix_poll(), added CAIF tx_flow handling */
934static unsigned int caif_poll(struct file *file,
935 struct socket *sock, poll_table *wait)
936{
937 struct sock *sk = sock->sk;
938 unsigned int mask;
939 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
940
941 sock_poll_wait(file, sk_sleep(sk), wait);
942 mask = 0;
943
944 /* exceptional events? */
945 if (sk->sk_err)
946 mask |= POLLERR;
947 if (sk->sk_shutdown == SHUTDOWN_MASK)
948 mask |= POLLHUP;
949 if (sk->sk_shutdown & RCV_SHUTDOWN)
950 mask |= POLLRDHUP;
951
952 /* readable? */
953 if (!skb_queue_empty(&sk->sk_receive_queue) ||
954 (sk->sk_shutdown & RCV_SHUTDOWN))
955 mask |= POLLIN | POLLRDNORM;
956
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000957 /*
958 * we set writable also when the other side has shut down the
959 * connection. This prevents stuck sockets.
960 */
961 if (sock_writeable(sk) && tx_flow_is_on(cf_sk))
962 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
963
964 return mask;
965}
966
967static const struct proto_ops caif_seqpacket_ops = {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000968 .family = PF_CAIF,
969 .owner = THIS_MODULE,
970 .release = caif_release,
971 .bind = sock_no_bind,
972 .connect = caif_connect,
973 .socketpair = sock_no_socketpair,
974 .accept = sock_no_accept,
975 .getname = sock_no_getname,
976 .poll = caif_poll,
977 .ioctl = sock_no_ioctl,
978 .listen = sock_no_listen,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000979 .shutdown = sock_no_shutdown,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000980 .setsockopt = setsockopt,
981 .getsockopt = sock_no_getsockopt,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000982 .sendmsg = caif_seqpkt_sendmsg,
983 .recvmsg = caif_seqpkt_recvmsg,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000984 .mmap = sock_no_mmap,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000985 .sendpage = sock_no_sendpage,
986};
987
988static const struct proto_ops caif_stream_ops = {
989 .family = PF_CAIF,
990 .owner = THIS_MODULE,
991 .release = caif_release,
992 .bind = sock_no_bind,
993 .connect = caif_connect,
994 .socketpair = sock_no_socketpair,
995 .accept = sock_no_accept,
996 .getname = sock_no_getname,
997 .poll = caif_poll,
998 .ioctl = sock_no_ioctl,
999 .listen = sock_no_listen,
1000 .shutdown = sock_no_shutdown,
1001 .setsockopt = setsockopt,
1002 .getsockopt = sock_no_getsockopt,
1003 .sendmsg = caif_stream_sendmsg,
1004 .recvmsg = caif_stream_recvmsg,
1005 .mmap = sock_no_mmap,
1006 .sendpage = sock_no_sendpage,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001007};
1008
1009/* This function is called when a socket is finally destroyed. */
1010static void caif_sock_destructor(struct sock *sk)
1011{
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001012 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001013 caif_assert(!atomic_read(&sk->sk_wmem_alloc));
1014 caif_assert(sk_unhashed(sk));
1015 caif_assert(!sk->sk_socket);
1016 if (!sock_flag(sk, SOCK_DEAD)) {
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +00001017 pr_debug("Attempt to release alive CAIF socket: %p\n", sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001018 return;
1019 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001020 sk_stream_kill_queues(&cf_sk->sk);
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +00001021 caif_free_client(&cf_sk->layer);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001022}
1023
1024static int caif_create(struct net *net, struct socket *sock, int protocol,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001025 int kern)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001026{
1027 struct sock *sk = NULL;
1028 struct caifsock *cf_sk = NULL;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001029 static struct proto prot = {.name = "PF_CAIF",
1030 .owner = THIS_MODULE,
1031 .obj_size = sizeof(struct caifsock),
1032 };
1033
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001034 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_NET_ADMIN))
1035 return -EPERM;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001036 /*
1037 * The sock->type specifies the socket type to use.
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001038 * The CAIF socket is a packet stream in the sense
1039 * that it is packet based. CAIF trusts the reliability
1040 * of the link, no resending is implemented.
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001041 */
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001042 if (sock->type == SOCK_SEQPACKET)
1043 sock->ops = &caif_seqpacket_ops;
1044 else if (sock->type == SOCK_STREAM)
1045 sock->ops = &caif_stream_ops;
1046 else
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001047 return -ESOCKTNOSUPPORT;
1048
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001049 if (protocol < 0 || protocol >= CAIFPROTO_MAX)
1050 return -EPROTONOSUPPORT;
1051 /*
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001052 * Set the socket state to unconnected. The socket state
1053 * is really not used at all in the net/core or socket.c but the
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001054 * initialization makes sure that sock->state is not uninitialized.
1055 */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001056 sk = sk_alloc(net, PF_CAIF, GFP_KERNEL, &prot);
1057 if (!sk)
1058 return -ENOMEM;
1059
1060 cf_sk = container_of(sk, struct caifsock, sk);
1061
1062 /* Store the protocol */
1063 sk->sk_protocol = (unsigned char) protocol;
1064
Dmitry Tarnyagin44764812012-04-12 08:27:24 +00001065 /* Initialize default priority for well-known cases */
1066 switch (protocol) {
1067 case CAIFPROTO_AT:
1068 sk->sk_priority = TC_PRIO_CONTROL;
1069 break;
1070 case CAIFPROTO_RFM:
1071 sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
1072 break;
1073 default:
1074 sk->sk_priority = TC_PRIO_BESTEFFORT;
1075 }
1076
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001077 /*
1078 * Lock in order to try to stop someone from opening the socket
1079 * too early.
1080 */
1081 lock_sock(&(cf_sk->sk));
1082
1083 /* Initialize the nozero default sock structure data. */
1084 sock_init_data(sock, sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001085 sk->sk_destruct = caif_sock_destructor;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001086
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001087 mutex_init(&cf_sk->readlock); /* single task reading lock */
1088 cf_sk->layer.ctrlcmd = caif_ctrl_cb;
1089 cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
1090 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001091
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001092 set_tx_flow_off(cf_sk);
1093 set_rx_flow_on(cf_sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001094
1095 /* Set default options on configuration */
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001096 cf_sk->conn_req.link_selector = CAIF_LINK_LOW_LATENCY;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001097 cf_sk->conn_req.protocol = protocol;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001098 release_sock(&cf_sk->sk);
1099 return 0;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001100}
1101
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001102
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001103static struct net_proto_family caif_family_ops = {
1104 .family = PF_CAIF,
1105 .create = caif_create,
1106 .owner = THIS_MODULE,
1107};
1108
sjur.brandeland@stericsson.com4a695822012-02-03 04:36:19 +00001109static int __init caif_sktinit_module(void)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001110{
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001111 int err = sock_register(&caif_family_ops);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001112 if (!err)
1113 return err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001114 return 0;
1115}
1116
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001117static void __exit caif_sktexit_module(void)
1118{
1119 sock_unregister(PF_CAIF);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001120}
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001121module_init(caif_sktinit_module);
1122module_exit(caif_sktexit_module);