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