1 /* RxRPC packet reception
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/module.h>
15 #include <linux/net.h>
16 #include <linux/skbuff.h>
17 #include <linux/errqueue.h>
18 #include <linux/udp.h>
20 #include <linux/in6.h>
21 #include <linux/icmp.h>
22 #include <linux/gfp.h>
24 #include <net/af_rxrpc.h>
27 #include <net/net_namespace.h>
28 #include "ar-internal.h"
31 * queue a packet for recvmsg to pass to userspace
32 * - the caller must hold a lock on call->lock
33 * - must not be called with interrupts disabled (sk_filter() disables BH's)
34 * - eats the packet whether successful or not
35 * - there must be just one reference to the packet, which the caller passes to
38 int rxrpc_queue_rcv_skb(struct rxrpc_call *call, struct sk_buff *skb,
39 bool force, bool terminal)
41 struct rxrpc_skb_priv *sp;
42 struct rxrpc_sock *rx = call->socket;
46 _enter(",,%d,%d", force, terminal);
48 ASSERT(!irqs_disabled());
51 ASSERTCMP(sp->call, ==, call);
53 /* if we've already posted the terminal message for a call, then we
54 * don't post any more */
55 if (test_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags)) {
56 _debug("already terminated");
57 ASSERTCMP(call->state, >=, RXRPC_CALL_COMPLETE);
65 /* cast skb->rcvbuf to unsigned... It's pointless, but
66 * reduces number of warnings when compiling with -W
69 // if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
70 // (unsigned int) sk->sk_rcvbuf)
73 ret = sk_filter(sk, skb);
78 spin_lock_bh(&sk->sk_receive_queue.lock);
79 if (!test_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags) &&
80 !test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
81 call->socket->sk.sk_state != RXRPC_CLOSE) {
82 skb->destructor = rxrpc_packet_destructor;
85 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
88 _debug("<<<< TERMINAL MESSAGE >>>>");
89 set_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags);
92 /* allow interception by a kernel service */
93 if (rx->interceptor) {
94 rx->interceptor(sk, call->user_call_ID, skb);
95 spin_unlock_bh(&sk->sk_receive_queue.lock);
97 _net("post skb %p", skb);
98 __skb_queue_tail(&sk->sk_receive_queue, skb);
99 spin_unlock_bh(&sk->sk_receive_queue.lock);
101 if (!sock_flag(sk, SOCK_DEAD))
102 sk->sk_data_ready(sk);
106 spin_unlock_bh(&sk->sk_receive_queue.lock);
113 _leave(" = %d", ret);
118 * process a DATA packet, posting the packet to the appropriate queue
119 * - eats the packet if successful
121 static int rxrpc_fast_process_data(struct rxrpc_call *call,
122 struct sk_buff *skb, u32 seq)
124 struct rxrpc_skb_priv *sp;
126 int ret, ackbit, ack;
130 _enter("{%u,%u},,{%u}", call->rx_data_post, call->rx_first_oos, seq);
133 ASSERTCMP(sp->call, ==, NULL);
134 flags = sp->hdr.flags;
135 serial = sp->hdr.serial;
137 spin_lock(&call->lock);
139 if (call->state > RXRPC_CALL_COMPLETE)
142 ASSERTCMP(call->rx_data_expect, >=, call->rx_data_post);
143 ASSERTCMP(call->rx_data_post, >=, call->rx_data_recv);
144 ASSERTCMP(call->rx_data_recv, >=, call->rx_data_eaten);
146 if (seq < call->rx_data_post) {
147 _debug("dup #%u [-%u]", seq, call->rx_data_post);
148 ack = RXRPC_ACK_DUPLICATE;
150 goto discard_and_ack;
153 /* we may already have the packet in the out of sequence queue */
154 ackbit = seq - (call->rx_data_eaten + 1);
155 ASSERTCMP(ackbit, >=, 0);
156 if (__test_and_set_bit(ackbit, call->ackr_window)) {
157 _debug("dup oos #%u [%u,%u]",
158 seq, call->rx_data_eaten, call->rx_data_post);
159 ack = RXRPC_ACK_DUPLICATE;
160 goto discard_and_ack;
163 if (seq >= call->ackr_win_top) {
164 _debug("exceed #%u [%u]", seq, call->ackr_win_top);
165 __clear_bit(ackbit, call->ackr_window);
166 ack = RXRPC_ACK_EXCEEDS_WINDOW;
167 goto discard_and_ack;
170 if (seq == call->rx_data_expect) {
171 clear_bit(RXRPC_CALL_EXPECT_OOS, &call->flags);
172 call->rx_data_expect++;
173 } else if (seq > call->rx_data_expect) {
174 _debug("oos #%u [%u]", seq, call->rx_data_expect);
175 call->rx_data_expect = seq + 1;
176 if (test_and_set_bit(RXRPC_CALL_EXPECT_OOS, &call->flags)) {
177 ack = RXRPC_ACK_OUT_OF_SEQUENCE;
178 goto enqueue_and_ack;
183 if (seq != call->rx_data_post) {
184 _debug("ahead #%u [%u]", seq, call->rx_data_post);
188 if (test_bit(RXRPC_CALL_RCVD_LAST, &call->flags))
191 /* if the packet need security things doing to it, then it goes down
193 if (call->conn->security_ix)
197 rxrpc_get_call(call);
198 atomic_inc(&call->skb_count);
199 terminal = ((flags & RXRPC_LAST_PACKET) &&
200 !(flags & RXRPC_CLIENT_INITIATED));
201 ret = rxrpc_queue_rcv_skb(call, skb, false, terminal);
203 if (ret == -ENOMEM || ret == -ENOBUFS) {
204 __clear_bit(ackbit, call->ackr_window);
205 ack = RXRPC_ACK_NOSPACE;
206 goto discard_and_ack;
214 _debug("post #%u", seq);
215 ASSERTCMP(call->rx_data_post, ==, seq);
216 call->rx_data_post++;
218 if (flags & RXRPC_LAST_PACKET)
219 set_bit(RXRPC_CALL_RCVD_LAST, &call->flags);
221 /* if we've reached an out of sequence packet then we need to drain
222 * that queue into the socket Rx queue now */
223 if (call->rx_data_post == call->rx_first_oos) {
224 _debug("drain rx oos now");
225 read_lock(&call->state_lock);
226 if (call->state < RXRPC_CALL_COMPLETE &&
227 !test_and_set_bit(RXRPC_CALL_EV_DRAIN_RX_OOS, &call->events))
228 rxrpc_queue_call(call);
229 read_unlock(&call->state_lock);
232 spin_unlock(&call->lock);
233 atomic_inc(&call->ackr_not_idle);
234 rxrpc_propose_ACK(call, RXRPC_ACK_DELAY, serial, false);
235 _leave(" = 0 [posted]");
241 spin_unlock(&call->lock);
242 _leave(" = %d", ret);
246 _debug("discard and ACK packet %p", skb);
247 __rxrpc_propose_ACK(call, ack, serial, true);
249 spin_unlock(&call->lock);
251 _leave(" = 0 [discarded]");
255 __rxrpc_propose_ACK(call, ack, serial, true);
257 _net("defer skb %p", skb);
258 spin_unlock(&call->lock);
259 skb_queue_tail(&call->rx_queue, skb);
260 atomic_inc(&call->ackr_not_idle);
261 read_lock(&call->state_lock);
262 if (call->state < RXRPC_CALL_DEAD)
263 rxrpc_queue_call(call);
264 read_unlock(&call->state_lock);
265 _leave(" = 0 [queued]");
270 * assume an implicit ACKALL of the transmission phase of a client socket upon
271 * reception of the first reply packet
273 static void rxrpc_assume_implicit_ackall(struct rxrpc_call *call, u32 serial)
275 write_lock_bh(&call->state_lock);
277 switch (call->state) {
278 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
279 call->state = RXRPC_CALL_CLIENT_RECV_REPLY;
280 call->acks_latest = serial;
282 _debug("implicit ACKALL %%%u", call->acks_latest);
283 set_bit(RXRPC_CALL_EV_RCVD_ACKALL, &call->events);
284 write_unlock_bh(&call->state_lock);
286 if (try_to_del_timer_sync(&call->resend_timer) >= 0) {
287 clear_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events);
288 clear_bit(RXRPC_CALL_EV_RESEND, &call->events);
289 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
294 write_unlock_bh(&call->state_lock);
300 * post an incoming packet to the nominated call to deal with
301 * - must get rid of the sk_buff, either by freeing it or by queuing it
303 void rxrpc_fast_process_packet(struct rxrpc_call *call, struct sk_buff *skb)
305 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
307 u32 hi_serial, abort_code;
309 _enter("%p,%p", call, skb);
311 ASSERT(!irqs_disabled());
313 #if 0 // INJECT RX ERROR
314 if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA) {
317 printk("DROPPED 3RD PACKET!!!!!!!!!!!!!\n");
324 /* track the latest serial number on this connection for ACK packet
326 hi_serial = atomic_read(&call->conn->hi_serial);
327 while (sp->hdr.serial > hi_serial)
328 hi_serial = atomic_cmpxchg(&call->conn->hi_serial, hi_serial,
331 /* request ACK generation for any ACK or DATA packet that requests
333 if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
334 _proto("ACK Requested on %%%u", sp->hdr.serial);
335 rxrpc_propose_ACK(call, RXRPC_ACK_REQUESTED, sp->hdr.serial, false);
338 switch (sp->hdr.type) {
339 case RXRPC_PACKET_TYPE_ABORT:
342 if (skb_copy_bits(skb, 0, &wtmp, sizeof(wtmp)) < 0)
345 abort_code = ntohl(wtmp);
346 _proto("Rx ABORT %%%u { %x }", sp->hdr.serial, abort_code);
348 write_lock_bh(&call->state_lock);
349 if (call->state < RXRPC_CALL_COMPLETE) {
350 call->state = RXRPC_CALL_REMOTELY_ABORTED;
351 call->remote_abort = abort_code;
352 set_bit(RXRPC_CALL_EV_RCVD_ABORT, &call->events);
353 rxrpc_queue_call(call);
355 goto free_packet_unlock;
357 case RXRPC_PACKET_TYPE_BUSY:
358 _proto("Rx BUSY %%%u", sp->hdr.serial);
360 if (rxrpc_conn_is_service(call->conn))
363 write_lock_bh(&call->state_lock);
364 switch (call->state) {
365 case RXRPC_CALL_CLIENT_SEND_REQUEST:
366 call->state = RXRPC_CALL_SERVER_BUSY;
367 set_bit(RXRPC_CALL_EV_RCVD_BUSY, &call->events);
368 rxrpc_queue_call(call);
369 case RXRPC_CALL_SERVER_BUSY:
370 goto free_packet_unlock;
372 goto protocol_error_locked;
376 _proto("Rx %s %%%u", rxrpc_pkts[sp->hdr.type], sp->hdr.serial);
379 case RXRPC_PACKET_TYPE_DATA:
380 _proto("Rx DATA %%%u { #%u }", sp->hdr.serial, sp->hdr.seq);
382 if (sp->hdr.seq == 0)
385 call->ackr_prev_seq = sp->hdr.seq;
387 /* received data implicitly ACKs all of the request packets we
388 * sent when we're acting as a client */
389 if (call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY)
390 rxrpc_assume_implicit_ackall(call, sp->hdr.serial);
392 switch (rxrpc_fast_process_data(call, skb, sp->hdr.seq)) {
400 /* data packet received beyond the last packet */
405 case RXRPC_PACKET_TYPE_ACKALL:
406 case RXRPC_PACKET_TYPE_ACK:
407 /* ACK processing is done in process context */
408 read_lock_bh(&call->state_lock);
409 if (call->state < RXRPC_CALL_DEAD) {
410 skb_queue_tail(&call->rx_queue, skb);
411 rxrpc_queue_call(call);
414 read_unlock_bh(&call->state_lock);
419 _debug("protocol error");
420 write_lock_bh(&call->state_lock);
421 protocol_error_locked:
422 if (call->state <= RXRPC_CALL_COMPLETE) {
423 call->state = RXRPC_CALL_LOCALLY_ABORTED;
424 call->local_abort = RX_PROTOCOL_ERROR;
425 set_bit(RXRPC_CALL_EV_ABORT, &call->events);
426 rxrpc_queue_call(call);
429 write_unlock_bh(&call->state_lock);
437 * split up a jumbo data packet
439 static void rxrpc_process_jumbo_packet(struct rxrpc_call *call,
440 struct sk_buff *jumbo)
442 struct rxrpc_jumbo_header jhdr;
443 struct rxrpc_skb_priv *sp;
444 struct sk_buff *part;
446 _enter(",{%u,%u}", jumbo->data_len, jumbo->len);
448 sp = rxrpc_skb(jumbo);
451 sp->hdr.flags &= ~RXRPC_JUMBO_PACKET;
453 /* make a clone to represent the first subpacket in what's left
454 * of the jumbo packet */
455 part = skb_clone(jumbo, GFP_ATOMIC);
457 /* simply ditch the tail in the event of ENOMEM */
458 pskb_trim(jumbo, RXRPC_JUMBO_DATALEN);
463 pskb_trim(part, RXRPC_JUMBO_DATALEN);
465 if (!pskb_pull(jumbo, RXRPC_JUMBO_DATALEN))
468 if (skb_copy_bits(jumbo, 0, &jhdr, sizeof(jhdr)) < 0)
470 if (!pskb_pull(jumbo, sizeof(jhdr)))
475 sp->hdr.flags = jhdr.flags;
476 sp->hdr._rsvd = ntohs(jhdr._rsvd);
478 _proto("Rx DATA Jumbo %%%u", sp->hdr.serial - 1);
480 rxrpc_fast_process_packet(call, part);
483 } while (sp->hdr.flags & RXRPC_JUMBO_PACKET);
485 rxrpc_fast_process_packet(call, jumbo);
490 _debug("protocol error");
491 rxrpc_free_skb(part);
492 rxrpc_free_skb(jumbo);
493 write_lock_bh(&call->state_lock);
494 if (call->state <= RXRPC_CALL_COMPLETE) {
495 call->state = RXRPC_CALL_LOCALLY_ABORTED;
496 call->local_abort = RX_PROTOCOL_ERROR;
497 set_bit(RXRPC_CALL_EV_ABORT, &call->events);
498 rxrpc_queue_call(call);
500 write_unlock_bh(&call->state_lock);
505 * post an incoming packet to the appropriate call/socket to deal with
506 * - must get rid of the sk_buff, either by freeing it or by queuing it
508 static void rxrpc_post_packet_to_call(struct rxrpc_call *call,
511 struct rxrpc_skb_priv *sp;
513 _enter("%p,%p", call, skb);
517 _debug("extant call [%d]", call->state);
519 read_lock(&call->state_lock);
520 switch (call->state) {
521 case RXRPC_CALL_LOCALLY_ABORTED:
522 if (!test_and_set_bit(RXRPC_CALL_EV_ABORT, &call->events)) {
523 rxrpc_queue_call(call);
526 case RXRPC_CALL_REMOTELY_ABORTED:
527 case RXRPC_CALL_NETWORK_ERROR:
528 case RXRPC_CALL_DEAD:
530 case RXRPC_CALL_COMPLETE:
531 case RXRPC_CALL_CLIENT_FINAL_ACK:
532 /* complete server call */
533 if (rxrpc_conn_is_service(call->conn))
535 /* resend last packet of a completed call */
536 _debug("final ack again");
537 rxrpc_get_call(call);
538 set_bit(RXRPC_CALL_EV_ACK_FINAL, &call->events);
539 rxrpc_queue_call(call);
545 read_unlock(&call->state_lock);
546 rxrpc_get_call(call);
548 if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
549 sp->hdr.flags & RXRPC_JUMBO_PACKET)
550 rxrpc_process_jumbo_packet(call, skb);
552 rxrpc_fast_process_packet(call, skb);
554 rxrpc_put_call(call);
558 if (sp->hdr.type != RXRPC_PACKET_TYPE_ABORT) {
559 skb->priority = RX_CALL_DEAD;
560 rxrpc_reject_packet(call->conn->params.local, skb);
566 read_unlock(&call->state_lock);
572 * post connection-level events to the connection
573 * - this includes challenges, responses and some aborts
575 static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
578 _enter("%p,%p", conn, skb);
580 skb_queue_tail(&conn->rx_queue, skb);
581 rxrpc_queue_conn(conn);
585 * post endpoint-level events to the local endpoint
586 * - this includes debug and version messages
588 static void rxrpc_post_packet_to_local(struct rxrpc_local *local,
591 _enter("%p,%p", local, skb);
593 skb_queue_tail(&local->event_queue, skb);
594 rxrpc_queue_local(local);
598 * Extract the wire header from a packet and translate the byte order.
601 int rxrpc_extract_header(struct rxrpc_skb_priv *sp, struct sk_buff *skb)
603 struct rxrpc_wire_header whdr;
605 /* dig out the RxRPC connection details */
606 if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0)
608 if (!pskb_pull(skb, sizeof(whdr)))
611 memset(sp, 0, sizeof(*sp));
612 sp->hdr.epoch = ntohl(whdr.epoch);
613 sp->hdr.cid = ntohl(whdr.cid);
614 sp->hdr.callNumber = ntohl(whdr.callNumber);
615 sp->hdr.seq = ntohl(whdr.seq);
616 sp->hdr.serial = ntohl(whdr.serial);
617 sp->hdr.flags = whdr.flags;
618 sp->hdr.type = whdr.type;
619 sp->hdr.userStatus = whdr.userStatus;
620 sp->hdr.securityIndex = whdr.securityIndex;
621 sp->hdr._rsvd = ntohs(whdr._rsvd);
622 sp->hdr.serviceId = ntohs(whdr.serviceId);
627 * handle data received on the local endpoint
628 * - may be called in interrupt context
630 * The socket is locked by the caller and this prevents the socket from being
631 * shut down and the local endpoint from going away, thus sk_user_data will not
632 * be cleared until this function returns.
634 void rxrpc_data_ready(struct sock *sk)
636 struct rxrpc_connection *conn;
637 struct rxrpc_skb_priv *sp;
638 struct rxrpc_local *local = sk->sk_user_data;
644 ASSERT(!irqs_disabled());
646 skb = skb_recv_datagram(sk, 0, 1, &ret);
650 _debug("UDP socket error %d", ret);
656 _net("recv skb %p", skb);
658 /* we'll probably need to checksum it (didn't call sock_recvmsg) */
659 if (skb_checksum_complete(skb)) {
661 __UDP_INC_STATS(&init_net, UDP_MIB_INERRORS, 0);
662 _leave(" [CSUM failed]");
666 __UDP_INC_STATS(&init_net, UDP_MIB_INDATAGRAMS, 0);
668 /* The socket buffer we have is owned by UDP, with UDP's data all over
669 * it, but we really want our own data there.
674 _net("Rx UDP packet from %08x:%04hu",
675 ntohl(ip_hdr(skb)->saddr), ntohs(udp_hdr(skb)->source));
677 /* dig out the RxRPC connection details */
678 if (rxrpc_extract_header(sp, skb) < 0)
681 _net("Rx RxRPC %s ep=%x call=%x:%x",
682 sp->hdr.flags & RXRPC_CLIENT_INITIATED ? "ToServer" : "ToClient",
683 sp->hdr.epoch, sp->hdr.cid, sp->hdr.callNumber);
685 if (sp->hdr.type >= RXRPC_N_PACKET_TYPES ||
686 !((RXRPC_SUPPORTED_PACKET_TYPES >> sp->hdr.type) & 1)) {
687 _proto("Rx Bad Packet Type %u", sp->hdr.type);
691 if (sp->hdr.type == RXRPC_PACKET_TYPE_VERSION) {
692 rxrpc_post_packet_to_local(local, skb);
696 if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
697 (sp->hdr.callNumber == 0 || sp->hdr.seq == 0))
702 conn = rxrpc_find_connection_rcu(local, skb);
704 goto cant_route_call;
706 if (sp->hdr.callNumber == 0) {
707 /* Connection-level packet */
708 _debug("CONN %p {%d}", conn, conn->debug_id);
709 rxrpc_post_packet_to_conn(conn, skb);
711 /* Call-bound packets are routed by connection channel. */
712 unsigned int channel = sp->hdr.cid & RXRPC_CHANNELMASK;
713 struct rxrpc_channel *chan = &conn->channels[channel];
714 struct rxrpc_call *call = rcu_dereference(chan->call);
716 if (!call || atomic_read(&call->usage) == 0)
717 goto cant_route_call;
719 rxrpc_post_packet_to_call(call, skb);
729 _debug("can't route call");
730 if (sp->hdr.flags & RXRPC_CLIENT_INITIATED &&
731 sp->hdr.type == RXRPC_PACKET_TYPE_DATA) {
732 if (sp->hdr.seq == 1) {
733 _debug("first packet");
734 skb_queue_tail(&local->accept_queue, skb);
735 rxrpc_queue_work(&local->processor);
736 _leave(" [incoming]");
739 skb->priority = RX_INVALID_OPERATION;
741 skb->priority = RX_CALL_DEAD;
744 if (sp->hdr.type != RXRPC_PACKET_TYPE_ABORT) {
745 _debug("reject type %d",sp->hdr.type);
746 rxrpc_reject_packet(local, skb);
750 _leave(" [no call]");
754 skb->priority = RX_PROTOCOL_ERROR;
755 rxrpc_reject_packet(local, skb);