1/* 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
40	skb = sock_dequeue_err_skb(sk);
41	if (!skb) {
42		_leave("UDP socket errqueue empty");
43		return;
44	}
45	serr = SKB_EXT_ERR(skb);
46	if (!skb->len && serr->ee.ee_origin == SO_EE_ORIGIN_TIMESTAMPING) {
47		_leave("UDP empty message");
48		kfree_skb(skb);
49		return;
50	}
51
52	rxrpc_new_skb(skb);
53
54	addr = *(__be32 *)(skb_network_header(skb) + serr->addr_offset);
55	port = serr->port;
56
57	_net("Rx UDP Error from %pI4:%hu", &addr, ntohs(port));
58	_debug("Msg l:%d d:%d", skb->len, skb->data_len);
59
60	peer = rxrpc_find_peer(local, addr, port);
61	if (IS_ERR(peer)) {
62		rxrpc_free_skb(skb);
63		_leave(" [no peer]");
64		return;
65	}
66
67	trans = rxrpc_find_transport(local, peer);
68	if (!trans) {
69		rxrpc_put_peer(peer);
70		rxrpc_free_skb(skb);
71		_leave(" [no trans]");
72		return;
73	}
74
75	if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP &&
76	    serr->ee.ee_type == ICMP_DEST_UNREACH &&
77	    serr->ee.ee_code == ICMP_FRAG_NEEDED
78	    ) {
79		u32 mtu = serr->ee.ee_info;
80
81		_net("Rx Received ICMP Fragmentation Needed (%d)", mtu);
82
83		/* wind down the local interface MTU */
84		if (mtu > 0 && peer->if_mtu == 65535 && mtu < peer->if_mtu) {
85			peer->if_mtu = mtu;
86			_net("I/F MTU %u", mtu);
87		}
88
89		if (mtu == 0) {
90			/* they didn't give us a size, estimate one */
91			mtu = peer->if_mtu;
92			if (mtu > 1500) {
93				mtu >>= 1;
94				if (mtu < 1500)
95					mtu = 1500;
96			} else {
97				mtu -= 100;
98				if (mtu < peer->hdrsize)
99					mtu = peer->hdrsize + 4;
100			}
101		}
102
103		if (mtu < peer->mtu) {
104			spin_lock_bh(&peer->lock);
105			peer->mtu = mtu;
106			peer->maxdata = peer->mtu - peer->hdrsize;
107			spin_unlock_bh(&peer->lock);
108			_net("Net MTU %u (maxdata %u)",
109			     peer->mtu, peer->maxdata);
110		}
111	}
112
113	rxrpc_put_peer(peer);
114
115	/* pass the transport ref to error_handler to release */
116	skb_queue_tail(&trans->error_queue, skb);
117	rxrpc_queue_work(&trans->error_handler);
118
119	_leave("");
120}
121
122/*
123 * deal with UDP error messages
124 */
125void rxrpc_UDP_error_handler(struct work_struct *work)
126{
127	struct sock_extended_err *ee;
128	struct sock_exterr_skb *serr;
129	struct rxrpc_transport *trans =
130		container_of(work, struct rxrpc_transport, error_handler);
131	struct sk_buff *skb;
132	int err;
133
134	_enter("");
135
136	skb = skb_dequeue(&trans->error_queue);
137	if (!skb)
138		return;
139
140	serr = SKB_EXT_ERR(skb);
141	ee = &serr->ee;
142
143	_net("Rx Error o=%d t=%d c=%d e=%d",
144	     ee->ee_origin, ee->ee_type, ee->ee_code, ee->ee_errno);
145
146	err = ee->ee_errno;
147
148	switch (ee->ee_origin) {
149	case SO_EE_ORIGIN_ICMP:
150		switch (ee->ee_type) {
151		case ICMP_DEST_UNREACH:
152			switch (ee->ee_code) {
153			case ICMP_NET_UNREACH:
154				_net("Rx Received ICMP Network Unreachable");
155				err = ENETUNREACH;
156				break;
157			case ICMP_HOST_UNREACH:
158				_net("Rx Received ICMP Host Unreachable");
159				err = EHOSTUNREACH;
160				break;
161			case ICMP_PORT_UNREACH:
162				_net("Rx Received ICMP Port Unreachable");
163				err = ECONNREFUSED;
164				break;
165			case ICMP_FRAG_NEEDED:
166				_net("Rx Received ICMP Fragmentation Needed (%d)",
167				     ee->ee_info);
168				err = 0; /* dealt with elsewhere */
169				break;
170			case ICMP_NET_UNKNOWN:
171				_net("Rx Received ICMP Unknown Network");
172				err = ENETUNREACH;
173				break;
174			case ICMP_HOST_UNKNOWN:
175				_net("Rx Received ICMP Unknown Host");
176				err = EHOSTUNREACH;
177				break;
178			default:
179				_net("Rx Received ICMP DestUnreach code=%u",
180				     ee->ee_code);
181				break;
182			}
183			break;
184
185		case ICMP_TIME_EXCEEDED:
186			_net("Rx Received ICMP TTL Exceeded");
187			break;
188
189		default:
190			_proto("Rx Received ICMP error { type=%u code=%u }",
191			       ee->ee_type, ee->ee_code);
192			break;
193		}
194		break;
195
196	case SO_EE_ORIGIN_LOCAL:
197		_proto("Rx Received local error { error=%d }",
198		       ee->ee_errno);
199		break;
200
201	case SO_EE_ORIGIN_NONE:
202	case SO_EE_ORIGIN_ICMP6:
203	default:
204		_proto("Rx Received error report { orig=%u }",
205		       ee->ee_origin);
206		break;
207	}
208
209	/* terminate all the affected calls if there's an unrecoverable
210	 * error */
211	if (err) {
212		struct rxrpc_call *call, *_n;
213
214		_debug("ISSUE ERROR %d", err);
215
216		spin_lock_bh(&trans->peer->lock);
217		trans->peer->net_error = err;
218
219		list_for_each_entry_safe(call, _n, &trans->peer->error_targets,
220					 error_link) {
221			write_lock(&call->state_lock);
222			if (call->state != RXRPC_CALL_COMPLETE &&
223			    call->state < RXRPC_CALL_NETWORK_ERROR) {
224				call->state = RXRPC_CALL_NETWORK_ERROR;
225				set_bit(RXRPC_CALL_RCVD_ERROR, &call->events);
226				rxrpc_queue_call(call);
227			}
228			write_unlock(&call->state_lock);
229			list_del_init(&call->error_link);
230		}
231
232		spin_unlock_bh(&trans->peer->lock);
233	}
234
235	if (!skb_queue_empty(&trans->error_queue))
236		rxrpc_queue_work(&trans->error_handler);
237
238	rxrpc_free_skb(skb);
239	rxrpc_put_transport(trans);
240	_leave("");
241}
242