2 * Copyright (C) ST-Ericsson AB 2010
3 * Author: Sjur Brendeland
4 * License terms: GNU General Public License (GPL) version 2
7 #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
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>
20 #include <linux/debugfs.h>
21 #include <linux/caif/caif_socket.h>
22 #include <linux/pkt_sched.h>
24 #include <net/tcp_states.h>
25 #include <net/caif/caif_layer.h>
26 #include <net/caif/caif_dev.h>
27 #include <net/caif/cfpkt.h>
29 MODULE_LICENSE("GPL");
30 MODULE_ALIAS_NETPROTO(AF_CAIF
);
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.
38 CAIF_CONNECTED
= TCP_ESTABLISHED
,
39 CAIF_CONNECTING
= TCP_SYN_SENT
,
40 CAIF_DISCONNECTED
= TCP_CLOSE
43 #define TX_FLOW_ON_BIT 1
44 #define RX_FLOW_ON_BIT 2
47 struct sock sk
; /* must be first member */
50 struct caif_connect_request conn_req
;
51 struct mutex readlock
;
52 struct dentry
*debugfs_socket_dir
;
53 int headroom
, tailroom
, maxframe
;
56 static int rx_flow_is_on(struct caifsock
*cf_sk
)
58 return test_bit(RX_FLOW_ON_BIT
,
59 (void *) &cf_sk
->flow_state
);
62 static int tx_flow_is_on(struct caifsock
*cf_sk
)
64 return test_bit(TX_FLOW_ON_BIT
,
65 (void *) &cf_sk
->flow_state
);
68 static void set_rx_flow_off(struct caifsock
*cf_sk
)
70 clear_bit(RX_FLOW_ON_BIT
,
71 (void *) &cf_sk
->flow_state
);
74 static void set_rx_flow_on(struct caifsock
*cf_sk
)
76 set_bit(RX_FLOW_ON_BIT
,
77 (void *) &cf_sk
->flow_state
);
80 static void set_tx_flow_off(struct caifsock
*cf_sk
)
82 clear_bit(TX_FLOW_ON_BIT
,
83 (void *) &cf_sk
->flow_state
);
86 static void set_tx_flow_on(struct caifsock
*cf_sk
)
88 set_bit(TX_FLOW_ON_BIT
,
89 (void *) &cf_sk
->flow_state
);
92 static void caif_read_lock(struct sock
*sk
)
94 struct caifsock
*cf_sk
;
95 cf_sk
= container_of(sk
, struct caifsock
, sk
);
96 mutex_lock(&cf_sk
->readlock
);
99 static void caif_read_unlock(struct sock
*sk
)
101 struct caifsock
*cf_sk
;
102 cf_sk
= container_of(sk
, struct caifsock
, sk
);
103 mutex_unlock(&cf_sk
->readlock
);
106 static int sk_rcvbuf_lowwater(struct caifsock
*cf_sk
)
108 /* A quarter of full buffer is used a low water mark */
109 return cf_sk
->sk
.sk_rcvbuf
/ 4;
112 static void caif_flow_ctrl(struct sock
*sk
, int mode
)
114 struct caifsock
*cf_sk
;
115 cf_sk
= container_of(sk
, struct caifsock
, sk
);
116 if (cf_sk
->layer
.dn
&& cf_sk
->layer
.dn
->modemcmd
)
117 cf_sk
->layer
.dn
->modemcmd(cf_sk
->layer
.dn
, mode
);
121 * Copied from sock.c:sock_queue_rcv_skb(), but changed so packets are
122 * not dropped, but CAIF is sending flow off instead.
124 static int caif_queue_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
128 struct sk_buff_head
*list
= &sk
->sk_receive_queue
;
129 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
131 if (atomic_read(&sk
->sk_rmem_alloc
) + skb
->truesize
>=
132 (unsigned int)sk
->sk_rcvbuf
&& rx_flow_is_on(cf_sk
)) {
133 net_dbg_ratelimited("sending flow OFF (queue len = %d %d)\n",
134 atomic_read(&cf_sk
->sk
.sk_rmem_alloc
),
135 sk_rcvbuf_lowwater(cf_sk
));
136 set_rx_flow_off(cf_sk
);
137 caif_flow_ctrl(sk
, CAIF_MODEMCMD_FLOW_OFF_REQ
);
140 err
= sk_filter(sk
, skb
);
143 if (!sk_rmem_schedule(sk
, skb
, skb
->truesize
) && rx_flow_is_on(cf_sk
)) {
144 set_rx_flow_off(cf_sk
);
145 net_dbg_ratelimited("sending flow OFF due to rmem_schedule\n");
146 caif_flow_ctrl(sk
, CAIF_MODEMCMD_FLOW_OFF_REQ
);
149 skb_set_owner_r(skb
, sk
);
150 /* Cache the SKB length before we tack it onto the receive
151 * queue. Once it is added it no longer belongs to us and
152 * may be freed by other threads of control pulling packets
155 spin_lock_irqsave(&list
->lock
, flags
);
156 if (!sock_flag(sk
, SOCK_DEAD
))
157 __skb_queue_tail(list
, skb
);
158 spin_unlock_irqrestore(&list
->lock
, flags
);
160 if (!sock_flag(sk
, SOCK_DEAD
))
161 sk
->sk_data_ready(sk
);
167 /* Packet Receive Callback function called from CAIF Stack */
168 static int caif_sktrecv_cb(struct cflayer
*layr
, struct cfpkt
*pkt
)
170 struct caifsock
*cf_sk
;
173 cf_sk
= container_of(layr
, struct caifsock
, layer
);
174 skb
= cfpkt_tonative(pkt
);
176 if (unlikely(cf_sk
->sk
.sk_state
!= CAIF_CONNECTED
)) {
180 caif_queue_rcv_skb(&cf_sk
->sk
, skb
);
184 static void cfsk_hold(struct cflayer
*layr
)
186 struct caifsock
*cf_sk
= container_of(layr
, struct caifsock
, layer
);
187 sock_hold(&cf_sk
->sk
);
190 static void cfsk_put(struct cflayer
*layr
)
192 struct caifsock
*cf_sk
= container_of(layr
, struct caifsock
, layer
);
193 sock_put(&cf_sk
->sk
);
196 /* Packet Control Callback function called from CAIF */
197 static void caif_ctrl_cb(struct cflayer
*layr
,
198 enum caif_ctrlcmd flow
,
201 struct caifsock
*cf_sk
= container_of(layr
, struct caifsock
, layer
);
203 case CAIF_CTRLCMD_FLOW_ON_IND
:
204 /* OK from modem to start sending again */
205 set_tx_flow_on(cf_sk
);
206 cf_sk
->sk
.sk_state_change(&cf_sk
->sk
);
209 case CAIF_CTRLCMD_FLOW_OFF_IND
:
210 /* Modem asks us to shut up */
211 set_tx_flow_off(cf_sk
);
212 cf_sk
->sk
.sk_state_change(&cf_sk
->sk
);
215 case CAIF_CTRLCMD_INIT_RSP
:
216 /* We're now connected */
217 caif_client_register_refcnt(&cf_sk
->layer
,
218 cfsk_hold
, cfsk_put
);
219 cf_sk
->sk
.sk_state
= CAIF_CONNECTED
;
220 set_tx_flow_on(cf_sk
);
221 cf_sk
->sk
.sk_shutdown
= 0;
222 cf_sk
->sk
.sk_state_change(&cf_sk
->sk
);
225 case CAIF_CTRLCMD_DEINIT_RSP
:
226 /* We're now disconnected */
227 cf_sk
->sk
.sk_state
= CAIF_DISCONNECTED
;
228 cf_sk
->sk
.sk_state_change(&cf_sk
->sk
);
231 case CAIF_CTRLCMD_INIT_FAIL_RSP
:
232 /* Connect request failed */
233 cf_sk
->sk
.sk_err
= ECONNREFUSED
;
234 cf_sk
->sk
.sk_state
= CAIF_DISCONNECTED
;
235 cf_sk
->sk
.sk_shutdown
= SHUTDOWN_MASK
;
237 * Socket "standards" seems to require POLLOUT to
238 * be set at connect failure.
240 set_tx_flow_on(cf_sk
);
241 cf_sk
->sk
.sk_state_change(&cf_sk
->sk
);
244 case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND
:
245 /* Modem has closed this connection, or device is down. */
246 cf_sk
->sk
.sk_shutdown
= SHUTDOWN_MASK
;
247 cf_sk
->sk
.sk_err
= ECONNRESET
;
248 set_rx_flow_on(cf_sk
);
249 cf_sk
->sk
.sk_error_report(&cf_sk
->sk
);
253 pr_debug("Unexpected flow command %d\n", flow
);
257 static void caif_check_flow_release(struct sock
*sk
)
259 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
261 if (rx_flow_is_on(cf_sk
))
264 if (atomic_read(&sk
->sk_rmem_alloc
) <= sk_rcvbuf_lowwater(cf_sk
)) {
265 set_rx_flow_on(cf_sk
);
266 caif_flow_ctrl(sk
, CAIF_MODEMCMD_FLOW_ON_REQ
);
271 * Copied from unix_dgram_recvmsg, but removed credit checks,
272 * changed locking, address handling and added MSG_TRUNC.
274 static int caif_seqpkt_recvmsg(struct socket
*sock
, struct msghdr
*m
,
275 size_t len
, int flags
)
278 struct sock
*sk
= sock
->sk
;
287 skb
= skb_recv_datagram(sk
, flags
, 0 , &ret
);
292 m
->msg_flags
|= MSG_TRUNC
;
296 ret
= skb_copy_datagram_msg(skb
, 0, m
, copylen
);
300 ret
= (flags
& MSG_TRUNC
) ? skb
->len
: copylen
;
302 skb_free_datagram(sk
, skb
);
303 caif_check_flow_release(sk
);
311 /* Copied from unix_stream_wait_data, identical except for lock call. */
312 static long caif_stream_data_wait(struct sock
*sk
, long timeo
)
318 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
320 if (!skb_queue_empty(&sk
->sk_receive_queue
) ||
322 sk
->sk_state
!= CAIF_CONNECTED
||
323 sock_flag(sk
, SOCK_DEAD
) ||
324 (sk
->sk_shutdown
& RCV_SHUTDOWN
) ||
325 signal_pending(current
) ||
329 set_bit(SOCK_ASYNC_WAITDATA
, &sk
->sk_socket
->flags
);
331 timeo
= schedule_timeout(timeo
);
334 if (sock_flag(sk
, SOCK_DEAD
))
337 clear_bit(SOCK_ASYNC_WAITDATA
, &sk
->sk_socket
->flags
);
340 finish_wait(sk_sleep(sk
), &wait
);
347 * Copied from unix_stream_recvmsg, but removed credit checks,
348 * changed locking calls, changed address handling.
350 static int caif_stream_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
351 size_t size
, int flags
)
353 struct sock
*sk
= sock
->sk
;
364 * Lock the socket to prevent queue disordering
365 * while sleeps in memcpy_tomsg
368 if (sk
->sk_state
== CAIF_CONNECTING
)
372 target
= sock_rcvlowat(sk
, flags
&MSG_WAITALL
, size
);
373 timeo
= sock_rcvtimeo(sk
, flags
&MSG_DONTWAIT
);
380 if (sock_flag(sk
, SOCK_DEAD
)) {
384 skb
= skb_dequeue(&sk
->sk_receive_queue
);
385 caif_check_flow_release(sk
);
388 if (copied
>= target
)
391 * POSIX 1003.1g mandates this order.
393 err
= sock_error(sk
);
397 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
401 if (sk
->sk_state
!= CAIF_CONNECTED
)
403 if (sock_flag(sk
, SOCK_DEAD
))
412 caif_read_unlock(sk
);
414 timeo
= caif_stream_data_wait(sk
, timeo
);
416 if (signal_pending(current
)) {
417 err
= sock_intr_errno(timeo
);
427 chunk
= min_t(unsigned int, skb
->len
, size
);
428 if (memcpy_to_msg(msg
, skb
->data
, chunk
)) {
429 skb_queue_head(&sk
->sk_receive_queue
, skb
);
437 /* Mark read part of skb as used */
438 if (!(flags
& MSG_PEEK
)) {
439 skb_pull(skb
, chunk
);
441 /* put the skb back if we didn't use it up. */
443 skb_queue_head(&sk
->sk_receive_queue
, skb
);
450 * It is questionable, see note in unix_dgram_recvmsg.
452 /* put message back and return */
453 skb_queue_head(&sk
->sk_receive_queue
, skb
);
457 caif_read_unlock(sk
);
460 return copied
? : err
;
464 * Copied from sock.c:sock_wait_for_wmem, but change to wait for
465 * CAIF flow-on and sock_writable.
467 static long caif_wait_for_flow_on(struct caifsock
*cf_sk
,
468 int wait_writeable
, long timeo
, int *err
)
470 struct sock
*sk
= &cf_sk
->sk
;
474 if (tx_flow_is_on(cf_sk
) &&
475 (!wait_writeable
|| sock_writeable(&cf_sk
->sk
)))
481 if (signal_pending(current
))
483 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
485 if (sk
->sk_shutdown
& SHUTDOWN_MASK
)
491 if (cf_sk
->sk
.sk_state
!= CAIF_CONNECTED
)
493 timeo
= schedule_timeout(timeo
);
495 finish_wait(sk_sleep(sk
), &wait
);
500 * Transmit a SKB. The device may temporarily request re-transmission
501 * by returning EAGAIN.
503 static int transmit_skb(struct sk_buff
*skb
, struct caifsock
*cf_sk
,
504 int noblock
, long timeo
)
508 pkt
= cfpkt_fromnative(CAIF_DIR_OUT
, skb
);
509 memset(skb
->cb
, 0, sizeof(struct caif_payload_info
));
510 cfpkt_set_prio(pkt
, cf_sk
->sk
.sk_priority
);
512 if (cf_sk
->layer
.dn
== NULL
) {
517 return cf_sk
->layer
.dn
->transmit(cf_sk
->layer
.dn
, pkt
);
520 /* Copied from af_unix:unix_dgram_sendmsg, and adapted to CAIF */
521 static int caif_seqpkt_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
524 struct sock
*sk
= sock
->sk
;
525 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
528 struct sk_buff
*skb
= NULL
;
532 ret
= sock_error(sk
);
537 if (msg
->msg_flags
&MSG_OOB
)
541 if (msg
->msg_namelen
)
545 if (unlikely(msg
->msg_iter
.iov
->iov_base
== NULL
))
547 noblock
= msg
->msg_flags
& MSG_DONTWAIT
;
549 timeo
= sock_sndtimeo(sk
, noblock
);
550 timeo
= caif_wait_for_flow_on(container_of(sk
, struct caifsock
, sk
),
556 if (cf_sk
->sk
.sk_state
!= CAIF_CONNECTED
||
557 sock_flag(sk
, SOCK_DEAD
) ||
558 (sk
->sk_shutdown
& RCV_SHUTDOWN
))
561 /* Error if trying to write more than maximum frame size. */
563 if (len
> cf_sk
->maxframe
&& cf_sk
->sk
.sk_protocol
!= CAIFPROTO_RFM
)
566 buffer_size
= len
+ cf_sk
->headroom
+ cf_sk
->tailroom
;
569 skb
= sock_alloc_send_skb(sk
, buffer_size
, noblock
, &ret
);
571 if (!skb
|| skb_tailroom(skb
) < buffer_size
)
574 skb_reserve(skb
, cf_sk
->headroom
);
576 ret
= memcpy_from_msg(skb_put(skb
, len
), msg
, len
);
580 ret
= transmit_skb(skb
, cf_sk
, noblock
, timeo
);
582 /* skb is already freed */
592 * Copied from unix_stream_sendmsg and adapted to CAIF:
593 * Changed removed permission handling and added waiting for flow on
594 * and other minor adaptations.
596 static int caif_stream_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
599 struct sock
*sk
= sock
->sk
;
600 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
607 if (unlikely(msg
->msg_flags
&MSG_OOB
))
610 if (unlikely(msg
->msg_namelen
))
613 timeo
= sock_sndtimeo(sk
, msg
->msg_flags
& MSG_DONTWAIT
);
614 timeo
= caif_wait_for_flow_on(cf_sk
, 1, timeo
, &err
);
616 if (unlikely(sk
->sk_shutdown
& SEND_SHUTDOWN
))
623 if (size
> cf_sk
->maxframe
)
624 size
= cf_sk
->maxframe
;
626 /* If size is more than half of sndbuf, chop up message */
627 if (size
> ((sk
->sk_sndbuf
>> 1) - 64))
628 size
= (sk
->sk_sndbuf
>> 1) - 64;
630 if (size
> SKB_MAX_ALLOC
)
631 size
= SKB_MAX_ALLOC
;
633 skb
= sock_alloc_send_skb(sk
,
634 size
+ cf_sk
->headroom
+
636 msg
->msg_flags
&MSG_DONTWAIT
,
641 skb_reserve(skb
, cf_sk
->headroom
);
643 * If you pass two values to the sock_alloc_send_skb
644 * it tries to grab the large buffer with GFP_NOFS
645 * (which can fail easily), and if it fails grab the
646 * fallback size buffer which is under a page and will
649 size
= min_t(int, size
, skb_tailroom(skb
));
651 err
= memcpy_from_msg(skb_put(skb
, size
), msg
, size
);
656 err
= transmit_skb(skb
, cf_sk
,
657 msg
->msg_flags
&MSG_DONTWAIT
, timeo
);
659 /* skb is already freed */
668 if (sent
== 0 && !(msg
->msg_flags
&MSG_NOSIGNAL
))
669 send_sig(SIGPIPE
, current
, 0);
675 static int setsockopt(struct socket
*sock
,
676 int lvl
, int opt
, char __user
*ov
, unsigned int ol
)
678 struct sock
*sk
= sock
->sk
;
679 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
682 if (cf_sk
->sk
.sk_socket
->state
!= SS_UNCONNECTED
)
686 case CAIFSO_LINK_SELECT
:
687 if (ol
< sizeof(int))
691 if (copy_from_user(&linksel
, ov
, sizeof(int)))
693 lock_sock(&(cf_sk
->sk
));
694 cf_sk
->conn_req
.link_selector
= linksel
;
695 release_sock(&cf_sk
->sk
);
698 case CAIFSO_REQ_PARAM
:
701 if (cf_sk
->sk
.sk_protocol
!= CAIFPROTO_UTIL
)
703 lock_sock(&(cf_sk
->sk
));
704 if (ol
> sizeof(cf_sk
->conn_req
.param
.data
) ||
705 copy_from_user(&cf_sk
->conn_req
.param
.data
, ov
, ol
)) {
706 release_sock(&cf_sk
->sk
);
709 cf_sk
->conn_req
.param
.size
= ol
;
710 release_sock(&cf_sk
->sk
);
724 * caif_connect() - Connect a CAIF Socket
725 * Copied and modified af_irda.c:irda_connect().
727 * Note : by consulting "errno", the user space caller may learn the cause
728 * of the failure. Most of them are visible in the function, others may come
729 * from subroutines called and are listed here :
730 * o -EAFNOSUPPORT: bad socket family or type.
731 * o -ESOCKTNOSUPPORT: bad socket type or protocol
732 * o -EINVAL: bad socket address, or CAIF link type
733 * o -ECONNREFUSED: remote end refused the connection.
734 * o -EINPROGRESS: connect request sent but timed out (or non-blocking)
735 * o -EISCONN: already connected.
736 * o -ETIMEDOUT: Connection timed out (send timeout)
737 * o -ENODEV: No link layer to send request
738 * o -ECONNRESET: Received Shutdown indication or lost link layer
739 * o -ENOMEM: Out of memory
742 * o sk_state: holds the CAIF_* protocol state, it's updated by
744 * o sock->state: holds the SS_* socket state and is updated by connect and
747 static int caif_connect(struct socket
*sock
, struct sockaddr
*uaddr
,
748 int addr_len
, int flags
)
750 struct sock
*sk
= sock
->sk
;
751 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
754 int ifindex
, headroom
, tailroom
;
756 struct net_device
*dev
;
761 if (uaddr
->sa_family
!= AF_CAIF
)
764 switch (sock
->state
) {
766 /* Normal case, a fresh connect */
767 caif_assert(sk
->sk_state
== CAIF_DISCONNECTED
);
770 switch (sk
->sk_state
) {
772 sock
->state
= SS_CONNECTED
;
775 case CAIF_DISCONNECTED
:
776 /* Reconnect allowed */
778 case CAIF_CONNECTING
:
780 if (flags
& O_NONBLOCK
)
786 caif_assert(sk
->sk_state
== CAIF_CONNECTED
||
787 sk
->sk_state
== CAIF_DISCONNECTED
);
788 if (sk
->sk_shutdown
& SHUTDOWN_MASK
) {
789 /* Allow re-connect after SHUTDOWN_IND */
790 caif_disconnect_client(sock_net(sk
), &cf_sk
->layer
);
791 caif_free_client(&cf_sk
->layer
);
794 /* No reconnect on a seqpacket socket */
797 case SS_DISCONNECTING
:
799 caif_assert(1); /*Should never happen */
802 sk
->sk_state
= CAIF_DISCONNECTED
;
803 sock
->state
= SS_UNCONNECTED
;
804 sk_stream_kill_queues(&cf_sk
->sk
);
807 if (addr_len
!= sizeof(struct sockaddr_caif
))
810 memcpy(&cf_sk
->conn_req
.sockaddr
, uaddr
,
811 sizeof(struct sockaddr_caif
));
813 /* Move to connecting socket, start sending Connect Requests */
814 sock
->state
= SS_CONNECTING
;
815 sk
->sk_state
= CAIF_CONNECTING
;
817 /* Check priority value comming from socket */
818 /* if priority value is out of range it will be ajusted */
819 if (cf_sk
->sk
.sk_priority
> CAIF_PRIO_MAX
)
820 cf_sk
->conn_req
.priority
= CAIF_PRIO_MAX
;
821 else if (cf_sk
->sk
.sk_priority
< CAIF_PRIO_MIN
)
822 cf_sk
->conn_req
.priority
= CAIF_PRIO_MIN
;
824 cf_sk
->conn_req
.priority
= cf_sk
->sk
.sk_priority
;
826 /*ifindex = id of the interface.*/
827 cf_sk
->conn_req
.ifindex
= cf_sk
->sk
.sk_bound_dev_if
;
829 cf_sk
->layer
.receive
= caif_sktrecv_cb
;
831 err
= caif_connect_client(sock_net(sk
), &cf_sk
->conn_req
,
832 &cf_sk
->layer
, &ifindex
, &headroom
, &tailroom
);
835 cf_sk
->sk
.sk_socket
->state
= SS_UNCONNECTED
;
836 cf_sk
->sk
.sk_state
= CAIF_DISCONNECTED
;
842 dev
= dev_get_by_index_rcu(sock_net(sk
), ifindex
);
847 cf_sk
->headroom
= LL_RESERVED_SPACE_EXTRA(dev
, headroom
);
851 cf_sk
->tailroom
= tailroom
;
852 cf_sk
->maxframe
= mtu
- (headroom
+ tailroom
);
853 if (cf_sk
->maxframe
< 1) {
854 pr_warn("CAIF Interface MTU too small (%d)\n", dev
->mtu
);
862 if (sk
->sk_state
!= CAIF_CONNECTED
&& (flags
& O_NONBLOCK
))
865 timeo
= sock_sndtimeo(sk
, flags
& O_NONBLOCK
);
869 timeo
= wait_event_interruptible_timeout(*sk_sleep(sk
),
870 sk
->sk_state
!= CAIF_CONNECTING
,
874 goto out
; /* -ERESTARTSYS */
877 if (timeo
== 0 && sk
->sk_state
!= CAIF_CONNECTED
)
879 if (sk
->sk_state
!= CAIF_CONNECTED
) {
880 sock
->state
= SS_UNCONNECTED
;
881 err
= sock_error(sk
);
886 sock
->state
= SS_CONNECTED
;
894 * caif_release() - Disconnect a CAIF Socket
895 * Copied and modified af_irda.c:irda_release().
897 static int caif_release(struct socket
*sock
)
899 struct sock
*sk
= sock
->sk
;
900 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
905 set_tx_flow_off(cf_sk
);
908 * Ensure that packets are not queued after this point in time.
909 * caif_queue_rcv_skb checks SOCK_DEAD holding the queue lock,
910 * this ensures no packets when sock is dead.
912 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
913 sock_set_flag(sk
, SOCK_DEAD
);
914 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
917 WARN_ON(IS_ERR(cf_sk
->debugfs_socket_dir
));
918 debugfs_remove_recursive(cf_sk
->debugfs_socket_dir
);
920 lock_sock(&(cf_sk
->sk
));
921 sk
->sk_state
= CAIF_DISCONNECTED
;
922 sk
->sk_shutdown
= SHUTDOWN_MASK
;
924 caif_disconnect_client(sock_net(sk
), &cf_sk
->layer
);
925 cf_sk
->sk
.sk_socket
->state
= SS_DISCONNECTING
;
926 wake_up_interruptible_poll(sk_sleep(sk
), POLLERR
|POLLHUP
);
929 sk_stream_kill_queues(&cf_sk
->sk
);
935 /* Copied from af_unix.c:unix_poll(), added CAIF tx_flow handling */
936 static unsigned int caif_poll(struct file
*file
,
937 struct socket
*sock
, poll_table
*wait
)
939 struct sock
*sk
= sock
->sk
;
941 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
943 sock_poll_wait(file
, sk_sleep(sk
), wait
);
946 /* exceptional events? */
949 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
951 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
955 if (!skb_queue_empty(&sk
->sk_receive_queue
) ||
956 (sk
->sk_shutdown
& RCV_SHUTDOWN
))
957 mask
|= POLLIN
| POLLRDNORM
;
960 * we set writable also when the other side has shut down the
961 * connection. This prevents stuck sockets.
963 if (sock_writeable(sk
) && tx_flow_is_on(cf_sk
))
964 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
969 static const struct proto_ops caif_seqpacket_ops
= {
971 .owner
= THIS_MODULE
,
972 .release
= caif_release
,
973 .bind
= sock_no_bind
,
974 .connect
= caif_connect
,
975 .socketpair
= sock_no_socketpair
,
976 .accept
= sock_no_accept
,
977 .getname
= sock_no_getname
,
979 .ioctl
= sock_no_ioctl
,
980 .listen
= sock_no_listen
,
981 .shutdown
= sock_no_shutdown
,
982 .setsockopt
= setsockopt
,
983 .getsockopt
= sock_no_getsockopt
,
984 .sendmsg
= caif_seqpkt_sendmsg
,
985 .recvmsg
= caif_seqpkt_recvmsg
,
986 .mmap
= sock_no_mmap
,
987 .sendpage
= sock_no_sendpage
,
990 static const struct proto_ops caif_stream_ops
= {
992 .owner
= THIS_MODULE
,
993 .release
= caif_release
,
994 .bind
= sock_no_bind
,
995 .connect
= caif_connect
,
996 .socketpair
= sock_no_socketpair
,
997 .accept
= sock_no_accept
,
998 .getname
= sock_no_getname
,
1000 .ioctl
= sock_no_ioctl
,
1001 .listen
= sock_no_listen
,
1002 .shutdown
= sock_no_shutdown
,
1003 .setsockopt
= setsockopt
,
1004 .getsockopt
= sock_no_getsockopt
,
1005 .sendmsg
= caif_stream_sendmsg
,
1006 .recvmsg
= caif_stream_recvmsg
,
1007 .mmap
= sock_no_mmap
,
1008 .sendpage
= sock_no_sendpage
,
1011 /* This function is called when a socket is finally destroyed. */
1012 static void caif_sock_destructor(struct sock
*sk
)
1014 struct caifsock
*cf_sk
= container_of(sk
, struct caifsock
, sk
);
1015 caif_assert(!atomic_read(&sk
->sk_wmem_alloc
));
1016 caif_assert(sk_unhashed(sk
));
1017 caif_assert(!sk
->sk_socket
);
1018 if (!sock_flag(sk
, SOCK_DEAD
)) {
1019 pr_debug("Attempt to release alive CAIF socket: %p\n", sk
);
1022 sk_stream_kill_queues(&cf_sk
->sk
);
1023 caif_free_client(&cf_sk
->layer
);
1026 static int caif_create(struct net
*net
, struct socket
*sock
, int protocol
,
1029 struct sock
*sk
= NULL
;
1030 struct caifsock
*cf_sk
= NULL
;
1031 static struct proto prot
= {.name
= "PF_CAIF",
1032 .owner
= THIS_MODULE
,
1033 .obj_size
= sizeof(struct caifsock
),
1036 if (!capable(CAP_SYS_ADMIN
) && !capable(CAP_NET_ADMIN
))
1039 * The sock->type specifies the socket type to use.
1040 * The CAIF socket is a packet stream in the sense
1041 * that it is packet based. CAIF trusts the reliability
1042 * of the link, no resending is implemented.
1044 if (sock
->type
== SOCK_SEQPACKET
)
1045 sock
->ops
= &caif_seqpacket_ops
;
1046 else if (sock
->type
== SOCK_STREAM
)
1047 sock
->ops
= &caif_stream_ops
;
1049 return -ESOCKTNOSUPPORT
;
1051 if (protocol
< 0 || protocol
>= CAIFPROTO_MAX
)
1052 return -EPROTONOSUPPORT
;
1054 * Set the socket state to unconnected. The socket state
1055 * is really not used at all in the net/core or socket.c but the
1056 * initialization makes sure that sock->state is not uninitialized.
1058 sk
= sk_alloc(net
, PF_CAIF
, GFP_KERNEL
, &prot
);
1062 cf_sk
= container_of(sk
, struct caifsock
, sk
);
1064 /* Store the protocol */
1065 sk
->sk_protocol
= (unsigned char) protocol
;
1067 /* Initialize default priority for well-known cases */
1070 sk
->sk_priority
= TC_PRIO_CONTROL
;
1073 sk
->sk_priority
= TC_PRIO_INTERACTIVE_BULK
;
1076 sk
->sk_priority
= TC_PRIO_BESTEFFORT
;
1080 * Lock in order to try to stop someone from opening the socket
1083 lock_sock(&(cf_sk
->sk
));
1085 /* Initialize the nozero default sock structure data. */
1086 sock_init_data(sock
, sk
);
1087 sk
->sk_destruct
= caif_sock_destructor
;
1089 mutex_init(&cf_sk
->readlock
); /* single task reading lock */
1090 cf_sk
->layer
.ctrlcmd
= caif_ctrl_cb
;
1091 cf_sk
->sk
.sk_socket
->state
= SS_UNCONNECTED
;
1092 cf_sk
->sk
.sk_state
= CAIF_DISCONNECTED
;
1094 set_tx_flow_off(cf_sk
);
1095 set_rx_flow_on(cf_sk
);
1097 /* Set default options on configuration */
1098 cf_sk
->conn_req
.link_selector
= CAIF_LINK_LOW_LATENCY
;
1099 cf_sk
->conn_req
.protocol
= protocol
;
1100 release_sock(&cf_sk
->sk
);
1105 static struct net_proto_family caif_family_ops
= {
1107 .create
= caif_create
,
1108 .owner
= THIS_MODULE
,
1111 static int __init
caif_sktinit_module(void)
1113 int err
= sock_register(&caif_family_ops
);
1119 static void __exit
caif_sktexit_module(void)
1121 sock_unregister(PF_CAIF
);
1123 module_init(caif_sktinit_module
);
1124 module_exit(caif_sktexit_module
);