Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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