totemudp.c 38 KB

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