totemudp.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424
  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. iovec->iov_len = FRAME_SIZE_MAX;
  429. return (0);
  430. }
  431. /*
  432. * Handle incoming message
  433. */
  434. instance->totemudp_deliver_fn (
  435. instance->context,
  436. iovec->iov_base,
  437. iovec->iov_len);
  438. iovec->iov_len = FRAME_SIZE_MAX;
  439. return (0);
  440. }
  441. static int netif_determine (
  442. struct totemudp_instance *instance,
  443. struct totem_ip_address *bindnet,
  444. struct totem_ip_address *bound_to,
  445. int *interface_up,
  446. int *interface_num)
  447. {
  448. int res;
  449. res = totemip_iface_check (bindnet, bound_to,
  450. interface_up, interface_num,
  451. instance->totem_config->clear_node_high_bit);
  452. return (res);
  453. }
  454. /*
  455. * If the interface is up, the sockets for totem are built. If the interface is down
  456. * this function is requeued in the timer list to retry building the sockets later.
  457. */
  458. static void timer_function_netif_check_timeout (
  459. void *data)
  460. {
  461. struct totemudp_instance *instance = (struct totemudp_instance *)data;
  462. int interface_up;
  463. int interface_num;
  464. struct totem_ip_address *bind_address;
  465. /*
  466. * Build sockets for every interface
  467. */
  468. netif_determine (instance,
  469. &instance->totem_interface->bindnet,
  470. &instance->totem_interface->boundto,
  471. &interface_up, &interface_num);
  472. /*
  473. * If the network interface isn't back up and we are already
  474. * in loopback mode, add timer to check again and return
  475. */
  476. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  477. interface_up == 0) ||
  478. (instance->my_memb_entries == 1 &&
  479. instance->netif_bind_state == BIND_STATE_REGULAR &&
  480. interface_up == 1)) {
  481. qb_loop_timer_add (instance->totemudp_poll_handle,
  482. QB_LOOP_MED,
  483. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  484. (void *)instance,
  485. timer_function_netif_check_timeout,
  486. &instance->timer_netif_check_timeout);
  487. /*
  488. * Add a timer to check for a downed regular interface
  489. */
  490. return;
  491. }
  492. if (instance->totemudp_sockets.mcast_recv > 0) {
  493. qb_loop_poll_del (instance->totemudp_poll_handle,
  494. instance->totemudp_sockets.mcast_recv);
  495. close (instance->totemudp_sockets.mcast_recv);
  496. }
  497. if (instance->totemudp_sockets.mcast_send > 0) {
  498. close (instance->totemudp_sockets.mcast_send);
  499. }
  500. if (instance->totemudp_sockets.local_mcast_loop[0] > 0) {
  501. qb_loop_poll_del (instance->totemudp_poll_handle,
  502. instance->totemudp_sockets.local_mcast_loop[0]);
  503. close (instance->totemudp_sockets.local_mcast_loop[0]);
  504. close (instance->totemudp_sockets.local_mcast_loop[1]);
  505. }
  506. if (instance->totemudp_sockets.token > 0) {
  507. qb_loop_poll_del (instance->totemudp_poll_handle,
  508. instance->totemudp_sockets.token);
  509. close (instance->totemudp_sockets.token);
  510. }
  511. if (interface_up == 0) {
  512. /*
  513. * Interface is not up
  514. */
  515. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  516. bind_address = &localhost;
  517. /*
  518. * Add a timer to retry building interfaces and request memb_gather_enter
  519. */
  520. qb_loop_timer_add (instance->totemudp_poll_handle,
  521. QB_LOOP_MED,
  522. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  523. (void *)instance,
  524. timer_function_netif_check_timeout,
  525. &instance->timer_netif_check_timeout);
  526. } else {
  527. /*
  528. * Interface is up
  529. */
  530. instance->netif_bind_state = BIND_STATE_REGULAR;
  531. bind_address = &instance->totem_interface->bindnet;
  532. }
  533. /*
  534. * Create and bind the multicast and unicast sockets
  535. */
  536. (void)totemudp_build_sockets (instance,
  537. &instance->mcast_address,
  538. bind_address,
  539. &instance->totemudp_sockets,
  540. &instance->totem_interface->boundto);
  541. qb_loop_poll_add (
  542. instance->totemudp_poll_handle,
  543. QB_LOOP_MED,
  544. instance->totemudp_sockets.mcast_recv,
  545. POLLIN, instance, net_deliver_fn);
  546. qb_loop_poll_add (
  547. instance->totemudp_poll_handle,
  548. QB_LOOP_MED,
  549. instance->totemudp_sockets.local_mcast_loop[0],
  550. POLLIN, instance, net_deliver_fn);
  551. qb_loop_poll_add (
  552. instance->totemudp_poll_handle,
  553. QB_LOOP_MED,
  554. instance->totemudp_sockets.token,
  555. POLLIN, instance, net_deliver_fn);
  556. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  557. /*
  558. * This reports changes in the interface to the user and totemsrp
  559. */
  560. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  561. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  562. log_printf (instance->totemudp_log_level_notice,
  563. "The network interface [%s] is now up.",
  564. totemip_print (&instance->totem_interface->boundto));
  565. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  566. instance->totemudp_iface_change_fn (instance->context, &instance->my_id);
  567. }
  568. /*
  569. * Add a timer to check for interface going down in single membership
  570. */
  571. if (instance->my_memb_entries == 1) {
  572. qb_loop_timer_add (instance->totemudp_poll_handle,
  573. QB_LOOP_MED,
  574. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  575. (void *)instance,
  576. timer_function_netif_check_timeout,
  577. &instance->timer_netif_check_timeout);
  578. }
  579. } else {
  580. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  581. log_printf (instance->totemudp_log_level_notice,
  582. "The network interface is down.");
  583. instance->totemudp_iface_change_fn (instance->context, &instance->my_id);
  584. }
  585. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  586. }
  587. }
  588. /* Set the socket priority to INTERACTIVE to ensure
  589. that our messages don't get queued behind anything else */
  590. static void totemudp_traffic_control_set(struct totemudp_instance *instance, int sock)
  591. {
  592. #ifdef SO_PRIORITY
  593. int prio = 6; /* TC_PRIO_INTERACTIVE */
  594. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int))) {
  595. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning, "Could not set traffic priority");
  596. }
  597. #endif
  598. }
  599. static int totemudp_build_sockets_ip (
  600. struct totemudp_instance *instance,
  601. struct totem_ip_address *mcast_address,
  602. struct totem_ip_address *bindnet_address,
  603. struct totemudp_socket *sockets,
  604. struct totem_ip_address *bound_to,
  605. int interface_num)
  606. {
  607. struct sockaddr_storage sockaddr;
  608. struct ipv6_mreq mreq6;
  609. struct ip_mreq mreq;
  610. struct sockaddr_storage mcast_ss, boundto_ss;
  611. struct sockaddr_in6 *mcast_sin6 = (struct sockaddr_in6 *)&mcast_ss;
  612. struct sockaddr_in *mcast_sin = (struct sockaddr_in *)&mcast_ss;
  613. struct sockaddr_in *boundto_sin = (struct sockaddr_in *)&boundto_ss;
  614. unsigned int sendbuf_size;
  615. unsigned int recvbuf_size;
  616. unsigned int optlen = sizeof (sendbuf_size);
  617. int addrlen;
  618. int res;
  619. int flag;
  620. uint8_t sflag;
  621. int i;
  622. /*
  623. * Create multicast recv socket
  624. */
  625. sockets->mcast_recv = socket (bindnet_address->family, SOCK_DGRAM, 0);
  626. if (sockets->mcast_recv == -1) {
  627. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  628. "socket() failed");
  629. return (-1);
  630. }
  631. totemip_nosigpipe (sockets->mcast_recv);
  632. res = fcntl (sockets->mcast_recv, F_SETFL, O_NONBLOCK);
  633. if (res == -1) {
  634. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  635. "Could not set non-blocking operation on multicast socket");
  636. return (-1);
  637. }
  638. /*
  639. * Force reuse
  640. */
  641. flag = 1;
  642. if ( setsockopt(sockets->mcast_recv, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  643. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  644. "setsockopt(SO_REUSEADDR) failed");
  645. return (-1);
  646. }
  647. /*
  648. * Bind to multicast socket used for multicast receives
  649. */
  650. totemip_totemip_to_sockaddr_convert(mcast_address,
  651. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  652. res = bind (sockets->mcast_recv, (struct sockaddr *)&sockaddr, addrlen);
  653. if (res == -1) {
  654. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  655. "Unable to bind the socket to receive multicast packets");
  656. return (-1);
  657. }
  658. /*
  659. * Create local multicast loop socket
  660. */
  661. if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sockets->local_mcast_loop) == -1) {
  662. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  663. "socket() failed");
  664. return (-1);
  665. }
  666. for (i = 0; i < 2; i++) {
  667. totemip_nosigpipe (sockets->local_mcast_loop[i]);
  668. res = fcntl (sockets->local_mcast_loop[i], F_SETFL, O_NONBLOCK);
  669. if (res == -1) {
  670. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  671. "Could not set non-blocking operation on multicast socket");
  672. return (-1);
  673. }
  674. }
  675. /*
  676. * Setup mcast send socket
  677. */
  678. sockets->mcast_send = socket (bindnet_address->family, SOCK_DGRAM, 0);
  679. if (sockets->mcast_send == -1) {
  680. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  681. "socket() failed");
  682. return (-1);
  683. }
  684. totemip_nosigpipe (sockets->mcast_send);
  685. res = fcntl (sockets->mcast_send, F_SETFL, O_NONBLOCK);
  686. if (res == -1) {
  687. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  688. "Could not set non-blocking operation on multicast socket");
  689. return (-1);
  690. }
  691. /*
  692. * Force reuse
  693. */
  694. flag = 1;
  695. if ( setsockopt(sockets->mcast_send, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  696. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  697. "setsockopt(SO_REUSEADDR) failed");
  698. return (-1);
  699. }
  700. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port - 1,
  701. &sockaddr, &addrlen);
  702. res = bind (sockets->mcast_send, (struct sockaddr *)&sockaddr, addrlen);
  703. if (res == -1) {
  704. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  705. "Unable to bind the socket to send multicast packets");
  706. return (-1);
  707. }
  708. /*
  709. * Setup unicast socket
  710. */
  711. sockets->token = socket (bindnet_address->family, SOCK_DGRAM, 0);
  712. if (sockets->token == -1) {
  713. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  714. "socket() failed");
  715. return (-1);
  716. }
  717. totemip_nosigpipe (sockets->token);
  718. res = fcntl (sockets->token, F_SETFL, O_NONBLOCK);
  719. if (res == -1) {
  720. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  721. "Could not set non-blocking operation on token socket");
  722. return (-1);
  723. }
  724. /*
  725. * Force reuse
  726. */
  727. flag = 1;
  728. if ( setsockopt(sockets->token, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  729. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  730. "setsockopt(SO_REUSEADDR) failed");
  731. return (-1);
  732. }
  733. /*
  734. * Bind to unicast socket used for token send/receives
  735. * This has the side effect of binding to the correct interface
  736. */
  737. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  738. res = bind (sockets->token, (struct sockaddr *)&sockaddr, addrlen);
  739. if (res == -1) {
  740. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  741. "Unable to bind UDP unicast socket");
  742. return (-1);
  743. }
  744. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  745. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  746. /*
  747. * Set buffer sizes to avoid overruns
  748. */
  749. res = setsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  750. if (res == -1) {
  751. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  752. "Unable to set SO_RCVBUF size on UDP mcast socket");
  753. return (-1);
  754. }
  755. res = setsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  756. if (res == -1) {
  757. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  758. "Unable to set SO_SNDBUF size on UDP mcast socket");
  759. return (-1);
  760. }
  761. res = setsockopt (sockets->local_mcast_loop[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  762. if (res == -1) {
  763. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  764. "Unable to set SO_RCVBUF size on UDP local mcast loop socket");
  765. return (-1);
  766. }
  767. res = setsockopt (sockets->local_mcast_loop[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  768. if (res == -1) {
  769. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  770. "Unable to set SO_SNDBUF size on UDP local mcast loop socket");
  771. return (-1);
  772. }
  773. res = getsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  774. if (res == 0) {
  775. log_printf (instance->totemudp_log_level_debug,
  776. "Receive multicast socket recv buffer size (%d bytes).", recvbuf_size);
  777. }
  778. res = getsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  779. if (res == 0) {
  780. log_printf (instance->totemudp_log_level_debug,
  781. "Transmit multicast socket send buffer size (%d bytes).", sendbuf_size);
  782. }
  783. res = getsockopt (sockets->local_mcast_loop[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  784. if (res == 0) {
  785. log_printf (instance->totemudp_log_level_debug,
  786. "Local receive multicast loop socket recv buffer size (%d bytes).", recvbuf_size);
  787. }
  788. res = getsockopt (sockets->local_mcast_loop[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  789. if (res == 0) {
  790. log_printf (instance->totemudp_log_level_debug,
  791. "Local transmit multicast loop socket send buffer size (%d bytes).", sendbuf_size);
  792. }
  793. /*
  794. * Join group membership on socket
  795. */
  796. totemip_totemip_to_sockaddr_convert(mcast_address, instance->totem_interface->ip_port, &mcast_ss, &addrlen);
  797. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &boundto_ss, &addrlen);
  798. if (instance->totem_config->broadcast_use == 1) {
  799. unsigned int broadcast = 1;
  800. if ((setsockopt(sockets->mcast_recv, SOL_SOCKET,
  801. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  802. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  803. "setting broadcast option failed");
  804. return (-1);
  805. }
  806. if ((setsockopt(sockets->mcast_send, SOL_SOCKET,
  807. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  808. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  809. "setting broadcast option failed");
  810. return (-1);
  811. }
  812. } else {
  813. switch (bindnet_address->family) {
  814. case AF_INET:
  815. memset(&mreq, 0, sizeof(mreq));
  816. mreq.imr_multiaddr.s_addr = mcast_sin->sin_addr.s_addr;
  817. mreq.imr_interface.s_addr = boundto_sin->sin_addr.s_addr;
  818. res = setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  819. &mreq, sizeof (mreq));
  820. if (res == -1) {
  821. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  822. "join ipv4 multicast group failed");
  823. return (-1);
  824. }
  825. break;
  826. case AF_INET6:
  827. memset(&mreq6, 0, sizeof(mreq6));
  828. memcpy(&mreq6.ipv6mr_multiaddr, &mcast_sin6->sin6_addr, sizeof(struct in6_addr));
  829. mreq6.ipv6mr_interface = interface_num;
  830. res = setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_JOIN_GROUP,
  831. &mreq6, sizeof (mreq6));
  832. if (res == -1) {
  833. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  834. "join ipv6 multicast group failed");
  835. return (-1);
  836. }
  837. break;
  838. }
  839. }
  840. /*
  841. * Turn off multicast loopback
  842. */
  843. flag = 0;
  844. switch ( bindnet_address->family ) {
  845. case AF_INET:
  846. sflag = 0;
  847. res = setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_LOOP,
  848. &sflag, sizeof (sflag));
  849. break;
  850. case AF_INET6:
  851. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
  852. &flag, sizeof (flag));
  853. }
  854. if (res == -1) {
  855. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  856. "Unable to turn off multicast loopback");
  857. return (-1);
  858. }
  859. /*
  860. * Set multicast packets TTL
  861. */
  862. flag = instance->totem_interface->ttl;
  863. if (bindnet_address->family == AF_INET6) {
  864. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
  865. &flag, sizeof (flag));
  866. if (res == -1) {
  867. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  868. "set mcast v6 TTL failed");
  869. return (-1);
  870. }
  871. } else {
  872. sflag = flag;
  873. res = setsockopt(sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_TTL,
  874. &sflag, sizeof(sflag));
  875. if (res == -1) {
  876. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  877. "set mcast v4 TTL failed");
  878. return (-1);
  879. }
  880. }
  881. /*
  882. * Bind to a specific interface for multicast send and receive
  883. */
  884. switch ( bindnet_address->family ) {
  885. case AF_INET:
  886. if (setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_IF,
  887. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  888. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  889. "cannot select interface for multicast packets (send)");
  890. return (-1);
  891. }
  892. if (setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_MULTICAST_IF,
  893. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  894. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  895. "cannot select interface for multicast packets (recv)");
  896. return (-1);
  897. }
  898. break;
  899. case AF_INET6:
  900. if (setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  901. &interface_num, sizeof (interface_num)) < 0) {
  902. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  903. "cannot select interface for multicast packets (send v6)");
  904. return (-1);
  905. }
  906. if (setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  907. &interface_num, sizeof (interface_num)) < 0) {
  908. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  909. "cannot select interface for multicast packets (recv v6)");
  910. return (-1);
  911. }
  912. break;
  913. }
  914. return 0;
  915. }
  916. static int totemudp_build_sockets (
  917. struct totemudp_instance *instance,
  918. struct totem_ip_address *mcast_address,
  919. struct totem_ip_address *bindnet_address,
  920. struct totemudp_socket *sockets,
  921. struct totem_ip_address *bound_to)
  922. {
  923. int interface_num;
  924. int interface_up;
  925. int res;
  926. /*
  927. * Determine the ip address bound to and the interface name
  928. */
  929. res = netif_determine (instance,
  930. bindnet_address,
  931. bound_to,
  932. &interface_up,
  933. &interface_num);
  934. if (res == -1) {
  935. return (-1);
  936. }
  937. totemip_copy(&instance->my_id, bound_to);
  938. res = totemudp_build_sockets_ip (instance, mcast_address,
  939. bindnet_address, sockets, bound_to, interface_num);
  940. /* We only send out of the token socket */
  941. totemudp_traffic_control_set(instance, sockets->token);
  942. return res;
  943. }
  944. /*
  945. * Totem Network interface - also does encryption/decryption
  946. * depends on poll abstraction, POSIX, IPV4
  947. */
  948. /*
  949. * Create an instance
  950. */
  951. int totemudp_initialize (
  952. qb_loop_t *poll_handle,
  953. void **udp_context,
  954. struct totem_config *totem_config,
  955. totemsrp_stats_t *stats,
  956. int interface_no,
  957. void *context,
  958. void (*deliver_fn) (
  959. void *context,
  960. const void *msg,
  961. unsigned int msg_len),
  962. void (*iface_change_fn) (
  963. void *context,
  964. const struct totem_ip_address *iface_address),
  965. void (*target_set_completed) (
  966. void *context))
  967. {
  968. struct totemudp_instance *instance;
  969. instance = malloc (sizeof (struct totemudp_instance));
  970. if (instance == NULL) {
  971. return (-1);
  972. }
  973. totemudp_instance_initialize (instance);
  974. instance->totem_config = totem_config;
  975. instance->stats = stats;
  976. /*
  977. * Configure logging
  978. */
  979. instance->totemudp_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  980. instance->totemudp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  981. instance->totemudp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  982. instance->totemudp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  983. instance->totemudp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  984. instance->totemudp_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  985. instance->totemudp_log_printf = totem_config->totem_logging_configuration.log_printf;
  986. /*
  987. * Initialize random number generator for later use to generate salt
  988. */
  989. instance->crypto_inst = crypto_init (totem_config->private_key,
  990. totem_config->private_key_len,
  991. totem_config->crypto_cipher_type,
  992. totem_config->crypto_hash_type,
  993. totem_config->crypto_compat_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. return (-1);
  1001. }
  1002. /*
  1003. * Initialize local variables for totemudp
  1004. */
  1005. instance->totem_interface = &totem_config->interfaces[interface_no];
  1006. totemip_copy (&instance->mcast_address, &instance->totem_interface->mcast_addr);
  1007. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  1008. instance->totemudp_poll_handle = poll_handle;
  1009. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  1010. instance->context = context;
  1011. instance->totemudp_deliver_fn = deliver_fn;
  1012. instance->totemudp_iface_change_fn = iface_change_fn;
  1013. instance->totemudp_target_set_completed = target_set_completed;
  1014. totemip_localhost (instance->mcast_address.family, &localhost);
  1015. localhost.nodeid = instance->totem_config->node_id;
  1016. /*
  1017. * RRP layer isn't ready to receive message because it hasn't
  1018. * initialized yet. Add short timer to check the interfaces.
  1019. */
  1020. qb_loop_timer_add (instance->totemudp_poll_handle,
  1021. QB_LOOP_MED,
  1022. 100*QB_TIME_NS_IN_MSEC,
  1023. (void *)instance,
  1024. timer_function_netif_check_timeout,
  1025. &instance->timer_netif_check_timeout);
  1026. *udp_context = instance;
  1027. return (0);
  1028. }
  1029. void *totemudp_buffer_alloc (void)
  1030. {
  1031. return malloc (FRAME_SIZE_MAX);
  1032. }
  1033. void totemudp_buffer_release (void *ptr)
  1034. {
  1035. return free (ptr);
  1036. }
  1037. int totemudp_processor_count_set (
  1038. void *udp_context,
  1039. int processor_count)
  1040. {
  1041. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1042. int res = 0;
  1043. instance->my_memb_entries = processor_count;
  1044. qb_loop_timer_del (instance->totemudp_poll_handle,
  1045. instance->timer_netif_check_timeout);
  1046. if (processor_count == 1) {
  1047. qb_loop_timer_add (instance->totemudp_poll_handle,
  1048. QB_LOOP_MED,
  1049. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  1050. (void *)instance,
  1051. timer_function_netif_check_timeout,
  1052. &instance->timer_netif_check_timeout);
  1053. }
  1054. return (res);
  1055. }
  1056. int totemudp_recv_flush (void *udp_context)
  1057. {
  1058. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1059. struct pollfd ufd;
  1060. int nfds;
  1061. int res = 0;
  1062. int i;
  1063. int sock;
  1064. instance->flushing = 1;
  1065. for (i = 0; i < 2; i++) {
  1066. sock = -1;
  1067. if (i == 0) {
  1068. sock = instance->totemudp_sockets.mcast_recv;
  1069. }
  1070. if (i == 1) {
  1071. sock = instance->totemudp_sockets.local_mcast_loop[0];
  1072. }
  1073. assert(sock != -1);
  1074. do {
  1075. ufd.fd = sock;
  1076. ufd.events = POLLIN;
  1077. nfds = poll (&ufd, 1, 0);
  1078. if (nfds == 1 && ufd.revents & POLLIN) {
  1079. net_deliver_fn (sock, ufd.revents, instance);
  1080. }
  1081. } while (nfds == 1);
  1082. }
  1083. instance->flushing = 0;
  1084. return (res);
  1085. }
  1086. int totemudp_send_flush (void *udp_context)
  1087. {
  1088. return 0;
  1089. }
  1090. int totemudp_token_send (
  1091. void *udp_context,
  1092. const void *msg,
  1093. unsigned int msg_len)
  1094. {
  1095. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1096. int res = 0;
  1097. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  1098. return (res);
  1099. }
  1100. int totemudp_mcast_flush_send (
  1101. void *udp_context,
  1102. const void *msg,
  1103. unsigned int msg_len)
  1104. {
  1105. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1106. int res = 0;
  1107. mcast_sendmsg (instance, msg, msg_len);
  1108. return (res);
  1109. }
  1110. int totemudp_mcast_noflush_send (
  1111. void *udp_context,
  1112. const void *msg,
  1113. unsigned int msg_len)
  1114. {
  1115. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1116. int res = 0;
  1117. mcast_sendmsg (instance, msg, msg_len);
  1118. return (res);
  1119. }
  1120. extern int totemudp_iface_check (void *udp_context)
  1121. {
  1122. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1123. int res = 0;
  1124. timer_function_netif_check_timeout (instance);
  1125. return (res);
  1126. }
  1127. extern void totemudp_net_mtu_adjust (void *udp_context, struct totem_config *totem_config)
  1128. {
  1129. #define UDPIP_HEADER_SIZE (20 + 8) /* 20 bytes for ip 8 bytes for udp */
  1130. totem_config->net_mtu -= crypto_sec_header_size(totem_config->crypto_cipher_type,
  1131. totem_config->crypto_hash_type) +
  1132. UDPIP_HEADER_SIZE;
  1133. }
  1134. const char *totemudp_iface_print (void *udp_context) {
  1135. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1136. const char *ret_char;
  1137. ret_char = totemip_print (&instance->my_id);
  1138. return (ret_char);
  1139. }
  1140. int totemudp_iface_get (
  1141. void *udp_context,
  1142. struct totem_ip_address *addr)
  1143. {
  1144. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1145. int res = 0;
  1146. memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
  1147. return (res);
  1148. }
  1149. int totemudp_token_target_set (
  1150. void *udp_context,
  1151. const struct totem_ip_address *token_target)
  1152. {
  1153. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1154. int res = 0;
  1155. memcpy (&instance->token_target, token_target,
  1156. sizeof (struct totem_ip_address));
  1157. instance->totemudp_target_set_completed (instance->context);
  1158. return (res);
  1159. }
  1160. extern int totemudp_recv_mcast_empty (
  1161. void *udp_context)
  1162. {
  1163. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1164. unsigned int res;
  1165. struct sockaddr_storage system_from;
  1166. struct msghdr msg_recv;
  1167. struct pollfd ufd;
  1168. int nfds;
  1169. int msg_processed = 0;
  1170. int i;
  1171. int sock;
  1172. /*
  1173. * Receive datagram
  1174. */
  1175. msg_recv.msg_name = &system_from;
  1176. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  1177. msg_recv.msg_iov = &instance->totemudp_iov_recv_flush;
  1178. msg_recv.msg_iovlen = 1;
  1179. #ifdef HAVE_MSGHDR_CONTROL
  1180. msg_recv.msg_control = 0;
  1181. #endif
  1182. #ifdef HAVE_MSGHDR_CONTROLLEN
  1183. msg_recv.msg_controllen = 0;
  1184. #endif
  1185. #ifdef HAVE_MSGHDR_FLAGS
  1186. msg_recv.msg_flags = 0;
  1187. #endif
  1188. #ifdef HAVE_MSGHDR_ACCRIGHTS
  1189. msg_recv.msg_accrights = NULL;
  1190. #endif
  1191. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  1192. msg_recv.msg_accrightslen = 0;
  1193. #endif
  1194. for (i = 0; i < 2; i++) {
  1195. sock = -1;
  1196. if (i == 0) {
  1197. sock = instance->totemudp_sockets.mcast_recv;
  1198. }
  1199. if (i == 1) {
  1200. sock = instance->totemudp_sockets.local_mcast_loop[0];
  1201. }
  1202. assert(sock != -1);
  1203. do {
  1204. ufd.fd = sock;
  1205. ufd.events = POLLIN;
  1206. nfds = poll (&ufd, 1, 0);
  1207. if (nfds == 1 && ufd.revents & POLLIN) {
  1208. res = recvmsg (sock, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  1209. if (res != -1) {
  1210. msg_processed = 1;
  1211. } else {
  1212. msg_processed = -1;
  1213. }
  1214. }
  1215. } while (nfds == 1);
  1216. }
  1217. return (msg_processed);
  1218. }