totemudp.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. /*
  2. * Copyright (c) 2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2012 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  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 MontaVista Software, 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 <config.h>
  35. #include <assert.h>
  36. #include <pthread.h>
  37. #include <sys/mman.h>
  38. #include <sys/types.h>
  39. #include <sys/stat.h>
  40. #include <sys/socket.h>
  41. #include <netdb.h>
  42. #include <sys/un.h>
  43. #include <sys/ioctl.h>
  44. #include <sys/param.h>
  45. #include <netinet/in.h>
  46. #include <arpa/inet.h>
  47. #include <unistd.h>
  48. #include <fcntl.h>
  49. #include <stdlib.h>
  50. #include <stdio.h>
  51. #include <errno.h>
  52. #include <sched.h>
  53. #include <time.h>
  54. #include <sys/time.h>
  55. #include <sys/poll.h>
  56. #include <limits.h>
  57. #include <corosync/sq.h>
  58. #include <corosync/swab.h>
  59. #include <corosync/list.h>
  60. #include <qb/qbdefs.h>
  61. #include <qb/qbloop.h>
  62. #define LOGSYS_UTILS_ONLY 1
  63. #include <corosync/logsys.h>
  64. #include "totemudp.h"
  65. #include "util.h"
  66. #include "totemcrypto.h"
  67. #include <nss.h>
  68. #include <pk11pub.h>
  69. #include <pkcs11.h>
  70. #include <prerror.h>
  71. #ifndef MSG_NOSIGNAL
  72. #define MSG_NOSIGNAL 0
  73. #endif
  74. #define MCAST_SOCKET_BUFFER_SIZE (TRANSMITS_ALLOWED * FRAME_SIZE_MAX)
  75. #define NETIF_STATE_REPORT_UP 1
  76. #define NETIF_STATE_REPORT_DOWN 2
  77. #define BIND_STATE_UNBOUND 0
  78. #define BIND_STATE_REGULAR 1
  79. #define BIND_STATE_LOOPBACK 2
  80. #define MESSAGE_TYPE_MCAST 1
  81. struct totemudp_socket {
  82. int mcast_recv;
  83. int mcast_send;
  84. int token;
  85. };
  86. struct totemudp_instance {
  87. struct crypto_instance *crypto_inst;
  88. qb_loop_t *totemudp_poll_handle;
  89. struct totem_interface *totem_interface;
  90. int netif_state_report;
  91. int netif_bind_state;
  92. void *context;
  93. void (*totemudp_deliver_fn) (
  94. void *context,
  95. const void *msg,
  96. unsigned int msg_len);
  97. void (*totemudp_iface_change_fn) (
  98. void *context,
  99. const struct totem_ip_address *iface_address);
  100. void (*totemudp_target_set_completed) (void *context);
  101. /*
  102. * Function and data used to log messages
  103. */
  104. int totemudp_log_level_security;
  105. int totemudp_log_level_error;
  106. int totemudp_log_level_warning;
  107. int totemudp_log_level_notice;
  108. int totemudp_log_level_debug;
  109. int totemudp_subsys_id;
  110. void (*totemudp_log_printf) (
  111. int level,
  112. int subsys,
  113. const char *function,
  114. const char *file,
  115. int line,
  116. const char *format,
  117. ...)__attribute__((format(printf, 6, 7)));
  118. void *udp_context;
  119. char iov_buffer[FRAME_SIZE_MAX];
  120. char iov_buffer_flush[FRAME_SIZE_MAX];
  121. struct iovec totemudp_iov_recv;
  122. struct iovec totemudp_iov_recv_flush;
  123. struct totemudp_socket totemudp_sockets;
  124. struct totem_ip_address mcast_address;
  125. int stats_sent;
  126. int stats_recv;
  127. int stats_delv;
  128. int stats_remcasts;
  129. int stats_orf_token;
  130. struct timeval stats_tv_start;
  131. struct totem_ip_address my_id;
  132. int firstrun;
  133. qb_loop_timer_handle timer_netif_check_timeout;
  134. unsigned int my_memb_entries;
  135. int flushing;
  136. struct totem_config *totem_config;
  137. struct totem_ip_address token_target;
  138. };
  139. struct work_item {
  140. const void *msg;
  141. unsigned int msg_len;
  142. struct totemudp_instance *instance;
  143. };
  144. static int totemudp_build_sockets (
  145. struct totemudp_instance *instance,
  146. struct totem_ip_address *bindnet_address,
  147. struct totem_ip_address *mcastaddress,
  148. struct totemudp_socket *sockets,
  149. struct totem_ip_address *bound_to);
  150. static struct totem_ip_address localhost;
  151. static void totemudp_instance_initialize (struct totemudp_instance *instance)
  152. {
  153. memset (instance, 0, sizeof (struct totemudp_instance));
  154. instance->netif_state_report = NETIF_STATE_REPORT_UP | NETIF_STATE_REPORT_DOWN;
  155. instance->totemudp_iov_recv.iov_base = instance->iov_buffer;
  156. instance->totemudp_iov_recv.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  157. instance->totemudp_iov_recv_flush.iov_base = instance->iov_buffer_flush;
  158. instance->totemudp_iov_recv_flush.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  159. /*
  160. * There is always atleast 1 processor
  161. */
  162. instance->my_memb_entries = 1;
  163. }
  164. #define log_printf(level, format, args...) \
  165. do { \
  166. instance->totemudp_log_printf ( \
  167. level, instance->totemudp_subsys_id, \
  168. __FUNCTION__, __FILE__, __LINE__, \
  169. (const char *)format, ##args); \
  170. } while (0);
  171. #define LOGSYS_PERROR(err_num, level, fmt, args...) \
  172. do { \
  173. char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
  174. const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
  175. instance->totemudp_log_printf ( \
  176. level, instance->totemudp_subsys_id, \
  177. __FUNCTION__, __FILE__, __LINE__, \
  178. fmt ": %s (%d)\n", ##args, _error_ptr, err_num); \
  179. } while(0)
  180. int totemudp_crypto_set (
  181. void *udp_context,
  182. unsigned int type)
  183. {
  184. return (0);
  185. }
  186. static inline void ucast_sendmsg (
  187. struct totemudp_instance *instance,
  188. struct totem_ip_address *system_to,
  189. const void *msg,
  190. unsigned int msg_len)
  191. {
  192. struct msghdr msg_ucast;
  193. int res = 0;
  194. size_t buf_out_len;
  195. unsigned char buf_out[FRAME_SIZE_MAX];
  196. struct sockaddr_storage sockaddr;
  197. struct iovec iovec;
  198. int addrlen;
  199. /*
  200. * Encrypt and digest the message
  201. */
  202. if (crypto_encrypt_and_sign (
  203. instance->crypto_inst,
  204. (const unsigned char *)msg,
  205. msg_len,
  206. buf_out,
  207. &buf_out_len) != 0) {
  208. /*
  209. * TODO: how to handle error here
  210. */
  211. log_printf(LOGSYS_LEVEL_CRIT, "Unable to crypt? now what?");
  212. }
  213. iovec.iov_base = (void *)buf_out;
  214. iovec.iov_len = buf_out_len;
  215. /*
  216. * Build unicast message
  217. */
  218. totemip_totemip_to_sockaddr_convert(system_to,
  219. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  220. msg_ucast.msg_name = &sockaddr;
  221. msg_ucast.msg_namelen = addrlen;
  222. msg_ucast.msg_iov = (void *)&iovec;
  223. msg_ucast.msg_iovlen = 1;
  224. #if !defined(COROSYNC_SOLARIS)
  225. msg_ucast.msg_control = 0;
  226. msg_ucast.msg_controllen = 0;
  227. msg_ucast.msg_flags = 0;
  228. #else
  229. msg_ucast.msg_accrights = NULL;
  230. msg_ucast.msg_accrightslen = 0;
  231. #endif
  232. /*
  233. * Transmit unicast message
  234. * An error here is recovered by totemsrp
  235. */
  236. res = sendmsg (instance->totemudp_sockets.mcast_send, &msg_ucast,
  237. MSG_NOSIGNAL);
  238. if (res < 0) {
  239. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  240. "sendmsg(ucast) failed (non-critical)");
  241. }
  242. }
  243. static inline void mcast_sendmsg (
  244. struct totemudp_instance *instance,
  245. const void *msg,
  246. unsigned int msg_len)
  247. {
  248. struct msghdr msg_mcast;
  249. int res = 0;
  250. size_t buf_out_len;
  251. unsigned char buf_out[FRAME_SIZE_MAX];
  252. struct iovec iovec;
  253. struct sockaddr_storage sockaddr;
  254. int addrlen;
  255. /*
  256. * Encrypt and digest the message
  257. */
  258. if (crypto_encrypt_and_sign (
  259. instance->crypto_inst,
  260. (const unsigned char *)msg,
  261. msg_len,
  262. buf_out,
  263. &buf_out_len) != 0) {
  264. /*
  265. * TODO: how to handle error here
  266. */
  267. log_printf(LOGSYS_LEVEL_CRIT, "unable to crypt? now what?");
  268. }
  269. iovec.iov_base = (void *)&buf_out;
  270. iovec.iov_len = buf_out_len;
  271. /*
  272. * Build multicast message
  273. */
  274. totemip_totemip_to_sockaddr_convert(&instance->mcast_address,
  275. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  276. msg_mcast.msg_name = &sockaddr;
  277. msg_mcast.msg_namelen = addrlen;
  278. msg_mcast.msg_iov = (void *)&iovec;
  279. msg_mcast.msg_iovlen = 1;
  280. #if !defined(COROSYNC_SOLARIS)
  281. msg_mcast.msg_control = 0;
  282. msg_mcast.msg_controllen = 0;
  283. msg_mcast.msg_flags = 0;
  284. #else
  285. msg_mcast.msg_accrights = NULL;
  286. msg_mcast.msg_accrightslen = 0;
  287. #endif
  288. /*
  289. * Transmit multicast message
  290. * An error here is recovered by totemsrp
  291. */
  292. res = sendmsg (instance->totemudp_sockets.mcast_send, &msg_mcast,
  293. MSG_NOSIGNAL);
  294. if (res < 0) {
  295. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  296. "sendmsg(mcast) failed (non-critical)");
  297. }
  298. }
  299. int totemudp_finalize (
  300. void *udp_context)
  301. {
  302. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  303. int res = 0;
  304. if (instance->totemudp_sockets.mcast_recv > 0) {
  305. close (instance->totemudp_sockets.mcast_recv);
  306. qb_loop_poll_del (instance->totemudp_poll_handle,
  307. instance->totemudp_sockets.mcast_recv);
  308. }
  309. if (instance->totemudp_sockets.mcast_send > 0) {
  310. close (instance->totemudp_sockets.mcast_send);
  311. }
  312. if (instance->totemudp_sockets.token > 0) {
  313. close (instance->totemudp_sockets.token);
  314. qb_loop_poll_del (instance->totemudp_poll_handle,
  315. instance->totemudp_sockets.token);
  316. }
  317. return (res);
  318. }
  319. /*
  320. * Only designed to work with a message with one iov
  321. */
  322. static int net_deliver_fn (
  323. int fd,
  324. int revents,
  325. void *data)
  326. {
  327. struct totemudp_instance *instance = (struct totemudp_instance *)data;
  328. struct msghdr msg_recv;
  329. struct iovec *iovec;
  330. struct sockaddr_storage system_from;
  331. int bytes_received;
  332. int res = 0;
  333. char *message_type;
  334. if (instance->flushing == 1) {
  335. iovec = &instance->totemudp_iov_recv_flush;
  336. } else {
  337. iovec = &instance->totemudp_iov_recv;
  338. }
  339. /*
  340. * Receive datagram
  341. */
  342. msg_recv.msg_name = &system_from;
  343. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  344. msg_recv.msg_iov = iovec;
  345. msg_recv.msg_iovlen = 1;
  346. #if !defined(COROSYNC_SOLARIS)
  347. msg_recv.msg_control = 0;
  348. msg_recv.msg_controllen = 0;
  349. msg_recv.msg_flags = 0;
  350. #else
  351. msg_recv.msg_accrights = NULL;
  352. msg_recv.msg_accrightslen = 0;
  353. #endif
  354. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  355. if (bytes_received == -1) {
  356. return (0);
  357. } else {
  358. instance->stats_recv += bytes_received;
  359. }
  360. /*
  361. * Authenticate and if authenticated, decrypt datagram
  362. */
  363. res = crypto_authenticate_and_decrypt (instance->crypto_inst, iovec->iov_base, &bytes_received);
  364. if (res == -1) {
  365. log_printf (instance->totemudp_log_level_security, "Received message has invalid digest... ignoring.");
  366. log_printf (instance->totemudp_log_level_security,
  367. "Invalid packet data");
  368. iovec->iov_len = FRAME_SIZE_MAX;
  369. return 0;
  370. }
  371. iovec->iov_len = bytes_received;
  372. /*
  373. * Drop all non-mcast messages (more specifically join
  374. * messages should be dropped)
  375. */
  376. message_type = (char *)iovec->iov_base;
  377. if (instance->flushing == 1 && *message_type != MESSAGE_TYPE_MCAST) {
  378. iovec->iov_len = FRAME_SIZE_MAX;
  379. return (0);
  380. }
  381. /*
  382. * Handle incoming message
  383. */
  384. instance->totemudp_deliver_fn (
  385. instance->context,
  386. iovec->iov_base,
  387. iovec->iov_len);
  388. iovec->iov_len = FRAME_SIZE_MAX;
  389. return (0);
  390. }
  391. static int netif_determine (
  392. struct totemudp_instance *instance,
  393. struct totem_ip_address *bindnet,
  394. struct totem_ip_address *bound_to,
  395. int *interface_up,
  396. int *interface_num)
  397. {
  398. int res;
  399. res = totemip_iface_check (bindnet, bound_to,
  400. interface_up, interface_num,
  401. instance->totem_config->clear_node_high_bit);
  402. return (res);
  403. }
  404. /*
  405. * If the interface is up, the sockets for totem are built. If the interface is down
  406. * this function is requeued in the timer list to retry building the sockets later.
  407. */
  408. static void timer_function_netif_check_timeout (
  409. void *data)
  410. {
  411. struct totemudp_instance *instance = (struct totemudp_instance *)data;
  412. int interface_up;
  413. int interface_num;
  414. struct totem_ip_address *bind_address;
  415. /*
  416. * Build sockets for every interface
  417. */
  418. netif_determine (instance,
  419. &instance->totem_interface->bindnet,
  420. &instance->totem_interface->boundto,
  421. &interface_up, &interface_num);
  422. /*
  423. * If the network interface isn't back up and we are already
  424. * in loopback mode, add timer to check again and return
  425. */
  426. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  427. interface_up == 0) ||
  428. (instance->my_memb_entries == 1 &&
  429. instance->netif_bind_state == BIND_STATE_REGULAR &&
  430. interface_up == 1)) {
  431. qb_loop_timer_add (instance->totemudp_poll_handle,
  432. QB_LOOP_MED,
  433. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  434. (void *)instance,
  435. timer_function_netif_check_timeout,
  436. &instance->timer_netif_check_timeout);
  437. /*
  438. * Add a timer to check for a downed regular interface
  439. */
  440. return;
  441. }
  442. if (instance->totemudp_sockets.mcast_recv > 0) {
  443. close (instance->totemudp_sockets.mcast_recv);
  444. qb_loop_poll_del (instance->totemudp_poll_handle,
  445. instance->totemudp_sockets.mcast_recv);
  446. }
  447. if (instance->totemudp_sockets.mcast_send > 0) {
  448. close (instance->totemudp_sockets.mcast_send);
  449. }
  450. if (instance->totemudp_sockets.token > 0) {
  451. close (instance->totemudp_sockets.token);
  452. qb_loop_poll_del (instance->totemudp_poll_handle,
  453. instance->totemudp_sockets.token);
  454. }
  455. if (interface_up == 0) {
  456. /*
  457. * Interface is not up
  458. */
  459. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  460. bind_address = &localhost;
  461. /*
  462. * Add a timer to retry building interfaces and request memb_gather_enter
  463. */
  464. qb_loop_timer_add (instance->totemudp_poll_handle,
  465. QB_LOOP_MED,
  466. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  467. (void *)instance,
  468. timer_function_netif_check_timeout,
  469. &instance->timer_netif_check_timeout);
  470. } else {
  471. /*
  472. * Interface is up
  473. */
  474. instance->netif_bind_state = BIND_STATE_REGULAR;
  475. bind_address = &instance->totem_interface->bindnet;
  476. }
  477. /*
  478. * Create and bind the multicast and unicast sockets
  479. */
  480. (void)totemudp_build_sockets (instance,
  481. &instance->mcast_address,
  482. bind_address,
  483. &instance->totemudp_sockets,
  484. &instance->totem_interface->boundto);
  485. qb_loop_poll_add (
  486. instance->totemudp_poll_handle,
  487. QB_LOOP_MED,
  488. instance->totemudp_sockets.mcast_recv,
  489. POLLIN, instance, net_deliver_fn);
  490. qb_loop_poll_add (
  491. instance->totemudp_poll_handle,
  492. QB_LOOP_MED,
  493. instance->totemudp_sockets.token,
  494. POLLIN, instance, net_deliver_fn);
  495. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  496. /*
  497. * This reports changes in the interface to the user and totemsrp
  498. */
  499. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  500. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  501. log_printf (instance->totemudp_log_level_notice,
  502. "The network interface [%s] is now up.",
  503. totemip_print (&instance->totem_interface->boundto));
  504. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  505. instance->totemudp_iface_change_fn (instance->context, &instance->my_id);
  506. }
  507. /*
  508. * Add a timer to check for interface going down in single membership
  509. */
  510. if (instance->my_memb_entries == 1) {
  511. qb_loop_timer_add (instance->totemudp_poll_handle,
  512. QB_LOOP_MED,
  513. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  514. (void *)instance,
  515. timer_function_netif_check_timeout,
  516. &instance->timer_netif_check_timeout);
  517. }
  518. } else {
  519. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  520. log_printf (instance->totemudp_log_level_notice,
  521. "The network interface is down.");
  522. instance->totemudp_iface_change_fn (instance->context, &instance->my_id);
  523. }
  524. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  525. }
  526. }
  527. /* Set the socket priority to INTERACTIVE to ensure
  528. that our messages don't get queued behind anything else */
  529. static void totemudp_traffic_control_set(struct totemudp_instance *instance, int sock)
  530. {
  531. #ifdef SO_PRIORITY
  532. int prio = 6; /* TC_PRIO_INTERACTIVE */
  533. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int))) {
  534. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning, "Could not set traffic priority");
  535. }
  536. #endif
  537. }
  538. static int totemudp_build_sockets_ip (
  539. struct totemudp_instance *instance,
  540. struct totem_ip_address *mcast_address,
  541. struct totem_ip_address *bindnet_address,
  542. struct totemudp_socket *sockets,
  543. struct totem_ip_address *bound_to,
  544. int interface_num)
  545. {
  546. struct sockaddr_storage sockaddr;
  547. struct ipv6_mreq mreq6;
  548. struct ip_mreq mreq;
  549. struct sockaddr_storage mcast_ss, boundto_ss;
  550. struct sockaddr_in6 *mcast_sin6 = (struct sockaddr_in6 *)&mcast_ss;
  551. struct sockaddr_in *mcast_sin = (struct sockaddr_in *)&mcast_ss;
  552. struct sockaddr_in *boundto_sin = (struct sockaddr_in *)&boundto_ss;
  553. unsigned int sendbuf_size;
  554. unsigned int recvbuf_size;
  555. unsigned int optlen = sizeof (sendbuf_size);
  556. int addrlen;
  557. int res;
  558. int flag;
  559. /*
  560. * Create multicast recv socket
  561. */
  562. sockets->mcast_recv = socket (bindnet_address->family, SOCK_DGRAM, 0);
  563. if (sockets->mcast_recv == -1) {
  564. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  565. "socket() failed");
  566. return (-1);
  567. }
  568. totemip_nosigpipe (sockets->mcast_recv);
  569. res = fcntl (sockets->mcast_recv, F_SETFL, O_NONBLOCK);
  570. if (res == -1) {
  571. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  572. "Could not set non-blocking operation on multicast socket");
  573. return (-1);
  574. }
  575. /*
  576. * Force reuse
  577. */
  578. flag = 1;
  579. if ( setsockopt(sockets->mcast_recv, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  580. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  581. "setsockopt(SO_REUSEADDR) failed");
  582. return (-1);
  583. }
  584. /*
  585. * Bind to multicast socket used for multicast receives
  586. */
  587. totemip_totemip_to_sockaddr_convert(mcast_address,
  588. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  589. res = bind (sockets->mcast_recv, (struct sockaddr *)&sockaddr, addrlen);
  590. if (res == -1) {
  591. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  592. "Unable to bind the socket to receive multicast packets");
  593. return (-1);
  594. }
  595. /*
  596. * Setup mcast send socket
  597. */
  598. sockets->mcast_send = socket (bindnet_address->family, SOCK_DGRAM, 0);
  599. if (sockets->mcast_send == -1) {
  600. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  601. "socket() failed");
  602. return (-1);
  603. }
  604. totemip_nosigpipe (sockets->mcast_send);
  605. res = fcntl (sockets->mcast_send, F_SETFL, O_NONBLOCK);
  606. if (res == -1) {
  607. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  608. "Could not set non-blocking operation on multicast socket");
  609. return (-1);
  610. }
  611. /*
  612. * Force reuse
  613. */
  614. flag = 1;
  615. if ( setsockopt(sockets->mcast_send, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  616. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  617. "setsockopt(SO_REUSEADDR) failed");
  618. return (-1);
  619. }
  620. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port - 1,
  621. &sockaddr, &addrlen);
  622. res = bind (sockets->mcast_send, (struct sockaddr *)&sockaddr, addrlen);
  623. if (res == -1) {
  624. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  625. "Unable to bind the socket to send multicast packets");
  626. return (-1);
  627. }
  628. /*
  629. * Setup unicast socket
  630. */
  631. sockets->token = socket (bindnet_address->family, SOCK_DGRAM, 0);
  632. if (sockets->token == -1) {
  633. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  634. "socket() failed");
  635. return (-1);
  636. }
  637. totemip_nosigpipe (sockets->token);
  638. res = fcntl (sockets->token, F_SETFL, O_NONBLOCK);
  639. if (res == -1) {
  640. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  641. "Could not set non-blocking operation on token socket");
  642. return (-1);
  643. }
  644. /*
  645. * Force reuse
  646. */
  647. flag = 1;
  648. if ( setsockopt(sockets->token, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  649. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  650. "setsockopt(SO_REUSEADDR) failed");
  651. return (-1);
  652. }
  653. /*
  654. * Bind to unicast socket used for token send/receives
  655. * This has the side effect of binding to the correct interface
  656. */
  657. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  658. res = bind (sockets->token, (struct sockaddr *)&sockaddr, addrlen);
  659. if (res == -1) {
  660. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  661. "Unable to bind UDP unicast socket");
  662. return (-1);
  663. }
  664. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  665. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  666. /*
  667. * Set buffer sizes to avoid overruns
  668. */
  669. res = setsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  670. res = setsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  671. res = getsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  672. if (res == 0) {
  673. log_printf (instance->totemudp_log_level_debug,
  674. "Receive multicast socket recv buffer size (%d bytes).", recvbuf_size);
  675. }
  676. res = getsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  677. if (res == 0) {
  678. log_printf (instance->totemudp_log_level_debug,
  679. "Transmit multicast socket send buffer size (%d bytes).", sendbuf_size);
  680. }
  681. /*
  682. * Join group membership on socket
  683. */
  684. totemip_totemip_to_sockaddr_convert(mcast_address, instance->totem_interface->ip_port, &mcast_ss, &addrlen);
  685. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &boundto_ss, &addrlen);
  686. if (instance->totem_config->broadcast_use == 1) {
  687. unsigned int broadcast = 1;
  688. if ((setsockopt(sockets->mcast_recv, SOL_SOCKET,
  689. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  690. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  691. "setting broadcast option failed");
  692. return (-1);
  693. }
  694. if ((setsockopt(sockets->mcast_send, SOL_SOCKET,
  695. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  696. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  697. "setting broadcast option failed");
  698. return (-1);
  699. }
  700. } else {
  701. switch (bindnet_address->family) {
  702. case AF_INET:
  703. memset(&mreq, 0, sizeof(mreq));
  704. mreq.imr_multiaddr.s_addr = mcast_sin->sin_addr.s_addr;
  705. mreq.imr_interface.s_addr = boundto_sin->sin_addr.s_addr;
  706. res = setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  707. &mreq, sizeof (mreq));
  708. if (res == -1) {
  709. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  710. "join ipv4 multicast group failed");
  711. return (-1);
  712. }
  713. break;
  714. case AF_INET6:
  715. memset(&mreq6, 0, sizeof(mreq6));
  716. memcpy(&mreq6.ipv6mr_multiaddr, &mcast_sin6->sin6_addr, sizeof(struct in6_addr));
  717. mreq6.ipv6mr_interface = interface_num;
  718. res = setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_JOIN_GROUP,
  719. &mreq6, sizeof (mreq6));
  720. if (res == -1) {
  721. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  722. "join ipv6 multicast group failed");
  723. return (-1);
  724. }
  725. break;
  726. }
  727. }
  728. /*
  729. * Turn on multicast loopback
  730. */
  731. flag = 1;
  732. switch ( bindnet_address->family ) {
  733. case AF_INET:
  734. res = setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_LOOP,
  735. &flag, sizeof (flag));
  736. break;
  737. case AF_INET6:
  738. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
  739. &flag, sizeof (flag));
  740. }
  741. if (res == -1) {
  742. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  743. "Unable to turn on multicast loopback");
  744. return (-1);
  745. }
  746. /*
  747. * Set multicast packets TTL
  748. */
  749. flag = instance->totem_interface->ttl;
  750. if (bindnet_address->family == AF_INET6) {
  751. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
  752. &flag, sizeof (flag));
  753. if (res == -1) {
  754. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  755. "set mcast v6 TTL failed");
  756. return (-1);
  757. }
  758. } else {
  759. res = setsockopt(sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_TTL,
  760. &flag, sizeof(flag));
  761. if (res == -1) {
  762. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  763. "set mcast v4 TTL failed");
  764. return (-1);
  765. }
  766. }
  767. /*
  768. * Bind to a specific interface for multicast send and receive
  769. */
  770. switch ( bindnet_address->family ) {
  771. case AF_INET:
  772. if (setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_IF,
  773. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  774. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  775. "cannot select interface for multicast packets (send)");
  776. return (-1);
  777. }
  778. if (setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_MULTICAST_IF,
  779. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  780. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  781. "cannot select interface for multicast packets (recv)");
  782. return (-1);
  783. }
  784. break;
  785. case AF_INET6:
  786. if (setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  787. &interface_num, sizeof (interface_num)) < 0) {
  788. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  789. "cannot select interface for multicast packets (send v6)");
  790. return (-1);
  791. }
  792. if (setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  793. &interface_num, sizeof (interface_num)) < 0) {
  794. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  795. "cannot select interface for multicast packets (recv v6)");
  796. return (-1);
  797. }
  798. break;
  799. }
  800. return 0;
  801. }
  802. static int totemudp_build_sockets (
  803. struct totemudp_instance *instance,
  804. struct totem_ip_address *mcast_address,
  805. struct totem_ip_address *bindnet_address,
  806. struct totemudp_socket *sockets,
  807. struct totem_ip_address *bound_to)
  808. {
  809. int interface_num;
  810. int interface_up;
  811. int res;
  812. /*
  813. * Determine the ip address bound to and the interface name
  814. */
  815. res = netif_determine (instance,
  816. bindnet_address,
  817. bound_to,
  818. &interface_up,
  819. &interface_num);
  820. if (res == -1) {
  821. return (-1);
  822. }
  823. totemip_copy(&instance->my_id, bound_to);
  824. res = totemudp_build_sockets_ip (instance, mcast_address,
  825. bindnet_address, sockets, bound_to, interface_num);
  826. /* We only send out of the token socket */
  827. totemudp_traffic_control_set(instance, sockets->token);
  828. return res;
  829. }
  830. /*
  831. * Totem Network interface - also does encryption/decryption
  832. * depends on poll abstraction, POSIX, IPV4
  833. */
  834. /*
  835. * Create an instance
  836. */
  837. int totemudp_initialize (
  838. qb_loop_t *poll_handle,
  839. void **udp_context,
  840. struct totem_config *totem_config,
  841. int interface_no,
  842. void *context,
  843. void (*deliver_fn) (
  844. void *context,
  845. const void *msg,
  846. unsigned int msg_len),
  847. void (*iface_change_fn) (
  848. void *context,
  849. const struct totem_ip_address *iface_address),
  850. void (*target_set_completed) (
  851. void *context))
  852. {
  853. struct totemudp_instance *instance;
  854. instance = malloc (sizeof (struct totemudp_instance));
  855. if (instance == NULL) {
  856. return (-1);
  857. }
  858. totemudp_instance_initialize (instance);
  859. instance->totem_config = totem_config;
  860. /*
  861. * Configure logging
  862. */
  863. instance->totemudp_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  864. instance->totemudp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  865. instance->totemudp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  866. instance->totemudp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  867. instance->totemudp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  868. instance->totemudp_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  869. instance->totemudp_log_printf = totem_config->totem_logging_configuration.log_printf;
  870. /*
  871. * Initialize random number generator for later use to generate salt
  872. */
  873. instance->crypto_inst = crypto_init (totem_config->private_key,
  874. totem_config->private_key_len,
  875. totem_config->crypto_cipher_type,
  876. totem_config->crypto_hash_type,
  877. instance->totemudp_log_printf,
  878. instance->totemudp_log_level_security,
  879. instance->totemudp_log_level_notice,
  880. instance->totemudp_log_level_error,
  881. instance->totemudp_subsys_id);
  882. if (instance->crypto_inst == NULL) {
  883. return (-1);
  884. }
  885. /*
  886. * Initialize local variables for totemudp
  887. */
  888. instance->totem_interface = &totem_config->interfaces[interface_no];
  889. totemip_copy (&instance->mcast_address, &instance->totem_interface->mcast_addr);
  890. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  891. instance->totemudp_poll_handle = poll_handle;
  892. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  893. instance->context = context;
  894. instance->totemudp_deliver_fn = deliver_fn;
  895. instance->totemudp_iface_change_fn = iface_change_fn;
  896. instance->totemudp_target_set_completed = target_set_completed;
  897. totemip_localhost (instance->mcast_address.family, &localhost);
  898. localhost.nodeid = instance->totem_config->node_id;
  899. /*
  900. * RRP layer isn't ready to receive message because it hasn't
  901. * initialized yet. Add short timer to check the interfaces.
  902. */
  903. qb_loop_timer_add (instance->totemudp_poll_handle,
  904. QB_LOOP_MED,
  905. 100*QB_TIME_NS_IN_MSEC,
  906. (void *)instance,
  907. timer_function_netif_check_timeout,
  908. &instance->timer_netif_check_timeout);
  909. *udp_context = instance;
  910. return (0);
  911. }
  912. void *totemudp_buffer_alloc (void)
  913. {
  914. return malloc (FRAME_SIZE_MAX);
  915. }
  916. void totemudp_buffer_release (void *ptr)
  917. {
  918. return free (ptr);
  919. }
  920. int totemudp_processor_count_set (
  921. void *udp_context,
  922. int processor_count)
  923. {
  924. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  925. int res = 0;
  926. instance->my_memb_entries = processor_count;
  927. qb_loop_timer_del (instance->totemudp_poll_handle,
  928. instance->timer_netif_check_timeout);
  929. if (processor_count == 1) {
  930. qb_loop_timer_add (instance->totemudp_poll_handle,
  931. QB_LOOP_MED,
  932. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  933. (void *)instance,
  934. timer_function_netif_check_timeout,
  935. &instance->timer_netif_check_timeout);
  936. }
  937. return (res);
  938. }
  939. int totemudp_recv_flush (void *udp_context)
  940. {
  941. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  942. struct pollfd ufd;
  943. int nfds;
  944. int res = 0;
  945. instance->flushing = 1;
  946. do {
  947. ufd.fd = instance->totemudp_sockets.mcast_recv;
  948. ufd.events = POLLIN;
  949. nfds = poll (&ufd, 1, 0);
  950. if (nfds == 1 && ufd.revents & POLLIN) {
  951. net_deliver_fn (instance->totemudp_sockets.mcast_recv,
  952. ufd.revents, instance);
  953. }
  954. } while (nfds == 1);
  955. instance->flushing = 0;
  956. return (res);
  957. }
  958. int totemudp_send_flush (void *udp_context)
  959. {
  960. return 0;
  961. }
  962. int totemudp_token_send (
  963. void *udp_context,
  964. const void *msg,
  965. unsigned int msg_len)
  966. {
  967. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  968. int res = 0;
  969. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  970. return (res);
  971. }
  972. int totemudp_mcast_flush_send (
  973. void *udp_context,
  974. const void *msg,
  975. unsigned int msg_len)
  976. {
  977. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  978. int res = 0;
  979. mcast_sendmsg (instance, msg, msg_len);
  980. return (res);
  981. }
  982. int totemudp_mcast_noflush_send (
  983. void *udp_context,
  984. const void *msg,
  985. unsigned int msg_len)
  986. {
  987. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  988. int res = 0;
  989. mcast_sendmsg (instance, msg, msg_len);
  990. return (res);
  991. }
  992. extern int totemudp_iface_check (void *udp_context)
  993. {
  994. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  995. int res = 0;
  996. timer_function_netif_check_timeout (instance);
  997. return (res);
  998. }
  999. extern void totemudp_net_mtu_adjust (void *udp_context, struct totem_config *totem_config)
  1000. {
  1001. #define UDPIP_HEADER_SIZE (20 + 8) /* 20 bytes for ip 8 bytes for udp */
  1002. totem_config->net_mtu -= crypto_sec_header_size(totem_config->crypto_cipher_type,
  1003. totem_config->crypto_hash_type) +
  1004. UDPIP_HEADER_SIZE;
  1005. }
  1006. const char *totemudp_iface_print (void *udp_context) {
  1007. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1008. const char *ret_char;
  1009. ret_char = totemip_print (&instance->my_id);
  1010. return (ret_char);
  1011. }
  1012. int totemudp_iface_get (
  1013. void *udp_context,
  1014. struct totem_ip_address *addr)
  1015. {
  1016. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1017. int res = 0;
  1018. memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
  1019. return (res);
  1020. }
  1021. int totemudp_token_target_set (
  1022. void *udp_context,
  1023. const struct totem_ip_address *token_target)
  1024. {
  1025. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1026. int res = 0;
  1027. memcpy (&instance->token_target, token_target,
  1028. sizeof (struct totem_ip_address));
  1029. instance->totemudp_target_set_completed (instance->context);
  1030. return (res);
  1031. }
  1032. extern int totemudp_recv_mcast_empty (
  1033. void *udp_context)
  1034. {
  1035. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1036. unsigned int res;
  1037. struct sockaddr_storage system_from;
  1038. struct msghdr msg_recv;
  1039. struct pollfd ufd;
  1040. int nfds;
  1041. int msg_processed = 0;
  1042. /*
  1043. * Receive datagram
  1044. */
  1045. msg_recv.msg_name = &system_from;
  1046. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  1047. msg_recv.msg_iov = &instance->totemudp_iov_recv_flush;
  1048. msg_recv.msg_iovlen = 1;
  1049. #if !defined(COROSYNC_SOLARIS)
  1050. msg_recv.msg_control = 0;
  1051. msg_recv.msg_controllen = 0;
  1052. msg_recv.msg_flags = 0;
  1053. #else
  1054. msg_recv.msg_accrights = NULL;
  1055. msg_recv.msg_accrightslen = 0;
  1056. #endif
  1057. do {
  1058. ufd.fd = instance->totemudp_sockets.mcast_recv;
  1059. ufd.events = POLLIN;
  1060. nfds = poll (&ufd, 1, 0);
  1061. if (nfds == 1 && ufd.revents & POLLIN) {
  1062. res = recvmsg (instance->totemudp_sockets.mcast_recv, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  1063. if (res != -1) {
  1064. msg_processed = 1;
  1065. } else {
  1066. msg_processed = -1;
  1067. }
  1068. }
  1069. } while (nfds == 1);
  1070. return (msg_processed);
  1071. }