David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1 | /* RxRPC packet transmission |
| 2 | * |
| 3 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. |
| 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
Joe Perches | 9b6d539 | 2016-06-02 12:08:52 -0700 | [diff] [blame] | 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 14 | #include <linux/net.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/gfp.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 16 | #include <linux/skbuff.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 17 | #include <linux/export.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 18 | #include <net/sock.h> |
| 19 | #include <net/af_rxrpc.h> |
| 20 | #include "ar-internal.h" |
| 21 | |
David Howells | 8d94aa3 | 2016-09-07 09:19:31 +0100 | [diff] [blame^] | 22 | struct rxrpc_pkt_buffer { |
| 23 | struct rxrpc_wire_header whdr; |
| 24 | union { |
| 25 | struct { |
| 26 | struct rxrpc_ackpacket ack; |
| 27 | u8 acks[255]; |
| 28 | u8 pad[3]; |
| 29 | }; |
| 30 | __be32 abort_code; |
| 31 | }; |
| 32 | struct rxrpc_ackinfo ackinfo; |
| 33 | }; |
| 34 | |
| 35 | /* |
| 36 | * Fill out an ACK packet. |
| 37 | */ |
| 38 | static size_t rxrpc_fill_out_ack(struct rxrpc_call *call, |
| 39 | struct rxrpc_pkt_buffer *pkt) |
| 40 | { |
| 41 | u32 mtu, jmax; |
| 42 | u8 *ackp = pkt->acks; |
| 43 | |
| 44 | pkt->ack.bufferSpace = htons(8); |
| 45 | pkt->ack.maxSkew = htons(0); |
| 46 | pkt->ack.firstPacket = htonl(call->rx_data_eaten + 1); |
| 47 | pkt->ack.previousPacket = htonl(call->ackr_prev_seq); |
| 48 | pkt->ack.serial = htonl(call->ackr_serial); |
| 49 | pkt->ack.reason = RXRPC_ACK_IDLE; |
| 50 | pkt->ack.nAcks = 0; |
| 51 | |
| 52 | mtu = call->peer->if_mtu; |
| 53 | mtu -= call->peer->hdrsize; |
| 54 | jmax = rxrpc_rx_jumbo_max; |
| 55 | pkt->ackinfo.rxMTU = htonl(rxrpc_rx_mtu); |
| 56 | pkt->ackinfo.maxMTU = htonl(mtu); |
| 57 | pkt->ackinfo.rwind = htonl(rxrpc_rx_window_size); |
| 58 | pkt->ackinfo.jumbo_max = htonl(jmax); |
| 59 | |
| 60 | *ackp++ = 0; |
| 61 | *ackp++ = 0; |
| 62 | *ackp++ = 0; |
| 63 | return 3; |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * Send a final ACK or ABORT call packet. |
| 68 | */ |
| 69 | int rxrpc_send_call_packet(struct rxrpc_call *call, u8 type) |
| 70 | { |
| 71 | struct rxrpc_connection *conn = NULL; |
| 72 | struct rxrpc_pkt_buffer *pkt; |
| 73 | struct msghdr msg; |
| 74 | struct kvec iov[2]; |
| 75 | rxrpc_serial_t serial; |
| 76 | size_t len, n; |
| 77 | int ioc, ret; |
| 78 | u32 abort_code; |
| 79 | |
| 80 | _enter("%u,%s", call->debug_id, rxrpc_pkts[type]); |
| 81 | |
| 82 | spin_lock_bh(&call->lock); |
| 83 | if (call->conn) |
| 84 | conn = rxrpc_get_connection_maybe(call->conn); |
| 85 | spin_unlock_bh(&call->lock); |
| 86 | if (!conn) |
| 87 | return -ECONNRESET; |
| 88 | |
| 89 | pkt = kzalloc(sizeof(*pkt), GFP_KERNEL); |
| 90 | if (!pkt) { |
| 91 | rxrpc_put_connection(conn); |
| 92 | return -ENOMEM; |
| 93 | } |
| 94 | |
| 95 | serial = atomic_inc_return(&conn->serial); |
| 96 | |
| 97 | msg.msg_name = &call->peer->srx.transport; |
| 98 | msg.msg_namelen = call->peer->srx.transport_len; |
| 99 | msg.msg_control = NULL; |
| 100 | msg.msg_controllen = 0; |
| 101 | msg.msg_flags = 0; |
| 102 | |
| 103 | pkt->whdr.epoch = htonl(conn->proto.epoch); |
| 104 | pkt->whdr.cid = htonl(call->cid); |
| 105 | pkt->whdr.callNumber = htonl(call->call_id); |
| 106 | pkt->whdr.seq = 0; |
| 107 | pkt->whdr.serial = htonl(serial); |
| 108 | pkt->whdr.type = type; |
| 109 | pkt->whdr.flags = conn->out_clientflag; |
| 110 | pkt->whdr.userStatus = 0; |
| 111 | pkt->whdr.securityIndex = call->security_ix; |
| 112 | pkt->whdr._rsvd = 0; |
| 113 | pkt->whdr.serviceId = htons(call->service_id); |
| 114 | |
| 115 | iov[0].iov_base = pkt; |
| 116 | iov[0].iov_len = sizeof(pkt->whdr); |
| 117 | len = sizeof(pkt->whdr); |
| 118 | |
| 119 | switch (type) { |
| 120 | case RXRPC_PACKET_TYPE_ACK: |
| 121 | spin_lock_bh(&call->lock); |
| 122 | n = rxrpc_fill_out_ack(call, pkt); |
| 123 | call->ackr_reason = 0; |
| 124 | |
| 125 | spin_unlock_bh(&call->lock); |
| 126 | |
| 127 | _proto("Tx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }", |
| 128 | serial, |
| 129 | ntohs(pkt->ack.maxSkew), |
| 130 | ntohl(pkt->ack.firstPacket), |
| 131 | ntohl(pkt->ack.previousPacket), |
| 132 | ntohl(pkt->ack.serial), |
| 133 | rxrpc_acks(pkt->ack.reason), |
| 134 | pkt->ack.nAcks); |
| 135 | |
| 136 | iov[0].iov_len += sizeof(pkt->ack) + n; |
| 137 | iov[1].iov_base = &pkt->ackinfo; |
| 138 | iov[1].iov_len = sizeof(pkt->ackinfo); |
| 139 | len += sizeof(pkt->ack) + n + sizeof(pkt->ackinfo); |
| 140 | ioc = 2; |
| 141 | break; |
| 142 | |
| 143 | case RXRPC_PACKET_TYPE_ABORT: |
| 144 | abort_code = call->abort_code; |
| 145 | pkt->abort_code = htonl(abort_code); |
| 146 | _proto("Tx ABORT %%%u { %d }", serial, abort_code); |
| 147 | iov[0].iov_len += sizeof(pkt->abort_code); |
| 148 | len += sizeof(pkt->abort_code); |
| 149 | ioc = 1; |
| 150 | break; |
| 151 | |
| 152 | default: |
| 153 | BUG(); |
| 154 | ret = -ENOANO; |
| 155 | goto out; |
| 156 | } |
| 157 | |
| 158 | ret = kernel_sendmsg(conn->params.local->socket, |
| 159 | &msg, iov, ioc, len); |
| 160 | |
| 161 | out: |
| 162 | rxrpc_put_connection(conn); |
| 163 | kfree(pkt); |
| 164 | return ret; |
| 165 | } |
| 166 | |
David Howells | 5873c08 | 2014-02-07 18:58:44 +0000 | [diff] [blame] | 167 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 168 | * send a packet through the transport endpoint |
| 169 | */ |
David Howells | 985a5c8 | 2016-06-17 11:53:37 +0100 | [diff] [blame] | 170 | int rxrpc_send_data_packet(struct rxrpc_connection *conn, struct sk_buff *skb) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 171 | { |
| 172 | struct kvec iov[1]; |
| 173 | struct msghdr msg; |
| 174 | int ret, opt; |
| 175 | |
| 176 | _enter(",{%d}", skb->len); |
| 177 | |
| 178 | iov[0].iov_base = skb->head; |
| 179 | iov[0].iov_len = skb->len; |
| 180 | |
David Howells | 985a5c8 | 2016-06-17 11:53:37 +0100 | [diff] [blame] | 181 | msg.msg_name = &conn->params.peer->srx.transport; |
| 182 | msg.msg_namelen = conn->params.peer->srx.transport_len; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 183 | msg.msg_control = NULL; |
| 184 | msg.msg_controllen = 0; |
| 185 | msg.msg_flags = 0; |
| 186 | |
| 187 | /* send the packet with the don't fragment bit set if we currently |
| 188 | * think it's small enough */ |
David Howells | 985a5c8 | 2016-06-17 11:53:37 +0100 | [diff] [blame] | 189 | if (skb->len - sizeof(struct rxrpc_wire_header) < conn->params.peer->maxdata) { |
| 190 | down_read(&conn->params.local->defrag_sem); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 191 | /* send the packet by UDP |
| 192 | * - returns -EMSGSIZE if UDP would have to fragment the packet |
| 193 | * to go out of the interface |
| 194 | * - in which case, we'll have processed the ICMP error |
| 195 | * message and update the peer record |
| 196 | */ |
David Howells | 985a5c8 | 2016-06-17 11:53:37 +0100 | [diff] [blame] | 197 | ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 1, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 198 | iov[0].iov_len); |
| 199 | |
David Howells | 985a5c8 | 2016-06-17 11:53:37 +0100 | [diff] [blame] | 200 | up_read(&conn->params.local->defrag_sem); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 201 | if (ret == -EMSGSIZE) |
| 202 | goto send_fragmentable; |
| 203 | |
David Howells | 985a5c8 | 2016-06-17 11:53:37 +0100 | [diff] [blame] | 204 | _leave(" = %d [%u]", ret, conn->params.peer->maxdata); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 205 | return ret; |
| 206 | } |
| 207 | |
| 208 | send_fragmentable: |
| 209 | /* attempt to send this message with fragmentation enabled */ |
| 210 | _debug("send fragment"); |
| 211 | |
David Howells | 985a5c8 | 2016-06-17 11:53:37 +0100 | [diff] [blame] | 212 | down_write(&conn->params.local->defrag_sem); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 213 | |
David Howells | 985a5c8 | 2016-06-17 11:53:37 +0100 | [diff] [blame] | 214 | switch (conn->params.local->srx.transport.family) { |
| 215 | case AF_INET: |
| 216 | opt = IP_PMTUDISC_DONT; |
| 217 | ret = kernel_setsockopt(conn->params.local->socket, |
| 218 | SOL_IP, IP_MTU_DISCOVER, |
| 219 | (char *)&opt, sizeof(opt)); |
| 220 | if (ret == 0) { |
| 221 | ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 1, |
| 222 | iov[0].iov_len); |
| 223 | |
| 224 | opt = IP_PMTUDISC_DO; |
| 225 | kernel_setsockopt(conn->params.local->socket, SOL_IP, |
| 226 | IP_MTU_DISCOVER, |
| 227 | (char *)&opt, sizeof(opt)); |
| 228 | } |
| 229 | break; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 230 | } |
| 231 | |
David Howells | 985a5c8 | 2016-06-17 11:53:37 +0100 | [diff] [blame] | 232 | up_write(&conn->params.local->defrag_sem); |
| 233 | _leave(" = %d [frag %u]", ret, conn->params.peer->maxdata); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 234 | return ret; |
| 235 | } |