4
0

totemudp.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508
  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 <sys/uio.h>
  57. #include <limits.h>
  58. #include <corosync/sq.h>
  59. #include <corosync/swab.h>
  60. #include <corosync/list.h>
  61. #include <qb/qbdefs.h>
  62. #include <qb/qbloop.h>
  63. #define LOGSYS_UTILS_ONLY 1
  64. #include <corosync/logsys.h>
  65. #include "totemudp.h"
  66. #include "util.h"
  67. #include "totemcrypto.h"
  68. #include <nss.h>
  69. #include <pk11pub.h>
  70. #include <pkcs11.h>
  71. #include <prerror.h>
  72. #ifndef MSG_NOSIGNAL
  73. #define MSG_NOSIGNAL 0
  74. #endif
  75. #define MCAST_SOCKET_BUFFER_SIZE (TRANSMITS_ALLOWED * FRAME_SIZE_MAX)
  76. #define NETIF_STATE_REPORT_UP 1
  77. #define NETIF_STATE_REPORT_DOWN 2
  78. #define BIND_STATE_UNBOUND 0
  79. #define BIND_STATE_REGULAR 1
  80. #define BIND_STATE_LOOPBACK 2
  81. struct totemudp_socket {
  82. int mcast_recv;
  83. int mcast_send;
  84. int token;
  85. /*
  86. * Socket used for local multicast delivery. We don't rely on multicast
  87. * loop and rather this UNIX DGRAM socket is used. Socket is created by
  88. * socketpair call and they are used in same way as pipe (so [0] is read
  89. * end and [1] is write end)
  90. */
  91. int local_mcast_loop[2];
  92. };
  93. struct totemudp_instance {
  94. struct crypto_instance *crypto_inst;
  95. qb_loop_t *totemudp_poll_handle;
  96. struct totem_interface *totem_interface;
  97. int netif_state_report;
  98. int netif_bind_state;
  99. void *context;
  100. void (*totemudp_deliver_fn) (
  101. void *context,
  102. const void *msg,
  103. unsigned int msg_len);
  104. void (*totemudp_iface_change_fn) (
  105. void *context,
  106. const struct totem_ip_address *iface_address);
  107. void (*totemudp_target_set_completed) (void *context);
  108. /*
  109. * Function and data used to log messages
  110. */
  111. int totemudp_log_level_security;
  112. int totemudp_log_level_error;
  113. int totemudp_log_level_warning;
  114. int totemudp_log_level_notice;
  115. int totemudp_log_level_debug;
  116. int totemudp_subsys_id;
  117. void (*totemudp_log_printf) (
  118. int level,
  119. int subsys,
  120. const char *function,
  121. const char *file,
  122. int line,
  123. const char *format,
  124. ...)__attribute__((format(printf, 6, 7)));
  125. void *udp_context;
  126. char iov_buffer[FRAME_SIZE_MAX];
  127. char iov_buffer_flush[FRAME_SIZE_MAX];
  128. struct iovec totemudp_iov_recv;
  129. struct iovec totemudp_iov_recv_flush;
  130. struct totemudp_socket totemudp_sockets;
  131. struct totem_ip_address mcast_address;
  132. int stats_sent;
  133. int stats_recv;
  134. int stats_delv;
  135. int stats_remcasts;
  136. int stats_orf_token;
  137. struct timeval stats_tv_start;
  138. struct totem_ip_address my_id;
  139. int firstrun;
  140. qb_loop_timer_handle timer_netif_check_timeout;
  141. unsigned int my_memb_entries;
  142. int flushing;
  143. struct totem_config *totem_config;
  144. totemsrp_stats_t *stats;
  145. struct totem_ip_address token_target;
  146. };
  147. struct work_item {
  148. const void *msg;
  149. unsigned int msg_len;
  150. struct totemudp_instance *instance;
  151. };
  152. static int totemudp_build_sockets (
  153. struct totemudp_instance *instance,
  154. struct totem_ip_address *bindnet_address,
  155. struct totem_ip_address *mcastaddress,
  156. struct totemudp_socket *sockets,
  157. struct totem_ip_address *bound_to);
  158. static struct totem_ip_address localhost;
  159. static void totemudp_instance_initialize (struct totemudp_instance *instance)
  160. {
  161. memset (instance, 0, sizeof (struct totemudp_instance));
  162. instance->netif_state_report = NETIF_STATE_REPORT_UP | NETIF_STATE_REPORT_DOWN;
  163. instance->totemudp_iov_recv.iov_base = instance->iov_buffer;
  164. instance->totemudp_iov_recv.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  165. instance->totemudp_iov_recv_flush.iov_base = instance->iov_buffer_flush;
  166. instance->totemudp_iov_recv_flush.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  167. /*
  168. * There is always atleast 1 processor
  169. */
  170. instance->my_memb_entries = 1;
  171. }
  172. #define log_printf(level, format, args...) \
  173. do { \
  174. instance->totemudp_log_printf ( \
  175. level, instance->totemudp_subsys_id, \
  176. __FUNCTION__, __FILE__, __LINE__, \
  177. (const char *)format, ##args); \
  178. } while (0);
  179. #define LOGSYS_PERROR(err_num, level, fmt, args...) \
  180. do { \
  181. char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
  182. const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
  183. instance->totemudp_log_printf ( \
  184. level, instance->totemudp_subsys_id, \
  185. __FUNCTION__, __FILE__, __LINE__, \
  186. fmt ": %s (%d)\n", ##args, _error_ptr, err_num); \
  187. } while(0)
  188. int totemudp_crypto_set (
  189. void *udp_context,
  190. const char *cipher_type,
  191. const char *hash_type)
  192. {
  193. return (0);
  194. }
  195. static inline void ucast_sendmsg (
  196. struct totemudp_instance *instance,
  197. struct totem_ip_address *system_to,
  198. const void *msg,
  199. unsigned int msg_len)
  200. {
  201. struct msghdr msg_ucast;
  202. int res = 0;
  203. size_t buf_out_len;
  204. unsigned char buf_out[FRAME_SIZE_MAX];
  205. struct sockaddr_storage sockaddr;
  206. struct iovec iovec;
  207. int addrlen;
  208. if (msg_len + crypto_get_current_sec_header_size(instance->crypto_inst) > sizeof(buf_out)) {
  209. log_printf(LOGSYS_LEVEL_CRIT, "UDP message for ucast is too big. Ignoring message");
  210. return ;
  211. }
  212. /*
  213. * Encrypt and digest the message
  214. */
  215. if (crypto_encrypt_and_sign (
  216. instance->crypto_inst,
  217. (const unsigned char *)msg,
  218. msg_len,
  219. buf_out,
  220. &buf_out_len) != 0) {
  221. log_printf(LOGSYS_LEVEL_CRIT, "Error encrypting/signing packet (non-critical)");
  222. return;
  223. }
  224. iovec.iov_base = (void *)buf_out;
  225. iovec.iov_len = buf_out_len;
  226. /*
  227. * Build unicast message
  228. */
  229. memset(&msg_ucast, 0, sizeof(msg_ucast));
  230. totemip_totemip_to_sockaddr_convert(system_to,
  231. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  232. msg_ucast.msg_name = &sockaddr;
  233. msg_ucast.msg_namelen = addrlen;
  234. msg_ucast.msg_iov = (void *)&iovec;
  235. msg_ucast.msg_iovlen = 1;
  236. #ifdef HAVE_MSGHDR_CONTROL
  237. msg_ucast.msg_control = 0;
  238. #endif
  239. #ifdef HAVE_MSGHDR_CONTROLLEN
  240. msg_ucast.msg_controllen = 0;
  241. #endif
  242. #ifdef HAVE_MSGHDR_FLAGS
  243. msg_ucast.msg_flags = 0;
  244. #endif
  245. #ifdef HAVE_MSGHDR_ACCRIGHTS
  246. msg_ucast.msg_accrights = NULL;
  247. #endif
  248. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  249. msg_ucast.msg_accrightslen = 0;
  250. #endif
  251. /*
  252. * Transmit unicast message
  253. * An error here is recovered by totemsrp
  254. */
  255. res = sendmsg (instance->totemudp_sockets.mcast_send, &msg_ucast,
  256. MSG_NOSIGNAL);
  257. if (res < 0) {
  258. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  259. "sendmsg(ucast) failed (non-critical)");
  260. }
  261. }
  262. static inline void mcast_sendmsg (
  263. struct totemudp_instance *instance,
  264. const void *msg,
  265. unsigned int msg_len)
  266. {
  267. struct msghdr msg_mcast;
  268. int res = 0;
  269. size_t buf_out_len;
  270. unsigned char buf_out[FRAME_SIZE_MAX];
  271. struct iovec iovec;
  272. struct sockaddr_storage sockaddr;
  273. int addrlen;
  274. if (msg_len + crypto_get_current_sec_header_size(instance->crypto_inst) > sizeof(buf_out)) {
  275. log_printf(LOGSYS_LEVEL_CRIT, "UDP message for mcast is too big. Ignoring message");
  276. return ;
  277. }
  278. /*
  279. * Encrypt and digest the message
  280. */
  281. if (crypto_encrypt_and_sign (
  282. instance->crypto_inst,
  283. (const unsigned char *)msg,
  284. msg_len,
  285. buf_out,
  286. &buf_out_len) != 0) {
  287. log_printf(LOGSYS_LEVEL_CRIT, "Error encrypting/signing packet (non-critical)");
  288. return;
  289. }
  290. iovec.iov_base = (void *)&buf_out;
  291. iovec.iov_len = buf_out_len;
  292. /*
  293. * Build multicast message
  294. */
  295. totemip_totemip_to_sockaddr_convert(&instance->mcast_address,
  296. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  297. memset(&msg_mcast, 0, sizeof(msg_mcast));
  298. msg_mcast.msg_name = &sockaddr;
  299. msg_mcast.msg_namelen = addrlen;
  300. msg_mcast.msg_iov = (void *)&iovec;
  301. msg_mcast.msg_iovlen = 1;
  302. #ifdef HAVE_MSGHDR_CONTROL
  303. msg_mcast.msg_control = 0;
  304. #endif
  305. #ifdef HAVE_MSGHDR_CONTROLLEN
  306. msg_mcast.msg_controllen = 0;
  307. #endif
  308. #ifdef HAVE_MSGHDR_FLAGS
  309. msg_mcast.msg_flags = 0;
  310. #endif
  311. #ifdef HAVE_MSGHDR_ACCRIGHTS
  312. msg_mcast.msg_accrights = NULL;
  313. #endif
  314. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  315. msg_mcast.msg_accrightslen = 0;
  316. #endif
  317. /*
  318. * Transmit multicast message
  319. * An error here is recovered by totemsrp
  320. */
  321. res = sendmsg (instance->totemudp_sockets.mcast_send, &msg_mcast,
  322. MSG_NOSIGNAL);
  323. if (res < 0) {
  324. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  325. "sendmsg(mcast) failed (non-critical)");
  326. instance->stats->continuous_sendmsg_failures++;
  327. } else {
  328. instance->stats->continuous_sendmsg_failures = 0;
  329. }
  330. /*
  331. * Transmit multicast message to local unix mcast loop
  332. * An error here is recovered by totemsrp
  333. */
  334. msg_mcast.msg_name = NULL;
  335. msg_mcast.msg_namelen = 0;
  336. res = sendmsg (instance->totemudp_sockets.local_mcast_loop[1], &msg_mcast,
  337. MSG_NOSIGNAL);
  338. if (res < 0) {
  339. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  340. "sendmsg(local mcast loop) failed (non-critical)");
  341. }
  342. }
  343. int totemudp_finalize (
  344. void *udp_context)
  345. {
  346. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  347. int res = 0;
  348. if (instance->totemudp_sockets.mcast_recv > 0) {
  349. qb_loop_poll_del (instance->totemudp_poll_handle,
  350. instance->totemudp_sockets.mcast_recv);
  351. close (instance->totemudp_sockets.mcast_recv);
  352. }
  353. if (instance->totemudp_sockets.mcast_send > 0) {
  354. close (instance->totemudp_sockets.mcast_send);
  355. }
  356. if (instance->totemudp_sockets.local_mcast_loop[0] > 0) {
  357. qb_loop_poll_del (instance->totemudp_poll_handle,
  358. instance->totemudp_sockets.local_mcast_loop[0]);
  359. close (instance->totemudp_sockets.local_mcast_loop[0]);
  360. close (instance->totemudp_sockets.local_mcast_loop[1]);
  361. }
  362. if (instance->totemudp_sockets.token > 0) {
  363. qb_loop_poll_del (instance->totemudp_poll_handle,
  364. instance->totemudp_sockets.token);
  365. close (instance->totemudp_sockets.token);
  366. }
  367. return (res);
  368. }
  369. /*
  370. * Only designed to work with a message with one iov
  371. */
  372. static int net_deliver_fn (
  373. int fd,
  374. int revents,
  375. void *data)
  376. {
  377. struct totemudp_instance *instance = (struct totemudp_instance *)data;
  378. struct msghdr msg_recv;
  379. struct iovec *iovec;
  380. struct sockaddr_storage system_from;
  381. int bytes_received;
  382. int res = 0;
  383. int truncated_packet;
  384. if (instance->flushing == 1) {
  385. iovec = &instance->totemudp_iov_recv_flush;
  386. } else {
  387. iovec = &instance->totemudp_iov_recv;
  388. }
  389. /*
  390. * Receive datagram
  391. */
  392. msg_recv.msg_name = &system_from;
  393. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  394. msg_recv.msg_iov = iovec;
  395. msg_recv.msg_iovlen = 1;
  396. #ifdef HAVE_MSGHDR_CONTROL
  397. msg_recv.msg_control = 0;
  398. #endif
  399. #ifdef HAVE_MSGHDR_CONTROLLEN
  400. msg_recv.msg_controllen = 0;
  401. #endif
  402. #ifdef HAVE_MSGHDR_FLAGS
  403. msg_recv.msg_flags = 0;
  404. #endif
  405. #ifdef HAVE_MSGHDR_ACCRIGHTS
  406. msg_recv.msg_accrights = NULL;
  407. #endif
  408. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  409. msg_recv.msg_accrightslen = 0;
  410. #endif
  411. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  412. if (bytes_received == -1) {
  413. return (0);
  414. } else {
  415. instance->stats_recv += bytes_received;
  416. }
  417. truncated_packet = 0;
  418. #ifdef HAVE_MSGHDR_FLAGS
  419. if (msg_recv.msg_flags & MSG_TRUNC) {
  420. truncated_packet = 1;
  421. }
  422. #else
  423. /*
  424. * We don't have MSGHDR_FLAGS, but we can (hopefully) safely make assumption that
  425. * if bytes_received == FRAME_SIZE_MAX then packet is truncated
  426. */
  427. if (bytes_received == FRAME_SIZE_MAX) {
  428. truncated_packet = 1;
  429. }
  430. #endif
  431. if (truncated_packet) {
  432. log_printf(instance->totemudp_log_level_error,
  433. "Received too big message. This may be because something bad is happening"
  434. "on the network (attack?), or you tried join more nodes than corosync is"
  435. "compiled with (%u) or bug in the code (bad estimation of "
  436. "the FRAME_SIZE_MAX). Dropping packet.", PROCESSOR_COUNT_MAX);
  437. return (0);
  438. }
  439. /*
  440. * Authenticate and if authenticated, decrypt datagram
  441. */
  442. res = crypto_authenticate_and_decrypt (instance->crypto_inst, iovec->iov_base, &bytes_received);
  443. if (res == -1) {
  444. log_printf (instance->totemudp_log_level_security, "Received message has invalid digest... ignoring.");
  445. log_printf (instance->totemudp_log_level_security,
  446. "Invalid packet data");
  447. iovec->iov_len = FRAME_SIZE_MAX;
  448. return 0;
  449. }
  450. iovec->iov_len = bytes_received;
  451. /*
  452. * Handle incoming message
  453. */
  454. instance->totemudp_deliver_fn (
  455. instance->context,
  456. iovec->iov_base,
  457. iovec->iov_len);
  458. iovec->iov_len = FRAME_SIZE_MAX;
  459. return (0);
  460. }
  461. static int netif_determine (
  462. struct totemudp_instance *instance,
  463. struct totem_ip_address *bindnet,
  464. struct totem_ip_address *bound_to,
  465. int *interface_up,
  466. int *interface_num)
  467. {
  468. int res;
  469. res = totemip_iface_check (bindnet, bound_to,
  470. interface_up, interface_num,
  471. instance->totem_config->clear_node_high_bit);
  472. return (res);
  473. }
  474. /*
  475. * If the interface is up, the sockets for totem are built. If the interface is down
  476. * this function is requeued in the timer list to retry building the sockets later.
  477. */
  478. static void timer_function_netif_check_timeout (
  479. void *data)
  480. {
  481. struct totemudp_instance *instance = (struct totemudp_instance *)data;
  482. int interface_up;
  483. int interface_num;
  484. struct totem_ip_address *bind_address;
  485. /*
  486. * Build sockets for every interface
  487. */
  488. netif_determine (instance,
  489. &instance->totem_interface->bindnet,
  490. &instance->totem_interface->boundto,
  491. &interface_up, &interface_num);
  492. /*
  493. * If the network interface isn't back up and we are already
  494. * in loopback mode, add timer to check again and return
  495. */
  496. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  497. interface_up == 0) ||
  498. (instance->my_memb_entries == 1 &&
  499. instance->netif_bind_state == BIND_STATE_REGULAR &&
  500. interface_up == 1)) {
  501. qb_loop_timer_add (instance->totemudp_poll_handle,
  502. QB_LOOP_MED,
  503. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  504. (void *)instance,
  505. timer_function_netif_check_timeout,
  506. &instance->timer_netif_check_timeout);
  507. /*
  508. * Add a timer to check for a downed regular interface
  509. */
  510. return;
  511. }
  512. if (instance->totemudp_sockets.mcast_recv > 0) {
  513. qb_loop_poll_del (instance->totemudp_poll_handle,
  514. instance->totemudp_sockets.mcast_recv);
  515. close (instance->totemudp_sockets.mcast_recv);
  516. }
  517. if (instance->totemudp_sockets.mcast_send > 0) {
  518. close (instance->totemudp_sockets.mcast_send);
  519. }
  520. if (instance->totemudp_sockets.local_mcast_loop[0] > 0) {
  521. qb_loop_poll_del (instance->totemudp_poll_handle,
  522. instance->totemudp_sockets.local_mcast_loop[0]);
  523. close (instance->totemudp_sockets.local_mcast_loop[0]);
  524. close (instance->totemudp_sockets.local_mcast_loop[1]);
  525. }
  526. if (instance->totemudp_sockets.token > 0) {
  527. qb_loop_poll_del (instance->totemudp_poll_handle,
  528. instance->totemudp_sockets.token);
  529. close (instance->totemudp_sockets.token);
  530. }
  531. if (interface_up == 0) {
  532. /*
  533. * Interface is not up
  534. */
  535. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  536. bind_address = &localhost;
  537. /*
  538. * Add a timer to retry building interfaces and request memb_gather_enter
  539. */
  540. qb_loop_timer_add (instance->totemudp_poll_handle,
  541. QB_LOOP_MED,
  542. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  543. (void *)instance,
  544. timer_function_netif_check_timeout,
  545. &instance->timer_netif_check_timeout);
  546. } else {
  547. /*
  548. * Interface is up
  549. */
  550. instance->netif_bind_state = BIND_STATE_REGULAR;
  551. bind_address = &instance->totem_interface->bindnet;
  552. }
  553. /*
  554. * Create and bind the multicast and unicast sockets
  555. */
  556. (void)totemudp_build_sockets (instance,
  557. &instance->mcast_address,
  558. bind_address,
  559. &instance->totemudp_sockets,
  560. &instance->totem_interface->boundto);
  561. qb_loop_poll_add (
  562. instance->totemudp_poll_handle,
  563. QB_LOOP_MED,
  564. instance->totemudp_sockets.mcast_recv,
  565. POLLIN, instance, net_deliver_fn);
  566. qb_loop_poll_add (
  567. instance->totemudp_poll_handle,
  568. QB_LOOP_MED,
  569. instance->totemudp_sockets.local_mcast_loop[0],
  570. POLLIN, instance, net_deliver_fn);
  571. qb_loop_poll_add (
  572. instance->totemudp_poll_handle,
  573. QB_LOOP_MED,
  574. instance->totemudp_sockets.token,
  575. POLLIN, instance, net_deliver_fn);
  576. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  577. /*
  578. * This reports changes in the interface to the user and totemsrp
  579. */
  580. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  581. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  582. log_printf (instance->totemudp_log_level_notice,
  583. "The network interface [%s] is now up.",
  584. totemip_print (&instance->totem_interface->boundto));
  585. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  586. instance->totemudp_iface_change_fn (instance->context, &instance->my_id);
  587. }
  588. /*
  589. * Add a timer to check for interface going down in single membership
  590. */
  591. if (instance->my_memb_entries == 1) {
  592. qb_loop_timer_add (instance->totemudp_poll_handle,
  593. QB_LOOP_MED,
  594. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  595. (void *)instance,
  596. timer_function_netif_check_timeout,
  597. &instance->timer_netif_check_timeout);
  598. }
  599. } else {
  600. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  601. log_printf (instance->totemudp_log_level_notice,
  602. "The network interface is down.");
  603. instance->totemudp_iface_change_fn (instance->context, &instance->my_id);
  604. }
  605. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  606. }
  607. }
  608. /* Set the socket priority to INTERACTIVE to ensure
  609. that our messages don't get queued behind anything else */
  610. static void totemudp_traffic_control_set(struct totemudp_instance *instance, int sock)
  611. {
  612. #ifdef SO_PRIORITY
  613. int prio = 6; /* TC_PRIO_INTERACTIVE */
  614. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int))) {
  615. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning, "Could not set traffic priority");
  616. }
  617. #endif
  618. }
  619. static int totemudp_build_sockets_ip (
  620. struct totemudp_instance *instance,
  621. struct totem_ip_address *mcast_address,
  622. struct totem_ip_address *bindnet_address,
  623. struct totemudp_socket *sockets,
  624. struct totem_ip_address *bound_to,
  625. int interface_num)
  626. {
  627. struct sockaddr_storage sockaddr;
  628. struct ipv6_mreq mreq6;
  629. struct ip_mreq mreq;
  630. struct sockaddr_storage mcast_ss, boundto_ss;
  631. struct sockaddr_in6 *mcast_sin6 = (struct sockaddr_in6 *)&mcast_ss;
  632. struct sockaddr_in *mcast_sin = (struct sockaddr_in *)&mcast_ss;
  633. struct sockaddr_in *boundto_sin = (struct sockaddr_in *)&boundto_ss;
  634. unsigned int sendbuf_size;
  635. unsigned int recvbuf_size;
  636. unsigned int optlen = sizeof (sendbuf_size);
  637. unsigned int retries;
  638. int addrlen;
  639. int res;
  640. int flag;
  641. uint8_t sflag;
  642. int i;
  643. /*
  644. * Create multicast recv socket
  645. */
  646. sockets->mcast_recv = socket (bindnet_address->family, SOCK_DGRAM, 0);
  647. if (sockets->mcast_recv == -1) {
  648. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  649. "socket() failed");
  650. return (-1);
  651. }
  652. totemip_nosigpipe (sockets->mcast_recv);
  653. res = fcntl (sockets->mcast_recv, F_SETFL, O_NONBLOCK);
  654. if (res == -1) {
  655. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  656. "Could not set non-blocking operation on multicast socket");
  657. return (-1);
  658. }
  659. /*
  660. * Force reuse
  661. */
  662. flag = 1;
  663. if ( setsockopt(sockets->mcast_recv, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  664. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  665. "setsockopt(SO_REUSEADDR) failed");
  666. return (-1);
  667. }
  668. /*
  669. * Create local multicast loop socket
  670. */
  671. if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sockets->local_mcast_loop) == -1) {
  672. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  673. "socket() failed");
  674. return (-1);
  675. }
  676. for (i = 0; i < 2; i++) {
  677. totemip_nosigpipe (sockets->local_mcast_loop[i]);
  678. res = fcntl (sockets->local_mcast_loop[i], F_SETFL, O_NONBLOCK);
  679. if (res == -1) {
  680. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  681. "Could not set non-blocking operation on multicast socket");
  682. return (-1);
  683. }
  684. }
  685. /*
  686. * Setup mcast send socket
  687. */
  688. sockets->mcast_send = socket (bindnet_address->family, SOCK_DGRAM, 0);
  689. if (sockets->mcast_send == -1) {
  690. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  691. "socket() failed");
  692. return (-1);
  693. }
  694. totemip_nosigpipe (sockets->mcast_send);
  695. res = fcntl (sockets->mcast_send, F_SETFL, O_NONBLOCK);
  696. if (res == -1) {
  697. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  698. "Could not set non-blocking operation on multicast socket");
  699. return (-1);
  700. }
  701. /*
  702. * Force reuse
  703. */
  704. flag = 1;
  705. if ( setsockopt(sockets->mcast_send, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  706. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  707. "setsockopt(SO_REUSEADDR) failed");
  708. return (-1);
  709. }
  710. totemip_totemip_to_sockaddr_convert_with_scopeid(bound_to, instance->totem_interface->ip_port - 1,
  711. &sockaddr, &addrlen, 1);
  712. retries = 0;
  713. while (1) {
  714. res = bind (sockets->mcast_send, (struct sockaddr *)&sockaddr, addrlen);
  715. if (res == 0) {
  716. break;
  717. }
  718. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  719. "Unable to bind the socket to send multicast packets");
  720. if (++retries > BIND_MAX_RETRIES) {
  721. break;
  722. }
  723. /*
  724. * Wait for a while
  725. */
  726. (void)poll(NULL, 0, BIND_RETRIES_INTERVAL * retries);
  727. }
  728. if (res == -1) {
  729. return (-1);
  730. }
  731. /*
  732. * Setup unicast socket
  733. */
  734. sockets->token = socket (bindnet_address->family, SOCK_DGRAM, 0);
  735. if (sockets->token == -1) {
  736. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  737. "socket() failed");
  738. return (-1);
  739. }
  740. totemip_nosigpipe (sockets->token);
  741. res = fcntl (sockets->token, F_SETFL, O_NONBLOCK);
  742. if (res == -1) {
  743. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  744. "Could not set non-blocking operation on token socket");
  745. return (-1);
  746. }
  747. /*
  748. * Force reuse
  749. */
  750. flag = 1;
  751. if ( setsockopt(sockets->token, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  752. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  753. "setsockopt(SO_REUSEADDR) failed");
  754. return (-1);
  755. }
  756. /*
  757. * Bind to unicast socket used for token send/receives
  758. * This has the side effect of binding to the correct interface
  759. */
  760. totemip_totemip_to_sockaddr_convert_with_scopeid(bound_to, instance->totem_interface->ip_port,
  761. &sockaddr, &addrlen, 1);
  762. retries = 0;
  763. while (1) {
  764. res = bind (sockets->token, (struct sockaddr *)&sockaddr, addrlen);
  765. if (res == 0) {
  766. break;
  767. }
  768. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  769. "Unable to bind UDP unicast socket");
  770. if (++retries > BIND_MAX_RETRIES) {
  771. break;
  772. }
  773. /*
  774. * Wait for a while
  775. */
  776. (void)poll(NULL, 0, BIND_RETRIES_INTERVAL * retries);
  777. }
  778. if (res == -1) {
  779. return (-1);
  780. }
  781. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  782. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  783. /*
  784. * Set buffer sizes to avoid overruns
  785. */
  786. res = setsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  787. if (res == -1) {
  788. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  789. "Unable to set SO_RCVBUF size on UDP mcast socket");
  790. return (-1);
  791. }
  792. res = setsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  793. if (res == -1) {
  794. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  795. "Unable to set SO_SNDBUF size on UDP mcast socket");
  796. return (-1);
  797. }
  798. res = setsockopt (sockets->local_mcast_loop[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  799. if (res == -1) {
  800. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  801. "Unable to set SO_RCVBUF size on UDP local mcast loop socket");
  802. return (-1);
  803. }
  804. res = setsockopt (sockets->local_mcast_loop[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  805. if (res == -1) {
  806. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  807. "Unable to set SO_SNDBUF size on UDP local mcast loop socket");
  808. return (-1);
  809. }
  810. res = getsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  811. if (res == 0) {
  812. log_printf (instance->totemudp_log_level_debug,
  813. "Receive multicast socket recv buffer size (%d bytes).", recvbuf_size);
  814. }
  815. res = getsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  816. if (res == 0) {
  817. log_printf (instance->totemudp_log_level_debug,
  818. "Transmit multicast socket send buffer size (%d bytes).", sendbuf_size);
  819. }
  820. res = getsockopt (sockets->local_mcast_loop[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  821. if (res == 0) {
  822. log_printf (instance->totemudp_log_level_debug,
  823. "Local receive multicast loop socket recv buffer size (%d bytes).", recvbuf_size);
  824. }
  825. res = getsockopt (sockets->local_mcast_loop[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  826. if (res == 0) {
  827. log_printf (instance->totemudp_log_level_debug,
  828. "Local transmit multicast loop socket send buffer size (%d bytes).", sendbuf_size);
  829. }
  830. /*
  831. * Join group membership on socket
  832. */
  833. totemip_totemip_to_sockaddr_convert(mcast_address, instance->totem_interface->ip_port, &mcast_ss, &addrlen);
  834. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &boundto_ss, &addrlen);
  835. if (instance->totem_config->broadcast_use == 1) {
  836. unsigned int broadcast = 1;
  837. if ((setsockopt(sockets->mcast_recv, SOL_SOCKET,
  838. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  839. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  840. "setting broadcast option failed");
  841. return (-1);
  842. }
  843. if ((setsockopt(sockets->mcast_send, SOL_SOCKET,
  844. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  845. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  846. "setting broadcast option failed");
  847. return (-1);
  848. }
  849. } else {
  850. switch (bindnet_address->family) {
  851. case AF_INET:
  852. memset(&mreq, 0, sizeof(mreq));
  853. mreq.imr_multiaddr.s_addr = mcast_sin->sin_addr.s_addr;
  854. mreq.imr_interface.s_addr = boundto_sin->sin_addr.s_addr;
  855. res = setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  856. &mreq, sizeof (mreq));
  857. if (res == -1) {
  858. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  859. "join ipv4 multicast group failed");
  860. return (-1);
  861. }
  862. break;
  863. case AF_INET6:
  864. memset(&mreq6, 0, sizeof(mreq6));
  865. memcpy(&mreq6.ipv6mr_multiaddr, &mcast_sin6->sin6_addr, sizeof(struct in6_addr));
  866. mreq6.ipv6mr_interface = interface_num;
  867. res = setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_JOIN_GROUP,
  868. &mreq6, sizeof (mreq6));
  869. if (res == -1) {
  870. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  871. "join ipv6 multicast group failed");
  872. return (-1);
  873. }
  874. break;
  875. }
  876. }
  877. /*
  878. * Turn off multicast loopback
  879. */
  880. flag = 0;
  881. switch ( bindnet_address->family ) {
  882. case AF_INET:
  883. sflag = 0;
  884. res = setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_LOOP,
  885. &sflag, sizeof (sflag));
  886. break;
  887. case AF_INET6:
  888. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
  889. &flag, sizeof (flag));
  890. }
  891. if (res == -1) {
  892. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  893. "Unable to turn off multicast loopback");
  894. return (-1);
  895. }
  896. /*
  897. * Set multicast packets TTL
  898. */
  899. flag = instance->totem_interface->ttl;
  900. if (bindnet_address->family == AF_INET6) {
  901. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
  902. &flag, sizeof (flag));
  903. if (res == -1) {
  904. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  905. "set mcast v6 TTL failed");
  906. return (-1);
  907. }
  908. } else {
  909. sflag = flag;
  910. res = setsockopt(sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_TTL,
  911. &sflag, sizeof(sflag));
  912. if (res == -1) {
  913. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  914. "set mcast v4 TTL failed");
  915. return (-1);
  916. }
  917. }
  918. /*
  919. * Bind to a specific interface for multicast send and receive
  920. */
  921. switch ( bindnet_address->family ) {
  922. case AF_INET:
  923. if (setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_IF,
  924. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  925. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  926. "cannot select interface for multicast packets (send)");
  927. return (-1);
  928. }
  929. if (setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_MULTICAST_IF,
  930. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  931. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  932. "cannot select interface for multicast packets (recv)");
  933. return (-1);
  934. }
  935. break;
  936. case AF_INET6:
  937. if (setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  938. &interface_num, sizeof (interface_num)) < 0) {
  939. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  940. "cannot select interface for multicast packets (send v6)");
  941. return (-1);
  942. }
  943. if (setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  944. &interface_num, sizeof (interface_num)) < 0) {
  945. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  946. "cannot select interface for multicast packets (recv v6)");
  947. return (-1);
  948. }
  949. break;
  950. }
  951. /*
  952. * Bind to multicast socket used for multicast receives
  953. * This needs to happen after all of the multicast setsockopt() calls
  954. * as the kernel seems to only put them into effect (for IPV6) when bind()
  955. * is called.
  956. */
  957. totemip_totemip_to_sockaddr_convert(mcast_address,
  958. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  959. retries = 0;
  960. while (1) {
  961. res = bind (sockets->mcast_recv, (struct sockaddr *)&sockaddr, addrlen);
  962. if (res == 0) {
  963. break;
  964. }
  965. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  966. "Unable to bind the socket to receive multicast packets");
  967. if (++retries > BIND_MAX_RETRIES) {
  968. break;
  969. }
  970. /*
  971. * Wait for a while
  972. */
  973. (void)poll(NULL, 0, BIND_RETRIES_INTERVAL * retries);
  974. }
  975. if (res == -1) {
  976. return (-1);
  977. }
  978. return 0;
  979. }
  980. static int totemudp_build_sockets (
  981. struct totemudp_instance *instance,
  982. struct totem_ip_address *mcast_address,
  983. struct totem_ip_address *bindnet_address,
  984. struct totemudp_socket *sockets,
  985. struct totem_ip_address *bound_to)
  986. {
  987. int interface_num;
  988. int interface_up;
  989. int res;
  990. /*
  991. * Determine the ip address bound to and the interface name
  992. */
  993. res = netif_determine (instance,
  994. bindnet_address,
  995. bound_to,
  996. &interface_up,
  997. &interface_num);
  998. if (res == -1) {
  999. return (-1);
  1000. }
  1001. totemip_copy(&instance->my_id, bound_to);
  1002. res = totemudp_build_sockets_ip (instance, mcast_address,
  1003. bindnet_address, sockets, bound_to, interface_num);
  1004. if (res == -1) {
  1005. /* if we get here, corosync won't work anyway, so better leaving than faking to work */
  1006. LOGSYS_PERROR (errno, instance->totemudp_log_level_error,
  1007. "Unable to create sockets, exiting");
  1008. exit(EXIT_FAILURE);
  1009. }
  1010. /* We only send out of the token socket */
  1011. totemudp_traffic_control_set(instance, sockets->token);
  1012. return res;
  1013. }
  1014. /*
  1015. * Totem Network interface - also does encryption/decryption
  1016. * depends on poll abstraction, POSIX, IPV4
  1017. */
  1018. /*
  1019. * Create an instance
  1020. */
  1021. int totemudp_initialize (
  1022. qb_loop_t *poll_handle,
  1023. void **udp_context,
  1024. struct totem_config *totem_config,
  1025. totemsrp_stats_t *stats,
  1026. int interface_no,
  1027. void *context,
  1028. void (*deliver_fn) (
  1029. void *context,
  1030. const void *msg,
  1031. unsigned int msg_len),
  1032. void (*iface_change_fn) (
  1033. void *context,
  1034. const struct totem_ip_address *iface_address),
  1035. void (*target_set_completed) (
  1036. void *context))
  1037. {
  1038. struct totemudp_instance *instance;
  1039. instance = malloc (sizeof (struct totemudp_instance));
  1040. if (instance == NULL) {
  1041. return (-1);
  1042. }
  1043. totemudp_instance_initialize (instance);
  1044. instance->totem_config = totem_config;
  1045. instance->stats = stats;
  1046. /*
  1047. * Configure logging
  1048. */
  1049. instance->totemudp_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  1050. instance->totemudp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  1051. instance->totemudp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  1052. instance->totemudp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  1053. instance->totemudp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  1054. instance->totemudp_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1055. instance->totemudp_log_printf = totem_config->totem_logging_configuration.log_printf;
  1056. /*
  1057. * Initialize random number generator for later use to generate salt
  1058. */
  1059. instance->crypto_inst = crypto_init (totem_config->private_key,
  1060. totem_config->private_key_len,
  1061. totem_config->crypto_cipher_type,
  1062. totem_config->crypto_hash_type,
  1063. instance->totemudp_log_printf,
  1064. instance->totemudp_log_level_security,
  1065. instance->totemudp_log_level_notice,
  1066. instance->totemudp_log_level_error,
  1067. instance->totemudp_subsys_id);
  1068. if (instance->crypto_inst == NULL) {
  1069. free(instance);
  1070. return (-1);
  1071. }
  1072. /*
  1073. * Initialize local variables for totemudp
  1074. */
  1075. instance->totem_interface = &totem_config->interfaces[interface_no];
  1076. totemip_copy (&instance->mcast_address, &instance->totem_interface->mcast_addr);
  1077. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  1078. instance->totemudp_poll_handle = poll_handle;
  1079. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  1080. instance->context = context;
  1081. instance->totemudp_deliver_fn = deliver_fn;
  1082. instance->totemudp_iface_change_fn = iface_change_fn;
  1083. instance->totemudp_target_set_completed = target_set_completed;
  1084. totemip_localhost (instance->mcast_address.family, &localhost);
  1085. localhost.nodeid = instance->totem_config->node_id;
  1086. /*
  1087. * RRP layer isn't ready to receive message because it hasn't
  1088. * initialized yet. Add short timer to check the interfaces.
  1089. */
  1090. qb_loop_timer_add (instance->totemudp_poll_handle,
  1091. QB_LOOP_MED,
  1092. 100*QB_TIME_NS_IN_MSEC,
  1093. (void *)instance,
  1094. timer_function_netif_check_timeout,
  1095. &instance->timer_netif_check_timeout);
  1096. *udp_context = instance;
  1097. return (0);
  1098. }
  1099. void *totemudp_buffer_alloc (void)
  1100. {
  1101. return malloc (FRAME_SIZE_MAX);
  1102. }
  1103. void totemudp_buffer_release (void *ptr)
  1104. {
  1105. return free (ptr);
  1106. }
  1107. int totemudp_processor_count_set (
  1108. void *udp_context,
  1109. int processor_count)
  1110. {
  1111. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1112. int res = 0;
  1113. instance->my_memb_entries = processor_count;
  1114. qb_loop_timer_del (instance->totemudp_poll_handle,
  1115. instance->timer_netif_check_timeout);
  1116. if (processor_count == 1) {
  1117. qb_loop_timer_add (instance->totemudp_poll_handle,
  1118. QB_LOOP_MED,
  1119. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  1120. (void *)instance,
  1121. timer_function_netif_check_timeout,
  1122. &instance->timer_netif_check_timeout);
  1123. }
  1124. return (res);
  1125. }
  1126. int totemudp_recv_flush (void *udp_context)
  1127. {
  1128. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1129. struct pollfd ufd;
  1130. int nfds;
  1131. int res = 0;
  1132. int i;
  1133. int sock;
  1134. instance->flushing = 1;
  1135. for (i = 0; i < 2; i++) {
  1136. sock = -1;
  1137. if (i == 0) {
  1138. sock = instance->totemudp_sockets.mcast_recv;
  1139. }
  1140. if (i == 1) {
  1141. sock = instance->totemudp_sockets.local_mcast_loop[0];
  1142. }
  1143. assert(sock != -1);
  1144. do {
  1145. ufd.fd = sock;
  1146. ufd.events = POLLIN;
  1147. nfds = poll (&ufd, 1, 0);
  1148. if (nfds == 1 && ufd.revents & POLLIN) {
  1149. net_deliver_fn (sock, ufd.revents, instance);
  1150. }
  1151. } while (nfds == 1);
  1152. }
  1153. instance->flushing = 0;
  1154. return (res);
  1155. }
  1156. int totemudp_send_flush (void *udp_context)
  1157. {
  1158. return 0;
  1159. }
  1160. int totemudp_token_send (
  1161. void *udp_context,
  1162. const void *msg,
  1163. unsigned int msg_len)
  1164. {
  1165. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1166. int res = 0;
  1167. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  1168. return (res);
  1169. }
  1170. int totemudp_mcast_flush_send (
  1171. void *udp_context,
  1172. const void *msg,
  1173. unsigned int msg_len)
  1174. {
  1175. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1176. int res = 0;
  1177. mcast_sendmsg (instance, msg, msg_len);
  1178. return (res);
  1179. }
  1180. int totemudp_mcast_noflush_send (
  1181. void *udp_context,
  1182. const void *msg,
  1183. unsigned int msg_len)
  1184. {
  1185. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1186. int res = 0;
  1187. mcast_sendmsg (instance, msg, msg_len);
  1188. return (res);
  1189. }
  1190. extern int totemudp_iface_check (void *udp_context)
  1191. {
  1192. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1193. int res = 0;
  1194. timer_function_netif_check_timeout (instance);
  1195. return (res);
  1196. }
  1197. extern void totemudp_net_mtu_adjust (void *udp_context, struct totem_config *totem_config)
  1198. {
  1199. assert(totem_config->interface_count > 0);
  1200. totem_config->net_mtu -= crypto_sec_header_size(totem_config->crypto_cipher_type,
  1201. totem_config->crypto_hash_type) +
  1202. totemip_udpip_header_size(totem_config->interfaces[0].bindnet.family);
  1203. }
  1204. const char *totemudp_iface_print (void *udp_context) {
  1205. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1206. const char *ret_char;
  1207. ret_char = totemip_print (&instance->my_id);
  1208. return (ret_char);
  1209. }
  1210. int totemudp_iface_get (
  1211. void *udp_context,
  1212. struct totem_ip_address *addr)
  1213. {
  1214. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1215. int res = 0;
  1216. memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
  1217. return (res);
  1218. }
  1219. int totemudp_token_target_set (
  1220. void *udp_context,
  1221. const struct totem_ip_address *token_target)
  1222. {
  1223. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1224. int res = 0;
  1225. memcpy (&instance->token_target, token_target,
  1226. sizeof (struct totem_ip_address));
  1227. instance->totemudp_target_set_completed (instance->context);
  1228. return (res);
  1229. }
  1230. extern int totemudp_recv_mcast_empty (
  1231. void *udp_context)
  1232. {
  1233. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1234. unsigned int res;
  1235. struct sockaddr_storage system_from;
  1236. struct msghdr msg_recv;
  1237. struct pollfd ufd;
  1238. int nfds;
  1239. int msg_processed = 0;
  1240. int i;
  1241. int sock;
  1242. /*
  1243. * Receive datagram
  1244. */
  1245. msg_recv.msg_name = &system_from;
  1246. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  1247. msg_recv.msg_iov = &instance->totemudp_iov_recv_flush;
  1248. msg_recv.msg_iovlen = 1;
  1249. #ifdef HAVE_MSGHDR_CONTROL
  1250. msg_recv.msg_control = 0;
  1251. #endif
  1252. #ifdef HAVE_MSGHDR_CONTROLLEN
  1253. msg_recv.msg_controllen = 0;
  1254. #endif
  1255. #ifdef HAVE_MSGHDR_FLAGS
  1256. msg_recv.msg_flags = 0;
  1257. #endif
  1258. #ifdef HAVE_MSGHDR_ACCRIGHTS
  1259. msg_recv.msg_accrights = NULL;
  1260. #endif
  1261. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  1262. msg_recv.msg_accrightslen = 0;
  1263. #endif
  1264. for (i = 0; i < 2; i++) {
  1265. sock = -1;
  1266. if (i == 0) {
  1267. sock = instance->totemudp_sockets.mcast_recv;
  1268. }
  1269. if (i == 1) {
  1270. sock = instance->totemudp_sockets.local_mcast_loop[0];
  1271. }
  1272. assert(sock != -1);
  1273. do {
  1274. ufd.fd = sock;
  1275. ufd.events = POLLIN;
  1276. nfds = poll (&ufd, 1, 0);
  1277. if (nfds == 1 && ufd.revents & POLLIN) {
  1278. res = recvmsg (sock, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  1279. if (res != -1) {
  1280. msg_processed = 1;
  1281. } else {
  1282. msg_processed = -1;
  1283. }
  1284. }
  1285. } while (nfds == 1);
  1286. }
  1287. return (msg_processed);
  1288. }