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 * Directly produce an abort from a packet.
72 bool rxrpc_direct_abort(struct sk_buff *skb, enum rxrpc_abort_reason why,
73 s32 abort_code, int err)
75 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
77 trace_rxrpc_abort(0, why, sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
79 skb->mark = RXRPC_SKB_MARK_REJECT_ABORT;
80 skb->priority = abort_code;
84 static bool rxrpc_bad_message(struct sk_buff *skb, enum rxrpc_abort_reason why)
86 return rxrpc_direct_abort(skb, why, RX_PROTOCOL_ERROR, -EBADMSG);
89 #define just_discard true
92 * Process event packets targeted at a local endpoint.
94 static bool rxrpc_input_version(struct rxrpc_local *local, struct sk_buff *skb)
96 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
101 rxrpc_see_skb(skb, rxrpc_skb_see_version);
102 if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header), &v, 1) >= 0) {
104 rxrpc_send_version_request(local, &sp->hdr, skb);
111 * Extract the wire header from a packet and translate the byte order.
113 static bool rxrpc_extract_header(struct rxrpc_skb_priv *sp,
116 struct rxrpc_wire_header whdr;
118 /* dig out the RxRPC connection details */
119 if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0)
120 return rxrpc_bad_message(skb, rxrpc_badmsg_short_hdr);
122 memset(sp, 0, sizeof(*sp));
123 sp->hdr.epoch = ntohl(whdr.epoch);
124 sp->hdr.cid = ntohl(whdr.cid);
125 sp->hdr.callNumber = ntohl(whdr.callNumber);
126 sp->hdr.seq = ntohl(whdr.seq);
127 sp->hdr.serial = ntohl(whdr.serial);
128 sp->hdr.flags = whdr.flags;
129 sp->hdr.type = whdr.type;
130 sp->hdr.userStatus = whdr.userStatus;
131 sp->hdr.securityIndex = whdr.securityIndex;
132 sp->hdr._rsvd = ntohs(whdr._rsvd);
133 sp->hdr.serviceId = ntohs(whdr.serviceId);
138 * Extract the abort code from an ABORT packet and stash it in skb->priority.
140 static bool rxrpc_extract_abort(struct sk_buff *skb)
144 if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
145 &wtmp, sizeof(wtmp)) < 0)
147 skb->priority = ntohl(wtmp);
152 * Process packets received on the local endpoint
154 static bool rxrpc_input_packet(struct rxrpc_local *local, struct sk_buff **_skb)
156 struct rxrpc_connection *conn;
157 struct sockaddr_rxrpc peer_srx;
158 struct rxrpc_skb_priv *sp;
159 struct rxrpc_peer *peer = NULL;
160 struct sk_buff *skb = *_skb;
163 skb_pull(skb, sizeof(struct udphdr));
167 /* dig out the RxRPC connection details */
168 if (!rxrpc_extract_header(sp, skb))
171 if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
173 if ((lose++ & 7) == 7) {
174 trace_rxrpc_rx_lose(sp);
179 trace_rxrpc_rx_packet(sp);
181 switch (sp->hdr.type) {
182 case RXRPC_PACKET_TYPE_VERSION:
183 if (rxrpc_to_client(sp))
185 return rxrpc_input_version(local, skb);
187 case RXRPC_PACKET_TYPE_BUSY:
188 if (rxrpc_to_server(sp))
191 case RXRPC_PACKET_TYPE_ACK:
192 case RXRPC_PACKET_TYPE_ACKALL:
193 if (sp->hdr.callNumber == 0)
194 return rxrpc_bad_message(skb, rxrpc_badmsg_zero_call);
196 case RXRPC_PACKET_TYPE_ABORT:
197 if (!rxrpc_extract_abort(skb))
198 return just_discard; /* Just discard if malformed */
201 case RXRPC_PACKET_TYPE_DATA:
202 if (sp->hdr.callNumber == 0)
203 return rxrpc_bad_message(skb, rxrpc_badmsg_zero_call);
204 if (sp->hdr.seq == 0)
205 return rxrpc_bad_message(skb, rxrpc_badmsg_zero_seq);
207 /* Unshare the packet so that it can be modified for in-place
210 if (sp->hdr.securityIndex != 0) {
211 skb = skb_unshare(skb, GFP_ATOMIC);
213 rxrpc_eaten_skb(*_skb, rxrpc_skb_eaten_by_unshare_nomem);
219 rxrpc_eaten_skb(*_skb, rxrpc_skb_eaten_by_unshare);
221 rxrpc_new_skb(skb, rxrpc_skb_new_unshared);
227 case RXRPC_PACKET_TYPE_CHALLENGE:
228 if (rxrpc_to_server(sp))
231 case RXRPC_PACKET_TYPE_RESPONSE:
232 if (rxrpc_to_client(sp))
236 /* Packet types 9-11 should just be ignored. */
237 case RXRPC_PACKET_TYPE_PARAMS:
238 case RXRPC_PACKET_TYPE_10:
239 case RXRPC_PACKET_TYPE_11:
243 return rxrpc_bad_message(skb, rxrpc_badmsg_unsupported_packet);
246 if (sp->hdr.serviceId == 0)
247 return rxrpc_bad_message(skb, rxrpc_badmsg_zero_service);
249 if (WARN_ON_ONCE(rxrpc_extract_addr_from_skb(&peer_srx, skb) < 0))
250 return just_discard; /* Unsupported address type. */
252 if (peer_srx.transport.family != local->srx.transport.family &&
253 (peer_srx.transport.family == AF_INET &&
254 local->srx.transport.family != AF_INET6)) {
255 pr_warn_ratelimited("AF_RXRPC: Protocol mismatch %u not %u\n",
256 peer_srx.transport.family,
257 local->srx.transport.family);
258 return just_discard; /* Wrong address type. */
261 if (rxrpc_to_client(sp)) {
263 conn = rxrpc_find_client_connection_rcu(local, &peer_srx, skb);
264 conn = rxrpc_get_connection_maybe(conn, rxrpc_conn_get_call_input);
267 return rxrpc_protocol_error(skb, rxrpc_eproto_no_client_conn);
269 ret = rxrpc_input_packet_on_conn(conn, &peer_srx, skb);
270 rxrpc_put_connection(conn, rxrpc_conn_put_call_input);
274 /* We need to look up service connections by the full protocol
275 * parameter set. We look up the peer first as an intermediate step
276 * and then the connection from the peer's tree.
280 peer = rxrpc_lookup_peer_rcu(local, &peer_srx);
283 return rxrpc_new_incoming_call(local, NULL, NULL, &peer_srx, skb);
286 conn = rxrpc_find_service_conn_rcu(peer, skb);
287 conn = rxrpc_get_connection_maybe(conn, rxrpc_conn_get_call_input);
290 ret = rxrpc_input_packet_on_conn(conn, &peer_srx, skb);
291 rxrpc_put_connection(conn, rxrpc_conn_put_call_input);
295 peer = rxrpc_get_peer_maybe(peer, rxrpc_peer_get_input);
298 ret = rxrpc_new_incoming_call(local, peer, NULL, &peer_srx, skb);
299 rxrpc_put_peer(peer, rxrpc_peer_put_input);
304 * Deal with a packet that's associated with an extant connection.
306 static int rxrpc_input_packet_on_conn(struct rxrpc_connection *conn,
307 struct sockaddr_rxrpc *peer_srx,
310 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
311 struct rxrpc_channel *chan;
312 struct rxrpc_call *call = NULL;
313 unsigned int channel;
316 if (sp->hdr.securityIndex != conn->security_ix)
317 return rxrpc_direct_abort(skb, rxrpc_eproto_wrong_security,
318 RXKADINCONSISTENCY, -EBADMSG);
320 if (sp->hdr.serviceId != conn->service_id) {
323 if (!test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags))
324 return rxrpc_protocol_error(skb, rxrpc_eproto_reupgrade);
326 old_id = cmpxchg(&conn->service_id, conn->orig_service_id,
328 if (old_id != conn->orig_service_id &&
329 old_id != sp->hdr.serviceId)
330 return rxrpc_protocol_error(skb, rxrpc_eproto_bad_upgrade);
333 if (after(sp->hdr.serial, conn->hi_serial))
334 conn->hi_serial = sp->hdr.serial;
336 /* It's a connection-level packet if the call number is 0. */
337 if (sp->hdr.callNumber == 0)
338 return rxrpc_input_conn_packet(conn, skb);
340 /* Call-bound packets are routed by connection channel. */
341 channel = sp->hdr.cid & RXRPC_CHANNELMASK;
342 chan = &conn->channels[channel];
344 /* Ignore really old calls */
345 if (sp->hdr.callNumber < chan->last_call)
348 if (sp->hdr.callNumber == chan->last_call) {
350 sp->hdr.type == RXRPC_PACKET_TYPE_ABORT)
353 /* For the previous service call, if completed successfully, we
354 * discard all further packets.
356 if (rxrpc_conn_is_service(conn) &&
357 chan->last_type == RXRPC_PACKET_TYPE_ACK)
360 /* But otherwise we need to retransmit the final packet from
361 * data cached in the connection record.
363 if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA)
364 trace_rxrpc_rx_data(chan->call_debug_id,
368 rxrpc_conn_retransmit_call(conn, skb, channel);
373 call = rxrpc_try_get_call(rcu_dereference(chan->call),
374 rxrpc_call_get_input);
377 if (sp->hdr.callNumber > chan->call_id) {
378 if (rxrpc_to_client(sp)) {
379 rxrpc_put_call(call, rxrpc_call_put_input);
380 return rxrpc_protocol_error(skb,
381 rxrpc_eproto_unexpected_implicit_end);
385 rxrpc_implicit_end_call(call, skb);
386 rxrpc_put_call(call, rxrpc_call_put_input);
392 if (rxrpc_to_client(sp))
393 return rxrpc_protocol_error(skb, rxrpc_eproto_no_client_call);
394 return rxrpc_new_incoming_call(conn->local, conn->peer, conn,
398 ret = rxrpc_input_call_event(call, skb);
399 rxrpc_put_call(call, rxrpc_call_put_input);
404 * I/O and event handling thread.
406 int rxrpc_io_thread(void *data)
408 struct rxrpc_connection *conn;
409 struct sk_buff_head rx_queue;
410 struct rxrpc_local *local = data;
411 struct rxrpc_call *call;
415 complete(&local->io_thread_ready);
417 skb_queue_head_init(&rx_queue);
419 set_user_nice(current, MIN_NICE);
422 rxrpc_inc_stat(local->rxnet, stat_io_loop);
424 /* Deal with connections that want immediate attention. */
425 conn = list_first_entry_or_null(&local->conn_attend_q,
426 struct rxrpc_connection,
429 spin_lock_bh(&local->lock);
430 list_del_init(&conn->attend_link);
431 spin_unlock_bh(&local->lock);
433 rxrpc_input_conn_event(conn, NULL);
434 rxrpc_put_connection(conn, rxrpc_conn_put_poke);
438 /* Deal with calls that want immediate attention. */
439 if ((call = list_first_entry_or_null(&local->call_attend_q,
442 spin_lock_bh(&local->lock);
443 list_del_init(&call->attend_link);
444 spin_unlock_bh(&local->lock);
446 trace_rxrpc_call_poked(call);
447 rxrpc_input_call_event(call, NULL);
448 rxrpc_put_call(call, rxrpc_call_put_poke);
452 /* Process received packets and errors. */
453 if ((skb = __skb_dequeue(&rx_queue))) {
455 case RXRPC_SKB_MARK_PACKET:
457 if (!rxrpc_input_packet(local, &skb))
458 rxrpc_reject_packet(local, skb);
459 trace_rxrpc_rx_done(skb->mark, skb->priority);
460 rxrpc_free_skb(skb, rxrpc_skb_put_input);
462 case RXRPC_SKB_MARK_ERROR:
463 rxrpc_input_error(local, skb);
464 rxrpc_free_skb(skb, rxrpc_skb_put_error_report);
469 rxrpc_free_skb(skb, rxrpc_skb_put_unknown);
475 if (!skb_queue_empty(&local->rx_queue)) {
476 spin_lock_irq(&local->rx_queue.lock);
477 skb_queue_splice_tail_init(&local->rx_queue, &rx_queue);
478 spin_unlock_irq(&local->rx_queue.lock);
482 set_current_state(TASK_INTERRUPTIBLE);
483 should_stop = kthread_should_stop();
484 if (!skb_queue_empty(&local->rx_queue) ||
485 !list_empty(&local->call_attend_q) ||
486 !list_empty(&local->conn_attend_q)) {
487 __set_current_state(TASK_RUNNING);
496 __set_current_state(TASK_RUNNING);
497 rxrpc_see_local(local, rxrpc_local_stop);
498 rxrpc_destroy_local(local);
499 local->io_thread = NULL;
500 rxrpc_see_local(local, rxrpc_local_stopped);