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