blob: fd1f5df0827c782a45c873691e6411da4240a446 [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/mutex.h>
21#include <linux/debugfs.h>
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000022#include <linux/caif/caif_socket.h>
Sjur Braendelandbece7b22010-04-28 08:54:38 +000023#include <asm/atomic.h>
24#include <net/sock.h>
25#include <net/tcp_states.h>
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000026#include <net/caif/caif_layer.h>
27#include <net/caif/caif_dev.h>
28#include <net/caif/cfpkt.h>
29
30MODULE_LICENSE("GPL");
Sjur Braendelandbece7b22010-04-28 08:54:38 +000031MODULE_ALIAS_NETPROTO(AF_CAIF);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000032
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +000033#define CAIF_DEF_SNDBUF (4096*10)
34#define CAIF_DEF_RCVBUF (4096*100)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000035
Sjur Braendelandbece7b22010-04-28 08:54:38 +000036/*
37 * CAIF state is re-using the TCP socket states.
38 * caif_states stored in sk_state reflect the state as reported by
39 * the CAIF stack, while sk_socket->state is the state of the socket.
40 */
41enum caif_states {
42 CAIF_CONNECTED = TCP_ESTABLISHED,
43 CAIF_CONNECTING = TCP_SYN_SENT,
44 CAIF_DISCONNECTED = TCP_CLOSE
45};
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000046
Sjur Braendelandbece7b22010-04-28 08:54:38 +000047#define TX_FLOW_ON_BIT 1
48#define RX_FLOW_ON_BIT 2
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000049
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000050static struct dentry *debugfsdir;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000051
52#ifdef CONFIG_DEBUG_FS
53struct debug_fs_counter {
Sjur Braendelandbece7b22010-04-28 08:54:38 +000054 atomic_t caif_nr_socks;
55 atomic_t num_connect_req;
56 atomic_t num_connect_resp;
57 atomic_t num_connect_fail_resp;
58 atomic_t num_disconnect;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000059 atomic_t num_remote_shutdown_ind;
60 atomic_t num_tx_flow_off_ind;
61 atomic_t num_tx_flow_on_ind;
62 atomic_t num_rx_flow_off;
63 atomic_t num_rx_flow_on;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000064};
Sjur Braendelanda9a8f102010-05-21 02:16:11 +000065static struct debug_fs_counter cnt;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000066#define dbfs_atomic_inc(v) atomic_inc(v)
67#define dbfs_atomic_dec(v) atomic_dec(v)
68#else
69#define dbfs_atomic_inc(v)
70#define dbfs_atomic_dec(v)
71#endif
72
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000073struct caifsock {
Sjur Braendelandbece7b22010-04-28 08:54:38 +000074 struct sock sk; /* must be first member */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000075 struct cflayer layer;
Sjur Braendelandbece7b22010-04-28 08:54:38 +000076 char name[CAIF_LAYER_NAME_SZ]; /* Used for debugging */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000077 u32 flow_state;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000078 struct caif_connect_request conn_req;
Sjur Braendelandbece7b22010-04-28 08:54:38 +000079 struct mutex readlock;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000080 struct dentry *debugfs_socket_dir;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +000081 int headroom, tailroom, maxframe;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000082};
83
Sjur Braendelandbece7b22010-04-28 08:54:38 +000084static int rx_flow_is_on(struct caifsock *cf_sk)
85{
86 return test_bit(RX_FLOW_ON_BIT,
87 (void *) &cf_sk->flow_state);
88}
89
90static int tx_flow_is_on(struct caifsock *cf_sk)
91{
92 return test_bit(TX_FLOW_ON_BIT,
93 (void *) &cf_sk->flow_state);
94}
95
96static void set_rx_flow_off(struct caifsock *cf_sk)
97{
98 clear_bit(RX_FLOW_ON_BIT,
99 (void *) &cf_sk->flow_state);
100}
101
102static void set_rx_flow_on(struct caifsock *cf_sk)
103{
104 set_bit(RX_FLOW_ON_BIT,
105 (void *) &cf_sk->flow_state);
106}
107
108static void set_tx_flow_off(struct caifsock *cf_sk)
109{
110 clear_bit(TX_FLOW_ON_BIT,
111 (void *) &cf_sk->flow_state);
112}
113
114static void set_tx_flow_on(struct caifsock *cf_sk)
115{
116 set_bit(TX_FLOW_ON_BIT,
117 (void *) &cf_sk->flow_state);
118}
119
120static void caif_read_lock(struct sock *sk)
121{
122 struct caifsock *cf_sk;
123 cf_sk = container_of(sk, struct caifsock, sk);
124 mutex_lock(&cf_sk->readlock);
125}
126
127static void caif_read_unlock(struct sock *sk)
128{
129 struct caifsock *cf_sk;
130 cf_sk = container_of(sk, struct caifsock, sk);
131 mutex_unlock(&cf_sk->readlock);
132}
133
Sjur Braendelanda9a8f102010-05-21 02:16:11 +0000134static int sk_rcvbuf_lowwater(struct caifsock *cf_sk)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000135{
136 /* A quarter of full buffer is used a low water mark */
137 return cf_sk->sk.sk_rcvbuf / 4;
138}
139
Sjur Braendelanda9a8f102010-05-21 02:16:11 +0000140static void caif_flow_ctrl(struct sock *sk, int mode)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000141{
142 struct caifsock *cf_sk;
143 cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000144 if (cf_sk->layer.dn && cf_sk->layer.dn->modemcmd)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000145 cf_sk->layer.dn->modemcmd(cf_sk->layer.dn, mode);
146}
147
148/*
149 * Copied from sock.c:sock_queue_rcv_skb(), but changed so packets are
150 * not dropped, but CAIF is sending flow off instead.
151 */
Sjur Braendelanda9a8f102010-05-21 02:16:11 +0000152static int caif_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000153{
154 int err;
155 int skb_len;
156 unsigned long flags;
157 struct sk_buff_head *list = &sk->sk_receive_queue;
158 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
159
160 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
161 (unsigned)sk->sk_rcvbuf && rx_flow_is_on(cf_sk)) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000162 trace_printk("CAIF: %s(): "
163 "sending flow OFF (queue len = %d %d)\n",
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000164 __func__,
165 atomic_read(&cf_sk->sk.sk_rmem_alloc),
166 sk_rcvbuf_lowwater(cf_sk));
167 set_rx_flow_off(cf_sk);
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000168 dbfs_atomic_inc(&cnt.num_rx_flow_off);
169 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_OFF_REQ);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000170 }
171
172 err = sk_filter(sk, skb);
173 if (err)
174 return err;
175 if (!sk_rmem_schedule(sk, skb->truesize) && rx_flow_is_on(cf_sk)) {
176 set_rx_flow_off(cf_sk);
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000177 trace_printk("CAIF: %s(): "
178 "sending flow OFF due to rmem_schedule\n",
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000179 __func__);
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000180 dbfs_atomic_inc(&cnt.num_rx_flow_off);
181 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_OFF_REQ);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000182 }
183 skb->dev = NULL;
184 skb_set_owner_r(skb, sk);
185 /* Cache the SKB length before we tack it onto the receive
186 * queue. Once it is added it no longer belongs to us and
187 * may be freed by other threads of control pulling packets
188 * from the queue.
189 */
190 skb_len = skb->len;
191 spin_lock_irqsave(&list->lock, flags);
192 if (!sock_flag(sk, SOCK_DEAD))
193 __skb_queue_tail(list, skb);
194 spin_unlock_irqrestore(&list->lock, flags);
195
196 if (!sock_flag(sk, SOCK_DEAD))
197 sk->sk_data_ready(sk, skb_len);
198 else
199 kfree_skb(skb);
200 return 0;
201}
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000202
203/* Packet Receive Callback function called from CAIF Stack */
204static int caif_sktrecv_cb(struct cflayer *layr, struct cfpkt *pkt)
205{
206 struct caifsock *cf_sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000207 struct sk_buff *skb;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000208
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000209 cf_sk = container_of(layr, struct caifsock, layer);
210 skb = cfpkt_tonative(pkt);
211
212 if (unlikely(cf_sk->sk.sk_state != CAIF_CONNECTED)) {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000213 cfpkt_destroy(pkt);
214 return 0;
215 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000216 caif_queue_rcv_skb(&cf_sk->sk, skb);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000217 return 0;
218}
219
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000220/* Packet Control Callback function called from CAIF */
221static void caif_ctrl_cb(struct cflayer *layr,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000222 enum caif_ctrlcmd flow,
223 int phyid)
224{
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000225 struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000226 switch (flow) {
227 case CAIF_CTRLCMD_FLOW_ON_IND:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000228 /* OK from modem to start sending again */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000229 dbfs_atomic_inc(&cnt.num_tx_flow_on_ind);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000230 set_tx_flow_on(cf_sk);
231 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000232 break;
233
234 case CAIF_CTRLCMD_FLOW_OFF_IND:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000235 /* Modem asks us to shut up */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000236 dbfs_atomic_inc(&cnt.num_tx_flow_off_ind);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000237 set_tx_flow_off(cf_sk);
238 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000239 break;
240
241 case CAIF_CTRLCMD_INIT_RSP:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000242 /* We're now connected */
243 dbfs_atomic_inc(&cnt.num_connect_resp);
244 cf_sk->sk.sk_state = CAIF_CONNECTED;
245 set_tx_flow_on(cf_sk);
246 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000247 break;
248
249 case CAIF_CTRLCMD_DEINIT_RSP:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000250 /* We're now disconnected */
251 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
252 cf_sk->sk.sk_state_change(&cf_sk->sk);
253 cfcnfg_release_adap_layer(&cf_sk->layer);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000254 break;
255
256 case CAIF_CTRLCMD_INIT_FAIL_RSP:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000257 /* Connect request failed */
258 dbfs_atomic_inc(&cnt.num_connect_fail_resp);
259 cf_sk->sk.sk_err = ECONNREFUSED;
260 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
261 cf_sk->sk.sk_shutdown = SHUTDOWN_MASK;
262 /*
263 * Socket "standards" seems to require POLLOUT to
264 * be set at connect failure.
265 */
266 set_tx_flow_on(cf_sk);
267 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000268 break;
269
270 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000271 /* Modem has closed this connection, or device is down. */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000272 dbfs_atomic_inc(&cnt.num_remote_shutdown_ind);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000273 cf_sk->sk.sk_shutdown = SHUTDOWN_MASK;
274 cf_sk->sk.sk_err = ECONNRESET;
275 set_rx_flow_on(cf_sk);
276 cf_sk->sk.sk_error_report(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000277 break;
278
279 default:
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000280 pr_debug("Unexpected flow command %d\n", flow);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000281 }
282}
283
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000284static void caif_check_flow_release(struct sock *sk)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000285{
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000286 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
287
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000288 if (rx_flow_is_on(cf_sk))
289 return;
290
291 if (atomic_read(&sk->sk_rmem_alloc) <= sk_rcvbuf_lowwater(cf_sk)) {
292 dbfs_atomic_inc(&cnt.num_rx_flow_on);
293 set_rx_flow_on(cf_sk);
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000294 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_ON_REQ);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000295 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000296}
297
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000298/*
299 * Copied from unix_dgram_recvmsg, but removed credit checks,
300 * changed locking, address handling and added MSG_TRUNC.
301 */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000302static int caif_seqpkt_recvmsg(struct kiocb *iocb, struct socket *sock,
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000303 struct msghdr *m, size_t len, int flags)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000304
305{
306 struct sock *sk = sock->sk;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000307 struct sk_buff *skb;
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000308 int ret;
309 int copylen;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000310
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000311 ret = -EOPNOTSUPP;
312 if (m->msg_flags&MSG_OOB)
313 goto read_error;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000314
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000315 skb = skb_recv_datagram(sk, flags, 0 , &ret);
316 if (!skb)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000317 goto read_error;
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000318 copylen = skb->len;
319 if (len < copylen) {
320 m->msg_flags |= MSG_TRUNC;
321 copylen = len;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000322 }
323
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000324 ret = skb_copy_datagram_iovec(skb, 0, m->msg_iov, copylen);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000325 if (ret)
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000326 goto out_free;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000327
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000328 ret = (flags & MSG_TRUNC) ? skb->len : copylen;
329out_free:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000330 skb_free_datagram(sk, skb);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000331 caif_check_flow_release(sk);
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000332 return ret;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000333
334read_error:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000335 return ret;
336}
337
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000338
339/* Copied from unix_stream_wait_data, identical except for lock call. */
340static long caif_stream_data_wait(struct sock *sk, long timeo)
341{
342 DEFINE_WAIT(wait);
343 lock_sock(sk);
344
345 for (;;) {
346 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
347
348 if (!skb_queue_empty(&sk->sk_receive_queue) ||
349 sk->sk_err ||
350 sk->sk_state != CAIF_CONNECTED ||
351 sock_flag(sk, SOCK_DEAD) ||
352 (sk->sk_shutdown & RCV_SHUTDOWN) ||
353 signal_pending(current) ||
354 !timeo)
355 break;
356
357 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
358 release_sock(sk);
359 timeo = schedule_timeout(timeo);
360 lock_sock(sk);
361 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
362 }
363
364 finish_wait(sk_sleep(sk), &wait);
365 release_sock(sk);
366 return timeo;
367}
368
369
370/*
371 * Copied from unix_stream_recvmsg, but removed credit checks,
372 * changed locking calls, changed address handling.
373 */
374static int caif_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
375 struct msghdr *msg, size_t size,
376 int flags)
377{
378 struct sock *sk = sock->sk;
379 int copied = 0;
380 int target;
381 int err = 0;
382 long timeo;
383
384 err = -EOPNOTSUPP;
385 if (flags&MSG_OOB)
386 goto out;
387
388 msg->msg_namelen = 0;
389
390 /*
391 * Lock the socket to prevent queue disordering
392 * while sleeps in memcpy_tomsg
393 */
394 err = -EAGAIN;
395 if (sk->sk_state == CAIF_CONNECTING)
396 goto out;
397
398 caif_read_lock(sk);
399 target = sock_rcvlowat(sk, flags&MSG_WAITALL, size);
400 timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
401
402 do {
403 int chunk;
404 struct sk_buff *skb;
405
406 lock_sock(sk);
407 skb = skb_dequeue(&sk->sk_receive_queue);
408 caif_check_flow_release(sk);
409
410 if (skb == NULL) {
411 if (copied >= target)
412 goto unlock;
413 /*
414 * POSIX 1003.1g mandates this order.
415 */
416 err = sock_error(sk);
417 if (err)
418 goto unlock;
419 err = -ECONNRESET;
420 if (sk->sk_shutdown & RCV_SHUTDOWN)
421 goto unlock;
422
423 err = -EPIPE;
424 if (sk->sk_state != CAIF_CONNECTED)
425 goto unlock;
426 if (sock_flag(sk, SOCK_DEAD))
427 goto unlock;
428
429 release_sock(sk);
430
431 err = -EAGAIN;
432 if (!timeo)
433 break;
434
435 caif_read_unlock(sk);
436
437 timeo = caif_stream_data_wait(sk, timeo);
438
439 if (signal_pending(current)) {
440 err = sock_intr_errno(timeo);
441 goto out;
442 }
443 caif_read_lock(sk);
444 continue;
445unlock:
446 release_sock(sk);
447 break;
448 }
449 release_sock(sk);
450 chunk = min_t(unsigned int, skb->len, size);
451 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
452 skb_queue_head(&sk->sk_receive_queue, skb);
453 if (copied == 0)
454 copied = -EFAULT;
455 break;
456 }
457 copied += chunk;
458 size -= chunk;
459
460 /* Mark read part of skb as used */
461 if (!(flags & MSG_PEEK)) {
462 skb_pull(skb, chunk);
463
464 /* put the skb back if we didn't use it up. */
465 if (skb->len) {
466 skb_queue_head(&sk->sk_receive_queue, skb);
467 break;
468 }
469 kfree_skb(skb);
470
471 } else {
472 /*
473 * It is questionable, see note in unix_dgram_recvmsg.
474 */
475 /* put message back and return */
476 skb_queue_head(&sk->sk_receive_queue, skb);
477 break;
478 }
479 } while (size);
480 caif_read_unlock(sk);
481
482out:
483 return copied ? : err;
484}
485
486/*
487 * Copied from sock.c:sock_wait_for_wmem, but change to wait for
488 * CAIF flow-on and sock_writable.
489 */
490static long caif_wait_for_flow_on(struct caifsock *cf_sk,
491 int wait_writeable, long timeo, int *err)
492{
493 struct sock *sk = &cf_sk->sk;
494 DEFINE_WAIT(wait);
495 for (;;) {
496 *err = 0;
497 if (tx_flow_is_on(cf_sk) &&
498 (!wait_writeable || sock_writeable(&cf_sk->sk)))
499 break;
500 *err = -ETIMEDOUT;
501 if (!timeo)
502 break;
503 *err = -ERESTARTSYS;
504 if (signal_pending(current))
505 break;
506 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
507 *err = -ECONNRESET;
508 if (sk->sk_shutdown & SHUTDOWN_MASK)
509 break;
510 *err = -sk->sk_err;
511 if (sk->sk_err)
512 break;
513 *err = -EPIPE;
514 if (cf_sk->sk.sk_state != CAIF_CONNECTED)
515 break;
516 timeo = schedule_timeout(timeo);
517 }
518 finish_wait(sk_sleep(sk), &wait);
519 return timeo;
520}
521
522/*
523 * Transmit a SKB. The device may temporarily request re-transmission
524 * by returning EAGAIN.
525 */
526static int transmit_skb(struct sk_buff *skb, struct caifsock *cf_sk,
527 int noblock, long timeo)
528{
529 struct cfpkt *pkt;
530 int ret, loopcnt = 0;
531
532 pkt = cfpkt_fromnative(CAIF_DIR_OUT, skb);
533 memset(cfpkt_info(pkt), 0, sizeof(struct caif_payload_info));
534 do {
535
536 ret = -ETIMEDOUT;
537
538 /* Slight paranoia, probably not needed. */
539 if (unlikely(loopcnt++ > 1000)) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000540 pr_warn("transmit retries failed, error = %d\n", ret);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000541 break;
542 }
543
544 if (cf_sk->layer.dn != NULL)
545 ret = cf_sk->layer.dn->transmit(cf_sk->layer.dn, pkt);
546 if (likely(ret >= 0))
547 break;
548 /* if transmit return -EAGAIN, then retry */
549 if (noblock && ret == -EAGAIN)
550 break;
551 timeo = caif_wait_for_flow_on(cf_sk, 0, timeo, &ret);
552 if (signal_pending(current)) {
553 ret = sock_intr_errno(timeo);
554 break;
555 }
556 if (ret)
557 break;
558 if (cf_sk->sk.sk_state != CAIF_CONNECTED ||
559 sock_flag(&cf_sk->sk, SOCK_DEAD) ||
560 (cf_sk->sk.sk_shutdown & RCV_SHUTDOWN)) {
561 ret = -EPIPE;
562 cf_sk->sk.sk_err = EPIPE;
563 break;
564 }
565 } while (ret == -EAGAIN);
566 return ret;
567}
568
569/* Copied from af_unix:unix_dgram_sendmsg, and adapted to CAIF */
570static int caif_seqpkt_sendmsg(struct kiocb *kiocb, struct socket *sock,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000571 struct msghdr *msg, size_t len)
572{
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000573 struct sock *sk = sock->sk;
574 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000575 int buffer_size;
576 int ret = 0;
577 struct sk_buff *skb = NULL;
578 int noblock;
579 long timeo;
580 caif_assert(cf_sk);
581 ret = sock_error(sk);
582 if (ret)
583 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000584
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000585 ret = -EOPNOTSUPP;
586 if (msg->msg_flags&MSG_OOB)
587 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000588
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000589 ret = -EOPNOTSUPP;
590 if (msg->msg_namelen)
591 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000592
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000593 ret = -EINVAL;
594 if (unlikely(msg->msg_iov->iov_base == NULL))
595 goto err;
596 noblock = msg->msg_flags & MSG_DONTWAIT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000597
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000598 timeo = sock_sndtimeo(sk, noblock);
599 timeo = caif_wait_for_flow_on(container_of(sk, struct caifsock, sk),
600 1, timeo, &ret);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000601
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000602 if (ret)
603 goto err;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000604 ret = -EPIPE;
605 if (cf_sk->sk.sk_state != CAIF_CONNECTED ||
606 sock_flag(sk, SOCK_DEAD) ||
607 (sk->sk_shutdown & RCV_SHUTDOWN))
608 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000609
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000610 /* Error if trying to write more than maximum frame size. */
611 ret = -EMSGSIZE;
612 if (len > cf_sk->maxframe && cf_sk->sk.sk_protocol != CAIFPROTO_RFM)
613 goto err;
614
615 buffer_size = len + cf_sk->headroom + cf_sk->tailroom;
616
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000617 ret = -ENOMEM;
618 skb = sock_alloc_send_skb(sk, buffer_size, noblock, &ret);
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000619
620 if (!skb || skb_tailroom(skb) < buffer_size)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000621 goto err;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000622
623 skb_reserve(skb, cf_sk->headroom);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000624
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000625 ret = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000626
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000627 if (ret)
628 goto err;
629 ret = transmit_skb(skb, cf_sk, noblock, timeo);
630 if (ret < 0)
631 goto err;
632 return len;
633err:
634 kfree_skb(skb);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000635 return ret;
636}
637
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000638/*
639 * Copied from unix_stream_sendmsg and adapted to CAIF:
640 * Changed removed permission handling and added waiting for flow on
641 * and other minor adaptations.
642 */
643static int caif_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
644 struct msghdr *msg, size_t len)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000645{
646 struct sock *sk = sock->sk;
647 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000648 int err, size;
649 struct sk_buff *skb;
650 int sent = 0;
651 long timeo;
652
653 err = -EOPNOTSUPP;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000654 if (unlikely(msg->msg_flags&MSG_OOB))
655 goto out_err;
656
657 if (unlikely(msg->msg_namelen))
658 goto out_err;
659
660 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
661 timeo = caif_wait_for_flow_on(cf_sk, 1, timeo, &err);
662
663 if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN))
664 goto pipe_err;
665
666 while (sent < len) {
667
668 size = len-sent;
669
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000670 if (size > cf_sk->maxframe)
671 size = cf_sk->maxframe;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000672
673 /* If size is more than half of sndbuf, chop up message */
674 if (size > ((sk->sk_sndbuf >> 1) - 64))
675 size = (sk->sk_sndbuf >> 1) - 64;
676
677 if (size > SKB_MAX_ALLOC)
678 size = SKB_MAX_ALLOC;
679
680 skb = sock_alloc_send_skb(sk,
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000681 size + cf_sk->headroom +
682 cf_sk->tailroom,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000683 msg->msg_flags&MSG_DONTWAIT,
684 &err);
685 if (skb == NULL)
686 goto out_err;
687
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000688 skb_reserve(skb, cf_sk->headroom);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000689 /*
690 * If you pass two values to the sock_alloc_send_skb
691 * it tries to grab the large buffer with GFP_NOFS
692 * (which can fail easily), and if it fails grab the
693 * fallback size buffer which is under a page and will
694 * succeed. [Alan]
695 */
696 size = min_t(int, size, skb_tailroom(skb));
697
698 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
699 if (err) {
700 kfree_skb(skb);
701 goto out_err;
702 }
703 err = transmit_skb(skb, cf_sk,
704 msg->msg_flags&MSG_DONTWAIT, timeo);
705 if (err < 0) {
706 kfree_skb(skb);
707 goto pipe_err;
708 }
709 sent += size;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000710 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000711
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000712 return sent;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000713
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000714pipe_err:
715 if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL))
716 send_sig(SIGPIPE, current, 0);
717 err = -EPIPE;
718out_err:
719 return sent ? : err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000720}
721
722static int setsockopt(struct socket *sock,
723 int lvl, int opt, char __user *ov, unsigned int ol)
724{
725 struct sock *sk = sock->sk;
726 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
727 int prio, linksel;
728 struct ifreq ifreq;
729
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000730 if (cf_sk->sk.sk_socket->state != SS_UNCONNECTED)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000731 return -ENOPROTOOPT;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000732
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000733 switch (opt) {
734 case CAIFSO_LINK_SELECT:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000735 if (ol < sizeof(int))
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000736 return -EINVAL;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000737 if (lvl != SOL_CAIF)
738 goto bad_sol;
739 if (copy_from_user(&linksel, ov, sizeof(int)))
740 return -EINVAL;
741 lock_sock(&(cf_sk->sk));
742 cf_sk->conn_req.link_selector = linksel;
743 release_sock(&cf_sk->sk);
744 return 0;
745
746 case SO_PRIORITY:
747 if (lvl != SOL_SOCKET)
748 goto bad_sol;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000749 if (ol < sizeof(int))
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000750 return -EINVAL;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000751 if (copy_from_user(&prio, ov, sizeof(int)))
752 return -EINVAL;
753 lock_sock(&(cf_sk->sk));
754 cf_sk->conn_req.priority = prio;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000755 release_sock(&cf_sk->sk);
756 return 0;
757
758 case SO_BINDTODEVICE:
759 if (lvl != SOL_SOCKET)
760 goto bad_sol;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000761 if (ol < sizeof(struct ifreq))
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000762 return -EINVAL;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000763 if (copy_from_user(&ifreq, ov, sizeof(ifreq)))
764 return -EFAULT;
765 lock_sock(&(cf_sk->sk));
766 strncpy(cf_sk->conn_req.link_name, ifreq.ifr_name,
767 sizeof(cf_sk->conn_req.link_name));
768 cf_sk->conn_req.link_name
769 [sizeof(cf_sk->conn_req.link_name)-1] = 0;
770 release_sock(&cf_sk->sk);
771 return 0;
772
773 case CAIFSO_REQ_PARAM:
774 if (lvl != SOL_CAIF)
775 goto bad_sol;
776 if (cf_sk->sk.sk_protocol != CAIFPROTO_UTIL)
777 return -ENOPROTOOPT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000778 lock_sock(&(cf_sk->sk));
779 cf_sk->conn_req.param.size = ol;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000780 if (ol > sizeof(cf_sk->conn_req.param.data) ||
781 copy_from_user(&cf_sk->conn_req.param.data, ov, ol)) {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000782 release_sock(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000783 return -EINVAL;
784 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000785 release_sock(&cf_sk->sk);
786 return 0;
787
788 default:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000789 return -ENOPROTOOPT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000790 }
791
792 return 0;
793bad_sol:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000794 return -ENOPROTOOPT;
795
796}
797
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000798/*
799 * caif_connect() - Connect a CAIF Socket
800 * Copied and modified af_irda.c:irda_connect().
801 *
802 * Note : by consulting "errno", the user space caller may learn the cause
803 * of the failure. Most of them are visible in the function, others may come
804 * from subroutines called and are listed here :
805 * o -EAFNOSUPPORT: bad socket family or type.
806 * o -ESOCKTNOSUPPORT: bad socket type or protocol
807 * o -EINVAL: bad socket address, or CAIF link type
808 * o -ECONNREFUSED: remote end refused the connection.
809 * o -EINPROGRESS: connect request sent but timed out (or non-blocking)
810 * o -EISCONN: already connected.
811 * o -ETIMEDOUT: Connection timed out (send timeout)
812 * o -ENODEV: No link layer to send request
813 * o -ECONNRESET: Received Shutdown indication or lost link layer
814 * o -ENOMEM: Out of memory
815 *
816 * State Strategy:
817 * o sk_state: holds the CAIF_* protocol state, it's updated by
818 * caif_ctrl_cb.
819 * o sock->state: holds the SS_* socket state and is updated by connect and
820 * disconnect.
821 */
822static int caif_connect(struct socket *sock, struct sockaddr *uaddr,
823 int addr_len, int flags)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000824{
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000825 struct sock *sk = sock->sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000826 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
827 long timeo;
828 int err;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000829 int ifindex, headroom, tailroom;
830 struct net_device *dev;
831
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000832 lock_sock(sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000833
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000834 err = -EAFNOSUPPORT;
835 if (uaddr->sa_family != AF_CAIF)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000836 goto out;
837
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000838 switch (sock->state) {
839 case SS_UNCONNECTED:
840 /* Normal case, a fresh connect */
841 caif_assert(sk->sk_state == CAIF_DISCONNECTED);
842 break;
843 case SS_CONNECTING:
844 switch (sk->sk_state) {
845 case CAIF_CONNECTED:
846 sock->state = SS_CONNECTED;
847 err = -EISCONN;
848 goto out;
849 case CAIF_DISCONNECTED:
850 /* Reconnect allowed */
851 break;
852 case CAIF_CONNECTING:
853 err = -EALREADY;
854 if (flags & O_NONBLOCK)
855 goto out;
856 goto wait_connect;
857 }
858 break;
859 case SS_CONNECTED:
860 caif_assert(sk->sk_state == CAIF_CONNECTED ||
861 sk->sk_state == CAIF_DISCONNECTED);
862 if (sk->sk_shutdown & SHUTDOWN_MASK) {
863 /* Allow re-connect after SHUTDOWN_IND */
864 caif_disconnect_client(&cf_sk->layer);
865 break;
866 }
867 /* No reconnect on a seqpacket socket */
868 err = -EISCONN;
869 goto out;
870 case SS_DISCONNECTING:
871 case SS_FREE:
872 caif_assert(1); /*Should never happen */
873 break;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000874 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000875 sk->sk_state = CAIF_DISCONNECTED;
876 sock->state = SS_UNCONNECTED;
877 sk_stream_kill_queues(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000878
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000879 err = -EINVAL;
Dan Carpenterf5d72af2010-05-22 10:43:42 +0000880 if (addr_len != sizeof(struct sockaddr_caif))
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000881 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000882
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000883 memcpy(&cf_sk->conn_req.sockaddr, uaddr,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000884 sizeof(struct sockaddr_caif));
885
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000886 /* Move to connecting socket, start sending Connect Requests */
887 sock->state = SS_CONNECTING;
888 sk->sk_state = CAIF_CONNECTING;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000889
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000890 dbfs_atomic_inc(&cnt.num_connect_req);
891 cf_sk->layer.receive = caif_sktrecv_cb;
892 err = caif_connect_client(&cf_sk->conn_req,
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000893 &cf_sk->layer, &ifindex, &headroom, &tailroom);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000894 if (err < 0) {
895 cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
896 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
897 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000898 }
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000899 dev = dev_get_by_index(sock_net(sk), ifindex);
900 cf_sk->headroom = LL_RESERVED_SPACE_EXTRA(dev, headroom);
901 cf_sk->tailroom = tailroom;
902 cf_sk->maxframe = dev->mtu - (headroom + tailroom);
903 dev_put(dev);
904 if (cf_sk->maxframe < 1) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000905 pr_warn("CAIF Interface MTU too small (%d)\n", dev->mtu);
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000906 err = -ENODEV;
907 goto out;
908 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000909
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000910 err = -EINPROGRESS;
911wait_connect:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000912
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000913 if (sk->sk_state != CAIF_CONNECTED && (flags & O_NONBLOCK))
914 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000915
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000916 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000917
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000918 release_sock(sk);
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000919 err = -ERESTARTSYS;
920 timeo = wait_event_interruptible_timeout(*sk_sleep(sk),
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000921 sk->sk_state != CAIF_CONNECTING,
922 timeo);
923 lock_sock(sk);
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000924 if (timeo < 0)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000925 goto out; /* -ERESTARTSYS */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000926
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000927 err = -ETIMEDOUT;
928 if (timeo == 0 && sk->sk_state != CAIF_CONNECTED)
929 goto out;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000930 if (sk->sk_state != CAIF_CONNECTED) {
931 sock->state = SS_UNCONNECTED;
932 err = sock_error(sk);
933 if (!err)
934 err = -ECONNREFUSED;
935 goto out;
936 }
937 sock->state = SS_CONNECTED;
938 err = 0;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000939out:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000940 release_sock(sk);
941 return err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000942}
943
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000944/*
945 * caif_release() - Disconnect a CAIF Socket
946 * Copied and modified af_irda.c:irda_release().
947 */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000948static int caif_release(struct socket *sock)
949{
950 struct sock *sk = sock->sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000951 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
952 int res = 0;
953
954 if (!sk)
955 return 0;
956
957 set_tx_flow_off(cf_sk);
958
959 /*
960 * Ensure that packets are not queued after this point in time.
961 * caif_queue_rcv_skb checks SOCK_DEAD holding the queue lock,
962 * this ensures no packets when sock is dead.
963 */
964 spin_lock(&sk->sk_receive_queue.lock);
965 sock_set_flag(sk, SOCK_DEAD);
966 spin_unlock(&sk->sk_receive_queue.lock);
967 sock->sk = NULL;
968
969 dbfs_atomic_inc(&cnt.num_disconnect);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000970
971 if (cf_sk->debugfs_socket_dir != NULL)
972 debugfs_remove_recursive(cf_sk->debugfs_socket_dir);
973
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000974 lock_sock(&(cf_sk->sk));
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000975 sk->sk_state = CAIF_DISCONNECTED;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000976 sk->sk_shutdown = SHUTDOWN_MASK;
977
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000978 if (cf_sk->sk.sk_socket->state == SS_CONNECTED ||
979 cf_sk->sk.sk_socket->state == SS_CONNECTING)
980 res = caif_disconnect_client(&cf_sk->layer);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000981
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000982 cf_sk->sk.sk_socket->state = SS_DISCONNECTING;
983 wake_up_interruptible_poll(sk_sleep(sk), POLLERR|POLLHUP);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000984
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000985 sock_orphan(sk);
986 cf_sk->layer.dn = NULL;
987 sk_stream_kill_queues(&cf_sk->sk);
988 release_sock(sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000989 sock_put(sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000990 return res;
991}
992
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000993/* Copied from af_unix.c:unix_poll(), added CAIF tx_flow handling */
994static unsigned int caif_poll(struct file *file,
995 struct socket *sock, poll_table *wait)
996{
997 struct sock *sk = sock->sk;
998 unsigned int mask;
999 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
1000
1001 sock_poll_wait(file, sk_sleep(sk), wait);
1002 mask = 0;
1003
1004 /* exceptional events? */
1005 if (sk->sk_err)
1006 mask |= POLLERR;
1007 if (sk->sk_shutdown == SHUTDOWN_MASK)
1008 mask |= POLLHUP;
1009 if (sk->sk_shutdown & RCV_SHUTDOWN)
1010 mask |= POLLRDHUP;
1011
1012 /* readable? */
1013 if (!skb_queue_empty(&sk->sk_receive_queue) ||
1014 (sk->sk_shutdown & RCV_SHUTDOWN))
1015 mask |= POLLIN | POLLRDNORM;
1016
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001017 /*
1018 * we set writable also when the other side has shut down the
1019 * connection. This prevents stuck sockets.
1020 */
1021 if (sock_writeable(sk) && tx_flow_is_on(cf_sk))
1022 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1023
1024 return mask;
1025}
1026
1027static const struct proto_ops caif_seqpacket_ops = {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001028 .family = PF_CAIF,
1029 .owner = THIS_MODULE,
1030 .release = caif_release,
1031 .bind = sock_no_bind,
1032 .connect = caif_connect,
1033 .socketpair = sock_no_socketpair,
1034 .accept = sock_no_accept,
1035 .getname = sock_no_getname,
1036 .poll = caif_poll,
1037 .ioctl = sock_no_ioctl,
1038 .listen = sock_no_listen,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001039 .shutdown = sock_no_shutdown,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001040 .setsockopt = setsockopt,
1041 .getsockopt = sock_no_getsockopt,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001042 .sendmsg = caif_seqpkt_sendmsg,
1043 .recvmsg = caif_seqpkt_recvmsg,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001044 .mmap = sock_no_mmap,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001045 .sendpage = sock_no_sendpage,
1046};
1047
1048static const struct proto_ops caif_stream_ops = {
1049 .family = PF_CAIF,
1050 .owner = THIS_MODULE,
1051 .release = caif_release,
1052 .bind = sock_no_bind,
1053 .connect = caif_connect,
1054 .socketpair = sock_no_socketpair,
1055 .accept = sock_no_accept,
1056 .getname = sock_no_getname,
1057 .poll = caif_poll,
1058 .ioctl = sock_no_ioctl,
1059 .listen = sock_no_listen,
1060 .shutdown = sock_no_shutdown,
1061 .setsockopt = setsockopt,
1062 .getsockopt = sock_no_getsockopt,
1063 .sendmsg = caif_stream_sendmsg,
1064 .recvmsg = caif_stream_recvmsg,
1065 .mmap = sock_no_mmap,
1066 .sendpage = sock_no_sendpage,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001067};
1068
1069/* This function is called when a socket is finally destroyed. */
1070static void caif_sock_destructor(struct sock *sk)
1071{
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001072 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001073 caif_assert(!atomic_read(&sk->sk_wmem_alloc));
1074 caif_assert(sk_unhashed(sk));
1075 caif_assert(!sk->sk_socket);
1076 if (!sock_flag(sk, SOCK_DEAD)) {
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001077 pr_info("Attempt to release alive CAIF socket: %p\n", sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001078 return;
1079 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001080 sk_stream_kill_queues(&cf_sk->sk);
1081 dbfs_atomic_dec(&cnt.caif_nr_socks);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001082}
1083
1084static int caif_create(struct net *net, struct socket *sock, int protocol,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001085 int kern)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001086{
1087 struct sock *sk = NULL;
1088 struct caifsock *cf_sk = NULL;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001089 static struct proto prot = {.name = "PF_CAIF",
1090 .owner = THIS_MODULE,
1091 .obj_size = sizeof(struct caifsock),
1092 };
1093
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001094 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_NET_ADMIN))
1095 return -EPERM;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001096 /*
1097 * The sock->type specifies the socket type to use.
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001098 * The CAIF socket is a packet stream in the sense
1099 * that it is packet based. CAIF trusts the reliability
1100 * of the link, no resending is implemented.
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001101 */
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001102 if (sock->type == SOCK_SEQPACKET)
1103 sock->ops = &caif_seqpacket_ops;
1104 else if (sock->type == SOCK_STREAM)
1105 sock->ops = &caif_stream_ops;
1106 else
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001107 return -ESOCKTNOSUPPORT;
1108
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001109 if (protocol < 0 || protocol >= CAIFPROTO_MAX)
1110 return -EPROTONOSUPPORT;
1111 /*
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001112 * Set the socket state to unconnected. The socket state
1113 * is really not used at all in the net/core or socket.c but the
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001114 * initialization makes sure that sock->state is not uninitialized.
1115 */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001116 sk = sk_alloc(net, PF_CAIF, GFP_KERNEL, &prot);
1117 if (!sk)
1118 return -ENOMEM;
1119
1120 cf_sk = container_of(sk, struct caifsock, sk);
1121
1122 /* Store the protocol */
1123 sk->sk_protocol = (unsigned char) protocol;
1124
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001125 /* Sendbuf dictates the amount of outbound packets not yet sent */
1126 sk->sk_sndbuf = CAIF_DEF_SNDBUF;
1127 sk->sk_rcvbuf = CAIF_DEF_RCVBUF;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001128
1129 /*
1130 * Lock in order to try to stop someone from opening the socket
1131 * too early.
1132 */
1133 lock_sock(&(cf_sk->sk));
1134
1135 /* Initialize the nozero default sock structure data. */
1136 sock_init_data(sock, sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001137 sk->sk_destruct = caif_sock_destructor;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001138
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001139 mutex_init(&cf_sk->readlock); /* single task reading lock */
1140 cf_sk->layer.ctrlcmd = caif_ctrl_cb;
1141 cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
1142 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001143
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001144 set_tx_flow_off(cf_sk);
1145 set_rx_flow_on(cf_sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001146
1147 /* Set default options on configuration */
1148 cf_sk->conn_req.priority = CAIF_PRIO_NORMAL;
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001149 cf_sk->conn_req.link_selector = CAIF_LINK_LOW_LATENCY;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001150 cf_sk->conn_req.protocol = protocol;
1151 /* Increase the number of sockets created. */
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001152 dbfs_atomic_inc(&cnt.caif_nr_socks);
1153#ifdef CONFIG_DEBUG_FS
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001154 if (!IS_ERR(debugfsdir)) {
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001155 /* Fill in some information concerning the misc socket. */
1156 snprintf(cf_sk->name, sizeof(cf_sk->name), "cfsk%d",
1157 atomic_read(&cnt.caif_nr_socks));
1158
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001159 cf_sk->debugfs_socket_dir =
1160 debugfs_create_dir(cf_sk->name, debugfsdir);
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001161 debugfs_create_u32("sk_state", S_IRUSR | S_IWUSR,
1162 cf_sk->debugfs_socket_dir,
1163 (u32 *) &cf_sk->sk.sk_state);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001164 debugfs_create_u32("flow_state", S_IRUSR | S_IWUSR,
1165 cf_sk->debugfs_socket_dir, &cf_sk->flow_state);
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001166 debugfs_create_u32("sk_rmem_alloc", S_IRUSR | S_IWUSR,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001167 cf_sk->debugfs_socket_dir,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001168 (u32 *) &cf_sk->sk.sk_rmem_alloc);
1169 debugfs_create_u32("sk_wmem_alloc", S_IRUSR | S_IWUSR,
1170 cf_sk->debugfs_socket_dir,
1171 (u32 *) &cf_sk->sk.sk_wmem_alloc);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001172 debugfs_create_u32("identity", S_IRUSR | S_IWUSR,
1173 cf_sk->debugfs_socket_dir,
1174 (u32 *) &cf_sk->layer.id);
1175 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001176#endif
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001177 release_sock(&cf_sk->sk);
1178 return 0;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001179}
1180
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001181
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001182static struct net_proto_family caif_family_ops = {
1183 .family = PF_CAIF,
1184 .create = caif_create,
1185 .owner = THIS_MODULE,
1186};
1187
Sjur Braendelanda9a8f102010-05-21 02:16:11 +00001188static int af_caif_init(void)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001189{
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001190 int err = sock_register(&caif_family_ops);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001191 if (!err)
1192 return err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001193 return 0;
1194}
1195
1196static int __init caif_sktinit_module(void)
1197{
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001198#ifdef CONFIG_DEBUG_FS
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001199 debugfsdir = debugfs_create_dir("caif_sk", NULL);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001200 if (!IS_ERR(debugfsdir)) {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001201 debugfs_create_u32("num_sockets", S_IRUSR | S_IWUSR,
1202 debugfsdir,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001203 (u32 *) &cnt.caif_nr_socks);
1204 debugfs_create_u32("num_connect_req", S_IRUSR | S_IWUSR,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001205 debugfsdir,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001206 (u32 *) &cnt.num_connect_req);
1207 debugfs_create_u32("num_connect_resp", S_IRUSR | S_IWUSR,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001208 debugfsdir,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001209 (u32 *) &cnt.num_connect_resp);
1210 debugfs_create_u32("num_connect_fail_resp", S_IRUSR | S_IWUSR,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001211 debugfsdir,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001212 (u32 *) &cnt.num_connect_fail_resp);
1213 debugfs_create_u32("num_disconnect", S_IRUSR | S_IWUSR,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001214 debugfsdir,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001215 (u32 *) &cnt.num_disconnect);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001216 debugfs_create_u32("num_remote_shutdown_ind",
1217 S_IRUSR | S_IWUSR, debugfsdir,
1218 (u32 *) &cnt.num_remote_shutdown_ind);
1219 debugfs_create_u32("num_tx_flow_off_ind", S_IRUSR | S_IWUSR,
1220 debugfsdir,
1221 (u32 *) &cnt.num_tx_flow_off_ind);
1222 debugfs_create_u32("num_tx_flow_on_ind", S_IRUSR | S_IWUSR,
1223 debugfsdir,
1224 (u32 *) &cnt.num_tx_flow_on_ind);
1225 debugfs_create_u32("num_rx_flow_off", S_IRUSR | S_IWUSR,
1226 debugfsdir,
1227 (u32 *) &cnt.num_rx_flow_off);
1228 debugfs_create_u32("num_rx_flow_on", S_IRUSR | S_IWUSR,
1229 debugfsdir,
1230 (u32 *) &cnt.num_rx_flow_on);
1231 }
1232#endif
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001233 return af_caif_init();
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001234}
1235
1236static void __exit caif_sktexit_module(void)
1237{
1238 sock_unregister(PF_CAIF);
1239 if (debugfsdir != NULL)
1240 debugfs_remove_recursive(debugfsdir);
1241}
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001242module_init(caif_sktinit_module);
1243module_exit(caif_sktexit_module);