totemudp.c 37 KB

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