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