peer_event.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /* Peer event handling, typically ICMP messages.
  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. #include <linux/module.h>
  12. #include <linux/net.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/errqueue.h>
  15. #include <linux/udp.h>
  16. #include <linux/in.h>
  17. #include <linux/in6.h>
  18. #include <linux/icmp.h>
  19. #include <net/sock.h>
  20. #include <net/af_rxrpc.h>
  21. #include <net/ip.h>
  22. #include "ar-internal.h"
  23. static void rxrpc_store_error(struct rxrpc_peer *, struct sock_exterr_skb *);
  24. /*
  25. * Find the peer associated with an ICMP packet.
  26. */
  27. static struct rxrpc_peer *rxrpc_lookup_peer_icmp_rcu(struct rxrpc_local *local,
  28. const struct sk_buff *skb)
  29. {
  30. struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
  31. struct sockaddr_rxrpc srx;
  32. _enter("");
  33. memset(&srx, 0, sizeof(srx));
  34. srx.transport_type = local->srx.transport_type;
  35. srx.transport.family = local->srx.transport.family;
  36. /* Can we see an ICMP4 packet on an ICMP6 listening socket? and vice
  37. * versa?
  38. */
  39. switch (srx.transport.family) {
  40. case AF_INET:
  41. srx.transport.sin.sin_port = serr->port;
  42. srx.transport_len = sizeof(struct sockaddr_in);
  43. switch (serr->ee.ee_origin) {
  44. case SO_EE_ORIGIN_ICMP:
  45. _net("Rx ICMP");
  46. memcpy(&srx.transport.sin.sin_addr,
  47. skb_network_header(skb) + serr->addr_offset,
  48. sizeof(struct in_addr));
  49. break;
  50. case SO_EE_ORIGIN_ICMP6:
  51. _net("Rx ICMP6 on v4 sock");
  52. memcpy(&srx.transport.sin.sin_addr,
  53. skb_network_header(skb) + serr->addr_offset + 12,
  54. sizeof(struct in_addr));
  55. break;
  56. default:
  57. memcpy(&srx.transport.sin.sin_addr, &ip_hdr(skb)->saddr,
  58. sizeof(struct in_addr));
  59. break;
  60. }
  61. break;
  62. #ifdef CONFIG_AF_RXRPC_IPV6
  63. case AF_INET6:
  64. srx.transport.sin6.sin6_port = serr->port;
  65. srx.transport_len = sizeof(struct sockaddr_in6);
  66. switch (serr->ee.ee_origin) {
  67. case SO_EE_ORIGIN_ICMP6:
  68. _net("Rx ICMP6");
  69. memcpy(&srx.transport.sin6.sin6_addr,
  70. skb_network_header(skb) + serr->addr_offset,
  71. sizeof(struct in6_addr));
  72. break;
  73. case SO_EE_ORIGIN_ICMP:
  74. _net("Rx ICMP on v6 sock");
  75. memcpy(srx.transport.sin6.sin6_addr.s6_addr + 12,
  76. skb_network_header(skb) + serr->addr_offset,
  77. sizeof(struct in_addr));
  78. break;
  79. default:
  80. memcpy(&srx.transport.sin6.sin6_addr,
  81. &ipv6_hdr(skb)->saddr,
  82. sizeof(struct in6_addr));
  83. break;
  84. }
  85. break;
  86. #endif
  87. default:
  88. BUG();
  89. }
  90. return rxrpc_lookup_peer_rcu(local, &srx);
  91. }
  92. /*
  93. * Handle an MTU/fragmentation problem.
  94. */
  95. static void rxrpc_adjust_mtu(struct rxrpc_peer *peer, struct sock_exterr_skb *serr)
  96. {
  97. u32 mtu = serr->ee.ee_info;
  98. _net("Rx ICMP Fragmentation Needed (%d)", mtu);
  99. /* wind down the local interface MTU */
  100. if (mtu > 0 && peer->if_mtu == 65535 && mtu < peer->if_mtu) {
  101. peer->if_mtu = mtu;
  102. _net("I/F MTU %u", mtu);
  103. }
  104. if (mtu == 0) {
  105. /* they didn't give us a size, estimate one */
  106. mtu = peer->if_mtu;
  107. if (mtu > 1500) {
  108. mtu >>= 1;
  109. if (mtu < 1500)
  110. mtu = 1500;
  111. } else {
  112. mtu -= 100;
  113. if (mtu < peer->hdrsize)
  114. mtu = peer->hdrsize + 4;
  115. }
  116. }
  117. if (mtu < peer->mtu) {
  118. spin_lock_bh(&peer->lock);
  119. peer->mtu = mtu;
  120. peer->maxdata = peer->mtu - peer->hdrsize;
  121. spin_unlock_bh(&peer->lock);
  122. _net("Net MTU %u (maxdata %u)",
  123. peer->mtu, peer->maxdata);
  124. }
  125. }
  126. /*
  127. * Handle an error received on the local endpoint.
  128. */
  129. void rxrpc_error_report(struct sock *sk)
  130. {
  131. struct sock_exterr_skb *serr;
  132. struct rxrpc_local *local = sk->sk_user_data;
  133. struct rxrpc_peer *peer;
  134. struct sk_buff *skb;
  135. _enter("%p{%d}", sk, local->debug_id);
  136. skb = sock_dequeue_err_skb(sk);
  137. if (!skb) {
  138. _leave("UDP socket errqueue empty");
  139. return;
  140. }
  141. rxrpc_new_skb(skb, rxrpc_skb_rx_received);
  142. serr = SKB_EXT_ERR(skb);
  143. if (!skb->len && serr->ee.ee_origin == SO_EE_ORIGIN_TIMESTAMPING) {
  144. _leave("UDP empty message");
  145. rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
  146. return;
  147. }
  148. rcu_read_lock();
  149. peer = rxrpc_lookup_peer_icmp_rcu(local, skb);
  150. if (peer && !rxrpc_get_peer_maybe(peer))
  151. peer = NULL;
  152. if (!peer) {
  153. rcu_read_unlock();
  154. rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
  155. _leave(" [no peer]");
  156. return;
  157. }
  158. if ((serr->ee.ee_origin == SO_EE_ORIGIN_ICMP &&
  159. serr->ee.ee_type == ICMP_DEST_UNREACH &&
  160. serr->ee.ee_code == ICMP_FRAG_NEEDED)) {
  161. rxrpc_adjust_mtu(peer, serr);
  162. rcu_read_unlock();
  163. rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
  164. rxrpc_put_peer(peer);
  165. _leave(" [MTU update]");
  166. return;
  167. }
  168. rxrpc_store_error(peer, serr);
  169. rcu_read_unlock();
  170. rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
  171. /* The ref we obtained is passed off to the work item */
  172. rxrpc_queue_work(&peer->error_distributor);
  173. _leave("");
  174. }
  175. /*
  176. * Map an error report to error codes on the peer record.
  177. */
  178. static void rxrpc_store_error(struct rxrpc_peer *peer,
  179. struct sock_exterr_skb *serr)
  180. {
  181. struct sock_extended_err *ee;
  182. int err;
  183. _enter("");
  184. ee = &serr->ee;
  185. _net("Rx Error o=%d t=%d c=%d e=%d",
  186. ee->ee_origin, ee->ee_type, ee->ee_code, ee->ee_errno);
  187. err = ee->ee_errno;
  188. switch (ee->ee_origin) {
  189. case SO_EE_ORIGIN_ICMP:
  190. switch (ee->ee_type) {
  191. case ICMP_DEST_UNREACH:
  192. switch (ee->ee_code) {
  193. case ICMP_NET_UNREACH:
  194. _net("Rx Received ICMP Network Unreachable");
  195. break;
  196. case ICMP_HOST_UNREACH:
  197. _net("Rx Received ICMP Host Unreachable");
  198. break;
  199. case ICMP_PORT_UNREACH:
  200. _net("Rx Received ICMP Port Unreachable");
  201. break;
  202. case ICMP_NET_UNKNOWN:
  203. _net("Rx Received ICMP Unknown Network");
  204. break;
  205. case ICMP_HOST_UNKNOWN:
  206. _net("Rx Received ICMP Unknown Host");
  207. break;
  208. default:
  209. _net("Rx Received ICMP DestUnreach code=%u",
  210. ee->ee_code);
  211. break;
  212. }
  213. break;
  214. case ICMP_TIME_EXCEEDED:
  215. _net("Rx Received ICMP TTL Exceeded");
  216. break;
  217. default:
  218. _proto("Rx Received ICMP error { type=%u code=%u }",
  219. ee->ee_type, ee->ee_code);
  220. break;
  221. }
  222. break;
  223. case SO_EE_ORIGIN_NONE:
  224. case SO_EE_ORIGIN_LOCAL:
  225. _proto("Rx Received local error { error=%d }", err);
  226. err += RXRPC_LOCAL_ERROR_OFFSET;
  227. break;
  228. case SO_EE_ORIGIN_ICMP6:
  229. default:
  230. _proto("Rx Received error report { orig=%u }", ee->ee_origin);
  231. break;
  232. }
  233. peer->error_report = err;
  234. }
  235. /*
  236. * Distribute an error that occurred on a peer
  237. */
  238. void rxrpc_peer_error_distributor(struct work_struct *work)
  239. {
  240. struct rxrpc_peer *peer =
  241. container_of(work, struct rxrpc_peer, error_distributor);
  242. struct rxrpc_call *call;
  243. enum rxrpc_call_completion compl;
  244. int error;
  245. _enter("");
  246. error = READ_ONCE(peer->error_report);
  247. if (error < RXRPC_LOCAL_ERROR_OFFSET) {
  248. compl = RXRPC_CALL_NETWORK_ERROR;
  249. } else {
  250. compl = RXRPC_CALL_LOCAL_ERROR;
  251. error -= RXRPC_LOCAL_ERROR_OFFSET;
  252. }
  253. _debug("ISSUE ERROR %s %d", rxrpc_call_completions[compl], error);
  254. spin_lock_bh(&peer->lock);
  255. while (!hlist_empty(&peer->error_targets)) {
  256. call = hlist_entry(peer->error_targets.first,
  257. struct rxrpc_call, error_link);
  258. hlist_del_init(&call->error_link);
  259. rxrpc_see_call(call);
  260. if (rxrpc_set_call_completion(call, compl, 0, error))
  261. rxrpc_notify_socket(call);
  262. }
  263. spin_unlock_bh(&peer->lock);
  264. rxrpc_put_peer(peer);
  265. _leave("");
  266. }
  267. /*
  268. * Add RTT information to cache. This is called in softirq mode and has
  269. * exclusive access to the peer RTT data.
  270. */
  271. void rxrpc_peer_add_rtt(struct rxrpc_call *call, enum rxrpc_rtt_rx_trace why,
  272. rxrpc_serial_t send_serial, rxrpc_serial_t resp_serial,
  273. ktime_t send_time, ktime_t resp_time)
  274. {
  275. struct rxrpc_peer *peer = call->peer;
  276. s64 rtt;
  277. u64 sum = peer->rtt_sum, avg;
  278. u8 cursor = peer->rtt_cursor, usage = peer->rtt_usage;
  279. rtt = ktime_to_ns(ktime_sub(resp_time, send_time));
  280. if (rtt < 0)
  281. return;
  282. /* Replace the oldest datum in the RTT buffer */
  283. sum -= peer->rtt_cache[cursor];
  284. sum += rtt;
  285. peer->rtt_cache[cursor] = rtt;
  286. peer->rtt_cursor = (cursor + 1) & (RXRPC_RTT_CACHE_SIZE - 1);
  287. peer->rtt_sum = sum;
  288. if (usage < RXRPC_RTT_CACHE_SIZE) {
  289. usage++;
  290. peer->rtt_usage = usage;
  291. }
  292. /* Now recalculate the average */
  293. if (usage == RXRPC_RTT_CACHE_SIZE) {
  294. avg = sum / RXRPC_RTT_CACHE_SIZE;
  295. } else {
  296. avg = sum;
  297. do_div(avg, usage);
  298. }
  299. peer->rtt = avg;
  300. trace_rxrpc_rtt_rx(call, why, send_serial, resp_serial, rtt,
  301. usage, avg);
  302. }