David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 1 | /* Peer event handling, typically ICMP messages. |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 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 | |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 25 | static void rxrpc_store_error(struct rxrpc_peer *, struct sock_exterr_skb *); |
| 26 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 27 | /* |
David Howells | be6e670 | 2016-04-04 14:00:32 +0100 | [diff] [blame] | 28 | * Find the peer associated with an ICMP packet. |
| 29 | */ |
| 30 | static 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 | |
David Howells | 75b54cb | 2016-09-13 08:49:05 +0100 | [diff] [blame] | 69 | case AF_INET6: |
| 70 | srx.transport.sin6.sin6_port = serr->port; |
| 71 | srx.transport_len = sizeof(struct sockaddr_in6); |
| 72 | switch (serr->ee.ee_origin) { |
| 73 | case SO_EE_ORIGIN_ICMP6: |
| 74 | _net("Rx ICMP6"); |
| 75 | memcpy(&srx.transport.sin6.sin6_addr, |
| 76 | skb_network_header(skb) + serr->addr_offset, |
| 77 | sizeof(struct in6_addr)); |
| 78 | break; |
| 79 | case SO_EE_ORIGIN_ICMP: |
| 80 | _net("Rx ICMP on v6 sock"); |
| 81 | memcpy(&srx.transport.sin6.sin6_addr.s6_addr + 12, |
| 82 | skb_network_header(skb) + serr->addr_offset, |
| 83 | sizeof(struct in_addr)); |
| 84 | break; |
| 85 | default: |
| 86 | memcpy(&srx.transport.sin6.sin6_addr, |
| 87 | &ipv6_hdr(skb)->saddr, |
| 88 | sizeof(struct in6_addr)); |
| 89 | break; |
| 90 | } |
| 91 | break; |
| 92 | |
David Howells | be6e670 | 2016-04-04 14:00:32 +0100 | [diff] [blame] | 93 | default: |
| 94 | BUG(); |
| 95 | } |
| 96 | |
| 97 | return rxrpc_lookup_peer_rcu(local, &srx); |
| 98 | } |
| 99 | |
| 100 | /* |
David Howells | 1a70c05 | 2016-04-04 14:00:33 +0100 | [diff] [blame] | 101 | * Handle an MTU/fragmentation problem. |
| 102 | */ |
| 103 | static void rxrpc_adjust_mtu(struct rxrpc_peer *peer, struct sock_exterr_skb *serr) |
| 104 | { |
| 105 | u32 mtu = serr->ee.ee_info; |
| 106 | |
| 107 | _net("Rx ICMP Fragmentation Needed (%d)", mtu); |
| 108 | |
| 109 | /* wind down the local interface MTU */ |
| 110 | if (mtu > 0 && peer->if_mtu == 65535 && mtu < peer->if_mtu) { |
| 111 | peer->if_mtu = mtu; |
| 112 | _net("I/F MTU %u", mtu); |
| 113 | } |
| 114 | |
| 115 | if (mtu == 0) { |
| 116 | /* they didn't give us a size, estimate one */ |
| 117 | mtu = peer->if_mtu; |
| 118 | if (mtu > 1500) { |
| 119 | mtu >>= 1; |
| 120 | if (mtu < 1500) |
| 121 | mtu = 1500; |
| 122 | } else { |
| 123 | mtu -= 100; |
| 124 | if (mtu < peer->hdrsize) |
| 125 | mtu = peer->hdrsize + 4; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | if (mtu < peer->mtu) { |
| 130 | spin_lock_bh(&peer->lock); |
| 131 | peer->mtu = mtu; |
| 132 | peer->maxdata = peer->mtu - peer->hdrsize; |
| 133 | spin_unlock_bh(&peer->lock); |
| 134 | _net("Net MTU %u (maxdata %u)", |
| 135 | peer->mtu, peer->maxdata); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /* |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 140 | * Handle an error received on the local endpoint. |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 141 | */ |
David Howells | abe89ef | 2016-04-04 14:00:32 +0100 | [diff] [blame] | 142 | void rxrpc_error_report(struct sock *sk) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 143 | { |
| 144 | struct sock_exterr_skb *serr; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 145 | struct rxrpc_local *local = sk->sk_user_data; |
| 146 | struct rxrpc_peer *peer; |
| 147 | struct sk_buff *skb; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 148 | |
| 149 | _enter("%p{%d}", sk, local->debug_id); |
| 150 | |
Willem de Bruijn | 364a9e9 | 2014-08-31 21:30:27 -0400 | [diff] [blame] | 151 | skb = sock_dequeue_err_skb(sk); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 152 | if (!skb) { |
| 153 | _leave("UDP socket errqueue empty"); |
| 154 | return; |
| 155 | } |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 156 | rxrpc_new_skb(skb); |
Willem de Bruijn | c247f05 | 2015-03-07 20:33:22 -0500 | [diff] [blame] | 157 | serr = SKB_EXT_ERR(skb); |
| 158 | if (!skb->len && serr->ee.ee_origin == SO_EE_ORIGIN_TIMESTAMPING) { |
Willem de Bruijn | 49ca0d8 | 2015-01-30 13:29:31 -0500 | [diff] [blame] | 159 | _leave("UDP empty message"); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 160 | rxrpc_free_skb(skb); |
Willem de Bruijn | 49ca0d8 | 2015-01-30 13:29:31 -0500 | [diff] [blame] | 161 | return; |
| 162 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 163 | |
David Howells | be6e670 | 2016-04-04 14:00:32 +0100 | [diff] [blame] | 164 | rcu_read_lock(); |
| 165 | peer = rxrpc_lookup_peer_icmp_rcu(local, skb); |
| 166 | if (peer && !rxrpc_get_peer_maybe(peer)) |
| 167 | peer = NULL; |
| 168 | if (!peer) { |
| 169 | rcu_read_unlock(); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 170 | rxrpc_free_skb(skb); |
| 171 | _leave(" [no peer]"); |
| 172 | return; |
| 173 | } |
| 174 | |
David Howells | 1a70c05 | 2016-04-04 14:00:33 +0100 | [diff] [blame] | 175 | if ((serr->ee.ee_origin == SO_EE_ORIGIN_ICMP && |
| 176 | serr->ee.ee_type == ICMP_DEST_UNREACH && |
| 177 | serr->ee.ee_code == ICMP_FRAG_NEEDED)) { |
| 178 | rxrpc_adjust_mtu(peer, serr); |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 179 | rcu_read_unlock(); |
David Howells | 1a70c05 | 2016-04-04 14:00:33 +0100 | [diff] [blame] | 180 | rxrpc_free_skb(skb); |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 181 | rxrpc_put_peer(peer); |
| 182 | _leave(" [MTU update]"); |
| 183 | return; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 184 | } |
| 185 | |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 186 | rxrpc_store_error(peer, serr); |
David Howells | be6e670 | 2016-04-04 14:00:32 +0100 | [diff] [blame] | 187 | rcu_read_unlock(); |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 188 | rxrpc_free_skb(skb); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 189 | |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 190 | /* The ref we obtained is passed off to the work item */ |
| 191 | rxrpc_queue_work(&peer->error_distributor); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 192 | _leave(""); |
| 193 | } |
| 194 | |
| 195 | /* |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 196 | * Map an error report to error codes on the peer record. |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 197 | */ |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 198 | static void rxrpc_store_error(struct rxrpc_peer *peer, |
| 199 | struct sock_exterr_skb *serr) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 200 | { |
| 201 | struct sock_extended_err *ee; |
David S. Miller | c9d10c4 | 2011-05-19 18:37:11 -0400 | [diff] [blame] | 202 | int err; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 203 | |
| 204 | _enter(""); |
| 205 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 206 | ee = &serr->ee; |
| 207 | |
| 208 | _net("Rx Error o=%d t=%d c=%d e=%d", |
| 209 | ee->ee_origin, ee->ee_type, ee->ee_code, ee->ee_errno); |
| 210 | |
| 211 | err = ee->ee_errno; |
| 212 | |
| 213 | switch (ee->ee_origin) { |
| 214 | case SO_EE_ORIGIN_ICMP: |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 215 | switch (ee->ee_type) { |
| 216 | case ICMP_DEST_UNREACH: |
| 217 | switch (ee->ee_code) { |
| 218 | case ICMP_NET_UNREACH: |
| 219 | _net("Rx Received ICMP Network Unreachable"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 220 | break; |
| 221 | case ICMP_HOST_UNREACH: |
| 222 | _net("Rx Received ICMP Host Unreachable"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 223 | break; |
| 224 | case ICMP_PORT_UNREACH: |
| 225 | _net("Rx Received ICMP Port Unreachable"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 226 | break; |
| 227 | case ICMP_NET_UNKNOWN: |
| 228 | _net("Rx Received ICMP Unknown Network"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 229 | break; |
| 230 | case ICMP_HOST_UNKNOWN: |
| 231 | _net("Rx Received ICMP Unknown Host"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 232 | break; |
| 233 | default: |
| 234 | _net("Rx Received ICMP DestUnreach code=%u", |
| 235 | ee->ee_code); |
| 236 | break; |
| 237 | } |
| 238 | break; |
| 239 | |
| 240 | case ICMP_TIME_EXCEEDED: |
| 241 | _net("Rx Received ICMP TTL Exceeded"); |
| 242 | break; |
| 243 | |
| 244 | default: |
| 245 | _proto("Rx Received ICMP error { type=%u code=%u }", |
| 246 | ee->ee_type, ee->ee_code); |
| 247 | break; |
| 248 | } |
| 249 | break; |
| 250 | |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 251 | case SO_EE_ORIGIN_NONE: |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 252 | case SO_EE_ORIGIN_LOCAL: |
David Howells | fe77d5f | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 253 | _proto("Rx Received local error { error=%d }", err); |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 254 | err += RXRPC_LOCAL_ERROR_OFFSET; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 255 | break; |
| 256 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 257 | case SO_EE_ORIGIN_ICMP6: |
| 258 | default: |
David Howells | fe77d5f | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 259 | _proto("Rx Received error report { orig=%u }", ee->ee_origin); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 260 | break; |
| 261 | } |
| 262 | |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 263 | peer->error_report = err; |
| 264 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 265 | |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 266 | /* |
| 267 | * Distribute an error that occurred on a peer |
| 268 | */ |
| 269 | void rxrpc_peer_error_distributor(struct work_struct *work) |
| 270 | { |
| 271 | struct rxrpc_peer *peer = |
| 272 | container_of(work, struct rxrpc_peer, error_distributor); |
| 273 | struct rxrpc_call *call; |
David Howells | f5c17aa | 2016-08-30 09:49:28 +0100 | [diff] [blame] | 274 | enum rxrpc_call_completion compl; |
David Howells | f5c17aa | 2016-08-30 09:49:28 +0100 | [diff] [blame] | 275 | int error; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 276 | |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 277 | _enter(""); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 278 | |
David Howells | f5c17aa | 2016-08-30 09:49:28 +0100 | [diff] [blame] | 279 | error = READ_ONCE(peer->error_report); |
| 280 | if (error < RXRPC_LOCAL_ERROR_OFFSET) { |
| 281 | compl = RXRPC_CALL_NETWORK_ERROR; |
| 282 | } else { |
| 283 | compl = RXRPC_CALL_LOCAL_ERROR; |
| 284 | error -= RXRPC_LOCAL_ERROR_OFFSET; |
| 285 | } |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 286 | |
David Howells | f5c17aa | 2016-08-30 09:49:28 +0100 | [diff] [blame] | 287 | _debug("ISSUE ERROR %s %d", rxrpc_call_completions[compl], error); |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 288 | |
| 289 | spin_lock_bh(&peer->lock); |
| 290 | |
| 291 | while (!hlist_empty(&peer->error_targets)) { |
| 292 | call = hlist_entry(peer->error_targets.first, |
| 293 | struct rxrpc_call, error_link); |
| 294 | hlist_del_init(&call->error_link); |
David Howells | e34d423 | 2016-08-30 09:49:29 +0100 | [diff] [blame] | 295 | rxrpc_see_call(call); |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 296 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 297 | if (rxrpc_set_call_completion(call, compl, 0, error)) |
| 298 | rxrpc_notify_socket(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 299 | } |
| 300 | |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 301 | spin_unlock_bh(&peer->lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 302 | |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 303 | rxrpc_put_peer(peer); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 304 | _leave(""); |
| 305 | } |