1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* RxRPC packet reception
4 * Copyright (C) 2007, 2016, 2022 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include "ar-internal.h"
12 static int rxrpc_input_packet_on_conn(struct rxrpc_connection *conn,
13 struct sockaddr_rxrpc *peer_srx,
17 * handle data received on the local endpoint
18 * - may be called in interrupt context
20 * [!] Note that as this is called from the encap_rcv hook, the socket is not
21 * held locked by the caller and nothing prevents sk_user_data on the UDP from
22 * being cleared in the middle of processing this function.
24 * Called with the RCU read lock held from the IP layer via UDP.
26 int rxrpc_encap_rcv(struct sock *udp_sk, struct sk_buff *skb)
28 struct rxrpc_local *local = rcu_dereference_sk_user_data(udp_sk);
30 if (unlikely(!local)) {
35 skb->tstamp = ktime_get_real();
37 skb->mark = RXRPC_SKB_MARK_PACKET;
38 rxrpc_new_skb(skb, rxrpc_skb_new_encap_rcv);
39 skb_queue_tail(&local->rx_queue, skb);
40 rxrpc_wake_up_io_thread(local);
45 * Handle an error received on the local endpoint.
47 void rxrpc_error_report(struct sock *sk)
49 struct rxrpc_local *local;
53 local = rcu_dereference_sk_user_data(sk);
54 if (unlikely(!local)) {
59 while ((skb = skb_dequeue(&sk->sk_error_queue))) {
60 skb->mark = RXRPC_SKB_MARK_ERROR;
61 rxrpc_new_skb(skb, rxrpc_skb_new_error_report);
62 skb_queue_tail(&local->rx_queue, skb);
65 rxrpc_wake_up_io_thread(local);
70 * Process event packets targeted at a local endpoint.
72 static void rxrpc_input_version(struct rxrpc_local *local, struct sk_buff *skb)
74 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
79 rxrpc_see_skb(skb, rxrpc_skb_see_version);
80 if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header), &v, 1) >= 0) {
82 rxrpc_send_version_request(local, &sp->hdr, skb);
87 * Extract the wire header from a packet and translate the byte order.
90 int rxrpc_extract_header(struct rxrpc_skb_priv *sp, struct sk_buff *skb)
92 struct rxrpc_wire_header whdr;
94 /* dig out the RxRPC connection details */
95 if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0) {
96 trace_rxrpc_rx_eproto(NULL, sp->hdr.serial,
97 tracepoint_string("bad_hdr"));
101 memset(sp, 0, sizeof(*sp));
102 sp->hdr.epoch = ntohl(whdr.epoch);
103 sp->hdr.cid = ntohl(whdr.cid);
104 sp->hdr.callNumber = ntohl(whdr.callNumber);
105 sp->hdr.seq = ntohl(whdr.seq);
106 sp->hdr.serial = ntohl(whdr.serial);
107 sp->hdr.flags = whdr.flags;
108 sp->hdr.type = whdr.type;
109 sp->hdr.userStatus = whdr.userStatus;
110 sp->hdr.securityIndex = whdr.securityIndex;
111 sp->hdr._rsvd = ntohs(whdr._rsvd);
112 sp->hdr.serviceId = ntohs(whdr.serviceId);
117 * Extract the abort code from an ABORT packet and stash it in skb->priority.
119 static bool rxrpc_extract_abort(struct sk_buff *skb)
123 if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
124 &wtmp, sizeof(wtmp)) < 0)
126 skb->priority = ntohl(wtmp);
131 * Process packets received on the local endpoint
133 static int rxrpc_input_packet(struct rxrpc_local *local, struct sk_buff **_skb)
135 struct rxrpc_connection *conn;
136 struct sockaddr_rxrpc peer_srx;
137 struct rxrpc_skb_priv *sp;
138 struct rxrpc_peer *peer = NULL;
139 struct sk_buff *skb = *_skb;
142 skb_pull(skb, sizeof(struct udphdr));
146 /* dig out the RxRPC connection details */
147 if (rxrpc_extract_header(sp, skb) < 0)
150 if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
152 if ((lose++ & 7) == 7) {
153 trace_rxrpc_rx_lose(sp);
158 trace_rxrpc_rx_packet(sp);
160 switch (sp->hdr.type) {
161 case RXRPC_PACKET_TYPE_VERSION:
162 if (rxrpc_to_client(sp))
164 rxrpc_input_version(local, skb);
167 case RXRPC_PACKET_TYPE_BUSY:
168 if (rxrpc_to_server(sp))
171 case RXRPC_PACKET_TYPE_ACK:
172 case RXRPC_PACKET_TYPE_ACKALL:
173 if (sp->hdr.callNumber == 0)
176 case RXRPC_PACKET_TYPE_ABORT:
177 if (!rxrpc_extract_abort(skb))
178 return 0; /* Just discard if malformed */
181 case RXRPC_PACKET_TYPE_DATA:
182 if (sp->hdr.callNumber == 0 ||
186 /* Unshare the packet so that it can be modified for in-place
189 if (sp->hdr.securityIndex != 0) {
190 skb = skb_unshare(skb, GFP_ATOMIC);
192 rxrpc_eaten_skb(*_skb, rxrpc_skb_eaten_by_unshare_nomem);
198 rxrpc_eaten_skb(*_skb, rxrpc_skb_eaten_by_unshare);
200 rxrpc_new_skb(skb, rxrpc_skb_new_unshared);
206 case RXRPC_PACKET_TYPE_CHALLENGE:
207 if (rxrpc_to_server(sp))
210 case RXRPC_PACKET_TYPE_RESPONSE:
211 if (rxrpc_to_client(sp))
215 /* Packet types 9-11 should just be ignored. */
216 case RXRPC_PACKET_TYPE_PARAMS:
217 case RXRPC_PACKET_TYPE_10:
218 case RXRPC_PACKET_TYPE_11:
225 if (sp->hdr.serviceId == 0)
228 if (WARN_ON_ONCE(rxrpc_extract_addr_from_skb(&peer_srx, skb) < 0))
229 return true; /* Unsupported address type - discard. */
231 if (peer_srx.transport.family != local->srx.transport.family &&
232 (peer_srx.transport.family == AF_INET &&
233 local->srx.transport.family != AF_INET6)) {
234 pr_warn_ratelimited("AF_RXRPC: Protocol mismatch %u not %u\n",
235 peer_srx.transport.family,
236 local->srx.transport.family);
237 return true; /* Wrong address type - discard. */
240 if (rxrpc_to_client(sp)) {
242 conn = rxrpc_find_client_connection_rcu(local, &peer_srx, skb);
243 conn = rxrpc_get_connection_maybe(conn, rxrpc_conn_get_call_input);
246 trace_rxrpc_abort(0, "NCC", sp->hdr.cid,
247 sp->hdr.callNumber, sp->hdr.seq,
248 RXKADINCONSISTENCY, EBADMSG);
252 ret = rxrpc_input_packet_on_conn(conn, &peer_srx, skb);
253 rxrpc_put_connection(conn, rxrpc_conn_put_call_input);
257 /* We need to look up service connections by the full protocol
258 * parameter set. We look up the peer first as an intermediate step
259 * and then the connection from the peer's tree.
263 peer = rxrpc_lookup_peer_rcu(local, &peer_srx);
266 return rxrpc_new_incoming_call(local, NULL, NULL, &peer_srx, skb);
269 conn = rxrpc_find_service_conn_rcu(peer, skb);
270 conn = rxrpc_get_connection_maybe(conn, rxrpc_conn_get_call_input);
273 ret = rxrpc_input_packet_on_conn(conn, &peer_srx, skb);
274 rxrpc_put_connection(conn, rxrpc_conn_put_call_input);
278 peer = rxrpc_get_peer_maybe(peer, rxrpc_peer_get_input);
281 ret = rxrpc_new_incoming_call(local, peer, NULL, &peer_srx, skb);
282 rxrpc_put_peer(peer, rxrpc_peer_put_input);
288 trace_rxrpc_abort(0, "BAD", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
289 RX_PROTOCOL_ERROR, EBADMSG);
291 skb->priority = RX_PROTOCOL_ERROR;
292 skb->mark = RXRPC_SKB_MARK_REJECT_ABORT;
294 rxrpc_reject_packet(local, skb);
299 * Deal with a packet that's associated with an extant connection.
301 static int rxrpc_input_packet_on_conn(struct rxrpc_connection *conn,
302 struct sockaddr_rxrpc *peer_srx,
305 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
306 struct rxrpc_channel *chan;
307 struct rxrpc_call *call = NULL;
308 unsigned int channel;
310 if (sp->hdr.securityIndex != conn->security_ix)
313 if (sp->hdr.serviceId != conn->service_id) {
316 if (!test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags))
318 old_id = cmpxchg(&conn->service_id, conn->orig_service_id,
321 if (old_id != conn->orig_service_id &&
322 old_id != sp->hdr.serviceId)
326 if (after(sp->hdr.serial, conn->hi_serial))
327 conn->hi_serial = sp->hdr.serial;
329 /* It's a connection-level packet if the call number is 0. */
330 if (sp->hdr.callNumber == 0)
331 return rxrpc_input_conn_packet(conn, skb);
333 /* Call-bound packets are routed by connection channel. */
334 channel = sp->hdr.cid & RXRPC_CHANNELMASK;
335 chan = &conn->channels[channel];
337 /* Ignore really old calls */
338 if (sp->hdr.callNumber < chan->last_call)
341 if (sp->hdr.callNumber == chan->last_call) {
343 sp->hdr.type == RXRPC_PACKET_TYPE_ABORT)
346 /* For the previous service call, if completed successfully, we
347 * discard all further packets.
349 if (rxrpc_conn_is_service(conn) &&
350 chan->last_type == RXRPC_PACKET_TYPE_ACK)
353 /* But otherwise we need to retransmit the final packet from
354 * data cached in the connection record.
356 if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA)
357 trace_rxrpc_rx_data(chan->call_debug_id,
361 rxrpc_input_conn_packet(conn, skb);
366 call = rxrpc_try_get_call(rcu_dereference(chan->call),
367 rxrpc_call_get_input);
370 if (sp->hdr.callNumber > chan->call_id) {
371 if (rxrpc_to_client(sp)) {
372 rxrpc_put_call(call, rxrpc_call_put_input);
377 rxrpc_implicit_end_call(call, skb);
378 rxrpc_put_call(call, rxrpc_call_put_input);
384 if (rxrpc_to_client(sp))
386 if (rxrpc_new_incoming_call(conn->local, conn->peer, conn,
392 rxrpc_input_call_event(call, skb);
393 rxrpc_put_call(call, rxrpc_call_put_input);
397 trace_rxrpc_abort(0, "SEC", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
398 RXKADINCONSISTENCY, EBADMSG);
399 skb->priority = RXKADINCONSISTENCY;
403 trace_rxrpc_abort(0, "UPG", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
404 RX_PROTOCOL_ERROR, EBADMSG);
408 trace_rxrpc_abort(0, "BAD", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
409 RX_PROTOCOL_ERROR, EBADMSG);
411 skb->priority = RX_PROTOCOL_ERROR;
413 skb->mark = RXRPC_SKB_MARK_REJECT_ABORT;
415 rxrpc_reject_packet(conn->local, skb);
420 * I/O and event handling thread.
422 int rxrpc_io_thread(void *data)
424 struct sk_buff_head rx_queue;
425 struct rxrpc_local *local = data;
426 struct rxrpc_call *call;
429 skb_queue_head_init(&rx_queue);
431 set_user_nice(current, MIN_NICE);
434 rxrpc_inc_stat(local->rxnet, stat_io_loop);
436 /* Deal with calls that want immediate attention. */
437 if ((call = list_first_entry_or_null(&local->call_attend_q,
440 spin_lock_bh(&local->lock);
441 list_del_init(&call->attend_link);
442 spin_unlock_bh(&local->lock);
444 trace_rxrpc_call_poked(call);
445 rxrpc_input_call_event(call, NULL);
446 rxrpc_put_call(call, rxrpc_call_put_poke);
450 /* Process received packets and errors. */
451 if ((skb = __skb_dequeue(&rx_queue))) {
453 case RXRPC_SKB_MARK_PACKET:
455 rxrpc_input_packet(local, &skb);
456 trace_rxrpc_rx_done(skb->mark, skb->priority);
457 rxrpc_free_skb(skb, rxrpc_skb_put_input);
459 case RXRPC_SKB_MARK_ERROR:
460 rxrpc_input_error(local, skb);
461 rxrpc_free_skb(skb, rxrpc_skb_put_error_report);
465 rxrpc_free_skb(skb, rxrpc_skb_put_unknown);
471 if (!skb_queue_empty(&local->rx_queue)) {
472 spin_lock_irq(&local->rx_queue.lock);
473 skb_queue_splice_tail_init(&local->rx_queue, &rx_queue);
474 spin_unlock_irq(&local->rx_queue.lock);
478 set_current_state(TASK_INTERRUPTIBLE);
479 if (!skb_queue_empty(&local->rx_queue) ||
480 !list_empty(&local->call_attend_q)) {
481 __set_current_state(TASK_RUNNING);
485 if (kthread_should_stop())
490 __set_current_state(TASK_RUNNING);
491 rxrpc_see_local(local, rxrpc_local_stop);
492 rxrpc_destroy_local(local);
493 local->io_thread = NULL;
494 rxrpc_see_local(local, rxrpc_local_stopped);