Commit | Line | Data |
---|---|---|
f66d7490 | 1 | /* Peer event handling, typically ICMP messages. |
17926a79 DH |
2 | * |
3 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. | |
4 | * Written by David Howells (dhowells@redhat.com) | |
5 | * | |
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. | |
10 | */ | |
11 | ||
12 | #include <linux/module.h> | |
13 | #include <linux/net.h> | |
14 | #include <linux/skbuff.h> | |
15 | #include <linux/errqueue.h> | |
16 | #include <linux/udp.h> | |
17 | #include <linux/in.h> | |
18 | #include <linux/in6.h> | |
19 | #include <linux/icmp.h> | |
20 | #include <net/sock.h> | |
21 | #include <net/af_rxrpc.h> | |
22 | #include <net/ip.h> | |
23 | #include "ar-internal.h" | |
24 | ||
f66d7490 | 25 | static void rxrpc_store_error(struct rxrpc_peer *, struct sock_exterr_skb *); |
f3344303 DH |
26 | static void rxrpc_distribute_error(struct rxrpc_peer *, int, |
27 | enum rxrpc_call_completion); | |
f66d7490 | 28 | |
be6e6707 DH |
29 | /* |
30 | * Find the peer associated with an ICMP packet. | |
31 | */ | |
32 | static struct rxrpc_peer *rxrpc_lookup_peer_icmp_rcu(struct rxrpc_local *local, | |
494337c9 DH |
33 | const struct sk_buff *skb, |
34 | struct sockaddr_rxrpc *srx) | |
be6e6707 DH |
35 | { |
36 | struct sock_exterr_skb *serr = SKB_EXT_ERR(skb); | |
be6e6707 DH |
37 | |
38 | _enter(""); | |
39 | ||
494337c9 DH |
40 | memset(srx, 0, sizeof(*srx)); |
41 | srx->transport_type = local->srx.transport_type; | |
42 | srx->transport_len = local->srx.transport_len; | |
43 | srx->transport.family = local->srx.transport.family; | |
be6e6707 DH |
44 | |
45 | /* Can we see an ICMP4 packet on an ICMP6 listening socket? and vice | |
46 | * versa? | |
47 | */ | |
494337c9 | 48 | switch (srx->transport.family) { |
be6e6707 | 49 | case AF_INET: |
46894a13 DH |
50 | srx->transport_len = sizeof(srx->transport.sin); |
51 | srx->transport.family = AF_INET; | |
494337c9 | 52 | srx->transport.sin.sin_port = serr->port; |
be6e6707 DH |
53 | switch (serr->ee.ee_origin) { |
54 | case SO_EE_ORIGIN_ICMP: | |
55 | _net("Rx ICMP"); | |
494337c9 | 56 | memcpy(&srx->transport.sin.sin_addr, |
be6e6707 DH |
57 | skb_network_header(skb) + serr->addr_offset, |
58 | sizeof(struct in_addr)); | |
59 | break; | |
60 | case SO_EE_ORIGIN_ICMP6: | |
61 | _net("Rx ICMP6 on v4 sock"); | |
494337c9 | 62 | memcpy(&srx->transport.sin.sin_addr, |
be6e6707 DH |
63 | skb_network_header(skb) + serr->addr_offset + 12, |
64 | sizeof(struct in_addr)); | |
65 | break; | |
66 | default: | |
494337c9 | 67 | memcpy(&srx->transport.sin.sin_addr, &ip_hdr(skb)->saddr, |
be6e6707 DH |
68 | sizeof(struct in_addr)); |
69 | break; | |
70 | } | |
71 | break; | |
72 | ||
d1912747 | 73 | #ifdef CONFIG_AF_RXRPC_IPV6 |
75b54cb5 | 74 | case AF_INET6: |
75b54cb5 DH |
75 | switch (serr->ee.ee_origin) { |
76 | case SO_EE_ORIGIN_ICMP6: | |
77 | _net("Rx ICMP6"); | |
46894a13 | 78 | srx->transport.sin6.sin6_port = serr->port; |
494337c9 | 79 | memcpy(&srx->transport.sin6.sin6_addr, |
75b54cb5 DH |
80 | skb_network_header(skb) + serr->addr_offset, |
81 | sizeof(struct in6_addr)); | |
82 | break; | |
83 | case SO_EE_ORIGIN_ICMP: | |
84 | _net("Rx ICMP on v6 sock"); | |
46894a13 DH |
85 | srx->transport_len = sizeof(srx->transport.sin); |
86 | srx->transport.family = AF_INET; | |
87 | srx->transport.sin.sin_port = serr->port; | |
88 | memcpy(&srx->transport.sin.sin_addr, | |
75b54cb5 DH |
89 | skb_network_header(skb) + serr->addr_offset, |
90 | sizeof(struct in_addr)); | |
91 | break; | |
92 | default: | |
494337c9 | 93 | memcpy(&srx->transport.sin6.sin6_addr, |
75b54cb5 DH |
94 | &ipv6_hdr(skb)->saddr, |
95 | sizeof(struct in6_addr)); | |
96 | break; | |
97 | } | |
98 | break; | |
d1912747 | 99 | #endif |
75b54cb5 | 100 | |
be6e6707 DH |
101 | default: |
102 | BUG(); | |
103 | } | |
104 | ||
494337c9 | 105 | return rxrpc_lookup_peer_rcu(local, srx); |
be6e6707 DH |
106 | } |
107 | ||
1a70c05b DH |
108 | /* |
109 | * Handle an MTU/fragmentation problem. | |
110 | */ | |
111 | static void rxrpc_adjust_mtu(struct rxrpc_peer *peer, struct sock_exterr_skb *serr) | |
112 | { | |
113 | u32 mtu = serr->ee.ee_info; | |
114 | ||
115 | _net("Rx ICMP Fragmentation Needed (%d)", mtu); | |
116 | ||
117 | /* wind down the local interface MTU */ | |
118 | if (mtu > 0 && peer->if_mtu == 65535 && mtu < peer->if_mtu) { | |
119 | peer->if_mtu = mtu; | |
120 | _net("I/F MTU %u", mtu); | |
121 | } | |
122 | ||
123 | if (mtu == 0) { | |
124 | /* they didn't give us a size, estimate one */ | |
125 | mtu = peer->if_mtu; | |
126 | if (mtu > 1500) { | |
127 | mtu >>= 1; | |
128 | if (mtu < 1500) | |
129 | mtu = 1500; | |
130 | } else { | |
131 | mtu -= 100; | |
132 | if (mtu < peer->hdrsize) | |
133 | mtu = peer->hdrsize + 4; | |
134 | } | |
135 | } | |
136 | ||
137 | if (mtu < peer->mtu) { | |
138 | spin_lock_bh(&peer->lock); | |
139 | peer->mtu = mtu; | |
140 | peer->maxdata = peer->mtu - peer->hdrsize; | |
141 | spin_unlock_bh(&peer->lock); | |
142 | _net("Net MTU %u (maxdata %u)", | |
143 | peer->mtu, peer->maxdata); | |
144 | } | |
145 | } | |
146 | ||
17926a79 | 147 | /* |
f66d7490 | 148 | * Handle an error received on the local endpoint. |
17926a79 | 149 | */ |
abe89ef0 | 150 | void rxrpc_error_report(struct sock *sk) |
17926a79 DH |
151 | { |
152 | struct sock_exterr_skb *serr; | |
494337c9 | 153 | struct sockaddr_rxrpc srx; |
17926a79 DH |
154 | struct rxrpc_local *local = sk->sk_user_data; |
155 | struct rxrpc_peer *peer; | |
156 | struct sk_buff *skb; | |
17926a79 DH |
157 | |
158 | _enter("%p{%d}", sk, local->debug_id); | |
159 | ||
56d282d9 MD |
160 | /* Clear the outstanding error value on the socket so that it doesn't |
161 | * cause kernel_sendmsg() to return it later. | |
162 | */ | |
163 | sock_error(sk); | |
164 | ||
364a9e93 | 165 | skb = sock_dequeue_err_skb(sk); |
17926a79 DH |
166 | if (!skb) { |
167 | _leave("UDP socket errqueue empty"); | |
168 | return; | |
169 | } | |
71f3ca40 | 170 | rxrpc_new_skb(skb, rxrpc_skb_rx_received); |
c247f053 WB |
171 | serr = SKB_EXT_ERR(skb); |
172 | if (!skb->len && serr->ee.ee_origin == SO_EE_ORIGIN_TIMESTAMPING) { | |
49ca0d8b | 173 | _leave("UDP empty message"); |
71f3ca40 | 174 | rxrpc_free_skb(skb, rxrpc_skb_rx_freed); |
49ca0d8b WB |
175 | return; |
176 | } | |
17926a79 | 177 | |
be6e6707 | 178 | rcu_read_lock(); |
494337c9 | 179 | peer = rxrpc_lookup_peer_icmp_rcu(local, skb, &srx); |
be6e6707 DH |
180 | if (peer && !rxrpc_get_peer_maybe(peer)) |
181 | peer = NULL; | |
182 | if (!peer) { | |
183 | rcu_read_unlock(); | |
71f3ca40 | 184 | rxrpc_free_skb(skb, rxrpc_skb_rx_freed); |
17926a79 DH |
185 | _leave(" [no peer]"); |
186 | return; | |
187 | } | |
188 | ||
494337c9 DH |
189 | trace_rxrpc_rx_icmp(peer, &serr->ee, &srx); |
190 | ||
1a70c05b DH |
191 | if ((serr->ee.ee_origin == SO_EE_ORIGIN_ICMP && |
192 | serr->ee.ee_type == ICMP_DEST_UNREACH && | |
193 | serr->ee.ee_code == ICMP_FRAG_NEEDED)) { | |
194 | rxrpc_adjust_mtu(peer, serr); | |
f66d7490 | 195 | rcu_read_unlock(); |
71f3ca40 | 196 | rxrpc_free_skb(skb, rxrpc_skb_rx_freed); |
f66d7490 DH |
197 | rxrpc_put_peer(peer); |
198 | _leave(" [MTU update]"); | |
199 | return; | |
17926a79 DH |
200 | } |
201 | ||
f66d7490 | 202 | rxrpc_store_error(peer, serr); |
be6e6707 | 203 | rcu_read_unlock(); |
71f3ca40 | 204 | rxrpc_free_skb(skb, rxrpc_skb_rx_freed); |
1890fea7 | 205 | rxrpc_put_peer(peer); |
17926a79 | 206 | |
17926a79 DH |
207 | _leave(""); |
208 | } | |
209 | ||
210 | /* | |
f66d7490 | 211 | * Map an error report to error codes on the peer record. |
17926a79 | 212 | */ |
f66d7490 DH |
213 | static void rxrpc_store_error(struct rxrpc_peer *peer, |
214 | struct sock_exterr_skb *serr) | |
17926a79 | 215 | { |
f3344303 | 216 | enum rxrpc_call_completion compl = RXRPC_CALL_NETWORK_ERROR; |
17926a79 | 217 | struct sock_extended_err *ee; |
c9d10c49 | 218 | int err; |
17926a79 DH |
219 | |
220 | _enter(""); | |
221 | ||
17926a79 DH |
222 | ee = &serr->ee; |
223 | ||
17926a79 DH |
224 | err = ee->ee_errno; |
225 | ||
226 | switch (ee->ee_origin) { | |
227 | case SO_EE_ORIGIN_ICMP: | |
17926a79 DH |
228 | switch (ee->ee_type) { |
229 | case ICMP_DEST_UNREACH: | |
230 | switch (ee->ee_code) { | |
231 | case ICMP_NET_UNREACH: | |
232 | _net("Rx Received ICMP Network Unreachable"); | |
17926a79 DH |
233 | break; |
234 | case ICMP_HOST_UNREACH: | |
235 | _net("Rx Received ICMP Host Unreachable"); | |
17926a79 DH |
236 | break; |
237 | case ICMP_PORT_UNREACH: | |
238 | _net("Rx Received ICMP Port Unreachable"); | |
17926a79 DH |
239 | break; |
240 | case ICMP_NET_UNKNOWN: | |
241 | _net("Rx Received ICMP Unknown Network"); | |
17926a79 DH |
242 | break; |
243 | case ICMP_HOST_UNKNOWN: | |
244 | _net("Rx Received ICMP Unknown Host"); | |
17926a79 DH |
245 | break; |
246 | default: | |
247 | _net("Rx Received ICMP DestUnreach code=%u", | |
248 | ee->ee_code); | |
249 | break; | |
250 | } | |
251 | break; | |
252 | ||
253 | case ICMP_TIME_EXCEEDED: | |
254 | _net("Rx Received ICMP TTL Exceeded"); | |
255 | break; | |
256 | ||
257 | default: | |
258 | _proto("Rx Received ICMP error { type=%u code=%u }", | |
259 | ee->ee_type, ee->ee_code); | |
260 | break; | |
261 | } | |
262 | break; | |
263 | ||
f66d7490 | 264 | case SO_EE_ORIGIN_NONE: |
17926a79 | 265 | case SO_EE_ORIGIN_LOCAL: |
fe77d5fc | 266 | _proto("Rx Received local error { error=%d }", err); |
f3344303 | 267 | compl = RXRPC_CALL_LOCAL_ERROR; |
17926a79 DH |
268 | break; |
269 | ||
17926a79 DH |
270 | case SO_EE_ORIGIN_ICMP6: |
271 | default: | |
fe77d5fc | 272 | _proto("Rx Received error report { orig=%u }", ee->ee_origin); |
17926a79 DH |
273 | break; |
274 | } | |
275 | ||
f3344303 | 276 | rxrpc_distribute_error(peer, err, compl); |
f66d7490 DH |
277 | } |
278 | ||
279 | /* | |
f3344303 | 280 | * Distribute an error that occurred on a peer. |
f66d7490 | 281 | */ |
f3344303 DH |
282 | static void rxrpc_distribute_error(struct rxrpc_peer *peer, int error, |
283 | enum rxrpc_call_completion compl) | |
f66d7490 | 284 | { |
f66d7490 | 285 | struct rxrpc_call *call; |
17926a79 | 286 | |
f3344303 | 287 | hlist_for_each_entry_rcu(call, &peer->error_targets, error_link) { |
e34d4234 | 288 | rxrpc_see_call(call); |
f3344303 DH |
289 | if (call->state < RXRPC_CALL_COMPLETE && |
290 | rxrpc_set_call_completion(call, compl, 0, -error)) | |
248f219c | 291 | rxrpc_notify_socket(call); |
17926a79 | 292 | } |
17926a79 | 293 | } |
cf1a6474 DH |
294 | |
295 | /* | |
296 | * Add RTT information to cache. This is called in softirq mode and has | |
297 | * exclusive access to the peer RTT data. | |
298 | */ | |
299 | void rxrpc_peer_add_rtt(struct rxrpc_call *call, enum rxrpc_rtt_rx_trace why, | |
300 | rxrpc_serial_t send_serial, rxrpc_serial_t resp_serial, | |
301 | ktime_t send_time, ktime_t resp_time) | |
302 | { | |
303 | struct rxrpc_peer *peer = call->peer; | |
304 | s64 rtt; | |
305 | u64 sum = peer->rtt_sum, avg; | |
306 | u8 cursor = peer->rtt_cursor, usage = peer->rtt_usage; | |
307 | ||
308 | rtt = ktime_to_ns(ktime_sub(resp_time, send_time)); | |
309 | if (rtt < 0) | |
310 | return; | |
311 | ||
c1e15b49 DH |
312 | spin_lock(&peer->rtt_input_lock); |
313 | ||
cf1a6474 DH |
314 | /* Replace the oldest datum in the RTT buffer */ |
315 | sum -= peer->rtt_cache[cursor]; | |
316 | sum += rtt; | |
317 | peer->rtt_cache[cursor] = rtt; | |
318 | peer->rtt_cursor = (cursor + 1) & (RXRPC_RTT_CACHE_SIZE - 1); | |
319 | peer->rtt_sum = sum; | |
320 | if (usage < RXRPC_RTT_CACHE_SIZE) { | |
321 | usage++; | |
322 | peer->rtt_usage = usage; | |
323 | } | |
324 | ||
c1e15b49 DH |
325 | spin_unlock(&peer->rtt_input_lock); |
326 | ||
cf1a6474 DH |
327 | /* Now recalculate the average */ |
328 | if (usage == RXRPC_RTT_CACHE_SIZE) { | |
329 | avg = sum / RXRPC_RTT_CACHE_SIZE; | |
330 | } else { | |
331 | avg = sum; | |
332 | do_div(avg, usage); | |
333 | } | |
334 | ||
c1e15b49 | 335 | /* Don't need to update this under lock */ |
cf1a6474 DH |
336 | peer->rtt = avg; |
337 | trace_rxrpc_rtt_rx(call, why, send_serial, resp_serial, rtt, | |
338 | usage, avg); | |
339 | } | |
ace45bec DH |
340 | |
341 | /* | |
330bdcfa | 342 | * Perform keep-alive pings. |
ace45bec | 343 | */ |
330bdcfa DH |
344 | static void rxrpc_peer_keepalive_dispatch(struct rxrpc_net *rxnet, |
345 | struct list_head *collector, | |
346 | time64_t base, | |
347 | u8 cursor) | |
ace45bec | 348 | { |
ace45bec | 349 | struct rxrpc_peer *peer; |
330bdcfa DH |
350 | const u8 mask = ARRAY_SIZE(rxnet->peer_keepalive) - 1; |
351 | time64_t keepalive_at; | |
352 | int slot; | |
ace45bec | 353 | |
330bdcfa | 354 | spin_lock_bh(&rxnet->peer_hash_lock); |
ace45bec | 355 | |
330bdcfa DH |
356 | while (!list_empty(collector)) { |
357 | peer = list_entry(collector->next, | |
358 | struct rxrpc_peer, keepalive_link); | |
ace45bec | 359 | |
330bdcfa DH |
360 | list_del_init(&peer->keepalive_link); |
361 | if (!rxrpc_get_peer_maybe(peer)) | |
362 | continue; | |
ace45bec | 363 | |
ace45bec | 364 | spin_unlock_bh(&rxnet->peer_hash_lock); |
ace45bec | 365 | |
330bdcfa DH |
366 | keepalive_at = peer->last_tx_at + RXRPC_KEEPALIVE_TIME; |
367 | slot = keepalive_at - base; | |
368 | _debug("%02x peer %u t=%d {%pISp}", | |
369 | cursor, peer->debug_id, slot, &peer->srx.transport); | |
370 | ||
371 | if (keepalive_at <= base || | |
372 | keepalive_at > base + RXRPC_KEEPALIVE_TIME) { | |
373 | rxrpc_send_keepalive(peer); | |
374 | slot = RXRPC_KEEPALIVE_TIME; | |
ace45bec DH |
375 | } |
376 | ||
330bdcfa DH |
377 | /* A transmission to this peer occurred since last we examined |
378 | * it so put it into the appropriate future bucket. | |
379 | */ | |
380 | slot += cursor; | |
381 | slot &= mask; | |
382 | spin_lock_bh(&rxnet->peer_hash_lock); | |
383 | list_add_tail(&peer->keepalive_link, | |
384 | &rxnet->peer_keepalive[slot & mask]); | |
385 | rxrpc_put_peer(peer); | |
ace45bec DH |
386 | } |
387 | ||
ace45bec | 388 | spin_unlock_bh(&rxnet->peer_hash_lock); |
330bdcfa | 389 | } |
ace45bec | 390 | |
330bdcfa DH |
391 | /* |
392 | * Perform keep-alive pings with VERSION packets to keep any NAT alive. | |
393 | */ | |
394 | void rxrpc_peer_keepalive_worker(struct work_struct *work) | |
395 | { | |
396 | struct rxrpc_net *rxnet = | |
397 | container_of(work, struct rxrpc_net, peer_keepalive_work); | |
398 | const u8 mask = ARRAY_SIZE(rxnet->peer_keepalive) - 1; | |
399 | time64_t base, now, delay; | |
400 | u8 cursor, stop; | |
401 | LIST_HEAD(collector); | |
ace45bec | 402 | |
330bdcfa DH |
403 | now = ktime_get_seconds(); |
404 | base = rxnet->peer_keepalive_base; | |
405 | cursor = rxnet->peer_keepalive_cursor; | |
406 | _enter("%lld,%u", base - now, cursor); | |
ace45bec | 407 | |
330bdcfa DH |
408 | if (!rxnet->live) |
409 | return; | |
ace45bec | 410 | |
330bdcfa DH |
411 | /* Remove to a temporary list all the peers that are currently lodged |
412 | * in expired buckets plus all new peers. | |
413 | * | |
414 | * Everything in the bucket at the cursor is processed this | |
415 | * second; the bucket at cursor + 1 goes at now + 1s and so | |
416 | * on... | |
ace45bec | 417 | */ |
ace45bec | 418 | spin_lock_bh(&rxnet->peer_hash_lock); |
330bdcfa DH |
419 | list_splice_init(&rxnet->peer_keepalive_new, &collector); |
420 | ||
421 | stop = cursor + ARRAY_SIZE(rxnet->peer_keepalive); | |
422 | while (base <= now && (s8)(cursor - stop) < 0) { | |
423 | list_splice_tail_init(&rxnet->peer_keepalive[cursor & mask], | |
424 | &collector); | |
425 | base++; | |
426 | cursor++; | |
427 | } | |
ace45bec | 428 | |
330bdcfa DH |
429 | base = now; |
430 | spin_unlock_bh(&rxnet->peer_hash_lock); | |
ace45bec | 431 | |
ace45bec DH |
432 | rxnet->peer_keepalive_base = base; |
433 | rxnet->peer_keepalive_cursor = cursor; | |
330bdcfa DH |
434 | rxrpc_peer_keepalive_dispatch(rxnet, &collector, base, cursor); |
435 | ASSERT(list_empty(&collector)); | |
436 | ||
437 | /* Schedule the timer for the next occupied timeslot. */ | |
438 | cursor = rxnet->peer_keepalive_cursor; | |
439 | stop = cursor + RXRPC_KEEPALIVE_TIME - 1; | |
440 | for (; (s8)(cursor - stop) < 0; cursor++) { | |
441 | if (!list_empty(&rxnet->peer_keepalive[cursor & mask])) | |
442 | break; | |
443 | base++; | |
444 | } | |
445 | ||
446 | now = ktime_get_seconds(); | |
447 | delay = base - now; | |
448 | if (delay < 1) | |
449 | delay = 1; | |
450 | delay *= HZ; | |
451 | if (rxnet->live) | |
452 | timer_reduce(&rxnet->peer_keepalive_timer, jiffies + delay); | |
453 | ||
ace45bec DH |
454 | _leave(""); |
455 | } |