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