blob: 74c0fcd36838dfc7326eeddb7726483576da0413 [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/*
26 * handle an error received on the local endpoint
27 */
28void rxrpc_UDP_error_report(struct sock *sk)
29{
30 struct sock_exterr_skb *serr;
31 struct rxrpc_transport *trans;
32 struct rxrpc_local *local = sk->sk_user_data;
33 struct rxrpc_peer *peer;
34 struct sk_buff *skb;
35 __be32 addr;
36 __be16 port;
37
38 _enter("%p{%d}", sk, local->debug_id);
39
Willem de Bruijn364a9e92014-08-31 21:30:27 -040040 skb = sock_dequeue_err_skb(sk);
David Howells17926a72007-04-26 15:48:28 -070041 if (!skb) {
42 _leave("UDP socket errqueue empty");
43 return;
44 }
45
46 rxrpc_new_skb(skb);
47
48 serr = SKB_EXT_ERR(skb);
49 addr = *(__be32 *)(skb_network_header(skb) + serr->addr_offset);
50 port = serr->port;
51
Harvey Harrison21454aa2008-10-31 00:54:56 -070052 _net("Rx UDP Error from %pI4:%hu", &addr, ntohs(port));
David Howells17926a72007-04-26 15:48:28 -070053 _debug("Msg l:%d d:%d", skb->len, skb->data_len);
54
55 peer = rxrpc_find_peer(local, addr, port);
56 if (IS_ERR(peer)) {
57 rxrpc_free_skb(skb);
58 _leave(" [no peer]");
59 return;
60 }
61
62 trans = rxrpc_find_transport(local, peer);
63 if (!trans) {
64 rxrpc_put_peer(peer);
65 rxrpc_free_skb(skb);
66 _leave(" [no trans]");
67 return;
68 }
69
70 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP &&
71 serr->ee.ee_type == ICMP_DEST_UNREACH &&
72 serr->ee.ee_code == ICMP_FRAG_NEEDED
73 ) {
74 u32 mtu = serr->ee.ee_info;
75
76 _net("Rx Received ICMP Fragmentation Needed (%d)", mtu);
77
78 /* wind down the local interface MTU */
79 if (mtu > 0 && peer->if_mtu == 65535 && mtu < peer->if_mtu) {
80 peer->if_mtu = mtu;
81 _net("I/F MTU %u", mtu);
82 }
83
David Howells17926a72007-04-26 15:48:28 -070084 if (mtu == 0) {
85 /* they didn't give us a size, estimate one */
David Howells6c9a2d32014-02-14 12:02:41 +000086 mtu = peer->if_mtu;
David Howells17926a72007-04-26 15:48:28 -070087 if (mtu > 1500) {
88 mtu >>= 1;
89 if (mtu < 1500)
90 mtu = 1500;
91 } else {
92 mtu -= 100;
93 if (mtu < peer->hdrsize)
94 mtu = peer->hdrsize + 4;
95 }
96 }
97
98 if (mtu < peer->mtu) {
David Howells224711d2007-05-04 12:41:11 -070099 spin_lock_bh(&peer->lock);
David Howells17926a72007-04-26 15:48:28 -0700100 peer->mtu = mtu;
101 peer->maxdata = peer->mtu - peer->hdrsize;
David Howells224711d2007-05-04 12:41:11 -0700102 spin_unlock_bh(&peer->lock);
David Howells17926a72007-04-26 15:48:28 -0700103 _net("Net MTU %u (maxdata %u)",
104 peer->mtu, peer->maxdata);
105 }
106 }
107
108 rxrpc_put_peer(peer);
109
110 /* pass the transport ref to error_handler to release */
111 skb_queue_tail(&trans->error_queue, skb);
David Howells651350d2007-04-26 15:50:17 -0700112 rxrpc_queue_work(&trans->error_handler);
David Howells17926a72007-04-26 15:48:28 -0700113
David Howells17926a72007-04-26 15:48:28 -0700114 _leave("");
115}
116
117/*
118 * deal with UDP error messages
119 */
120void rxrpc_UDP_error_handler(struct work_struct *work)
121{
122 struct sock_extended_err *ee;
123 struct sock_exterr_skb *serr;
124 struct rxrpc_transport *trans =
125 container_of(work, struct rxrpc_transport, error_handler);
126 struct sk_buff *skb;
David S. Millerc9d10c42011-05-19 18:37:11 -0400127 int err;
David Howells17926a72007-04-26 15:48:28 -0700128
129 _enter("");
130
131 skb = skb_dequeue(&trans->error_queue);
132 if (!skb)
133 return;
134
135 serr = SKB_EXT_ERR(skb);
136 ee = &serr->ee;
137
138 _net("Rx Error o=%d t=%d c=%d e=%d",
139 ee->ee_origin, ee->ee_type, ee->ee_code, ee->ee_errno);
140
141 err = ee->ee_errno;
142
143 switch (ee->ee_origin) {
144 case SO_EE_ORIGIN_ICMP:
David Howells17926a72007-04-26 15:48:28 -0700145 switch (ee->ee_type) {
146 case ICMP_DEST_UNREACH:
147 switch (ee->ee_code) {
148 case ICMP_NET_UNREACH:
149 _net("Rx Received ICMP Network Unreachable");
150 err = ENETUNREACH;
151 break;
152 case ICMP_HOST_UNREACH:
153 _net("Rx Received ICMP Host Unreachable");
154 err = EHOSTUNREACH;
155 break;
156 case ICMP_PORT_UNREACH:
157 _net("Rx Received ICMP Port Unreachable");
158 err = ECONNREFUSED;
159 break;
160 case ICMP_FRAG_NEEDED:
161 _net("Rx Received ICMP Fragmentation Needed (%d)",
162 ee->ee_info);
163 err = 0; /* dealt with elsewhere */
164 break;
165 case ICMP_NET_UNKNOWN:
166 _net("Rx Received ICMP Unknown Network");
167 err = ENETUNREACH;
168 break;
169 case ICMP_HOST_UNKNOWN:
170 _net("Rx Received ICMP Unknown Host");
171 err = EHOSTUNREACH;
172 break;
173 default:
174 _net("Rx Received ICMP DestUnreach code=%u",
175 ee->ee_code);
176 break;
177 }
178 break;
179
180 case ICMP_TIME_EXCEEDED:
181 _net("Rx Received ICMP TTL Exceeded");
182 break;
183
184 default:
185 _proto("Rx Received ICMP error { type=%u code=%u }",
186 ee->ee_type, ee->ee_code);
187 break;
188 }
189 break;
190
191 case SO_EE_ORIGIN_LOCAL:
192 _proto("Rx Received local error { error=%d }",
193 ee->ee_errno);
David Howells17926a72007-04-26 15:48:28 -0700194 break;
195
196 case SO_EE_ORIGIN_NONE:
197 case SO_EE_ORIGIN_ICMP6:
198 default:
199 _proto("Rx Received error report { orig=%u }",
200 ee->ee_origin);
David Howells17926a72007-04-26 15:48:28 -0700201 break;
202 }
203
204 /* terminate all the affected calls if there's an unrecoverable
205 * error */
206 if (err) {
207 struct rxrpc_call *call, *_n;
208
209 _debug("ISSUE ERROR %d", err);
210
211 spin_lock_bh(&trans->peer->lock);
212 trans->peer->net_error = err;
213
214 list_for_each_entry_safe(call, _n, &trans->peer->error_targets,
215 error_link) {
216 write_lock(&call->state_lock);
217 if (call->state != RXRPC_CALL_COMPLETE &&
218 call->state < RXRPC_CALL_NETWORK_ERROR) {
219 call->state = RXRPC_CALL_NETWORK_ERROR;
220 set_bit(RXRPC_CALL_RCVD_ERROR, &call->events);
David Howells651350d2007-04-26 15:50:17 -0700221 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700222 }
223 write_unlock(&call->state_lock);
224 list_del_init(&call->error_link);
225 }
226
227 spin_unlock_bh(&trans->peer->lock);
228 }
229
230 if (!skb_queue_empty(&trans->error_queue))
David Howells651350d2007-04-26 15:50:17 -0700231 rxrpc_queue_work(&trans->error_handler);
David Howells17926a72007-04-26 15:48:28 -0700232
233 rxrpc_free_skb(skb);
234 rxrpc_put_transport(trans);
235 _leave("");
236}