blob: 865078d76ad3a22e3741d8779037d2a155f396e7 [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
69 default:
70 BUG();
71 }
72
73 return rxrpc_lookup_peer_rcu(local, &srx);
74}
75
76/*
David Howells1a70c052016-04-04 14:00:33 +010077 * Handle an MTU/fragmentation problem.
78 */
79static void rxrpc_adjust_mtu(struct rxrpc_peer *peer, struct sock_exterr_skb *serr)
80{
81 u32 mtu = serr->ee.ee_info;
82
83 _net("Rx ICMP Fragmentation Needed (%d)", mtu);
84
85 /* wind down the local interface MTU */
86 if (mtu > 0 && peer->if_mtu == 65535 && mtu < peer->if_mtu) {
87 peer->if_mtu = mtu;
88 _net("I/F MTU %u", mtu);
89 }
90
91 if (mtu == 0) {
92 /* they didn't give us a size, estimate one */
93 mtu = peer->if_mtu;
94 if (mtu > 1500) {
95 mtu >>= 1;
96 if (mtu < 1500)
97 mtu = 1500;
98 } else {
99 mtu -= 100;
100 if (mtu < peer->hdrsize)
101 mtu = peer->hdrsize + 4;
102 }
103 }
104
105 if (mtu < peer->mtu) {
106 spin_lock_bh(&peer->lock);
107 peer->mtu = mtu;
108 peer->maxdata = peer->mtu - peer->hdrsize;
109 spin_unlock_bh(&peer->lock);
110 _net("Net MTU %u (maxdata %u)",
111 peer->mtu, peer->maxdata);
112 }
113}
114
115/*
David Howellsf66d7492016-04-04 14:00:34 +0100116 * Handle an error received on the local endpoint.
David Howells17926a72007-04-26 15:48:28 -0700117 */
David Howellsabe89ef2016-04-04 14:00:32 +0100118void rxrpc_error_report(struct sock *sk)
David Howells17926a72007-04-26 15:48:28 -0700119{
120 struct sock_exterr_skb *serr;
David Howells17926a72007-04-26 15:48:28 -0700121 struct rxrpc_local *local = sk->sk_user_data;
122 struct rxrpc_peer *peer;
123 struct sk_buff *skb;
David Howells17926a72007-04-26 15:48:28 -0700124
125 _enter("%p{%d}", sk, local->debug_id);
126
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400127 skb = sock_dequeue_err_skb(sk);
David Howells17926a72007-04-26 15:48:28 -0700128 if (!skb) {
129 _leave("UDP socket errqueue empty");
130 return;
131 }
Willem de Bruijnc247f052015-03-07 20:33:22 -0500132 serr = SKB_EXT_ERR(skb);
133 if (!skb->len && serr->ee.ee_origin == SO_EE_ORIGIN_TIMESTAMPING) {
Willem de Bruijn49ca0d82015-01-30 13:29:31 -0500134 _leave("UDP empty message");
135 kfree_skb(skb);
136 return;
137 }
David Howells17926a72007-04-26 15:48:28 -0700138
139 rxrpc_new_skb(skb);
140
David Howellsbe6e6702016-04-04 14:00:32 +0100141 rcu_read_lock();
142 peer = rxrpc_lookup_peer_icmp_rcu(local, skb);
143 if (peer && !rxrpc_get_peer_maybe(peer))
144 peer = NULL;
145 if (!peer) {
146 rcu_read_unlock();
David Howells17926a72007-04-26 15:48:28 -0700147 rxrpc_free_skb(skb);
148 _leave(" [no peer]");
149 return;
150 }
151
David Howells1a70c052016-04-04 14:00:33 +0100152 if ((serr->ee.ee_origin == SO_EE_ORIGIN_ICMP &&
153 serr->ee.ee_type == ICMP_DEST_UNREACH &&
154 serr->ee.ee_code == ICMP_FRAG_NEEDED)) {
155 rxrpc_adjust_mtu(peer, serr);
David Howellsf66d7492016-04-04 14:00:34 +0100156 rcu_read_unlock();
David Howells1a70c052016-04-04 14:00:33 +0100157 rxrpc_free_skb(skb);
David Howellsf66d7492016-04-04 14:00:34 +0100158 rxrpc_put_peer(peer);
159 _leave(" [MTU update]");
160 return;
David Howells17926a72007-04-26 15:48:28 -0700161 }
162
David Howellsf66d7492016-04-04 14:00:34 +0100163 rxrpc_store_error(peer, serr);
David Howellsbe6e6702016-04-04 14:00:32 +0100164 rcu_read_unlock();
David Howellsf66d7492016-04-04 14:00:34 +0100165 rxrpc_free_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700166
David Howellsf66d7492016-04-04 14:00:34 +0100167 /* The ref we obtained is passed off to the work item */
168 rxrpc_queue_work(&peer->error_distributor);
David Howells17926a72007-04-26 15:48:28 -0700169 _leave("");
170}
171
172/*
David Howellsf66d7492016-04-04 14:00:34 +0100173 * Map an error report to error codes on the peer record.
David Howells17926a72007-04-26 15:48:28 -0700174 */
David Howellsf66d7492016-04-04 14:00:34 +0100175static void rxrpc_store_error(struct rxrpc_peer *peer,
176 struct sock_exterr_skb *serr)
David Howells17926a72007-04-26 15:48:28 -0700177{
178 struct sock_extended_err *ee;
David S. Millerc9d10c42011-05-19 18:37:11 -0400179 int err;
David Howells17926a72007-04-26 15:48:28 -0700180
181 _enter("");
182
David Howells17926a72007-04-26 15:48:28 -0700183 ee = &serr->ee;
184
185 _net("Rx Error o=%d t=%d c=%d e=%d",
186 ee->ee_origin, ee->ee_type, ee->ee_code, ee->ee_errno);
187
188 err = ee->ee_errno;
189
190 switch (ee->ee_origin) {
191 case SO_EE_ORIGIN_ICMP:
David Howells17926a72007-04-26 15:48:28 -0700192 switch (ee->ee_type) {
193 case ICMP_DEST_UNREACH:
194 switch (ee->ee_code) {
195 case ICMP_NET_UNREACH:
196 _net("Rx Received ICMP Network Unreachable");
David Howells17926a72007-04-26 15:48:28 -0700197 break;
198 case ICMP_HOST_UNREACH:
199 _net("Rx Received ICMP Host Unreachable");
David Howells17926a72007-04-26 15:48:28 -0700200 break;
201 case ICMP_PORT_UNREACH:
202 _net("Rx Received ICMP Port Unreachable");
David Howells17926a72007-04-26 15:48:28 -0700203 break;
204 case ICMP_NET_UNKNOWN:
205 _net("Rx Received ICMP Unknown Network");
David Howells17926a72007-04-26 15:48:28 -0700206 break;
207 case ICMP_HOST_UNKNOWN:
208 _net("Rx Received ICMP Unknown Host");
David Howells17926a72007-04-26 15:48:28 -0700209 break;
210 default:
211 _net("Rx Received ICMP DestUnreach code=%u",
212 ee->ee_code);
213 break;
214 }
215 break;
216
217 case ICMP_TIME_EXCEEDED:
218 _net("Rx Received ICMP TTL Exceeded");
219 break;
220
221 default:
222 _proto("Rx Received ICMP error { type=%u code=%u }",
223 ee->ee_type, ee->ee_code);
224 break;
225 }
226 break;
227
David Howellsf66d7492016-04-04 14:00:34 +0100228 case SO_EE_ORIGIN_NONE:
David Howells17926a72007-04-26 15:48:28 -0700229 case SO_EE_ORIGIN_LOCAL:
David Howellsfe77d5f2016-04-04 14:00:34 +0100230 _proto("Rx Received local error { error=%d }", err);
David Howellsf66d7492016-04-04 14:00:34 +0100231 err += RXRPC_LOCAL_ERROR_OFFSET;
David Howells17926a72007-04-26 15:48:28 -0700232 break;
233
David Howells17926a72007-04-26 15:48:28 -0700234 case SO_EE_ORIGIN_ICMP6:
235 default:
David Howellsfe77d5f2016-04-04 14:00:34 +0100236 _proto("Rx Received error report { orig=%u }", ee->ee_origin);
David Howells17926a72007-04-26 15:48:28 -0700237 break;
238 }
239
David Howellsf66d7492016-04-04 14:00:34 +0100240 peer->error_report = err;
241}
David Howells17926a72007-04-26 15:48:28 -0700242
David Howellsf66d7492016-04-04 14:00:34 +0100243/*
244 * Distribute an error that occurred on a peer
245 */
246void rxrpc_peer_error_distributor(struct work_struct *work)
247{
248 struct rxrpc_peer *peer =
249 container_of(work, struct rxrpc_peer, error_distributor);
250 struct rxrpc_call *call;
David Howellsf5c17aa2016-08-30 09:49:28 +0100251 enum rxrpc_call_completion compl;
252 bool queue;
253 int error;
David Howells17926a72007-04-26 15:48:28 -0700254
David Howellsf66d7492016-04-04 14:00:34 +0100255 _enter("");
David Howells17926a72007-04-26 15:48:28 -0700256
David Howellsf5c17aa2016-08-30 09:49:28 +0100257 error = READ_ONCE(peer->error_report);
258 if (error < RXRPC_LOCAL_ERROR_OFFSET) {
259 compl = RXRPC_CALL_NETWORK_ERROR;
260 } else {
261 compl = RXRPC_CALL_LOCAL_ERROR;
262 error -= RXRPC_LOCAL_ERROR_OFFSET;
263 }
David Howellsf66d7492016-04-04 14:00:34 +0100264
David Howellsf5c17aa2016-08-30 09:49:28 +0100265 _debug("ISSUE ERROR %s %d", rxrpc_call_completions[compl], error);
David Howellsf66d7492016-04-04 14:00:34 +0100266
267 spin_lock_bh(&peer->lock);
268
269 while (!hlist_empty(&peer->error_targets)) {
270 call = hlist_entry(peer->error_targets.first,
271 struct rxrpc_call, error_link);
272 hlist_del_init(&call->error_link);
273
David Howellsf5c17aa2016-08-30 09:49:28 +0100274 queue = false;
David Howellsf66d7492016-04-04 14:00:34 +0100275 write_lock(&call->state_lock);
David Howellsf5c17aa2016-08-30 09:49:28 +0100276 if (__rxrpc_set_call_completion(call, compl, 0, error)) {
David Howellsf66d7492016-04-04 14:00:34 +0100277 set_bit(RXRPC_CALL_EV_RCVD_ERROR, &call->events);
David Howellsf5c17aa2016-08-30 09:49:28 +0100278 queue = true;
David Howells17926a72007-04-26 15:48:28 -0700279 }
David Howellsf66d7492016-04-04 14:00:34 +0100280 write_unlock(&call->state_lock);
David Howellsf5c17aa2016-08-30 09:49:28 +0100281 if (queue)
282 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700283 }
284
David Howellsf66d7492016-04-04 14:00:34 +0100285 spin_unlock_bh(&peer->lock);
David Howells17926a72007-04-26 15:48:28 -0700286
David Howellsf66d7492016-04-04 14:00:34 +0100287 rxrpc_put_peer(peer);
David Howells17926a72007-04-26 15:48:28 -0700288 _leave("");
289}