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