totemudp.c 33 KB

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