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; 77*9a3dedcfSDavid 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 83*9a3dedcfSDavid Howells tmp = atomic_xchg(&call->ackr_nr_unacked, 0); 84*9a3dedcfSDavid Howells tmp |= atomic_xchg(&call->ackr_nr_consumed, 0); 85*9a3dedcfSDavid Howells if (!tmp && (reason == RXRPC_ACK_DELAY || 86*9a3dedcfSDavid Howells reason == RXRPC_ACK_IDLE)) 87*9a3dedcfSDavid Howells return 0; 88*9a3dedcfSDavid 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); 233*9a3dedcfSDavid Howells if (n == 0) { 234*9a3dedcfSDavid Howells kfree(pkt); 235*9a3dedcfSDavid Howells return 0; 236*9a3dedcfSDavid 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 { 3535a924b89SDavid Howells struct rxrpc_connection *conn = call->conn; 3545a924b89SDavid Howells struct rxrpc_wire_header whdr; 3555a924b89SDavid Howells struct rxrpc_skb_priv *sp = rxrpc_skb(skb); 3568c3e34a4SDavid Howells struct msghdr msg; 3575a924b89SDavid Howells struct kvec iov[2]; 3585a924b89SDavid Howells rxrpc_serial_t serial; 3595a924b89SDavid Howells size_t len; 3604700c4d8SDavid Howells int ret, rtt_slot = -1; 3618c3e34a4SDavid Howells 3628c3e34a4SDavid Howells _enter(",{%d}", skb->len); 3638c3e34a4SDavid Howells 364245500d8SDavid Howells if (hlist_unhashed(&call->error_link)) { 365245500d8SDavid Howells spin_lock_bh(&call->peer->lock); 366245500d8SDavid Howells hlist_add_head_rcu(&call->error_link, &call->peer->error_targets); 367245500d8SDavid Howells spin_unlock_bh(&call->peer->lock); 368245500d8SDavid Howells } 369245500d8SDavid Howells 3705a924b89SDavid Howells /* Each transmission of a Tx packet needs a new serial number */ 3715a924b89SDavid Howells serial = atomic_inc_return(&conn->serial); 3728c3e34a4SDavid Howells 3735a924b89SDavid Howells whdr.epoch = htonl(conn->proto.epoch); 3745a924b89SDavid Howells whdr.cid = htonl(call->cid); 3755a924b89SDavid Howells whdr.callNumber = htonl(call->call_id); 3765a924b89SDavid Howells whdr.seq = htonl(sp->hdr.seq); 3775a924b89SDavid Howells whdr.serial = htonl(serial); 3785a924b89SDavid Howells whdr.type = RXRPC_PACKET_TYPE_DATA; 3795a924b89SDavid Howells whdr.flags = sp->hdr.flags; 3805a924b89SDavid Howells whdr.userStatus = 0; 3815a924b89SDavid Howells whdr.securityIndex = call->security_ix; 3825a924b89SDavid Howells whdr._rsvd = htons(sp->hdr._rsvd); 3835a924b89SDavid Howells whdr.serviceId = htons(call->service_id); 3845a924b89SDavid Howells 3854e255721SDavid Howells if (test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags) && 3864e255721SDavid Howells sp->hdr.seq == 1) 3874e255721SDavid Howells whdr.userStatus = RXRPC_USERSTATUS_SERVICE_UPGRADE; 3884e255721SDavid Howells 3895a924b89SDavid Howells iov[0].iov_base = &whdr; 3905a924b89SDavid Howells iov[0].iov_len = sizeof(whdr); 3915a924b89SDavid Howells iov[1].iov_base = skb->head; 3925a924b89SDavid Howells iov[1].iov_len = skb->len; 3935a924b89SDavid Howells len = iov[0].iov_len + iov[1].iov_len; 3945a924b89SDavid Howells 3955a924b89SDavid Howells msg.msg_name = &call->peer->srx.transport; 3965a924b89SDavid Howells msg.msg_namelen = call->peer->srx.transport_len; 3978c3e34a4SDavid Howells msg.msg_control = NULL; 3988c3e34a4SDavid Howells msg.msg_controllen = 0; 3998c3e34a4SDavid Howells msg.msg_flags = 0; 4008c3e34a4SDavid Howells 40157494343SDavid Howells /* If our RTT cache needs working on, request an ACK. Also request 40257494343SDavid Howells * ACKs if a DATA packet appears to have been lost. 403b604dd98SDavid Howells * 404b604dd98SDavid Howells * However, we mustn't request an ACK on the last reply packet of a 405b604dd98SDavid Howells * service call, lest OpenAFS incorrectly send us an ACK with some 406b604dd98SDavid Howells * soft-ACKs in it and then never follow up with a proper hard ACK. 40757494343SDavid Howells */ 408b604dd98SDavid Howells if ((!(sp->hdr.flags & RXRPC_LAST_PACKET) || 409b604dd98SDavid Howells rxrpc_to_server(sp) 410b604dd98SDavid Howells ) && 411bd1fdf8cSDavid Howells (test_and_clear_bit(RXRPC_CALL_EV_ACK_LOST, &call->events) || 412bd1fdf8cSDavid Howells retrans || 413b112a670SDavid Howells call->cong_mode == RXRPC_CALL_SLOW_START || 414c410bf01SDavid Howells (call->peer->rtt_count < 3 && sp->hdr.seq & 1) || 4150d4b103cSDavid Howells ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000), 416bf7d620aSDavid Howells ktime_get_real()))) 4170d4b103cSDavid Howells whdr.flags |= RXRPC_REQUEST_ACK; 4180d4b103cSDavid Howells 4198a681c36SDavid Howells if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) { 4208a681c36SDavid Howells static int lose; 4218a681c36SDavid Howells if ((lose++ & 7) == 7) { 422a1767077SDavid Howells ret = 0; 423526949e8SArnd Bergmann trace_rxrpc_tx_data(call, sp->hdr.seq, serial, 424526949e8SArnd Bergmann whdr.flags, retrans, true); 425526949e8SArnd Bergmann goto done; 4268a681c36SDavid Howells } 4278a681c36SDavid Howells } 4288a681c36SDavid Howells 429526949e8SArnd Bergmann trace_rxrpc_tx_data(call, sp->hdr.seq, serial, whdr.flags, retrans, 430526949e8SArnd Bergmann false); 4315a924b89SDavid Howells 4328c3e34a4SDavid Howells /* send the packet with the don't fragment bit set if we currently 4338c3e34a4SDavid Howells * think it's small enough */ 4345a924b89SDavid Howells if (iov[1].iov_len >= call->peer->maxdata) 4355a924b89SDavid Howells goto send_fragmentable; 4365a924b89SDavid Howells 437985a5c82SDavid Howells down_read(&conn->params.local->defrag_sem); 438b604dd98SDavid Howells 439b604dd98SDavid Howells sp->hdr.serial = serial; 440b604dd98SDavid Howells smp_wmb(); /* Set serial before timestamp */ 441b604dd98SDavid Howells skb->tstamp = ktime_get_real(); 4424700c4d8SDavid Howells if (whdr.flags & RXRPC_REQUEST_ACK) 4434700c4d8SDavid Howells rtt_slot = rxrpc_begin_rtt_probe(call, serial, rxrpc_rtt_tx_data); 444b604dd98SDavid Howells 4458c3e34a4SDavid Howells /* send the packet by UDP 4468c3e34a4SDavid Howells * - returns -EMSGSIZE if UDP would have to fragment the packet 4478c3e34a4SDavid Howells * to go out of the interface 4488c3e34a4SDavid Howells * - in which case, we'll have processed the ICMP error 4498c3e34a4SDavid Howells * message and update the peer record 4508c3e34a4SDavid Howells */ 4515a924b89SDavid Howells ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len); 452330bdcfaSDavid Howells conn->params.peer->last_tx_at = ktime_get_seconds(); 4538c3e34a4SDavid Howells 454985a5c82SDavid Howells up_read(&conn->params.local->defrag_sem); 4554700c4d8SDavid Howells if (ret < 0) { 4564700c4d8SDavid Howells rxrpc_cancel_rtt_probe(call, serial, rtt_slot); 4576b47fe1dSDavid Howells trace_rxrpc_tx_fail(call->debug_id, serial, ret, 4584764c0daSDavid Howells rxrpc_tx_point_call_data_nofrag); 4594700c4d8SDavid Howells } else { 4604764c0daSDavid Howells trace_rxrpc_tx_packet(call->debug_id, &whdr, 4614764c0daSDavid Howells rxrpc_tx_point_call_data_nofrag); 4624700c4d8SDavid Howells } 4634700c4d8SDavid Howells 464c7e86acfSDavid Howells rxrpc_tx_backoff(call, ret); 4658c3e34a4SDavid Howells if (ret == -EMSGSIZE) 4668c3e34a4SDavid Howells goto send_fragmentable; 4678c3e34a4SDavid Howells 4685a924b89SDavid Howells done: 46950235c4bSDavid Howells if (ret >= 0) { 4700d4b103cSDavid Howells if (whdr.flags & RXRPC_REQUEST_ACK) { 471b604dd98SDavid Howells call->peer->rtt_last_req = skb->tstamp; 472c410bf01SDavid Howells if (call->peer->rtt_count > 1) { 473bd1fdf8cSDavid Howells unsigned long nowj = jiffies, ack_lost_at; 474bd1fdf8cSDavid Howells 4752c13c05cSDavid Howells ack_lost_at = rxrpc_get_rto_backoff(call->peer, false); 476bd1fdf8cSDavid Howells ack_lost_at += nowj; 477bd1fdf8cSDavid Howells WRITE_ONCE(call->ack_lost_at, ack_lost_at); 478bd1fdf8cSDavid Howells rxrpc_reduce_call_timer(call, ack_lost_at, nowj, 479bd1fdf8cSDavid Howells rxrpc_timer_set_for_lost_ack); 480bd1fdf8cSDavid Howells } 4818c3e34a4SDavid Howells } 482c54e43d7SDavid Howells 483c54e43d7SDavid Howells if (sp->hdr.seq == 1 && 484c54e43d7SDavid Howells !test_and_set_bit(RXRPC_CALL_BEGAN_RX_TIMER, 485c54e43d7SDavid Howells &call->flags)) { 486c54e43d7SDavid Howells unsigned long nowj = jiffies, expect_rx_by; 487c54e43d7SDavid Howells 488c54e43d7SDavid Howells expect_rx_by = nowj + call->next_rx_timo; 489c54e43d7SDavid Howells WRITE_ONCE(call->expect_rx_by, expect_rx_by); 490c54e43d7SDavid Howells rxrpc_reduce_call_timer(call, expect_rx_by, nowj, 491c54e43d7SDavid Howells rxrpc_timer_set_for_normal); 492c54e43d7SDavid Howells } 493415f44e4SDavid Howells 494415f44e4SDavid Howells rxrpc_set_keepalive(call); 495c7e86acfSDavid Howells } else { 496c7e86acfSDavid Howells /* Cancel the call if the initial transmission fails, 497c7e86acfSDavid Howells * particularly if that's due to network routing issues that 498c7e86acfSDavid Howells * aren't going away anytime soon. The layer above can arrange 499c7e86acfSDavid Howells * the retransmission. 500c7e86acfSDavid Howells */ 501c7e86acfSDavid Howells if (!test_and_set_bit(RXRPC_CALL_BEGAN_RX_TIMER, &call->flags)) 502c7e86acfSDavid Howells rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR, 503c7e86acfSDavid Howells RX_USER_ABORT, ret); 504c7e86acfSDavid Howells } 505415f44e4SDavid Howells 5065a924b89SDavid Howells _leave(" = %d [%u]", ret, call->peer->maxdata); 5075a924b89SDavid Howells return ret; 5088c3e34a4SDavid Howells 5098c3e34a4SDavid Howells send_fragmentable: 5108c3e34a4SDavid Howells /* attempt to send this message with fragmentation enabled */ 5118c3e34a4SDavid Howells _debug("send fragment"); 5128c3e34a4SDavid Howells 513985a5c82SDavid Howells down_write(&conn->params.local->defrag_sem); 514985a5c82SDavid Howells 515b604dd98SDavid Howells sp->hdr.serial = serial; 516b604dd98SDavid Howells smp_wmb(); /* Set serial before timestamp */ 517b604dd98SDavid Howells skb->tstamp = ktime_get_real(); 5184700c4d8SDavid Howells if (whdr.flags & RXRPC_REQUEST_ACK) 5194700c4d8SDavid Howells rtt_slot = rxrpc_begin_rtt_probe(call, serial, rxrpc_rtt_tx_data); 520b604dd98SDavid Howells 521985a5c82SDavid Howells switch (conn->params.local->srx.transport.family) { 5220e631eeeSDavid Howells case AF_INET6: 523985a5c82SDavid Howells case AF_INET: 5242de569bdSChristoph Hellwig ip_sock_set_mtu_discover(conn->params.local->socket->sk, 5252de569bdSChristoph Hellwig IP_PMTUDISC_DONT); 5265a924b89SDavid Howells ret = kernel_sendmsg(conn->params.local->socket, &msg, 5275a924b89SDavid Howells iov, 2, len); 528330bdcfaSDavid Howells conn->params.peer->last_tx_at = ktime_get_seconds(); 5298c3e34a4SDavid Howells 5302de569bdSChristoph Hellwig ip_sock_set_mtu_discover(conn->params.local->socket->sk, 5312de569bdSChristoph Hellwig IP_PMTUDISC_DO); 53275b54cb5SDavid Howells break; 5333427beb6SDavid Howells 5343427beb6SDavid Howells default: 5353427beb6SDavid Howells BUG(); 5368c3e34a4SDavid Howells } 5378c3e34a4SDavid Howells 5384700c4d8SDavid Howells if (ret < 0) { 5394700c4d8SDavid Howells rxrpc_cancel_rtt_probe(call, serial, rtt_slot); 5406b47fe1dSDavid Howells trace_rxrpc_tx_fail(call->debug_id, serial, ret, 5414764c0daSDavid Howells rxrpc_tx_point_call_data_frag); 5424700c4d8SDavid Howells } else { 5434764c0daSDavid Howells trace_rxrpc_tx_packet(call->debug_id, &whdr, 5444764c0daSDavid Howells rxrpc_tx_point_call_data_frag); 5454700c4d8SDavid Howells } 546c7e86acfSDavid Howells rxrpc_tx_backoff(call, ret); 5476b47fe1dSDavid Howells 548985a5c82SDavid Howells up_write(&conn->params.local->defrag_sem); 5495a924b89SDavid Howells goto done; 5508c3e34a4SDavid Howells } 551248f219cSDavid Howells 552248f219cSDavid Howells /* 553248f219cSDavid Howells * reject packets through the local endpoint 554248f219cSDavid Howells */ 555248f219cSDavid Howells void rxrpc_reject_packets(struct rxrpc_local *local) 556248f219cSDavid Howells { 5571c2bc7b9SDavid Howells struct sockaddr_rxrpc srx; 558248f219cSDavid Howells struct rxrpc_skb_priv *sp; 559248f219cSDavid Howells struct rxrpc_wire_header whdr; 560248f219cSDavid Howells struct sk_buff *skb; 561248f219cSDavid Howells struct msghdr msg; 562248f219cSDavid Howells struct kvec iov[2]; 563248f219cSDavid Howells size_t size; 564248f219cSDavid Howells __be32 code; 565ece64fecSDavid Howells int ret, ioc; 566248f219cSDavid Howells 567248f219cSDavid Howells _enter("%d", local->debug_id); 568248f219cSDavid Howells 569248f219cSDavid Howells iov[0].iov_base = &whdr; 570248f219cSDavid Howells iov[0].iov_len = sizeof(whdr); 571248f219cSDavid Howells iov[1].iov_base = &code; 572248f219cSDavid Howells iov[1].iov_len = sizeof(code); 573248f219cSDavid Howells 5741c2bc7b9SDavid Howells msg.msg_name = &srx.transport; 575248f219cSDavid Howells msg.msg_control = NULL; 576248f219cSDavid Howells msg.msg_controllen = 0; 577248f219cSDavid Howells msg.msg_flags = 0; 578248f219cSDavid Howells 579248f219cSDavid Howells memset(&whdr, 0, sizeof(whdr)); 580248f219cSDavid Howells 581248f219cSDavid Howells while ((skb = skb_dequeue(&local->reject_queue))) { 582987db9f7SDavid Howells rxrpc_see_skb(skb, rxrpc_skb_seen); 583248f219cSDavid Howells sp = rxrpc_skb(skb); 5841c2bc7b9SDavid Howells 585ece64fecSDavid Howells switch (skb->mark) { 586ece64fecSDavid Howells case RXRPC_SKB_MARK_REJECT_BUSY: 587ece64fecSDavid Howells whdr.type = RXRPC_PACKET_TYPE_BUSY; 588ece64fecSDavid Howells size = sizeof(whdr); 589ece64fecSDavid Howells ioc = 1; 590ece64fecSDavid Howells break; 591ece64fecSDavid Howells case RXRPC_SKB_MARK_REJECT_ABORT: 592ece64fecSDavid Howells whdr.type = RXRPC_PACKET_TYPE_ABORT; 593ece64fecSDavid Howells code = htonl(skb->priority); 594ece64fecSDavid Howells size = sizeof(whdr) + sizeof(code); 595ece64fecSDavid Howells ioc = 2; 596ece64fecSDavid Howells break; 597ece64fecSDavid Howells default: 598987db9f7SDavid Howells rxrpc_free_skb(skb, rxrpc_skb_freed); 599ece64fecSDavid Howells continue; 600ece64fecSDavid Howells } 601ece64fecSDavid Howells 6025a790b73SDavid Howells if (rxrpc_extract_addr_from_skb(&srx, skb) == 0) { 6031c2bc7b9SDavid Howells msg.msg_namelen = srx.transport_len; 6041c2bc7b9SDavid Howells 605248f219cSDavid Howells whdr.epoch = htonl(sp->hdr.epoch); 606248f219cSDavid Howells whdr.cid = htonl(sp->hdr.cid); 607248f219cSDavid Howells whdr.callNumber = htonl(sp->hdr.callNumber); 608248f219cSDavid Howells whdr.serviceId = htons(sp->hdr.serviceId); 609248f219cSDavid Howells whdr.flags = sp->hdr.flags; 610248f219cSDavid Howells whdr.flags ^= RXRPC_CLIENT_INITIATED; 611248f219cSDavid Howells whdr.flags &= RXRPC_CLIENT_INITIATED; 612248f219cSDavid Howells 613d6672a5aSYueHaibing ret = kernel_sendmsg(local->socket, &msg, 614d6672a5aSYueHaibing iov, ioc, size); 6156b47fe1dSDavid Howells if (ret < 0) 6166b47fe1dSDavid Howells trace_rxrpc_tx_fail(local->debug_id, 0, ret, 6174764c0daSDavid Howells rxrpc_tx_point_reject); 6184764c0daSDavid Howells else 6194764c0daSDavid Howells trace_rxrpc_tx_packet(local->debug_id, &whdr, 6204764c0daSDavid Howells rxrpc_tx_point_reject); 621248f219cSDavid Howells } 622248f219cSDavid Howells 623987db9f7SDavid Howells rxrpc_free_skb(skb, rxrpc_skb_freed); 624248f219cSDavid Howells } 625248f219cSDavid Howells 626248f219cSDavid Howells _leave(""); 627248f219cSDavid Howells } 628ace45becSDavid Howells 629ace45becSDavid Howells /* 630ace45becSDavid Howells * Send a VERSION reply to a peer as a keepalive. 631ace45becSDavid Howells */ 632ace45becSDavid Howells void rxrpc_send_keepalive(struct rxrpc_peer *peer) 633ace45becSDavid Howells { 634ace45becSDavid Howells struct rxrpc_wire_header whdr; 635ace45becSDavid Howells struct msghdr msg; 636ace45becSDavid Howells struct kvec iov[2]; 637ace45becSDavid Howells size_t len; 638ace45becSDavid Howells int ret; 639ace45becSDavid Howells 640ace45becSDavid Howells _enter(""); 641ace45becSDavid Howells 642ace45becSDavid Howells msg.msg_name = &peer->srx.transport; 643ace45becSDavid Howells msg.msg_namelen = peer->srx.transport_len; 644ace45becSDavid Howells msg.msg_control = NULL; 645ace45becSDavid Howells msg.msg_controllen = 0; 646ace45becSDavid Howells msg.msg_flags = 0; 647ace45becSDavid Howells 648ace45becSDavid Howells whdr.epoch = htonl(peer->local->rxnet->epoch); 649ace45becSDavid Howells whdr.cid = 0; 650ace45becSDavid Howells whdr.callNumber = 0; 651ace45becSDavid Howells whdr.seq = 0; 652ace45becSDavid Howells whdr.serial = 0; 653ace45becSDavid Howells whdr.type = RXRPC_PACKET_TYPE_VERSION; /* Not client-initiated */ 654ace45becSDavid Howells whdr.flags = RXRPC_LAST_PACKET; 655ace45becSDavid Howells whdr.userStatus = 0; 656ace45becSDavid Howells whdr.securityIndex = 0; 657ace45becSDavid Howells whdr._rsvd = 0; 658ace45becSDavid Howells whdr.serviceId = 0; 659ace45becSDavid Howells 660ace45becSDavid Howells iov[0].iov_base = &whdr; 661ace45becSDavid Howells iov[0].iov_len = sizeof(whdr); 662ace45becSDavid Howells iov[1].iov_base = (char *)rxrpc_keepalive_string; 663ace45becSDavid Howells iov[1].iov_len = sizeof(rxrpc_keepalive_string); 664ace45becSDavid Howells 665ace45becSDavid Howells len = iov[0].iov_len + iov[1].iov_len; 666ace45becSDavid Howells 667ace45becSDavid Howells _proto("Tx VERSION (keepalive)"); 668ace45becSDavid Howells 669ace45becSDavid Howells ret = kernel_sendmsg(peer->local->socket, &msg, iov, 2, len); 670ace45becSDavid Howells if (ret < 0) 6716b47fe1dSDavid Howells trace_rxrpc_tx_fail(peer->debug_id, 0, ret, 6724764c0daSDavid Howells rxrpc_tx_point_version_keepalive); 6734764c0daSDavid Howells else 6744764c0daSDavid Howells trace_rxrpc_tx_packet(peer->debug_id, &whdr, 6754764c0daSDavid Howells rxrpc_tx_point_version_keepalive); 676ace45becSDavid Howells 677330bdcfaSDavid Howells peer->last_tx_at = ktime_get_seconds(); 678ace45becSDavid Howells _leave(""); 679ace45becSDavid Howells } 680