blob: 2eca2dd0000fd7dce7fc614ed5e476f0ca886874 [file] [log] [blame]
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001/*
2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Sjur Brendeland sjur.brandeland@stericsson.com
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00004 * License terms: GNU General Public License (GPL) version 2
5 */
6
Joe Perchesb31fa5b2010-09-05 21:31:11 +00007#define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
8
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00009#include <linux/fs.h>
10#include <linux/init.h>
11#include <linux/module.h>
12#include <linux/sched.h>
13#include <linux/spinlock.h>
14#include <linux/mutex.h>
15#include <linux/list.h>
16#include <linux/wait.h>
17#include <linux/poll.h>
18#include <linux/tcp.h>
19#include <linux/uaccess.h>
Sjur Braendelandbece7b22010-04-28 08:54:38 +000020#include <linux/debugfs.h>
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000021#include <linux/caif/caif_socket.h>
Sjur Braendelandbece7b22010-04-28 08:54:38 +000022#include <asm/atomic.h>
23#include <net/sock.h>
24#include <net/tcp_states.h>
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000025#include <net/caif/caif_layer.h>
26#include <net/caif/caif_dev.h>
27#include <net/caif/cfpkt.h>
28
29MODULE_LICENSE("GPL");
Sjur Braendelandbece7b22010-04-28 08:54:38 +000030MODULE_ALIAS_NETPROTO(AF_CAIF);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000031
Sjur Braendelandbece7b22010-04-28 08:54:38 +000032/*
33 * CAIF state is re-using the TCP socket states.
34 * caif_states stored in sk_state reflect the state as reported by
35 * the CAIF stack, while sk_socket->state is the state of the socket.
36 */
37enum caif_states {
38 CAIF_CONNECTED = TCP_ESTABLISHED,
39 CAIF_CONNECTING = TCP_SYN_SENT,
40 CAIF_DISCONNECTED = TCP_CLOSE
41};
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000042
Sjur Braendelandbece7b22010-04-28 08:54:38 +000043#define TX_FLOW_ON_BIT 1
44#define RX_FLOW_ON_BIT 2
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000045
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000046static struct dentry *debugfsdir;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000047
48#ifdef CONFIG_DEBUG_FS
49struct debug_fs_counter {
Sjur Braendelandbece7b22010-04-28 08:54:38 +000050 atomic_t caif_nr_socks;
51 atomic_t num_connect_req;
52 atomic_t num_connect_resp;
53 atomic_t num_connect_fail_resp;
54 atomic_t num_disconnect;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000055 atomic_t num_remote_shutdown_ind;
56 atomic_t num_tx_flow_off_ind;
57 atomic_t num_tx_flow_on_ind;
58 atomic_t num_rx_flow_off;
59 atomic_t num_rx_flow_on;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000060};
Sjur Braendelanda9a8f102010-05-21 02:16:11 +000061static struct debug_fs_counter cnt;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000062#define dbfs_atomic_inc(v) atomic_inc(v)
63#define dbfs_atomic_dec(v) atomic_dec(v)
64#else
65#define dbfs_atomic_inc(v)
66#define dbfs_atomic_dec(v)
67#endif
68
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000069struct caifsock {
Sjur Braendelandbece7b22010-04-28 08:54:38 +000070 struct sock sk; /* must be first member */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000071 struct cflayer layer;
Sjur Braendelandbece7b22010-04-28 08:54:38 +000072 char name[CAIF_LAYER_NAME_SZ]; /* Used for debugging */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000073 u32 flow_state;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000074 struct caif_connect_request conn_req;
Sjur Braendelandbece7b22010-04-28 08:54:38 +000075 struct mutex readlock;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000076 struct dentry *debugfs_socket_dir;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +000077 int headroom, tailroom, maxframe;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +000078};
79
Sjur Braendelandbece7b22010-04-28 08:54:38 +000080static int rx_flow_is_on(struct caifsock *cf_sk)
81{
82 return test_bit(RX_FLOW_ON_BIT,
83 (void *) &cf_sk->flow_state);
84}
85
86static int tx_flow_is_on(struct caifsock *cf_sk)
87{
88 return test_bit(TX_FLOW_ON_BIT,
89 (void *) &cf_sk->flow_state);
90}
91
92static void set_rx_flow_off(struct caifsock *cf_sk)
93{
94 clear_bit(RX_FLOW_ON_BIT,
95 (void *) &cf_sk->flow_state);
96}
97
98static void set_rx_flow_on(struct caifsock *cf_sk)
99{
100 set_bit(RX_FLOW_ON_BIT,
101 (void *) &cf_sk->flow_state);
102}
103
104static void set_tx_flow_off(struct caifsock *cf_sk)
105{
106 clear_bit(TX_FLOW_ON_BIT,
107 (void *) &cf_sk->flow_state);
108}
109
110static void set_tx_flow_on(struct caifsock *cf_sk)
111{
112 set_bit(TX_FLOW_ON_BIT,
113 (void *) &cf_sk->flow_state);
114}
115
116static void caif_read_lock(struct sock *sk)
117{
118 struct caifsock *cf_sk;
119 cf_sk = container_of(sk, struct caifsock, sk);
120 mutex_lock(&cf_sk->readlock);
121}
122
123static void caif_read_unlock(struct sock *sk)
124{
125 struct caifsock *cf_sk;
126 cf_sk = container_of(sk, struct caifsock, sk);
127 mutex_unlock(&cf_sk->readlock);
128}
129
Sjur Braendelanda9a8f102010-05-21 02:16:11 +0000130static int sk_rcvbuf_lowwater(struct caifsock *cf_sk)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000131{
132 /* A quarter of full buffer is used a low water mark */
133 return cf_sk->sk.sk_rcvbuf / 4;
134}
135
Sjur Braendelanda9a8f102010-05-21 02:16:11 +0000136static void caif_flow_ctrl(struct sock *sk, int mode)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000137{
138 struct caifsock *cf_sk;
139 cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000140 if (cf_sk->layer.dn && cf_sk->layer.dn->modemcmd)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000141 cf_sk->layer.dn->modemcmd(cf_sk->layer.dn, mode);
142}
143
144/*
145 * Copied from sock.c:sock_queue_rcv_skb(), but changed so packets are
146 * not dropped, but CAIF is sending flow off instead.
147 */
Sjur Braendelanda9a8f102010-05-21 02:16:11 +0000148static int caif_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000149{
150 int err;
151 int skb_len;
152 unsigned long flags;
153 struct sk_buff_head *list = &sk->sk_receive_queue;
154 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
155
156 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
157 (unsigned)sk->sk_rcvbuf && rx_flow_is_on(cf_sk)) {
Sjur Braendelandb04367d2010-09-21 11:44:44 +0000158 pr_debug("sending flow OFF (queue len = %d %d)\n",
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000159 atomic_read(&cf_sk->sk.sk_rmem_alloc),
160 sk_rcvbuf_lowwater(cf_sk));
161 set_rx_flow_off(cf_sk);
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000162 dbfs_atomic_inc(&cnt.num_rx_flow_off);
163 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_OFF_REQ);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000164 }
165
166 err = sk_filter(sk, skb);
167 if (err)
168 return err;
169 if (!sk_rmem_schedule(sk, skb->truesize) && rx_flow_is_on(cf_sk)) {
170 set_rx_flow_off(cf_sk);
Sjur Braendelandb04367d2010-09-21 11:44:44 +0000171 pr_debug("sending flow OFF due to rmem_schedule\n");
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000172 dbfs_atomic_inc(&cnt.num_rx_flow_off);
173 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_OFF_REQ);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000174 }
175 skb->dev = NULL;
176 skb_set_owner_r(skb, sk);
177 /* Cache the SKB length before we tack it onto the receive
178 * queue. Once it is added it no longer belongs to us and
179 * may be freed by other threads of control pulling packets
180 * from the queue.
181 */
182 skb_len = skb->len;
183 spin_lock_irqsave(&list->lock, flags);
184 if (!sock_flag(sk, SOCK_DEAD))
185 __skb_queue_tail(list, skb);
186 spin_unlock_irqrestore(&list->lock, flags);
187
188 if (!sock_flag(sk, SOCK_DEAD))
189 sk->sk_data_ready(sk, skb_len);
190 else
191 kfree_skb(skb);
192 return 0;
193}
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000194
195/* Packet Receive Callback function called from CAIF Stack */
196static int caif_sktrecv_cb(struct cflayer *layr, struct cfpkt *pkt)
197{
198 struct caifsock *cf_sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000199 struct sk_buff *skb;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000200
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000201 cf_sk = container_of(layr, struct caifsock, layer);
202 skb = cfpkt_tonative(pkt);
203
204 if (unlikely(cf_sk->sk.sk_state != CAIF_CONNECTED)) {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000205 cfpkt_destroy(pkt);
206 return 0;
207 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000208 caif_queue_rcv_skb(&cf_sk->sk, skb);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000209 return 0;
210}
211
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000212/* Packet Control Callback function called from CAIF */
213static void caif_ctrl_cb(struct cflayer *layr,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000214 enum caif_ctrlcmd flow,
215 int phyid)
216{
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000217 struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000218 switch (flow) {
219 case CAIF_CTRLCMD_FLOW_ON_IND:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000220 /* OK from modem to start sending again */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000221 dbfs_atomic_inc(&cnt.num_tx_flow_on_ind);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000222 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_FLOW_OFF_IND:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000227 /* Modem asks us to shut up */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000228 dbfs_atomic_inc(&cnt.num_tx_flow_off_ind);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000229 set_tx_flow_off(cf_sk);
230 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000231 break;
232
233 case CAIF_CTRLCMD_INIT_RSP:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000234 /* We're now connected */
235 dbfs_atomic_inc(&cnt.num_connect_resp);
236 cf_sk->sk.sk_state = CAIF_CONNECTED;
237 set_tx_flow_on(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_DEINIT_RSP:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000242 /* We're now disconnected */
243 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
244 cf_sk->sk.sk_state_change(&cf_sk->sk);
245 cfcnfg_release_adap_layer(&cf_sk->layer);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000246 break;
247
248 case CAIF_CTRLCMD_INIT_FAIL_RSP:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000249 /* Connect request failed */
250 dbfs_atomic_inc(&cnt.num_connect_fail_resp);
251 cf_sk->sk.sk_err = ECONNREFUSED;
252 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
253 cf_sk->sk.sk_shutdown = SHUTDOWN_MASK;
254 /*
255 * Socket "standards" seems to require POLLOUT to
256 * be set at connect failure.
257 */
258 set_tx_flow_on(cf_sk);
259 cf_sk->sk.sk_state_change(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000260 break;
261
262 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000263 /* Modem has closed this connection, or device is down. */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000264 dbfs_atomic_inc(&cnt.num_remote_shutdown_ind);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000265 cf_sk->sk.sk_shutdown = SHUTDOWN_MASK;
266 cf_sk->sk.sk_err = ECONNRESET;
267 set_rx_flow_on(cf_sk);
268 cf_sk->sk.sk_error_report(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000269 break;
270
271 default:
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000272 pr_debug("Unexpected flow command %d\n", flow);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000273 }
274}
275
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000276static void caif_check_flow_release(struct sock *sk)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000277{
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000278 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
279
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000280 if (rx_flow_is_on(cf_sk))
281 return;
282
283 if (atomic_read(&sk->sk_rmem_alloc) <= sk_rcvbuf_lowwater(cf_sk)) {
284 dbfs_atomic_inc(&cnt.num_rx_flow_on);
285 set_rx_flow_on(cf_sk);
Sjur Braendelandca6a09f2010-05-21 02:16:10 +0000286 caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_ON_REQ);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000287 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000288}
289
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000290/*
291 * Copied from unix_dgram_recvmsg, but removed credit checks,
292 * changed locking, address handling and added MSG_TRUNC.
293 */
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000294static int caif_seqpkt_recvmsg(struct kiocb *iocb, struct socket *sock,
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000295 struct msghdr *m, size_t len, int flags)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000296
297{
298 struct sock *sk = sock->sk;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000299 struct sk_buff *skb;
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000300 int ret;
301 int copylen;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000302
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000303 ret = -EOPNOTSUPP;
304 if (m->msg_flags&MSG_OOB)
305 goto read_error;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000306
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000307 skb = skb_recv_datagram(sk, flags, 0 , &ret);
308 if (!skb)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000309 goto read_error;
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000310 copylen = skb->len;
311 if (len < copylen) {
312 m->msg_flags |= MSG_TRUNC;
313 copylen = len;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000314 }
315
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000316 ret = skb_copy_datagram_iovec(skb, 0, m->msg_iov, copylen);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000317 if (ret)
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000318 goto out_free;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000319
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000320 ret = (flags & MSG_TRUNC) ? skb->len : copylen;
321out_free:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000322 skb_free_datagram(sk, skb);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000323 caif_check_flow_release(sk);
Sjur Braendelanddcda1382010-05-21 02:16:12 +0000324 return ret;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000325
326read_error:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000327 return ret;
328}
329
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000330
331/* Copied from unix_stream_wait_data, identical except for lock call. */
332static long caif_stream_data_wait(struct sock *sk, long timeo)
333{
334 DEFINE_WAIT(wait);
335 lock_sock(sk);
336
337 for (;;) {
338 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
339
340 if (!skb_queue_empty(&sk->sk_receive_queue) ||
341 sk->sk_err ||
342 sk->sk_state != CAIF_CONNECTED ||
343 sock_flag(sk, SOCK_DEAD) ||
344 (sk->sk_shutdown & RCV_SHUTDOWN) ||
345 signal_pending(current) ||
346 !timeo)
347 break;
348
349 set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
350 release_sock(sk);
351 timeo = schedule_timeout(timeo);
352 lock_sock(sk);
353 clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
354 }
355
356 finish_wait(sk_sleep(sk), &wait);
357 release_sock(sk);
358 return timeo;
359}
360
361
362/*
363 * Copied from unix_stream_recvmsg, but removed credit checks,
364 * changed locking calls, changed address handling.
365 */
366static int caif_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
367 struct msghdr *msg, size_t size,
368 int flags)
369{
370 struct sock *sk = sock->sk;
371 int copied = 0;
372 int target;
373 int err = 0;
374 long timeo;
375
376 err = -EOPNOTSUPP;
377 if (flags&MSG_OOB)
378 goto out;
379
380 msg->msg_namelen = 0;
381
382 /*
383 * Lock the socket to prevent queue disordering
384 * while sleeps in memcpy_tomsg
385 */
386 err = -EAGAIN;
387 if (sk->sk_state == CAIF_CONNECTING)
388 goto out;
389
390 caif_read_lock(sk);
391 target = sock_rcvlowat(sk, flags&MSG_WAITALL, size);
392 timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
393
394 do {
395 int chunk;
396 struct sk_buff *skb;
397
398 lock_sock(sk);
399 skb = skb_dequeue(&sk->sk_receive_queue);
400 caif_check_flow_release(sk);
401
402 if (skb == NULL) {
403 if (copied >= target)
404 goto unlock;
405 /*
406 * POSIX 1003.1g mandates this order.
407 */
408 err = sock_error(sk);
409 if (err)
410 goto unlock;
411 err = -ECONNRESET;
412 if (sk->sk_shutdown & RCV_SHUTDOWN)
413 goto unlock;
414
415 err = -EPIPE;
416 if (sk->sk_state != CAIF_CONNECTED)
417 goto unlock;
418 if (sock_flag(sk, SOCK_DEAD))
419 goto unlock;
420
421 release_sock(sk);
422
423 err = -EAGAIN;
424 if (!timeo)
425 break;
426
427 caif_read_unlock(sk);
428
429 timeo = caif_stream_data_wait(sk, timeo);
430
431 if (signal_pending(current)) {
432 err = sock_intr_errno(timeo);
433 goto out;
434 }
435 caif_read_lock(sk);
436 continue;
437unlock:
438 release_sock(sk);
439 break;
440 }
441 release_sock(sk);
442 chunk = min_t(unsigned int, skb->len, size);
443 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
444 skb_queue_head(&sk->sk_receive_queue, skb);
445 if (copied == 0)
446 copied = -EFAULT;
447 break;
448 }
449 copied += chunk;
450 size -= chunk;
451
452 /* Mark read part of skb as used */
453 if (!(flags & MSG_PEEK)) {
454 skb_pull(skb, chunk);
455
456 /* put the skb back if we didn't use it up. */
457 if (skb->len) {
458 skb_queue_head(&sk->sk_receive_queue, skb);
459 break;
460 }
461 kfree_skb(skb);
462
463 } else {
464 /*
465 * It is questionable, see note in unix_dgram_recvmsg.
466 */
467 /* put message back and return */
468 skb_queue_head(&sk->sk_receive_queue, skb);
469 break;
470 }
471 } while (size);
472 caif_read_unlock(sk);
473
474out:
475 return copied ? : err;
476}
477
478/*
479 * Copied from sock.c:sock_wait_for_wmem, but change to wait for
480 * CAIF flow-on and sock_writable.
481 */
482static long caif_wait_for_flow_on(struct caifsock *cf_sk,
483 int wait_writeable, long timeo, int *err)
484{
485 struct sock *sk = &cf_sk->sk;
486 DEFINE_WAIT(wait);
487 for (;;) {
488 *err = 0;
489 if (tx_flow_is_on(cf_sk) &&
490 (!wait_writeable || sock_writeable(&cf_sk->sk)))
491 break;
492 *err = -ETIMEDOUT;
493 if (!timeo)
494 break;
495 *err = -ERESTARTSYS;
496 if (signal_pending(current))
497 break;
498 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
499 *err = -ECONNRESET;
500 if (sk->sk_shutdown & SHUTDOWN_MASK)
501 break;
502 *err = -sk->sk_err;
503 if (sk->sk_err)
504 break;
505 *err = -EPIPE;
506 if (cf_sk->sk.sk_state != CAIF_CONNECTED)
507 break;
508 timeo = schedule_timeout(timeo);
509 }
510 finish_wait(sk_sleep(sk), &wait);
511 return timeo;
512}
513
514/*
515 * Transmit a SKB. The device may temporarily request re-transmission
516 * by returning EAGAIN.
517 */
518static int transmit_skb(struct sk_buff *skb, struct caifsock *cf_sk,
519 int noblock, long timeo)
520{
521 struct cfpkt *pkt;
522 int ret, loopcnt = 0;
523
524 pkt = cfpkt_fromnative(CAIF_DIR_OUT, skb);
525 memset(cfpkt_info(pkt), 0, sizeof(struct caif_payload_info));
526 do {
527
528 ret = -ETIMEDOUT;
529
530 /* Slight paranoia, probably not needed. */
531 if (unlikely(loopcnt++ > 1000)) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000532 pr_warn("transmit retries failed, error = %d\n", ret);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000533 break;
534 }
535
536 if (cf_sk->layer.dn != NULL)
537 ret = cf_sk->layer.dn->transmit(cf_sk->layer.dn, pkt);
538 if (likely(ret >= 0))
539 break;
540 /* if transmit return -EAGAIN, then retry */
541 if (noblock && ret == -EAGAIN)
542 break;
543 timeo = caif_wait_for_flow_on(cf_sk, 0, timeo, &ret);
544 if (signal_pending(current)) {
545 ret = sock_intr_errno(timeo);
546 break;
547 }
548 if (ret)
549 break;
550 if (cf_sk->sk.sk_state != CAIF_CONNECTED ||
551 sock_flag(&cf_sk->sk, SOCK_DEAD) ||
552 (cf_sk->sk.sk_shutdown & RCV_SHUTDOWN)) {
553 ret = -EPIPE;
554 cf_sk->sk.sk_err = EPIPE;
555 break;
556 }
557 } while (ret == -EAGAIN);
558 return ret;
559}
560
561/* Copied from af_unix:unix_dgram_sendmsg, and adapted to CAIF */
562static int caif_seqpkt_sendmsg(struct kiocb *kiocb, struct socket *sock,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000563 struct msghdr *msg, size_t len)
564{
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000565 struct sock *sk = sock->sk;
566 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000567 int buffer_size;
568 int ret = 0;
569 struct sk_buff *skb = NULL;
570 int noblock;
571 long timeo;
572 caif_assert(cf_sk);
573 ret = sock_error(sk);
574 if (ret)
575 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000576
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000577 ret = -EOPNOTSUPP;
578 if (msg->msg_flags&MSG_OOB)
579 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000580
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000581 ret = -EOPNOTSUPP;
582 if (msg->msg_namelen)
583 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000584
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000585 ret = -EINVAL;
586 if (unlikely(msg->msg_iov->iov_base == NULL))
587 goto err;
588 noblock = msg->msg_flags & MSG_DONTWAIT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000589
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000590 timeo = sock_sndtimeo(sk, noblock);
591 timeo = caif_wait_for_flow_on(container_of(sk, struct caifsock, sk),
592 1, timeo, &ret);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000593
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000594 if (ret)
595 goto err;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000596 ret = -EPIPE;
597 if (cf_sk->sk.sk_state != CAIF_CONNECTED ||
598 sock_flag(sk, SOCK_DEAD) ||
599 (sk->sk_shutdown & RCV_SHUTDOWN))
600 goto err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000601
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000602 /* Error if trying to write more than maximum frame size. */
603 ret = -EMSGSIZE;
604 if (len > cf_sk->maxframe && cf_sk->sk.sk_protocol != CAIFPROTO_RFM)
605 goto err;
606
607 buffer_size = len + cf_sk->headroom + cf_sk->tailroom;
608
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000609 ret = -ENOMEM;
610 skb = sock_alloc_send_skb(sk, buffer_size, noblock, &ret);
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000611
612 if (!skb || skb_tailroom(skb) < buffer_size)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000613 goto err;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000614
615 skb_reserve(skb, cf_sk->headroom);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000616
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000617 ret = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000618
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000619 if (ret)
620 goto err;
621 ret = transmit_skb(skb, cf_sk, noblock, timeo);
622 if (ret < 0)
623 goto err;
624 return len;
625err:
626 kfree_skb(skb);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000627 return ret;
628}
629
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000630/*
631 * Copied from unix_stream_sendmsg and adapted to CAIF:
632 * Changed removed permission handling and added waiting for flow on
633 * and other minor adaptations.
634 */
635static int caif_stream_sendmsg(struct kiocb *kiocb, struct socket *sock,
636 struct msghdr *msg, size_t len)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000637{
638 struct sock *sk = sock->sk;
639 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000640 int err, size;
641 struct sk_buff *skb;
642 int sent = 0;
643 long timeo;
644
645 err = -EOPNOTSUPP;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000646 if (unlikely(msg->msg_flags&MSG_OOB))
647 goto out_err;
648
649 if (unlikely(msg->msg_namelen))
650 goto out_err;
651
652 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
653 timeo = caif_wait_for_flow_on(cf_sk, 1, timeo, &err);
654
655 if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN))
656 goto pipe_err;
657
658 while (sent < len) {
659
660 size = len-sent;
661
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000662 if (size > cf_sk->maxframe)
663 size = cf_sk->maxframe;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000664
665 /* If size is more than half of sndbuf, chop up message */
666 if (size > ((sk->sk_sndbuf >> 1) - 64))
667 size = (sk->sk_sndbuf >> 1) - 64;
668
669 if (size > SKB_MAX_ALLOC)
670 size = SKB_MAX_ALLOC;
671
672 skb = sock_alloc_send_skb(sk,
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000673 size + cf_sk->headroom +
674 cf_sk->tailroom,
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000675 msg->msg_flags&MSG_DONTWAIT,
676 &err);
677 if (skb == NULL)
678 goto out_err;
679
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000680 skb_reserve(skb, cf_sk->headroom);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000681 /*
682 * If you pass two values to the sock_alloc_send_skb
683 * it tries to grab the large buffer with GFP_NOFS
684 * (which can fail easily), and if it fails grab the
685 * fallback size buffer which is under a page and will
686 * succeed. [Alan]
687 */
688 size = min_t(int, size, skb_tailroom(skb));
689
690 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
691 if (err) {
692 kfree_skb(skb);
693 goto out_err;
694 }
695 err = transmit_skb(skb, cf_sk,
696 msg->msg_flags&MSG_DONTWAIT, timeo);
697 if (err < 0) {
698 kfree_skb(skb);
699 goto pipe_err;
700 }
701 sent += size;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000702 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000703
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000704 return sent;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000705
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000706pipe_err:
707 if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL))
708 send_sig(SIGPIPE, current, 0);
709 err = -EPIPE;
710out_err:
711 return sent ? : err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000712}
713
714static int setsockopt(struct socket *sock,
715 int lvl, int opt, char __user *ov, unsigned int ol)
716{
717 struct sock *sk = sock->sk;
718 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
719 int prio, linksel;
720 struct ifreq ifreq;
721
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000722 if (cf_sk->sk.sk_socket->state != SS_UNCONNECTED)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000723 return -ENOPROTOOPT;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000724
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000725 switch (opt) {
726 case CAIFSO_LINK_SELECT:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000727 if (ol < sizeof(int))
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000728 return -EINVAL;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000729 if (lvl != SOL_CAIF)
730 goto bad_sol;
731 if (copy_from_user(&linksel, ov, sizeof(int)))
732 return -EINVAL;
733 lock_sock(&(cf_sk->sk));
734 cf_sk->conn_req.link_selector = linksel;
735 release_sock(&cf_sk->sk);
736 return 0;
737
738 case SO_PRIORITY:
739 if (lvl != SOL_SOCKET)
740 goto bad_sol;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000741 if (ol < sizeof(int))
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000742 return -EINVAL;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000743 if (copy_from_user(&prio, ov, sizeof(int)))
744 return -EINVAL;
745 lock_sock(&(cf_sk->sk));
746 cf_sk->conn_req.priority = prio;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000747 release_sock(&cf_sk->sk);
748 return 0;
749
750 case SO_BINDTODEVICE:
751 if (lvl != SOL_SOCKET)
752 goto bad_sol;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000753 if (ol < sizeof(struct ifreq))
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000754 return -EINVAL;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000755 if (copy_from_user(&ifreq, ov, sizeof(ifreq)))
756 return -EFAULT;
757 lock_sock(&(cf_sk->sk));
758 strncpy(cf_sk->conn_req.link_name, ifreq.ifr_name,
759 sizeof(cf_sk->conn_req.link_name));
760 cf_sk->conn_req.link_name
761 [sizeof(cf_sk->conn_req.link_name)-1] = 0;
762 release_sock(&cf_sk->sk);
763 return 0;
764
765 case CAIFSO_REQ_PARAM:
766 if (lvl != SOL_CAIF)
767 goto bad_sol;
768 if (cf_sk->sk.sk_protocol != CAIFPROTO_UTIL)
769 return -ENOPROTOOPT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000770 lock_sock(&(cf_sk->sk));
771 cf_sk->conn_req.param.size = ol;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000772 if (ol > sizeof(cf_sk->conn_req.param.data) ||
773 copy_from_user(&cf_sk->conn_req.param.data, ov, ol)) {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000774 release_sock(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000775 return -EINVAL;
776 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000777 release_sock(&cf_sk->sk);
778 return 0;
779
780 default:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000781 return -ENOPROTOOPT;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000782 }
783
784 return 0;
785bad_sol:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000786 return -ENOPROTOOPT;
787
788}
789
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000790/*
791 * caif_connect() - Connect a CAIF Socket
792 * Copied and modified af_irda.c:irda_connect().
793 *
794 * Note : by consulting "errno", the user space caller may learn the cause
795 * of the failure. Most of them are visible in the function, others may come
796 * from subroutines called and are listed here :
797 * o -EAFNOSUPPORT: bad socket family or type.
798 * o -ESOCKTNOSUPPORT: bad socket type or protocol
799 * o -EINVAL: bad socket address, or CAIF link type
800 * o -ECONNREFUSED: remote end refused the connection.
801 * o -EINPROGRESS: connect request sent but timed out (or non-blocking)
802 * o -EISCONN: already connected.
803 * o -ETIMEDOUT: Connection timed out (send timeout)
804 * o -ENODEV: No link layer to send request
805 * o -ECONNRESET: Received Shutdown indication or lost link layer
806 * o -ENOMEM: Out of memory
807 *
808 * State Strategy:
809 * o sk_state: holds the CAIF_* protocol state, it's updated by
810 * caif_ctrl_cb.
811 * o sock->state: holds the SS_* socket state and is updated by connect and
812 * disconnect.
813 */
814static int caif_connect(struct socket *sock, struct sockaddr *uaddr,
815 int addr_len, int flags)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000816{
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000817 struct sock *sk = sock->sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000818 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
819 long timeo;
820 int err;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000821 int ifindex, headroom, tailroom;
Eric Dumazet79315062010-10-04 22:42:08 +0000822 unsigned int mtu;
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000823 struct net_device *dev;
824
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000825 lock_sock(sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000826
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000827 err = -EAFNOSUPPORT;
828 if (uaddr->sa_family != AF_CAIF)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000829 goto out;
830
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000831 switch (sock->state) {
832 case SS_UNCONNECTED:
833 /* Normal case, a fresh connect */
834 caif_assert(sk->sk_state == CAIF_DISCONNECTED);
835 break;
836 case SS_CONNECTING:
837 switch (sk->sk_state) {
838 case CAIF_CONNECTED:
839 sock->state = SS_CONNECTED;
840 err = -EISCONN;
841 goto out;
842 case CAIF_DISCONNECTED:
843 /* Reconnect allowed */
844 break;
845 case CAIF_CONNECTING:
846 err = -EALREADY;
847 if (flags & O_NONBLOCK)
848 goto out;
849 goto wait_connect;
850 }
851 break;
852 case SS_CONNECTED:
853 caif_assert(sk->sk_state == CAIF_CONNECTED ||
854 sk->sk_state == CAIF_DISCONNECTED);
855 if (sk->sk_shutdown & SHUTDOWN_MASK) {
856 /* Allow re-connect after SHUTDOWN_IND */
857 caif_disconnect_client(&cf_sk->layer);
858 break;
859 }
860 /* No reconnect on a seqpacket socket */
861 err = -EISCONN;
862 goto out;
863 case SS_DISCONNECTING:
864 case SS_FREE:
865 caif_assert(1); /*Should never happen */
866 break;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000867 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000868 sk->sk_state = CAIF_DISCONNECTED;
869 sock->state = SS_UNCONNECTED;
870 sk_stream_kill_queues(&cf_sk->sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000871
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000872 err = -EINVAL;
Dan Carpenterf5d72af2010-05-22 10:43:42 +0000873 if (addr_len != sizeof(struct sockaddr_caif))
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000874 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000875
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000876 memcpy(&cf_sk->conn_req.sockaddr, uaddr,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000877 sizeof(struct sockaddr_caif));
878
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000879 /* Move to connecting socket, start sending Connect Requests */
880 sock->state = SS_CONNECTING;
881 sk->sk_state = CAIF_CONNECTING;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000882
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000883 dbfs_atomic_inc(&cnt.num_connect_req);
884 cf_sk->layer.receive = caif_sktrecv_cb;
885 err = caif_connect_client(&cf_sk->conn_req,
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000886 &cf_sk->layer, &ifindex, &headroom, &tailroom);
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000887 if (err < 0) {
888 cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
889 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
890 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000891 }
Eric Dumazet79315062010-10-04 22:42:08 +0000892
893 err = -ENODEV;
894 rcu_read_lock();
895 dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
896 if (!dev) {
897 rcu_read_unlock();
898 goto out;
899 }
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000900 cf_sk->headroom = LL_RESERVED_SPACE_EXTRA(dev, headroom);
Eric Dumazet79315062010-10-04 22:42:08 +0000901 mtu = dev->mtu;
902 rcu_read_unlock();
903
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000904 cf_sk->tailroom = tailroom;
Eric Dumazet79315062010-10-04 22:42:08 +0000905 cf_sk->maxframe = mtu - (headroom + tailroom);
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000906 if (cf_sk->maxframe < 1) {
Joe Perchesb31fa5b2010-09-05 21:31:11 +0000907 pr_warn("CAIF Interface MTU too small (%d)\n", dev->mtu);
Sjur Braendeland2aa40ae2010-06-17 06:55:40 +0000908 goto out;
909 }
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000910
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000911 err = -EINPROGRESS;
912wait_connect:
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000913
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000914 if (sk->sk_state != CAIF_CONNECTED && (flags & O_NONBLOCK))
915 goto out;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000916
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000917 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000918
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000919 release_sock(sk);
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000920 err = -ERESTARTSYS;
921 timeo = wait_event_interruptible_timeout(*sk_sleep(sk),
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000922 sk->sk_state != CAIF_CONNECTING,
923 timeo);
924 lock_sock(sk);
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000925 if (timeo < 0)
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000926 goto out; /* -ERESTARTSYS */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000927
Sjur Braendeland9e4b8162010-05-21 02:16:07 +0000928 err = -ETIMEDOUT;
929 if (timeo == 0 && sk->sk_state != CAIF_CONNECTED)
930 goto out;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000931 if (sk->sk_state != CAIF_CONNECTED) {
932 sock->state = SS_UNCONNECTED;
933 err = sock_error(sk);
934 if (!err)
935 err = -ECONNREFUSED;
936 goto out;
937 }
938 sock->state = SS_CONNECTED;
939 err = 0;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000940out:
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000941 release_sock(sk);
942 return err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000943}
944
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000945/*
946 * caif_release() - Disconnect a CAIF Socket
947 * Copied and modified af_irda.c:irda_release().
948 */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000949static int caif_release(struct socket *sock)
950{
951 struct sock *sk = sock->sk;
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000952 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
953 int res = 0;
954
955 if (!sk)
956 return 0;
957
958 set_tx_flow_off(cf_sk);
959
960 /*
961 * Ensure that packets are not queued after this point in time.
962 * caif_queue_rcv_skb checks SOCK_DEAD holding the queue lock,
963 * this ensures no packets when sock is dead.
964 */
965 spin_lock(&sk->sk_receive_queue.lock);
966 sock_set_flag(sk, SOCK_DEAD);
967 spin_unlock(&sk->sk_receive_queue.lock);
968 sock->sk = NULL;
969
970 dbfs_atomic_inc(&cnt.num_disconnect);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000971
972 if (cf_sk->debugfs_socket_dir != NULL)
973 debugfs_remove_recursive(cf_sk->debugfs_socket_dir);
974
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000975 lock_sock(&(cf_sk->sk));
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000976 sk->sk_state = CAIF_DISCONNECTED;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000977 sk->sk_shutdown = SHUTDOWN_MASK;
978
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000979 if (cf_sk->sk.sk_socket->state == SS_CONNECTED ||
980 cf_sk->sk.sk_socket->state == SS_CONNECTING)
981 res = caif_disconnect_client(&cf_sk->layer);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000982
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000983 cf_sk->sk.sk_socket->state = SS_DISCONNECTING;
984 wake_up_interruptible_poll(sk_sleep(sk), POLLERR|POLLHUP);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000985
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000986 sock_orphan(sk);
987 cf_sk->layer.dn = NULL;
988 sk_stream_kill_queues(&cf_sk->sk);
989 release_sock(sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000990 sock_put(sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +0000991 return res;
992}
993
Sjur Braendelandbece7b22010-04-28 08:54:38 +0000994/* Copied from af_unix.c:unix_poll(), added CAIF tx_flow handling */
995static unsigned int caif_poll(struct file *file,
996 struct socket *sock, poll_table *wait)
997{
998 struct sock *sk = sock->sk;
999 unsigned int mask;
1000 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
1001
1002 sock_poll_wait(file, sk_sleep(sk), wait);
1003 mask = 0;
1004
1005 /* exceptional events? */
1006 if (sk->sk_err)
1007 mask |= POLLERR;
1008 if (sk->sk_shutdown == SHUTDOWN_MASK)
1009 mask |= POLLHUP;
1010 if (sk->sk_shutdown & RCV_SHUTDOWN)
1011 mask |= POLLRDHUP;
1012
1013 /* readable? */
1014 if (!skb_queue_empty(&sk->sk_receive_queue) ||
1015 (sk->sk_shutdown & RCV_SHUTDOWN))
1016 mask |= POLLIN | POLLRDNORM;
1017
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001018 /*
1019 * we set writable also when the other side has shut down the
1020 * connection. This prevents stuck sockets.
1021 */
1022 if (sock_writeable(sk) && tx_flow_is_on(cf_sk))
1023 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1024
1025 return mask;
1026}
1027
1028static const struct proto_ops caif_seqpacket_ops = {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001029 .family = PF_CAIF,
1030 .owner = THIS_MODULE,
1031 .release = caif_release,
1032 .bind = sock_no_bind,
1033 .connect = caif_connect,
1034 .socketpair = sock_no_socketpair,
1035 .accept = sock_no_accept,
1036 .getname = sock_no_getname,
1037 .poll = caif_poll,
1038 .ioctl = sock_no_ioctl,
1039 .listen = sock_no_listen,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001040 .shutdown = sock_no_shutdown,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001041 .setsockopt = setsockopt,
1042 .getsockopt = sock_no_getsockopt,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001043 .sendmsg = caif_seqpkt_sendmsg,
1044 .recvmsg = caif_seqpkt_recvmsg,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001045 .mmap = sock_no_mmap,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001046 .sendpage = sock_no_sendpage,
1047};
1048
1049static const struct proto_ops caif_stream_ops = {
1050 .family = PF_CAIF,
1051 .owner = THIS_MODULE,
1052 .release = caif_release,
1053 .bind = sock_no_bind,
1054 .connect = caif_connect,
1055 .socketpair = sock_no_socketpair,
1056 .accept = sock_no_accept,
1057 .getname = sock_no_getname,
1058 .poll = caif_poll,
1059 .ioctl = sock_no_ioctl,
1060 .listen = sock_no_listen,
1061 .shutdown = sock_no_shutdown,
1062 .setsockopt = setsockopt,
1063 .getsockopt = sock_no_getsockopt,
1064 .sendmsg = caif_stream_sendmsg,
1065 .recvmsg = caif_stream_recvmsg,
1066 .mmap = sock_no_mmap,
1067 .sendpage = sock_no_sendpage,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001068};
1069
1070/* This function is called when a socket is finally destroyed. */
1071static void caif_sock_destructor(struct sock *sk)
1072{
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001073 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001074 caif_assert(!atomic_read(&sk->sk_wmem_alloc));
1075 caif_assert(sk_unhashed(sk));
1076 caif_assert(!sk->sk_socket);
1077 if (!sock_flag(sk, SOCK_DEAD)) {
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001078 pr_info("Attempt to release alive CAIF socket: %p\n", sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001079 return;
1080 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001081 sk_stream_kill_queues(&cf_sk->sk);
1082 dbfs_atomic_dec(&cnt.caif_nr_socks);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001083}
1084
1085static int caif_create(struct net *net, struct socket *sock, int protocol,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001086 int kern)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001087{
1088 struct sock *sk = NULL;
1089 struct caifsock *cf_sk = NULL;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001090 static struct proto prot = {.name = "PF_CAIF",
1091 .owner = THIS_MODULE,
1092 .obj_size = sizeof(struct caifsock),
1093 };
1094
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001095 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_NET_ADMIN))
1096 return -EPERM;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001097 /*
1098 * The sock->type specifies the socket type to use.
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001099 * The CAIF socket is a packet stream in the sense
1100 * that it is packet based. CAIF trusts the reliability
1101 * of the link, no resending is implemented.
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001102 */
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001103 if (sock->type == SOCK_SEQPACKET)
1104 sock->ops = &caif_seqpacket_ops;
1105 else if (sock->type == SOCK_STREAM)
1106 sock->ops = &caif_stream_ops;
1107 else
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001108 return -ESOCKTNOSUPPORT;
1109
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001110 if (protocol < 0 || protocol >= CAIFPROTO_MAX)
1111 return -EPROTONOSUPPORT;
1112 /*
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001113 * Set the socket state to unconnected. The socket state
1114 * is really not used at all in the net/core or socket.c but the
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001115 * initialization makes sure that sock->state is not uninitialized.
1116 */
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001117 sk = sk_alloc(net, PF_CAIF, GFP_KERNEL, &prot);
1118 if (!sk)
1119 return -ENOMEM;
1120
1121 cf_sk = container_of(sk, struct caifsock, sk);
1122
1123 /* Store the protocol */
1124 sk->sk_protocol = (unsigned char) protocol;
1125
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001126 /*
1127 * Lock in order to try to stop someone from opening the socket
1128 * too early.
1129 */
1130 lock_sock(&(cf_sk->sk));
1131
1132 /* Initialize the nozero default sock structure data. */
1133 sock_init_data(sock, sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001134 sk->sk_destruct = caif_sock_destructor;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001135
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001136 mutex_init(&cf_sk->readlock); /* single task reading lock */
1137 cf_sk->layer.ctrlcmd = caif_ctrl_cb;
1138 cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
1139 cf_sk->sk.sk_state = CAIF_DISCONNECTED;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001140
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001141 set_tx_flow_off(cf_sk);
1142 set_rx_flow_on(cf_sk);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001143
1144 /* Set default options on configuration */
1145 cf_sk->conn_req.priority = CAIF_PRIO_NORMAL;
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001146 cf_sk->conn_req.link_selector = CAIF_LINK_LOW_LATENCY;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001147 cf_sk->conn_req.protocol = protocol;
1148 /* Increase the number of sockets created. */
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001149 dbfs_atomic_inc(&cnt.caif_nr_socks);
1150#ifdef CONFIG_DEBUG_FS
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001151 if (!IS_ERR(debugfsdir)) {
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001152 /* Fill in some information concerning the misc socket. */
1153 snprintf(cf_sk->name, sizeof(cf_sk->name), "cfsk%d",
1154 atomic_read(&cnt.caif_nr_socks));
1155
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001156 cf_sk->debugfs_socket_dir =
1157 debugfs_create_dir(cf_sk->name, debugfsdir);
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001158 debugfs_create_u32("sk_state", S_IRUSR | S_IWUSR,
1159 cf_sk->debugfs_socket_dir,
1160 (u32 *) &cf_sk->sk.sk_state);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001161 debugfs_create_u32("flow_state", S_IRUSR | S_IWUSR,
1162 cf_sk->debugfs_socket_dir, &cf_sk->flow_state);
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001163 debugfs_create_u32("sk_rmem_alloc", S_IRUSR | S_IWUSR,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001164 cf_sk->debugfs_socket_dir,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001165 (u32 *) &cf_sk->sk.sk_rmem_alloc);
1166 debugfs_create_u32("sk_wmem_alloc", S_IRUSR | S_IWUSR,
1167 cf_sk->debugfs_socket_dir,
1168 (u32 *) &cf_sk->sk.sk_wmem_alloc);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001169 debugfs_create_u32("identity", S_IRUSR | S_IWUSR,
1170 cf_sk->debugfs_socket_dir,
1171 (u32 *) &cf_sk->layer.id);
1172 }
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001173#endif
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001174 release_sock(&cf_sk->sk);
1175 return 0;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001176}
1177
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001178
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001179static struct net_proto_family caif_family_ops = {
1180 .family = PF_CAIF,
1181 .create = caif_create,
1182 .owner = THIS_MODULE,
1183};
1184
Sjur Braendelanda9a8f102010-05-21 02:16:11 +00001185static int af_caif_init(void)
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001186{
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001187 int err = sock_register(&caif_family_ops);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001188 if (!err)
1189 return err;
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001190 return 0;
1191}
1192
1193static int __init caif_sktinit_module(void)
1194{
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001195#ifdef CONFIG_DEBUG_FS
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001196 debugfsdir = debugfs_create_dir("caif_sk", NULL);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001197 if (!IS_ERR(debugfsdir)) {
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001198 debugfs_create_u32("num_sockets", S_IRUSR | S_IWUSR,
1199 debugfsdir,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001200 (u32 *) &cnt.caif_nr_socks);
1201 debugfs_create_u32("num_connect_req", S_IRUSR | S_IWUSR,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001202 debugfsdir,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001203 (u32 *) &cnt.num_connect_req);
1204 debugfs_create_u32("num_connect_resp", 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_resp);
1207 debugfs_create_u32("num_connect_fail_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_fail_resp);
1210 debugfs_create_u32("num_disconnect", S_IRUSR | S_IWUSR,
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001211 debugfsdir,
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001212 (u32 *) &cnt.num_disconnect);
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001213 debugfs_create_u32("num_remote_shutdown_ind",
1214 S_IRUSR | S_IWUSR, debugfsdir,
1215 (u32 *) &cnt.num_remote_shutdown_ind);
1216 debugfs_create_u32("num_tx_flow_off_ind", S_IRUSR | S_IWUSR,
1217 debugfsdir,
1218 (u32 *) &cnt.num_tx_flow_off_ind);
1219 debugfs_create_u32("num_tx_flow_on_ind", S_IRUSR | S_IWUSR,
1220 debugfsdir,
1221 (u32 *) &cnt.num_tx_flow_on_ind);
1222 debugfs_create_u32("num_rx_flow_off", S_IRUSR | S_IWUSR,
1223 debugfsdir,
1224 (u32 *) &cnt.num_rx_flow_off);
1225 debugfs_create_u32("num_rx_flow_on", S_IRUSR | S_IWUSR,
1226 debugfsdir,
1227 (u32 *) &cnt.num_rx_flow_on);
1228 }
1229#endif
Sjur Braendelandbece7b22010-04-28 08:54:38 +00001230 return af_caif_init();
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001231}
1232
1233static void __exit caif_sktexit_module(void)
1234{
1235 sock_unregister(PF_CAIF);
1236 if (debugfsdir != NULL)
1237 debugfs_remove_recursive(debugfsdir);
1238}
Sjur Braendelande6f95ec2010-03-30 13:56:26 +00001239module_init(caif_sktinit_module);
1240module_exit(caif_sktexit_module);