Merge remote-tracking branch 'tpmdd/next'
[deliverable/linux.git] / net / rxrpc / peer_event.c
CommitLineData
f66d7490 1/* Peer event handling, typically ICMP messages.
17926a79
DH
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
f66d7490
DH
25static void rxrpc_store_error(struct rxrpc_peer *, struct sock_exterr_skb *);
26
be6e6707
DH
27/*
28 * 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
1a70c05b
DH
76/*
77 * 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
17926a79 115/*
f66d7490 116 * Handle an error received on the local endpoint.
17926a79 117 */
abe89ef0 118void rxrpc_error_report(struct sock *sk)
17926a79
DH
119{
120 struct sock_exterr_skb *serr;
17926a79
DH
121 struct rxrpc_local *local = sk->sk_user_data;
122 struct rxrpc_peer *peer;
123 struct sk_buff *skb;
17926a79
DH
124
125 _enter("%p{%d}", sk, local->debug_id);
126
364a9e93 127 skb = sock_dequeue_err_skb(sk);
17926a79
DH
128 if (!skb) {
129 _leave("UDP socket errqueue empty");
130 return;
131 }
248f219c 132 rxrpc_new_skb(skb);
c247f053
WB
133 serr = SKB_EXT_ERR(skb);
134 if (!skb->len && serr->ee.ee_origin == SO_EE_ORIGIN_TIMESTAMPING) {
49ca0d8b 135 _leave("UDP empty message");
248f219c 136 rxrpc_free_skb(skb);
49ca0d8b
WB
137 return;
138 }
17926a79 139
be6e6707
DH
140 rcu_read_lock();
141 peer = rxrpc_lookup_peer_icmp_rcu(local, skb);
142 if (peer && !rxrpc_get_peer_maybe(peer))
143 peer = NULL;
144 if (!peer) {
145 rcu_read_unlock();
17926a79
DH
146 rxrpc_free_skb(skb);
147 _leave(" [no peer]");
148 return;
149 }
150
1a70c05b
DH
151 if ((serr->ee.ee_origin == SO_EE_ORIGIN_ICMP &&
152 serr->ee.ee_type == ICMP_DEST_UNREACH &&
153 serr->ee.ee_code == ICMP_FRAG_NEEDED)) {
154 rxrpc_adjust_mtu(peer, serr);
f66d7490 155 rcu_read_unlock();
1a70c05b 156 rxrpc_free_skb(skb);
f66d7490
DH
157 rxrpc_put_peer(peer);
158 _leave(" [MTU update]");
159 return;
17926a79
DH
160 }
161
f66d7490 162 rxrpc_store_error(peer, serr);
be6e6707 163 rcu_read_unlock();
f66d7490 164 rxrpc_free_skb(skb);
17926a79 165
f66d7490
DH
166 /* The ref we obtained is passed off to the work item */
167 rxrpc_queue_work(&peer->error_distributor);
17926a79
DH
168 _leave("");
169}
170
171/*
f66d7490 172 * Map an error report to error codes on the peer record.
17926a79 173 */
f66d7490
DH
174static void rxrpc_store_error(struct rxrpc_peer *peer,
175 struct sock_exterr_skb *serr)
17926a79
DH
176{
177 struct sock_extended_err *ee;
c9d10c49 178 int err;
17926a79
DH
179
180 _enter("");
181
17926a79
DH
182 ee = &serr->ee;
183
184 _net("Rx Error o=%d t=%d c=%d e=%d",
185 ee->ee_origin, ee->ee_type, ee->ee_code, ee->ee_errno);
186
187 err = ee->ee_errno;
188
189 switch (ee->ee_origin) {
190 case SO_EE_ORIGIN_ICMP:
17926a79
DH
191 switch (ee->ee_type) {
192 case ICMP_DEST_UNREACH:
193 switch (ee->ee_code) {
194 case ICMP_NET_UNREACH:
195 _net("Rx Received ICMP Network Unreachable");
17926a79
DH
196 break;
197 case ICMP_HOST_UNREACH:
198 _net("Rx Received ICMP Host Unreachable");
17926a79
DH
199 break;
200 case ICMP_PORT_UNREACH:
201 _net("Rx Received ICMP Port Unreachable");
17926a79
DH
202 break;
203 case ICMP_NET_UNKNOWN:
204 _net("Rx Received ICMP Unknown Network");
17926a79
DH
205 break;
206 case ICMP_HOST_UNKNOWN:
207 _net("Rx Received ICMP Unknown Host");
17926a79
DH
208 break;
209 default:
210 _net("Rx Received ICMP DestUnreach code=%u",
211 ee->ee_code);
212 break;
213 }
214 break;
215
216 case ICMP_TIME_EXCEEDED:
217 _net("Rx Received ICMP TTL Exceeded");
218 break;
219
220 default:
221 _proto("Rx Received ICMP error { type=%u code=%u }",
222 ee->ee_type, ee->ee_code);
223 break;
224 }
225 break;
226
f66d7490 227 case SO_EE_ORIGIN_NONE:
17926a79 228 case SO_EE_ORIGIN_LOCAL:
fe77d5fc 229 _proto("Rx Received local error { error=%d }", err);
f66d7490 230 err += RXRPC_LOCAL_ERROR_OFFSET;
17926a79
DH
231 break;
232
17926a79
DH
233 case SO_EE_ORIGIN_ICMP6:
234 default:
fe77d5fc 235 _proto("Rx Received error report { orig=%u }", ee->ee_origin);
17926a79
DH
236 break;
237 }
238
f66d7490
DH
239 peer->error_report = err;
240}
241
242/*
243 * Distribute an error that occurred on a peer
244 */
245void rxrpc_peer_error_distributor(struct work_struct *work)
246{
247 struct rxrpc_peer *peer =
248 container_of(work, struct rxrpc_peer, error_distributor);
249 struct rxrpc_call *call;
f5c17aae 250 enum rxrpc_call_completion compl;
f5c17aae 251 int error;
f66d7490
DH
252
253 _enter("");
17926a79 254
f5c17aae
DH
255 error = READ_ONCE(peer->error_report);
256 if (error < RXRPC_LOCAL_ERROR_OFFSET) {
257 compl = RXRPC_CALL_NETWORK_ERROR;
258 } else {
259 compl = RXRPC_CALL_LOCAL_ERROR;
260 error -= RXRPC_LOCAL_ERROR_OFFSET;
261 }
17926a79 262
f5c17aae 263 _debug("ISSUE ERROR %s %d", rxrpc_call_completions[compl], error);
17926a79 264
f66d7490 265 spin_lock_bh(&peer->lock);
17926a79 266
f66d7490
DH
267 while (!hlist_empty(&peer->error_targets)) {
268 call = hlist_entry(peer->error_targets.first,
269 struct rxrpc_call, error_link);
270 hlist_del_init(&call->error_link);
e34d4234 271 rxrpc_see_call(call);
f66d7490 272
248f219c
DH
273 if (rxrpc_set_call_completion(call, compl, 0, error))
274 rxrpc_notify_socket(call);
17926a79
DH
275 }
276
f66d7490 277 spin_unlock_bh(&peer->lock);
17926a79 278
f66d7490 279 rxrpc_put_peer(peer);
17926a79
DH
280 _leave("");
281}
This page took 0.826422 seconds and 5 git commands to generate.