ar-error.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. if (mtu == 0) {
  72. /* they didn't give us a size, estimate one */
  73. if (mtu > 1500) {
  74. mtu >>= 1;
  75. if (mtu < 1500)
  76. mtu = 1500;
  77. } else {
  78. mtu -= 100;
  79. if (mtu < peer->hdrsize)
  80. mtu = peer->hdrsize + 4;
  81. }
  82. }
  83. if (mtu < peer->mtu) {
  84. spin_lock_bh(&peer->lock);
  85. peer->mtu = mtu;
  86. peer->maxdata = peer->mtu - peer->hdrsize;
  87. spin_unlock_bh(&peer->lock);
  88. _net("Net MTU %u (maxdata %u)",
  89. peer->mtu, peer->maxdata);
  90. }
  91. }
  92. rxrpc_put_peer(peer);
  93. /* pass the transport ref to error_handler to release */
  94. skb_queue_tail(&trans->error_queue, skb);
  95. rxrpc_queue_work(&trans->error_handler);
  96. /* reset and regenerate socket error */
  97. spin_lock_bh(&sk->sk_error_queue.lock);
  98. sk->sk_err = 0;
  99. skb = skb_peek(&sk->sk_error_queue);
  100. if (skb) {
  101. sk->sk_err = SKB_EXT_ERR(skb)->ee.ee_errno;
  102. spin_unlock_bh(&sk->sk_error_queue.lock);
  103. sk->sk_error_report(sk);
  104. } else {
  105. spin_unlock_bh(&sk->sk_error_queue.lock);
  106. }
  107. _leave("");
  108. }
  109. /*
  110. * deal with UDP error messages
  111. */
  112. void rxrpc_UDP_error_handler(struct work_struct *work)
  113. {
  114. struct sock_extended_err *ee;
  115. struct sock_exterr_skb *serr;
  116. struct rxrpc_transport *trans =
  117. container_of(work, struct rxrpc_transport, error_handler);
  118. struct sk_buff *skb;
  119. int err;
  120. _enter("");
  121. skb = skb_dequeue(&trans->error_queue);
  122. if (!skb)
  123. return;
  124. serr = SKB_EXT_ERR(skb);
  125. ee = &serr->ee;
  126. _net("Rx Error o=%d t=%d c=%d e=%d",
  127. ee->ee_origin, ee->ee_type, ee->ee_code, ee->ee_errno);
  128. err = ee->ee_errno;
  129. switch (ee->ee_origin) {
  130. case SO_EE_ORIGIN_ICMP:
  131. switch (ee->ee_type) {
  132. case ICMP_DEST_UNREACH:
  133. switch (ee->ee_code) {
  134. case ICMP_NET_UNREACH:
  135. _net("Rx Received ICMP Network Unreachable");
  136. err = ENETUNREACH;
  137. break;
  138. case ICMP_HOST_UNREACH:
  139. _net("Rx Received ICMP Host Unreachable");
  140. err = EHOSTUNREACH;
  141. break;
  142. case ICMP_PORT_UNREACH:
  143. _net("Rx Received ICMP Port Unreachable");
  144. err = ECONNREFUSED;
  145. break;
  146. case ICMP_FRAG_NEEDED:
  147. _net("Rx Received ICMP Fragmentation Needed (%d)",
  148. ee->ee_info);
  149. err = 0; /* dealt with elsewhere */
  150. break;
  151. case ICMP_NET_UNKNOWN:
  152. _net("Rx Received ICMP Unknown Network");
  153. err = ENETUNREACH;
  154. break;
  155. case ICMP_HOST_UNKNOWN:
  156. _net("Rx Received ICMP Unknown Host");
  157. err = EHOSTUNREACH;
  158. break;
  159. default:
  160. _net("Rx Received ICMP DestUnreach code=%u",
  161. ee->ee_code);
  162. break;
  163. }
  164. break;
  165. case ICMP_TIME_EXCEEDED:
  166. _net("Rx Received ICMP TTL Exceeded");
  167. break;
  168. default:
  169. _proto("Rx Received ICMP error { type=%u code=%u }",
  170. ee->ee_type, ee->ee_code);
  171. break;
  172. }
  173. break;
  174. case SO_EE_ORIGIN_LOCAL:
  175. _proto("Rx Received local error { error=%d }",
  176. ee->ee_errno);
  177. break;
  178. case SO_EE_ORIGIN_NONE:
  179. case SO_EE_ORIGIN_ICMP6:
  180. default:
  181. _proto("Rx Received error report { orig=%u }",
  182. ee->ee_origin);
  183. break;
  184. }
  185. /* terminate all the affected calls if there's an unrecoverable
  186. * error */
  187. if (err) {
  188. struct rxrpc_call *call, *_n;
  189. _debug("ISSUE ERROR %d", err);
  190. spin_lock_bh(&trans->peer->lock);
  191. trans->peer->net_error = err;
  192. list_for_each_entry_safe(call, _n, &trans->peer->error_targets,
  193. error_link) {
  194. write_lock(&call->state_lock);
  195. if (call->state != RXRPC_CALL_COMPLETE &&
  196. call->state < RXRPC_CALL_NETWORK_ERROR) {
  197. call->state = RXRPC_CALL_NETWORK_ERROR;
  198. set_bit(RXRPC_CALL_RCVD_ERROR, &call->events);
  199. rxrpc_queue_call(call);
  200. }
  201. write_unlock(&call->state_lock);
  202. list_del_init(&call->error_link);
  203. }
  204. spin_unlock_bh(&trans->peer->lock);
  205. }
  206. if (!skb_queue_empty(&trans->error_queue))
  207. rxrpc_queue_work(&trans->error_handler);
  208. rxrpc_free_skb(skb);
  209. rxrpc_put_transport(trans);
  210. _leave("");
  211. }