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