12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 28c3e34a4SDavid Howells /* RxRPC packet transmission 38c3e34a4SDavid Howells * 48c3e34a4SDavid Howells * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. 58c3e34a4SDavid Howells * Written by David Howells (dhowells@redhat.com) 68c3e34a4SDavid Howells */ 78c3e34a4SDavid Howells 88c3e34a4SDavid Howells #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 98c3e34a4SDavid Howells 108c3e34a4SDavid Howells #include <linux/net.h> 118c3e34a4SDavid Howells #include <linux/gfp.h> 128c3e34a4SDavid Howells #include <linux/skbuff.h> 138c3e34a4SDavid Howells #include <linux/export.h> 148c3e34a4SDavid Howells #include <net/sock.h> 158c3e34a4SDavid Howells #include <net/af_rxrpc.h> 168c3e34a4SDavid Howells #include "ar-internal.h" 178c3e34a4SDavid Howells 1826cb02aaSDavid Howells struct rxrpc_ack_buffer { 198d94aa38SDavid Howells struct rxrpc_wire_header whdr; 208d94aa38SDavid Howells struct rxrpc_ackpacket ack; 218d94aa38SDavid Howells u8 acks[255]; 228d94aa38SDavid Howells u8 pad[3]; 238d94aa38SDavid Howells struct rxrpc_ackinfo ackinfo; 248d94aa38SDavid Howells }; 258d94aa38SDavid Howells 2626cb02aaSDavid Howells struct rxrpc_abort_buffer { 2726cb02aaSDavid Howells struct rxrpc_wire_header whdr; 2826cb02aaSDavid Howells __be32 abort_code; 2926cb02aaSDavid Howells }; 3026cb02aaSDavid Howells 31ace45becSDavid Howells static const char rxrpc_keepalive_string[] = ""; 32ace45becSDavid Howells 338d94aa38SDavid Howells /* 34c7e86acfSDavid Howells * Increase Tx backoff on transmission failure and clear it on success. 35c7e86acfSDavid Howells */ 36c7e86acfSDavid Howells static void rxrpc_tx_backoff(struct rxrpc_call *call, int ret) 37c7e86acfSDavid Howells { 38c7e86acfSDavid Howells if (ret < 0) { 39c7e86acfSDavid Howells u16 tx_backoff = READ_ONCE(call->tx_backoff); 40c7e86acfSDavid Howells 41c7e86acfSDavid Howells if (tx_backoff < HZ) 42c7e86acfSDavid Howells WRITE_ONCE(call->tx_backoff, tx_backoff + 1); 43c7e86acfSDavid Howells } else { 44c7e86acfSDavid Howells WRITE_ONCE(call->tx_backoff, 0); 45c7e86acfSDavid Howells } 46c7e86acfSDavid Howells } 47c7e86acfSDavid Howells 48c7e86acfSDavid Howells /* 49415f44e4SDavid Howells * Arrange for a keepalive ping a certain time after we last transmitted. This 50415f44e4SDavid Howells * lets the far side know we're still interested in this call and helps keep 51415f44e4SDavid Howells * the route through any intervening firewall open. 52415f44e4SDavid Howells * 53415f44e4SDavid Howells * Receiving a response to the ping will prevent the ->expect_rx_by timer from 54415f44e4SDavid Howells * expiring. 55415f44e4SDavid Howells */ 56415f44e4SDavid Howells static void rxrpc_set_keepalive(struct rxrpc_call *call) 57415f44e4SDavid Howells { 58415f44e4SDavid Howells unsigned long now = jiffies, keepalive_at = call->next_rx_timo / 6; 59415f44e4SDavid Howells 60415f44e4SDavid Howells keepalive_at += now; 61415f44e4SDavid Howells WRITE_ONCE(call->keepalive_at, keepalive_at); 62415f44e4SDavid Howells rxrpc_reduce_call_timer(call, keepalive_at, now, 63415f44e4SDavid Howells rxrpc_timer_set_for_keepalive); 64415f44e4SDavid Howells } 65415f44e4SDavid Howells 66415f44e4SDavid Howells /* 678d94aa38SDavid Howells * Fill out an ACK packet. 688d94aa38SDavid Howells */ 691457cc4cSDavid Howells static size_t rxrpc_fill_out_ack(struct rxrpc_connection *conn, 701457cc4cSDavid Howells struct rxrpc_call *call, 7126cb02aaSDavid Howells struct rxrpc_ack_buffer *pkt, 72805b21b9SDavid Howells rxrpc_seq_t *_hard_ack, 73a5af7e1fSDavid Howells rxrpc_seq_t *_top, 74a5af7e1fSDavid Howells u8 reason) 758d94aa38SDavid Howells { 76f3639df2SDavid Howells rxrpc_serial_t serial; 779a3dedcfSDavid Howells unsigned int tmp; 78248f219cSDavid Howells rxrpc_seq_t hard_ack, top, seq; 79248f219cSDavid Howells int ix; 808d94aa38SDavid Howells u32 mtu, jmax; 818d94aa38SDavid Howells u8 *ackp = pkt->acks; 828d94aa38SDavid Howells 839a3dedcfSDavid Howells tmp = atomic_xchg(&call->ackr_nr_unacked, 0); 849a3dedcfSDavid Howells tmp |= atomic_xchg(&call->ackr_nr_consumed, 0); 859a3dedcfSDavid Howells if (!tmp && (reason == RXRPC_ACK_DELAY || 869a3dedcfSDavid Howells reason == RXRPC_ACK_IDLE)) 879a3dedcfSDavid Howells return 0; 889a3dedcfSDavid Howells 89248f219cSDavid Howells /* Barrier against rxrpc_input_data(). */ 90f3639df2SDavid Howells serial = call->ackr_serial; 91248f219cSDavid Howells hard_ack = READ_ONCE(call->rx_hard_ack); 92248f219cSDavid Howells top = smp_load_acquire(&call->rx_top); 93805b21b9SDavid Howells *_hard_ack = hard_ack; 94805b21b9SDavid Howells *_top = top; 95248f219cSDavid Howells 968d94aa38SDavid Howells pkt->ack.bufferSpace = htons(8); 97e8c3af6bSDavid Howells pkt->ack.maxSkew = htons(0); 98248f219cSDavid Howells pkt->ack.firstPacket = htonl(hard_ack + 1); 9981524b63SDavid Howells pkt->ack.previousPacket = htonl(call->ackr_highest_seq); 100f3639df2SDavid Howells pkt->ack.serial = htonl(serial); 101a5af7e1fSDavid Howells pkt->ack.reason = reason; 102248f219cSDavid Howells pkt->ack.nAcks = top - hard_ack; 1038d94aa38SDavid Howells 104a5af7e1fSDavid Howells if (reason == RXRPC_ACK_PING) 1058e83134dSDavid Howells pkt->whdr.flags |= RXRPC_REQUEST_ACK; 1068e83134dSDavid Howells 107248f219cSDavid Howells if (after(top, hard_ack)) { 108248f219cSDavid Howells seq = hard_ack + 1; 109248f219cSDavid Howells do { 110248f219cSDavid Howells ix = seq & RXRPC_RXTX_BUFF_MASK; 111248f219cSDavid Howells if (call->rxtx_buffer[ix]) 112248f219cSDavid Howells *ackp++ = RXRPC_ACK_TYPE_ACK; 113248f219cSDavid Howells else 114248f219cSDavid Howells *ackp++ = RXRPC_ACK_TYPE_NACK; 115248f219cSDavid Howells seq++; 116248f219cSDavid Howells } while (before_eq(seq, top)); 117248f219cSDavid Howells } 118248f219cSDavid Howells 1191457cc4cSDavid Howells mtu = conn->params.peer->if_mtu; 1201457cc4cSDavid Howells mtu -= conn->params.peer->hdrsize; 12175e42126SDavid Howells jmax = (call->nr_jumbo_bad > 3) ? 1 : rxrpc_rx_jumbo_max; 1228d94aa38SDavid Howells pkt->ackinfo.rxMTU = htonl(rxrpc_rx_mtu); 1238d94aa38SDavid Howells pkt->ackinfo.maxMTU = htonl(mtu); 12475e42126SDavid Howells pkt->ackinfo.rwind = htonl(call->rx_winsize); 1258d94aa38SDavid Howells pkt->ackinfo.jumbo_max = htonl(jmax); 1268d94aa38SDavid Howells 1278d94aa38SDavid Howells *ackp++ = 0; 1288d94aa38SDavid Howells *ackp++ = 0; 1298d94aa38SDavid Howells *ackp++ = 0; 130248f219cSDavid Howells return top - hard_ack + 3; 1318d94aa38SDavid Howells } 1328d94aa38SDavid Howells 1338d94aa38SDavid Howells /* 1344700c4d8SDavid Howells * Record the beginning of an RTT probe. 1354700c4d8SDavid Howells */ 1364700c4d8SDavid Howells static int rxrpc_begin_rtt_probe(struct rxrpc_call *call, rxrpc_serial_t serial, 1374700c4d8SDavid Howells enum rxrpc_rtt_tx_trace why) 1384700c4d8SDavid Howells { 1394700c4d8SDavid Howells unsigned long avail = call->rtt_avail; 1404700c4d8SDavid Howells int rtt_slot = 9; 1414700c4d8SDavid Howells 1424700c4d8SDavid Howells if (!(avail & RXRPC_CALL_RTT_AVAIL_MASK)) 1434700c4d8SDavid Howells goto no_slot; 1444700c4d8SDavid Howells 1454700c4d8SDavid Howells rtt_slot = __ffs(avail & RXRPC_CALL_RTT_AVAIL_MASK); 1464700c4d8SDavid Howells if (!test_and_clear_bit(rtt_slot, &call->rtt_avail)) 1474700c4d8SDavid Howells goto no_slot; 1484700c4d8SDavid Howells 1494700c4d8SDavid Howells call->rtt_serial[rtt_slot] = serial; 1504700c4d8SDavid Howells call->rtt_sent_at[rtt_slot] = ktime_get_real(); 1514700c4d8SDavid Howells smp_wmb(); /* Write data before avail bit */ 1524700c4d8SDavid Howells set_bit(rtt_slot + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail); 1534700c4d8SDavid Howells 1544700c4d8SDavid Howells trace_rxrpc_rtt_tx(call, why, rtt_slot, serial); 1554700c4d8SDavid Howells return rtt_slot; 1564700c4d8SDavid Howells 1574700c4d8SDavid Howells no_slot: 1584700c4d8SDavid Howells trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_no_slot, rtt_slot, serial); 1594700c4d8SDavid Howells return -1; 1604700c4d8SDavid Howells } 1614700c4d8SDavid Howells 1624700c4d8SDavid Howells /* 1634700c4d8SDavid Howells * Cancel an RTT probe. 1644700c4d8SDavid Howells */ 1654700c4d8SDavid Howells static void rxrpc_cancel_rtt_probe(struct rxrpc_call *call, 1664700c4d8SDavid Howells rxrpc_serial_t serial, int rtt_slot) 1674700c4d8SDavid Howells { 1684700c4d8SDavid Howells if (rtt_slot != -1) { 1694700c4d8SDavid Howells clear_bit(rtt_slot + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail); 1704700c4d8SDavid Howells smp_wmb(); /* Clear pending bit before setting slot */ 1714700c4d8SDavid Howells set_bit(rtt_slot, &call->rtt_avail); 1724700c4d8SDavid Howells trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_cancel, rtt_slot, serial); 1734700c4d8SDavid Howells } 1744700c4d8SDavid Howells } 1754700c4d8SDavid Howells 1764700c4d8SDavid Howells /* 17726cb02aaSDavid Howells * Send an ACK call packet. 1788d94aa38SDavid Howells */ 179bd1fdf8cSDavid Howells int rxrpc_send_ack_packet(struct rxrpc_call *call, bool ping, 180bd1fdf8cSDavid Howells rxrpc_serial_t *_serial) 1818d94aa38SDavid Howells { 1825273a191SDavid Howells struct rxrpc_connection *conn; 18326cb02aaSDavid Howells struct rxrpc_ack_buffer *pkt; 1848d94aa38SDavid Howells struct msghdr msg; 1858d94aa38SDavid Howells struct kvec iov[2]; 1868d94aa38SDavid Howells rxrpc_serial_t serial; 187805b21b9SDavid Howells rxrpc_seq_t hard_ack, top; 1888d94aa38SDavid Howells size_t len, n; 1894700c4d8SDavid Howells int ret, rtt_slot = -1; 190a5af7e1fSDavid Howells u8 reason; 1918d94aa38SDavid Howells 1925273a191SDavid Howells if (test_bit(RXRPC_CALL_DISCONNECTED, &call->flags)) 1938d94aa38SDavid Howells return -ECONNRESET; 1948d94aa38SDavid Howells 1958d94aa38SDavid Howells pkt = kzalloc(sizeof(*pkt), GFP_KERNEL); 1965273a191SDavid Howells if (!pkt) 1978d94aa38SDavid Howells return -ENOMEM; 1985273a191SDavid Howells 1995273a191SDavid Howells conn = call->conn; 2008d94aa38SDavid Howells 2018d94aa38SDavid Howells msg.msg_name = &call->peer->srx.transport; 2028d94aa38SDavid Howells msg.msg_namelen = call->peer->srx.transport_len; 2038d94aa38SDavid Howells msg.msg_control = NULL; 2048d94aa38SDavid Howells msg.msg_controllen = 0; 2058d94aa38SDavid Howells msg.msg_flags = 0; 2068d94aa38SDavid Howells 2078d94aa38SDavid Howells pkt->whdr.epoch = htonl(conn->proto.epoch); 2088d94aa38SDavid Howells pkt->whdr.cid = htonl(call->cid); 2098d94aa38SDavid Howells pkt->whdr.callNumber = htonl(call->call_id); 2108d94aa38SDavid Howells pkt->whdr.seq = 0; 21126cb02aaSDavid Howells pkt->whdr.type = RXRPC_PACKET_TYPE_ACK; 21226cb02aaSDavid Howells pkt->whdr.flags = RXRPC_SLOW_START_OK | conn->out_clientflag; 2138d94aa38SDavid Howells pkt->whdr.userStatus = 0; 2148d94aa38SDavid Howells pkt->whdr.securityIndex = call->security_ix; 2158d94aa38SDavid Howells pkt->whdr._rsvd = 0; 2168d94aa38SDavid Howells pkt->whdr.serviceId = htons(call->service_id); 2178d94aa38SDavid Howells 2188d94aa38SDavid Howells spin_lock_bh(&call->lock); 219a5af7e1fSDavid Howells if (ping) { 220a5af7e1fSDavid Howells reason = RXRPC_ACK_PING; 221a5af7e1fSDavid Howells } else { 222a5af7e1fSDavid Howells reason = call->ackr_reason; 22327d0fc43SDavid Howells if (!call->ackr_reason) { 22427d0fc43SDavid Howells spin_unlock_bh(&call->lock); 22527d0fc43SDavid Howells ret = 0; 22627d0fc43SDavid Howells goto out; 22727d0fc43SDavid Howells } 2288d94aa38SDavid Howells call->ackr_reason = 0; 229a5af7e1fSDavid Howells } 2301457cc4cSDavid Howells n = rxrpc_fill_out_ack(conn, call, pkt, &hard_ack, &top, reason); 2318d94aa38SDavid Howells 2328d94aa38SDavid Howells spin_unlock_bh(&call->lock); 2339a3dedcfSDavid Howells if (n == 0) { 2349a3dedcfSDavid Howells kfree(pkt); 2359a3dedcfSDavid Howells return 0; 2369a3dedcfSDavid Howells } 2378d94aa38SDavid Howells 23826cb02aaSDavid Howells iov[0].iov_base = pkt; 23926cb02aaSDavid Howells iov[0].iov_len = sizeof(pkt->whdr) + sizeof(pkt->ack) + n; 2408d94aa38SDavid Howells iov[1].iov_base = &pkt->ackinfo; 2418d94aa38SDavid Howells iov[1].iov_len = sizeof(pkt->ackinfo); 24226cb02aaSDavid Howells len = iov[0].iov_len + iov[1].iov_len; 2438d94aa38SDavid Howells 244b86e218eSDavid Howells serial = atomic_inc_return(&conn->serial); 245b86e218eSDavid Howells pkt->whdr.serial = htonl(serial); 2464764c0daSDavid Howells trace_rxrpc_tx_ack(call->debug_id, serial, 247b86e218eSDavid Howells ntohl(pkt->ack.firstPacket), 248b86e218eSDavid Howells ntohl(pkt->ack.serial), 249b86e218eSDavid Howells pkt->ack.reason, pkt->ack.nAcks); 250bd1fdf8cSDavid Howells if (_serial) 251bd1fdf8cSDavid Howells *_serial = serial; 252b86e218eSDavid Howells 2534700c4d8SDavid Howells if (ping) 2544700c4d8SDavid Howells rtt_slot = rxrpc_begin_rtt_probe(call, serial, rxrpc_rtt_tx_ping); 25526cb02aaSDavid Howells 25626cb02aaSDavid Howells ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len); 257330bdcfaSDavid Howells conn->params.peer->last_tx_at = ktime_get_seconds(); 2586b47fe1dSDavid Howells if (ret < 0) 2596b47fe1dSDavid Howells trace_rxrpc_tx_fail(call->debug_id, serial, ret, 2604764c0daSDavid Howells rxrpc_tx_point_call_ack); 2614764c0daSDavid Howells else 2624764c0daSDavid Howells trace_rxrpc_tx_packet(call->debug_id, &pkt->whdr, 2634764c0daSDavid Howells rxrpc_tx_point_call_ack); 264c7e86acfSDavid Howells rxrpc_tx_backoff(call, ret); 2658d94aa38SDavid Howells 26626cb02aaSDavid Howells if (call->state < RXRPC_CALL_COMPLETE) { 267805b21b9SDavid Howells if (ret < 0) { 2684700c4d8SDavid Howells rxrpc_cancel_rtt_probe(call, serial, rtt_slot); 269248f219cSDavid Howells rxrpc_propose_ACK(call, pkt->ack.reason, 270248f219cSDavid Howells ntohl(pkt->ack.serial), 271c7e86acfSDavid Howells false, true, 2729c7ad434SDavid Howells rxrpc_propose_ack_retry_tx); 273248f219cSDavid Howells } 274415f44e4SDavid Howells 275415f44e4SDavid Howells rxrpc_set_keepalive(call); 276248f219cSDavid Howells } 277248f219cSDavid Howells 2788d94aa38SDavid Howells out: 2798d94aa38SDavid Howells kfree(pkt); 2808d94aa38SDavid Howells return ret; 2818d94aa38SDavid Howells } 2828d94aa38SDavid Howells 2838c3e34a4SDavid Howells /* 28426cb02aaSDavid Howells * Send an ABORT call packet. 28526cb02aaSDavid Howells */ 28626cb02aaSDavid Howells int rxrpc_send_abort_packet(struct rxrpc_call *call) 28726cb02aaSDavid Howells { 2885273a191SDavid Howells struct rxrpc_connection *conn; 28926cb02aaSDavid Howells struct rxrpc_abort_buffer pkt; 29026cb02aaSDavid Howells struct msghdr msg; 29126cb02aaSDavid Howells struct kvec iov[1]; 29226cb02aaSDavid Howells rxrpc_serial_t serial; 29326cb02aaSDavid Howells int ret; 29426cb02aaSDavid Howells 295dcbefc30SDavid Howells /* Don't bother sending aborts for a client call once the server has 296dcbefc30SDavid Howells * hard-ACK'd all of its request data. After that point, we're not 297dcbefc30SDavid Howells * going to stop the operation proceeding, and whilst we might limit 298dcbefc30SDavid Howells * the reply, it's not worth it if we can send a new call on the same 299dcbefc30SDavid Howells * channel instead, thereby closing off this call. 300dcbefc30SDavid Howells */ 301dcbefc30SDavid Howells if (rxrpc_is_client_call(call) && 302dcbefc30SDavid Howells test_bit(RXRPC_CALL_TX_LAST, &call->flags)) 303dcbefc30SDavid Howells return 0; 304dcbefc30SDavid Howells 3055273a191SDavid Howells if (test_bit(RXRPC_CALL_DISCONNECTED, &call->flags)) 30626cb02aaSDavid Howells return -ECONNRESET; 30726cb02aaSDavid Howells 3085273a191SDavid Howells conn = call->conn; 3095273a191SDavid Howells 31026cb02aaSDavid Howells msg.msg_name = &call->peer->srx.transport; 31126cb02aaSDavid Howells msg.msg_namelen = call->peer->srx.transport_len; 31226cb02aaSDavid Howells msg.msg_control = NULL; 31326cb02aaSDavid Howells msg.msg_controllen = 0; 31426cb02aaSDavid Howells msg.msg_flags = 0; 31526cb02aaSDavid Howells 31626cb02aaSDavid Howells pkt.whdr.epoch = htonl(conn->proto.epoch); 31726cb02aaSDavid Howells pkt.whdr.cid = htonl(call->cid); 31826cb02aaSDavid Howells pkt.whdr.callNumber = htonl(call->call_id); 31926cb02aaSDavid Howells pkt.whdr.seq = 0; 32026cb02aaSDavid Howells pkt.whdr.type = RXRPC_PACKET_TYPE_ABORT; 32126cb02aaSDavid Howells pkt.whdr.flags = conn->out_clientflag; 32226cb02aaSDavid Howells pkt.whdr.userStatus = 0; 32326cb02aaSDavid Howells pkt.whdr.securityIndex = call->security_ix; 32426cb02aaSDavid Howells pkt.whdr._rsvd = 0; 32526cb02aaSDavid Howells pkt.whdr.serviceId = htons(call->service_id); 32626cb02aaSDavid Howells pkt.abort_code = htonl(call->abort_code); 32726cb02aaSDavid Howells 32826cb02aaSDavid Howells iov[0].iov_base = &pkt; 32926cb02aaSDavid Howells iov[0].iov_len = sizeof(pkt); 33026cb02aaSDavid Howells 33126cb02aaSDavid Howells serial = atomic_inc_return(&conn->serial); 33226cb02aaSDavid Howells pkt.whdr.serial = htonl(serial); 33326cb02aaSDavid Howells 33426cb02aaSDavid Howells ret = kernel_sendmsg(conn->params.local->socket, 33526cb02aaSDavid Howells &msg, iov, 1, sizeof(pkt)); 336330bdcfaSDavid Howells conn->params.peer->last_tx_at = ktime_get_seconds(); 3376b47fe1dSDavid Howells if (ret < 0) 3386b47fe1dSDavid Howells trace_rxrpc_tx_fail(call->debug_id, serial, ret, 3394764c0daSDavid Howells rxrpc_tx_point_call_abort); 3404764c0daSDavid Howells else 3414764c0daSDavid Howells trace_rxrpc_tx_packet(call->debug_id, &pkt.whdr, 3424764c0daSDavid Howells rxrpc_tx_point_call_abort); 343c7e86acfSDavid Howells rxrpc_tx_backoff(call, ret); 34426cb02aaSDavid Howells return ret; 34526cb02aaSDavid Howells } 34626cb02aaSDavid Howells 34726cb02aaSDavid Howells /* 3488c3e34a4SDavid Howells * send a packet through the transport endpoint 3498c3e34a4SDavid Howells */ 350a1767077SDavid Howells int rxrpc_send_data_packet(struct rxrpc_call *call, struct sk_buff *skb, 351a1767077SDavid Howells bool retrans) 3528c3e34a4SDavid Howells { 353*4d843be5SDavid Howells enum rxrpc_req_ack_trace why; 3545a924b89SDavid Howells struct rxrpc_connection *conn = call->conn; 3555a924b89SDavid Howells struct rxrpc_wire_header whdr; 3565a924b89SDavid Howells struct rxrpc_skb_priv *sp = rxrpc_skb(skb); 3578c3e34a4SDavid Howells struct msghdr msg; 3585a924b89SDavid Howells struct kvec iov[2]; 3595a924b89SDavid Howells rxrpc_serial_t serial; 3605a924b89SDavid Howells size_t len; 3614700c4d8SDavid Howells int ret, rtt_slot = -1; 3628c3e34a4SDavid Howells 3638c3e34a4SDavid Howells _enter(",{%d}", skb->len); 3648c3e34a4SDavid Howells 365245500d8SDavid Howells if (hlist_unhashed(&call->error_link)) { 366245500d8SDavid Howells spin_lock_bh(&call->peer->lock); 367245500d8SDavid Howells hlist_add_head_rcu(&call->error_link, &call->peer->error_targets); 368245500d8SDavid Howells spin_unlock_bh(&call->peer->lock); 369245500d8SDavid Howells } 370245500d8SDavid Howells 3715a924b89SDavid Howells /* Each transmission of a Tx packet needs a new serial number */ 3725a924b89SDavid Howells serial = atomic_inc_return(&conn->serial); 3738c3e34a4SDavid Howells 3745a924b89SDavid Howells whdr.epoch = htonl(conn->proto.epoch); 3755a924b89SDavid Howells whdr.cid = htonl(call->cid); 3765a924b89SDavid Howells whdr.callNumber = htonl(call->call_id); 3775a924b89SDavid Howells whdr.seq = htonl(sp->hdr.seq); 3785a924b89SDavid Howells whdr.serial = htonl(serial); 3795a924b89SDavid Howells whdr.type = RXRPC_PACKET_TYPE_DATA; 3805a924b89SDavid Howells whdr.flags = sp->hdr.flags; 3815a924b89SDavid Howells whdr.userStatus = 0; 3825a924b89SDavid Howells whdr.securityIndex = call->security_ix; 3835a924b89SDavid Howells whdr._rsvd = htons(sp->hdr._rsvd); 3845a924b89SDavid Howells whdr.serviceId = htons(call->service_id); 3855a924b89SDavid Howells 3864e255721SDavid Howells if (test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags) && 3874e255721SDavid Howells sp->hdr.seq == 1) 3884e255721SDavid Howells whdr.userStatus = RXRPC_USERSTATUS_SERVICE_UPGRADE; 3894e255721SDavid Howells 3905a924b89SDavid Howells iov[0].iov_base = &whdr; 3915a924b89SDavid Howells iov[0].iov_len = sizeof(whdr); 3925a924b89SDavid Howells iov[1].iov_base = skb->head; 3935a924b89SDavid Howells iov[1].iov_len = skb->len; 3945a924b89SDavid Howells len = iov[0].iov_len + iov[1].iov_len; 3955a924b89SDavid Howells 3965a924b89SDavid Howells msg.msg_name = &call->peer->srx.transport; 3975a924b89SDavid Howells msg.msg_namelen = call->peer->srx.transport_len; 3988c3e34a4SDavid Howells msg.msg_control = NULL; 3998c3e34a4SDavid Howells msg.msg_controllen = 0; 4008c3e34a4SDavid Howells msg.msg_flags = 0; 4018c3e34a4SDavid Howells 40257494343SDavid Howells /* If our RTT cache needs working on, request an ACK. Also request 40357494343SDavid Howells * ACKs if a DATA packet appears to have been lost. 404b604dd98SDavid Howells * 405b604dd98SDavid Howells * However, we mustn't request an ACK on the last reply packet of a 406b604dd98SDavid Howells * service call, lest OpenAFS incorrectly send us an ACK with some 407b604dd98SDavid Howells * soft-ACKs in it and then never follow up with a proper hard ACK. 40857494343SDavid Howells */ 409*4d843be5SDavid Howells if (whdr.flags & RXRPC_REQUEST_ACK) 410*4d843be5SDavid Howells why = rxrpc_reqack_already_on; 411*4d843be5SDavid Howells else if ((whdr.flags & RXRPC_LAST_PACKET) && rxrpc_to_client(sp)) 412*4d843be5SDavid Howells why = rxrpc_reqack_no_srv_last; 413*4d843be5SDavid Howells else if (test_and_clear_bit(RXRPC_CALL_EV_ACK_LOST, &call->events)) 414*4d843be5SDavid Howells why = rxrpc_reqack_ack_lost; 415*4d843be5SDavid Howells else if (retrans) 416*4d843be5SDavid Howells why = rxrpc_reqack_retrans; 417*4d843be5SDavid Howells else if (call->cong_mode == RXRPC_CALL_SLOW_START && call->cong_cwnd <= 2) 418*4d843be5SDavid Howells why = rxrpc_reqack_slow_start; 419*4d843be5SDavid Howells else if (call->tx_winsize <= 2) 420*4d843be5SDavid Howells why = rxrpc_reqack_small_txwin; 421*4d843be5SDavid Howells else if (call->peer->rtt_count < 3 && sp->hdr.seq & 1) 422*4d843be5SDavid Howells why = rxrpc_reqack_more_rtt; 423*4d843be5SDavid Howells else if (ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000), ktime_get_real())) 424*4d843be5SDavid Howells why = rxrpc_reqack_old_rtt; 425*4d843be5SDavid Howells else 426*4d843be5SDavid Howells goto dont_set_request_ack; 427*4d843be5SDavid Howells 428*4d843be5SDavid Howells trace_rxrpc_req_ack(call->debug_id, sp->hdr.seq, why); 429*4d843be5SDavid Howells if (why != rxrpc_reqack_no_srv_last) 4300d4b103cSDavid Howells whdr.flags |= RXRPC_REQUEST_ACK; 431*4d843be5SDavid Howells dont_set_request_ack: 4320d4b103cSDavid Howells 4338a681c36SDavid Howells if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) { 4348a681c36SDavid Howells static int lose; 4358a681c36SDavid Howells if ((lose++ & 7) == 7) { 436a1767077SDavid Howells ret = 0; 437526949e8SArnd Bergmann trace_rxrpc_tx_data(call, sp->hdr.seq, serial, 438526949e8SArnd Bergmann whdr.flags, retrans, true); 439526949e8SArnd Bergmann goto done; 4408a681c36SDavid Howells } 4418a681c36SDavid Howells } 4428a681c36SDavid Howells 443526949e8SArnd Bergmann trace_rxrpc_tx_data(call, sp->hdr.seq, serial, whdr.flags, retrans, 444526949e8SArnd Bergmann false); 4455a924b89SDavid Howells 4468c3e34a4SDavid Howells /* send the packet with the don't fragment bit set if we currently 4478c3e34a4SDavid Howells * think it's small enough */ 4485a924b89SDavid Howells if (iov[1].iov_len >= call->peer->maxdata) 4495a924b89SDavid Howells goto send_fragmentable; 4505a924b89SDavid Howells 451985a5c82SDavid Howells down_read(&conn->params.local->defrag_sem); 452b604dd98SDavid Howells 453b604dd98SDavid Howells sp->hdr.serial = serial; 454b604dd98SDavid Howells smp_wmb(); /* Set serial before timestamp */ 455b604dd98SDavid Howells skb->tstamp = ktime_get_real(); 4564700c4d8SDavid Howells if (whdr.flags & RXRPC_REQUEST_ACK) 4574700c4d8SDavid Howells rtt_slot = rxrpc_begin_rtt_probe(call, serial, rxrpc_rtt_tx_data); 458b604dd98SDavid Howells 4598c3e34a4SDavid Howells /* send the packet by UDP 4608c3e34a4SDavid Howells * - returns -EMSGSIZE if UDP would have to fragment the packet 4618c3e34a4SDavid Howells * to go out of the interface 4628c3e34a4SDavid Howells * - in which case, we'll have processed the ICMP error 4638c3e34a4SDavid Howells * message and update the peer record 4648c3e34a4SDavid Howells */ 4655a924b89SDavid Howells ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len); 466330bdcfaSDavid Howells conn->params.peer->last_tx_at = ktime_get_seconds(); 4678c3e34a4SDavid Howells 468985a5c82SDavid Howells up_read(&conn->params.local->defrag_sem); 4694700c4d8SDavid Howells if (ret < 0) { 4704700c4d8SDavid Howells rxrpc_cancel_rtt_probe(call, serial, rtt_slot); 4716b47fe1dSDavid Howells trace_rxrpc_tx_fail(call->debug_id, serial, ret, 4724764c0daSDavid Howells rxrpc_tx_point_call_data_nofrag); 4734700c4d8SDavid Howells } else { 4744764c0daSDavid Howells trace_rxrpc_tx_packet(call->debug_id, &whdr, 4754764c0daSDavid Howells rxrpc_tx_point_call_data_nofrag); 4764700c4d8SDavid Howells } 4774700c4d8SDavid Howells 478c7e86acfSDavid Howells rxrpc_tx_backoff(call, ret); 4798c3e34a4SDavid Howells if (ret == -EMSGSIZE) 4808c3e34a4SDavid Howells goto send_fragmentable; 4818c3e34a4SDavid Howells 4825a924b89SDavid Howells done: 48350235c4bSDavid Howells if (ret >= 0) { 4840d4b103cSDavid Howells if (whdr.flags & RXRPC_REQUEST_ACK) { 485b604dd98SDavid Howells call->peer->rtt_last_req = skb->tstamp; 486c410bf01SDavid Howells if (call->peer->rtt_count > 1) { 487bd1fdf8cSDavid Howells unsigned long nowj = jiffies, ack_lost_at; 488bd1fdf8cSDavid Howells 4892c13c05cSDavid Howells ack_lost_at = rxrpc_get_rto_backoff(call->peer, false); 490bd1fdf8cSDavid Howells ack_lost_at += nowj; 491bd1fdf8cSDavid Howells WRITE_ONCE(call->ack_lost_at, ack_lost_at); 492bd1fdf8cSDavid Howells rxrpc_reduce_call_timer(call, ack_lost_at, nowj, 493bd1fdf8cSDavid Howells rxrpc_timer_set_for_lost_ack); 494bd1fdf8cSDavid Howells } 4958c3e34a4SDavid Howells } 496c54e43d7SDavid Howells 497c54e43d7SDavid Howells if (sp->hdr.seq == 1 && 498c54e43d7SDavid Howells !test_and_set_bit(RXRPC_CALL_BEGAN_RX_TIMER, 499c54e43d7SDavid Howells &call->flags)) { 500c54e43d7SDavid Howells unsigned long nowj = jiffies, expect_rx_by; 501c54e43d7SDavid Howells 502c54e43d7SDavid Howells expect_rx_by = nowj + call->next_rx_timo; 503c54e43d7SDavid Howells WRITE_ONCE(call->expect_rx_by, expect_rx_by); 504c54e43d7SDavid Howells rxrpc_reduce_call_timer(call, expect_rx_by, nowj, 505c54e43d7SDavid Howells rxrpc_timer_set_for_normal); 506c54e43d7SDavid Howells } 507415f44e4SDavid Howells 508415f44e4SDavid Howells rxrpc_set_keepalive(call); 509c7e86acfSDavid Howells } else { 510c7e86acfSDavid Howells /* Cancel the call if the initial transmission fails, 511c7e86acfSDavid Howells * particularly if that's due to network routing issues that 512c7e86acfSDavid Howells * aren't going away anytime soon. The layer above can arrange 513c7e86acfSDavid Howells * the retransmission. 514c7e86acfSDavid Howells */ 515c7e86acfSDavid Howells if (!test_and_set_bit(RXRPC_CALL_BEGAN_RX_TIMER, &call->flags)) 516c7e86acfSDavid Howells rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR, 517c7e86acfSDavid Howells RX_USER_ABORT, ret); 518c7e86acfSDavid Howells } 519415f44e4SDavid Howells 5205a924b89SDavid Howells _leave(" = %d [%u]", ret, call->peer->maxdata); 5215a924b89SDavid Howells return ret; 5228c3e34a4SDavid Howells 5238c3e34a4SDavid Howells send_fragmentable: 5248c3e34a4SDavid Howells /* attempt to send this message with fragmentation enabled */ 5258c3e34a4SDavid Howells _debug("send fragment"); 5268c3e34a4SDavid Howells 527985a5c82SDavid Howells down_write(&conn->params.local->defrag_sem); 528985a5c82SDavid Howells 529b604dd98SDavid Howells sp->hdr.serial = serial; 530b604dd98SDavid Howells smp_wmb(); /* Set serial before timestamp */ 531b604dd98SDavid Howells skb->tstamp = ktime_get_real(); 5324700c4d8SDavid Howells if (whdr.flags & RXRPC_REQUEST_ACK) 5334700c4d8SDavid Howells rtt_slot = rxrpc_begin_rtt_probe(call, serial, rxrpc_rtt_tx_data); 534b604dd98SDavid Howells 535985a5c82SDavid Howells switch (conn->params.local->srx.transport.family) { 5360e631eeeSDavid Howells case AF_INET6: 537985a5c82SDavid Howells case AF_INET: 5382de569bdSChristoph Hellwig ip_sock_set_mtu_discover(conn->params.local->socket->sk, 5392de569bdSChristoph Hellwig IP_PMTUDISC_DONT); 5405a924b89SDavid Howells ret = kernel_sendmsg(conn->params.local->socket, &msg, 5415a924b89SDavid Howells iov, 2, len); 542330bdcfaSDavid Howells conn->params.peer->last_tx_at = ktime_get_seconds(); 5438c3e34a4SDavid Howells 5442de569bdSChristoph Hellwig ip_sock_set_mtu_discover(conn->params.local->socket->sk, 5452de569bdSChristoph Hellwig IP_PMTUDISC_DO); 54675b54cb5SDavid Howells break; 5473427beb6SDavid Howells 5483427beb6SDavid Howells default: 5493427beb6SDavid Howells BUG(); 5508c3e34a4SDavid Howells } 5518c3e34a4SDavid Howells 5524700c4d8SDavid Howells if (ret < 0) { 5534700c4d8SDavid Howells rxrpc_cancel_rtt_probe(call, serial, rtt_slot); 5546b47fe1dSDavid Howells trace_rxrpc_tx_fail(call->debug_id, serial, ret, 5554764c0daSDavid Howells rxrpc_tx_point_call_data_frag); 5564700c4d8SDavid Howells } else { 5574764c0daSDavid Howells trace_rxrpc_tx_packet(call->debug_id, &whdr, 5584764c0daSDavid Howells rxrpc_tx_point_call_data_frag); 5594700c4d8SDavid Howells } 560c7e86acfSDavid Howells rxrpc_tx_backoff(call, ret); 5616b47fe1dSDavid Howells 562985a5c82SDavid Howells up_write(&conn->params.local->defrag_sem); 5635a924b89SDavid Howells goto done; 5648c3e34a4SDavid Howells } 565248f219cSDavid Howells 566248f219cSDavid Howells /* 567248f219cSDavid Howells * reject packets through the local endpoint 568248f219cSDavid Howells */ 569248f219cSDavid Howells void rxrpc_reject_packets(struct rxrpc_local *local) 570248f219cSDavid Howells { 5711c2bc7b9SDavid Howells struct sockaddr_rxrpc srx; 572248f219cSDavid Howells struct rxrpc_skb_priv *sp; 573248f219cSDavid Howells struct rxrpc_wire_header whdr; 574248f219cSDavid Howells struct sk_buff *skb; 575248f219cSDavid Howells struct msghdr msg; 576248f219cSDavid Howells struct kvec iov[2]; 577248f219cSDavid Howells size_t size; 578248f219cSDavid Howells __be32 code; 579ece64fecSDavid Howells int ret, ioc; 580248f219cSDavid Howells 581248f219cSDavid Howells _enter("%d", local->debug_id); 582248f219cSDavid Howells 583248f219cSDavid Howells iov[0].iov_base = &whdr; 584248f219cSDavid Howells iov[0].iov_len = sizeof(whdr); 585248f219cSDavid Howells iov[1].iov_base = &code; 586248f219cSDavid Howells iov[1].iov_len = sizeof(code); 587248f219cSDavid Howells 5881c2bc7b9SDavid Howells msg.msg_name = &srx.transport; 589248f219cSDavid Howells msg.msg_control = NULL; 590248f219cSDavid Howells msg.msg_controllen = 0; 591248f219cSDavid Howells msg.msg_flags = 0; 592248f219cSDavid Howells 593248f219cSDavid Howells memset(&whdr, 0, sizeof(whdr)); 594248f219cSDavid Howells 595248f219cSDavid Howells while ((skb = skb_dequeue(&local->reject_queue))) { 596987db9f7SDavid Howells rxrpc_see_skb(skb, rxrpc_skb_seen); 597248f219cSDavid Howells sp = rxrpc_skb(skb); 5981c2bc7b9SDavid Howells 599ece64fecSDavid Howells switch (skb->mark) { 600ece64fecSDavid Howells case RXRPC_SKB_MARK_REJECT_BUSY: 601ece64fecSDavid Howells whdr.type = RXRPC_PACKET_TYPE_BUSY; 602ece64fecSDavid Howells size = sizeof(whdr); 603ece64fecSDavid Howells ioc = 1; 604ece64fecSDavid Howells break; 605ece64fecSDavid Howells case RXRPC_SKB_MARK_REJECT_ABORT: 606ece64fecSDavid Howells whdr.type = RXRPC_PACKET_TYPE_ABORT; 607ece64fecSDavid Howells code = htonl(skb->priority); 608ece64fecSDavid Howells size = sizeof(whdr) + sizeof(code); 609ece64fecSDavid Howells ioc = 2; 610ece64fecSDavid Howells break; 611ece64fecSDavid Howells default: 612987db9f7SDavid Howells rxrpc_free_skb(skb, rxrpc_skb_freed); 613ece64fecSDavid Howells continue; 614ece64fecSDavid Howells } 615ece64fecSDavid Howells 6165a790b73SDavid Howells if (rxrpc_extract_addr_from_skb(&srx, skb) == 0) { 6171c2bc7b9SDavid Howells msg.msg_namelen = srx.transport_len; 6181c2bc7b9SDavid Howells 619248f219cSDavid Howells whdr.epoch = htonl(sp->hdr.epoch); 620248f219cSDavid Howells whdr.cid = htonl(sp->hdr.cid); 621248f219cSDavid Howells whdr.callNumber = htonl(sp->hdr.callNumber); 622248f219cSDavid Howells whdr.serviceId = htons(sp->hdr.serviceId); 623248f219cSDavid Howells whdr.flags = sp->hdr.flags; 624248f219cSDavid Howells whdr.flags ^= RXRPC_CLIENT_INITIATED; 625248f219cSDavid Howells whdr.flags &= RXRPC_CLIENT_INITIATED; 626248f219cSDavid Howells 627d6672a5aSYueHaibing ret = kernel_sendmsg(local->socket, &msg, 628d6672a5aSYueHaibing iov, ioc, size); 6296b47fe1dSDavid Howells if (ret < 0) 6306b47fe1dSDavid Howells trace_rxrpc_tx_fail(local->debug_id, 0, ret, 6314764c0daSDavid Howells rxrpc_tx_point_reject); 6324764c0daSDavid Howells else 6334764c0daSDavid Howells trace_rxrpc_tx_packet(local->debug_id, &whdr, 6344764c0daSDavid Howells rxrpc_tx_point_reject); 635248f219cSDavid Howells } 636248f219cSDavid Howells 637987db9f7SDavid Howells rxrpc_free_skb(skb, rxrpc_skb_freed); 638248f219cSDavid Howells } 639248f219cSDavid Howells 640248f219cSDavid Howells _leave(""); 641248f219cSDavid Howells } 642ace45becSDavid Howells 643ace45becSDavid Howells /* 644ace45becSDavid Howells * Send a VERSION reply to a peer as a keepalive. 645ace45becSDavid Howells */ 646ace45becSDavid Howells void rxrpc_send_keepalive(struct rxrpc_peer *peer) 647ace45becSDavid Howells { 648ace45becSDavid Howells struct rxrpc_wire_header whdr; 649ace45becSDavid Howells struct msghdr msg; 650ace45becSDavid Howells struct kvec iov[2]; 651ace45becSDavid Howells size_t len; 652ace45becSDavid Howells int ret; 653ace45becSDavid Howells 654ace45becSDavid Howells _enter(""); 655ace45becSDavid Howells 656ace45becSDavid Howells msg.msg_name = &peer->srx.transport; 657ace45becSDavid Howells msg.msg_namelen = peer->srx.transport_len; 658ace45becSDavid Howells msg.msg_control = NULL; 659ace45becSDavid Howells msg.msg_controllen = 0; 660ace45becSDavid Howells msg.msg_flags = 0; 661ace45becSDavid Howells 662ace45becSDavid Howells whdr.epoch = htonl(peer->local->rxnet->epoch); 663ace45becSDavid Howells whdr.cid = 0; 664ace45becSDavid Howells whdr.callNumber = 0; 665ace45becSDavid Howells whdr.seq = 0; 666ace45becSDavid Howells whdr.serial = 0; 667ace45becSDavid Howells whdr.type = RXRPC_PACKET_TYPE_VERSION; /* Not client-initiated */ 668ace45becSDavid Howells whdr.flags = RXRPC_LAST_PACKET; 669ace45becSDavid Howells whdr.userStatus = 0; 670ace45becSDavid Howells whdr.securityIndex = 0; 671ace45becSDavid Howells whdr._rsvd = 0; 672ace45becSDavid Howells whdr.serviceId = 0; 673ace45becSDavid Howells 674ace45becSDavid Howells iov[0].iov_base = &whdr; 675ace45becSDavid Howells iov[0].iov_len = sizeof(whdr); 676ace45becSDavid Howells iov[1].iov_base = (char *)rxrpc_keepalive_string; 677ace45becSDavid Howells iov[1].iov_len = sizeof(rxrpc_keepalive_string); 678ace45becSDavid Howells 679ace45becSDavid Howells len = iov[0].iov_len + iov[1].iov_len; 680ace45becSDavid Howells 681ace45becSDavid Howells _proto("Tx VERSION (keepalive)"); 682ace45becSDavid Howells 683ace45becSDavid Howells ret = kernel_sendmsg(peer->local->socket, &msg, iov, 2, len); 684ace45becSDavid Howells if (ret < 0) 6856b47fe1dSDavid Howells trace_rxrpc_tx_fail(peer->debug_id, 0, ret, 6864764c0daSDavid Howells rxrpc_tx_point_version_keepalive); 6874764c0daSDavid Howells else 6884764c0daSDavid Howells trace_rxrpc_tx_packet(peer->debug_id, &whdr, 6894764c0daSDavid Howells rxrpc_tx_point_version_keepalive); 690ace45becSDavid Howells 691330bdcfaSDavid Howells peer->last_tx_at = ktime_get_seconds(); 692ace45becSDavid Howells _leave(""); 693ace45becSDavid Howells } 694