xref: /linux/net/rxrpc/peer_event.c (revision 1e15510b71c99c6e49134d756df91069f7d18141)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Peer event handling, typically ICMP messages.
3  *
4  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7 
8 #include <linux/module.h>
9 #include <linux/net.h>
10 #include <linux/skbuff.h>
11 #include <linux/errqueue.h>
12 #include <linux/udp.h>
13 #include <linux/in.h>
14 #include <linux/in6.h>
15 #include <linux/icmp.h>
16 #include <net/sock.h>
17 #include <net/af_rxrpc.h>
18 #include <net/ip.h>
19 #include "ar-internal.h"
20 
21 static void rxrpc_store_error(struct rxrpc_peer *, struct sk_buff *);
22 static void rxrpc_distribute_error(struct rxrpc_peer *, struct sk_buff *,
23 				   enum rxrpc_call_completion, int);
24 
25 /*
26  * Find the peer associated with a local error.
27  */
rxrpc_lookup_peer_local_rcu(struct rxrpc_local * local,const struct sk_buff * skb,struct sockaddr_rxrpc * srx)28 static struct rxrpc_peer *rxrpc_lookup_peer_local_rcu(struct rxrpc_local *local,
29 						      const struct sk_buff *skb,
30 						      struct sockaddr_rxrpc *srx)
31 {
32 	struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
33 
34 	_enter("");
35 
36 	memset(srx, 0, sizeof(*srx));
37 	srx->transport_type = local->srx.transport_type;
38 	srx->transport_len = local->srx.transport_len;
39 	srx->transport.family = local->srx.transport.family;
40 
41 	/* Can we see an ICMP4 packet on an ICMP6 listening socket?  and vice
42 	 * versa?
43 	 */
44 	switch (srx->transport.family) {
45 	case AF_INET:
46 		srx->transport_len = sizeof(srx->transport.sin);
47 		srx->transport.family = AF_INET;
48 		srx->transport.sin.sin_port = serr->port;
49 		switch (serr->ee.ee_origin) {
50 		case SO_EE_ORIGIN_ICMP:
51 			memcpy(&srx->transport.sin.sin_addr,
52 			       skb_network_header(skb) + serr->addr_offset,
53 			       sizeof(struct in_addr));
54 			break;
55 		case SO_EE_ORIGIN_ICMP6:
56 			memcpy(&srx->transport.sin.sin_addr,
57 			       skb_network_header(skb) + serr->addr_offset + 12,
58 			       sizeof(struct in_addr));
59 			break;
60 		default:
61 			memcpy(&srx->transport.sin.sin_addr, &ip_hdr(skb)->saddr,
62 			       sizeof(struct in_addr));
63 			break;
64 		}
65 		break;
66 
67 #ifdef CONFIG_AF_RXRPC_IPV6
68 	case AF_INET6:
69 		switch (serr->ee.ee_origin) {
70 		case SO_EE_ORIGIN_ICMP6:
71 			srx->transport.sin6.sin6_port = serr->port;
72 			memcpy(&srx->transport.sin6.sin6_addr,
73 			       skb_network_header(skb) + serr->addr_offset,
74 			       sizeof(struct in6_addr));
75 			break;
76 		case SO_EE_ORIGIN_ICMP:
77 			srx->transport_len = sizeof(srx->transport.sin);
78 			srx->transport.family = AF_INET;
79 			srx->transport.sin.sin_port = serr->port;
80 			memcpy(&srx->transport.sin.sin_addr,
81 			       skb_network_header(skb) + serr->addr_offset,
82 			       sizeof(struct in_addr));
83 			break;
84 		default:
85 			memcpy(&srx->transport.sin6.sin6_addr,
86 			       &ipv6_hdr(skb)->saddr,
87 			       sizeof(struct in6_addr));
88 			break;
89 		}
90 		break;
91 #endif
92 
93 	default:
94 		BUG();
95 	}
96 
97 	return rxrpc_lookup_peer_rcu(local, srx);
98 }
99 
100 /*
101  * Handle an MTU/fragmentation problem.
102  */
rxrpc_adjust_mtu(struct rxrpc_peer * peer,unsigned int mtu)103 static void rxrpc_adjust_mtu(struct rxrpc_peer *peer, unsigned int mtu)
104 {
105 	unsigned int max_data;
106 
107 	/* wind down the local interface MTU */
108 	if (mtu > 0 && peer->if_mtu == 65535 && mtu < peer->if_mtu)
109 		peer->if_mtu = mtu;
110 
111 	if (mtu == 0) {
112 		/* they didn't give us a size, estimate one */
113 		mtu = peer->if_mtu;
114 		if (mtu > 1500) {
115 			mtu >>= 1;
116 			if (mtu < 1500)
117 				mtu = 1500;
118 		} else {
119 			mtu -= 100;
120 			if (mtu < peer->hdrsize)
121 				mtu = peer->hdrsize + 4;
122 		}
123 	}
124 
125 	max_data = max_t(int, mtu - peer->hdrsize, 500);
126 	if (max_data < peer->max_data) {
127 		if (peer->pmtud_good > max_data)
128 			peer->pmtud_good = max_data;
129 		if (peer->pmtud_bad > max_data + 1)
130 			peer->pmtud_bad = max_data + 1;
131 
132 		trace_rxrpc_pmtud_reduce(peer, 0, max_data, rxrpc_pmtud_reduce_icmp);
133 		peer->max_data = max_data;
134 	}
135 }
136 
137 /*
138  * Handle an error received on the local endpoint.
139  */
rxrpc_input_error(struct rxrpc_local * local,struct sk_buff * skb)140 void rxrpc_input_error(struct rxrpc_local *local, struct sk_buff *skb)
141 {
142 	struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
143 	struct sockaddr_rxrpc srx;
144 	struct rxrpc_peer *peer = NULL;
145 
146 	_enter("L=%x", local->debug_id);
147 
148 	if (!skb->len && serr->ee.ee_origin == SO_EE_ORIGIN_TIMESTAMPING) {
149 		_leave("UDP empty message");
150 		return;
151 	}
152 
153 	rcu_read_lock();
154 	peer = rxrpc_lookup_peer_local_rcu(local, skb, &srx);
155 	if (peer && !rxrpc_get_peer_maybe(peer, rxrpc_peer_get_input_error))
156 		peer = NULL;
157 	rcu_read_unlock();
158 	if (!peer)
159 		return;
160 
161 	trace_rxrpc_rx_icmp(peer, &serr->ee, &srx);
162 
163 	if ((serr->ee.ee_origin == SO_EE_ORIGIN_ICMP &&
164 	     serr->ee.ee_type == ICMP_DEST_UNREACH &&
165 	     serr->ee.ee_code == ICMP_FRAG_NEEDED)) {
166 		rxrpc_adjust_mtu(peer, serr->ee.ee_info);
167 		goto out;
168 	}
169 
170 	if ((serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6 &&
171 	     serr->ee.ee_type == ICMPV6_PKT_TOOBIG &&
172 	     serr->ee.ee_code == 0)) {
173 		rxrpc_adjust_mtu(peer, serr->ee.ee_info);
174 		goto out;
175 	}
176 
177 	rxrpc_store_error(peer, skb);
178 out:
179 	rxrpc_put_peer(peer, rxrpc_peer_put_input_error);
180 }
181 
182 /*
183  * Map an error report to error codes on the peer record.
184  */
rxrpc_store_error(struct rxrpc_peer * peer,struct sk_buff * skb)185 static void rxrpc_store_error(struct rxrpc_peer *peer, struct sk_buff *skb)
186 {
187 	enum rxrpc_call_completion compl = RXRPC_CALL_NETWORK_ERROR;
188 	struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
189 	struct sock_extended_err *ee = &serr->ee;
190 	int err = ee->ee_errno;
191 
192 	_enter("");
193 
194 	switch (ee->ee_origin) {
195 	case SO_EE_ORIGIN_NONE:
196 	case SO_EE_ORIGIN_LOCAL:
197 		compl = RXRPC_CALL_LOCAL_ERROR;
198 		break;
199 
200 	case SO_EE_ORIGIN_ICMP6:
201 		if (err == EACCES)
202 			err = EHOSTUNREACH;
203 		fallthrough;
204 	case SO_EE_ORIGIN_ICMP:
205 	default:
206 		break;
207 	}
208 
209 	rxrpc_distribute_error(peer, skb, compl, err);
210 }
211 
212 /*
213  * Distribute an error that occurred on a peer.
214  */
rxrpc_distribute_error(struct rxrpc_peer * peer,struct sk_buff * skb,enum rxrpc_call_completion compl,int err)215 static void rxrpc_distribute_error(struct rxrpc_peer *peer, struct sk_buff *skb,
216 				   enum rxrpc_call_completion compl, int err)
217 {
218 	struct rxrpc_call *call;
219 	HLIST_HEAD(error_targets);
220 
221 	spin_lock_irq(&peer->lock);
222 	hlist_move_list(&peer->error_targets, &error_targets);
223 
224 	while (!hlist_empty(&error_targets)) {
225 		call = hlist_entry(error_targets.first,
226 				   struct rxrpc_call, error_link);
227 		hlist_del_init(&call->error_link);
228 		spin_unlock_irq(&peer->lock);
229 
230 		rxrpc_see_call(call, rxrpc_call_see_distribute_error);
231 		rxrpc_set_call_completion(call, compl, 0, -err);
232 		rxrpc_input_call_event(call);
233 
234 		spin_lock_irq(&peer->lock);
235 	}
236 
237 	spin_unlock_irq(&peer->lock);
238 }
239 
240 /*
241  * Perform keep-alive pings.
242  */
rxrpc_peer_keepalive_dispatch(struct rxrpc_net * rxnet,struct list_head * collector,time64_t base,u8 cursor)243 static void rxrpc_peer_keepalive_dispatch(struct rxrpc_net *rxnet,
244 					  struct list_head *collector,
245 					  time64_t base,
246 					  u8 cursor)
247 {
248 	struct rxrpc_peer *peer;
249 	const u8 mask = ARRAY_SIZE(rxnet->peer_keepalive) - 1;
250 	time64_t keepalive_at;
251 	bool use;
252 	int slot;
253 
254 	spin_lock_bh(&rxnet->peer_hash_lock);
255 
256 	while (!list_empty(collector)) {
257 		peer = list_entry(collector->next,
258 				  struct rxrpc_peer, keepalive_link);
259 
260 		list_del_init(&peer->keepalive_link);
261 		if (!rxrpc_get_peer_maybe(peer, rxrpc_peer_get_keepalive))
262 			continue;
263 
264 		use = __rxrpc_use_local(peer->local, rxrpc_local_use_peer_keepalive);
265 		spin_unlock_bh(&rxnet->peer_hash_lock);
266 
267 		if (use) {
268 			keepalive_at = peer->last_tx_at + RXRPC_KEEPALIVE_TIME;
269 			slot = keepalive_at - base;
270 			_debug("%02x peer %u t=%d {%pISp}",
271 			       cursor, peer->debug_id, slot, &peer->srx.transport);
272 
273 			if (keepalive_at <= base ||
274 			    keepalive_at > base + RXRPC_KEEPALIVE_TIME) {
275 				rxrpc_send_keepalive(peer);
276 				slot = RXRPC_KEEPALIVE_TIME;
277 			}
278 
279 			/* A transmission to this peer occurred since last we
280 			 * examined it so put it into the appropriate future
281 			 * bucket.
282 			 */
283 			slot += cursor;
284 			slot &= mask;
285 			spin_lock_bh(&rxnet->peer_hash_lock);
286 			list_add_tail(&peer->keepalive_link,
287 				      &rxnet->peer_keepalive[slot & mask]);
288 			spin_unlock_bh(&rxnet->peer_hash_lock);
289 			rxrpc_unuse_local(peer->local, rxrpc_local_unuse_peer_keepalive);
290 		}
291 		rxrpc_put_peer(peer, rxrpc_peer_put_keepalive);
292 		spin_lock_bh(&rxnet->peer_hash_lock);
293 	}
294 
295 	spin_unlock_bh(&rxnet->peer_hash_lock);
296 }
297 
298 /*
299  * Perform keep-alive pings with VERSION packets to keep any NAT alive.
300  */
rxrpc_peer_keepalive_worker(struct work_struct * work)301 void rxrpc_peer_keepalive_worker(struct work_struct *work)
302 {
303 	struct rxrpc_net *rxnet =
304 		container_of(work, struct rxrpc_net, peer_keepalive_work);
305 	const u8 mask = ARRAY_SIZE(rxnet->peer_keepalive) - 1;
306 	time64_t base, now, delay;
307 	u8 cursor, stop;
308 	LIST_HEAD(collector);
309 
310 	now = ktime_get_seconds();
311 	base = rxnet->peer_keepalive_base;
312 	cursor = rxnet->peer_keepalive_cursor;
313 	_enter("%lld,%u", base - now, cursor);
314 
315 	if (!rxnet->live)
316 		return;
317 
318 	/* Remove to a temporary list all the peers that are currently lodged
319 	 * in expired buckets plus all new peers.
320 	 *
321 	 * Everything in the bucket at the cursor is processed this
322 	 * second; the bucket at cursor + 1 goes at now + 1s and so
323 	 * on...
324 	 */
325 	spin_lock_bh(&rxnet->peer_hash_lock);
326 	list_splice_init(&rxnet->peer_keepalive_new, &collector);
327 
328 	stop = cursor + ARRAY_SIZE(rxnet->peer_keepalive);
329 	while (base <= now && (s8)(cursor - stop) < 0) {
330 		list_splice_tail_init(&rxnet->peer_keepalive[cursor & mask],
331 				      &collector);
332 		base++;
333 		cursor++;
334 	}
335 
336 	base = now;
337 	spin_unlock_bh(&rxnet->peer_hash_lock);
338 
339 	rxnet->peer_keepalive_base = base;
340 	rxnet->peer_keepalive_cursor = cursor;
341 	rxrpc_peer_keepalive_dispatch(rxnet, &collector, base, cursor);
342 	ASSERT(list_empty(&collector));
343 
344 	/* Schedule the timer for the next occupied timeslot. */
345 	cursor = rxnet->peer_keepalive_cursor;
346 	stop = cursor + RXRPC_KEEPALIVE_TIME - 1;
347 	for (; (s8)(cursor - stop) < 0; cursor++) {
348 		if (!list_empty(&rxnet->peer_keepalive[cursor & mask]))
349 			break;
350 		base++;
351 	}
352 
353 	now = ktime_get_seconds();
354 	delay = base - now;
355 	if (delay < 1)
356 		delay = 1;
357 	delay *= HZ;
358 	if (rxnet->live)
359 		timer_reduce(&rxnet->peer_keepalive_timer, jiffies + delay);
360 
361 	_leave("");
362 }
363 
364 /*
365  * Do path MTU probing.
366  */
rxrpc_input_probe_for_pmtud(struct rxrpc_connection * conn,rxrpc_serial_t acked_serial,bool sendmsg_fail)367 void rxrpc_input_probe_for_pmtud(struct rxrpc_connection *conn, rxrpc_serial_t acked_serial,
368 				 bool sendmsg_fail)
369 {
370 	struct rxrpc_peer *peer = conn->peer;
371 	unsigned int max_data = peer->max_data;
372 	int good, trial, bad, jumbo;
373 
374 	good  = peer->pmtud_good;
375 	trial = peer->pmtud_trial;
376 	bad   = peer->pmtud_bad;
377 	if (good >= bad - 1) {
378 		conn->pmtud_probe = 0;
379 		peer->pmtud_lost = false;
380 		return;
381 	}
382 
383 	if (!peer->pmtud_probing)
384 		goto send_probe;
385 
386 	if (sendmsg_fail || after(acked_serial, conn->pmtud_probe)) {
387 		/* Retry a lost probe. */
388 		if (!peer->pmtud_lost) {
389 			trace_rxrpc_pmtud_lost(conn, acked_serial);
390 			conn->pmtud_probe = 0;
391 			peer->pmtud_lost = true;
392 			goto send_probe;
393 		}
394 
395 		/* The probed size didn't seem to get through. */
396 		bad = trial;
397 		peer->pmtud_bad = bad;
398 		if (bad <= max_data)
399 			max_data = bad - 1;
400 	} else {
401 		/* It did get through. */
402 		good = trial;
403 		peer->pmtud_good = good;
404 		if (good > max_data)
405 			max_data = good;
406 	}
407 
408 	max_data = umin(max_data, peer->ackr_max_data);
409 	if (max_data != peer->max_data)
410 		peer->max_data = max_data;
411 
412 	jumbo = max_data + sizeof(struct rxrpc_jumbo_header);
413 	jumbo /= RXRPC_JUMBO_SUBPKTLEN;
414 	peer->pmtud_jumbo = jumbo;
415 
416 	trace_rxrpc_pmtud_rx(conn, acked_serial);
417 	conn->pmtud_probe = 0;
418 	peer->pmtud_lost = false;
419 
420 	if (good < RXRPC_JUMBO(2) && bad > RXRPC_JUMBO(2))
421 		trial = RXRPC_JUMBO(2);
422 	else if (good < RXRPC_JUMBO(4) && bad > RXRPC_JUMBO(4))
423 		trial = RXRPC_JUMBO(4);
424 	else if (good < RXRPC_JUMBO(3) && bad > RXRPC_JUMBO(3))
425 		trial = RXRPC_JUMBO(3);
426 	else if (good < RXRPC_JUMBO(6) && bad > RXRPC_JUMBO(6))
427 		trial = RXRPC_JUMBO(6);
428 	else if (good < RXRPC_JUMBO(5) && bad > RXRPC_JUMBO(5))
429 		trial = RXRPC_JUMBO(5);
430 	else if (good < RXRPC_JUMBO(8) && bad > RXRPC_JUMBO(8))
431 		trial = RXRPC_JUMBO(8);
432 	else if (good < RXRPC_JUMBO(7) && bad > RXRPC_JUMBO(7))
433 		trial = RXRPC_JUMBO(7);
434 	else
435 		trial = (good + bad) / 2;
436 	peer->pmtud_trial = trial;
437 
438 	if (good >= bad)
439 		return;
440 
441 send_probe:
442 	peer->pmtud_pending = true;
443 }
444