blob: fb8944355264689b4e61ba9f9402fe8cad902420 [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;
144 if (!sk_rmem_schedule(sk, skb->truesize) && rx_flow_is_on(cf_sk)) {
145 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);
223 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000224 break;
225
226 case CAIF_CTRLCMD_DEINIT_RSP:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000227 /* We're now disconnected */
228 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
229 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000230 break;
231
232 case CAIF_CTRLCMD_INIT_FAIL_RSP:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000233 /* Connect request failed */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000234 cf_sk->sk.sk_err = ECONNREFUSED;
235 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
236 cf_sk->sk.sk_shutdown = SHUTDOWN_MASK;
237 /*
238 * Socket "standards" seems to require POLLOUT to
239 * be set at connect failure.
240 */
241 set_tx_flow_on(cf_sk);
242 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000243 break;
244
245 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000246 /* Modem has closed this connection, or device is down. */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000247 cf_sk->sk.sk_shutdown = SHUTDOWN_MASK;
248 cf_sk->sk.sk_err = ECONNRESET;
249 set_rx_flow_on(cf_sk);
250 cf_sk->sk.sk_error_report(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000251 break;
252
253 default:
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000254 pr_debug("Unexpected flow command %d\n", flow);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000255 }
256}
257
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000258static void caif_check_flow_release(struct sock *sk)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000259{
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000260 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
261
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000262 if (rx_flow_is_on(cf_sk))
263 return;
264
265 if (atomic_read(&sk->sk_rmem_alloc) <= sk_rcvbuf_lowwater(cf_sk)) {
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000266 set_rx_flow_on(cf_sk);
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000267 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_ON_REQ);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000268 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000269}
270
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000271/*
272 * Copied from unix_dgram_recvmsg, but removed credit checks,
273 * changed locking, address handling and added MSG_TRUNC.
274 */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000275static int caif_seqpkt_recvmsg(struct kiocb *iocb, struct socket *sock,
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000276 struct msghdr *m, size_t len, int flags)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000277
278{
279 struct sock *sk = sock->sk;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000280 struct sk_buff *skb;
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000281 int ret;
282 int copylen;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000283
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000284 ret = -EOPNOTSUPP;
285 if (m->msg_flags&MSG_OOB)
286 goto read_error;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000287
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000288 skb = skb_recv_datagram(sk, flags, 0 , &ret);
289 if (!skb)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000290 goto read_error;
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000291 copylen = skb->len;
292 if (len < copylen) {
293 m->msg_flags |= MSG_TRUNC;
294 copylen = len;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000295 }
296
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000297 ret = skb_copy_datagram_iovec(skb, 0, m->msg_iov, copylen);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000298 if (ret)
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000299 goto out_free;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000300
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000301 ret = (flags & MSG_TRUNC) ? skb->len : copylen;
302out_free:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000303 skb_free_datagram(sk, skb);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000304 caif_check_flow_release(sk);
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000305 return ret;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000306
307read_error:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000308 return ret;
309}
310
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000311
312/* Copied from unix_stream_wait_data, identical except for lock call. */
313static long caif_stream_data_wait(struct sock *sk, long timeo)
314{
315 DEFINE_WAIT(wait);
316 lock_sock(sk);
317
318 for (;;) {
319 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
320
321 if (!skb_queue_empty(&sk->sk_receive_queue) ||
322 sk->sk_err ||
323 sk->sk_state != CAIF_CONNECTED ||
324 sock_flag(sk, SOCK_DEAD) ||
325 (sk->sk_shutdown & RCV_SHUTDOWN) ||
326 signal_pending(current) ||
327 !timeo)
328 break;
329
330 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
331 release_sock(sk);
332 timeo = schedule_timeout(timeo);
333 lock_sock(sk);
334 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
335 }
336
337 finish_wait(sk_sleep(sk), &wait);
338 release_sock(sk);
339 return timeo;
340}
341
342
343/*
344 * Copied from unix_stream_recvmsg, but removed credit checks,
345 * changed locking calls, changed address handling.
346 */
347static int caif_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
348 struct msghdr *msg, size_t size,
349 int flags)
350{
351 struct sock *sk = sock->sk;
352 int copied = 0;
353 int target;
354 int err = 0;
355 long timeo;
356
357 err = -EOPNOTSUPP;
358 if (flags&MSG_OOB)
359 goto out;
360
361 msg->msg_namelen = 0;
362
363 /*
364 * Lock the socket to prevent queue disordering
365 * while sleeps in memcpy_tomsg
366 */
367 err = -EAGAIN;
368 if (sk->sk_state == CAIF_CONNECTING)
369 goto out;
370
371 caif_read_lock(sk);
372 target = sock_rcvlowat(sk, flags&MSG_WAITALL, size);
373 timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
374
375 do {
376 int chunk;
377 struct sk_buff *skb;
378
379 lock_sock(sk);
380 skb = skb_dequeue(&sk->sk_receive_queue);
381 caif_check_flow_release(sk);
382
383 if (skb == NULL) {
384 if (copied >= target)
385 goto unlock;
386 /*
387 * POSIX 1003.1g mandates this order.
388 */
389 err = sock_error(sk);
390 if (err)
391 goto unlock;
392 err = -ECONNRESET;
393 if (sk->sk_shutdown & RCV_SHUTDOWN)
394 goto unlock;
395
396 err = -EPIPE;
397 if (sk->sk_state != CAIF_CONNECTED)
398 goto unlock;
399 if (sock_flag(sk, SOCK_DEAD))
400 goto unlock;
401
402 release_sock(sk);
403
404 err = -EAGAIN;
405 if (!timeo)
406 break;
407
408 caif_read_unlock(sk);
409
410 timeo = caif_stream_data_wait(sk, timeo);
411
412 if (signal_pending(current)) {
413 err = sock_intr_errno(timeo);
414 goto out;
415 }
416 caif_read_lock(sk);
417 continue;
418unlock:
419 release_sock(sk);
420 break;
421 }
422 release_sock(sk);
423 chunk = min_t(unsigned int, skb->len, size);
424 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
425 skb_queue_head(&sk->sk_receive_queue, skb);
426 if (copied == 0)
427 copied = -EFAULT;
428 break;
429 }
430 copied += chunk;
431 size -= chunk;
432
433 /* Mark read part of skb as used */
434 if (!(flags & MSG_PEEK)) {
435 skb_pull(skb, chunk);
436
437 /* put the skb back if we didn't use it up. */
438 if (skb->len) {
439 skb_queue_head(&sk->sk_receive_queue, skb);
440 break;
441 }
442 kfree_skb(skb);
443
444 } else {
445 /*
446 * It is questionable, see note in unix_dgram_recvmsg.
447 */
448 /* put message back and return */
449 skb_queue_head(&sk->sk_receive_queue, skb);
450 break;
451 }
452 } while (size);
453 caif_read_unlock(sk);
454
455out:
456 return copied ? : err;
457}
458
459/*
460 * Copied from sock.c:sock_wait_for_wmem, but change to wait for
461 * CAIF flow-on and sock_writable.
462 */
463static long caif_wait_for_flow_on(struct caifsock *cf_sk,
464 int wait_writeable, long timeo, int *err)
465{
466 struct sock *sk = &cf_sk->sk;
467 DEFINE_WAIT(wait);
468 for (;;) {
469 *err = 0;
470 if (tx_flow_is_on(cf_sk) &&
471 (!wait_writeable || sock_writeable(&cf_sk->sk)))
472 break;
473 *err = -ETIMEDOUT;
474 if (!timeo)
475 break;
476 *err = -ERESTARTSYS;
477 if (signal_pending(current))
478 break;
479 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
480 *err = -ECONNRESET;
481 if (sk->sk_shutdown & SHUTDOWN_MASK)
482 break;
483 *err = -sk->sk_err;
484 if (sk->sk_err)
485 break;
486 *err = -EPIPE;
487 if (cf_sk->sk.sk_state != CAIF_CONNECTED)
488 break;
489 timeo = schedule_timeout(timeo);
490 }
491 finish_wait(sk_sleep(sk), &wait);
492 return timeo;
493}
494
495/*
496 * Transmit a SKB. The device may temporarily request re-transmission
497 * by returning EAGAIN.
498 */
499static int transmit_skb(struct sk_buff *skb, struct caifsock *cf_sk,
500 int noblock, long timeo)
501{
502 struct cfpkt *pkt;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000503
504 pkt = cfpkt_fromnative(CAIF_DIR_OUT, skb);
sjur.brandeland@stericsson.com3f874ad2011-05-13 02:44:08 +0000505 memset(skb->cb, 0, sizeof(struct caif_payload_info));
Dmitry Tarnyagin44764812012-04-12 08:27:24 +0000506 cfpkt_set_prio(pkt, cf_sk->sk.sk_priority);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000507
Dmitry Tarnyaginba760572012-02-02 01:21:03 +0000508 if (cf_sk->layer.dn == NULL) {
509 kfree_skb(skb);
Sjur Brændeland4dd820c2011-04-11 10:43:51 +0000510 return -EINVAL;
Dmitry Tarnyaginba760572012-02-02 01:21:03 +0000511 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000512
Sjur Brændeland4dd820c2011-04-11 10:43:51 +0000513 return cf_sk->layer.dn->transmit(cf_sk->layer.dn, pkt);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000514}
515
516/* Copied from af_unix:unix_dgram_sendmsg, and adapted to CAIF */
517static int caif_seqpkt_sendmsg(struct kiocb *kiocb, struct socket *sock,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000518 struct msghdr *msg, size_t len)
519{
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000520 struct sock *sk = sock->sk;
521 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000522 int buffer_size;
523 int ret = 0;
524 struct sk_buff *skb = NULL;
525 int noblock;
526 long timeo;
527 caif_assert(cf_sk);
528 ret = sock_error(sk);
529 if (ret)
530 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000531
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000532 ret = -EOPNOTSUPP;
533 if (msg->msg_flags&MSG_OOB)
534 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000535
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000536 ret = -EOPNOTSUPP;
537 if (msg->msg_namelen)
538 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000539
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000540 ret = -EINVAL;
541 if (unlikely(msg->msg_iov->iov_base == NULL))
542 goto err;
543 noblock = msg->msg_flags & MSG_DONTWAIT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000544
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000545 timeo = sock_sndtimeo(sk, noblock);
546 timeo = caif_wait_for_flow_on(container_of(sk, struct caifsock, sk),
547 1, timeo, &ret);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000548
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000549 if (ret)
550 goto err;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000551 ret = -EPIPE;
552 if (cf_sk->sk.sk_state != CAIF_CONNECTED ||
553 sock_flag(sk, SOCK_DEAD) ||
554 (sk->sk_shutdown & RCV_SHUTDOWN))
555 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000556
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000557 /* Error if trying to write more than maximum frame size. */
558 ret = -EMSGSIZE;
559 if (len > cf_sk->maxframe && cf_sk->sk.sk_protocol != CAIFPROTO_RFM)
560 goto err;
561
562 buffer_size = len + cf_sk->headroom + cf_sk->tailroom;
563
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000564 ret = -ENOMEM;
565 skb = sock_alloc_send_skb(sk, buffer_size, noblock, &ret);
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000566
567 if (!skb || skb_tailroom(skb) < buffer_size)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000568 goto err;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000569
570 skb_reserve(skb, cf_sk->headroom);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000571
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000572 ret = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000573
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000574 if (ret)
575 goto err;
576 ret = transmit_skb(skb, cf_sk, noblock, timeo);
577 if (ret < 0)
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000578 /* skb is already freed */
579 return ret;
580
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000581 return len;
582err:
583 kfree_skb(skb);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000584 return ret;
585}
586
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000587/*
588 * Copied from unix_stream_sendmsg and adapted to CAIF:
589 * Changed removed permission handling and added waiting for flow on
590 * and other minor adaptations.
591 */
592static int caif_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
593 struct msghdr *msg, size_t len)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000594{
595 struct sock *sk = sock->sk;
596 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000597 int err, size;
598 struct sk_buff *skb;
599 int sent = 0;
600 long timeo;
601
602 err = -EOPNOTSUPP;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000603 if (unlikely(msg->msg_flags&MSG_OOB))
604 goto out_err;
605
606 if (unlikely(msg->msg_namelen))
607 goto out_err;
608
609 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
610 timeo = caif_wait_for_flow_on(cf_sk, 1, timeo, &err);
611
612 if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN))
613 goto pipe_err;
614
615 while (sent < len) {
616
617 size = len-sent;
618
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000619 if (size > cf_sk->maxframe)
620 size = cf_sk->maxframe;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000621
622 /* If size is more than half of sndbuf, chop up message */
623 if (size > ((sk->sk_sndbuf >> 1) - 64))
624 size = (sk->sk_sndbuf >> 1) - 64;
625
626 if (size > SKB_MAX_ALLOC)
627 size = SKB_MAX_ALLOC;
628
629 skb = sock_alloc_send_skb(sk,
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000630 size + cf_sk->headroom +
631 cf_sk->tailroom,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000632 msg->msg_flags&MSG_DONTWAIT,
633 &err);
634 if (skb == NULL)
635 goto out_err;
636
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000637 skb_reserve(skb, cf_sk->headroom);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000638 /*
639 * If you pass two values to the sock_alloc_send_skb
640 * it tries to grab the large buffer with GFP_NOFS
641 * (which can fail easily), and if it fails grab the
642 * fallback size buffer which is under a page and will
643 * succeed. [Alan]
644 */
645 size = min_t(int, size, skb_tailroom(skb));
646
647 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
648 if (err) {
649 kfree_skb(skb);
650 goto out_err;
651 }
652 err = transmit_skb(skb, cf_sk,
653 msg->msg_flags&MSG_DONTWAIT, timeo);
Dmitry Tarnyaginba760572012-02-02 01:21:03 +0000654 if (err < 0)
655 /* skb is already freed */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000656 goto pipe_err;
Dmitry Tarnyaginba760572012-02-02 01:21:03 +0000657
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000658 sent += size;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000659 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000660
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000661 return sent;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000662
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000663pipe_err:
664 if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL))
665 send_sig(SIGPIPE, current, 0);
666 err = -EPIPE;
667out_err:
668 return sent ? : err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000669}
670
671static int setsockopt(struct socket *sock,
672 int lvl, int opt, char __user *ov, unsigned int ol)
673{
674 struct sock *sk = sock->sk;
675 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +0000676 int linksel;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000677
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000678 if (cf_sk->sk.sk_socket->state != SS_UNCONNECTED)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000679 return -ENOPROTOOPT;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000680
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000681 switch (opt) {
682 case CAIFSO_LINK_SELECT:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000683 if (ol < sizeof(int))
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000684 return -EINVAL;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000685 if (lvl != SOL_CAIF)
686 goto bad_sol;
687 if (copy_from_user(&linksel, ov, sizeof(int)))
688 return -EINVAL;
689 lock_sock(&(cf_sk->sk));
690 cf_sk->conn_req.link_selector = linksel;
691 release_sock(&cf_sk->sk);
692 return 0;
693
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000694 case CAIFSO_REQ_PARAM:
695 if (lvl != SOL_CAIF)
696 goto bad_sol;
697 if (cf_sk->sk.sk_protocol != CAIFPROTO_UTIL)
698 return -ENOPROTOOPT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000699 lock_sock(&(cf_sk->sk));
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000700 if (ol > sizeof(cf_sk->conn_req.param.data) ||
701 copy_from_user(&cf_sk->conn_req.param.data, ov, ol)) {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000702 release_sock(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000703 return -EINVAL;
704 }
Dan Rosenberg91b5c982011-01-10 16:00:54 -0800705 cf_sk->conn_req.param.size = ol;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000706 release_sock(&cf_sk->sk);
707 return 0;
708
709 default:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000710 return -ENOPROTOOPT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000711 }
712
713 return 0;
714bad_sol:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000715 return -ENOPROTOOPT;
716
717}
718
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000719/*
720 * caif_connect() - Connect a CAIF Socket
721 * Copied and modified af_irda.c:irda_connect().
722 *
723 * Note : by consulting "errno", the user space caller may learn the cause
724 * of the failure. Most of them are visible in the function, others may come
725 * from subroutines called and are listed here :
726 * o -EAFNOSUPPORT: bad socket family or type.
727 * o -ESOCKTNOSUPPORT: bad socket type or protocol
728 * o -EINVAL: bad socket address, or CAIF link type
729 * o -ECONNREFUSED: remote end refused the connection.
730 * o -EINPROGRESS: connect request sent but timed out (or non-blocking)
731 * o -EISCONN: already connected.
732 * o -ETIMEDOUT: Connection timed out (send timeout)
733 * o -ENODEV: No link layer to send request
734 * o -ECONNRESET: Received Shutdown indication or lost link layer
735 * o -ENOMEM: Out of memory
736 *
737 * State Strategy:
738 * o sk_state: holds the CAIF_* protocol state, it's updated by
739 * caif_ctrl_cb.
740 * o sock->state: holds the SS_* socket state and is updated by connect and
741 * disconnect.
742 */
743static int caif_connect(struct socket *sock, struct sockaddr *uaddr,
744 int addr_len, int flags)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000745{
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000746 struct sock *sk = sock->sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000747 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
748 long timeo;
749 int err;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000750 int ifindex, headroom, tailroom;
Eric Dumazet79315062010-10-04 22:42:08 +0000751 unsigned int mtu;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000752 struct net_device *dev;
753
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000754 lock_sock(sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000755
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000756 err = -EAFNOSUPPORT;
757 if (uaddr->sa_family != AF_CAIF)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000758 goto out;
759
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000760 switch (sock->state) {
761 case SS_UNCONNECTED:
762 /* Normal case, a fresh connect */
763 caif_assert(sk->sk_state == CAIF_DISCONNECTED);
764 break;
765 case SS_CONNECTING:
766 switch (sk->sk_state) {
767 case CAIF_CONNECTED:
768 sock->state = SS_CONNECTED;
769 err = -EISCONN;
770 goto out;
771 case CAIF_DISCONNECTED:
772 /* Reconnect allowed */
773 break;
774 case CAIF_CONNECTING:
775 err = -EALREADY;
776 if (flags & O_NONBLOCK)
777 goto out;
778 goto wait_connect;
779 }
780 break;
781 case SS_CONNECTED:
782 caif_assert(sk->sk_state == CAIF_CONNECTED ||
783 sk->sk_state == CAIF_DISCONNECTED);
784 if (sk->sk_shutdown & SHUTDOWN_MASK) {
785 /* Allow re-connect after SHUTDOWN_IND */
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000786 caif_disconnect_client(sock_net(sk), &cf_sk->layer);
sjur.brandeland@stericsson.com54e90fb2011-05-22 11:18:51 +0000787 caif_free_client(&cf_sk->layer);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000788 break;
789 }
790 /* No reconnect on a seqpacket socket */
791 err = -EISCONN;
792 goto out;
793 case SS_DISCONNECTING:
794 case SS_FREE:
795 caif_assert(1); /*Should never happen */
796 break;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000797 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000798 sk->sk_state = CAIF_DISCONNECTED;
799 sock->state = SS_UNCONNECTED;
800 sk_stream_kill_queues(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000801
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000802 err = -EINVAL;
Dan Carpenterf5d72af2010-05-22 10:43:42 +0000803 if (addr_len != sizeof(struct sockaddr_caif))
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000804 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000805
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000806 memcpy(&cf_sk->conn_req.sockaddr, uaddr,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000807 sizeof(struct sockaddr_caif));
808
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000809 /* Move to connecting socket, start sending Connect Requests */
810 sock->state = SS_CONNECTING;
811 sk->sk_state = CAIF_CONNECTING;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000812
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +0000813 /* Check priority value comming from socket */
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +0000814 /* if priority value is out of range it will be ajusted */
815 if (cf_sk->sk.sk_priority > CAIF_PRIO_MAX)
816 cf_sk->conn_req.priority = CAIF_PRIO_MAX;
817 else if (cf_sk->sk.sk_priority < CAIF_PRIO_MIN)
818 cf_sk->conn_req.priority = CAIF_PRIO_MIN;
819 else
820 cf_sk->conn_req.priority = cf_sk->sk.sk_priority;
821
822 /*ifindex = id of the interface.*/
823 cf_sk->conn_req.ifindex = cf_sk->sk.sk_bound_dev_if;
824
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000825 cf_sk->layer.receive = caif_sktrecv_cb;
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000826
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000827 err = caif_connect_client(sock_net(sk), &cf_sk->conn_req,
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000828 &cf_sk->layer, &ifindex, &headroom, &tailroom);
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +0000829
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000830 if (err < 0) {
831 cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
832 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
833 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000834 }
Eric Dumazet79315062010-10-04 22:42:08 +0000835
836 err = -ENODEV;
837 rcu_read_lock();
838 dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
839 if (!dev) {
840 rcu_read_unlock();
841 goto out;
842 }
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000843 cf_sk->headroom = LL_RESERVED_SPACE_EXTRA(dev, headroom);
Eric Dumazet79315062010-10-04 22:42:08 +0000844 mtu = dev->mtu;
845 rcu_read_unlock();
846
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000847 cf_sk->tailroom = tailroom;
Eric Dumazet79315062010-10-04 22:42:08 +0000848 cf_sk->maxframe = mtu - (headroom + tailroom);
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000849 if (cf_sk->maxframe < 1) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000850 pr_warn("CAIF Interface MTU too small (%d)\n", dev->mtu);
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +0000851 err = -ENODEV;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000852 goto out;
853 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000854
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000855 err = -EINPROGRESS;
856wait_connect:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000857
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000858 if (sk->sk_state != CAIF_CONNECTED && (flags & O_NONBLOCK))
859 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000860
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000861 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000862
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000863 release_sock(sk);
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000864 err = -ERESTARTSYS;
865 timeo = wait_event_interruptible_timeout(*sk_sleep(sk),
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000866 sk->sk_state != CAIF_CONNECTING,
867 timeo);
868 lock_sock(sk);
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000869 if (timeo < 0)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000870 goto out; /* -ERESTARTSYS */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000871
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000872 err = -ETIMEDOUT;
873 if (timeo == 0 && sk->sk_state != CAIF_CONNECTED)
874 goto out;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000875 if (sk->sk_state != CAIF_CONNECTED) {
876 sock->state = SS_UNCONNECTED;
877 err = sock_error(sk);
878 if (!err)
879 err = -ECONNREFUSED;
880 goto out;
881 }
882 sock->state = SS_CONNECTED;
883 err = 0;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000884out:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000885 release_sock(sk);
886 return err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000887}
888
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000889/*
890 * caif_release() - Disconnect a CAIF Socket
891 * Copied and modified af_irda.c:irda_release().
892 */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000893static int caif_release(struct socket *sock)
894{
895 struct sock *sk = sock->sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000896 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000897
898 if (!sk)
899 return 0;
900
901 set_tx_flow_off(cf_sk);
902
903 /*
904 * Ensure that packets are not queued after this point in time.
905 * caif_queue_rcv_skb checks SOCK_DEAD holding the queue lock,
906 * this ensures no packets when sock is dead.
907 */
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000908 spin_lock_bh(&sk->sk_receive_queue.lock);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000909 sock_set_flag(sk, SOCK_DEAD);
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000910 spin_unlock_bh(&sk->sk_receive_queue.lock);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000911 sock->sk = NULL;
912
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +0000913 WARN_ON(IS_ERR(cf_sk->debugfs_socket_dir));
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000914 if (cf_sk->debugfs_socket_dir != NULL)
915 debugfs_remove_recursive(cf_sk->debugfs_socket_dir);
916
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000917 lock_sock(&(cf_sk->sk));
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000918 sk->sk_state = CAIF_DISCONNECTED;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000919 sk->sk_shutdown = SHUTDOWN_MASK;
920
sjur.brandeland@stericsson.com54e90fb2011-05-22 11:18:51 +0000921 caif_disconnect_client(sock_net(sk), &cf_sk->layer);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000922 cf_sk->sk.sk_socket->state = SS_DISCONNECTING;
923 wake_up_interruptible_poll(sk_sleep(sk), POLLERR|POLLHUP);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000924
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000925 sock_orphan(sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000926 sk_stream_kill_queues(&cf_sk->sk);
927 release_sock(sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000928 sock_put(sk);
sjur.brandeland@stericsson.com54e90fb2011-05-22 11:18:51 +0000929 return 0;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000930}
931
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000932/* Copied from af_unix.c:unix_poll(), added CAIF tx_flow handling */
933static unsigned int caif_poll(struct file *file,
934 struct socket *sock, poll_table *wait)
935{
936 struct sock *sk = sock->sk;
937 unsigned int mask;
938 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
939
940 sock_poll_wait(file, sk_sleep(sk), wait);
941 mask = 0;
942
943 /* exceptional events? */
944 if (sk->sk_err)
945 mask |= POLLERR;
946 if (sk->sk_shutdown == SHUTDOWN_MASK)
947 mask |= POLLHUP;
948 if (sk->sk_shutdown & RCV_SHUTDOWN)
949 mask |= POLLRDHUP;
950
951 /* readable? */
952 if (!skb_queue_empty(&sk->sk_receive_queue) ||
953 (sk->sk_shutdown & RCV_SHUTDOWN))
954 mask |= POLLIN | POLLRDNORM;
955
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000956 /*
957 * we set writable also when the other side has shut down the
958 * connection. This prevents stuck sockets.
959 */
960 if (sock_writeable(sk) && tx_flow_is_on(cf_sk))
961 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
962
963 return mask;
964}
965
966static const struct proto_ops caif_seqpacket_ops = {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000967 .family = PF_CAIF,
968 .owner = THIS_MODULE,
969 .release = caif_release,
970 .bind = sock_no_bind,
971 .connect = caif_connect,
972 .socketpair = sock_no_socketpair,
973 .accept = sock_no_accept,
974 .getname = sock_no_getname,
975 .poll = caif_poll,
976 .ioctl = sock_no_ioctl,
977 .listen = sock_no_listen,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000978 .shutdown = sock_no_shutdown,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000979 .setsockopt = setsockopt,
980 .getsockopt = sock_no_getsockopt,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000981 .sendmsg = caif_seqpkt_sendmsg,
982 .recvmsg = caif_seqpkt_recvmsg,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000983 .mmap = sock_no_mmap,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000984 .sendpage = sock_no_sendpage,
985};
986
987static const struct proto_ops caif_stream_ops = {
988 .family = PF_CAIF,
989 .owner = THIS_MODULE,
990 .release = caif_release,
991 .bind = sock_no_bind,
992 .connect = caif_connect,
993 .socketpair = sock_no_socketpair,
994 .accept = sock_no_accept,
995 .getname = sock_no_getname,
996 .poll = caif_poll,
997 .ioctl = sock_no_ioctl,
998 .listen = sock_no_listen,
999 .shutdown = sock_no_shutdown,
1000 .setsockopt = setsockopt,
1001 .getsockopt = sock_no_getsockopt,
1002 .sendmsg = caif_stream_sendmsg,
1003 .recvmsg = caif_stream_recvmsg,
1004 .mmap = sock_no_mmap,
1005 .sendpage = sock_no_sendpage,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001006};
1007
1008/* This function is called when a socket is finally destroyed. */
1009static void caif_sock_destructor(struct sock *sk)
1010{
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001011 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001012 caif_assert(!atomic_read(&sk->sk_wmem_alloc));
1013 caif_assert(sk_unhashed(sk));
1014 caif_assert(!sk->sk_socket);
1015 if (!sock_flag(sk, SOCK_DEAD)) {
sjur.brandeland@stericsson.com33b2f552011-05-13 02:44:07 +00001016 pr_debug("Attempt to release alive CAIF socket: %p\n", sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001017 return;
1018 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001019 sk_stream_kill_queues(&cf_sk->sk);
sjur.brandeland@stericsson.comb3ccfbe2011-05-13 02:44:04 +00001020 caif_free_client(&cf_sk->layer);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001021}
1022
1023static int caif_create(struct net *net, struct socket *sock, int protocol,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001024 int kern)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001025{
1026 struct sock *sk = NULL;
1027 struct caifsock *cf_sk = NULL;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001028 static struct proto prot = {.name = "PF_CAIF",
1029 .owner = THIS_MODULE,
1030 .obj_size = sizeof(struct caifsock),
1031 };
1032
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001033 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_NET_ADMIN))
1034 return -EPERM;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001035 /*
1036 * The sock->type specifies the socket type to use.
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001037 * The CAIF socket is a packet stream in the sense
1038 * that it is packet based. CAIF trusts the reliability
1039 * of the link, no resending is implemented.
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001040 */
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001041 if (sock->type == SOCK_SEQPACKET)
1042 sock->ops = &caif_seqpacket_ops;
1043 else if (sock->type == SOCK_STREAM)
1044 sock->ops = &caif_stream_ops;
1045 else
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001046 return -ESOCKTNOSUPPORT;
1047
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001048 if (protocol < 0 || protocol >= CAIFPROTO_MAX)
1049 return -EPROTONOSUPPORT;
1050 /*
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001051 * Set the socket state to unconnected. The socket state
1052 * is really not used at all in the net/core or socket.c but the
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001053 * initialization makes sure that sock->state is not uninitialized.
1054 */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001055 sk = sk_alloc(net, PF_CAIF, GFP_KERNEL, &prot);
1056 if (!sk)
1057 return -ENOMEM;
1058
1059 cf_sk = container_of(sk, struct caifsock, sk);
1060
1061 /* Store the protocol */
1062 sk->sk_protocol = (unsigned char) protocol;
1063
Dmitry Tarnyagin44764812012-04-12 08:27:24 +00001064 /* Initialize default priority for well-known cases */
1065 switch (protocol) {
1066 case CAIFPROTO_AT:
1067 sk->sk_priority = TC_PRIO_CONTROL;
1068 break;
1069 case CAIFPROTO_RFM:
1070 sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
1071 break;
1072 default:
1073 sk->sk_priority = TC_PRIO_BESTEFFORT;
1074 }
1075
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001076 /*
1077 * Lock in order to try to stop someone from opening the socket
1078 * too early.
1079 */
1080 lock_sock(&(cf_sk->sk));
1081
1082 /* Initialize the nozero default sock structure data. */
1083 sock_init_data(sock, sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001084 sk->sk_destruct = caif_sock_destructor;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001085
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001086 mutex_init(&cf_sk->readlock); /* single task reading lock */
1087 cf_sk->layer.ctrlcmd = caif_ctrl_cb;
1088 cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
1089 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001090
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001091 set_tx_flow_off(cf_sk);
1092 set_rx_flow_on(cf_sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001093
1094 /* Set default options on configuration */
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001095 cf_sk->conn_req.link_selector = CAIF_LINK_LOW_LATENCY;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001096 cf_sk->conn_req.protocol = protocol;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001097 release_sock(&cf_sk->sk);
1098 return 0;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001099}
1100
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001101
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001102static struct net_proto_family caif_family_ops = {
1103 .family = PF_CAIF,
1104 .create = caif_create,
1105 .owner = THIS_MODULE,
1106};
1107
sjur.brandeland@stericsson.com4a695822012-02-03 04:36:19 +00001108static int __init caif_sktinit_module(void)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001109{
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001110 int err = sock_register(&caif_family_ops);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001111 if (!err)
1112 return err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001113 return 0;
1114}
1115
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001116static void __exit caif_sktexit_module(void)
1117{
1118 sock_unregister(PF_CAIF);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001119}
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001120module_init(caif_sktinit_module);
1121module_exit(caif_sktexit_module);