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