qdevice-net-socket.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * Copyright (c) 2015-2020 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include "log.h"
  35. #include "msg.h"
  36. #include "msgio.h"
  37. #include "qnet-config.h"
  38. #include "qdevice-net-msg-received.h"
  39. #include "qdevice-net-nss.h"
  40. #include "qdevice-net-send.h"
  41. #include "qdevice-net-socket.h"
  42. /*
  43. * Socket callbacks
  44. */
  45. static int
  46. socket_set_events_cb(PRFileDesc *prfd, short *events, void *user_data1, void *user_data2)
  47. {
  48. struct qdevice_net_instance *instance = (struct qdevice_net_instance *)user_data1;
  49. if (!send_buffer_list_empty(&instance->send_buffer_list)) {
  50. *events |= POLLOUT;
  51. }
  52. return (0);
  53. }
  54. static int
  55. socket_read_cb(PRFileDesc *prfd, const PRPollDesc *pd, void *user_data1, void *user_data2)
  56. {
  57. struct qdevice_net_instance *instance = (struct qdevice_net_instance *)user_data1;
  58. if (qdevice_net_socket_read(instance) == -1) {
  59. instance->schedule_disconnect = 1;
  60. return (-1);
  61. }
  62. return (0);
  63. }
  64. static int
  65. socket_write_cb(PRFileDesc *prfd, const PRPollDesc *pd, void *user_data1, void *user_data2)
  66. {
  67. struct qdevice_net_instance *instance = (struct qdevice_net_instance *)user_data1;
  68. if (qdevice_net_socket_write(instance) == -1) {
  69. instance->schedule_disconnect = 1;
  70. return (-1);
  71. }
  72. return (0);
  73. }
  74. static int
  75. non_blocking_client_socket_write_cb(PRFileDesc *prfd, const PRPollDesc *pd, void *user_data1,
  76. void *user_data2)
  77. {
  78. int res;
  79. struct qdevice_net_instance *instance = (struct qdevice_net_instance *)user_data1;
  80. res = nss_sock_non_blocking_client_succeeded(pd);
  81. if (res == -1) {
  82. /*
  83. * Connect failed -> remove this fd from main loop and try next
  84. */
  85. res = qdevice_net_socket_del_from_main_poll_loop(instance);
  86. if (res == -1) {
  87. return (-1);
  88. }
  89. res = nss_sock_non_blocking_client_try_next(&instance->non_blocking_client);
  90. if (res == -1) {
  91. log_nss(LOG_ERR, "Can't connect to qnetd host.");
  92. nss_sock_non_blocking_client_destroy(&instance->non_blocking_client);
  93. }
  94. res = qdevice_net_socket_add_to_main_poll_loop(instance);
  95. if (res == -1) {
  96. return (-1);
  97. }
  98. } else if (res == 0) {
  99. /*
  100. * Poll again
  101. */
  102. } else if (res == 1) {
  103. /*
  104. * Connect success -> delete socket from main loop and add final one
  105. */
  106. res = qdevice_net_socket_del_from_main_poll_loop(instance);
  107. if (res == -1) {
  108. return (-1);
  109. }
  110. instance->socket = instance->non_blocking_client.socket;
  111. nss_sock_non_blocking_client_destroy(&instance->non_blocking_client);
  112. instance->non_blocking_client.socket = NULL;
  113. instance->state = QDEVICE_NET_INSTANCE_STATE_SENDING_PREINIT_REPLY;
  114. res = qdevice_net_socket_add_to_main_poll_loop(instance);
  115. if (res == -1) {
  116. return (-1);
  117. }
  118. log(LOG_DEBUG, "Sending preinit msg to qnetd");
  119. if (qdevice_net_send_preinit(instance) != 0) {
  120. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  121. return (-1);
  122. }
  123. } else {
  124. log(LOG_CRIT, "Unhandled nss_sock_non_blocking_client_succeeded");
  125. exit(EXIT_FAILURE);
  126. }
  127. return (0);
  128. }
  129. static int
  130. socket_err_cb(PRFileDesc *prfd, short revents, const PRPollDesc *pd, void *user_data1, void *user_data2)
  131. {
  132. struct qdevice_net_instance *instance = (struct qdevice_net_instance *)user_data1;
  133. log(LOG_ERR, "POLL_ERR (%u) on main socket", revents);
  134. instance->schedule_disconnect = 1;
  135. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_SERVER_CLOSED_CONNECTION;
  136. return (-1);
  137. }
  138. static int
  139. non_blocking_client_socket_err_cb(PRFileDesc *prfd, short revents, const PRPollDesc *pd,
  140. void *user_data1, void *user_data2)
  141. {
  142. struct qdevice_net_instance *instance = (struct qdevice_net_instance *)user_data1;
  143. /*
  144. * Workaround for RHEL<7. Pollout is never set for nonblocking connect (doesn't work
  145. * only with poll, select works as expected!???).
  146. * So test if client is still valid and if pollout was not already called (ensured
  147. * by default because of order in PR_Poll).
  148. * If both applies it's possible to emulate pollout set by calling poll_write.
  149. */
  150. if (!instance->non_blocking_client.destroyed) {
  151. return (non_blocking_client_socket_write_cb(prfd, pd, user_data1, user_data2));
  152. }
  153. return (0);
  154. }
  155. /*
  156. * Exported functions
  157. */
  158. /*
  159. * -1 means end of connection (EOF) or some other unhandled error. 0 = success
  160. */
  161. int
  162. qdevice_net_socket_read(struct qdevice_net_instance *instance)
  163. {
  164. int res;
  165. int ret_val;
  166. int orig_skipping_msg;
  167. orig_skipping_msg = instance->skipping_msg;
  168. res = msgio_read(instance->socket, &instance->receive_buffer,
  169. &instance->msg_already_received_bytes, &instance->skipping_msg);
  170. if (!orig_skipping_msg && instance->skipping_msg) {
  171. log(LOG_DEBUG, "msgio_read set skipping_msg");
  172. }
  173. ret_val = 0;
  174. switch (res) {
  175. case 0:
  176. /*
  177. * Partial read
  178. */
  179. break;
  180. case -1:
  181. log(LOG_DEBUG, "Server closed connection");
  182. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_SERVER_CLOSED_CONNECTION;
  183. ret_val = -1;
  184. break;
  185. case -2:
  186. log(LOG_ERR, "Unhandled error when reading from server. "
  187. "Disconnecting from server");
  188. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_READ_MESSAGE;
  189. ret_val = -1;
  190. break;
  191. case -3:
  192. log(LOG_ERR, "Can't store message header from server. "
  193. "Disconnecting from server");
  194. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_READ_MESSAGE;
  195. ret_val = -1;
  196. break;
  197. case -4:
  198. log(LOG_ERR, "Can't store message from server. "
  199. "Disconnecting from server");
  200. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_READ_MESSAGE;
  201. ret_val = -1;
  202. break;
  203. case -5:
  204. log(LOG_WARNING, "Server sent unsupported msg type %u. "
  205. "Disconnecting from server", msg_get_type(&instance->receive_buffer));
  206. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_UNSUPPORTED_MSG;
  207. ret_val = -1;
  208. break;
  209. case -6:
  210. log(LOG_WARNING,
  211. "Server wants to send too long message %u bytes. Disconnecting from server",
  212. msg_get_len(&instance->receive_buffer));
  213. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_READ_MESSAGE;
  214. ret_val = -1;
  215. break;
  216. case 1:
  217. /*
  218. * Full message received / skipped
  219. */
  220. if (!instance->skipping_msg) {
  221. if (qdevice_net_msg_received(instance) == -1) {
  222. ret_val = -1;
  223. }
  224. } else {
  225. log(LOG_CRIT, "net_socket_read in skipping msg state");
  226. exit(EXIT_FAILURE);
  227. }
  228. instance->skipping_msg = 0;
  229. instance->msg_already_received_bytes = 0;
  230. dynar_clean(&instance->receive_buffer);
  231. break;
  232. default:
  233. log(LOG_CRIT, "qdevice_net_socket_read unhandled error %d", res);
  234. exit(EXIT_FAILURE);
  235. break;
  236. }
  237. return (ret_val);
  238. }
  239. static int
  240. qdevice_net_socket_write_finished(struct qdevice_net_instance *instance)
  241. {
  242. PRFileDesc *new_pr_fd;
  243. if (instance->state == QDEVICE_NET_INSTANCE_STATE_WAITING_STARTTLS_BEING_SENT) {
  244. /*
  245. * StartTLS sent to server. Begin with TLS handshake
  246. */
  247. if ((new_pr_fd = nss_sock_start_ssl_as_client(instance->socket,
  248. instance->advanced_settings->net_nss_qnetd_cn,
  249. qdevice_net_nss_bad_cert_hook,
  250. qdevice_net_nss_get_client_auth_data,
  251. instance, 0, NULL)) == NULL) {
  252. log_nss(LOG_ERR, "Can't start TLS");
  253. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_START_TLS;
  254. return (-1);
  255. }
  256. /*
  257. * And send init msg
  258. */
  259. if (qdevice_net_send_init(instance) != 0) {
  260. instance->disconnect_reason =
  261. QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  262. return (-1);
  263. }
  264. instance->socket = new_pr_fd;
  265. instance->using_tls = 1;
  266. }
  267. return (0);
  268. }
  269. int
  270. qdevice_net_socket_write(struct qdevice_net_instance *instance)
  271. {
  272. int res;
  273. struct send_buffer_list_entry *send_buffer;
  274. enum msg_type sent_msg_type;
  275. send_buffer = send_buffer_list_get_active(&instance->send_buffer_list);
  276. if (send_buffer == NULL) {
  277. log(LOG_CRIT, "send_buffer_list_get_active returned NULL");
  278. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SEND_MESSAGE;
  279. return (-1);
  280. }
  281. res = msgio_write(instance->socket, &send_buffer->buffer,
  282. &send_buffer->msg_already_sent_bytes);
  283. if (res == 1) {
  284. sent_msg_type = msg_get_type(&send_buffer->buffer);
  285. send_buffer_list_delete(&instance->send_buffer_list, send_buffer);
  286. if (sent_msg_type != MSG_TYPE_ECHO_REQUEST) {
  287. if (qdevice_net_socket_write_finished(instance) == -1) {
  288. return (-1);
  289. }
  290. }
  291. }
  292. if (res == -1) {
  293. log_nss(LOG_CRIT, "PR_Send returned 0");
  294. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_SERVER_CLOSED_CONNECTION;
  295. return (-1);
  296. }
  297. if (res == -2) {
  298. log_nss(LOG_ERR, "Unhandled error when sending message to server");
  299. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SEND_MESSAGE;
  300. return (-1);
  301. }
  302. return (0);
  303. }
  304. int
  305. qdevice_net_socket_add_to_main_poll_loop(struct qdevice_net_instance *instance)
  306. {
  307. if (instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_CONNECT ||
  308. !instance->non_blocking_client.destroyed) {
  309. if (instance->state == QDEVICE_NET_INSTANCE_STATE_WAITING_CONNECT) {
  310. if (pr_poll_loop_add_prfd(&instance->qdevice_instance_ptr->main_poll_loop,
  311. instance->non_blocking_client.socket,
  312. POLLOUT|POLLPRI,
  313. NULL, NULL, non_blocking_client_socket_write_cb,
  314. non_blocking_client_socket_err_cb,
  315. instance, NULL) != 0) {
  316. log(LOG_ERR, "Can't add net socket (non_blocking_client) "
  317. "fd to main poll loop");
  318. return (-1);
  319. }
  320. } else {
  321. if (pr_poll_loop_add_prfd(&instance->qdevice_instance_ptr->main_poll_loop,
  322. instance->socket,
  323. POLLIN,
  324. socket_set_events_cb, socket_read_cb, socket_write_cb, socket_err_cb,
  325. instance, NULL) != 0) {
  326. log(LOG_ERR, "Can't add net socket fd to main poll loop");
  327. return (-1);
  328. }
  329. }
  330. }
  331. return (0);
  332. }
  333. int
  334. qdevice_net_socket_del_from_main_poll_loop(struct qdevice_net_instance *instance)
  335. {
  336. if (!instance->non_blocking_client.destroyed) {
  337. if (pr_poll_loop_del_prfd(&instance->qdevice_instance_ptr->main_poll_loop,
  338. instance->non_blocking_client.socket) != 0) {
  339. log(LOG_ERR, "Can't remove net socket (non_blocking_client) "
  340. "fd from main poll loop");
  341. return (-1);
  342. }
  343. }
  344. if (instance->socket != NULL) {
  345. if (pr_poll_loop_del_prfd(&instance->qdevice_instance_ptr->main_poll_loop,
  346. instance->socket) != 0) {
  347. log(LOG_ERR, "Can't remove net socket fd from main poll loop");
  348. return (-1);
  349. }
  350. }
  351. return (0);
  352. }