totemudp.c 33 KB

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