1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /* AF_RXRPC sendmsg() implementation.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved.
5*4882a593Smuzhiyun * Written by David Howells (dhowells@redhat.com)
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <linux/net.h>
11*4882a593Smuzhiyun #include <linux/gfp.h>
12*4882a593Smuzhiyun #include <linux/skbuff.h>
13*4882a593Smuzhiyun #include <linux/export.h>
14*4882a593Smuzhiyun #include <linux/sched/signal.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #include <net/sock.h>
17*4882a593Smuzhiyun #include <net/af_rxrpc.h>
18*4882a593Smuzhiyun #include "ar-internal.h"
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun /*
21*4882a593Smuzhiyun * Return true if there's sufficient Tx queue space.
22*4882a593Smuzhiyun */
rxrpc_check_tx_space(struct rxrpc_call * call,rxrpc_seq_t * _tx_win)23*4882a593Smuzhiyun static bool rxrpc_check_tx_space(struct rxrpc_call *call, rxrpc_seq_t *_tx_win)
24*4882a593Smuzhiyun {
25*4882a593Smuzhiyun unsigned int win_size =
26*4882a593Smuzhiyun min_t(unsigned int, call->tx_winsize,
27*4882a593Smuzhiyun call->cong_cwnd + call->cong_extra);
28*4882a593Smuzhiyun rxrpc_seq_t tx_win = READ_ONCE(call->tx_hard_ack);
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun if (_tx_win)
31*4882a593Smuzhiyun *_tx_win = tx_win;
32*4882a593Smuzhiyun return call->tx_top - tx_win < win_size;
33*4882a593Smuzhiyun }
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun /*
36*4882a593Smuzhiyun * Wait for space to appear in the Tx queue or a signal to occur.
37*4882a593Smuzhiyun */
rxrpc_wait_for_tx_window_intr(struct rxrpc_sock * rx,struct rxrpc_call * call,long * timeo)38*4882a593Smuzhiyun static int rxrpc_wait_for_tx_window_intr(struct rxrpc_sock *rx,
39*4882a593Smuzhiyun struct rxrpc_call *call,
40*4882a593Smuzhiyun long *timeo)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun for (;;) {
43*4882a593Smuzhiyun set_current_state(TASK_INTERRUPTIBLE);
44*4882a593Smuzhiyun if (rxrpc_check_tx_space(call, NULL))
45*4882a593Smuzhiyun return 0;
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun if (call->state >= RXRPC_CALL_COMPLETE)
48*4882a593Smuzhiyun return call->error;
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun if (signal_pending(current))
51*4882a593Smuzhiyun return sock_intr_errno(*timeo);
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun trace_rxrpc_transmit(call, rxrpc_transmit_wait);
54*4882a593Smuzhiyun *timeo = schedule_timeout(*timeo);
55*4882a593Smuzhiyun }
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun /*
59*4882a593Smuzhiyun * Wait for space to appear in the Tx queue uninterruptibly, but with
60*4882a593Smuzhiyun * a timeout of 2*RTT if no progress was made and a signal occurred.
61*4882a593Smuzhiyun */
rxrpc_wait_for_tx_window_waitall(struct rxrpc_sock * rx,struct rxrpc_call * call)62*4882a593Smuzhiyun static int rxrpc_wait_for_tx_window_waitall(struct rxrpc_sock *rx,
63*4882a593Smuzhiyun struct rxrpc_call *call)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun rxrpc_seq_t tx_start, tx_win;
66*4882a593Smuzhiyun signed long rtt, timeout;
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun rtt = READ_ONCE(call->peer->srtt_us) >> 3;
69*4882a593Smuzhiyun rtt = usecs_to_jiffies(rtt) * 2;
70*4882a593Smuzhiyun if (rtt < 2)
71*4882a593Smuzhiyun rtt = 2;
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun timeout = rtt;
74*4882a593Smuzhiyun tx_start = READ_ONCE(call->tx_hard_ack);
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun for (;;) {
77*4882a593Smuzhiyun set_current_state(TASK_UNINTERRUPTIBLE);
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun tx_win = READ_ONCE(call->tx_hard_ack);
80*4882a593Smuzhiyun if (rxrpc_check_tx_space(call, &tx_win))
81*4882a593Smuzhiyun return 0;
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun if (call->state >= RXRPC_CALL_COMPLETE)
84*4882a593Smuzhiyun return call->error;
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun if (timeout == 0 &&
87*4882a593Smuzhiyun tx_win == tx_start && signal_pending(current))
88*4882a593Smuzhiyun return -EINTR;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun if (tx_win != tx_start) {
91*4882a593Smuzhiyun timeout = rtt;
92*4882a593Smuzhiyun tx_start = tx_win;
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun trace_rxrpc_transmit(call, rxrpc_transmit_wait);
96*4882a593Smuzhiyun timeout = schedule_timeout(timeout);
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun /*
101*4882a593Smuzhiyun * Wait for space to appear in the Tx queue uninterruptibly.
102*4882a593Smuzhiyun */
rxrpc_wait_for_tx_window_nonintr(struct rxrpc_sock * rx,struct rxrpc_call * call,long * timeo)103*4882a593Smuzhiyun static int rxrpc_wait_for_tx_window_nonintr(struct rxrpc_sock *rx,
104*4882a593Smuzhiyun struct rxrpc_call *call,
105*4882a593Smuzhiyun long *timeo)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun for (;;) {
108*4882a593Smuzhiyun set_current_state(TASK_UNINTERRUPTIBLE);
109*4882a593Smuzhiyun if (rxrpc_check_tx_space(call, NULL))
110*4882a593Smuzhiyun return 0;
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun if (call->state >= RXRPC_CALL_COMPLETE)
113*4882a593Smuzhiyun return call->error;
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun trace_rxrpc_transmit(call, rxrpc_transmit_wait);
116*4882a593Smuzhiyun *timeo = schedule_timeout(*timeo);
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun /*
121*4882a593Smuzhiyun * wait for space to appear in the transmit/ACK window
122*4882a593Smuzhiyun * - caller holds the socket locked
123*4882a593Smuzhiyun */
rxrpc_wait_for_tx_window(struct rxrpc_sock * rx,struct rxrpc_call * call,long * timeo,bool waitall)124*4882a593Smuzhiyun static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx,
125*4882a593Smuzhiyun struct rxrpc_call *call,
126*4882a593Smuzhiyun long *timeo,
127*4882a593Smuzhiyun bool waitall)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun DECLARE_WAITQUEUE(myself, current);
130*4882a593Smuzhiyun int ret;
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun _enter(",{%u,%u,%u}",
133*4882a593Smuzhiyun call->tx_hard_ack, call->tx_top, call->tx_winsize);
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun add_wait_queue(&call->waitq, &myself);
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun switch (call->interruptibility) {
138*4882a593Smuzhiyun case RXRPC_INTERRUPTIBLE:
139*4882a593Smuzhiyun if (waitall)
140*4882a593Smuzhiyun ret = rxrpc_wait_for_tx_window_waitall(rx, call);
141*4882a593Smuzhiyun else
142*4882a593Smuzhiyun ret = rxrpc_wait_for_tx_window_intr(rx, call, timeo);
143*4882a593Smuzhiyun break;
144*4882a593Smuzhiyun case RXRPC_PREINTERRUPTIBLE:
145*4882a593Smuzhiyun case RXRPC_UNINTERRUPTIBLE:
146*4882a593Smuzhiyun default:
147*4882a593Smuzhiyun ret = rxrpc_wait_for_tx_window_nonintr(rx, call, timeo);
148*4882a593Smuzhiyun break;
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun remove_wait_queue(&call->waitq, &myself);
152*4882a593Smuzhiyun set_current_state(TASK_RUNNING);
153*4882a593Smuzhiyun _leave(" = %d", ret);
154*4882a593Smuzhiyun return ret;
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun /*
158*4882a593Smuzhiyun * Schedule an instant Tx resend.
159*4882a593Smuzhiyun */
rxrpc_instant_resend(struct rxrpc_call * call,int ix)160*4882a593Smuzhiyun static inline void rxrpc_instant_resend(struct rxrpc_call *call, int ix)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun spin_lock_bh(&call->lock);
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun if (call->state < RXRPC_CALL_COMPLETE) {
165*4882a593Smuzhiyun call->rxtx_annotations[ix] =
166*4882a593Smuzhiyun (call->rxtx_annotations[ix] & RXRPC_TX_ANNO_LAST) |
167*4882a593Smuzhiyun RXRPC_TX_ANNO_RETRANS;
168*4882a593Smuzhiyun if (!test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
169*4882a593Smuzhiyun rxrpc_queue_call(call);
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun spin_unlock_bh(&call->lock);
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun /*
176*4882a593Smuzhiyun * Notify the owner of the call that the transmit phase is ended and the last
177*4882a593Smuzhiyun * packet has been queued.
178*4882a593Smuzhiyun */
rxrpc_notify_end_tx(struct rxrpc_sock * rx,struct rxrpc_call * call,rxrpc_notify_end_tx_t notify_end_tx)179*4882a593Smuzhiyun static void rxrpc_notify_end_tx(struct rxrpc_sock *rx, struct rxrpc_call *call,
180*4882a593Smuzhiyun rxrpc_notify_end_tx_t notify_end_tx)
181*4882a593Smuzhiyun {
182*4882a593Smuzhiyun if (notify_end_tx)
183*4882a593Smuzhiyun notify_end_tx(&rx->sk, call, call->user_call_ID);
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun /*
187*4882a593Smuzhiyun * Queue a DATA packet for transmission, set the resend timeout and send
188*4882a593Smuzhiyun * the packet immediately. Returns the error from rxrpc_send_data_packet()
189*4882a593Smuzhiyun * in case the caller wants to do something with it.
190*4882a593Smuzhiyun */
rxrpc_queue_packet(struct rxrpc_sock * rx,struct rxrpc_call * call,struct sk_buff * skb,bool last,rxrpc_notify_end_tx_t notify_end_tx)191*4882a593Smuzhiyun static int rxrpc_queue_packet(struct rxrpc_sock *rx, struct rxrpc_call *call,
192*4882a593Smuzhiyun struct sk_buff *skb, bool last,
193*4882a593Smuzhiyun rxrpc_notify_end_tx_t notify_end_tx)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
196*4882a593Smuzhiyun unsigned long now;
197*4882a593Smuzhiyun rxrpc_seq_t seq = sp->hdr.seq;
198*4882a593Smuzhiyun int ret, ix;
199*4882a593Smuzhiyun u8 annotation = RXRPC_TX_ANNO_UNACK;
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun _net("queue skb %p [%d]", skb, seq);
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun ASSERTCMP(seq, ==, call->tx_top + 1);
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun if (last)
206*4882a593Smuzhiyun annotation |= RXRPC_TX_ANNO_LAST;
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun /* We have to set the timestamp before queueing as the retransmit
209*4882a593Smuzhiyun * algorithm can see the packet as soon as we queue it.
210*4882a593Smuzhiyun */
211*4882a593Smuzhiyun skb->tstamp = ktime_get_real();
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun ix = seq & RXRPC_RXTX_BUFF_MASK;
214*4882a593Smuzhiyun rxrpc_get_skb(skb, rxrpc_skb_got);
215*4882a593Smuzhiyun call->rxtx_annotations[ix] = annotation;
216*4882a593Smuzhiyun smp_wmb();
217*4882a593Smuzhiyun call->rxtx_buffer[ix] = skb;
218*4882a593Smuzhiyun call->tx_top = seq;
219*4882a593Smuzhiyun if (last)
220*4882a593Smuzhiyun trace_rxrpc_transmit(call, rxrpc_transmit_queue_last);
221*4882a593Smuzhiyun else
222*4882a593Smuzhiyun trace_rxrpc_transmit(call, rxrpc_transmit_queue);
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) {
225*4882a593Smuzhiyun _debug("________awaiting reply/ACK__________");
226*4882a593Smuzhiyun write_lock_bh(&call->state_lock);
227*4882a593Smuzhiyun switch (call->state) {
228*4882a593Smuzhiyun case RXRPC_CALL_CLIENT_SEND_REQUEST:
229*4882a593Smuzhiyun call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
230*4882a593Smuzhiyun rxrpc_notify_end_tx(rx, call, notify_end_tx);
231*4882a593Smuzhiyun break;
232*4882a593Smuzhiyun case RXRPC_CALL_SERVER_ACK_REQUEST:
233*4882a593Smuzhiyun call->state = RXRPC_CALL_SERVER_SEND_REPLY;
234*4882a593Smuzhiyun now = jiffies;
235*4882a593Smuzhiyun WRITE_ONCE(call->ack_at, now + MAX_JIFFY_OFFSET);
236*4882a593Smuzhiyun if (call->ackr_reason == RXRPC_ACK_DELAY)
237*4882a593Smuzhiyun call->ackr_reason = 0;
238*4882a593Smuzhiyun trace_rxrpc_timer(call, rxrpc_timer_init_for_send_reply, now);
239*4882a593Smuzhiyun if (!last)
240*4882a593Smuzhiyun break;
241*4882a593Smuzhiyun fallthrough;
242*4882a593Smuzhiyun case RXRPC_CALL_SERVER_SEND_REPLY:
243*4882a593Smuzhiyun call->state = RXRPC_CALL_SERVER_AWAIT_ACK;
244*4882a593Smuzhiyun rxrpc_notify_end_tx(rx, call, notify_end_tx);
245*4882a593Smuzhiyun break;
246*4882a593Smuzhiyun default:
247*4882a593Smuzhiyun break;
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun write_unlock_bh(&call->state_lock);
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun if (seq == 1 && rxrpc_is_client_call(call))
253*4882a593Smuzhiyun rxrpc_expose_client_call(call);
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun ret = rxrpc_send_data_packet(call, skb, false);
256*4882a593Smuzhiyun if (ret < 0) {
257*4882a593Smuzhiyun switch (ret) {
258*4882a593Smuzhiyun case -ENETUNREACH:
259*4882a593Smuzhiyun case -EHOSTUNREACH:
260*4882a593Smuzhiyun case -ECONNREFUSED:
261*4882a593Smuzhiyun rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
262*4882a593Smuzhiyun 0, ret);
263*4882a593Smuzhiyun goto out;
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun _debug("need instant resend %d", ret);
266*4882a593Smuzhiyun rxrpc_instant_resend(call, ix);
267*4882a593Smuzhiyun } else {
268*4882a593Smuzhiyun unsigned long now = jiffies;
269*4882a593Smuzhiyun unsigned long resend_at = now + call->peer->rto_j;
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun WRITE_ONCE(call->resend_at, resend_at);
272*4882a593Smuzhiyun rxrpc_reduce_call_timer(call, resend_at, now,
273*4882a593Smuzhiyun rxrpc_timer_set_for_send);
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun out:
277*4882a593Smuzhiyun rxrpc_free_skb(skb, rxrpc_skb_freed);
278*4882a593Smuzhiyun _leave(" = %d", ret);
279*4882a593Smuzhiyun return ret;
280*4882a593Smuzhiyun }
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun /*
283*4882a593Smuzhiyun * send data through a socket
284*4882a593Smuzhiyun * - must be called in process context
285*4882a593Smuzhiyun * - The caller holds the call user access mutex, but not the socket lock.
286*4882a593Smuzhiyun */
rxrpc_send_data(struct rxrpc_sock * rx,struct rxrpc_call * call,struct msghdr * msg,size_t len,rxrpc_notify_end_tx_t notify_end_tx,bool * _dropped_lock)287*4882a593Smuzhiyun static int rxrpc_send_data(struct rxrpc_sock *rx,
288*4882a593Smuzhiyun struct rxrpc_call *call,
289*4882a593Smuzhiyun struct msghdr *msg, size_t len,
290*4882a593Smuzhiyun rxrpc_notify_end_tx_t notify_end_tx,
291*4882a593Smuzhiyun bool *_dropped_lock)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun struct rxrpc_skb_priv *sp;
294*4882a593Smuzhiyun struct sk_buff *skb;
295*4882a593Smuzhiyun struct sock *sk = &rx->sk;
296*4882a593Smuzhiyun enum rxrpc_call_state state;
297*4882a593Smuzhiyun long timeo;
298*4882a593Smuzhiyun bool more = msg->msg_flags & MSG_MORE;
299*4882a593Smuzhiyun int ret, copied = 0;
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun /* this should be in poll */
304*4882a593Smuzhiyun sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun reload:
307*4882a593Smuzhiyun ret = -EPIPE;
308*4882a593Smuzhiyun if (sk->sk_shutdown & SEND_SHUTDOWN)
309*4882a593Smuzhiyun goto maybe_error;
310*4882a593Smuzhiyun state = READ_ONCE(call->state);
311*4882a593Smuzhiyun ret = -ESHUTDOWN;
312*4882a593Smuzhiyun if (state >= RXRPC_CALL_COMPLETE)
313*4882a593Smuzhiyun goto maybe_error;
314*4882a593Smuzhiyun ret = -EPROTO;
315*4882a593Smuzhiyun if (state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
316*4882a593Smuzhiyun state != RXRPC_CALL_SERVER_ACK_REQUEST &&
317*4882a593Smuzhiyun state != RXRPC_CALL_SERVER_SEND_REPLY)
318*4882a593Smuzhiyun goto maybe_error;
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun ret = -EMSGSIZE;
321*4882a593Smuzhiyun if (call->tx_total_len != -1) {
322*4882a593Smuzhiyun if (len - copied > call->tx_total_len)
323*4882a593Smuzhiyun goto maybe_error;
324*4882a593Smuzhiyun if (!more && len - copied != call->tx_total_len)
325*4882a593Smuzhiyun goto maybe_error;
326*4882a593Smuzhiyun }
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun skb = call->tx_pending;
329*4882a593Smuzhiyun call->tx_pending = NULL;
330*4882a593Smuzhiyun rxrpc_see_skb(skb, rxrpc_skb_seen);
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun do {
333*4882a593Smuzhiyun /* Check to see if there's a ping ACK to reply to. */
334*4882a593Smuzhiyun if (call->ackr_reason == RXRPC_ACK_PING_RESPONSE)
335*4882a593Smuzhiyun rxrpc_send_ack_packet(call, false, NULL);
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun if (!skb) {
338*4882a593Smuzhiyun size_t size, chunk, max, space;
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun _debug("alloc");
341*4882a593Smuzhiyun
342*4882a593Smuzhiyun if (!rxrpc_check_tx_space(call, NULL))
343*4882a593Smuzhiyun goto wait_for_space;
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun max = RXRPC_JUMBO_DATALEN;
346*4882a593Smuzhiyun max -= call->conn->security_size;
347*4882a593Smuzhiyun max &= ~(call->conn->size_align - 1UL);
348*4882a593Smuzhiyun
349*4882a593Smuzhiyun chunk = max;
350*4882a593Smuzhiyun if (chunk > msg_data_left(msg) && !more)
351*4882a593Smuzhiyun chunk = msg_data_left(msg);
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun space = chunk + call->conn->size_align;
354*4882a593Smuzhiyun space &= ~(call->conn->size_align - 1UL);
355*4882a593Smuzhiyun
356*4882a593Smuzhiyun size = space + call->conn->security_size;
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun _debug("SIZE: %zu/%zu/%zu", chunk, space, size);
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun /* create a buffer that we can retain until it's ACK'd */
361*4882a593Smuzhiyun skb = sock_alloc_send_skb(
362*4882a593Smuzhiyun sk, size, msg->msg_flags & MSG_DONTWAIT, &ret);
363*4882a593Smuzhiyun if (!skb)
364*4882a593Smuzhiyun goto maybe_error;
365*4882a593Smuzhiyun
366*4882a593Smuzhiyun sp = rxrpc_skb(skb);
367*4882a593Smuzhiyun sp->rx_flags |= RXRPC_SKB_TX_BUFFER;
368*4882a593Smuzhiyun rxrpc_new_skb(skb, rxrpc_skb_new);
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun _debug("ALLOC SEND %p", skb);
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun ASSERTCMP(skb->mark, ==, 0);
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun _debug("HS: %u", call->conn->security_size);
375*4882a593Smuzhiyun skb_reserve(skb, call->conn->security_size);
376*4882a593Smuzhiyun skb->len += call->conn->security_size;
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun sp->remain = chunk;
379*4882a593Smuzhiyun if (sp->remain > skb_tailroom(skb))
380*4882a593Smuzhiyun sp->remain = skb_tailroom(skb);
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun _net("skb: hr %d, tr %d, hl %d, rm %d",
383*4882a593Smuzhiyun skb_headroom(skb),
384*4882a593Smuzhiyun skb_tailroom(skb),
385*4882a593Smuzhiyun skb_headlen(skb),
386*4882a593Smuzhiyun sp->remain);
387*4882a593Smuzhiyun
388*4882a593Smuzhiyun skb->ip_summed = CHECKSUM_UNNECESSARY;
389*4882a593Smuzhiyun }
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun _debug("append");
392*4882a593Smuzhiyun sp = rxrpc_skb(skb);
393*4882a593Smuzhiyun
394*4882a593Smuzhiyun /* append next segment of data to the current buffer */
395*4882a593Smuzhiyun if (msg_data_left(msg) > 0) {
396*4882a593Smuzhiyun int copy = skb_tailroom(skb);
397*4882a593Smuzhiyun ASSERTCMP(copy, >, 0);
398*4882a593Smuzhiyun if (copy > msg_data_left(msg))
399*4882a593Smuzhiyun copy = msg_data_left(msg);
400*4882a593Smuzhiyun if (copy > sp->remain)
401*4882a593Smuzhiyun copy = sp->remain;
402*4882a593Smuzhiyun
403*4882a593Smuzhiyun _debug("add");
404*4882a593Smuzhiyun ret = skb_add_data(skb, &msg->msg_iter, copy);
405*4882a593Smuzhiyun _debug("added");
406*4882a593Smuzhiyun if (ret < 0)
407*4882a593Smuzhiyun goto efault;
408*4882a593Smuzhiyun sp->remain -= copy;
409*4882a593Smuzhiyun skb->mark += copy;
410*4882a593Smuzhiyun copied += copy;
411*4882a593Smuzhiyun if (call->tx_total_len != -1)
412*4882a593Smuzhiyun call->tx_total_len -= copy;
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun /* check for the far side aborting the call or a network error
416*4882a593Smuzhiyun * occurring */
417*4882a593Smuzhiyun if (call->state == RXRPC_CALL_COMPLETE)
418*4882a593Smuzhiyun goto call_terminated;
419*4882a593Smuzhiyun
420*4882a593Smuzhiyun /* add the packet to the send queue if it's now full */
421*4882a593Smuzhiyun if (sp->remain <= 0 ||
422*4882a593Smuzhiyun (msg_data_left(msg) == 0 && !more)) {
423*4882a593Smuzhiyun struct rxrpc_connection *conn = call->conn;
424*4882a593Smuzhiyun uint32_t seq;
425*4882a593Smuzhiyun size_t pad;
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun /* pad out if we're using security */
428*4882a593Smuzhiyun if (conn->security_ix) {
429*4882a593Smuzhiyun pad = conn->security_size + skb->mark;
430*4882a593Smuzhiyun pad = conn->size_align - pad;
431*4882a593Smuzhiyun pad &= conn->size_align - 1;
432*4882a593Smuzhiyun _debug("pad %zu", pad);
433*4882a593Smuzhiyun if (pad)
434*4882a593Smuzhiyun skb_put_zero(skb, pad);
435*4882a593Smuzhiyun }
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun seq = call->tx_top + 1;
438*4882a593Smuzhiyun
439*4882a593Smuzhiyun sp->hdr.seq = seq;
440*4882a593Smuzhiyun sp->hdr._rsvd = 0;
441*4882a593Smuzhiyun sp->hdr.flags = conn->out_clientflag;
442*4882a593Smuzhiyun
443*4882a593Smuzhiyun if (msg_data_left(msg) == 0 && !more)
444*4882a593Smuzhiyun sp->hdr.flags |= RXRPC_LAST_PACKET;
445*4882a593Smuzhiyun else if (call->tx_top - call->tx_hard_ack <
446*4882a593Smuzhiyun call->tx_winsize)
447*4882a593Smuzhiyun sp->hdr.flags |= RXRPC_MORE_PACKETS;
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun ret = call->security->secure_packet(
450*4882a593Smuzhiyun call, skb, skb->mark, skb->head);
451*4882a593Smuzhiyun if (ret < 0)
452*4882a593Smuzhiyun goto out;
453*4882a593Smuzhiyun
454*4882a593Smuzhiyun ret = rxrpc_queue_packet(rx, call, skb,
455*4882a593Smuzhiyun !msg_data_left(msg) && !more,
456*4882a593Smuzhiyun notify_end_tx);
457*4882a593Smuzhiyun /* Should check for failure here */
458*4882a593Smuzhiyun skb = NULL;
459*4882a593Smuzhiyun }
460*4882a593Smuzhiyun } while (msg_data_left(msg) > 0);
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun success:
463*4882a593Smuzhiyun ret = copied;
464*4882a593Smuzhiyun if (READ_ONCE(call->state) == RXRPC_CALL_COMPLETE) {
465*4882a593Smuzhiyun read_lock_bh(&call->state_lock);
466*4882a593Smuzhiyun if (call->error < 0)
467*4882a593Smuzhiyun ret = call->error;
468*4882a593Smuzhiyun read_unlock_bh(&call->state_lock);
469*4882a593Smuzhiyun }
470*4882a593Smuzhiyun out:
471*4882a593Smuzhiyun call->tx_pending = skb;
472*4882a593Smuzhiyun _leave(" = %d", ret);
473*4882a593Smuzhiyun return ret;
474*4882a593Smuzhiyun
475*4882a593Smuzhiyun call_terminated:
476*4882a593Smuzhiyun rxrpc_free_skb(skb, rxrpc_skb_freed);
477*4882a593Smuzhiyun _leave(" = %d", call->error);
478*4882a593Smuzhiyun return call->error;
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun maybe_error:
481*4882a593Smuzhiyun if (copied)
482*4882a593Smuzhiyun goto success;
483*4882a593Smuzhiyun goto out;
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun efault:
486*4882a593Smuzhiyun ret = -EFAULT;
487*4882a593Smuzhiyun goto out;
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun wait_for_space:
490*4882a593Smuzhiyun ret = -EAGAIN;
491*4882a593Smuzhiyun if (msg->msg_flags & MSG_DONTWAIT)
492*4882a593Smuzhiyun goto maybe_error;
493*4882a593Smuzhiyun mutex_unlock(&call->user_mutex);
494*4882a593Smuzhiyun *_dropped_lock = true;
495*4882a593Smuzhiyun ret = rxrpc_wait_for_tx_window(rx, call, &timeo,
496*4882a593Smuzhiyun msg->msg_flags & MSG_WAITALL);
497*4882a593Smuzhiyun if (ret < 0)
498*4882a593Smuzhiyun goto maybe_error;
499*4882a593Smuzhiyun if (call->interruptibility == RXRPC_INTERRUPTIBLE) {
500*4882a593Smuzhiyun if (mutex_lock_interruptible(&call->user_mutex) < 0) {
501*4882a593Smuzhiyun ret = sock_intr_errno(timeo);
502*4882a593Smuzhiyun goto maybe_error;
503*4882a593Smuzhiyun }
504*4882a593Smuzhiyun } else {
505*4882a593Smuzhiyun mutex_lock(&call->user_mutex);
506*4882a593Smuzhiyun }
507*4882a593Smuzhiyun *_dropped_lock = false;
508*4882a593Smuzhiyun goto reload;
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun /*
512*4882a593Smuzhiyun * extract control messages from the sendmsg() control buffer
513*4882a593Smuzhiyun */
rxrpc_sendmsg_cmsg(struct msghdr * msg,struct rxrpc_send_params * p)514*4882a593Smuzhiyun static int rxrpc_sendmsg_cmsg(struct msghdr *msg, struct rxrpc_send_params *p)
515*4882a593Smuzhiyun {
516*4882a593Smuzhiyun struct cmsghdr *cmsg;
517*4882a593Smuzhiyun bool got_user_ID = false;
518*4882a593Smuzhiyun int len;
519*4882a593Smuzhiyun
520*4882a593Smuzhiyun if (msg->msg_controllen == 0)
521*4882a593Smuzhiyun return -EINVAL;
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun for_each_cmsghdr(cmsg, msg) {
524*4882a593Smuzhiyun if (!CMSG_OK(msg, cmsg))
525*4882a593Smuzhiyun return -EINVAL;
526*4882a593Smuzhiyun
527*4882a593Smuzhiyun len = cmsg->cmsg_len - sizeof(struct cmsghdr);
528*4882a593Smuzhiyun _debug("CMSG %d, %d, %d",
529*4882a593Smuzhiyun cmsg->cmsg_level, cmsg->cmsg_type, len);
530*4882a593Smuzhiyun
531*4882a593Smuzhiyun if (cmsg->cmsg_level != SOL_RXRPC)
532*4882a593Smuzhiyun continue;
533*4882a593Smuzhiyun
534*4882a593Smuzhiyun switch (cmsg->cmsg_type) {
535*4882a593Smuzhiyun case RXRPC_USER_CALL_ID:
536*4882a593Smuzhiyun if (msg->msg_flags & MSG_CMSG_COMPAT) {
537*4882a593Smuzhiyun if (len != sizeof(u32))
538*4882a593Smuzhiyun return -EINVAL;
539*4882a593Smuzhiyun p->call.user_call_ID = *(u32 *)CMSG_DATA(cmsg);
540*4882a593Smuzhiyun } else {
541*4882a593Smuzhiyun if (len != sizeof(unsigned long))
542*4882a593Smuzhiyun return -EINVAL;
543*4882a593Smuzhiyun p->call.user_call_ID = *(unsigned long *)
544*4882a593Smuzhiyun CMSG_DATA(cmsg);
545*4882a593Smuzhiyun }
546*4882a593Smuzhiyun got_user_ID = true;
547*4882a593Smuzhiyun break;
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun case RXRPC_ABORT:
550*4882a593Smuzhiyun if (p->command != RXRPC_CMD_SEND_DATA)
551*4882a593Smuzhiyun return -EINVAL;
552*4882a593Smuzhiyun p->command = RXRPC_CMD_SEND_ABORT;
553*4882a593Smuzhiyun if (len != sizeof(p->abort_code))
554*4882a593Smuzhiyun return -EINVAL;
555*4882a593Smuzhiyun p->abort_code = *(unsigned int *)CMSG_DATA(cmsg);
556*4882a593Smuzhiyun if (p->abort_code == 0)
557*4882a593Smuzhiyun return -EINVAL;
558*4882a593Smuzhiyun break;
559*4882a593Smuzhiyun
560*4882a593Smuzhiyun case RXRPC_CHARGE_ACCEPT:
561*4882a593Smuzhiyun if (p->command != RXRPC_CMD_SEND_DATA)
562*4882a593Smuzhiyun return -EINVAL;
563*4882a593Smuzhiyun p->command = RXRPC_CMD_CHARGE_ACCEPT;
564*4882a593Smuzhiyun if (len != 0)
565*4882a593Smuzhiyun return -EINVAL;
566*4882a593Smuzhiyun break;
567*4882a593Smuzhiyun
568*4882a593Smuzhiyun case RXRPC_EXCLUSIVE_CALL:
569*4882a593Smuzhiyun p->exclusive = true;
570*4882a593Smuzhiyun if (len != 0)
571*4882a593Smuzhiyun return -EINVAL;
572*4882a593Smuzhiyun break;
573*4882a593Smuzhiyun
574*4882a593Smuzhiyun case RXRPC_UPGRADE_SERVICE:
575*4882a593Smuzhiyun p->upgrade = true;
576*4882a593Smuzhiyun if (len != 0)
577*4882a593Smuzhiyun return -EINVAL;
578*4882a593Smuzhiyun break;
579*4882a593Smuzhiyun
580*4882a593Smuzhiyun case RXRPC_TX_LENGTH:
581*4882a593Smuzhiyun if (p->call.tx_total_len != -1 || len != sizeof(__s64))
582*4882a593Smuzhiyun return -EINVAL;
583*4882a593Smuzhiyun p->call.tx_total_len = *(__s64 *)CMSG_DATA(cmsg);
584*4882a593Smuzhiyun if (p->call.tx_total_len < 0)
585*4882a593Smuzhiyun return -EINVAL;
586*4882a593Smuzhiyun break;
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun case RXRPC_SET_CALL_TIMEOUT:
589*4882a593Smuzhiyun if (len & 3 || len < 4 || len > 12)
590*4882a593Smuzhiyun return -EINVAL;
591*4882a593Smuzhiyun memcpy(&p->call.timeouts, CMSG_DATA(cmsg), len);
592*4882a593Smuzhiyun p->call.nr_timeouts = len / 4;
593*4882a593Smuzhiyun if (p->call.timeouts.hard > INT_MAX / HZ)
594*4882a593Smuzhiyun return -ERANGE;
595*4882a593Smuzhiyun if (p->call.nr_timeouts >= 2 && p->call.timeouts.idle > 60 * 60 * 1000)
596*4882a593Smuzhiyun return -ERANGE;
597*4882a593Smuzhiyun if (p->call.nr_timeouts >= 3 && p->call.timeouts.normal > 60 * 60 * 1000)
598*4882a593Smuzhiyun return -ERANGE;
599*4882a593Smuzhiyun break;
600*4882a593Smuzhiyun
601*4882a593Smuzhiyun default:
602*4882a593Smuzhiyun return -EINVAL;
603*4882a593Smuzhiyun }
604*4882a593Smuzhiyun }
605*4882a593Smuzhiyun
606*4882a593Smuzhiyun if (!got_user_ID)
607*4882a593Smuzhiyun return -EINVAL;
608*4882a593Smuzhiyun if (p->call.tx_total_len != -1 && p->command != RXRPC_CMD_SEND_DATA)
609*4882a593Smuzhiyun return -EINVAL;
610*4882a593Smuzhiyun _leave(" = 0");
611*4882a593Smuzhiyun return 0;
612*4882a593Smuzhiyun }
613*4882a593Smuzhiyun
614*4882a593Smuzhiyun /*
615*4882a593Smuzhiyun * Create a new client call for sendmsg().
616*4882a593Smuzhiyun * - Called with the socket lock held, which it must release.
617*4882a593Smuzhiyun * - If it returns a call, the call's lock will need releasing by the caller.
618*4882a593Smuzhiyun */
619*4882a593Smuzhiyun static struct rxrpc_call *
rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock * rx,struct msghdr * msg,struct rxrpc_send_params * p)620*4882a593Smuzhiyun rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg,
621*4882a593Smuzhiyun struct rxrpc_send_params *p)
622*4882a593Smuzhiyun __releases(&rx->sk.sk_lock.slock)
623*4882a593Smuzhiyun __acquires(&call->user_mutex)
624*4882a593Smuzhiyun {
625*4882a593Smuzhiyun struct rxrpc_conn_parameters cp;
626*4882a593Smuzhiyun struct rxrpc_call *call;
627*4882a593Smuzhiyun struct key *key;
628*4882a593Smuzhiyun
629*4882a593Smuzhiyun DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx, msg->msg_name);
630*4882a593Smuzhiyun
631*4882a593Smuzhiyun _enter("");
632*4882a593Smuzhiyun
633*4882a593Smuzhiyun if (!msg->msg_name) {
634*4882a593Smuzhiyun release_sock(&rx->sk);
635*4882a593Smuzhiyun return ERR_PTR(-EDESTADDRREQ);
636*4882a593Smuzhiyun }
637*4882a593Smuzhiyun
638*4882a593Smuzhiyun key = rx->key;
639*4882a593Smuzhiyun if (key && !rx->key->payload.data[0])
640*4882a593Smuzhiyun key = NULL;
641*4882a593Smuzhiyun
642*4882a593Smuzhiyun memset(&cp, 0, sizeof(cp));
643*4882a593Smuzhiyun cp.local = rx->local;
644*4882a593Smuzhiyun cp.key = rx->key;
645*4882a593Smuzhiyun cp.security_level = rx->min_sec_level;
646*4882a593Smuzhiyun cp.exclusive = rx->exclusive | p->exclusive;
647*4882a593Smuzhiyun cp.upgrade = p->upgrade;
648*4882a593Smuzhiyun cp.service_id = srx->srx_service;
649*4882a593Smuzhiyun call = rxrpc_new_client_call(rx, &cp, srx, &p->call, GFP_KERNEL,
650*4882a593Smuzhiyun atomic_inc_return(&rxrpc_debug_id));
651*4882a593Smuzhiyun /* The socket is now unlocked */
652*4882a593Smuzhiyun
653*4882a593Smuzhiyun rxrpc_put_peer(cp.peer);
654*4882a593Smuzhiyun _leave(" = %p\n", call);
655*4882a593Smuzhiyun return call;
656*4882a593Smuzhiyun }
657*4882a593Smuzhiyun
658*4882a593Smuzhiyun /*
659*4882a593Smuzhiyun * send a message forming part of a client call through an RxRPC socket
660*4882a593Smuzhiyun * - caller holds the socket locked
661*4882a593Smuzhiyun * - the socket may be either a client socket or a server socket
662*4882a593Smuzhiyun */
rxrpc_do_sendmsg(struct rxrpc_sock * rx,struct msghdr * msg,size_t len)663*4882a593Smuzhiyun int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
664*4882a593Smuzhiyun __releases(&rx->sk.sk_lock.slock)
665*4882a593Smuzhiyun __releases(&call->user_mutex)
666*4882a593Smuzhiyun {
667*4882a593Smuzhiyun enum rxrpc_call_state state;
668*4882a593Smuzhiyun struct rxrpc_call *call;
669*4882a593Smuzhiyun unsigned long now, j;
670*4882a593Smuzhiyun bool dropped_lock = false;
671*4882a593Smuzhiyun int ret;
672*4882a593Smuzhiyun
673*4882a593Smuzhiyun struct rxrpc_send_params p = {
674*4882a593Smuzhiyun .call.tx_total_len = -1,
675*4882a593Smuzhiyun .call.user_call_ID = 0,
676*4882a593Smuzhiyun .call.nr_timeouts = 0,
677*4882a593Smuzhiyun .call.interruptibility = RXRPC_INTERRUPTIBLE,
678*4882a593Smuzhiyun .abort_code = 0,
679*4882a593Smuzhiyun .command = RXRPC_CMD_SEND_DATA,
680*4882a593Smuzhiyun .exclusive = false,
681*4882a593Smuzhiyun .upgrade = false,
682*4882a593Smuzhiyun };
683*4882a593Smuzhiyun
684*4882a593Smuzhiyun _enter("");
685*4882a593Smuzhiyun
686*4882a593Smuzhiyun ret = rxrpc_sendmsg_cmsg(msg, &p);
687*4882a593Smuzhiyun if (ret < 0)
688*4882a593Smuzhiyun goto error_release_sock;
689*4882a593Smuzhiyun
690*4882a593Smuzhiyun if (p.command == RXRPC_CMD_CHARGE_ACCEPT) {
691*4882a593Smuzhiyun ret = -EINVAL;
692*4882a593Smuzhiyun if (rx->sk.sk_state != RXRPC_SERVER_LISTENING)
693*4882a593Smuzhiyun goto error_release_sock;
694*4882a593Smuzhiyun ret = rxrpc_user_charge_accept(rx, p.call.user_call_ID);
695*4882a593Smuzhiyun goto error_release_sock;
696*4882a593Smuzhiyun }
697*4882a593Smuzhiyun
698*4882a593Smuzhiyun call = rxrpc_find_call_by_user_ID(rx, p.call.user_call_ID);
699*4882a593Smuzhiyun if (!call) {
700*4882a593Smuzhiyun ret = -EBADSLT;
701*4882a593Smuzhiyun if (p.command != RXRPC_CMD_SEND_DATA)
702*4882a593Smuzhiyun goto error_release_sock;
703*4882a593Smuzhiyun call = rxrpc_new_client_call_for_sendmsg(rx, msg, &p);
704*4882a593Smuzhiyun /* The socket is now unlocked... */
705*4882a593Smuzhiyun if (IS_ERR(call))
706*4882a593Smuzhiyun return PTR_ERR(call);
707*4882a593Smuzhiyun /* ... and we have the call lock. */
708*4882a593Smuzhiyun ret = 0;
709*4882a593Smuzhiyun if (READ_ONCE(call->state) == RXRPC_CALL_COMPLETE)
710*4882a593Smuzhiyun goto out_put_unlock;
711*4882a593Smuzhiyun } else {
712*4882a593Smuzhiyun switch (READ_ONCE(call->state)) {
713*4882a593Smuzhiyun case RXRPC_CALL_UNINITIALISED:
714*4882a593Smuzhiyun case RXRPC_CALL_CLIENT_AWAIT_CONN:
715*4882a593Smuzhiyun case RXRPC_CALL_SERVER_PREALLOC:
716*4882a593Smuzhiyun case RXRPC_CALL_SERVER_SECURING:
717*4882a593Smuzhiyun rxrpc_put_call(call, rxrpc_call_put);
718*4882a593Smuzhiyun ret = -EBUSY;
719*4882a593Smuzhiyun goto error_release_sock;
720*4882a593Smuzhiyun default:
721*4882a593Smuzhiyun break;
722*4882a593Smuzhiyun }
723*4882a593Smuzhiyun
724*4882a593Smuzhiyun ret = mutex_lock_interruptible(&call->user_mutex);
725*4882a593Smuzhiyun release_sock(&rx->sk);
726*4882a593Smuzhiyun if (ret < 0) {
727*4882a593Smuzhiyun ret = -ERESTARTSYS;
728*4882a593Smuzhiyun goto error_put;
729*4882a593Smuzhiyun }
730*4882a593Smuzhiyun
731*4882a593Smuzhiyun if (p.call.tx_total_len != -1) {
732*4882a593Smuzhiyun ret = -EINVAL;
733*4882a593Smuzhiyun if (call->tx_total_len != -1 ||
734*4882a593Smuzhiyun call->tx_pending ||
735*4882a593Smuzhiyun call->tx_top != 0)
736*4882a593Smuzhiyun goto error_put;
737*4882a593Smuzhiyun call->tx_total_len = p.call.tx_total_len;
738*4882a593Smuzhiyun }
739*4882a593Smuzhiyun }
740*4882a593Smuzhiyun
741*4882a593Smuzhiyun switch (p.call.nr_timeouts) {
742*4882a593Smuzhiyun case 3:
743*4882a593Smuzhiyun j = msecs_to_jiffies(p.call.timeouts.normal);
744*4882a593Smuzhiyun if (p.call.timeouts.normal > 0 && j == 0)
745*4882a593Smuzhiyun j = 1;
746*4882a593Smuzhiyun WRITE_ONCE(call->next_rx_timo, j);
747*4882a593Smuzhiyun fallthrough;
748*4882a593Smuzhiyun case 2:
749*4882a593Smuzhiyun j = msecs_to_jiffies(p.call.timeouts.idle);
750*4882a593Smuzhiyun if (p.call.timeouts.idle > 0 && j == 0)
751*4882a593Smuzhiyun j = 1;
752*4882a593Smuzhiyun WRITE_ONCE(call->next_req_timo, j);
753*4882a593Smuzhiyun fallthrough;
754*4882a593Smuzhiyun case 1:
755*4882a593Smuzhiyun if (p.call.timeouts.hard > 0) {
756*4882a593Smuzhiyun j = msecs_to_jiffies(p.call.timeouts.hard);
757*4882a593Smuzhiyun now = jiffies;
758*4882a593Smuzhiyun j += now;
759*4882a593Smuzhiyun WRITE_ONCE(call->expect_term_by, j);
760*4882a593Smuzhiyun rxrpc_reduce_call_timer(call, j, now,
761*4882a593Smuzhiyun rxrpc_timer_set_for_hard);
762*4882a593Smuzhiyun }
763*4882a593Smuzhiyun break;
764*4882a593Smuzhiyun }
765*4882a593Smuzhiyun
766*4882a593Smuzhiyun state = READ_ONCE(call->state);
767*4882a593Smuzhiyun _debug("CALL %d USR %lx ST %d on CONN %p",
768*4882a593Smuzhiyun call->debug_id, call->user_call_ID, state, call->conn);
769*4882a593Smuzhiyun
770*4882a593Smuzhiyun if (state >= RXRPC_CALL_COMPLETE) {
771*4882a593Smuzhiyun /* it's too late for this call */
772*4882a593Smuzhiyun ret = -ESHUTDOWN;
773*4882a593Smuzhiyun } else if (p.command == RXRPC_CMD_SEND_ABORT) {
774*4882a593Smuzhiyun ret = 0;
775*4882a593Smuzhiyun if (rxrpc_abort_call("CMD", call, 0, p.abort_code, -ECONNABORTED))
776*4882a593Smuzhiyun ret = rxrpc_send_abort_packet(call);
777*4882a593Smuzhiyun } else if (p.command != RXRPC_CMD_SEND_DATA) {
778*4882a593Smuzhiyun ret = -EINVAL;
779*4882a593Smuzhiyun } else {
780*4882a593Smuzhiyun ret = rxrpc_send_data(rx, call, msg, len, NULL, &dropped_lock);
781*4882a593Smuzhiyun }
782*4882a593Smuzhiyun
783*4882a593Smuzhiyun out_put_unlock:
784*4882a593Smuzhiyun if (!dropped_lock)
785*4882a593Smuzhiyun mutex_unlock(&call->user_mutex);
786*4882a593Smuzhiyun error_put:
787*4882a593Smuzhiyun rxrpc_put_call(call, rxrpc_call_put);
788*4882a593Smuzhiyun _leave(" = %d", ret);
789*4882a593Smuzhiyun return ret;
790*4882a593Smuzhiyun
791*4882a593Smuzhiyun error_release_sock:
792*4882a593Smuzhiyun release_sock(&rx->sk);
793*4882a593Smuzhiyun return ret;
794*4882a593Smuzhiyun }
795*4882a593Smuzhiyun
796*4882a593Smuzhiyun /**
797*4882a593Smuzhiyun * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
798*4882a593Smuzhiyun * @sock: The socket the call is on
799*4882a593Smuzhiyun * @call: The call to send data through
800*4882a593Smuzhiyun * @msg: The data to send
801*4882a593Smuzhiyun * @len: The amount of data to send
802*4882a593Smuzhiyun * @notify_end_tx: Notification that the last packet is queued.
803*4882a593Smuzhiyun *
804*4882a593Smuzhiyun * Allow a kernel service to send data on a call. The call must be in an state
805*4882a593Smuzhiyun * appropriate to sending data. No control data should be supplied in @msg,
806*4882a593Smuzhiyun * nor should an address be supplied. MSG_MORE should be flagged if there's
807*4882a593Smuzhiyun * more data to come, otherwise this data will end the transmission phase.
808*4882a593Smuzhiyun */
rxrpc_kernel_send_data(struct socket * sock,struct rxrpc_call * call,struct msghdr * msg,size_t len,rxrpc_notify_end_tx_t notify_end_tx)809*4882a593Smuzhiyun int rxrpc_kernel_send_data(struct socket *sock, struct rxrpc_call *call,
810*4882a593Smuzhiyun struct msghdr *msg, size_t len,
811*4882a593Smuzhiyun rxrpc_notify_end_tx_t notify_end_tx)
812*4882a593Smuzhiyun {
813*4882a593Smuzhiyun bool dropped_lock = false;
814*4882a593Smuzhiyun int ret;
815*4882a593Smuzhiyun
816*4882a593Smuzhiyun _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]);
817*4882a593Smuzhiyun
818*4882a593Smuzhiyun ASSERTCMP(msg->msg_name, ==, NULL);
819*4882a593Smuzhiyun ASSERTCMP(msg->msg_control, ==, NULL);
820*4882a593Smuzhiyun
821*4882a593Smuzhiyun mutex_lock(&call->user_mutex);
822*4882a593Smuzhiyun
823*4882a593Smuzhiyun _debug("CALL %d USR %lx ST %d on CONN %p",
824*4882a593Smuzhiyun call->debug_id, call->user_call_ID, call->state, call->conn);
825*4882a593Smuzhiyun
826*4882a593Smuzhiyun switch (READ_ONCE(call->state)) {
827*4882a593Smuzhiyun case RXRPC_CALL_CLIENT_SEND_REQUEST:
828*4882a593Smuzhiyun case RXRPC_CALL_SERVER_ACK_REQUEST:
829*4882a593Smuzhiyun case RXRPC_CALL_SERVER_SEND_REPLY:
830*4882a593Smuzhiyun ret = rxrpc_send_data(rxrpc_sk(sock->sk), call, msg, len,
831*4882a593Smuzhiyun notify_end_tx, &dropped_lock);
832*4882a593Smuzhiyun break;
833*4882a593Smuzhiyun case RXRPC_CALL_COMPLETE:
834*4882a593Smuzhiyun read_lock_bh(&call->state_lock);
835*4882a593Smuzhiyun ret = call->error;
836*4882a593Smuzhiyun read_unlock_bh(&call->state_lock);
837*4882a593Smuzhiyun break;
838*4882a593Smuzhiyun default:
839*4882a593Smuzhiyun /* Request phase complete for this client call */
840*4882a593Smuzhiyun trace_rxrpc_rx_eproto(call, 0, tracepoint_string("late_send"));
841*4882a593Smuzhiyun ret = -EPROTO;
842*4882a593Smuzhiyun break;
843*4882a593Smuzhiyun }
844*4882a593Smuzhiyun
845*4882a593Smuzhiyun if (!dropped_lock)
846*4882a593Smuzhiyun mutex_unlock(&call->user_mutex);
847*4882a593Smuzhiyun _leave(" = %d", ret);
848*4882a593Smuzhiyun return ret;
849*4882a593Smuzhiyun }
850*4882a593Smuzhiyun EXPORT_SYMBOL(rxrpc_kernel_send_data);
851*4882a593Smuzhiyun
852*4882a593Smuzhiyun /**
853*4882a593Smuzhiyun * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
854*4882a593Smuzhiyun * @sock: The socket the call is on
855*4882a593Smuzhiyun * @call: The call to be aborted
856*4882a593Smuzhiyun * @abort_code: The abort code to stick into the ABORT packet
857*4882a593Smuzhiyun * @error: Local error value
858*4882a593Smuzhiyun * @why: 3-char string indicating why.
859*4882a593Smuzhiyun *
860*4882a593Smuzhiyun * Allow a kernel service to abort a call, if it's still in an abortable state
861*4882a593Smuzhiyun * and return true if the call was aborted, false if it was already complete.
862*4882a593Smuzhiyun */
rxrpc_kernel_abort_call(struct socket * sock,struct rxrpc_call * call,u32 abort_code,int error,const char * why)863*4882a593Smuzhiyun bool rxrpc_kernel_abort_call(struct socket *sock, struct rxrpc_call *call,
864*4882a593Smuzhiyun u32 abort_code, int error, const char *why)
865*4882a593Smuzhiyun {
866*4882a593Smuzhiyun bool aborted;
867*4882a593Smuzhiyun
868*4882a593Smuzhiyun _enter("{%d},%d,%d,%s", call->debug_id, abort_code, error, why);
869*4882a593Smuzhiyun
870*4882a593Smuzhiyun mutex_lock(&call->user_mutex);
871*4882a593Smuzhiyun
872*4882a593Smuzhiyun aborted = rxrpc_abort_call(why, call, 0, abort_code, error);
873*4882a593Smuzhiyun if (aborted)
874*4882a593Smuzhiyun rxrpc_send_abort_packet(call);
875*4882a593Smuzhiyun
876*4882a593Smuzhiyun mutex_unlock(&call->user_mutex);
877*4882a593Smuzhiyun return aborted;
878*4882a593Smuzhiyun }
879*4882a593Smuzhiyun EXPORT_SYMBOL(rxrpc_kernel_abort_call);
880*4882a593Smuzhiyun
881*4882a593Smuzhiyun /**
882*4882a593Smuzhiyun * rxrpc_kernel_set_tx_length - Set the total Tx length on a call
883*4882a593Smuzhiyun * @sock: The socket the call is on
884*4882a593Smuzhiyun * @call: The call to be informed
885*4882a593Smuzhiyun * @tx_total_len: The amount of data to be transmitted for this call
886*4882a593Smuzhiyun *
887*4882a593Smuzhiyun * Allow a kernel service to set the total transmit length on a call. This
888*4882a593Smuzhiyun * allows buffer-to-packet encrypt-and-copy to be performed.
889*4882a593Smuzhiyun *
890*4882a593Smuzhiyun * This function is primarily for use for setting the reply length since the
891*4882a593Smuzhiyun * request length can be set when beginning the call.
892*4882a593Smuzhiyun */
rxrpc_kernel_set_tx_length(struct socket * sock,struct rxrpc_call * call,s64 tx_total_len)893*4882a593Smuzhiyun void rxrpc_kernel_set_tx_length(struct socket *sock, struct rxrpc_call *call,
894*4882a593Smuzhiyun s64 tx_total_len)
895*4882a593Smuzhiyun {
896*4882a593Smuzhiyun WARN_ON(call->tx_total_len != -1);
897*4882a593Smuzhiyun call->tx_total_len = tx_total_len;
898*4882a593Smuzhiyun }
899*4882a593Smuzhiyun EXPORT_SYMBOL(rxrpc_kernel_set_tx_length);
900