totemudp.c 38 KB

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