Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) ST-Ericsson AB 2010 |
| 3 | * Author: Sjur Brendeland sjur.brandeland@stericsson.com |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 4 | * License terms: GNU General Public License (GPL) version 2 |
| 5 | */ |
| 6 | |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 7 | #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__ |
| 8 | |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 9 | #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 Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 20 | #include <linux/debugfs.h> |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 21 | #include <linux/caif/caif_socket.h> |
sjur.brandeland@stericsson.com | 54e90fb | 2011-05-22 11:18:51 +0000 | [diff] [blame] | 22 | #include <linux/atomic.h> |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 23 | #include <net/sock.h> |
| 24 | #include <net/tcp_states.h> |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 25 | #include <net/caif/caif_layer.h> |
| 26 | #include <net/caif/caif_dev.h> |
| 27 | #include <net/caif/cfpkt.h> |
| 28 | |
| 29 | MODULE_LICENSE("GPL"); |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 30 | MODULE_ALIAS_NETPROTO(AF_CAIF); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 31 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 32 | /* |
| 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 | */ |
| 37 | enum caif_states { |
| 38 | CAIF_CONNECTED = TCP_ESTABLISHED, |
| 39 | CAIF_CONNECTING = TCP_SYN_SENT, |
| 40 | CAIF_DISCONNECTED = TCP_CLOSE |
| 41 | }; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 42 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 43 | #define TX_FLOW_ON_BIT 1 |
| 44 | #define RX_FLOW_ON_BIT 2 |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 45 | |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 46 | static struct dentry *debugfsdir; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 47 | |
| 48 | #ifdef CONFIG_DEBUG_FS |
| 49 | struct debug_fs_counter { |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 50 | atomic_t caif_nr_socks; |
sjur.brandeland@stericsson.com | 33b2f55 | 2011-05-13 02:44:07 +0000 | [diff] [blame] | 51 | atomic_t caif_sock_create; |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 52 | atomic_t num_connect_req; |
| 53 | atomic_t num_connect_resp; |
| 54 | atomic_t num_connect_fail_resp; |
| 55 | atomic_t num_disconnect; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 56 | atomic_t num_remote_shutdown_ind; |
| 57 | atomic_t num_tx_flow_off_ind; |
| 58 | atomic_t num_tx_flow_on_ind; |
| 59 | atomic_t num_rx_flow_off; |
| 60 | atomic_t num_rx_flow_on; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 61 | }; |
Sjur Braendeland | a9a8f10 | 2010-05-21 02:16:11 +0000 | [diff] [blame] | 62 | static struct debug_fs_counter cnt; |
sjur.brandeland@stericsson.com | 33b2f55 | 2011-05-13 02:44:07 +0000 | [diff] [blame] | 63 | #define dbfs_atomic_inc(v) atomic_inc_return(v) |
| 64 | #define dbfs_atomic_dec(v) atomic_dec_return(v) |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 65 | #else |
sjur.brandeland@stericsson.com | 33b2f55 | 2011-05-13 02:44:07 +0000 | [diff] [blame] | 66 | #define dbfs_atomic_inc(v) 0 |
| 67 | #define dbfs_atomic_dec(v) 0 |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 68 | #endif |
| 69 | |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 70 | struct caifsock { |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 71 | struct sock sk; /* must be first member */ |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 72 | struct cflayer layer; |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 73 | char name[CAIF_LAYER_NAME_SZ]; /* Used for debugging */ |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 74 | u32 flow_state; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 75 | struct caif_connect_request conn_req; |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 76 | struct mutex readlock; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 77 | struct dentry *debugfs_socket_dir; |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 78 | int headroom, tailroom, maxframe; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 81 | static int rx_flow_is_on(struct caifsock *cf_sk) |
| 82 | { |
| 83 | return test_bit(RX_FLOW_ON_BIT, |
| 84 | (void *) &cf_sk->flow_state); |
| 85 | } |
| 86 | |
| 87 | static int tx_flow_is_on(struct caifsock *cf_sk) |
| 88 | { |
| 89 | return test_bit(TX_FLOW_ON_BIT, |
| 90 | (void *) &cf_sk->flow_state); |
| 91 | } |
| 92 | |
| 93 | static void set_rx_flow_off(struct caifsock *cf_sk) |
| 94 | { |
| 95 | clear_bit(RX_FLOW_ON_BIT, |
| 96 | (void *) &cf_sk->flow_state); |
| 97 | } |
| 98 | |
| 99 | static void set_rx_flow_on(struct caifsock *cf_sk) |
| 100 | { |
| 101 | set_bit(RX_FLOW_ON_BIT, |
| 102 | (void *) &cf_sk->flow_state); |
| 103 | } |
| 104 | |
| 105 | static void set_tx_flow_off(struct caifsock *cf_sk) |
| 106 | { |
| 107 | clear_bit(TX_FLOW_ON_BIT, |
| 108 | (void *) &cf_sk->flow_state); |
| 109 | } |
| 110 | |
| 111 | static void set_tx_flow_on(struct caifsock *cf_sk) |
| 112 | { |
| 113 | set_bit(TX_FLOW_ON_BIT, |
| 114 | (void *) &cf_sk->flow_state); |
| 115 | } |
| 116 | |
| 117 | static void caif_read_lock(struct sock *sk) |
| 118 | { |
| 119 | struct caifsock *cf_sk; |
| 120 | cf_sk = container_of(sk, struct caifsock, sk); |
| 121 | mutex_lock(&cf_sk->readlock); |
| 122 | } |
| 123 | |
| 124 | static void caif_read_unlock(struct sock *sk) |
| 125 | { |
| 126 | struct caifsock *cf_sk; |
| 127 | cf_sk = container_of(sk, struct caifsock, sk); |
| 128 | mutex_unlock(&cf_sk->readlock); |
| 129 | } |
| 130 | |
Sjur Braendeland | a9a8f10 | 2010-05-21 02:16:11 +0000 | [diff] [blame] | 131 | static int sk_rcvbuf_lowwater(struct caifsock *cf_sk) |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 132 | { |
| 133 | /* A quarter of full buffer is used a low water mark */ |
| 134 | return cf_sk->sk.sk_rcvbuf / 4; |
| 135 | } |
| 136 | |
Sjur Braendeland | a9a8f10 | 2010-05-21 02:16:11 +0000 | [diff] [blame] | 137 | static void caif_flow_ctrl(struct sock *sk, int mode) |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 138 | { |
| 139 | struct caifsock *cf_sk; |
| 140 | cf_sk = container_of(sk, struct caifsock, sk); |
Sjur Braendeland | ca6a09f | 2010-05-21 02:16:10 +0000 | [diff] [blame] | 141 | if (cf_sk->layer.dn && cf_sk->layer.dn->modemcmd) |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 142 | cf_sk->layer.dn->modemcmd(cf_sk->layer.dn, mode); |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Copied from sock.c:sock_queue_rcv_skb(), but changed so packets are |
| 147 | * not dropped, but CAIF is sending flow off instead. |
| 148 | */ |
Sjur Braendeland | a9a8f10 | 2010-05-21 02:16:11 +0000 | [diff] [blame] | 149 | static int caif_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 150 | { |
| 151 | int err; |
| 152 | int skb_len; |
| 153 | unsigned long flags; |
| 154 | struct sk_buff_head *list = &sk->sk_receive_queue; |
| 155 | struct caifsock *cf_sk = container_of(sk, struct caifsock, sk); |
| 156 | |
| 157 | if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= |
| 158 | (unsigned)sk->sk_rcvbuf && rx_flow_is_on(cf_sk)) { |
sjur.brandeland@stericsson.com | 33b2f55 | 2011-05-13 02:44:07 +0000 | [diff] [blame] | 159 | if (net_ratelimit()) |
| 160 | pr_debug("sending flow OFF (queue len = %d %d)\n", |
| 161 | atomic_read(&cf_sk->sk.sk_rmem_alloc), |
| 162 | sk_rcvbuf_lowwater(cf_sk)); |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 163 | set_rx_flow_off(cf_sk); |
Sjur Braendeland | ca6a09f | 2010-05-21 02:16:10 +0000 | [diff] [blame] | 164 | dbfs_atomic_inc(&cnt.num_rx_flow_off); |
| 165 | caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_OFF_REQ); |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | err = sk_filter(sk, skb); |
| 169 | if (err) |
| 170 | return err; |
| 171 | if (!sk_rmem_schedule(sk, skb->truesize) && rx_flow_is_on(cf_sk)) { |
| 172 | set_rx_flow_off(cf_sk); |
sjur.brandeland@stericsson.com | 33b2f55 | 2011-05-13 02:44:07 +0000 | [diff] [blame] | 173 | if (net_ratelimit()) |
| 174 | pr_debug("sending flow OFF due to rmem_schedule\n"); |
Sjur Braendeland | ca6a09f | 2010-05-21 02:16:10 +0000 | [diff] [blame] | 175 | dbfs_atomic_inc(&cnt.num_rx_flow_off); |
| 176 | caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_OFF_REQ); |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 177 | } |
| 178 | skb->dev = NULL; |
| 179 | skb_set_owner_r(skb, sk); |
| 180 | /* Cache the SKB length before we tack it onto the receive |
| 181 | * queue. Once it is added it no longer belongs to us and |
| 182 | * may be freed by other threads of control pulling packets |
| 183 | * from the queue. |
| 184 | */ |
| 185 | skb_len = skb->len; |
| 186 | spin_lock_irqsave(&list->lock, flags); |
| 187 | if (!sock_flag(sk, SOCK_DEAD)) |
| 188 | __skb_queue_tail(list, skb); |
| 189 | spin_unlock_irqrestore(&list->lock, flags); |
| 190 | |
| 191 | if (!sock_flag(sk, SOCK_DEAD)) |
| 192 | sk->sk_data_ready(sk, skb_len); |
| 193 | else |
| 194 | kfree_skb(skb); |
| 195 | return 0; |
| 196 | } |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 197 | |
| 198 | /* Packet Receive Callback function called from CAIF Stack */ |
| 199 | static int caif_sktrecv_cb(struct cflayer *layr, struct cfpkt *pkt) |
| 200 | { |
| 201 | struct caifsock *cf_sk; |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 202 | struct sk_buff *skb; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 203 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 204 | cf_sk = container_of(layr, struct caifsock, layer); |
| 205 | skb = cfpkt_tonative(pkt); |
| 206 | |
| 207 | if (unlikely(cf_sk->sk.sk_state != CAIF_CONNECTED)) { |
sjur.brandeland@stericsson.com | 3f874ad | 2011-05-13 02:44:08 +0000 | [diff] [blame] | 208 | kfree_skb(skb); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 209 | return 0; |
| 210 | } |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 211 | caif_queue_rcv_skb(&cf_sk->sk, skb); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 212 | return 0; |
| 213 | } |
| 214 | |
sjur.brandeland@stericsson.com | b3ccfbe | 2011-05-13 02:44:04 +0000 | [diff] [blame] | 215 | static void cfsk_hold(struct cflayer *layr) |
| 216 | { |
| 217 | struct caifsock *cf_sk = container_of(layr, struct caifsock, layer); |
| 218 | sock_hold(&cf_sk->sk); |
| 219 | } |
| 220 | |
| 221 | static void cfsk_put(struct cflayer *layr) |
| 222 | { |
| 223 | struct caifsock *cf_sk = container_of(layr, struct caifsock, layer); |
| 224 | sock_put(&cf_sk->sk); |
| 225 | } |
| 226 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 227 | /* Packet Control Callback function called from CAIF */ |
| 228 | static void caif_ctrl_cb(struct cflayer *layr, |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 229 | enum caif_ctrlcmd flow, |
| 230 | int phyid) |
| 231 | { |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 232 | struct caifsock *cf_sk = container_of(layr, struct caifsock, layer); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 233 | switch (flow) { |
| 234 | case CAIF_CTRLCMD_FLOW_ON_IND: |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 235 | /* OK from modem to start sending again */ |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 236 | dbfs_atomic_inc(&cnt.num_tx_flow_on_ind); |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 237 | set_tx_flow_on(cf_sk); |
| 238 | cf_sk->sk.sk_state_change(&cf_sk->sk); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 239 | break; |
| 240 | |
| 241 | case CAIF_CTRLCMD_FLOW_OFF_IND: |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 242 | /* Modem asks us to shut up */ |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 243 | dbfs_atomic_inc(&cnt.num_tx_flow_off_ind); |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 244 | set_tx_flow_off(cf_sk); |
| 245 | cf_sk->sk.sk_state_change(&cf_sk->sk); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 246 | break; |
| 247 | |
| 248 | case CAIF_CTRLCMD_INIT_RSP: |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 249 | /* We're now connected */ |
sjur.brandeland@stericsson.com | b3ccfbe | 2011-05-13 02:44:04 +0000 | [diff] [blame] | 250 | caif_client_register_refcnt(&cf_sk->layer, |
| 251 | cfsk_hold, cfsk_put); |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 252 | dbfs_atomic_inc(&cnt.num_connect_resp); |
| 253 | cf_sk->sk.sk_state = CAIF_CONNECTED; |
| 254 | set_tx_flow_on(cf_sk); |
| 255 | cf_sk->sk.sk_state_change(&cf_sk->sk); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 256 | break; |
| 257 | |
| 258 | case CAIF_CTRLCMD_DEINIT_RSP: |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 259 | /* We're now disconnected */ |
| 260 | cf_sk->sk.sk_state = CAIF_DISCONNECTED; |
| 261 | cf_sk->sk.sk_state_change(&cf_sk->sk); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 262 | break; |
| 263 | |
| 264 | case CAIF_CTRLCMD_INIT_FAIL_RSP: |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 265 | /* Connect request failed */ |
| 266 | dbfs_atomic_inc(&cnt.num_connect_fail_resp); |
| 267 | cf_sk->sk.sk_err = ECONNREFUSED; |
| 268 | cf_sk->sk.sk_state = CAIF_DISCONNECTED; |
| 269 | cf_sk->sk.sk_shutdown = SHUTDOWN_MASK; |
| 270 | /* |
| 271 | * Socket "standards" seems to require POLLOUT to |
| 272 | * be set at connect failure. |
| 273 | */ |
| 274 | set_tx_flow_on(cf_sk); |
| 275 | cf_sk->sk.sk_state_change(&cf_sk->sk); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 276 | break; |
| 277 | |
| 278 | case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND: |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 279 | /* Modem has closed this connection, or device is down. */ |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 280 | dbfs_atomic_inc(&cnt.num_remote_shutdown_ind); |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 281 | cf_sk->sk.sk_shutdown = SHUTDOWN_MASK; |
| 282 | cf_sk->sk.sk_err = ECONNRESET; |
| 283 | set_rx_flow_on(cf_sk); |
| 284 | cf_sk->sk.sk_error_report(&cf_sk->sk); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 285 | break; |
| 286 | |
| 287 | default: |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 288 | pr_debug("Unexpected flow command %d\n", flow); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 292 | static void caif_check_flow_release(struct sock *sk) |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 293 | { |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 294 | struct caifsock *cf_sk = container_of(sk, struct caifsock, sk); |
| 295 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 296 | if (rx_flow_is_on(cf_sk)) |
| 297 | return; |
| 298 | |
| 299 | if (atomic_read(&sk->sk_rmem_alloc) <= sk_rcvbuf_lowwater(cf_sk)) { |
| 300 | dbfs_atomic_inc(&cnt.num_rx_flow_on); |
| 301 | set_rx_flow_on(cf_sk); |
Sjur Braendeland | ca6a09f | 2010-05-21 02:16:10 +0000 | [diff] [blame] | 302 | caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_ON_REQ); |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 303 | } |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Sjur Braendeland | dcda138 | 2010-05-21 02:16:12 +0000 | [diff] [blame] | 306 | /* |
| 307 | * Copied from unix_dgram_recvmsg, but removed credit checks, |
| 308 | * changed locking, address handling and added MSG_TRUNC. |
| 309 | */ |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 310 | static int caif_seqpkt_recvmsg(struct kiocb *iocb, struct socket *sock, |
Sjur Braendeland | dcda138 | 2010-05-21 02:16:12 +0000 | [diff] [blame] | 311 | struct msghdr *m, size_t len, int flags) |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 312 | |
| 313 | { |
| 314 | struct sock *sk = sock->sk; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 315 | struct sk_buff *skb; |
Sjur Braendeland | dcda138 | 2010-05-21 02:16:12 +0000 | [diff] [blame] | 316 | int ret; |
| 317 | int copylen; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 318 | |
Sjur Braendeland | dcda138 | 2010-05-21 02:16:12 +0000 | [diff] [blame] | 319 | ret = -EOPNOTSUPP; |
| 320 | if (m->msg_flags&MSG_OOB) |
| 321 | goto read_error; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 322 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 323 | skb = skb_recv_datagram(sk, flags, 0 , &ret); |
| 324 | if (!skb) |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 325 | goto read_error; |
Sjur Braendeland | dcda138 | 2010-05-21 02:16:12 +0000 | [diff] [blame] | 326 | copylen = skb->len; |
| 327 | if (len < copylen) { |
| 328 | m->msg_flags |= MSG_TRUNC; |
| 329 | copylen = len; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Sjur Braendeland | dcda138 | 2010-05-21 02:16:12 +0000 | [diff] [blame] | 332 | ret = skb_copy_datagram_iovec(skb, 0, m->msg_iov, copylen); |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 333 | if (ret) |
Sjur Braendeland | dcda138 | 2010-05-21 02:16:12 +0000 | [diff] [blame] | 334 | goto out_free; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 335 | |
Sjur Braendeland | dcda138 | 2010-05-21 02:16:12 +0000 | [diff] [blame] | 336 | ret = (flags & MSG_TRUNC) ? skb->len : copylen; |
| 337 | out_free: |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 338 | skb_free_datagram(sk, skb); |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 339 | caif_check_flow_release(sk); |
Sjur Braendeland | dcda138 | 2010-05-21 02:16:12 +0000 | [diff] [blame] | 340 | return ret; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 341 | |
| 342 | read_error: |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 343 | return ret; |
| 344 | } |
| 345 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 346 | |
| 347 | /* Copied from unix_stream_wait_data, identical except for lock call. */ |
| 348 | static long caif_stream_data_wait(struct sock *sk, long timeo) |
| 349 | { |
| 350 | DEFINE_WAIT(wait); |
| 351 | lock_sock(sk); |
| 352 | |
| 353 | for (;;) { |
| 354 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
| 355 | |
| 356 | if (!skb_queue_empty(&sk->sk_receive_queue) || |
| 357 | sk->sk_err || |
| 358 | sk->sk_state != CAIF_CONNECTED || |
| 359 | sock_flag(sk, SOCK_DEAD) || |
| 360 | (sk->sk_shutdown & RCV_SHUTDOWN) || |
| 361 | signal_pending(current) || |
| 362 | !timeo) |
| 363 | break; |
| 364 | |
| 365 | set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags); |
| 366 | release_sock(sk); |
| 367 | timeo = schedule_timeout(timeo); |
| 368 | lock_sock(sk); |
| 369 | clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags); |
| 370 | } |
| 371 | |
| 372 | finish_wait(sk_sleep(sk), &wait); |
| 373 | release_sock(sk); |
| 374 | return timeo; |
| 375 | } |
| 376 | |
| 377 | |
| 378 | /* |
| 379 | * Copied from unix_stream_recvmsg, but removed credit checks, |
| 380 | * changed locking calls, changed address handling. |
| 381 | */ |
| 382 | static int caif_stream_recvmsg(struct kiocb *iocb, struct socket *sock, |
| 383 | struct msghdr *msg, size_t size, |
| 384 | int flags) |
| 385 | { |
| 386 | struct sock *sk = sock->sk; |
| 387 | int copied = 0; |
| 388 | int target; |
| 389 | int err = 0; |
| 390 | long timeo; |
| 391 | |
| 392 | err = -EOPNOTSUPP; |
| 393 | if (flags&MSG_OOB) |
| 394 | goto out; |
| 395 | |
| 396 | msg->msg_namelen = 0; |
| 397 | |
| 398 | /* |
| 399 | * Lock the socket to prevent queue disordering |
| 400 | * while sleeps in memcpy_tomsg |
| 401 | */ |
| 402 | err = -EAGAIN; |
| 403 | if (sk->sk_state == CAIF_CONNECTING) |
| 404 | goto out; |
| 405 | |
| 406 | caif_read_lock(sk); |
| 407 | target = sock_rcvlowat(sk, flags&MSG_WAITALL, size); |
| 408 | timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT); |
| 409 | |
| 410 | do { |
| 411 | int chunk; |
| 412 | struct sk_buff *skb; |
| 413 | |
| 414 | lock_sock(sk); |
| 415 | skb = skb_dequeue(&sk->sk_receive_queue); |
| 416 | caif_check_flow_release(sk); |
| 417 | |
| 418 | if (skb == NULL) { |
| 419 | if (copied >= target) |
| 420 | goto unlock; |
| 421 | /* |
| 422 | * POSIX 1003.1g mandates this order. |
| 423 | */ |
| 424 | err = sock_error(sk); |
| 425 | if (err) |
| 426 | goto unlock; |
| 427 | err = -ECONNRESET; |
| 428 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 429 | goto unlock; |
| 430 | |
| 431 | err = -EPIPE; |
| 432 | if (sk->sk_state != CAIF_CONNECTED) |
| 433 | goto unlock; |
| 434 | if (sock_flag(sk, SOCK_DEAD)) |
| 435 | goto unlock; |
| 436 | |
| 437 | release_sock(sk); |
| 438 | |
| 439 | err = -EAGAIN; |
| 440 | if (!timeo) |
| 441 | break; |
| 442 | |
| 443 | caif_read_unlock(sk); |
| 444 | |
| 445 | timeo = caif_stream_data_wait(sk, timeo); |
| 446 | |
| 447 | if (signal_pending(current)) { |
| 448 | err = sock_intr_errno(timeo); |
| 449 | goto out; |
| 450 | } |
| 451 | caif_read_lock(sk); |
| 452 | continue; |
| 453 | unlock: |
| 454 | release_sock(sk); |
| 455 | break; |
| 456 | } |
| 457 | release_sock(sk); |
| 458 | chunk = min_t(unsigned int, skb->len, size); |
| 459 | if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) { |
| 460 | skb_queue_head(&sk->sk_receive_queue, skb); |
| 461 | if (copied == 0) |
| 462 | copied = -EFAULT; |
| 463 | break; |
| 464 | } |
| 465 | copied += chunk; |
| 466 | size -= chunk; |
| 467 | |
| 468 | /* Mark read part of skb as used */ |
| 469 | if (!(flags & MSG_PEEK)) { |
| 470 | skb_pull(skb, chunk); |
| 471 | |
| 472 | /* put the skb back if we didn't use it up. */ |
| 473 | if (skb->len) { |
| 474 | skb_queue_head(&sk->sk_receive_queue, skb); |
| 475 | break; |
| 476 | } |
| 477 | kfree_skb(skb); |
| 478 | |
| 479 | } else { |
| 480 | /* |
| 481 | * It is questionable, see note in unix_dgram_recvmsg. |
| 482 | */ |
| 483 | /* put message back and return */ |
| 484 | skb_queue_head(&sk->sk_receive_queue, skb); |
| 485 | break; |
| 486 | } |
| 487 | } while (size); |
| 488 | caif_read_unlock(sk); |
| 489 | |
| 490 | out: |
| 491 | return copied ? : err; |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | * Copied from sock.c:sock_wait_for_wmem, but change to wait for |
| 496 | * CAIF flow-on and sock_writable. |
| 497 | */ |
| 498 | static long caif_wait_for_flow_on(struct caifsock *cf_sk, |
| 499 | int wait_writeable, long timeo, int *err) |
| 500 | { |
| 501 | struct sock *sk = &cf_sk->sk; |
| 502 | DEFINE_WAIT(wait); |
| 503 | for (;;) { |
| 504 | *err = 0; |
| 505 | if (tx_flow_is_on(cf_sk) && |
| 506 | (!wait_writeable || sock_writeable(&cf_sk->sk))) |
| 507 | break; |
| 508 | *err = -ETIMEDOUT; |
| 509 | if (!timeo) |
| 510 | break; |
| 511 | *err = -ERESTARTSYS; |
| 512 | if (signal_pending(current)) |
| 513 | break; |
| 514 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
| 515 | *err = -ECONNRESET; |
| 516 | if (sk->sk_shutdown & SHUTDOWN_MASK) |
| 517 | break; |
| 518 | *err = -sk->sk_err; |
| 519 | if (sk->sk_err) |
| 520 | break; |
| 521 | *err = -EPIPE; |
| 522 | if (cf_sk->sk.sk_state != CAIF_CONNECTED) |
| 523 | break; |
| 524 | timeo = schedule_timeout(timeo); |
| 525 | } |
| 526 | finish_wait(sk_sleep(sk), &wait); |
| 527 | return timeo; |
| 528 | } |
| 529 | |
| 530 | /* |
| 531 | * Transmit a SKB. The device may temporarily request re-transmission |
| 532 | * by returning EAGAIN. |
| 533 | */ |
| 534 | static int transmit_skb(struct sk_buff *skb, struct caifsock *cf_sk, |
| 535 | int noblock, long timeo) |
| 536 | { |
| 537 | struct cfpkt *pkt; |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 538 | |
| 539 | pkt = cfpkt_fromnative(CAIF_DIR_OUT, skb); |
sjur.brandeland@stericsson.com | 3f874ad | 2011-05-13 02:44:08 +0000 | [diff] [blame] | 540 | memset(skb->cb, 0, sizeof(struct caif_payload_info)); |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 541 | |
Dmitry Tarnyagin | ba76057 | 2012-02-02 01:21:03 +0000 | [diff] [blame] | 542 | if (cf_sk->layer.dn == NULL) { |
| 543 | kfree_skb(skb); |
Sjur Brændeland | 4dd820c | 2011-04-11 10:43:51 +0000 | [diff] [blame] | 544 | return -EINVAL; |
Dmitry Tarnyagin | ba76057 | 2012-02-02 01:21:03 +0000 | [diff] [blame] | 545 | } |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 546 | |
Sjur Brændeland | 4dd820c | 2011-04-11 10:43:51 +0000 | [diff] [blame] | 547 | return cf_sk->layer.dn->transmit(cf_sk->layer.dn, pkt); |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | /* Copied from af_unix:unix_dgram_sendmsg, and adapted to CAIF */ |
| 551 | static int caif_seqpkt_sendmsg(struct kiocb *kiocb, struct socket *sock, |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 552 | struct msghdr *msg, size_t len) |
| 553 | { |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 554 | struct sock *sk = sock->sk; |
| 555 | struct caifsock *cf_sk = container_of(sk, struct caifsock, sk); |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 556 | int buffer_size; |
| 557 | int ret = 0; |
| 558 | struct sk_buff *skb = NULL; |
| 559 | int noblock; |
| 560 | long timeo; |
| 561 | caif_assert(cf_sk); |
| 562 | ret = sock_error(sk); |
| 563 | if (ret) |
| 564 | goto err; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 565 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 566 | ret = -EOPNOTSUPP; |
| 567 | if (msg->msg_flags&MSG_OOB) |
| 568 | goto err; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 569 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 570 | ret = -EOPNOTSUPP; |
| 571 | if (msg->msg_namelen) |
| 572 | goto err; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 573 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 574 | ret = -EINVAL; |
| 575 | if (unlikely(msg->msg_iov->iov_base == NULL)) |
| 576 | goto err; |
| 577 | noblock = msg->msg_flags & MSG_DONTWAIT; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 578 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 579 | timeo = sock_sndtimeo(sk, noblock); |
| 580 | timeo = caif_wait_for_flow_on(container_of(sk, struct caifsock, sk), |
| 581 | 1, timeo, &ret); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 582 | |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 583 | if (ret) |
| 584 | goto err; |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 585 | ret = -EPIPE; |
| 586 | if (cf_sk->sk.sk_state != CAIF_CONNECTED || |
| 587 | sock_flag(sk, SOCK_DEAD) || |
| 588 | (sk->sk_shutdown & RCV_SHUTDOWN)) |
| 589 | goto err; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 590 | |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 591 | /* Error if trying to write more than maximum frame size. */ |
| 592 | ret = -EMSGSIZE; |
| 593 | if (len > cf_sk->maxframe && cf_sk->sk.sk_protocol != CAIFPROTO_RFM) |
| 594 | goto err; |
| 595 | |
| 596 | buffer_size = len + cf_sk->headroom + cf_sk->tailroom; |
| 597 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 598 | ret = -ENOMEM; |
| 599 | skb = sock_alloc_send_skb(sk, buffer_size, noblock, &ret); |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 600 | |
| 601 | if (!skb || skb_tailroom(skb) < buffer_size) |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 602 | goto err; |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 603 | |
| 604 | skb_reserve(skb, cf_sk->headroom); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 605 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 606 | ret = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 607 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 608 | if (ret) |
| 609 | goto err; |
| 610 | ret = transmit_skb(skb, cf_sk, noblock, timeo); |
| 611 | if (ret < 0) |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame] | 612 | /* skb is already freed */ |
| 613 | return ret; |
| 614 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 615 | return len; |
| 616 | err: |
| 617 | kfree_skb(skb); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 618 | return ret; |
| 619 | } |
| 620 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 621 | /* |
| 622 | * Copied from unix_stream_sendmsg and adapted to CAIF: |
| 623 | * Changed removed permission handling and added waiting for flow on |
| 624 | * and other minor adaptations. |
| 625 | */ |
| 626 | static int caif_stream_sendmsg(struct kiocb *kiocb, struct socket *sock, |
| 627 | struct msghdr *msg, size_t len) |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 628 | { |
| 629 | struct sock *sk = sock->sk; |
| 630 | struct caifsock *cf_sk = container_of(sk, struct caifsock, sk); |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 631 | int err, size; |
| 632 | struct sk_buff *skb; |
| 633 | int sent = 0; |
| 634 | long timeo; |
| 635 | |
| 636 | err = -EOPNOTSUPP; |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 637 | if (unlikely(msg->msg_flags&MSG_OOB)) |
| 638 | goto out_err; |
| 639 | |
| 640 | if (unlikely(msg->msg_namelen)) |
| 641 | goto out_err; |
| 642 | |
| 643 | timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); |
| 644 | timeo = caif_wait_for_flow_on(cf_sk, 1, timeo, &err); |
| 645 | |
| 646 | if (unlikely(sk->sk_shutdown & SEND_SHUTDOWN)) |
| 647 | goto pipe_err; |
| 648 | |
| 649 | while (sent < len) { |
| 650 | |
| 651 | size = len-sent; |
| 652 | |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 653 | if (size > cf_sk->maxframe) |
| 654 | size = cf_sk->maxframe; |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 655 | |
| 656 | /* If size is more than half of sndbuf, chop up message */ |
| 657 | if (size > ((sk->sk_sndbuf >> 1) - 64)) |
| 658 | size = (sk->sk_sndbuf >> 1) - 64; |
| 659 | |
| 660 | if (size > SKB_MAX_ALLOC) |
| 661 | size = SKB_MAX_ALLOC; |
| 662 | |
| 663 | skb = sock_alloc_send_skb(sk, |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 664 | size + cf_sk->headroom + |
| 665 | cf_sk->tailroom, |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 666 | msg->msg_flags&MSG_DONTWAIT, |
| 667 | &err); |
| 668 | if (skb == NULL) |
| 669 | goto out_err; |
| 670 | |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 671 | skb_reserve(skb, cf_sk->headroom); |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 672 | /* |
| 673 | * If you pass two values to the sock_alloc_send_skb |
| 674 | * it tries to grab the large buffer with GFP_NOFS |
| 675 | * (which can fail easily), and if it fails grab the |
| 676 | * fallback size buffer which is under a page and will |
| 677 | * succeed. [Alan] |
| 678 | */ |
| 679 | size = min_t(int, size, skb_tailroom(skb)); |
| 680 | |
| 681 | err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size); |
| 682 | if (err) { |
| 683 | kfree_skb(skb); |
| 684 | goto out_err; |
| 685 | } |
| 686 | err = transmit_skb(skb, cf_sk, |
| 687 | msg->msg_flags&MSG_DONTWAIT, timeo); |
Dmitry Tarnyagin | ba76057 | 2012-02-02 01:21:03 +0000 | [diff] [blame] | 688 | if (err < 0) |
| 689 | /* skb is already freed */ |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 690 | goto pipe_err; |
Dmitry Tarnyagin | ba76057 | 2012-02-02 01:21:03 +0000 | [diff] [blame] | 691 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 692 | sent += size; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 693 | } |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 694 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 695 | return sent; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 696 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 697 | pipe_err: |
| 698 | if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL)) |
| 699 | send_sig(SIGPIPE, current, 0); |
| 700 | err = -EPIPE; |
| 701 | out_err: |
| 702 | return sent ? : err; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | static int setsockopt(struct socket *sock, |
| 706 | int lvl, int opt, char __user *ov, unsigned int ol) |
| 707 | { |
| 708 | struct sock *sk = sock->sk; |
| 709 | struct caifsock *cf_sk = container_of(sk, struct caifsock, sk); |
André Carvalho de Matos | f2527ec | 2010-11-01 11:52:47 +0000 | [diff] [blame] | 710 | int linksel; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 711 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 712 | if (cf_sk->sk.sk_socket->state != SS_UNCONNECTED) |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 713 | return -ENOPROTOOPT; |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 714 | |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 715 | switch (opt) { |
| 716 | case CAIFSO_LINK_SELECT: |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 717 | if (ol < sizeof(int)) |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 718 | return -EINVAL; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 719 | if (lvl != SOL_CAIF) |
| 720 | goto bad_sol; |
| 721 | if (copy_from_user(&linksel, ov, sizeof(int))) |
| 722 | return -EINVAL; |
| 723 | lock_sock(&(cf_sk->sk)); |
| 724 | cf_sk->conn_req.link_selector = linksel; |
| 725 | release_sock(&cf_sk->sk); |
| 726 | return 0; |
| 727 | |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 728 | case CAIFSO_REQ_PARAM: |
| 729 | if (lvl != SOL_CAIF) |
| 730 | goto bad_sol; |
| 731 | if (cf_sk->sk.sk_protocol != CAIFPROTO_UTIL) |
| 732 | return -ENOPROTOOPT; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 733 | lock_sock(&(cf_sk->sk)); |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 734 | if (ol > sizeof(cf_sk->conn_req.param.data) || |
| 735 | copy_from_user(&cf_sk->conn_req.param.data, ov, ol)) { |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 736 | release_sock(&cf_sk->sk); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 737 | return -EINVAL; |
| 738 | } |
Dan Rosenberg | 91b5c98 | 2011-01-10 16:00:54 -0800 | [diff] [blame] | 739 | cf_sk->conn_req.param.size = ol; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 740 | release_sock(&cf_sk->sk); |
| 741 | return 0; |
| 742 | |
| 743 | default: |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 744 | return -ENOPROTOOPT; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | return 0; |
| 748 | bad_sol: |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 749 | return -ENOPROTOOPT; |
| 750 | |
| 751 | } |
| 752 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 753 | /* |
| 754 | * caif_connect() - Connect a CAIF Socket |
| 755 | * Copied and modified af_irda.c:irda_connect(). |
| 756 | * |
| 757 | * Note : by consulting "errno", the user space caller may learn the cause |
| 758 | * of the failure. Most of them are visible in the function, others may come |
| 759 | * from subroutines called and are listed here : |
| 760 | * o -EAFNOSUPPORT: bad socket family or type. |
| 761 | * o -ESOCKTNOSUPPORT: bad socket type or protocol |
| 762 | * o -EINVAL: bad socket address, or CAIF link type |
| 763 | * o -ECONNREFUSED: remote end refused the connection. |
| 764 | * o -EINPROGRESS: connect request sent but timed out (or non-blocking) |
| 765 | * o -EISCONN: already connected. |
| 766 | * o -ETIMEDOUT: Connection timed out (send timeout) |
| 767 | * o -ENODEV: No link layer to send request |
| 768 | * o -ECONNRESET: Received Shutdown indication or lost link layer |
| 769 | * o -ENOMEM: Out of memory |
| 770 | * |
| 771 | * State Strategy: |
| 772 | * o sk_state: holds the CAIF_* protocol state, it's updated by |
| 773 | * caif_ctrl_cb. |
| 774 | * o sock->state: holds the SS_* socket state and is updated by connect and |
| 775 | * disconnect. |
| 776 | */ |
| 777 | static int caif_connect(struct socket *sock, struct sockaddr *uaddr, |
| 778 | int addr_len, int flags) |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 779 | { |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 780 | struct sock *sk = sock->sk; |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 781 | struct caifsock *cf_sk = container_of(sk, struct caifsock, sk); |
| 782 | long timeo; |
| 783 | int err; |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 784 | int ifindex, headroom, tailroom; |
Eric Dumazet | 7931506 | 2010-10-04 22:42:08 +0000 | [diff] [blame] | 785 | unsigned int mtu; |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 786 | struct net_device *dev; |
| 787 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 788 | lock_sock(sk); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 789 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 790 | err = -EAFNOSUPPORT; |
| 791 | if (uaddr->sa_family != AF_CAIF) |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 792 | goto out; |
| 793 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 794 | switch (sock->state) { |
| 795 | case SS_UNCONNECTED: |
| 796 | /* Normal case, a fresh connect */ |
| 797 | caif_assert(sk->sk_state == CAIF_DISCONNECTED); |
| 798 | break; |
| 799 | case SS_CONNECTING: |
| 800 | switch (sk->sk_state) { |
| 801 | case CAIF_CONNECTED: |
| 802 | sock->state = SS_CONNECTED; |
| 803 | err = -EISCONN; |
| 804 | goto out; |
| 805 | case CAIF_DISCONNECTED: |
| 806 | /* Reconnect allowed */ |
| 807 | break; |
| 808 | case CAIF_CONNECTING: |
| 809 | err = -EALREADY; |
| 810 | if (flags & O_NONBLOCK) |
| 811 | goto out; |
| 812 | goto wait_connect; |
| 813 | } |
| 814 | break; |
| 815 | case SS_CONNECTED: |
| 816 | caif_assert(sk->sk_state == CAIF_CONNECTED || |
| 817 | sk->sk_state == CAIF_DISCONNECTED); |
| 818 | if (sk->sk_shutdown & SHUTDOWN_MASK) { |
| 819 | /* Allow re-connect after SHUTDOWN_IND */ |
sjur.brandeland@stericsson.com | bee925d | 2011-05-13 02:44:05 +0000 | [diff] [blame] | 820 | caif_disconnect_client(sock_net(sk), &cf_sk->layer); |
sjur.brandeland@stericsson.com | 54e90fb | 2011-05-22 11:18:51 +0000 | [diff] [blame] | 821 | caif_free_client(&cf_sk->layer); |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 822 | break; |
| 823 | } |
| 824 | /* No reconnect on a seqpacket socket */ |
| 825 | err = -EISCONN; |
| 826 | goto out; |
| 827 | case SS_DISCONNECTING: |
| 828 | case SS_FREE: |
| 829 | caif_assert(1); /*Should never happen */ |
| 830 | break; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 831 | } |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 832 | sk->sk_state = CAIF_DISCONNECTED; |
| 833 | sock->state = SS_UNCONNECTED; |
| 834 | sk_stream_kill_queues(&cf_sk->sk); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 835 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 836 | err = -EINVAL; |
Dan Carpenter | f5d72af | 2010-05-22 10:43:42 +0000 | [diff] [blame] | 837 | if (addr_len != sizeof(struct sockaddr_caif)) |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 838 | goto out; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 839 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 840 | memcpy(&cf_sk->conn_req.sockaddr, uaddr, |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 841 | sizeof(struct sockaddr_caif)); |
| 842 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 843 | /* Move to connecting socket, start sending Connect Requests */ |
| 844 | sock->state = SS_CONNECTING; |
| 845 | sk->sk_state = CAIF_CONNECTING; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 846 | |
sjur.brandeland@stericsson.com | 33b2f55 | 2011-05-13 02:44:07 +0000 | [diff] [blame] | 847 | /* Check priority value comming from socket */ |
André Carvalho de Matos | f2527ec | 2010-11-01 11:52:47 +0000 | [diff] [blame] | 848 | /* if priority value is out of range it will be ajusted */ |
| 849 | if (cf_sk->sk.sk_priority > CAIF_PRIO_MAX) |
| 850 | cf_sk->conn_req.priority = CAIF_PRIO_MAX; |
| 851 | else if (cf_sk->sk.sk_priority < CAIF_PRIO_MIN) |
| 852 | cf_sk->conn_req.priority = CAIF_PRIO_MIN; |
| 853 | else |
| 854 | cf_sk->conn_req.priority = cf_sk->sk.sk_priority; |
| 855 | |
| 856 | /*ifindex = id of the interface.*/ |
| 857 | cf_sk->conn_req.ifindex = cf_sk->sk.sk_bound_dev_if; |
| 858 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 859 | dbfs_atomic_inc(&cnt.num_connect_req); |
| 860 | cf_sk->layer.receive = caif_sktrecv_cb; |
sjur.brandeland@stericsson.com | b3ccfbe | 2011-05-13 02:44:04 +0000 | [diff] [blame] | 861 | |
sjur.brandeland@stericsson.com | bee925d | 2011-05-13 02:44:05 +0000 | [diff] [blame] | 862 | err = caif_connect_client(sock_net(sk), &cf_sk->conn_req, |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 863 | &cf_sk->layer, &ifindex, &headroom, &tailroom); |
sjur.brandeland@stericsson.com | b3ccfbe | 2011-05-13 02:44:04 +0000 | [diff] [blame] | 864 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 865 | if (err < 0) { |
| 866 | cf_sk->sk.sk_socket->state = SS_UNCONNECTED; |
| 867 | cf_sk->sk.sk_state = CAIF_DISCONNECTED; |
| 868 | goto out; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 869 | } |
Eric Dumazet | 7931506 | 2010-10-04 22:42:08 +0000 | [diff] [blame] | 870 | |
| 871 | err = -ENODEV; |
| 872 | rcu_read_lock(); |
| 873 | dev = dev_get_by_index_rcu(sock_net(sk), ifindex); |
| 874 | if (!dev) { |
| 875 | rcu_read_unlock(); |
| 876 | goto out; |
| 877 | } |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 878 | cf_sk->headroom = LL_RESERVED_SPACE_EXTRA(dev, headroom); |
Eric Dumazet | 7931506 | 2010-10-04 22:42:08 +0000 | [diff] [blame] | 879 | mtu = dev->mtu; |
| 880 | rcu_read_unlock(); |
| 881 | |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 882 | cf_sk->tailroom = tailroom; |
Eric Dumazet | 7931506 | 2010-10-04 22:42:08 +0000 | [diff] [blame] | 883 | cf_sk->maxframe = mtu - (headroom + tailroom); |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 884 | if (cf_sk->maxframe < 1) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 885 | pr_warn("CAIF Interface MTU too small (%d)\n", dev->mtu); |
André Carvalho de Matos | f2527ec | 2010-11-01 11:52:47 +0000 | [diff] [blame] | 886 | err = -ENODEV; |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 887 | goto out; |
| 888 | } |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 889 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 890 | err = -EINPROGRESS; |
| 891 | wait_connect: |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 892 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 893 | if (sk->sk_state != CAIF_CONNECTED && (flags & O_NONBLOCK)) |
| 894 | goto out; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 895 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 896 | timeo = sock_sndtimeo(sk, flags & O_NONBLOCK); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 897 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 898 | release_sock(sk); |
Sjur Braendeland | 9e4b816 | 2010-05-21 02:16:07 +0000 | [diff] [blame] | 899 | err = -ERESTARTSYS; |
| 900 | timeo = wait_event_interruptible_timeout(*sk_sleep(sk), |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 901 | sk->sk_state != CAIF_CONNECTING, |
| 902 | timeo); |
| 903 | lock_sock(sk); |
Sjur Braendeland | 9e4b816 | 2010-05-21 02:16:07 +0000 | [diff] [blame] | 904 | if (timeo < 0) |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 905 | goto out; /* -ERESTARTSYS */ |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 906 | |
Sjur Braendeland | 9e4b816 | 2010-05-21 02:16:07 +0000 | [diff] [blame] | 907 | err = -ETIMEDOUT; |
| 908 | if (timeo == 0 && sk->sk_state != CAIF_CONNECTED) |
| 909 | goto out; |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 910 | if (sk->sk_state != CAIF_CONNECTED) { |
| 911 | sock->state = SS_UNCONNECTED; |
| 912 | err = sock_error(sk); |
| 913 | if (!err) |
| 914 | err = -ECONNREFUSED; |
| 915 | goto out; |
| 916 | } |
| 917 | sock->state = SS_CONNECTED; |
| 918 | err = 0; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 919 | out: |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 920 | release_sock(sk); |
| 921 | return err; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 922 | } |
| 923 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 924 | /* |
| 925 | * caif_release() - Disconnect a CAIF Socket |
| 926 | * Copied and modified af_irda.c:irda_release(). |
| 927 | */ |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 928 | static int caif_release(struct socket *sock) |
| 929 | { |
| 930 | struct sock *sk = sock->sk; |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 931 | struct caifsock *cf_sk = container_of(sk, struct caifsock, sk); |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 932 | |
| 933 | if (!sk) |
| 934 | return 0; |
| 935 | |
| 936 | set_tx_flow_off(cf_sk); |
| 937 | |
| 938 | /* |
| 939 | * Ensure that packets are not queued after this point in time. |
| 940 | * caif_queue_rcv_skb checks SOCK_DEAD holding the queue lock, |
| 941 | * this ensures no packets when sock is dead. |
| 942 | */ |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame] | 943 | spin_lock_bh(&sk->sk_receive_queue.lock); |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 944 | sock_set_flag(sk, SOCK_DEAD); |
sjur.brandeland@stericsson.com | c85c295 | 2011-05-13 02:44:06 +0000 | [diff] [blame] | 945 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 946 | sock->sk = NULL; |
| 947 | |
| 948 | dbfs_atomic_inc(&cnt.num_disconnect); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 949 | |
sjur.brandeland@stericsson.com | 33b2f55 | 2011-05-13 02:44:07 +0000 | [diff] [blame] | 950 | WARN_ON(IS_ERR(cf_sk->debugfs_socket_dir)); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 951 | if (cf_sk->debugfs_socket_dir != NULL) |
| 952 | debugfs_remove_recursive(cf_sk->debugfs_socket_dir); |
| 953 | |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 954 | lock_sock(&(cf_sk->sk)); |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 955 | sk->sk_state = CAIF_DISCONNECTED; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 956 | sk->sk_shutdown = SHUTDOWN_MASK; |
| 957 | |
sjur.brandeland@stericsson.com | 54e90fb | 2011-05-22 11:18:51 +0000 | [diff] [blame] | 958 | caif_disconnect_client(sock_net(sk), &cf_sk->layer); |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 959 | cf_sk->sk.sk_socket->state = SS_DISCONNECTING; |
| 960 | wake_up_interruptible_poll(sk_sleep(sk), POLLERR|POLLHUP); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 961 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 962 | sock_orphan(sk); |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 963 | sk_stream_kill_queues(&cf_sk->sk); |
| 964 | release_sock(sk); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 965 | sock_put(sk); |
sjur.brandeland@stericsson.com | 54e90fb | 2011-05-22 11:18:51 +0000 | [diff] [blame] | 966 | return 0; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 969 | /* Copied from af_unix.c:unix_poll(), added CAIF tx_flow handling */ |
| 970 | static unsigned int caif_poll(struct file *file, |
| 971 | struct socket *sock, poll_table *wait) |
| 972 | { |
| 973 | struct sock *sk = sock->sk; |
| 974 | unsigned int mask; |
| 975 | struct caifsock *cf_sk = container_of(sk, struct caifsock, sk); |
| 976 | |
| 977 | sock_poll_wait(file, sk_sleep(sk), wait); |
| 978 | mask = 0; |
| 979 | |
| 980 | /* exceptional events? */ |
| 981 | if (sk->sk_err) |
| 982 | mask |= POLLERR; |
| 983 | if (sk->sk_shutdown == SHUTDOWN_MASK) |
| 984 | mask |= POLLHUP; |
| 985 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 986 | mask |= POLLRDHUP; |
| 987 | |
| 988 | /* readable? */ |
| 989 | if (!skb_queue_empty(&sk->sk_receive_queue) || |
| 990 | (sk->sk_shutdown & RCV_SHUTDOWN)) |
| 991 | mask |= POLLIN | POLLRDNORM; |
| 992 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 993 | /* |
| 994 | * we set writable also when the other side has shut down the |
| 995 | * connection. This prevents stuck sockets. |
| 996 | */ |
| 997 | if (sock_writeable(sk) && tx_flow_is_on(cf_sk)) |
| 998 | mask |= POLLOUT | POLLWRNORM | POLLWRBAND; |
| 999 | |
| 1000 | return mask; |
| 1001 | } |
| 1002 | |
| 1003 | static const struct proto_ops caif_seqpacket_ops = { |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1004 | .family = PF_CAIF, |
| 1005 | .owner = THIS_MODULE, |
| 1006 | .release = caif_release, |
| 1007 | .bind = sock_no_bind, |
| 1008 | .connect = caif_connect, |
| 1009 | .socketpair = sock_no_socketpair, |
| 1010 | .accept = sock_no_accept, |
| 1011 | .getname = sock_no_getname, |
| 1012 | .poll = caif_poll, |
| 1013 | .ioctl = sock_no_ioctl, |
| 1014 | .listen = sock_no_listen, |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1015 | .shutdown = sock_no_shutdown, |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1016 | .setsockopt = setsockopt, |
| 1017 | .getsockopt = sock_no_getsockopt, |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1018 | .sendmsg = caif_seqpkt_sendmsg, |
| 1019 | .recvmsg = caif_seqpkt_recvmsg, |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1020 | .mmap = sock_no_mmap, |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1021 | .sendpage = sock_no_sendpage, |
| 1022 | }; |
| 1023 | |
| 1024 | static const struct proto_ops caif_stream_ops = { |
| 1025 | .family = PF_CAIF, |
| 1026 | .owner = THIS_MODULE, |
| 1027 | .release = caif_release, |
| 1028 | .bind = sock_no_bind, |
| 1029 | .connect = caif_connect, |
| 1030 | .socketpair = sock_no_socketpair, |
| 1031 | .accept = sock_no_accept, |
| 1032 | .getname = sock_no_getname, |
| 1033 | .poll = caif_poll, |
| 1034 | .ioctl = sock_no_ioctl, |
| 1035 | .listen = sock_no_listen, |
| 1036 | .shutdown = sock_no_shutdown, |
| 1037 | .setsockopt = setsockopt, |
| 1038 | .getsockopt = sock_no_getsockopt, |
| 1039 | .sendmsg = caif_stream_sendmsg, |
| 1040 | .recvmsg = caif_stream_recvmsg, |
| 1041 | .mmap = sock_no_mmap, |
| 1042 | .sendpage = sock_no_sendpage, |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1043 | }; |
| 1044 | |
| 1045 | /* This function is called when a socket is finally destroyed. */ |
| 1046 | static void caif_sock_destructor(struct sock *sk) |
| 1047 | { |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1048 | struct caifsock *cf_sk = container_of(sk, struct caifsock, sk); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1049 | caif_assert(!atomic_read(&sk->sk_wmem_alloc)); |
| 1050 | caif_assert(sk_unhashed(sk)); |
| 1051 | caif_assert(!sk->sk_socket); |
| 1052 | if (!sock_flag(sk, SOCK_DEAD)) { |
sjur.brandeland@stericsson.com | 33b2f55 | 2011-05-13 02:44:07 +0000 | [diff] [blame] | 1053 | pr_debug("Attempt to release alive CAIF socket: %p\n", sk); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1054 | return; |
| 1055 | } |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1056 | sk_stream_kill_queues(&cf_sk->sk); |
| 1057 | dbfs_atomic_dec(&cnt.caif_nr_socks); |
sjur.brandeland@stericsson.com | b3ccfbe | 2011-05-13 02:44:04 +0000 | [diff] [blame] | 1058 | caif_free_client(&cf_sk->layer); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | static int caif_create(struct net *net, struct socket *sock, int protocol, |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1062 | int kern) |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1063 | { |
sjur.brandeland@stericsson.com | 33b2f55 | 2011-05-13 02:44:07 +0000 | [diff] [blame] | 1064 | int num; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1065 | struct sock *sk = NULL; |
| 1066 | struct caifsock *cf_sk = NULL; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1067 | static struct proto prot = {.name = "PF_CAIF", |
| 1068 | .owner = THIS_MODULE, |
| 1069 | .obj_size = sizeof(struct caifsock), |
| 1070 | }; |
| 1071 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1072 | if (!capable(CAP_SYS_ADMIN) && !capable(CAP_NET_ADMIN)) |
| 1073 | return -EPERM; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1074 | /* |
| 1075 | * The sock->type specifies the socket type to use. |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1076 | * The CAIF socket is a packet stream in the sense |
| 1077 | * that it is packet based. CAIF trusts the reliability |
| 1078 | * of the link, no resending is implemented. |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1079 | */ |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1080 | if (sock->type == SOCK_SEQPACKET) |
| 1081 | sock->ops = &caif_seqpacket_ops; |
| 1082 | else if (sock->type == SOCK_STREAM) |
| 1083 | sock->ops = &caif_stream_ops; |
| 1084 | else |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1085 | return -ESOCKTNOSUPPORT; |
| 1086 | |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1087 | if (protocol < 0 || protocol >= CAIFPROTO_MAX) |
| 1088 | return -EPROTONOSUPPORT; |
| 1089 | /* |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1090 | * Set the socket state to unconnected. The socket state |
| 1091 | * is really not used at all in the net/core or socket.c but the |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1092 | * initialization makes sure that sock->state is not uninitialized. |
| 1093 | */ |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1094 | sk = sk_alloc(net, PF_CAIF, GFP_KERNEL, &prot); |
| 1095 | if (!sk) |
| 1096 | return -ENOMEM; |
| 1097 | |
| 1098 | cf_sk = container_of(sk, struct caifsock, sk); |
| 1099 | |
| 1100 | /* Store the protocol */ |
| 1101 | sk->sk_protocol = (unsigned char) protocol; |
| 1102 | |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1103 | /* |
| 1104 | * Lock in order to try to stop someone from opening the socket |
| 1105 | * too early. |
| 1106 | */ |
| 1107 | lock_sock(&(cf_sk->sk)); |
| 1108 | |
| 1109 | /* Initialize the nozero default sock structure data. */ |
| 1110 | sock_init_data(sock, sk); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1111 | sk->sk_destruct = caif_sock_destructor; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1112 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1113 | mutex_init(&cf_sk->readlock); /* single task reading lock */ |
| 1114 | cf_sk->layer.ctrlcmd = caif_ctrl_cb; |
| 1115 | cf_sk->sk.sk_socket->state = SS_UNCONNECTED; |
| 1116 | cf_sk->sk.sk_state = CAIF_DISCONNECTED; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1117 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1118 | set_tx_flow_off(cf_sk); |
| 1119 | set_rx_flow_on(cf_sk); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1120 | |
| 1121 | /* Set default options on configuration */ |
sjur.brandeland@stericsson.com | 54e90fb | 2011-05-22 11:18:51 +0000 | [diff] [blame] | 1122 | cf_sk->sk.sk_priority = CAIF_PRIO_NORMAL; |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1123 | cf_sk->conn_req.link_selector = CAIF_LINK_LOW_LATENCY; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1124 | cf_sk->conn_req.protocol = protocol; |
| 1125 | /* Increase the number of sockets created. */ |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1126 | dbfs_atomic_inc(&cnt.caif_nr_socks); |
sjur.brandeland@stericsson.com | 33b2f55 | 2011-05-13 02:44:07 +0000 | [diff] [blame] | 1127 | num = dbfs_atomic_inc(&cnt.caif_sock_create); |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1128 | #ifdef CONFIG_DEBUG_FS |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1129 | if (!IS_ERR(debugfsdir)) { |
sjur.brandeland@stericsson.com | 33b2f55 | 2011-05-13 02:44:07 +0000 | [diff] [blame] | 1130 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1131 | /* Fill in some information concerning the misc socket. */ |
sjur.brandeland@stericsson.com | 33b2f55 | 2011-05-13 02:44:07 +0000 | [diff] [blame] | 1132 | snprintf(cf_sk->name, sizeof(cf_sk->name), "cfsk%d", num); |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1133 | |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1134 | cf_sk->debugfs_socket_dir = |
| 1135 | debugfs_create_dir(cf_sk->name, debugfsdir); |
sjur.brandeland@stericsson.com | 33b2f55 | 2011-05-13 02:44:07 +0000 | [diff] [blame] | 1136 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1137 | debugfs_create_u32("sk_state", S_IRUSR | S_IWUSR, |
| 1138 | cf_sk->debugfs_socket_dir, |
| 1139 | (u32 *) &cf_sk->sk.sk_state); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1140 | debugfs_create_u32("flow_state", S_IRUSR | S_IWUSR, |
| 1141 | cf_sk->debugfs_socket_dir, &cf_sk->flow_state); |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1142 | debugfs_create_u32("sk_rmem_alloc", S_IRUSR | S_IWUSR, |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1143 | cf_sk->debugfs_socket_dir, |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1144 | (u32 *) &cf_sk->sk.sk_rmem_alloc); |
| 1145 | debugfs_create_u32("sk_wmem_alloc", S_IRUSR | S_IWUSR, |
| 1146 | cf_sk->debugfs_socket_dir, |
| 1147 | (u32 *) &cf_sk->sk.sk_wmem_alloc); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1148 | debugfs_create_u32("identity", S_IRUSR | S_IWUSR, |
| 1149 | cf_sk->debugfs_socket_dir, |
| 1150 | (u32 *) &cf_sk->layer.id); |
| 1151 | } |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1152 | #endif |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1153 | release_sock(&cf_sk->sk); |
| 1154 | return 0; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1157 | |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1158 | static struct net_proto_family caif_family_ops = { |
| 1159 | .family = PF_CAIF, |
| 1160 | .create = caif_create, |
| 1161 | .owner = THIS_MODULE, |
| 1162 | }; |
| 1163 | |
Sjur Braendeland | a9a8f10 | 2010-05-21 02:16:11 +0000 | [diff] [blame] | 1164 | static int af_caif_init(void) |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1165 | { |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1166 | int err = sock_register(&caif_family_ops); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1167 | if (!err) |
| 1168 | return err; |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1169 | return 0; |
| 1170 | } |
| 1171 | |
| 1172 | static int __init caif_sktinit_module(void) |
| 1173 | { |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1174 | #ifdef CONFIG_DEBUG_FS |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1175 | debugfsdir = debugfs_create_dir("caif_sk", NULL); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1176 | if (!IS_ERR(debugfsdir)) { |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1177 | debugfs_create_u32("num_sockets", S_IRUSR | S_IWUSR, |
| 1178 | debugfsdir, |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1179 | (u32 *) &cnt.caif_nr_socks); |
sjur.brandeland@stericsson.com | 33b2f55 | 2011-05-13 02:44:07 +0000 | [diff] [blame] | 1180 | debugfs_create_u32("num_create", S_IRUSR | S_IWUSR, |
| 1181 | debugfsdir, |
| 1182 | (u32 *) &cnt.caif_sock_create); |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1183 | debugfs_create_u32("num_connect_req", S_IRUSR | S_IWUSR, |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1184 | debugfsdir, |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1185 | (u32 *) &cnt.num_connect_req); |
| 1186 | debugfs_create_u32("num_connect_resp", S_IRUSR | S_IWUSR, |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1187 | debugfsdir, |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1188 | (u32 *) &cnt.num_connect_resp); |
| 1189 | debugfs_create_u32("num_connect_fail_resp", S_IRUSR | S_IWUSR, |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1190 | debugfsdir, |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1191 | (u32 *) &cnt.num_connect_fail_resp); |
| 1192 | debugfs_create_u32("num_disconnect", S_IRUSR | S_IWUSR, |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1193 | debugfsdir, |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1194 | (u32 *) &cnt.num_disconnect); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1195 | debugfs_create_u32("num_remote_shutdown_ind", |
| 1196 | S_IRUSR | S_IWUSR, debugfsdir, |
| 1197 | (u32 *) &cnt.num_remote_shutdown_ind); |
| 1198 | debugfs_create_u32("num_tx_flow_off_ind", S_IRUSR | S_IWUSR, |
| 1199 | debugfsdir, |
| 1200 | (u32 *) &cnt.num_tx_flow_off_ind); |
| 1201 | debugfs_create_u32("num_tx_flow_on_ind", S_IRUSR | S_IWUSR, |
| 1202 | debugfsdir, |
| 1203 | (u32 *) &cnt.num_tx_flow_on_ind); |
| 1204 | debugfs_create_u32("num_rx_flow_off", S_IRUSR | S_IWUSR, |
| 1205 | debugfsdir, |
| 1206 | (u32 *) &cnt.num_rx_flow_off); |
| 1207 | debugfs_create_u32("num_rx_flow_on", S_IRUSR | S_IWUSR, |
| 1208 | debugfsdir, |
| 1209 | (u32 *) &cnt.num_rx_flow_on); |
| 1210 | } |
| 1211 | #endif |
Sjur Braendeland | bece7b2 | 2010-04-28 08:54:38 +0000 | [diff] [blame] | 1212 | return af_caif_init(); |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | static void __exit caif_sktexit_module(void) |
| 1216 | { |
| 1217 | sock_unregister(PF_CAIF); |
| 1218 | if (debugfsdir != NULL) |
| 1219 | debugfs_remove_recursive(debugfsdir); |
| 1220 | } |
Sjur Braendeland | e6f95ec | 2010-03-30 13:56:26 +0000 | [diff] [blame] | 1221 | module_init(caif_sktinit_module); |
| 1222 | module_exit(caif_sktexit_module); |