ar-error.c 5.8 KB

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