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