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