transport.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. /* transport.c: Rx Transport routines
  2. *
  3. * Copyright (C) 2002 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/sched.h>
  12. #include <linux/slab.h>
  13. #include <linux/module.h>
  14. #include <rxrpc/transport.h>
  15. #include <rxrpc/peer.h>
  16. #include <rxrpc/connection.h>
  17. #include <rxrpc/call.h>
  18. #include <rxrpc/message.h>
  19. #include <rxrpc/krxiod.h>
  20. #include <rxrpc/krxsecd.h>
  21. #include <linux/udp.h>
  22. #include <linux/in.h>
  23. #include <linux/in6.h>
  24. #include <linux/icmp.h>
  25. #include <linux/skbuff.h>
  26. #include <net/sock.h>
  27. #include <net/ip.h>
  28. #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
  29. #include <linux/ipv6.h> /* this should _really_ be in errqueue.h.. */
  30. #endif
  31. #include <linux/errqueue.h>
  32. #include <asm/uaccess.h>
  33. #include <asm/checksum.h>
  34. #include "internal.h"
  35. struct errormsg {
  36. struct cmsghdr cmsg; /* control message header */
  37. struct sock_extended_err ee; /* extended error information */
  38. struct sockaddr_in icmp_src; /* ICMP packet source address */
  39. };
  40. static DEFINE_SPINLOCK(rxrpc_transports_lock);
  41. static struct list_head rxrpc_transports = LIST_HEAD_INIT(rxrpc_transports);
  42. __RXACCT_DECL(atomic_t rxrpc_transport_count);
  43. LIST_HEAD(rxrpc_proc_transports);
  44. DECLARE_RWSEM(rxrpc_proc_transports_sem);
  45. static void rxrpc_data_ready(struct sock *sk, int count);
  46. static void rxrpc_error_report(struct sock *sk);
  47. static int rxrpc_trans_receive_new_call(struct rxrpc_transport *trans,
  48. struct list_head *msgq);
  49. static void rxrpc_trans_receive_error_report(struct rxrpc_transport *trans);
  50. /*****************************************************************************/
  51. /*
  52. * create a new transport endpoint using the specified UDP port
  53. */
  54. int rxrpc_create_transport(unsigned short port,
  55. struct rxrpc_transport **_trans)
  56. {
  57. struct rxrpc_transport *trans;
  58. struct sockaddr_in sin;
  59. mm_segment_t oldfs;
  60. struct sock *sock;
  61. int ret, opt;
  62. _enter("%hu", port);
  63. trans = kzalloc(sizeof(struct rxrpc_transport), GFP_KERNEL);
  64. if (!trans)
  65. return -ENOMEM;
  66. atomic_set(&trans->usage, 1);
  67. INIT_LIST_HEAD(&trans->services);
  68. INIT_LIST_HEAD(&trans->link);
  69. INIT_LIST_HEAD(&trans->krxiodq_link);
  70. spin_lock_init(&trans->lock);
  71. INIT_LIST_HEAD(&trans->peer_active);
  72. INIT_LIST_HEAD(&trans->peer_graveyard);
  73. spin_lock_init(&trans->peer_gylock);
  74. init_waitqueue_head(&trans->peer_gy_waitq);
  75. rwlock_init(&trans->peer_lock);
  76. atomic_set(&trans->peer_count, 0);
  77. trans->port = port;
  78. /* create a UDP socket to be my actual transport endpoint */
  79. ret = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &trans->socket);
  80. if (ret < 0)
  81. goto error;
  82. /* use the specified port */
  83. if (port) {
  84. memset(&sin, 0, sizeof(sin));
  85. sin.sin_family = AF_INET;
  86. sin.sin_port = htons(port);
  87. ret = trans->socket->ops->bind(trans->socket,
  88. (struct sockaddr *) &sin,
  89. sizeof(sin));
  90. if (ret < 0)
  91. goto error;
  92. }
  93. opt = 1;
  94. oldfs = get_fs();
  95. set_fs(KERNEL_DS);
  96. ret = trans->socket->ops->setsockopt(trans->socket, SOL_IP, IP_RECVERR,
  97. (char *) &opt, sizeof(opt));
  98. set_fs(oldfs);
  99. spin_lock(&rxrpc_transports_lock);
  100. list_add(&trans->link, &rxrpc_transports);
  101. spin_unlock(&rxrpc_transports_lock);
  102. /* set the socket up */
  103. sock = trans->socket->sk;
  104. sock->sk_user_data = trans;
  105. sock->sk_data_ready = rxrpc_data_ready;
  106. sock->sk_error_report = rxrpc_error_report;
  107. down_write(&rxrpc_proc_transports_sem);
  108. list_add_tail(&trans->proc_link, &rxrpc_proc_transports);
  109. up_write(&rxrpc_proc_transports_sem);
  110. __RXACCT(atomic_inc(&rxrpc_transport_count));
  111. *_trans = trans;
  112. _leave(" = 0 (%p)", trans);
  113. return 0;
  114. error:
  115. /* finish cleaning up the transport (not really needed here, but...) */
  116. if (trans->socket)
  117. trans->socket->ops->shutdown(trans->socket, 2);
  118. /* close the socket */
  119. if (trans->socket) {
  120. trans->socket->sk->sk_user_data = NULL;
  121. sock_release(trans->socket);
  122. trans->socket = NULL;
  123. }
  124. kfree(trans);
  125. _leave(" = %d", ret);
  126. return ret;
  127. } /* end rxrpc_create_transport() */
  128. /*****************************************************************************/
  129. /*
  130. * destroy a transport endpoint
  131. */
  132. void rxrpc_put_transport(struct rxrpc_transport *trans)
  133. {
  134. _enter("%p{u=%d p=%hu}",
  135. trans, atomic_read(&trans->usage), trans->port);
  136. BUG_ON(atomic_read(&trans->usage) <= 0);
  137. /* to prevent a race, the decrement and the dequeue must be
  138. * effectively atomic */
  139. spin_lock(&rxrpc_transports_lock);
  140. if (likely(!atomic_dec_and_test(&trans->usage))) {
  141. spin_unlock(&rxrpc_transports_lock);
  142. _leave("");
  143. return;
  144. }
  145. list_del(&trans->link);
  146. spin_unlock(&rxrpc_transports_lock);
  147. /* finish cleaning up the transport */
  148. if (trans->socket)
  149. trans->socket->ops->shutdown(trans->socket, 2);
  150. rxrpc_krxsecd_clear_transport(trans);
  151. rxrpc_krxiod_dequeue_transport(trans);
  152. /* discard all peer information */
  153. rxrpc_peer_clearall(trans);
  154. down_write(&rxrpc_proc_transports_sem);
  155. list_del(&trans->proc_link);
  156. up_write(&rxrpc_proc_transports_sem);
  157. __RXACCT(atomic_dec(&rxrpc_transport_count));
  158. /* close the socket */
  159. if (trans->socket) {
  160. trans->socket->sk->sk_user_data = NULL;
  161. sock_release(trans->socket);
  162. trans->socket = NULL;
  163. }
  164. kfree(trans);
  165. _leave("");
  166. } /* end rxrpc_put_transport() */
  167. /*****************************************************************************/
  168. /*
  169. * add a service to a transport to be listened upon
  170. */
  171. int rxrpc_add_service(struct rxrpc_transport *trans,
  172. struct rxrpc_service *newsrv)
  173. {
  174. struct rxrpc_service *srv;
  175. struct list_head *_p;
  176. int ret = -EEXIST;
  177. _enter("%p{%hu},%p{%hu}",
  178. trans, trans->port, newsrv, newsrv->service_id);
  179. /* verify that the service ID is not already present */
  180. spin_lock(&trans->lock);
  181. list_for_each(_p, &trans->services) {
  182. srv = list_entry(_p, struct rxrpc_service, link);
  183. if (srv->service_id == newsrv->service_id)
  184. goto out;
  185. }
  186. /* okay - add the transport to the list */
  187. list_add_tail(&newsrv->link, &trans->services);
  188. rxrpc_get_transport(trans);
  189. ret = 0;
  190. out:
  191. spin_unlock(&trans->lock);
  192. _leave("= %d", ret);
  193. return ret;
  194. } /* end rxrpc_add_service() */
  195. /*****************************************************************************/
  196. /*
  197. * remove a service from a transport
  198. */
  199. void rxrpc_del_service(struct rxrpc_transport *trans, struct rxrpc_service *srv)
  200. {
  201. _enter("%p{%hu},%p{%hu}", trans, trans->port, srv, srv->service_id);
  202. spin_lock(&trans->lock);
  203. list_del(&srv->link);
  204. spin_unlock(&trans->lock);
  205. rxrpc_put_transport(trans);
  206. _leave("");
  207. } /* end rxrpc_del_service() */
  208. /*****************************************************************************/
  209. /*
  210. * INET callback when data has been received on the socket.
  211. */
  212. static void rxrpc_data_ready(struct sock *sk, int count)
  213. {
  214. struct rxrpc_transport *trans;
  215. _enter("%p{t=%p},%d", sk, sk->sk_user_data, count);
  216. /* queue the transport for attention by krxiod */
  217. trans = (struct rxrpc_transport *) sk->sk_user_data;
  218. if (trans)
  219. rxrpc_krxiod_queue_transport(trans);
  220. /* wake up anyone waiting on the socket */
  221. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  222. wake_up_interruptible(sk->sk_sleep);
  223. _leave("");
  224. } /* end rxrpc_data_ready() */
  225. /*****************************************************************************/
  226. /*
  227. * INET callback when an ICMP error packet is received
  228. * - sk->err is error (EHOSTUNREACH, EPROTO or EMSGSIZE)
  229. */
  230. static void rxrpc_error_report(struct sock *sk)
  231. {
  232. struct rxrpc_transport *trans;
  233. _enter("%p{t=%p}", sk, sk->sk_user_data);
  234. /* queue the transport for attention by krxiod */
  235. trans = (struct rxrpc_transport *) sk->sk_user_data;
  236. if (trans) {
  237. trans->error_rcvd = 1;
  238. rxrpc_krxiod_queue_transport(trans);
  239. }
  240. /* wake up anyone waiting on the socket */
  241. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  242. wake_up_interruptible(sk->sk_sleep);
  243. _leave("");
  244. } /* end rxrpc_error_report() */
  245. /*****************************************************************************/
  246. /*
  247. * split a message up, allocating message records and filling them in
  248. * from the contents of a socket buffer
  249. */
  250. static int rxrpc_incoming_msg(struct rxrpc_transport *trans,
  251. struct sk_buff *pkt,
  252. struct list_head *msgq)
  253. {
  254. struct rxrpc_message *msg;
  255. int ret;
  256. _enter("");
  257. msg = kzalloc(sizeof(struct rxrpc_message), GFP_KERNEL);
  258. if (!msg) {
  259. _leave(" = -ENOMEM");
  260. return -ENOMEM;
  261. }
  262. atomic_set(&msg->usage, 1);
  263. list_add_tail(&msg->link,msgq);
  264. /* dig out the Rx routing parameters */
  265. if (skb_copy_bits(pkt, sizeof(struct udphdr),
  266. &msg->hdr, sizeof(msg->hdr)) < 0) {
  267. ret = -EBADMSG;
  268. goto error;
  269. }
  270. msg->trans = trans;
  271. msg->state = RXRPC_MSG_RECEIVED;
  272. skb_get_timestamp(pkt, &msg->stamp);
  273. if (msg->stamp.tv_sec == 0) {
  274. do_gettimeofday(&msg->stamp);
  275. if (pkt->sk)
  276. sock_enable_timestamp(pkt->sk);
  277. }
  278. msg->seq = ntohl(msg->hdr.seq);
  279. /* attach the packet */
  280. skb_get(pkt);
  281. msg->pkt = pkt;
  282. msg->offset = sizeof(struct udphdr) + sizeof(struct rxrpc_header);
  283. msg->dsize = msg->pkt->len - msg->offset;
  284. _net("Rx Received packet from %s (%08x;%08x,%1x,%d,%s,%02x,%d,%d)",
  285. msg->hdr.flags & RXRPC_CLIENT_INITIATED ? "client" : "server",
  286. ntohl(msg->hdr.epoch),
  287. (ntohl(msg->hdr.cid) & RXRPC_CIDMASK) >> RXRPC_CIDSHIFT,
  288. ntohl(msg->hdr.cid) & RXRPC_CHANNELMASK,
  289. ntohl(msg->hdr.callNumber),
  290. rxrpc_pkts[msg->hdr.type],
  291. msg->hdr.flags,
  292. ntohs(msg->hdr.serviceId),
  293. msg->hdr.securityIndex);
  294. __RXACCT(atomic_inc(&rxrpc_message_count));
  295. /* split off jumbo packets */
  296. while (msg->hdr.type == RXRPC_PACKET_TYPE_DATA &&
  297. msg->hdr.flags & RXRPC_JUMBO_PACKET
  298. ) {
  299. struct rxrpc_jumbo_header jumbo;
  300. struct rxrpc_message *jumbomsg = msg;
  301. _debug("split jumbo packet");
  302. /* quick sanity check */
  303. ret = -EBADMSG;
  304. if (msg->dsize <
  305. RXRPC_JUMBO_DATALEN + sizeof(struct rxrpc_jumbo_header))
  306. goto error;
  307. if (msg->hdr.flags & RXRPC_LAST_PACKET)
  308. goto error;
  309. /* dig out the secondary header */
  310. if (skb_copy_bits(pkt, msg->offset + RXRPC_JUMBO_DATALEN,
  311. &jumbo, sizeof(jumbo)) < 0)
  312. goto error;
  313. /* allocate a new message record */
  314. ret = -ENOMEM;
  315. msg = kmalloc(sizeof(struct rxrpc_message), GFP_KERNEL);
  316. if (!msg)
  317. goto error;
  318. memcpy(msg, jumbomsg, sizeof(*msg));
  319. list_add_tail(&msg->link, msgq);
  320. /* adjust the jumbo packet */
  321. jumbomsg->dsize = RXRPC_JUMBO_DATALEN;
  322. /* attach the packet here too */
  323. skb_get(pkt);
  324. /* adjust the parameters */
  325. msg->seq++;
  326. msg->hdr.seq = htonl(msg->seq);
  327. msg->hdr.serial = htonl(ntohl(msg->hdr.serial) + 1);
  328. msg->offset += RXRPC_JUMBO_DATALEN +
  329. sizeof(struct rxrpc_jumbo_header);
  330. msg->dsize -= RXRPC_JUMBO_DATALEN +
  331. sizeof(struct rxrpc_jumbo_header);
  332. msg->hdr.flags = jumbo.flags;
  333. msg->hdr._rsvd = jumbo._rsvd;
  334. _net("Rx Split jumbo packet from %s"
  335. " (%08x;%08x,%1x,%d,%s,%02x,%d,%d)",
  336. msg->hdr.flags & RXRPC_CLIENT_INITIATED ? "client" : "server",
  337. ntohl(msg->hdr.epoch),
  338. (ntohl(msg->hdr.cid) & RXRPC_CIDMASK) >> RXRPC_CIDSHIFT,
  339. ntohl(msg->hdr.cid) & RXRPC_CHANNELMASK,
  340. ntohl(msg->hdr.callNumber),
  341. rxrpc_pkts[msg->hdr.type],
  342. msg->hdr.flags,
  343. ntohs(msg->hdr.serviceId),
  344. msg->hdr.securityIndex);
  345. __RXACCT(atomic_inc(&rxrpc_message_count));
  346. }
  347. _leave(" = 0 #%d", atomic_read(&rxrpc_message_count));
  348. return 0;
  349. error:
  350. while (!list_empty(msgq)) {
  351. msg = list_entry(msgq->next, struct rxrpc_message, link);
  352. list_del_init(&msg->link);
  353. rxrpc_put_message(msg);
  354. }
  355. _leave(" = %d", ret);
  356. return ret;
  357. } /* end rxrpc_incoming_msg() */
  358. /*****************************************************************************/
  359. /*
  360. * accept a new call
  361. * - called from krxiod in process context
  362. */
  363. void rxrpc_trans_receive_packet(struct rxrpc_transport *trans)
  364. {
  365. struct rxrpc_message *msg;
  366. struct rxrpc_peer *peer;
  367. struct sk_buff *pkt;
  368. int ret;
  369. __be32 addr;
  370. __be16 port;
  371. LIST_HEAD(msgq);
  372. _enter("%p{%d}", trans, trans->port);
  373. for (;;) {
  374. /* deal with outstanting errors first */
  375. if (trans->error_rcvd)
  376. rxrpc_trans_receive_error_report(trans);
  377. /* attempt to receive a packet */
  378. pkt = skb_recv_datagram(trans->socket->sk, 0, 1, &ret);
  379. if (!pkt) {
  380. if (ret == -EAGAIN) {
  381. _leave(" EAGAIN");
  382. return;
  383. }
  384. /* an icmp error may have occurred */
  385. rxrpc_krxiod_queue_transport(trans);
  386. _leave(" error %d\n", ret);
  387. return;
  388. }
  389. /* we'll probably need to checksum it (didn't call
  390. * sock_recvmsg) */
  391. if (skb_checksum_complete(pkt)) {
  392. kfree_skb(pkt);
  393. rxrpc_krxiod_queue_transport(trans);
  394. _leave(" CSUM failed");
  395. return;
  396. }
  397. addr = pkt->nh.iph->saddr;
  398. port = pkt->h.uh->source;
  399. _net("Rx Received UDP packet from %08x:%04hu",
  400. ntohl(addr), ntohs(port));
  401. /* unmarshall the Rx parameters and split jumbo packets */
  402. ret = rxrpc_incoming_msg(trans, pkt, &msgq);
  403. if (ret < 0) {
  404. kfree_skb(pkt);
  405. rxrpc_krxiod_queue_transport(trans);
  406. _leave(" bad packet");
  407. return;
  408. }
  409. BUG_ON(list_empty(&msgq));
  410. msg = list_entry(msgq.next, struct rxrpc_message, link);
  411. /* locate the record for the peer from which it
  412. * originated */
  413. ret = rxrpc_peer_lookup(trans, addr, &peer);
  414. if (ret < 0) {
  415. kdebug("Rx No connections from that peer");
  416. rxrpc_trans_immediate_abort(trans, msg, -EINVAL);
  417. goto finished_msg;
  418. }
  419. /* try and find a matching connection */
  420. ret = rxrpc_connection_lookup(peer, msg, &msg->conn);
  421. if (ret < 0) {
  422. kdebug("Rx Unknown Connection");
  423. rxrpc_trans_immediate_abort(trans, msg, -EINVAL);
  424. rxrpc_put_peer(peer);
  425. goto finished_msg;
  426. }
  427. rxrpc_put_peer(peer);
  428. /* deal with the first packet of a new call */
  429. if (msg->hdr.flags & RXRPC_CLIENT_INITIATED &&
  430. msg->hdr.type == RXRPC_PACKET_TYPE_DATA &&
  431. ntohl(msg->hdr.seq) == 1
  432. ) {
  433. _debug("Rx New server call");
  434. rxrpc_trans_receive_new_call(trans, &msgq);
  435. goto finished_msg;
  436. }
  437. /* deal with subsequent packet(s) of call */
  438. _debug("Rx Call packet");
  439. while (!list_empty(&msgq)) {
  440. msg = list_entry(msgq.next, struct rxrpc_message, link);
  441. list_del_init(&msg->link);
  442. ret = rxrpc_conn_receive_call_packet(msg->conn, NULL, msg);
  443. if (ret < 0) {
  444. rxrpc_trans_immediate_abort(trans, msg, ret);
  445. rxrpc_put_message(msg);
  446. goto finished_msg;
  447. }
  448. rxrpc_put_message(msg);
  449. }
  450. goto finished_msg;
  451. /* dispose of the packets */
  452. finished_msg:
  453. while (!list_empty(&msgq)) {
  454. msg = list_entry(msgq.next, struct rxrpc_message, link);
  455. list_del_init(&msg->link);
  456. rxrpc_put_message(msg);
  457. }
  458. kfree_skb(pkt);
  459. }
  460. _leave("");
  461. } /* end rxrpc_trans_receive_packet() */
  462. /*****************************************************************************/
  463. /*
  464. * accept a new call from a client trying to connect to one of my services
  465. * - called in process context
  466. */
  467. static int rxrpc_trans_receive_new_call(struct rxrpc_transport *trans,
  468. struct list_head *msgq)
  469. {
  470. struct rxrpc_message *msg;
  471. _enter("");
  472. /* only bother with the first packet */
  473. msg = list_entry(msgq->next, struct rxrpc_message, link);
  474. list_del_init(&msg->link);
  475. rxrpc_krxsecd_queue_incoming_call(msg);
  476. rxrpc_put_message(msg);
  477. _leave(" = 0");
  478. return 0;
  479. } /* end rxrpc_trans_receive_new_call() */
  480. /*****************************************************************************/
  481. /*
  482. * perform an immediate abort without connection or call structures
  483. */
  484. int rxrpc_trans_immediate_abort(struct rxrpc_transport *trans,
  485. struct rxrpc_message *msg,
  486. int error)
  487. {
  488. struct rxrpc_header ahdr;
  489. struct sockaddr_in sin;
  490. struct msghdr msghdr;
  491. struct kvec iov[2];
  492. __be32 _error;
  493. int len, ret;
  494. _enter("%p,%p,%d", trans, msg, error);
  495. /* don't abort an abort packet */
  496. if (msg->hdr.type == RXRPC_PACKET_TYPE_ABORT) {
  497. _leave(" = 0");
  498. return 0;
  499. }
  500. _error = htonl(-error);
  501. /* set up the message to be transmitted */
  502. memcpy(&ahdr, &msg->hdr, sizeof(ahdr));
  503. ahdr.epoch = msg->hdr.epoch;
  504. ahdr.serial = htonl(1);
  505. ahdr.seq = 0;
  506. ahdr.type = RXRPC_PACKET_TYPE_ABORT;
  507. ahdr.flags = RXRPC_LAST_PACKET;
  508. ahdr.flags |= ~msg->hdr.flags & RXRPC_CLIENT_INITIATED;
  509. iov[0].iov_len = sizeof(ahdr);
  510. iov[0].iov_base = &ahdr;
  511. iov[1].iov_len = sizeof(_error);
  512. iov[1].iov_base = &_error;
  513. len = sizeof(ahdr) + sizeof(_error);
  514. memset(&sin,0,sizeof(sin));
  515. sin.sin_family = AF_INET;
  516. sin.sin_port = msg->pkt->h.uh->source;
  517. sin.sin_addr.s_addr = msg->pkt->nh.iph->saddr;
  518. msghdr.msg_name = &sin;
  519. msghdr.msg_namelen = sizeof(sin);
  520. msghdr.msg_control = NULL;
  521. msghdr.msg_controllen = 0;
  522. msghdr.msg_flags = MSG_DONTWAIT;
  523. _net("Sending message type %d of %d bytes to %08x:%d",
  524. ahdr.type,
  525. len,
  526. ntohl(sin.sin_addr.s_addr),
  527. ntohs(sin.sin_port));
  528. /* send the message */
  529. ret = kernel_sendmsg(trans->socket, &msghdr, iov, 2, len);
  530. _leave(" = %d", ret);
  531. return ret;
  532. } /* end rxrpc_trans_immediate_abort() */
  533. /*****************************************************************************/
  534. /*
  535. * receive an ICMP error report and percolate it to all connections
  536. * heading to the affected host or port
  537. */
  538. static void rxrpc_trans_receive_error_report(struct rxrpc_transport *trans)
  539. {
  540. struct rxrpc_connection *conn;
  541. struct sockaddr_in sin;
  542. struct rxrpc_peer *peer;
  543. struct list_head connq, *_p;
  544. struct errormsg emsg;
  545. struct msghdr msg;
  546. __be16 port;
  547. int local, err;
  548. _enter("%p", trans);
  549. for (;;) {
  550. trans->error_rcvd = 0;
  551. /* try and receive an error message */
  552. msg.msg_name = &sin;
  553. msg.msg_namelen = sizeof(sin);
  554. msg.msg_control = &emsg;
  555. msg.msg_controllen = sizeof(emsg);
  556. msg.msg_flags = 0;
  557. err = kernel_recvmsg(trans->socket, &msg, NULL, 0, 0,
  558. MSG_ERRQUEUE | MSG_DONTWAIT | MSG_TRUNC);
  559. if (err == -EAGAIN) {
  560. _leave("");
  561. return;
  562. }
  563. if (err < 0) {
  564. printk("%s: unable to recv an error report: %d\n",
  565. __FUNCTION__, err);
  566. _leave("");
  567. return;
  568. }
  569. msg.msg_controllen = (char *) msg.msg_control - (char *) &emsg;
  570. if (msg.msg_controllen < sizeof(emsg.cmsg) ||
  571. msg.msg_namelen < sizeof(sin)) {
  572. printk("%s: short control message"
  573. " (nlen=%u clen=%Zu fl=%x)\n",
  574. __FUNCTION__,
  575. msg.msg_namelen,
  576. msg.msg_controllen,
  577. msg.msg_flags);
  578. continue;
  579. }
  580. _net("Rx Received control message"
  581. " { len=%Zu level=%u type=%u }",
  582. emsg.cmsg.cmsg_len,
  583. emsg.cmsg.cmsg_level,
  584. emsg.cmsg.cmsg_type);
  585. if (sin.sin_family != AF_INET) {
  586. printk("Rx Ignoring error report with non-INET address"
  587. " (fam=%u)",
  588. sin.sin_family);
  589. continue;
  590. }
  591. _net("Rx Received message pertaining to host addr=%x port=%hu",
  592. ntohl(sin.sin_addr.s_addr), ntohs(sin.sin_port));
  593. if (emsg.cmsg.cmsg_level != SOL_IP ||
  594. emsg.cmsg.cmsg_type != IP_RECVERR) {
  595. printk("Rx Ignoring unknown error report"
  596. " { level=%u type=%u }",
  597. emsg.cmsg.cmsg_level,
  598. emsg.cmsg.cmsg_type);
  599. continue;
  600. }
  601. if (msg.msg_controllen < sizeof(emsg.cmsg) + sizeof(emsg.ee)) {
  602. printk("%s: short error message (%Zu)\n",
  603. __FUNCTION__, msg.msg_controllen);
  604. _leave("");
  605. return;
  606. }
  607. port = sin.sin_port;
  608. switch (emsg.ee.ee_origin) {
  609. case SO_EE_ORIGIN_ICMP:
  610. local = 0;
  611. switch (emsg.ee.ee_type) {
  612. case ICMP_DEST_UNREACH:
  613. switch (emsg.ee.ee_code) {
  614. case ICMP_NET_UNREACH:
  615. _net("Rx Received ICMP Network Unreachable");
  616. port = 0;
  617. err = -ENETUNREACH;
  618. break;
  619. case ICMP_HOST_UNREACH:
  620. _net("Rx Received ICMP Host Unreachable");
  621. port = 0;
  622. err = -EHOSTUNREACH;
  623. break;
  624. case ICMP_PORT_UNREACH:
  625. _net("Rx Received ICMP Port Unreachable");
  626. err = -ECONNREFUSED;
  627. break;
  628. case ICMP_NET_UNKNOWN:
  629. _net("Rx Received ICMP Unknown Network");
  630. port = 0;
  631. err = -ENETUNREACH;
  632. break;
  633. case ICMP_HOST_UNKNOWN:
  634. _net("Rx Received ICMP Unknown Host");
  635. port = 0;
  636. err = -EHOSTUNREACH;
  637. break;
  638. default:
  639. _net("Rx Received ICMP DestUnreach { code=%u }",
  640. emsg.ee.ee_code);
  641. err = emsg.ee.ee_errno;
  642. break;
  643. }
  644. break;
  645. case ICMP_TIME_EXCEEDED:
  646. _net("Rx Received ICMP TTL Exceeded");
  647. err = emsg.ee.ee_errno;
  648. break;
  649. default:
  650. _proto("Rx Received ICMP error { type=%u code=%u }",
  651. emsg.ee.ee_type, emsg.ee.ee_code);
  652. err = emsg.ee.ee_errno;
  653. break;
  654. }
  655. break;
  656. case SO_EE_ORIGIN_LOCAL:
  657. _proto("Rx Received local error { error=%d }",
  658. emsg.ee.ee_errno);
  659. local = 1;
  660. err = emsg.ee.ee_errno;
  661. break;
  662. case SO_EE_ORIGIN_NONE:
  663. case SO_EE_ORIGIN_ICMP6:
  664. default:
  665. _proto("Rx Received error report { orig=%u }",
  666. emsg.ee.ee_origin);
  667. local = 0;
  668. err = emsg.ee.ee_errno;
  669. break;
  670. }
  671. /* find all the connections between this transport and the
  672. * affected destination */
  673. INIT_LIST_HEAD(&connq);
  674. if (rxrpc_peer_lookup(trans, sin.sin_addr.s_addr,
  675. &peer) == 0) {
  676. read_lock(&peer->conn_lock);
  677. list_for_each(_p, &peer->conn_active) {
  678. conn = list_entry(_p, struct rxrpc_connection,
  679. link);
  680. if (port && conn->addr.sin_port != port)
  681. continue;
  682. if (!list_empty(&conn->err_link))
  683. continue;
  684. rxrpc_get_connection(conn);
  685. list_add_tail(&conn->err_link, &connq);
  686. }
  687. read_unlock(&peer->conn_lock);
  688. /* service all those connections */
  689. while (!list_empty(&connq)) {
  690. conn = list_entry(connq.next,
  691. struct rxrpc_connection,
  692. err_link);
  693. list_del(&conn->err_link);
  694. rxrpc_conn_handle_error(conn, local, err);
  695. rxrpc_put_connection(conn);
  696. }
  697. rxrpc_put_peer(peer);
  698. }
  699. }
  700. _leave("");
  701. return;
  702. } /* end rxrpc_trans_receive_error_report() */