peer_event.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. struct sockaddr_rxrpc *srx)
  30. {
  31. struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
  32. _enter("");
  33. memset(srx, 0, sizeof(*srx));
  34. srx->transport_type = local->srx.transport_type;
  35. srx->transport_len = local->srx.transport_len;
  36. srx->transport.family = local->srx.transport.family;
  37. /* Can we see an ICMP4 packet on an ICMP6 listening socket? and vice
  38. * versa?
  39. */
  40. switch (srx->transport.family) {
  41. case AF_INET:
  42. srx->transport.sin.sin_port = serr->port;
  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. switch (serr->ee.ee_origin) {
  66. case SO_EE_ORIGIN_ICMP6:
  67. _net("Rx ICMP6");
  68. memcpy(&srx->transport.sin6.sin6_addr,
  69. skb_network_header(skb) + serr->addr_offset,
  70. sizeof(struct in6_addr));
  71. break;
  72. case SO_EE_ORIGIN_ICMP:
  73. _net("Rx ICMP on v6 sock");
  74. srx->transport.sin6.sin6_addr.s6_addr32[0] = 0;
  75. srx->transport.sin6.sin6_addr.s6_addr32[1] = 0;
  76. srx->transport.sin6.sin6_addr.s6_addr32[2] = htonl(0xffff);
  77. memcpy(srx->transport.sin6.sin6_addr.s6_addr + 12,
  78. skb_network_header(skb) + serr->addr_offset,
  79. sizeof(struct in_addr));
  80. break;
  81. default:
  82. memcpy(&srx->transport.sin6.sin6_addr,
  83. &ipv6_hdr(skb)->saddr,
  84. sizeof(struct in6_addr));
  85. break;
  86. }
  87. break;
  88. #endif
  89. default:
  90. BUG();
  91. }
  92. return rxrpc_lookup_peer_rcu(local, srx);
  93. }
  94. /*
  95. * Handle an MTU/fragmentation problem.
  96. */
  97. static void rxrpc_adjust_mtu(struct rxrpc_peer *peer, struct sock_exterr_skb *serr)
  98. {
  99. u32 mtu = serr->ee.ee_info;
  100. _net("Rx ICMP Fragmentation Needed (%d)", mtu);
  101. /* wind down the local interface MTU */
  102. if (mtu > 0 && peer->if_mtu == 65535 && mtu < peer->if_mtu) {
  103. peer->if_mtu = mtu;
  104. _net("I/F MTU %u", mtu);
  105. }
  106. if (mtu == 0) {
  107. /* they didn't give us a size, estimate one */
  108. mtu = peer->if_mtu;
  109. if (mtu > 1500) {
  110. mtu >>= 1;
  111. if (mtu < 1500)
  112. mtu = 1500;
  113. } else {
  114. mtu -= 100;
  115. if (mtu < peer->hdrsize)
  116. mtu = peer->hdrsize + 4;
  117. }
  118. }
  119. if (mtu < peer->mtu) {
  120. spin_lock_bh(&peer->lock);
  121. peer->mtu = mtu;
  122. peer->maxdata = peer->mtu - peer->hdrsize;
  123. spin_unlock_bh(&peer->lock);
  124. _net("Net MTU %u (maxdata %u)",
  125. peer->mtu, peer->maxdata);
  126. }
  127. }
  128. /*
  129. * Handle an error received on the local endpoint.
  130. */
  131. void rxrpc_error_report(struct sock *sk)
  132. {
  133. struct sock_exterr_skb *serr;
  134. struct sockaddr_rxrpc srx;
  135. struct rxrpc_local *local = sk->sk_user_data;
  136. struct rxrpc_peer *peer;
  137. struct sk_buff *skb;
  138. _enter("%p{%d}", sk, local->debug_id);
  139. skb = sock_dequeue_err_skb(sk);
  140. if (!skb) {
  141. _leave("UDP socket errqueue empty");
  142. return;
  143. }
  144. rxrpc_new_skb(skb, rxrpc_skb_rx_received);
  145. serr = SKB_EXT_ERR(skb);
  146. if (!skb->len && serr->ee.ee_origin == SO_EE_ORIGIN_TIMESTAMPING) {
  147. _leave("UDP empty message");
  148. rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
  149. return;
  150. }
  151. rcu_read_lock();
  152. peer = rxrpc_lookup_peer_icmp_rcu(local, skb, &srx);
  153. if (peer && !rxrpc_get_peer_maybe(peer))
  154. peer = NULL;
  155. if (!peer) {
  156. rcu_read_unlock();
  157. rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
  158. _leave(" [no peer]");
  159. return;
  160. }
  161. trace_rxrpc_rx_icmp(peer, &serr->ee, &srx);
  162. if ((serr->ee.ee_origin == SO_EE_ORIGIN_ICMP &&
  163. serr->ee.ee_type == ICMP_DEST_UNREACH &&
  164. serr->ee.ee_code == ICMP_FRAG_NEEDED)) {
  165. rxrpc_adjust_mtu(peer, serr);
  166. rcu_read_unlock();
  167. rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
  168. rxrpc_put_peer(peer);
  169. _leave(" [MTU update]");
  170. return;
  171. }
  172. rxrpc_store_error(peer, serr);
  173. rcu_read_unlock();
  174. rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
  175. /* The ref we obtained is passed off to the work item */
  176. __rxrpc_queue_peer_error(peer);
  177. _leave("");
  178. }
  179. /*
  180. * Map an error report to error codes on the peer record.
  181. */
  182. static void rxrpc_store_error(struct rxrpc_peer *peer,
  183. struct sock_exterr_skb *serr)
  184. {
  185. struct sock_extended_err *ee;
  186. int err;
  187. _enter("");
  188. ee = &serr->ee;
  189. err = ee->ee_errno;
  190. switch (ee->ee_origin) {
  191. case SO_EE_ORIGIN_ICMP:
  192. switch (ee->ee_type) {
  193. case ICMP_DEST_UNREACH:
  194. switch (ee->ee_code) {
  195. case ICMP_NET_UNREACH:
  196. _net("Rx Received ICMP Network Unreachable");
  197. break;
  198. case ICMP_HOST_UNREACH:
  199. _net("Rx Received ICMP Host Unreachable");
  200. break;
  201. case ICMP_PORT_UNREACH:
  202. _net("Rx Received ICMP Port Unreachable");
  203. break;
  204. case ICMP_NET_UNKNOWN:
  205. _net("Rx Received ICMP Unknown Network");
  206. break;
  207. case ICMP_HOST_UNKNOWN:
  208. _net("Rx Received ICMP Unknown Host");
  209. break;
  210. default:
  211. _net("Rx Received ICMP DestUnreach code=%u",
  212. ee->ee_code);
  213. break;
  214. }
  215. break;
  216. case ICMP_TIME_EXCEEDED:
  217. _net("Rx Received ICMP TTL Exceeded");
  218. break;
  219. default:
  220. _proto("Rx Received ICMP error { type=%u code=%u }",
  221. ee->ee_type, ee->ee_code);
  222. break;
  223. }
  224. break;
  225. case SO_EE_ORIGIN_NONE:
  226. case SO_EE_ORIGIN_LOCAL:
  227. _proto("Rx Received local error { error=%d }", err);
  228. err += RXRPC_LOCAL_ERROR_OFFSET;
  229. break;
  230. case SO_EE_ORIGIN_ICMP6:
  231. default:
  232. _proto("Rx Received error report { orig=%u }", ee->ee_origin);
  233. break;
  234. }
  235. peer->error_report = err;
  236. }
  237. /*
  238. * Distribute an error that occurred on a peer
  239. */
  240. void rxrpc_peer_error_distributor(struct work_struct *work)
  241. {
  242. struct rxrpc_peer *peer =
  243. container_of(work, struct rxrpc_peer, error_distributor);
  244. struct rxrpc_call *call;
  245. enum rxrpc_call_completion compl;
  246. int error;
  247. _enter("");
  248. error = READ_ONCE(peer->error_report);
  249. if (error < RXRPC_LOCAL_ERROR_OFFSET) {
  250. compl = RXRPC_CALL_NETWORK_ERROR;
  251. } else {
  252. compl = RXRPC_CALL_LOCAL_ERROR;
  253. error -= RXRPC_LOCAL_ERROR_OFFSET;
  254. }
  255. _debug("ISSUE ERROR %s %d", rxrpc_call_completions[compl], error);
  256. spin_lock_bh(&peer->lock);
  257. while (!hlist_empty(&peer->error_targets)) {
  258. call = hlist_entry(peer->error_targets.first,
  259. struct rxrpc_call, error_link);
  260. hlist_del_init(&call->error_link);
  261. rxrpc_see_call(call);
  262. if (rxrpc_set_call_completion(call, compl, 0, -error))
  263. rxrpc_notify_socket(call);
  264. }
  265. spin_unlock_bh(&peer->lock);
  266. rxrpc_put_peer(peer);
  267. _leave("");
  268. }
  269. /*
  270. * Add RTT information to cache. This is called in softirq mode and has
  271. * exclusive access to the peer RTT data.
  272. */
  273. void rxrpc_peer_add_rtt(struct rxrpc_call *call, enum rxrpc_rtt_rx_trace why,
  274. rxrpc_serial_t send_serial, rxrpc_serial_t resp_serial,
  275. ktime_t send_time, ktime_t resp_time)
  276. {
  277. struct rxrpc_peer *peer = call->peer;
  278. s64 rtt;
  279. u64 sum = peer->rtt_sum, avg;
  280. u8 cursor = peer->rtt_cursor, usage = peer->rtt_usage;
  281. rtt = ktime_to_ns(ktime_sub(resp_time, send_time));
  282. if (rtt < 0)
  283. return;
  284. /* Replace the oldest datum in the RTT buffer */
  285. sum -= peer->rtt_cache[cursor];
  286. sum += rtt;
  287. peer->rtt_cache[cursor] = rtt;
  288. peer->rtt_cursor = (cursor + 1) & (RXRPC_RTT_CACHE_SIZE - 1);
  289. peer->rtt_sum = sum;
  290. if (usage < RXRPC_RTT_CACHE_SIZE) {
  291. usage++;
  292. peer->rtt_usage = usage;
  293. }
  294. /* Now recalculate the average */
  295. if (usage == RXRPC_RTT_CACHE_SIZE) {
  296. avg = sum / RXRPC_RTT_CACHE_SIZE;
  297. } else {
  298. avg = sum;
  299. do_div(avg, usage);
  300. }
  301. peer->rtt = avg;
  302. trace_rxrpc_rtt_rx(call, why, send_serial, resp_serial, rtt,
  303. usage, avg);
  304. }
  305. /*
  306. * Perform keep-alive pings.
  307. */
  308. static void rxrpc_peer_keepalive_dispatch(struct rxrpc_net *rxnet,
  309. struct list_head *collector,
  310. time64_t base,
  311. u8 cursor)
  312. {
  313. struct rxrpc_peer *peer;
  314. const u8 mask = ARRAY_SIZE(rxnet->peer_keepalive) - 1;
  315. time64_t keepalive_at;
  316. int slot;
  317. spin_lock_bh(&rxnet->peer_hash_lock);
  318. while (!list_empty(collector)) {
  319. peer = list_entry(collector->next,
  320. struct rxrpc_peer, keepalive_link);
  321. list_del_init(&peer->keepalive_link);
  322. if (!rxrpc_get_peer_maybe(peer))
  323. continue;
  324. spin_unlock_bh(&rxnet->peer_hash_lock);
  325. keepalive_at = peer->last_tx_at + RXRPC_KEEPALIVE_TIME;
  326. slot = keepalive_at - base;
  327. _debug("%02x peer %u t=%d {%pISp}",
  328. cursor, peer->debug_id, slot, &peer->srx.transport);
  329. if (keepalive_at <= base ||
  330. keepalive_at > base + RXRPC_KEEPALIVE_TIME) {
  331. rxrpc_send_keepalive(peer);
  332. slot = RXRPC_KEEPALIVE_TIME;
  333. }
  334. /* A transmission to this peer occurred since last we examined
  335. * it so put it into the appropriate future bucket.
  336. */
  337. slot += cursor;
  338. slot &= mask;
  339. spin_lock_bh(&rxnet->peer_hash_lock);
  340. list_add_tail(&peer->keepalive_link,
  341. &rxnet->peer_keepalive[slot & mask]);
  342. rxrpc_put_peer(peer);
  343. }
  344. spin_unlock_bh(&rxnet->peer_hash_lock);
  345. }
  346. /*
  347. * Perform keep-alive pings with VERSION packets to keep any NAT alive.
  348. */
  349. void rxrpc_peer_keepalive_worker(struct work_struct *work)
  350. {
  351. struct rxrpc_net *rxnet =
  352. container_of(work, struct rxrpc_net, peer_keepalive_work);
  353. const u8 mask = ARRAY_SIZE(rxnet->peer_keepalive) - 1;
  354. time64_t base, now, delay;
  355. u8 cursor, stop;
  356. LIST_HEAD(collector);
  357. now = ktime_get_seconds();
  358. base = rxnet->peer_keepalive_base;
  359. cursor = rxnet->peer_keepalive_cursor;
  360. _enter("%lld,%u", base - now, cursor);
  361. if (!rxnet->live)
  362. return;
  363. /* Remove to a temporary list all the peers that are currently lodged
  364. * in expired buckets plus all new peers.
  365. *
  366. * Everything in the bucket at the cursor is processed this
  367. * second; the bucket at cursor + 1 goes at now + 1s and so
  368. * on...
  369. */
  370. spin_lock_bh(&rxnet->peer_hash_lock);
  371. list_splice_init(&rxnet->peer_keepalive_new, &collector);
  372. stop = cursor + ARRAY_SIZE(rxnet->peer_keepalive);
  373. while (base <= now && (s8)(cursor - stop) < 0) {
  374. list_splice_tail_init(&rxnet->peer_keepalive[cursor & mask],
  375. &collector);
  376. base++;
  377. cursor++;
  378. }
  379. base = now;
  380. spin_unlock_bh(&rxnet->peer_hash_lock);
  381. rxnet->peer_keepalive_base = base;
  382. rxnet->peer_keepalive_cursor = cursor;
  383. rxrpc_peer_keepalive_dispatch(rxnet, &collector, base, cursor);
  384. ASSERT(list_empty(&collector));
  385. /* Schedule the timer for the next occupied timeslot. */
  386. cursor = rxnet->peer_keepalive_cursor;
  387. stop = cursor + RXRPC_KEEPALIVE_TIME - 1;
  388. for (; (s8)(cursor - stop) < 0; cursor++) {
  389. if (!list_empty(&rxnet->peer_keepalive[cursor & mask]))
  390. break;
  391. base++;
  392. }
  393. now = ktime_get_seconds();
  394. delay = base - now;
  395. if (delay < 1)
  396. delay = 1;
  397. delay *= HZ;
  398. if (rxnet->live)
  399. timer_reduce(&rxnet->peer_keepalive_timer, jiffies + delay);
  400. _leave("");
  401. }