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 | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame^] | 18 | #include <linux/udp.h> |
| 19 | #include <linux/ip.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 20 | #include <net/sock.h> |
| 21 | #include <net/af_rxrpc.h> |
| 22 | #include "ar-internal.h" |
| 23 | |
David Howells | 8d94aa3 | 2016-09-07 09:19:31 +0100 | [diff] [blame] | 24 | struct rxrpc_pkt_buffer { |
| 25 | struct rxrpc_wire_header whdr; |
| 26 | union { |
| 27 | struct { |
| 28 | struct rxrpc_ackpacket ack; |
| 29 | u8 acks[255]; |
| 30 | u8 pad[3]; |
| 31 | }; |
| 32 | __be32 abort_code; |
| 33 | }; |
| 34 | struct rxrpc_ackinfo ackinfo; |
| 35 | }; |
| 36 | |
| 37 | /* |
| 38 | * Fill out an ACK packet. |
| 39 | */ |
| 40 | static size_t rxrpc_fill_out_ack(struct rxrpc_call *call, |
| 41 | struct rxrpc_pkt_buffer *pkt) |
| 42 | { |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame^] | 43 | rxrpc_seq_t hard_ack, top, seq; |
| 44 | int ix; |
David Howells | 8d94aa3 | 2016-09-07 09:19:31 +0100 | [diff] [blame] | 45 | u32 mtu, jmax; |
| 46 | u8 *ackp = pkt->acks; |
| 47 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame^] | 48 | /* Barrier against rxrpc_input_data(). */ |
| 49 | hard_ack = READ_ONCE(call->rx_hard_ack); |
| 50 | top = smp_load_acquire(&call->rx_top); |
| 51 | |
David Howells | 8d94aa3 | 2016-09-07 09:19:31 +0100 | [diff] [blame] | 52 | pkt->ack.bufferSpace = htons(8); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame^] | 53 | pkt->ack.maxSkew = htons(call->ackr_skew); |
| 54 | pkt->ack.firstPacket = htonl(hard_ack + 1); |
David Howells | 8d94aa3 | 2016-09-07 09:19:31 +0100 | [diff] [blame] | 55 | pkt->ack.previousPacket = htonl(call->ackr_prev_seq); |
| 56 | pkt->ack.serial = htonl(call->ackr_serial); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame^] | 57 | pkt->ack.reason = call->ackr_reason; |
| 58 | pkt->ack.nAcks = top - hard_ack; |
David Howells | 8d94aa3 | 2016-09-07 09:19:31 +0100 | [diff] [blame] | 59 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame^] | 60 | if (after(top, hard_ack)) { |
| 61 | seq = hard_ack + 1; |
| 62 | do { |
| 63 | ix = seq & RXRPC_RXTX_BUFF_MASK; |
| 64 | if (call->rxtx_buffer[ix]) |
| 65 | *ackp++ = RXRPC_ACK_TYPE_ACK; |
| 66 | else |
| 67 | *ackp++ = RXRPC_ACK_TYPE_NACK; |
| 68 | seq++; |
| 69 | } while (before_eq(seq, top)); |
| 70 | } |
| 71 | |
| 72 | mtu = call->conn->params.peer->if_mtu; |
| 73 | mtu -= call->conn->params.peer->hdrsize; |
| 74 | jmax = (call->nr_jumbo_dup > 3) ? 1 : rxrpc_rx_jumbo_max; |
David Howells | 8d94aa3 | 2016-09-07 09:19:31 +0100 | [diff] [blame] | 75 | pkt->ackinfo.rxMTU = htonl(rxrpc_rx_mtu); |
| 76 | pkt->ackinfo.maxMTU = htonl(mtu); |
| 77 | pkt->ackinfo.rwind = htonl(rxrpc_rx_window_size); |
| 78 | pkt->ackinfo.jumbo_max = htonl(jmax); |
| 79 | |
| 80 | *ackp++ = 0; |
| 81 | *ackp++ = 0; |
| 82 | *ackp++ = 0; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame^] | 83 | return top - hard_ack + 3; |
David Howells | 8d94aa3 | 2016-09-07 09:19:31 +0100 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /* |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame^] | 87 | * Send an ACK or ABORT call packet. |
David Howells | 8d94aa3 | 2016-09-07 09:19:31 +0100 | [diff] [blame] | 88 | */ |
| 89 | int rxrpc_send_call_packet(struct rxrpc_call *call, u8 type) |
| 90 | { |
| 91 | struct rxrpc_connection *conn = NULL; |
| 92 | struct rxrpc_pkt_buffer *pkt; |
| 93 | struct msghdr msg; |
| 94 | struct kvec iov[2]; |
| 95 | rxrpc_serial_t serial; |
| 96 | size_t len, n; |
| 97 | int ioc, ret; |
| 98 | u32 abort_code; |
| 99 | |
| 100 | _enter("%u,%s", call->debug_id, rxrpc_pkts[type]); |
| 101 | |
| 102 | spin_lock_bh(&call->lock); |
| 103 | if (call->conn) |
| 104 | conn = rxrpc_get_connection_maybe(call->conn); |
| 105 | spin_unlock_bh(&call->lock); |
| 106 | if (!conn) |
| 107 | return -ECONNRESET; |
| 108 | |
| 109 | pkt = kzalloc(sizeof(*pkt), GFP_KERNEL); |
| 110 | if (!pkt) { |
| 111 | rxrpc_put_connection(conn); |
| 112 | return -ENOMEM; |
| 113 | } |
| 114 | |
| 115 | serial = atomic_inc_return(&conn->serial); |
| 116 | |
| 117 | msg.msg_name = &call->peer->srx.transport; |
| 118 | msg.msg_namelen = call->peer->srx.transport_len; |
| 119 | msg.msg_control = NULL; |
| 120 | msg.msg_controllen = 0; |
| 121 | msg.msg_flags = 0; |
| 122 | |
| 123 | pkt->whdr.epoch = htonl(conn->proto.epoch); |
| 124 | pkt->whdr.cid = htonl(call->cid); |
| 125 | pkt->whdr.callNumber = htonl(call->call_id); |
| 126 | pkt->whdr.seq = 0; |
| 127 | pkt->whdr.serial = htonl(serial); |
| 128 | pkt->whdr.type = type; |
| 129 | pkt->whdr.flags = conn->out_clientflag; |
| 130 | pkt->whdr.userStatus = 0; |
| 131 | pkt->whdr.securityIndex = call->security_ix; |
| 132 | pkt->whdr._rsvd = 0; |
| 133 | pkt->whdr.serviceId = htons(call->service_id); |
| 134 | |
| 135 | iov[0].iov_base = pkt; |
| 136 | iov[0].iov_len = sizeof(pkt->whdr); |
| 137 | len = sizeof(pkt->whdr); |
| 138 | |
| 139 | switch (type) { |
| 140 | case RXRPC_PACKET_TYPE_ACK: |
| 141 | spin_lock_bh(&call->lock); |
| 142 | n = rxrpc_fill_out_ack(call, pkt); |
| 143 | call->ackr_reason = 0; |
| 144 | |
| 145 | spin_unlock_bh(&call->lock); |
| 146 | |
| 147 | _proto("Tx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }", |
| 148 | serial, |
| 149 | ntohs(pkt->ack.maxSkew), |
| 150 | ntohl(pkt->ack.firstPacket), |
| 151 | ntohl(pkt->ack.previousPacket), |
| 152 | ntohl(pkt->ack.serial), |
| 153 | rxrpc_acks(pkt->ack.reason), |
| 154 | pkt->ack.nAcks); |
| 155 | |
| 156 | iov[0].iov_len += sizeof(pkt->ack) + n; |
| 157 | iov[1].iov_base = &pkt->ackinfo; |
| 158 | iov[1].iov_len = sizeof(pkt->ackinfo); |
| 159 | len += sizeof(pkt->ack) + n + sizeof(pkt->ackinfo); |
| 160 | ioc = 2; |
| 161 | break; |
| 162 | |
| 163 | case RXRPC_PACKET_TYPE_ABORT: |
| 164 | abort_code = call->abort_code; |
| 165 | pkt->abort_code = htonl(abort_code); |
| 166 | _proto("Tx ABORT %%%u { %d }", serial, abort_code); |
| 167 | iov[0].iov_len += sizeof(pkt->abort_code); |
| 168 | len += sizeof(pkt->abort_code); |
| 169 | ioc = 1; |
| 170 | break; |
| 171 | |
| 172 | default: |
| 173 | BUG(); |
| 174 | ret = -ENOANO; |
| 175 | goto out; |
| 176 | } |
| 177 | |
| 178 | ret = kernel_sendmsg(conn->params.local->socket, |
| 179 | &msg, iov, ioc, len); |
| 180 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame^] | 181 | if (ret < 0 && call->state < RXRPC_CALL_COMPLETE) { |
| 182 | switch (pkt->whdr.type) { |
| 183 | case RXRPC_PACKET_TYPE_ACK: |
| 184 | rxrpc_propose_ACK(call, pkt->ack.reason, |
| 185 | ntohs(pkt->ack.maxSkew), |
| 186 | ntohl(pkt->ack.serial), |
| 187 | true, true); |
| 188 | break; |
| 189 | case RXRPC_PACKET_TYPE_ABORT: |
| 190 | break; |
| 191 | } |
| 192 | } |
| 193 | |
David Howells | 8d94aa3 | 2016-09-07 09:19:31 +0100 | [diff] [blame] | 194 | out: |
| 195 | rxrpc_put_connection(conn); |
| 196 | kfree(pkt); |
| 197 | return ret; |
| 198 | } |
| 199 | |
David Howells | 5873c08 | 2014-02-07 18:58:44 +0000 | [diff] [blame] | 200 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 201 | * send a packet through the transport endpoint |
| 202 | */ |
David Howells | 985a5c8 | 2016-06-17 11:53:37 +0100 | [diff] [blame] | 203 | 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] | 204 | { |
| 205 | struct kvec iov[1]; |
| 206 | struct msghdr msg; |
| 207 | int ret, opt; |
| 208 | |
| 209 | _enter(",{%d}", skb->len); |
| 210 | |
| 211 | iov[0].iov_base = skb->head; |
| 212 | iov[0].iov_len = skb->len; |
| 213 | |
David Howells | 985a5c8 | 2016-06-17 11:53:37 +0100 | [diff] [blame] | 214 | msg.msg_name = &conn->params.peer->srx.transport; |
| 215 | msg.msg_namelen = conn->params.peer->srx.transport_len; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 216 | msg.msg_control = NULL; |
| 217 | msg.msg_controllen = 0; |
| 218 | msg.msg_flags = 0; |
| 219 | |
| 220 | /* send the packet with the don't fragment bit set if we currently |
| 221 | * think it's small enough */ |
David Howells | 985a5c8 | 2016-06-17 11:53:37 +0100 | [diff] [blame] | 222 | if (skb->len - sizeof(struct rxrpc_wire_header) < conn->params.peer->maxdata) { |
| 223 | down_read(&conn->params.local->defrag_sem); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 224 | /* send the packet by UDP |
| 225 | * - returns -EMSGSIZE if UDP would have to fragment the packet |
| 226 | * to go out of the interface |
| 227 | * - in which case, we'll have processed the ICMP error |
| 228 | * message and update the peer record |
| 229 | */ |
David Howells | 985a5c8 | 2016-06-17 11:53:37 +0100 | [diff] [blame] | 230 | ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 1, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 231 | iov[0].iov_len); |
| 232 | |
David Howells | 985a5c8 | 2016-06-17 11:53:37 +0100 | [diff] [blame] | 233 | up_read(&conn->params.local->defrag_sem); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 234 | if (ret == -EMSGSIZE) |
| 235 | goto send_fragmentable; |
| 236 | |
David Howells | 985a5c8 | 2016-06-17 11:53:37 +0100 | [diff] [blame] | 237 | _leave(" = %d [%u]", ret, conn->params.peer->maxdata); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 238 | return ret; |
| 239 | } |
| 240 | |
| 241 | send_fragmentable: |
| 242 | /* attempt to send this message with fragmentation enabled */ |
| 243 | _debug("send fragment"); |
| 244 | |
David Howells | 985a5c8 | 2016-06-17 11:53:37 +0100 | [diff] [blame] | 245 | down_write(&conn->params.local->defrag_sem); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 246 | |
David Howells | 985a5c8 | 2016-06-17 11:53:37 +0100 | [diff] [blame] | 247 | switch (conn->params.local->srx.transport.family) { |
| 248 | case AF_INET: |
| 249 | opt = IP_PMTUDISC_DONT; |
| 250 | ret = kernel_setsockopt(conn->params.local->socket, |
| 251 | SOL_IP, IP_MTU_DISCOVER, |
| 252 | (char *)&opt, sizeof(opt)); |
| 253 | if (ret == 0) { |
| 254 | ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 1, |
| 255 | iov[0].iov_len); |
| 256 | |
| 257 | opt = IP_PMTUDISC_DO; |
| 258 | kernel_setsockopt(conn->params.local->socket, SOL_IP, |
| 259 | IP_MTU_DISCOVER, |
| 260 | (char *)&opt, sizeof(opt)); |
| 261 | } |
| 262 | break; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 263 | } |
| 264 | |
David Howells | 985a5c8 | 2016-06-17 11:53:37 +0100 | [diff] [blame] | 265 | up_write(&conn->params.local->defrag_sem); |
| 266 | _leave(" = %d [frag %u]", ret, conn->params.peer->maxdata); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 267 | return ret; |
| 268 | } |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame^] | 269 | |
| 270 | /* |
| 271 | * reject packets through the local endpoint |
| 272 | */ |
| 273 | void rxrpc_reject_packets(struct rxrpc_local *local) |
| 274 | { |
| 275 | union { |
| 276 | struct sockaddr sa; |
| 277 | struct sockaddr_in sin; |
| 278 | } sa; |
| 279 | struct rxrpc_skb_priv *sp; |
| 280 | struct rxrpc_wire_header whdr; |
| 281 | struct sk_buff *skb; |
| 282 | struct msghdr msg; |
| 283 | struct kvec iov[2]; |
| 284 | size_t size; |
| 285 | __be32 code; |
| 286 | |
| 287 | _enter("%d", local->debug_id); |
| 288 | |
| 289 | iov[0].iov_base = &whdr; |
| 290 | iov[0].iov_len = sizeof(whdr); |
| 291 | iov[1].iov_base = &code; |
| 292 | iov[1].iov_len = sizeof(code); |
| 293 | size = sizeof(whdr) + sizeof(code); |
| 294 | |
| 295 | msg.msg_name = &sa; |
| 296 | msg.msg_control = NULL; |
| 297 | msg.msg_controllen = 0; |
| 298 | msg.msg_flags = 0; |
| 299 | |
| 300 | memset(&sa, 0, sizeof(sa)); |
| 301 | sa.sa.sa_family = local->srx.transport.family; |
| 302 | switch (sa.sa.sa_family) { |
| 303 | case AF_INET: |
| 304 | msg.msg_namelen = sizeof(sa.sin); |
| 305 | break; |
| 306 | default: |
| 307 | msg.msg_namelen = 0; |
| 308 | break; |
| 309 | } |
| 310 | |
| 311 | memset(&whdr, 0, sizeof(whdr)); |
| 312 | whdr.type = RXRPC_PACKET_TYPE_ABORT; |
| 313 | |
| 314 | while ((skb = skb_dequeue(&local->reject_queue))) { |
| 315 | rxrpc_see_skb(skb); |
| 316 | sp = rxrpc_skb(skb); |
| 317 | switch (sa.sa.sa_family) { |
| 318 | case AF_INET: |
| 319 | sa.sin.sin_port = udp_hdr(skb)->source; |
| 320 | sa.sin.sin_addr.s_addr = ip_hdr(skb)->saddr; |
| 321 | code = htonl(skb->priority); |
| 322 | |
| 323 | whdr.epoch = htonl(sp->hdr.epoch); |
| 324 | whdr.cid = htonl(sp->hdr.cid); |
| 325 | whdr.callNumber = htonl(sp->hdr.callNumber); |
| 326 | whdr.serviceId = htons(sp->hdr.serviceId); |
| 327 | whdr.flags = sp->hdr.flags; |
| 328 | whdr.flags ^= RXRPC_CLIENT_INITIATED; |
| 329 | whdr.flags &= RXRPC_CLIENT_INITIATED; |
| 330 | |
| 331 | kernel_sendmsg(local->socket, &msg, iov, 2, size); |
| 332 | break; |
| 333 | |
| 334 | default: |
| 335 | break; |
| 336 | } |
| 337 | |
| 338 | rxrpc_free_skb(skb); |
| 339 | } |
| 340 | |
| 341 | _leave(""); |
| 342 | } |