1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* incoming call handling
3  *
4  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7 
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 
10 #include <linux/module.h>
11 #include <linux/net.h>
12 #include <linux/skbuff.h>
13 #include <linux/errqueue.h>
14 #include <linux/udp.h>
15 #include <linux/in.h>
16 #include <linux/in6.h>
17 #include <linux/icmp.h>
18 #include <linux/gfp.h>
19 #include <linux/circ_buf.h>
20 #include <net/sock.h>
21 #include <net/af_rxrpc.h>
22 #include <net/ip.h>
23 #include "ar-internal.h"
24 
25 static void rxrpc_dummy_notify(struct sock *sk, struct rxrpc_call *call,
26 			       unsigned long user_call_ID)
27 {
28 }
29 
30 /*
31  * Preallocate a single service call, connection and peer and, if possible,
32  * give them a user ID and attach the user's side of the ID to them.
33  */
34 static int rxrpc_service_prealloc_one(struct rxrpc_sock *rx,
35 				      struct rxrpc_backlog *b,
36 				      rxrpc_notify_rx_t notify_rx,
37 				      unsigned long user_call_ID, gfp_t gfp,
38 				      unsigned int debug_id)
39 {
40 	struct rxrpc_call *call, *xcall;
41 	struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk));
42 	struct rb_node *parent, **pp;
43 	int max, tmp;
44 	unsigned int size = RXRPC_BACKLOG_MAX;
45 	unsigned int head, tail, call_head, call_tail;
46 
47 	max = rx->sk.sk_max_ack_backlog;
48 	tmp = rx->sk.sk_ack_backlog;
49 	if (tmp >= max) {
50 		_leave(" = -ENOBUFS [full %u]", max);
51 		return -ENOBUFS;
52 	}
53 	max -= tmp;
54 
55 	/* We don't need more conns and peers than we have calls, but on the
56 	 * other hand, we shouldn't ever use more peers than conns or conns
57 	 * than calls.
58 	 */
59 	call_head = b->call_backlog_head;
60 	call_tail = READ_ONCE(b->call_backlog_tail);
61 	tmp = CIRC_CNT(call_head, call_tail, size);
62 	if (tmp >= max) {
63 		_leave(" = -ENOBUFS [enough %u]", tmp);
64 		return -ENOBUFS;
65 	}
66 	max = tmp + 1;
67 
68 	head = b->peer_backlog_head;
69 	tail = READ_ONCE(b->peer_backlog_tail);
70 	if (CIRC_CNT(head, tail, size) < max) {
71 		struct rxrpc_peer *peer;
72 
73 		peer = rxrpc_alloc_peer(rx->local, gfp, rxrpc_peer_new_prealloc);
74 		if (!peer)
75 			return -ENOMEM;
76 		b->peer_backlog[head] = peer;
77 		smp_store_release(&b->peer_backlog_head,
78 				  (head + 1) & (size - 1));
79 	}
80 
81 	head = b->conn_backlog_head;
82 	tail = READ_ONCE(b->conn_backlog_tail);
83 	if (CIRC_CNT(head, tail, size) < max) {
84 		struct rxrpc_connection *conn;
85 
86 		conn = rxrpc_prealloc_service_connection(rxnet, gfp);
87 		if (!conn)
88 			return -ENOMEM;
89 		b->conn_backlog[head] = conn;
90 		smp_store_release(&b->conn_backlog_head,
91 				  (head + 1) & (size - 1));
92 	}
93 
94 	/* Now it gets complicated, because calls get registered with the
95 	 * socket here, with a user ID preassigned by the user.
96 	 */
97 	call = rxrpc_alloc_call(rx, gfp, debug_id);
98 	if (!call)
99 		return -ENOMEM;
100 	call->flags |= (1 << RXRPC_CALL_IS_SERVICE);
101 	rxrpc_set_call_state(call, RXRPC_CALL_SERVER_PREALLOC);
102 	__set_bit(RXRPC_CALL_EV_INITIAL_PING, &call->events);
103 
104 	trace_rxrpc_call(call->debug_id, refcount_read(&call->ref),
105 			 user_call_ID, rxrpc_call_new_prealloc_service);
106 
107 	write_lock(&rx->call_lock);
108 
109 	/* Check the user ID isn't already in use */
110 	pp = &rx->calls.rb_node;
111 	parent = NULL;
112 	while (*pp) {
113 		parent = *pp;
114 		xcall = rb_entry(parent, struct rxrpc_call, sock_node);
115 		if (user_call_ID < xcall->user_call_ID)
116 			pp = &(*pp)->rb_left;
117 		else if (user_call_ID > xcall->user_call_ID)
118 			pp = &(*pp)->rb_right;
119 		else
120 			goto id_in_use;
121 	}
122 
123 	call->user_call_ID = user_call_ID;
124 	call->notify_rx = notify_rx;
125 	if (rx->app_ops &&
126 	    rx->app_ops->user_attach_call) {
127 		rxrpc_get_call(call, rxrpc_call_get_kernel_service);
128 		rx->app_ops->user_attach_call(call, user_call_ID);
129 	}
130 
131 	rxrpc_get_call(call, rxrpc_call_get_userid);
132 	rb_link_node(&call->sock_node, parent, pp);
133 	rb_insert_color(&call->sock_node, &rx->calls);
134 	set_bit(RXRPC_CALL_HAS_USERID, &call->flags);
135 
136 	list_add(&call->sock_link, &rx->sock_calls);
137 
138 	write_unlock(&rx->call_lock);
139 
140 	rxnet = call->rxnet;
141 	spin_lock(&rxnet->call_lock);
142 	list_add_tail_rcu(&call->link, &rxnet->calls);
143 	spin_unlock(&rxnet->call_lock);
144 
145 	b->call_backlog[call_head] = call;
146 	smp_store_release(&b->call_backlog_head, (call_head + 1) & (size - 1));
147 	_leave(" = 0 [%d -> %lx]", call->debug_id, user_call_ID);
148 	return 0;
149 
150 id_in_use:
151 	write_unlock(&rx->call_lock);
152 	rxrpc_cleanup_call(call);
153 	_leave(" = -EBADSLT");
154 	return -EBADSLT;
155 }
156 
157 /*
158  * Allocate the preallocation buffers for incoming service calls.  These must
159  * be charged manually.
160  */
161 int rxrpc_service_prealloc(struct rxrpc_sock *rx, gfp_t gfp)
162 {
163 	struct rxrpc_backlog *b = rx->backlog;
164 
165 	if (!b) {
166 		b = kzalloc(sizeof(struct rxrpc_backlog), gfp);
167 		if (!b)
168 			return -ENOMEM;
169 		rx->backlog = b;
170 	}
171 
172 	return 0;
173 }
174 
175 /*
176  * Discard the preallocation on a service.
177  */
178 void rxrpc_discard_prealloc(struct rxrpc_sock *rx)
179 {
180 	struct rxrpc_backlog *b = rx->backlog;
181 	struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk));
182 	unsigned int size = RXRPC_BACKLOG_MAX, head, tail;
183 
184 	if (!b)
185 		return;
186 	rx->backlog = NULL;
187 
188 	/* Make sure that there aren't any incoming calls in progress before we
189 	 * clear the preallocation buffers.
190 	 */
191 	spin_lock_irq(&rx->incoming_lock);
192 	spin_unlock_irq(&rx->incoming_lock);
193 
194 	head = b->peer_backlog_head;
195 	tail = b->peer_backlog_tail;
196 	while (CIRC_CNT(head, tail, size) > 0) {
197 		struct rxrpc_peer *peer = b->peer_backlog[tail];
198 		rxrpc_put_local(peer->local, rxrpc_local_put_prealloc_peer);
199 		kfree(peer);
200 		tail = (tail + 1) & (size - 1);
201 	}
202 
203 	head = b->conn_backlog_head;
204 	tail = b->conn_backlog_tail;
205 	while (CIRC_CNT(head, tail, size) > 0) {
206 		struct rxrpc_connection *conn = b->conn_backlog[tail];
207 		write_lock(&rxnet->conn_lock);
208 		list_del(&conn->link);
209 		list_del(&conn->proc_link);
210 		write_unlock(&rxnet->conn_lock);
211 		kfree(conn);
212 		if (atomic_dec_and_test(&rxnet->nr_conns))
213 			wake_up_var(&rxnet->nr_conns);
214 		tail = (tail + 1) & (size - 1);
215 	}
216 
217 	head = b->call_backlog_head;
218 	tail = b->call_backlog_tail;
219 	while (CIRC_CNT(head, tail, size) > 0) {
220 		struct rxrpc_call *call = b->call_backlog[tail];
221 		rcu_assign_pointer(call->socket, rx);
222 		if (rx->app_ops &&
223 		    rx->app_ops->discard_new_call) {
224 			_debug("discard %lx", call->user_call_ID);
225 			rx->app_ops->discard_new_call(call, call->user_call_ID);
226 			if (call->notify_rx)
227 				call->notify_rx = rxrpc_dummy_notify;
228 			rxrpc_put_call(call, rxrpc_call_put_kernel);
229 		}
230 		rxrpc_call_completed(call);
231 		rxrpc_release_call(rx, call);
232 		rxrpc_put_call(call, rxrpc_call_put_discard_prealloc);
233 		tail = (tail + 1) & (size - 1);
234 	}
235 
236 	kfree(b);
237 }
238 
239 /*
240  * Allocate a new incoming call from the prealloc pool, along with a connection
241  * and a peer as necessary.
242  */
243 static struct rxrpc_call *rxrpc_alloc_incoming_call(struct rxrpc_sock *rx,
244 						    struct rxrpc_local *local,
245 						    struct rxrpc_peer *peer,
246 						    struct rxrpc_connection *conn,
247 						    const struct rxrpc_security *sec,
248 						    struct sockaddr_rxrpc *peer_srx,
249 						    struct sk_buff *skb)
250 {
251 	struct rxrpc_backlog *b = rx->backlog;
252 	struct rxrpc_call *call;
253 	unsigned short call_head, conn_head, peer_head;
254 	unsigned short call_tail, conn_tail, peer_tail;
255 	unsigned short call_count, conn_count;
256 
257 	/* #calls >= #conns >= #peers must hold true. */
258 	call_head = smp_load_acquire(&b->call_backlog_head);
259 	call_tail = b->call_backlog_tail;
260 	call_count = CIRC_CNT(call_head, call_tail, RXRPC_BACKLOG_MAX);
261 	conn_head = smp_load_acquire(&b->conn_backlog_head);
262 	conn_tail = b->conn_backlog_tail;
263 	conn_count = CIRC_CNT(conn_head, conn_tail, RXRPC_BACKLOG_MAX);
264 	ASSERTCMP(conn_count, >=, call_count);
265 	peer_head = smp_load_acquire(&b->peer_backlog_head);
266 	peer_tail = b->peer_backlog_tail;
267 	ASSERTCMP(CIRC_CNT(peer_head, peer_tail, RXRPC_BACKLOG_MAX), >=,
268 		  conn_count);
269 
270 	if (call_count == 0)
271 		return NULL;
272 
273 	if (!conn) {
274 		if (peer && !rxrpc_get_peer_maybe(peer, rxrpc_peer_get_service_conn))
275 			peer = NULL;
276 		if (!peer) {
277 			peer = b->peer_backlog[peer_tail];
278 			peer->srx = *peer_srx;
279 			b->peer_backlog[peer_tail] = NULL;
280 			smp_store_release(&b->peer_backlog_tail,
281 					  (peer_tail + 1) &
282 					  (RXRPC_BACKLOG_MAX - 1));
283 
284 			rxrpc_new_incoming_peer(local, peer);
285 		}
286 
287 		/* Now allocate and set up the connection */
288 		conn = b->conn_backlog[conn_tail];
289 		b->conn_backlog[conn_tail] = NULL;
290 		smp_store_release(&b->conn_backlog_tail,
291 				  (conn_tail + 1) & (RXRPC_BACKLOG_MAX - 1));
292 		conn->local = rxrpc_get_local(local, rxrpc_local_get_prealloc_conn);
293 		conn->peer = peer;
294 		rxrpc_see_connection(conn, rxrpc_conn_see_new_service_conn);
295 		rxrpc_new_incoming_connection(rx, conn, sec, skb);
296 	} else {
297 		rxrpc_get_connection(conn, rxrpc_conn_get_service_conn);
298 		atomic_inc(&conn->active);
299 	}
300 
301 	/* And now we can allocate and set up a new call */
302 	call = b->call_backlog[call_tail];
303 	b->call_backlog[call_tail] = NULL;
304 	smp_store_release(&b->call_backlog_tail,
305 			  (call_tail + 1) & (RXRPC_BACKLOG_MAX - 1));
306 
307 	rxrpc_see_call(call, rxrpc_call_see_accept);
308 	call->local = rxrpc_get_local(conn->local, rxrpc_local_get_call);
309 	call->conn = conn;
310 	call->security = conn->security;
311 	call->security_ix = conn->security_ix;
312 	call->peer = rxrpc_get_peer(conn->peer, rxrpc_peer_get_accept);
313 	call->dest_srx = peer->srx;
314 	call->cong_ssthresh = call->peer->cong_ssthresh;
315 	call->tx_last_sent = ktime_get_real();
316 	return call;
317 }
318 
319 /*
320  * Set up a new incoming call.  Called from the I/O thread.
321  *
322  * If this is for a kernel service, when we allocate the call, it will have
323  * three refs on it: (1) the kernel service, (2) the user_call_ID tree, (3) the
324  * retainer ref obtained from the backlog buffer.  Prealloc calls for userspace
325  * services only have the ref from the backlog buffer.
326  *
327  * If we want to report an error, we mark the skb with the packet type and
328  * abort code and return false.
329  */
330 bool rxrpc_new_incoming_call(struct rxrpc_local *local,
331 			     struct rxrpc_peer *peer,
332 			     struct rxrpc_connection *conn,
333 			     struct sockaddr_rxrpc *peer_srx,
334 			     struct sk_buff *skb)
335 {
336 	const struct rxrpc_security *sec = NULL;
337 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
338 	struct rxrpc_call *call = NULL;
339 	struct rxrpc_sock *rx;
340 
341 	_enter("");
342 
343 	/* Don't set up a call for anything other than a DATA packet. */
344 	if (sp->hdr.type != RXRPC_PACKET_TYPE_DATA)
345 		return rxrpc_protocol_error(skb, rxrpc_eproto_no_service_call);
346 
347 	read_lock_irq(&local->services_lock);
348 
349 	/* Weed out packets to services we're not offering.  Packets that would
350 	 * begin a call are explicitly rejected and the rest are just
351 	 * discarded.
352 	 */
353 	rx = local->service;
354 	if (!rx || (sp->hdr.serviceId != rx->srx.srx_service &&
355 		    sp->hdr.serviceId != rx->second_service)
356 	    ) {
357 		if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
358 		    sp->hdr.seq == 1)
359 			goto unsupported_service;
360 		goto discard;
361 	}
362 
363 	if (!conn) {
364 		sec = rxrpc_get_incoming_security(rx, skb);
365 		if (!sec)
366 			goto unsupported_security;
367 	}
368 
369 	spin_lock(&rx->incoming_lock);
370 	if (rx->sk.sk_state == RXRPC_SERVER_LISTEN_DISABLED ||
371 	    rx->sk.sk_state == RXRPC_CLOSE) {
372 		rxrpc_direct_abort(skb, rxrpc_abort_shut_down,
373 				   RX_INVALID_OPERATION, -ESHUTDOWN);
374 		goto no_call;
375 	}
376 
377 	call = rxrpc_alloc_incoming_call(rx, local, peer, conn, sec, peer_srx,
378 					 skb);
379 	if (!call) {
380 		skb->mark = RXRPC_SKB_MARK_REJECT_BUSY;
381 		goto no_call;
382 	}
383 
384 	trace_rxrpc_receive(call, rxrpc_receive_incoming,
385 			    sp->hdr.serial, sp->hdr.seq);
386 
387 	/* Make the call live. */
388 	rxrpc_incoming_call(rx, call, skb);
389 	conn = call->conn;
390 
391 	if (rx->app_ops &&
392 	    rx->app_ops->notify_new_call)
393 		rx->app_ops->notify_new_call(&rx->sk, call, call->user_call_ID);
394 
395 	spin_lock(&conn->state_lock);
396 	if (conn->state == RXRPC_CONN_SERVICE_UNSECURED) {
397 		conn->state = RXRPC_CONN_SERVICE_CHALLENGING;
398 		set_bit(RXRPC_CONN_EV_CHALLENGE, &call->conn->events);
399 		rxrpc_queue_conn(call->conn, rxrpc_conn_queue_challenge);
400 	}
401 	spin_unlock(&conn->state_lock);
402 
403 	spin_unlock(&rx->incoming_lock);
404 	read_unlock_irq(&local->services_lock);
405 
406 	if (hlist_unhashed(&call->error_link)) {
407 		spin_lock_irq(&call->peer->lock);
408 		hlist_add_head(&call->error_link, &call->peer->error_targets);
409 		spin_unlock_irq(&call->peer->lock);
410 	}
411 
412 	_leave(" = %p{%d}", call, call->debug_id);
413 	rxrpc_queue_rx_call_packet(call, skb);
414 	rxrpc_put_call(call, rxrpc_call_put_input);
415 	return true;
416 
417 unsupported_service:
418 	read_unlock_irq(&local->services_lock);
419 	return rxrpc_direct_abort(skb, rxrpc_abort_service_not_offered,
420 				  RX_INVALID_OPERATION, -EOPNOTSUPP);
421 unsupported_security:
422 	read_unlock_irq(&local->services_lock);
423 	return rxrpc_direct_abort(skb, rxrpc_abort_service_not_offered,
424 				  RX_INVALID_OPERATION, -EKEYREJECTED);
425 no_call:
426 	spin_unlock(&rx->incoming_lock);
427 	read_unlock_irq(&local->services_lock);
428 	_leave(" = f [%u]", skb->mark);
429 	return false;
430 discard:
431 	read_unlock_irq(&local->services_lock);
432 	return true;
433 }
434 
435 /*
436  * Charge up socket with preallocated calls, attaching user call IDs.
437  */
438 int rxrpc_user_charge_accept(struct rxrpc_sock *rx, unsigned long user_call_ID)
439 {
440 	struct rxrpc_backlog *b = rx->backlog;
441 
442 	if (rx->sk.sk_state == RXRPC_CLOSE)
443 		return -ESHUTDOWN;
444 
445 	return rxrpc_service_prealloc_one(rx, b, NULL, user_call_ID, GFP_KERNEL,
446 					  atomic_inc_return(&rxrpc_debug_id));
447 }
448 
449 /*
450  * rxrpc_kernel_charge_accept - Charge up socket with preallocated calls
451  * @sock: The socket on which to preallocate
452  * @notify_rx: Event notification function for the call
453  * @user_call_ID: The tag to attach to the preallocated call
454  * @gfp: The allocation conditions.
455  * @debug_id: The tracing debug ID.
456  *
457  * Charge up the socket with preallocated calls, each with a user ID.  The
458  * ->user_attach_call() callback function should be provided to effect the
459  * attachment from the user's side.  The user is given a ref to hold on the
460  * call.
461  *
462  * Note that the call may be come connected before this function returns.
463  */
464 int rxrpc_kernel_charge_accept(struct socket *sock, rxrpc_notify_rx_t notify_rx,
465 			       unsigned long user_call_ID, gfp_t gfp,
466 			       unsigned int debug_id)
467 {
468 	struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
469 	struct rxrpc_backlog *b = rx->backlog;
470 
471 	if (sock->sk->sk_state == RXRPC_CLOSE)
472 		return -ESHUTDOWN;
473 
474 	return rxrpc_service_prealloc_one(rx, b, notify_rx, user_call_ID,
475 					  gfp, debug_id);
476 }
477 EXPORT_SYMBOL(rxrpc_kernel_charge_accept);
478