blob: 2c2df3a5d1b915ee3a47f746f97534142dc3f7d3 [file] [log] [blame]
David Howells17926a72007-04-26 15:48:28 -07001/* Error message handling (ICMP)
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
25/*
David Howellsbe6e6702016-04-04 14:00:32 +010026 * Find the peer associated with an ICMP packet.
27 */
28static struct rxrpc_peer *rxrpc_lookup_peer_icmp_rcu(struct rxrpc_local *local,
29 const struct sk_buff *skb)
30{
31 struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
32 struct sockaddr_rxrpc srx;
33
34 _enter("");
35
36 memset(&srx, 0, sizeof(srx));
37 srx.transport_type = local->srx.transport_type;
38 srx.transport.family = local->srx.transport.family;
39
40 /* Can we see an ICMP4 packet on an ICMP6 listening socket? and vice
41 * versa?
42 */
43 switch (srx.transport.family) {
44 case AF_INET:
45 srx.transport.sin.sin_port = serr->port;
46 srx.transport_len = sizeof(struct sockaddr_in);
47 switch (serr->ee.ee_origin) {
48 case SO_EE_ORIGIN_ICMP:
49 _net("Rx ICMP");
50 memcpy(&srx.transport.sin.sin_addr,
51 skb_network_header(skb) + serr->addr_offset,
52 sizeof(struct in_addr));
53 break;
54 case SO_EE_ORIGIN_ICMP6:
55 _net("Rx ICMP6 on v4 sock");
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 default:
68 BUG();
69 }
70
71 return rxrpc_lookup_peer_rcu(local, &srx);
72}
73
74/*
David Howells17926a72007-04-26 15:48:28 -070075 * handle an error received on the local endpoint
76 */
David Howellsabe89ef2016-04-04 14:00:32 +010077void rxrpc_error_report(struct sock *sk)
David Howells17926a72007-04-26 15:48:28 -070078{
79 struct sock_exterr_skb *serr;
80 struct rxrpc_transport *trans;
81 struct rxrpc_local *local = sk->sk_user_data;
82 struct rxrpc_peer *peer;
83 struct sk_buff *skb;
84 __be32 addr;
85 __be16 port;
86
87 _enter("%p{%d}", sk, local->debug_id);
88
Willem de Bruijn364a9e92014-08-31 21:30:27 -040089 skb = sock_dequeue_err_skb(sk);
David Howells17926a72007-04-26 15:48:28 -070090 if (!skb) {
91 _leave("UDP socket errqueue empty");
92 return;
93 }
Willem de Bruijnc247f052015-03-07 20:33:22 -050094 serr = SKB_EXT_ERR(skb);
95 if (!skb->len && serr->ee.ee_origin == SO_EE_ORIGIN_TIMESTAMPING) {
Willem de Bruijn49ca0d82015-01-30 13:29:31 -050096 _leave("UDP empty message");
97 kfree_skb(skb);
98 return;
99 }
David Howells17926a72007-04-26 15:48:28 -0700100
101 rxrpc_new_skb(skb);
102
David Howells17926a72007-04-26 15:48:28 -0700103 addr = *(__be32 *)(skb_network_header(skb) + serr->addr_offset);
104 port = serr->port;
105
Harvey Harrison21454aa2008-10-31 00:54:56 -0700106 _net("Rx UDP Error from %pI4:%hu", &addr, ntohs(port));
David Howells17926a72007-04-26 15:48:28 -0700107 _debug("Msg l:%d d:%d", skb->len, skb->data_len);
108
David Howellsbe6e6702016-04-04 14:00:32 +0100109 rcu_read_lock();
110 peer = rxrpc_lookup_peer_icmp_rcu(local, skb);
111 if (peer && !rxrpc_get_peer_maybe(peer))
112 peer = NULL;
113 if (!peer) {
114 rcu_read_unlock();
David Howells17926a72007-04-26 15:48:28 -0700115 rxrpc_free_skb(skb);
116 _leave(" [no peer]");
117 return;
118 }
119
120 trans = rxrpc_find_transport(local, peer);
121 if (!trans) {
David Howellsbe6e6702016-04-04 14:00:32 +0100122 rcu_read_unlock();
David Howells17926a72007-04-26 15:48:28 -0700123 rxrpc_put_peer(peer);
124 rxrpc_free_skb(skb);
125 _leave(" [no trans]");
126 return;
127 }
128
129 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP &&
130 serr->ee.ee_type == ICMP_DEST_UNREACH &&
131 serr->ee.ee_code == ICMP_FRAG_NEEDED
132 ) {
133 u32 mtu = serr->ee.ee_info;
134
135 _net("Rx Received ICMP Fragmentation Needed (%d)", mtu);
136
137 /* wind down the local interface MTU */
138 if (mtu > 0 && peer->if_mtu == 65535 && mtu < peer->if_mtu) {
139 peer->if_mtu = mtu;
140 _net("I/F MTU %u", mtu);
141 }
142
David Howells17926a72007-04-26 15:48:28 -0700143 if (mtu == 0) {
144 /* they didn't give us a size, estimate one */
David Howells6c9a2d32014-02-14 12:02:41 +0000145 mtu = peer->if_mtu;
David Howells17926a72007-04-26 15:48:28 -0700146 if (mtu > 1500) {
147 mtu >>= 1;
148 if (mtu < 1500)
149 mtu = 1500;
150 } else {
151 mtu -= 100;
152 if (mtu < peer->hdrsize)
153 mtu = peer->hdrsize + 4;
154 }
155 }
156
157 if (mtu < peer->mtu) {
David Howells224711d2007-05-04 12:41:11 -0700158 spin_lock_bh(&peer->lock);
David Howells17926a72007-04-26 15:48:28 -0700159 peer->mtu = mtu;
160 peer->maxdata = peer->mtu - peer->hdrsize;
David Howells224711d2007-05-04 12:41:11 -0700161 spin_unlock_bh(&peer->lock);
David Howells17926a72007-04-26 15:48:28 -0700162 _net("Net MTU %u (maxdata %u)",
163 peer->mtu, peer->maxdata);
164 }
165 }
166
David Howellsbe6e6702016-04-04 14:00:32 +0100167 rcu_read_unlock();
David Howells17926a72007-04-26 15:48:28 -0700168 rxrpc_put_peer(peer);
169
170 /* pass the transport ref to error_handler to release */
171 skb_queue_tail(&trans->error_queue, skb);
David Howells651350d2007-04-26 15:50:17 -0700172 rxrpc_queue_work(&trans->error_handler);
David Howells17926a72007-04-26 15:48:28 -0700173 _leave("");
174}
175
176/*
177 * deal with UDP error messages
178 */
179void rxrpc_UDP_error_handler(struct work_struct *work)
180{
181 struct sock_extended_err *ee;
182 struct sock_exterr_skb *serr;
183 struct rxrpc_transport *trans =
184 container_of(work, struct rxrpc_transport, error_handler);
185 struct sk_buff *skb;
David S. Millerc9d10c42011-05-19 18:37:11 -0400186 int err;
David Howells17926a72007-04-26 15:48:28 -0700187
188 _enter("");
189
190 skb = skb_dequeue(&trans->error_queue);
191 if (!skb)
192 return;
193
194 serr = SKB_EXT_ERR(skb);
195 ee = &serr->ee;
196
197 _net("Rx Error o=%d t=%d c=%d e=%d",
198 ee->ee_origin, ee->ee_type, ee->ee_code, ee->ee_errno);
199
200 err = ee->ee_errno;
201
202 switch (ee->ee_origin) {
203 case SO_EE_ORIGIN_ICMP:
David Howells17926a72007-04-26 15:48:28 -0700204 switch (ee->ee_type) {
205 case ICMP_DEST_UNREACH:
206 switch (ee->ee_code) {
207 case ICMP_NET_UNREACH:
208 _net("Rx Received ICMP Network Unreachable");
David Howells17926a72007-04-26 15:48:28 -0700209 break;
210 case ICMP_HOST_UNREACH:
211 _net("Rx Received ICMP Host Unreachable");
David Howells17926a72007-04-26 15:48:28 -0700212 break;
213 case ICMP_PORT_UNREACH:
214 _net("Rx Received ICMP Port Unreachable");
David Howells17926a72007-04-26 15:48:28 -0700215 break;
216 case ICMP_NET_UNKNOWN:
217 _net("Rx Received ICMP Unknown Network");
David Howells17926a72007-04-26 15:48:28 -0700218 break;
219 case ICMP_HOST_UNKNOWN:
220 _net("Rx Received ICMP Unknown Host");
David Howells17926a72007-04-26 15:48:28 -0700221 break;
222 default:
223 _net("Rx Received ICMP DestUnreach code=%u",
224 ee->ee_code);
225 break;
226 }
227 break;
228
229 case ICMP_TIME_EXCEEDED:
230 _net("Rx Received ICMP TTL Exceeded");
231 break;
232
233 default:
234 _proto("Rx Received ICMP error { type=%u code=%u }",
235 ee->ee_type, ee->ee_code);
236 break;
237 }
238 break;
239
240 case SO_EE_ORIGIN_LOCAL:
241 _proto("Rx Received local error { error=%d }",
242 ee->ee_errno);
David Howells17926a72007-04-26 15:48:28 -0700243 break;
244
245 case SO_EE_ORIGIN_NONE:
246 case SO_EE_ORIGIN_ICMP6:
247 default:
248 _proto("Rx Received error report { orig=%u }",
249 ee->ee_origin);
David Howells17926a72007-04-26 15:48:28 -0700250 break;
251 }
252
253 /* terminate all the affected calls if there's an unrecoverable
254 * error */
255 if (err) {
256 struct rxrpc_call *call, *_n;
257
258 _debug("ISSUE ERROR %d", err);
259
260 spin_lock_bh(&trans->peer->lock);
261 trans->peer->net_error = err;
262
263 list_for_each_entry_safe(call, _n, &trans->peer->error_targets,
264 error_link) {
265 write_lock(&call->state_lock);
266 if (call->state != RXRPC_CALL_COMPLETE &&
267 call->state < RXRPC_CALL_NETWORK_ERROR) {
268 call->state = RXRPC_CALL_NETWORK_ERROR;
David Howells4c198ad2016-03-04 15:53:46 +0000269 set_bit(RXRPC_CALL_EV_RCVD_ERROR, &call->events);
David Howells651350d2007-04-26 15:50:17 -0700270 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700271 }
272 write_unlock(&call->state_lock);
273 list_del_init(&call->error_link);
274 }
275
276 spin_unlock_bh(&trans->peer->lock);
277 }
278
279 if (!skb_queue_empty(&trans->error_queue))
David Howells651350d2007-04-26 15:50:17 -0700280 rxrpc_queue_work(&trans->error_handler);
David Howells17926a72007-04-26 15:48:28 -0700281
282 rxrpc_free_skb(skb);
283 rxrpc_put_transport(trans);
284 _leave("");
285}