xref: /openbmc/linux/net/caif/caif_socket.c (revision 41415b8a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) ST-Ericsson AB 2010
4  * Author:	Sjur Brendeland
5  */
6 
7 #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
8 
9 #include <linux/filter.h>
10 #include <linux/fs.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/sched/signal.h>
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>
21 #include <linux/debugfs.h>
22 #include <linux/caif/caif_socket.h>
23 #include <linux/pkt_sched.h>
24 #include <net/sock.h>
25 #include <net/tcp_states.h>
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");
31 MODULE_ALIAS_NETPROTO(AF_CAIF);
32 
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 };
43 
44 #define TX_FLOW_ON_BIT	1
45 #define RX_FLOW_ON_BIT	2
46 
47 struct caifsock {
48 	struct sock sk; /* must be first member */
49 	struct cflayer layer;
50 	u32 flow_state;
51 	struct caif_connect_request conn_req;
52 	struct mutex readlock;
53 	struct dentry *debugfs_socket_dir;
54 	int headroom, tailroom, maxframe;
55 };
56 
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 
107 static int sk_rcvbuf_lowwater(struct caifsock *cf_sk)
108 {
109 	/* A quarter of full buffer is used a low water mark */
110 	return cf_sk->sk.sk_rcvbuf / 4;
111 }
112 
113 static void caif_flow_ctrl(struct sock *sk, int mode)
114 {
115 	struct caifsock *cf_sk;
116 	cf_sk = container_of(sk, struct caifsock, sk);
117 	if (cf_sk->layer.dn && cf_sk->layer.dn->modemcmd)
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  */
125 static void caif_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
126 {
127 	int err;
128 	unsigned long flags;
129 	struct sk_buff_head *list = &sk->sk_receive_queue;
130 	struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
131 	bool queued = false;
132 
133 	if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
134 		(unsigned int)sk->sk_rcvbuf && rx_flow_is_on(cf_sk)) {
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));
138 		set_rx_flow_off(cf_sk);
139 		caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_OFF_REQ);
140 	}
141 
142 	err = sk_filter(sk, skb);
143 	if (err)
144 		goto out;
145 
146 	if (!sk_rmem_schedule(sk, skb, skb->truesize) && rx_flow_is_on(cf_sk)) {
147 		set_rx_flow_off(cf_sk);
148 		net_dbg_ratelimited("sending flow OFF due to rmem_schedule\n");
149 		caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_OFF_REQ);
150 	}
151 	skb->dev = NULL;
152 	skb_set_owner_r(skb, sk);
153 	spin_lock_irqsave(&list->lock, flags);
154 	queued = !sock_flag(sk, SOCK_DEAD);
155 	if (queued)
156 		__skb_queue_tail(list, skb);
157 	spin_unlock_irqrestore(&list->lock, flags);
158 out:
159 	if (queued)
160 		sk->sk_data_ready(sk);
161 	else
162 		kfree_skb(skb);
163 }
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;
169 	struct sk_buff *skb;
170 
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)) {
175 		kfree_skb(skb);
176 		return 0;
177 	}
178 	caif_queue_rcv_skb(&cf_sk->sk, skb);
179 	return 0;
180 }
181 
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 
194 /* Packet Control Callback function called from CAIF */
195 static void caif_ctrl_cb(struct cflayer *layr,
196 			 enum caif_ctrlcmd flow,
197 			 int phyid)
198 {
199 	struct caifsock *cf_sk = container_of(layr, struct caifsock, layer);
200 	switch (flow) {
201 	case CAIF_CTRLCMD_FLOW_ON_IND:
202 		/* OK from modem to start sending again */
203 		set_tx_flow_on(cf_sk);
204 		cf_sk->sk.sk_state_change(&cf_sk->sk);
205 		break;
206 
207 	case CAIF_CTRLCMD_FLOW_OFF_IND:
208 		/* Modem asks us to shut up */
209 		set_tx_flow_off(cf_sk);
210 		cf_sk->sk.sk_state_change(&cf_sk->sk);
211 		break;
212 
213 	case CAIF_CTRLCMD_INIT_RSP:
214 		/* We're now connected */
215 		caif_client_register_refcnt(&cf_sk->layer,
216 						cfsk_hold, cfsk_put);
217 		cf_sk->sk.sk_state = CAIF_CONNECTED;
218 		set_tx_flow_on(cf_sk);
219 		cf_sk->sk.sk_shutdown = 0;
220 		cf_sk->sk.sk_state_change(&cf_sk->sk);
221 		break;
222 
223 	case CAIF_CTRLCMD_DEINIT_RSP:
224 		/* We're now disconnected */
225 		cf_sk->sk.sk_state = CAIF_DISCONNECTED;
226 		cf_sk->sk.sk_state_change(&cf_sk->sk);
227 		break;
228 
229 	case CAIF_CTRLCMD_INIT_FAIL_RSP:
230 		/* Connect request failed */
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);
240 		break;
241 
242 	case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND:
243 		/* Modem has closed this connection, or device is down. */
244 		cf_sk->sk.sk_shutdown = SHUTDOWN_MASK;
245 		cf_sk->sk.sk_err = ECONNRESET;
246 		set_rx_flow_on(cf_sk);
247 		sk_error_report(&cf_sk->sk);
248 		break;
249 
250 	default:
251 		pr_debug("Unexpected flow command %d\n", flow);
252 	}
253 }
254 
255 static void caif_check_flow_release(struct sock *sk)
256 {
257 	struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
258 
259 	if (rx_flow_is_on(cf_sk))
260 		return;
261 
262 	if (atomic_read(&sk->sk_rmem_alloc) <= sk_rcvbuf_lowwater(cf_sk)) {
263 			set_rx_flow_on(cf_sk);
264 			caif_flow_ctrl(sk, CAIF_MODEMCMD_FLOW_ON_REQ);
265 	}
266 }
267 
268 /*
269  * Copied from unix_dgram_recvmsg, but removed credit checks,
270  * changed locking, address handling and added MSG_TRUNC.
271  */
272 static int caif_seqpkt_recvmsg(struct socket *sock, struct msghdr *m,
273 			       size_t len, int flags)
274 
275 {
276 	struct sock *sk = sock->sk;
277 	struct sk_buff *skb;
278 	int ret;
279 	int copylen;
280 
281 	ret = -EOPNOTSUPP;
282 	if (flags & MSG_OOB)
283 		goto read_error;
284 
285 	skb = skb_recv_datagram(sk, flags, 0 , &ret);
286 	if (!skb)
287 		goto read_error;
288 	copylen = skb->len;
289 	if (len < copylen) {
290 		m->msg_flags |= MSG_TRUNC;
291 		copylen = len;
292 	}
293 
294 	ret = skb_copy_datagram_msg(skb, 0, m, copylen);
295 	if (ret)
296 		goto out_free;
297 
298 	ret = (flags & MSG_TRUNC) ? skb->len : copylen;
299 out_free:
300 	skb_free_datagram(sk, skb);
301 	caif_check_flow_release(sk);
302 	return ret;
303 
304 read_error:
305 	return ret;
306 }
307 
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 
327 		sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
328 		release_sock(sk);
329 		timeo = schedule_timeout(timeo);
330 		lock_sock(sk);
331 
332 		if (sock_flag(sk, SOCK_DEAD))
333 			break;
334 
335 		sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
336 	}
337 
338 	finish_wait(sk_sleep(sk), &wait);
339 	release_sock(sk);
340 	return timeo;
341 }
342 
343 
344 /*
345  * Copied from unix_stream_recvmsg, but removed credit checks,
346  * changed locking calls, changed address handling.
347  */
348 static int caif_stream_recvmsg(struct socket *sock, struct msghdr *msg,
349 			       size_t size, int flags)
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 
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);
378 		if (sock_flag(sk, SOCK_DEAD)) {
379 			err = -ECONNRESET;
380 			goto unlock;
381 		}
382 		skb = skb_dequeue(&sk->sk_receive_queue);
383 		caif_check_flow_release(sk);
384 
385 		if (skb == NULL) {
386 			if (copied >= target)
387 				goto unlock;
388 			/*
389 			 *	POSIX 1003.1g mandates this order.
390 			 */
391 			err = sock_error(sk);
392 			if (err)
393 				goto unlock;
394 			err = -ECONNRESET;
395 			if (sk->sk_shutdown & RCV_SHUTDOWN)
396 				goto unlock;
397 
398 			err = -EPIPE;
399 			if (sk->sk_state != CAIF_CONNECTED)
400 				goto unlock;
401 			if (sock_flag(sk, SOCK_DEAD))
402 				goto unlock;
403 
404 			release_sock(sk);
405 
406 			err = -EAGAIN;
407 			if (!timeo)
408 				break;
409 
410 			caif_read_unlock(sk);
411 
412 			timeo = caif_stream_data_wait(sk, timeo);
413 
414 			if (signal_pending(current)) {
415 				err = sock_intr_errno(timeo);
416 				goto out;
417 			}
418 			caif_read_lock(sk);
419 			continue;
420 unlock:
421 			release_sock(sk);
422 			break;
423 		}
424 		release_sock(sk);
425 		chunk = min_t(unsigned int, skb->len, size);
426 		if (memcpy_to_msg(msg, skb->data, chunk)) {
427 			skb_queue_head(&sk->sk_receive_queue, skb);
428 			if (copied == 0)
429 				copied = -EFAULT;
430 			break;
431 		}
432 		copied += chunk;
433 		size -= chunk;
434 
435 		/* Mark read part of skb as used */
436 		if (!(flags & MSG_PEEK)) {
437 			skb_pull(skb, chunk);
438 
439 			/* put the skb back if we didn't use it up. */
440 			if (skb->len) {
441 				skb_queue_head(&sk->sk_receive_queue, skb);
442 				break;
443 			}
444 			kfree_skb(skb);
445 
446 		} else {
447 			/*
448 			 * It is questionable, see note in unix_dgram_recvmsg.
449 			 */
450 			/* put message back and return */
451 			skb_queue_head(&sk->sk_receive_queue, skb);
452 			break;
453 		}
454 	} while (size);
455 	caif_read_unlock(sk);
456 
457 out:
458 	return copied ? : err;
459 }
460 
461 /*
462  * Copied from sock.c:sock_wait_for_wmem, but change to wait for
463  * CAIF flow-on and sock_writable.
464  */
465 static long caif_wait_for_flow_on(struct caifsock *cf_sk,
466 				  int wait_writeable, long timeo, int *err)
467 {
468 	struct sock *sk = &cf_sk->sk;
469 	DEFINE_WAIT(wait);
470 	for (;;) {
471 		*err = 0;
472 		if (tx_flow_is_on(cf_sk) &&
473 			(!wait_writeable || sock_writeable(&cf_sk->sk)))
474 			break;
475 		*err = -ETIMEDOUT;
476 		if (!timeo)
477 			break;
478 		*err = -ERESTARTSYS;
479 		if (signal_pending(current))
480 			break;
481 		prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
482 		*err = -ECONNRESET;
483 		if (sk->sk_shutdown & SHUTDOWN_MASK)
484 			break;
485 		*err = -sk->sk_err;
486 		if (sk->sk_err)
487 			break;
488 		*err = -EPIPE;
489 		if (cf_sk->sk.sk_state != CAIF_CONNECTED)
490 			break;
491 		timeo = schedule_timeout(timeo);
492 	}
493 	finish_wait(sk_sleep(sk), &wait);
494 	return timeo;
495 }
496 
497 /*
498  * Transmit a SKB. The device may temporarily request re-transmission
499  * by returning EAGAIN.
500  */
501 static int transmit_skb(struct sk_buff *skb, struct caifsock *cf_sk,
502 			int noblock, long timeo)
503 {
504 	struct cfpkt *pkt;
505 
506 	pkt = cfpkt_fromnative(CAIF_DIR_OUT, skb);
507 	memset(skb->cb, 0, sizeof(struct caif_payload_info));
508 	cfpkt_set_prio(pkt, cf_sk->sk.sk_priority);
509 
510 	if (cf_sk->layer.dn == NULL) {
511 		kfree_skb(skb);
512 		return -EINVAL;
513 	}
514 
515 	return cf_sk->layer.dn->transmit(cf_sk->layer.dn, pkt);
516 }
517 
518 /* Copied from af_unix:unix_dgram_sendmsg, and adapted to CAIF */
519 static int caif_seqpkt_sendmsg(struct socket *sock, struct msghdr *msg,
520 			       size_t len)
521 {
522 	struct sock *sk = sock->sk;
523 	struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
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;
533 
534 	ret = -EOPNOTSUPP;
535 	if (msg->msg_flags&MSG_OOB)
536 		goto err;
537 
538 	ret = -EOPNOTSUPP;
539 	if (msg->msg_namelen)
540 		goto err;
541 
542 	ret = -EINVAL;
543 	if (unlikely(msg->msg_iter.nr_segs == 0) ||
544 	    unlikely(msg->msg_iter.iov->iov_base == NULL))
545 		goto err;
546 	noblock = msg->msg_flags & MSG_DONTWAIT;
547 
548 	timeo = sock_sndtimeo(sk, noblock);
549 	timeo = caif_wait_for_flow_on(container_of(sk, struct caifsock, sk),
550 				1, timeo, &ret);
551 
552 	if (ret)
553 		goto err;
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;
559 
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 
567 	ret = -ENOMEM;
568 	skb = sock_alloc_send_skb(sk, buffer_size, noblock, &ret);
569 
570 	if (!skb || skb_tailroom(skb) < buffer_size)
571 		goto err;
572 
573 	skb_reserve(skb, cf_sk->headroom);
574 
575 	ret = memcpy_from_msg(skb_put(skb, len), msg, len);
576 
577 	if (ret)
578 		goto err;
579 	ret = transmit_skb(skb, cf_sk, noblock, timeo);
580 	if (ret < 0)
581 		/* skb is already freed */
582 		return ret;
583 
584 	return len;
585 err:
586 	kfree_skb(skb);
587 	return ret;
588 }
589 
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  */
595 static int caif_stream_sendmsg(struct socket *sock, struct msghdr *msg,
596 			       size_t len)
597 {
598 	struct sock *sk = sock->sk;
599 	struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
600 	int err, size;
601 	struct sk_buff *skb;
602 	int sent = 0;
603 	long timeo;
604 
605 	err = -EOPNOTSUPP;
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 
622 		if (size > cf_sk->maxframe)
623 			size = cf_sk->maxframe;
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,
633 					size + cf_sk->headroom +
634 					cf_sk->tailroom,
635 					msg->msg_flags&MSG_DONTWAIT,
636 					&err);
637 		if (skb == NULL)
638 			goto out_err;
639 
640 		skb_reserve(skb, cf_sk->headroom);
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 
650 		err = memcpy_from_msg(skb_put(skb, size), msg, size);
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);
657 		if (err < 0)
658 			/* skb is already freed */
659 			goto pipe_err;
660 
661 		sent += size;
662 	}
663 
664 	return sent;
665 
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;
672 }
673 
674 static int setsockopt(struct socket *sock, int lvl, int opt, sockptr_t ov,
675 		unsigned int ol)
676 {
677 	struct sock *sk = sock->sk;
678 	struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
679 	int linksel;
680 
681 	if (cf_sk->sk.sk_socket->state != SS_UNCONNECTED)
682 		return -ENOPROTOOPT;
683 
684 	switch (opt) {
685 	case CAIFSO_LINK_SELECT:
686 		if (ol < sizeof(int))
687 			return -EINVAL;
688 		if (lvl != SOL_CAIF)
689 			goto bad_sol;
690 		if (copy_from_sockptr(&linksel, ov, sizeof(int)))
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 
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;
702 		lock_sock(&(cf_sk->sk));
703 		if (ol > sizeof(cf_sk->conn_req.param.data) ||
704 		    copy_from_sockptr(&cf_sk->conn_req.param.data, ov, ol)) {
705 			release_sock(&cf_sk->sk);
706 			return -EINVAL;
707 		}
708 		cf_sk->conn_req.param.size = ol;
709 		release_sock(&cf_sk->sk);
710 		return 0;
711 
712 	default:
713 		return -ENOPROTOOPT;
714 	}
715 
716 	return 0;
717 bad_sol:
718 	return -ENOPROTOOPT;
719 
720 }
721 
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)
748 {
749 	struct sock *sk = sock->sk;
750 	struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
751 	long timeo;
752 	int err;
753 	int ifindex, headroom, tailroom;
754 	unsigned int mtu;
755 	struct net_device *dev;
756 
757 	lock_sock(sk);
758 
759 	err = -EINVAL;
760 	if (addr_len < offsetofend(struct sockaddr, sa_family))
761 		goto out;
762 
763 	err = -EAFNOSUPPORT;
764 	if (uaddr->sa_family != AF_CAIF)
765 		goto out;
766 
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 */
793 			caif_disconnect_client(sock_net(sk), &cf_sk->layer);
794 			caif_free_client(&cf_sk->layer);
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;
804 	}
805 	sk->sk_state = CAIF_DISCONNECTED;
806 	sock->state = SS_UNCONNECTED;
807 	sk_stream_kill_queues(&cf_sk->sk);
808 
809 	err = -EINVAL;
810 	if (addr_len != sizeof(struct sockaddr_caif))
811 		goto out;
812 
813 	memcpy(&cf_sk->conn_req.sockaddr, uaddr,
814 		sizeof(struct sockaddr_caif));
815 
816 	/* Move to connecting socket, start sending Connect Requests */
817 	sock->state = SS_CONNECTING;
818 	sk->sk_state = CAIF_CONNECTING;
819 
820 	/* Check priority value comming from socket */
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 
832 	cf_sk->layer.receive = caif_sktrecv_cb;
833 
834 	err = caif_connect_client(sock_net(sk), &cf_sk->conn_req,
835 				&cf_sk->layer, &ifindex, &headroom, &tailroom);
836 
837 	if (err < 0) {
838 		cf_sk->sk.sk_socket->state = SS_UNCONNECTED;
839 		cf_sk->sk.sk_state = CAIF_DISCONNECTED;
840 		goto out;
841 	}
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 	}
850 	cf_sk->headroom = LL_RESERVED_SPACE_EXTRA(dev, headroom);
851 	mtu = dev->mtu;
852 	rcu_read_unlock();
853 
854 	cf_sk->tailroom = tailroom;
855 	cf_sk->maxframe = mtu - (headroom + tailroom);
856 	if (cf_sk->maxframe < 1) {
857 		pr_warn("CAIF Interface MTU too small (%d)\n", dev->mtu);
858 		err = -ENODEV;
859 		goto out;
860 	}
861 
862 	err = -EINPROGRESS;
863 wait_connect:
864 
865 	if (sk->sk_state != CAIF_CONNECTED && (flags & O_NONBLOCK))
866 		goto out;
867 
868 	timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
869 
870 	release_sock(sk);
871 	err = -ERESTARTSYS;
872 	timeo = wait_event_interruptible_timeout(*sk_sleep(sk),
873 			sk->sk_state != CAIF_CONNECTING,
874 			timeo);
875 	lock_sock(sk);
876 	if (timeo < 0)
877 		goto out; /* -ERESTARTSYS */
878 
879 	err = -ETIMEDOUT;
880 	if (timeo == 0 && sk->sk_state != CAIF_CONNECTED)
881 		goto out;
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;
891 out:
892 	release_sock(sk);
893 	return err;
894 }
895 
896 /*
897  * caif_release() - Disconnect a CAIF Socket
898  * Copied and modified af_irda.c:irda_release().
899  */
900 static int caif_release(struct socket *sock)
901 {
902 	struct sock *sk = sock->sk;
903 	struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
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 	 */
915 	spin_lock_bh(&sk->sk_receive_queue.lock);
916 	sock_set_flag(sk, SOCK_DEAD);
917 	spin_unlock_bh(&sk->sk_receive_queue.lock);
918 	sock->sk = NULL;
919 
920 	WARN_ON(IS_ERR(cf_sk->debugfs_socket_dir));
921 	debugfs_remove_recursive(cf_sk->debugfs_socket_dir);
922 
923 	lock_sock(&(cf_sk->sk));
924 	sk->sk_state = CAIF_DISCONNECTED;
925 	sk->sk_shutdown = SHUTDOWN_MASK;
926 
927 	caif_disconnect_client(sock_net(sk), &cf_sk->layer);
928 	cf_sk->sk.sk_socket->state = SS_DISCONNECTING;
929 	wake_up_interruptible_poll(sk_sleep(sk), EPOLLERR|EPOLLHUP);
930 
931 	sock_orphan(sk);
932 	sk_stream_kill_queues(&cf_sk->sk);
933 	release_sock(sk);
934 	sock_put(sk);
935 	return 0;
936 }
937 
938 /* Copied from af_unix.c:unix_poll(), added CAIF tx_flow handling */
939 static __poll_t caif_poll(struct file *file,
940 			      struct socket *sock, poll_table *wait)
941 {
942 	struct sock *sk = sock->sk;
943 	__poll_t mask;
944 	struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
945 
946 	sock_poll_wait(file, sock, wait);
947 	mask = 0;
948 
949 	/* exceptional events? */
950 	if (sk->sk_err)
951 		mask |= EPOLLERR;
952 	if (sk->sk_shutdown == SHUTDOWN_MASK)
953 		mask |= EPOLLHUP;
954 	if (sk->sk_shutdown & RCV_SHUTDOWN)
955 		mask |= EPOLLRDHUP;
956 
957 	/* readable? */
958 	if (!skb_queue_empty_lockless(&sk->sk_receive_queue) ||
959 		(sk->sk_shutdown & RCV_SHUTDOWN))
960 		mask |= EPOLLIN | EPOLLRDNORM;
961 
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))
967 		mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
968 
969 	return mask;
970 }
971 
972 static const struct proto_ops caif_seqpacket_ops = {
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,
981 	.poll = caif_poll,
982 	.ioctl = sock_no_ioctl,
983 	.listen = sock_no_listen,
984 	.shutdown = sock_no_shutdown,
985 	.setsockopt = setsockopt,
986 	.sendmsg = caif_seqpkt_sendmsg,
987 	.recvmsg = caif_seqpkt_recvmsg,
988 	.mmap = sock_no_mmap,
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,
1001 	.poll = caif_poll,
1002 	.ioctl = sock_no_ioctl,
1003 	.listen = sock_no_listen,
1004 	.shutdown = sock_no_shutdown,
1005 	.setsockopt = setsockopt,
1006 	.sendmsg = caif_stream_sendmsg,
1007 	.recvmsg = caif_stream_recvmsg,
1008 	.mmap = sock_no_mmap,
1009 	.sendpage = sock_no_sendpage,
1010 };
1011 
1012 /* This function is called when a socket is finally destroyed. */
1013 static void caif_sock_destructor(struct sock *sk)
1014 {
1015 	struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
1016 	caif_assert(!refcount_read(&sk->sk_wmem_alloc));
1017 	caif_assert(sk_unhashed(sk));
1018 	caif_assert(!sk->sk_socket);
1019 	if (!sock_flag(sk, SOCK_DEAD)) {
1020 		pr_debug("Attempt to release alive CAIF socket: %p\n", sk);
1021 		return;
1022 	}
1023 	sk_stream_kill_queues(&cf_sk->sk);
1024 	caif_free_client(&cf_sk->layer);
1025 }
1026 
1027 static int caif_create(struct net *net, struct socket *sock, int protocol,
1028 		       int kern)
1029 {
1030 	struct sock *sk = NULL;
1031 	struct caifsock *cf_sk = NULL;
1032 	static struct proto prot = {.name = "PF_CAIF",
1033 		.owner = THIS_MODULE,
1034 		.obj_size = sizeof(struct caifsock),
1035 		.useroffset = offsetof(struct caifsock, conn_req.param),
1036 		.usersize = sizeof_field(struct caifsock, conn_req.param)
1037 	};
1038 
1039 	if (!capable(CAP_SYS_ADMIN) && !capable(CAP_NET_ADMIN))
1040 		return -EPERM;
1041 	/*
1042 	 * The sock->type specifies the socket type to use.
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.
1046 	 */
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
1052 		return -ESOCKTNOSUPPORT;
1053 
1054 	if (protocol < 0 || protocol >= CAIFPROTO_MAX)
1055 		return -EPROTONOSUPPORT;
1056 	/*
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
1059 	 * initialization makes sure that sock->state is not uninitialized.
1060 	 */
1061 	sk = sk_alloc(net, PF_CAIF, GFP_KERNEL, &prot, kern);
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 
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 
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);
1090 	sk->sk_destruct = caif_sock_destructor;
1091 
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;
1096 
1097 	set_tx_flow_off(cf_sk);
1098 	set_rx_flow_on(cf_sk);
1099 
1100 	/* Set default options on configuration */
1101 	cf_sk->conn_req.link_selector = CAIF_LINK_LOW_LATENCY;
1102 	cf_sk->conn_req.protocol = protocol;
1103 	release_sock(&cf_sk->sk);
1104 	return 0;
1105 }
1106 
1107 
1108 static const struct net_proto_family caif_family_ops = {
1109 	.family = PF_CAIF,
1110 	.create = caif_create,
1111 	.owner = THIS_MODULE,
1112 };
1113 
1114 static int __init caif_sktinit_module(void)
1115 {
1116 	return sock_register(&caif_family_ops);
1117 }
1118 
1119 static void __exit caif_sktexit_module(void)
1120 {
1121 	sock_unregister(PF_CAIF);
1122 }
1123 module_init(caif_sktinit_module);
1124 module_exit(caif_sktexit_module);
1125