blob: 9e0725f5652bd2faa0ab907933c04c530713e1dd [file] [log] [blame]
David Howellsf66d7492016-04-04 14:00:34 +01001/* Peer event handling, typically ICMP messages.
David Howells17926a72007-04-26 15:48:28 -07002 *
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
David Howellsf66d7492016-04-04 14:00:34 +010025static void rxrpc_store_error(struct rxrpc_peer *, struct sock_exterr_skb *);
26
David Howells17926a72007-04-26 15:48:28 -070027/*
David Howellsbe6e6702016-04-04 14:00:32 +010028 * Find the peer associated with an ICMP packet.
29 */
30static struct rxrpc_peer *rxrpc_lookup_peer_icmp_rcu(struct rxrpc_local *local,
31 const struct sk_buff *skb)
32{
33 struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
34 struct sockaddr_rxrpc srx;
35
36 _enter("");
37
38 memset(&srx, 0, sizeof(srx));
39 srx.transport_type = local->srx.transport_type;
40 srx.transport.family = local->srx.transport.family;
41
42 /* Can we see an ICMP4 packet on an ICMP6 listening socket? and vice
43 * versa?
44 */
45 switch (srx.transport.family) {
46 case AF_INET:
47 srx.transport.sin.sin_port = serr->port;
48 srx.transport_len = sizeof(struct sockaddr_in);
49 switch (serr->ee.ee_origin) {
50 case SO_EE_ORIGIN_ICMP:
51 _net("Rx ICMP");
52 memcpy(&srx.transport.sin.sin_addr,
53 skb_network_header(skb) + serr->addr_offset,
54 sizeof(struct in_addr));
55 break;
56 case SO_EE_ORIGIN_ICMP6:
57 _net("Rx ICMP6 on v4 sock");
58 memcpy(&srx.transport.sin.sin_addr,
59 skb_network_header(skb) + serr->addr_offset + 12,
60 sizeof(struct in_addr));
61 break;
62 default:
63 memcpy(&srx.transport.sin.sin_addr, &ip_hdr(skb)->saddr,
64 sizeof(struct in_addr));
65 break;
66 }
67 break;
68
David Howellsd1912742016-09-17 07:26:01 +010069#ifdef CONFIG_AF_RXRPC_IPV6
David Howells75b54cb2016-09-13 08:49:05 +010070 case AF_INET6:
71 srx.transport.sin6.sin6_port = serr->port;
72 srx.transport_len = sizeof(struct sockaddr_in6);
73 switch (serr->ee.ee_origin) {
74 case SO_EE_ORIGIN_ICMP6:
75 _net("Rx ICMP6");
76 memcpy(&srx.transport.sin6.sin6_addr,
77 skb_network_header(skb) + serr->addr_offset,
78 sizeof(struct in6_addr));
79 break;
80 case SO_EE_ORIGIN_ICMP:
81 _net("Rx ICMP on v6 sock");
David Howellsd1912742016-09-17 07:26:01 +010082 memcpy(srx.transport.sin6.sin6_addr.s6_addr + 12,
David Howells75b54cb2016-09-13 08:49:05 +010083 skb_network_header(skb) + serr->addr_offset,
84 sizeof(struct in_addr));
85 break;
86 default:
87 memcpy(&srx.transport.sin6.sin6_addr,
88 &ipv6_hdr(skb)->saddr,
89 sizeof(struct in6_addr));
90 break;
91 }
92 break;
David Howellsd1912742016-09-17 07:26:01 +010093#endif
David Howells75b54cb2016-09-13 08:49:05 +010094
David Howellsbe6e6702016-04-04 14:00:32 +010095 default:
96 BUG();
97 }
98
99 return rxrpc_lookup_peer_rcu(local, &srx);
100}
101
102/*
David Howells1a70c052016-04-04 14:00:33 +0100103 * Handle an MTU/fragmentation problem.
104 */
105static void rxrpc_adjust_mtu(struct rxrpc_peer *peer, struct sock_exterr_skb *serr)
106{
107 u32 mtu = serr->ee.ee_info;
108
109 _net("Rx ICMP Fragmentation Needed (%d)", mtu);
110
111 /* wind down the local interface MTU */
112 if (mtu > 0 && peer->if_mtu == 65535 && mtu < peer->if_mtu) {
113 peer->if_mtu = mtu;
114 _net("I/F MTU %u", mtu);
115 }
116
117 if (mtu == 0) {
118 /* they didn't give us a size, estimate one */
119 mtu = peer->if_mtu;
120 if (mtu > 1500) {
121 mtu >>= 1;
122 if (mtu < 1500)
123 mtu = 1500;
124 } else {
125 mtu -= 100;
126 if (mtu < peer->hdrsize)
127 mtu = peer->hdrsize + 4;
128 }
129 }
130
131 if (mtu < peer->mtu) {
132 spin_lock_bh(&peer->lock);
133 peer->mtu = mtu;
134 peer->maxdata = peer->mtu - peer->hdrsize;
135 spin_unlock_bh(&peer->lock);
136 _net("Net MTU %u (maxdata %u)",
137 peer->mtu, peer->maxdata);
138 }
139}
140
141/*
David Howellsf66d7492016-04-04 14:00:34 +0100142 * Handle an error received on the local endpoint.
David Howells17926a72007-04-26 15:48:28 -0700143 */
David Howellsabe89ef2016-04-04 14:00:32 +0100144void rxrpc_error_report(struct sock *sk)
David Howells17926a72007-04-26 15:48:28 -0700145{
146 struct sock_exterr_skb *serr;
David Howells17926a72007-04-26 15:48:28 -0700147 struct rxrpc_local *local = sk->sk_user_data;
148 struct rxrpc_peer *peer;
149 struct sk_buff *skb;
David Howells17926a72007-04-26 15:48:28 -0700150
151 _enter("%p{%d}", sk, local->debug_id);
152
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400153 skb = sock_dequeue_err_skb(sk);
David Howells17926a72007-04-26 15:48:28 -0700154 if (!skb) {
155 _leave("UDP socket errqueue empty");
156 return;
157 }
David Howells248f2192016-09-08 11:10:12 +0100158 rxrpc_new_skb(skb);
Willem de Bruijnc247f052015-03-07 20:33:22 -0500159 serr = SKB_EXT_ERR(skb);
160 if (!skb->len && serr->ee.ee_origin == SO_EE_ORIGIN_TIMESTAMPING) {
Willem de Bruijn49ca0d82015-01-30 13:29:31 -0500161 _leave("UDP empty message");
David Howells248f2192016-09-08 11:10:12 +0100162 rxrpc_free_skb(skb);
Willem de Bruijn49ca0d82015-01-30 13:29:31 -0500163 return;
164 }
David Howells17926a72007-04-26 15:48:28 -0700165
David Howellsbe6e6702016-04-04 14:00:32 +0100166 rcu_read_lock();
167 peer = rxrpc_lookup_peer_icmp_rcu(local, skb);
168 if (peer && !rxrpc_get_peer_maybe(peer))
169 peer = NULL;
170 if (!peer) {
171 rcu_read_unlock();
David Howells17926a72007-04-26 15:48:28 -0700172 rxrpc_free_skb(skb);
173 _leave(" [no peer]");
174 return;
175 }
176
David Howells1a70c052016-04-04 14:00:33 +0100177 if ((serr->ee.ee_origin == SO_EE_ORIGIN_ICMP &&
178 serr->ee.ee_type == ICMP_DEST_UNREACH &&
179 serr->ee.ee_code == ICMP_FRAG_NEEDED)) {
180 rxrpc_adjust_mtu(peer, serr);
David Howellsf66d7492016-04-04 14:00:34 +0100181 rcu_read_unlock();
David Howells1a70c052016-04-04 14:00:33 +0100182 rxrpc_free_skb(skb);
David Howellsf66d7492016-04-04 14:00:34 +0100183 rxrpc_put_peer(peer);
184 _leave(" [MTU update]");
185 return;
David Howells17926a72007-04-26 15:48:28 -0700186 }
187
David Howellsf66d7492016-04-04 14:00:34 +0100188 rxrpc_store_error(peer, serr);
David Howellsbe6e6702016-04-04 14:00:32 +0100189 rcu_read_unlock();
David Howellsf66d7492016-04-04 14:00:34 +0100190 rxrpc_free_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700191
David Howellsf66d7492016-04-04 14:00:34 +0100192 /* The ref we obtained is passed off to the work item */
193 rxrpc_queue_work(&peer->error_distributor);
David Howells17926a72007-04-26 15:48:28 -0700194 _leave("");
195}
196
197/*
David Howellsf66d7492016-04-04 14:00:34 +0100198 * Map an error report to error codes on the peer record.
David Howells17926a72007-04-26 15:48:28 -0700199 */
David Howellsf66d7492016-04-04 14:00:34 +0100200static void rxrpc_store_error(struct rxrpc_peer *peer,
201 struct sock_exterr_skb *serr)
David Howells17926a72007-04-26 15:48:28 -0700202{
203 struct sock_extended_err *ee;
David S. Millerc9d10c42011-05-19 18:37:11 -0400204 int err;
David Howells17926a72007-04-26 15:48:28 -0700205
206 _enter("");
207
David Howells17926a72007-04-26 15:48:28 -0700208 ee = &serr->ee;
209
210 _net("Rx Error o=%d t=%d c=%d e=%d",
211 ee->ee_origin, ee->ee_type, ee->ee_code, ee->ee_errno);
212
213 err = ee->ee_errno;
214
215 switch (ee->ee_origin) {
216 case SO_EE_ORIGIN_ICMP:
David Howells17926a72007-04-26 15:48:28 -0700217 switch (ee->ee_type) {
218 case ICMP_DEST_UNREACH:
219 switch (ee->ee_code) {
220 case ICMP_NET_UNREACH:
221 _net("Rx Received ICMP Network Unreachable");
David Howells17926a72007-04-26 15:48:28 -0700222 break;
223 case ICMP_HOST_UNREACH:
224 _net("Rx Received ICMP Host Unreachable");
David Howells17926a72007-04-26 15:48:28 -0700225 break;
226 case ICMP_PORT_UNREACH:
227 _net("Rx Received ICMP Port Unreachable");
David Howells17926a72007-04-26 15:48:28 -0700228 break;
229 case ICMP_NET_UNKNOWN:
230 _net("Rx Received ICMP Unknown Network");
David Howells17926a72007-04-26 15:48:28 -0700231 break;
232 case ICMP_HOST_UNKNOWN:
233 _net("Rx Received ICMP Unknown Host");
David Howells17926a72007-04-26 15:48:28 -0700234 break;
235 default:
236 _net("Rx Received ICMP DestUnreach code=%u",
237 ee->ee_code);
238 break;
239 }
240 break;
241
242 case ICMP_TIME_EXCEEDED:
243 _net("Rx Received ICMP TTL Exceeded");
244 break;
245
246 default:
247 _proto("Rx Received ICMP error { type=%u code=%u }",
248 ee->ee_type, ee->ee_code);
249 break;
250 }
251 break;
252
David Howellsf66d7492016-04-04 14:00:34 +0100253 case SO_EE_ORIGIN_NONE:
David Howells17926a72007-04-26 15:48:28 -0700254 case SO_EE_ORIGIN_LOCAL:
David Howellsfe77d5f2016-04-04 14:00:34 +0100255 _proto("Rx Received local error { error=%d }", err);
David Howellsf66d7492016-04-04 14:00:34 +0100256 err += RXRPC_LOCAL_ERROR_OFFSET;
David Howells17926a72007-04-26 15:48:28 -0700257 break;
258
David Howells17926a72007-04-26 15:48:28 -0700259 case SO_EE_ORIGIN_ICMP6:
260 default:
David Howellsfe77d5f2016-04-04 14:00:34 +0100261 _proto("Rx Received error report { orig=%u }", ee->ee_origin);
David Howells17926a72007-04-26 15:48:28 -0700262 break;
263 }
264
David Howellsf66d7492016-04-04 14:00:34 +0100265 peer->error_report = err;
266}
David Howells17926a72007-04-26 15:48:28 -0700267
David Howellsf66d7492016-04-04 14:00:34 +0100268/*
269 * Distribute an error that occurred on a peer
270 */
271void rxrpc_peer_error_distributor(struct work_struct *work)
272{
273 struct rxrpc_peer *peer =
274 container_of(work, struct rxrpc_peer, error_distributor);
275 struct rxrpc_call *call;
David Howellsf5c17aa2016-08-30 09:49:28 +0100276 enum rxrpc_call_completion compl;
David Howellsf5c17aa2016-08-30 09:49:28 +0100277 int error;
David Howells17926a72007-04-26 15:48:28 -0700278
David Howellsf66d7492016-04-04 14:00:34 +0100279 _enter("");
David Howells17926a72007-04-26 15:48:28 -0700280
David Howellsf5c17aa2016-08-30 09:49:28 +0100281 error = READ_ONCE(peer->error_report);
282 if (error < RXRPC_LOCAL_ERROR_OFFSET) {
283 compl = RXRPC_CALL_NETWORK_ERROR;
284 } else {
285 compl = RXRPC_CALL_LOCAL_ERROR;
286 error -= RXRPC_LOCAL_ERROR_OFFSET;
287 }
David Howellsf66d7492016-04-04 14:00:34 +0100288
David Howellsf5c17aa2016-08-30 09:49:28 +0100289 _debug("ISSUE ERROR %s %d", rxrpc_call_completions[compl], error);
David Howellsf66d7492016-04-04 14:00:34 +0100290
291 spin_lock_bh(&peer->lock);
292
293 while (!hlist_empty(&peer->error_targets)) {
294 call = hlist_entry(peer->error_targets.first,
295 struct rxrpc_call, error_link);
296 hlist_del_init(&call->error_link);
David Howellse34d4232016-08-30 09:49:29 +0100297 rxrpc_see_call(call);
David Howellsf66d7492016-04-04 14:00:34 +0100298
David Howells248f2192016-09-08 11:10:12 +0100299 if (rxrpc_set_call_completion(call, compl, 0, error))
300 rxrpc_notify_socket(call);
David Howells17926a72007-04-26 15:48:28 -0700301 }
302
David Howellsf66d7492016-04-04 14:00:34 +0100303 spin_unlock_bh(&peer->lock);
David Howells17926a72007-04-26 15:48:28 -0700304
David Howellsf66d7492016-04-04 14:00:34 +0100305 rxrpc_put_peer(peer);
David Howells17926a72007-04-26 15:48:28 -0700306 _leave("");
307}