totemnet.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  1. /*
  2. * Copyright (c) 2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2009 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 <signal.h>
  53. #include <sched.h>
  54. #include <time.h>
  55. #include <sys/time.h>
  56. #include <sys/poll.h>
  57. #include <corosync/queue.h>
  58. #include <corosync/sq.h>
  59. #include <corosync/list.h>
  60. #include <corosync/hdb.h>
  61. #include <corosync/swab.h>
  62. #include <corosync/totem/coropoll.h>
  63. #include "totemnet.h"
  64. #include "wthread.h"
  65. #include "crypto.h"
  66. #define MCAST_SOCKET_BUFFER_SIZE (TRANSMITS_ALLOWED * FRAME_SIZE_MAX)
  67. #define NETIF_STATE_REPORT_UP 1
  68. #define NETIF_STATE_REPORT_DOWN 2
  69. #define BIND_STATE_UNBOUND 0
  70. #define BIND_STATE_REGULAR 1
  71. #define BIND_STATE_LOOPBACK 2
  72. #define HMAC_HASH_SIZE 20
  73. struct security_header {
  74. unsigned char hash_digest[HMAC_HASH_SIZE]; /* The hash *MUST* be first in the data structure */
  75. unsigned char salt[16]; /* random number */
  76. char msg[0];
  77. } __attribute__((packed));
  78. struct totemnet_mcast_thread_state {
  79. unsigned char iobuf[FRAME_SIZE_MAX];
  80. prng_state prng_state;
  81. };
  82. struct totemnet_socket {
  83. int mcast_recv;
  84. int mcast_send;
  85. int token;
  86. };
  87. struct totemnet_instance {
  88. hmac_state totemnet_hmac_state;
  89. prng_state totemnet_prng_state;
  90. unsigned char totemnet_private_key[1024];
  91. unsigned int totemnet_private_key_len;
  92. hdb_handle_t totemnet_poll_handle;
  93. struct totem_interface *totem_interface;
  94. int netif_state_report;
  95. int netif_bind_state;
  96. struct worker_thread_group worker_thread_group;
  97. void *context;
  98. void (*totemnet_deliver_fn) (
  99. void *context,
  100. const void *msg,
  101. size_t msg_len);
  102. void (*totemnet_iface_change_fn) (
  103. void *context,
  104. const struct totem_ip_address *iface_address);
  105. /*
  106. * Function and data used to log messages
  107. */
  108. int totemnet_log_level_security;
  109. int totemnet_log_level_error;
  110. int totemnet_log_level_warning;
  111. int totemnet_log_level_notice;
  112. int totemnet_log_level_debug;
  113. int totemnet_subsys_id;
  114. void (*totemnet_log_printf) (int subsys,
  115. const char *function,
  116. const char *file,
  117. int line, unsigned int level,
  118. const char *format,
  119. ...)__attribute__((format(printf, 6, 7)));
  120. hdb_handle_t handle;
  121. char iov_buffer[FRAME_SIZE_MAX];
  122. char iov_buffer_flush[FRAME_SIZE_MAX];
  123. struct iovec totemnet_iov_recv;
  124. struct iovec totemnet_iov_recv_flush;
  125. struct totemnet_socket totemnet_sockets;
  126. struct totem_ip_address mcast_address;
  127. int stats_sent;
  128. int stats_recv;
  129. int stats_delv;
  130. int stats_remcasts;
  131. int stats_orf_token;
  132. struct timeval stats_tv_start;
  133. struct totem_ip_address my_id;
  134. int firstrun;
  135. poll_timer_handle timer_netif_check_timeout;
  136. unsigned int my_memb_entries;
  137. int flushing;
  138. struct totem_config *totem_config;
  139. struct totem_ip_address token_target;
  140. };
  141. struct work_item {
  142. struct iovec iovec[20];
  143. unsigned int iov_len;
  144. struct totemnet_instance *instance;
  145. };
  146. static void netif_down_check (struct totemnet_instance *instance);
  147. static int totemnet_build_sockets (
  148. struct totemnet_instance *instance,
  149. struct totem_ip_address *bindnet_address,
  150. struct totem_ip_address *mcastaddress,
  151. struct totemnet_socket *sockets,
  152. struct totem_ip_address *bound_to);
  153. static struct totem_ip_address localhost;
  154. DECLARE_HDB_DATABASE (totemnet_instance_database);
  155. static void totemnet_instance_initialize (struct totemnet_instance *instance)
  156. {
  157. memset (instance, 0, sizeof (struct totemnet_instance));
  158. instance->netif_state_report = NETIF_STATE_REPORT_UP | NETIF_STATE_REPORT_DOWN;
  159. instance->totemnet_iov_recv.iov_base = instance->iov_buffer;
  160. instance->totemnet_iov_recv.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  161. instance->totemnet_iov_recv_flush.iov_base = instance->iov_buffer_flush;
  162. instance->totemnet_iov_recv_flush.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  163. /*
  164. * There is always atleast 1 processor
  165. */
  166. instance->my_memb_entries = 1;
  167. }
  168. #define log_printf(level, format, args...) \
  169. do { \
  170. instance->totemnet_log_printf (instance->totemnet_subsys_id, \
  171. __FUNCTION__, __FILE__, __LINE__, \
  172. level, (const char *)format, ##args); \
  173. } while (0);
  174. static int authenticate_and_decrypt (
  175. struct totemnet_instance *instance,
  176. struct iovec *iov)
  177. {
  178. unsigned char keys[48];
  179. struct security_header *header = iov[0].iov_base;
  180. prng_state keygen_prng_state;
  181. prng_state stream_prng_state;
  182. unsigned char *hmac_key = &keys[32];
  183. unsigned char *cipher_key = &keys[16];
  184. unsigned char *initial_vector = &keys[0];
  185. unsigned char digest_comparison[HMAC_HASH_SIZE];
  186. unsigned long len;
  187. /*
  188. * Generate MAC, CIPHER, IV keys from private key
  189. */
  190. memset (keys, 0, sizeof (keys));
  191. sober128_start (&keygen_prng_state);
  192. sober128_add_entropy (instance->totemnet_private_key,
  193. instance->totemnet_private_key_len, &keygen_prng_state);
  194. sober128_add_entropy (header->salt, sizeof (header->salt), &keygen_prng_state);
  195. sober128_read (keys, sizeof (keys), &keygen_prng_state);
  196. /*
  197. * Setup stream cipher
  198. */
  199. sober128_start (&stream_prng_state);
  200. sober128_add_entropy (cipher_key, 16, &stream_prng_state);
  201. sober128_add_entropy (initial_vector, 16, &stream_prng_state);
  202. /*
  203. * Authenticate contents of message
  204. */
  205. hmac_init (&instance->totemnet_hmac_state, DIGEST_SHA1, hmac_key, 16);
  206. hmac_process (&instance->totemnet_hmac_state,
  207. (unsigned char *)iov->iov_base + HMAC_HASH_SIZE,
  208. iov->iov_len - HMAC_HASH_SIZE);
  209. len = hash_descriptor[DIGEST_SHA1]->hashsize;
  210. assert (HMAC_HASH_SIZE >= len);
  211. hmac_done (&instance->totemnet_hmac_state, digest_comparison, &len);
  212. if (memcmp (digest_comparison, header->hash_digest, len) != 0) {
  213. log_printf (instance->totemnet_log_level_security, "Received message has invalid digest... ignoring.\n");
  214. return (-1);
  215. }
  216. /*
  217. * Decrypt the contents of the message with the cipher key
  218. */
  219. sober128_read ((unsigned char*)iov->iov_base +
  220. sizeof (struct security_header),
  221. iov->iov_len - sizeof (struct security_header),
  222. &stream_prng_state);
  223. return (0);
  224. }
  225. static void encrypt_and_sign_worker (
  226. struct totemnet_instance *instance,
  227. unsigned char *buf,
  228. size_t *buf_len,
  229. const struct iovec *iovec,
  230. size_t iov_len,
  231. prng_state *prng_state_in)
  232. {
  233. int i;
  234. unsigned char *addr;
  235. unsigned char keys[48];
  236. struct security_header *header;
  237. unsigned char *hmac_key = &keys[32];
  238. unsigned char *cipher_key = &keys[16];
  239. unsigned char *initial_vector = &keys[0];
  240. unsigned long len;
  241. size_t outlen = 0;
  242. hmac_state hmac_state;
  243. prng_state keygen_prng_state;
  244. prng_state stream_prng_state;
  245. header = (struct security_header *)buf;
  246. addr = buf + sizeof (struct security_header);
  247. memset (keys, 0, sizeof (keys));
  248. memset (header->salt, 0, sizeof (header->salt));
  249. /*
  250. * Generate MAC, CIPHER, IV keys from private key
  251. */
  252. sober128_read (header->salt, sizeof (header->salt), prng_state_in);
  253. sober128_start (&keygen_prng_state);
  254. sober128_add_entropy (instance->totemnet_private_key,
  255. instance->totemnet_private_key_len,
  256. &keygen_prng_state);
  257. sober128_add_entropy (header->salt, sizeof (header->salt),
  258. &keygen_prng_state);
  259. sober128_read (keys, sizeof (keys), &keygen_prng_state);
  260. /*
  261. * Setup stream cipher
  262. */
  263. sober128_start (&stream_prng_state);
  264. sober128_add_entropy (cipher_key, 16, &stream_prng_state);
  265. sober128_add_entropy (initial_vector, 16, &stream_prng_state);
  266. outlen = sizeof (struct security_header);
  267. /*
  268. * Copy remainder of message, then encrypt it
  269. */
  270. for (i = 1; i < iov_len; i++) {
  271. memcpy (addr, iovec[i].iov_base, iovec[i].iov_len);
  272. addr += iovec[i].iov_len;
  273. outlen += iovec[i].iov_len;
  274. }
  275. /*
  276. * Encrypt message by XORing stream cipher data
  277. */
  278. sober128_read (buf + sizeof (struct security_header),
  279. outlen - sizeof (struct security_header),
  280. &stream_prng_state);
  281. memset (&hmac_state, 0, sizeof (hmac_state));
  282. /*
  283. * Sign the contents of the message with the hmac key and store signature in message
  284. */
  285. hmac_init (&hmac_state, DIGEST_SHA1, hmac_key, 16);
  286. hmac_process (&hmac_state,
  287. buf + HMAC_HASH_SIZE,
  288. outlen - HMAC_HASH_SIZE);
  289. len = hash_descriptor[DIGEST_SHA1]->hashsize;
  290. hmac_done (&hmac_state, header->hash_digest, &len);
  291. *buf_len = outlen;
  292. }
  293. static inline void ucast_sendmsg (
  294. struct totemnet_instance *instance,
  295. struct totem_ip_address *system_to,
  296. const struct iovec *iovec_in,
  297. size_t iov_len_in)
  298. {
  299. struct msghdr msg_ucast;
  300. int res = 0;
  301. size_t buf_len;
  302. unsigned char sheader[sizeof (struct security_header)];
  303. unsigned char encrypt_data[FRAME_SIZE_MAX];
  304. struct iovec iovec_encrypt[20];
  305. const struct iovec *iovec_sendmsg;
  306. struct sockaddr_storage sockaddr;
  307. unsigned int iov_len;
  308. int addrlen;
  309. if (instance->totem_config->secauth == 1) {
  310. iovec_encrypt[0].iov_base = sheader;
  311. iovec_encrypt[0].iov_len = sizeof (struct security_header);
  312. memcpy (&iovec_encrypt[1], &iovec_in[0],
  313. sizeof (struct iovec) * iov_len_in);
  314. /*
  315. * Encrypt and digest the message
  316. */
  317. encrypt_and_sign_worker (
  318. instance,
  319. encrypt_data,
  320. &buf_len,
  321. iovec_encrypt,
  322. iov_len_in + 1,
  323. &instance->totemnet_prng_state);
  324. iovec_encrypt[0].iov_base = encrypt_data;
  325. iovec_encrypt[0].iov_len = buf_len;
  326. iovec_sendmsg = &iovec_encrypt[0];
  327. iov_len = 1;
  328. } else {
  329. iovec_sendmsg = iovec_in;
  330. iov_len = iov_len_in;
  331. }
  332. /*
  333. * Build unicast message
  334. */
  335. totemip_totemip_to_sockaddr_convert(system_to,
  336. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  337. msg_ucast.msg_name = &sockaddr;
  338. msg_ucast.msg_namelen = addrlen;
  339. msg_ucast.msg_iov = (void *) iovec_sendmsg;
  340. msg_ucast.msg_iovlen = iov_len;
  341. msg_ucast.msg_control = 0;
  342. msg_ucast.msg_controllen = 0;
  343. msg_ucast.msg_flags = 0;
  344. /*
  345. * Transmit multicast message
  346. * An error here is recovered by totemsrp
  347. */
  348. res = sendmsg (instance->totemnet_sockets.mcast_send, &msg_ucast,
  349. MSG_NOSIGNAL);
  350. }
  351. static inline void mcast_sendmsg (
  352. struct totemnet_instance *instance,
  353. const struct iovec *iovec_in,
  354. size_t iov_len_in)
  355. {
  356. struct msghdr msg_mcast;
  357. int res = 0;
  358. size_t buf_len;
  359. unsigned char sheader[sizeof (struct security_header)];
  360. unsigned char encrypt_data[FRAME_SIZE_MAX];
  361. struct iovec iovec_encrypt[20];
  362. const struct iovec *iovec_sendmsg;
  363. struct sockaddr_storage sockaddr;
  364. unsigned int iov_len;
  365. int addrlen;
  366. if (instance->totem_config->secauth == 1) {
  367. iovec_encrypt[0].iov_base = sheader;
  368. iovec_encrypt[0].iov_len = sizeof (struct security_header);
  369. memcpy (&iovec_encrypt[1], &iovec_in[0],
  370. sizeof (struct iovec) * iov_len_in);
  371. /*
  372. * Encrypt and digest the message
  373. */
  374. encrypt_and_sign_worker (
  375. instance,
  376. encrypt_data,
  377. &buf_len,
  378. iovec_encrypt,
  379. iov_len_in + 1,
  380. &instance->totemnet_prng_state);
  381. iovec_encrypt[0].iov_base = encrypt_data;
  382. iovec_encrypt[0].iov_len = buf_len;
  383. iovec_sendmsg = &iovec_encrypt[0];
  384. iov_len = 1;
  385. } else {
  386. iovec_sendmsg = iovec_in;
  387. iov_len = iov_len_in;
  388. }
  389. /*
  390. * Build multicast message
  391. */
  392. totemip_totemip_to_sockaddr_convert(&instance->mcast_address,
  393. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  394. msg_mcast.msg_name = &sockaddr;
  395. msg_mcast.msg_namelen = addrlen;
  396. msg_mcast.msg_iov = (void *) iovec_sendmsg;
  397. msg_mcast.msg_iovlen = iov_len;
  398. msg_mcast.msg_control = 0;
  399. msg_mcast.msg_controllen = 0;
  400. msg_mcast.msg_flags = 0;
  401. /*
  402. * Transmit multicast message
  403. * An error here is recovered by totemsrp
  404. */
  405. res = sendmsg (instance->totemnet_sockets.mcast_send, &msg_mcast,
  406. MSG_NOSIGNAL);
  407. }
  408. static void totemnet_mcast_thread_state_constructor (
  409. void *totemnet_mcast_thread_state_in)
  410. {
  411. struct totemnet_mcast_thread_state *totemnet_mcast_thread_state =
  412. (struct totemnet_mcast_thread_state *)totemnet_mcast_thread_state_in;
  413. memset (totemnet_mcast_thread_state, 0,
  414. sizeof (totemnet_mcast_thread_state));
  415. rng_make_prng (128, PRNG_SOBER,
  416. &totemnet_mcast_thread_state->prng_state, NULL);
  417. }
  418. static void totemnet_mcast_worker_fn (void *thread_state, void *work_item_in)
  419. {
  420. struct work_item *work_item = (struct work_item *)work_item_in;
  421. struct totemnet_mcast_thread_state *totemnet_mcast_thread_state =
  422. (struct totemnet_mcast_thread_state *)thread_state;
  423. struct totemnet_instance *instance = work_item->instance;
  424. struct msghdr msg_mcast;
  425. unsigned char sheader[sizeof (struct security_header)];
  426. int res = 0;
  427. size_t buf_len;
  428. struct iovec iovec_encrypted;
  429. struct iovec *iovec_sendmsg;
  430. struct sockaddr_storage sockaddr;
  431. unsigned int iovs;
  432. int addrlen;
  433. if (instance->totem_config->secauth == 1) {
  434. memmove (&work_item->iovec[1], &work_item->iovec[0],
  435. work_item->iov_len * sizeof (struct iovec));
  436. work_item->iovec[0].iov_base = sheader;
  437. work_item->iovec[0].iov_len = sizeof (struct security_header);
  438. /*
  439. * Encrypt and digest the message
  440. */
  441. encrypt_and_sign_worker (
  442. instance,
  443. totemnet_mcast_thread_state->iobuf, &buf_len,
  444. work_item->iovec, work_item->iov_len + 1,
  445. &totemnet_mcast_thread_state->prng_state);
  446. iovec_sendmsg = &iovec_encrypted;
  447. iovec_sendmsg->iov_base = totemnet_mcast_thread_state->iobuf;
  448. iovec_sendmsg->iov_len = buf_len;
  449. iovs = 1;
  450. } else {
  451. iovec_sendmsg = work_item->iovec;
  452. iovs = work_item->iov_len;
  453. }
  454. totemip_totemip_to_sockaddr_convert(&instance->mcast_address,
  455. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  456. msg_mcast.msg_name = &sockaddr;
  457. msg_mcast.msg_namelen = addrlen;
  458. msg_mcast.msg_iov = iovec_sendmsg;
  459. msg_mcast.msg_iovlen = iovs;
  460. msg_mcast.msg_control = 0;
  461. msg_mcast.msg_controllen = 0;
  462. msg_mcast.msg_flags = 0;
  463. /*
  464. * Transmit multicast message
  465. * An error here is recovered by totemnet
  466. */
  467. res = sendmsg (instance->totemnet_sockets.mcast_send, &msg_mcast,
  468. MSG_NOSIGNAL);
  469. if (res > 0) {
  470. instance->stats_sent += res;
  471. }
  472. }
  473. int totemnet_finalize (
  474. hdb_handle_t handle)
  475. {
  476. struct totemnet_instance *instance;
  477. int res = 0;
  478. res = hdb_handle_get (&totemnet_instance_database, handle,
  479. (void *)&instance);
  480. if (res != 0) {
  481. res = ENOENT;
  482. goto error_exit;
  483. }
  484. worker_thread_group_exit (&instance->worker_thread_group);
  485. hdb_handle_put (&totemnet_instance_database, handle);
  486. error_exit:
  487. return (res);
  488. }
  489. /*
  490. * Only designed to work with a message with one iov
  491. */
  492. static int net_deliver_fn (
  493. hdb_handle_t handle,
  494. int fd,
  495. int revents,
  496. void *data)
  497. {
  498. struct totemnet_instance *instance = (struct totemnet_instance *)data;
  499. struct msghdr msg_recv;
  500. struct iovec *iovec;
  501. struct security_header *security_header;
  502. struct sockaddr_storage system_from;
  503. int bytes_received;
  504. int res = 0;
  505. unsigned char *msg_offset;
  506. unsigned int size_delv;
  507. if (instance->flushing == 1) {
  508. iovec = &instance->totemnet_iov_recv_flush;
  509. } else {
  510. iovec = &instance->totemnet_iov_recv;
  511. }
  512. /*
  513. * Receive datagram
  514. */
  515. msg_recv.msg_name = &system_from;
  516. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  517. msg_recv.msg_iov = iovec;
  518. msg_recv.msg_iovlen = 1;
  519. msg_recv.msg_control = 0;
  520. msg_recv.msg_controllen = 0;
  521. msg_recv.msg_flags = 0;
  522. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  523. if (bytes_received == -1) {
  524. return (0);
  525. } else {
  526. instance->stats_recv += bytes_received;
  527. }
  528. if ((instance->totem_config->secauth == 1) &&
  529. (bytes_received < sizeof (struct security_header))) {
  530. log_printf (instance->totemnet_log_level_security, "Received message is too short... ignoring %d.\n", bytes_received);
  531. return (0);
  532. }
  533. security_header = (struct security_header *)iovec->iov_base;
  534. iovec->iov_len = bytes_received;
  535. if (instance->totem_config->secauth == 1) {
  536. /*
  537. * Authenticate and if authenticated, decrypt datagram
  538. */
  539. res = authenticate_and_decrypt (instance, iovec);
  540. if (res == -1) {
  541. log_printf (instance->totemnet_log_level_security,
  542. "Invalid packet data\n");
  543. iovec->iov_len = FRAME_SIZE_MAX;
  544. return 0;
  545. }
  546. msg_offset = (unsigned char *)iovec->iov_base +
  547. sizeof (struct security_header);
  548. size_delv = bytes_received - sizeof (struct security_header);
  549. } else {
  550. msg_offset = iovec->iov_base;
  551. size_delv = bytes_received;
  552. }
  553. /*
  554. * Handle incoming message
  555. */
  556. instance->totemnet_deliver_fn (
  557. instance->context,
  558. msg_offset,
  559. size_delv);
  560. iovec->iov_len = FRAME_SIZE_MAX;
  561. return (0);
  562. }
  563. static int netif_determine (
  564. struct totemnet_instance *instance,
  565. struct totem_ip_address *bindnet,
  566. struct totem_ip_address *bound_to,
  567. int *interface_up,
  568. int *interface_num)
  569. {
  570. int res;
  571. res = totemip_iface_check (bindnet, bound_to,
  572. interface_up, interface_num,
  573. + 0); // TODO andrew can address this instance->totem_config->clear_node_high_bit);
  574. return (res);
  575. }
  576. /*
  577. * If the interface is up, the sockets for totem are built. If the interface is down
  578. * this function is requeued in the timer list to retry building the sockets later.
  579. */
  580. static void timer_function_netif_check_timeout (
  581. void *data)
  582. {
  583. struct totemnet_instance *instance = (struct totemnet_instance *)data;
  584. int res;
  585. int interface_up;
  586. int interface_num;
  587. struct totem_ip_address *bind_address;
  588. /*
  589. * Build sockets for every interface
  590. */
  591. netif_determine (instance,
  592. &instance->totem_interface->bindnet,
  593. &instance->totem_interface->boundto,
  594. &interface_up, &interface_num);
  595. /*
  596. * If the network interface isn't back up and we are already
  597. * in loopback mode, add timer to check again and return
  598. */
  599. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  600. interface_up == 0) ||
  601. (instance->my_memb_entries == 1 &&
  602. instance->netif_bind_state == BIND_STATE_REGULAR &&
  603. interface_up == 1)) {
  604. poll_timer_add (instance->totemnet_poll_handle,
  605. instance->totem_config->downcheck_timeout,
  606. (void *)instance,
  607. timer_function_netif_check_timeout,
  608. &instance->timer_netif_check_timeout);
  609. /*
  610. * Add a timer to check for a downed regular interface
  611. */
  612. return;
  613. }
  614. if (instance->totemnet_sockets.mcast_recv > 0) {
  615. close (instance->totemnet_sockets.mcast_recv);
  616. poll_dispatch_delete (instance->totemnet_poll_handle,
  617. instance->totemnet_sockets.mcast_recv);
  618. }
  619. if (instance->totemnet_sockets.mcast_send > 0) {
  620. close (instance->totemnet_sockets.mcast_send);
  621. }
  622. if (instance->totemnet_sockets.token > 0) {
  623. close (instance->totemnet_sockets.token);
  624. poll_dispatch_delete (instance->totemnet_poll_handle,
  625. instance->totemnet_sockets.token);
  626. }
  627. if (interface_up == 0) {
  628. /*
  629. * Interface is not up
  630. */
  631. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  632. bind_address = &localhost;
  633. /*
  634. * Add a timer to retry building interfaces and request memb_gather_enter
  635. */
  636. poll_timer_add (instance->totemnet_poll_handle,
  637. instance->totem_config->downcheck_timeout,
  638. (void *)instance,
  639. timer_function_netif_check_timeout,
  640. &instance->timer_netif_check_timeout);
  641. } else {
  642. /*
  643. * Interface is up
  644. */
  645. instance->netif_bind_state = BIND_STATE_REGULAR;
  646. bind_address = &instance->totem_interface->bindnet;
  647. }
  648. /*
  649. * Create and bind the multicast and unicast sockets
  650. */
  651. res = totemnet_build_sockets (instance,
  652. &instance->mcast_address,
  653. bind_address,
  654. &instance->totemnet_sockets,
  655. &instance->totem_interface->boundto);
  656. poll_dispatch_add (
  657. instance->totemnet_poll_handle,
  658. instance->totemnet_sockets.mcast_recv,
  659. POLLIN, instance, net_deliver_fn);
  660. poll_dispatch_add (
  661. instance->totemnet_poll_handle,
  662. instance->totemnet_sockets.token,
  663. POLLIN, instance, net_deliver_fn);
  664. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  665. /*
  666. * This reports changes in the interface to the user and totemsrp
  667. */
  668. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  669. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  670. log_printf (instance->totemnet_log_level_notice,
  671. "The network interface [%s] is now up.\n",
  672. totemip_print (&instance->totem_interface->boundto));
  673. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  674. instance->totemnet_iface_change_fn (instance->context, &instance->my_id);
  675. }
  676. /*
  677. * Add a timer to check for interface going down in single membership
  678. */
  679. if (instance->my_memb_entries == 1) {
  680. poll_timer_add (instance->totemnet_poll_handle,
  681. instance->totem_config->downcheck_timeout,
  682. (void *)instance,
  683. timer_function_netif_check_timeout,
  684. &instance->timer_netif_check_timeout);
  685. }
  686. } else {
  687. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  688. log_printf (instance->totemnet_log_level_notice,
  689. "The network interface is down.\n");
  690. instance->totemnet_iface_change_fn (instance->context, &instance->my_id);
  691. }
  692. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  693. }
  694. }
  695. /*
  696. * Check if an interface is down and reconfigure
  697. * totemnet waiting for it to come back up
  698. */
  699. static void netif_down_check (struct totemnet_instance *instance)
  700. {
  701. timer_function_netif_check_timeout (instance);
  702. }
  703. /* Set the socket priority to INTERACTIVE to ensure
  704. that our messages don't get queued behind anything else */
  705. static void totemnet_traffic_control_set(struct totemnet_instance *instance, int sock)
  706. {
  707. #ifdef SO_PRIORITY
  708. int prio = 6; /* TC_PRIO_INTERACTIVE */
  709. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int)))
  710. log_printf (instance->totemnet_log_level_warning, "Could not set traffic priority. (%s)\n", strerror (errno));
  711. #endif
  712. }
  713. static int totemnet_build_sockets_ip (
  714. struct totemnet_instance *instance,
  715. struct totem_ip_address *mcast_address,
  716. struct totem_ip_address *bindnet_address,
  717. struct totemnet_socket *sockets,
  718. struct totem_ip_address *bound_to,
  719. int interface_num)
  720. {
  721. struct sockaddr_storage sockaddr;
  722. struct ipv6_mreq mreq6;
  723. struct ip_mreq mreq;
  724. struct sockaddr_storage mcast_ss, boundto_ss;
  725. struct sockaddr_in6 *mcast_sin6 = (struct sockaddr_in6 *)&mcast_ss;
  726. struct sockaddr_in *mcast_sin = (struct sockaddr_in *)&mcast_ss;
  727. struct sockaddr_in *boundto_sin = (struct sockaddr_in *)&boundto_ss;
  728. unsigned int sendbuf_size;
  729. unsigned int recvbuf_size;
  730. unsigned int optlen = sizeof (sendbuf_size);
  731. int addrlen;
  732. int res;
  733. int flag;
  734. /*
  735. * Create multicast recv socket
  736. */
  737. sockets->mcast_recv = socket (bindnet_address->family, SOCK_DGRAM, 0);
  738. if (sockets->mcast_recv == -1) {
  739. perror ("socket");
  740. return (-1);
  741. }
  742. totemip_nosigpipe (sockets->mcast_recv);
  743. res = fcntl (sockets->mcast_recv, F_SETFL, O_NONBLOCK);
  744. if (res == -1) {
  745. log_printf (instance->totemnet_log_level_warning, "Could not set non-blocking operation on multicast socket: %s\n", strerror (errno));
  746. return (-1);
  747. }
  748. /*
  749. * Force reuse
  750. */
  751. flag = 1;
  752. if ( setsockopt(sockets->mcast_recv, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  753. perror("setsockopt reuseaddr");
  754. return (-1);
  755. }
  756. /*
  757. * Bind to multicast socket used for multicast receives
  758. */
  759. totemip_totemip_to_sockaddr_convert(mcast_address,
  760. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  761. res = bind (sockets->mcast_recv, (struct sockaddr *)&sockaddr, addrlen);
  762. if (res == -1) {
  763. perror ("bind mcast recv socket failed");
  764. return (-1);
  765. }
  766. /*
  767. * Setup mcast send socket
  768. */
  769. sockets->mcast_send = socket (bindnet_address->family, SOCK_DGRAM, 0);
  770. if (sockets->mcast_send == -1) {
  771. perror ("socket");
  772. return (-1);
  773. }
  774. totemip_nosigpipe (sockets->mcast_send);
  775. res = fcntl (sockets->mcast_send, F_SETFL, O_NONBLOCK);
  776. if (res == -1) {
  777. log_printf (instance->totemnet_log_level_warning, "Could not set non-blocking operation on multicast socket: %s\n", strerror (errno));
  778. return (-1);
  779. }
  780. /*
  781. * Force reuse
  782. */
  783. flag = 1;
  784. if ( setsockopt(sockets->mcast_send, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  785. perror("setsockopt reuseaddr");
  786. return (-1);
  787. }
  788. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port - 1,
  789. &sockaddr, &addrlen);
  790. res = bind (sockets->mcast_send, (struct sockaddr *)&sockaddr, addrlen);
  791. if (res == -1) {
  792. perror ("bind mcast send socket failed");
  793. return (-1);
  794. }
  795. /*
  796. * Setup unicast socket
  797. */
  798. sockets->token = socket (bindnet_address->family, SOCK_DGRAM, 0);
  799. if (sockets->token == -1) {
  800. perror ("socket2");
  801. return (-1);
  802. }
  803. totemip_nosigpipe (sockets->token);
  804. res = fcntl (sockets->token, F_SETFL, O_NONBLOCK);
  805. if (res == -1) {
  806. log_printf (instance->totemnet_log_level_warning, "Could not set non-blocking operation on token socket: %s\n", strerror (errno));
  807. return (-1);
  808. }
  809. /*
  810. * Force reuse
  811. */
  812. flag = 1;
  813. if ( setsockopt(sockets->token, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  814. perror("setsockopt reuseaddr");
  815. return (-1);
  816. }
  817. /*
  818. * Bind to unicast socket used for token send/receives
  819. * This has the side effect of binding to the correct interface
  820. */
  821. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  822. res = bind (sockets->token, (struct sockaddr *)&sockaddr, addrlen);
  823. if (res == -1) {
  824. perror ("bind token socket failed");
  825. return (-1);
  826. }
  827. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  828. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  829. /*
  830. * Set buffer sizes to avoid overruns
  831. */
  832. res = setsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  833. res = setsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  834. res = getsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  835. if (res == 0) {
  836. log_printf (instance->totemnet_log_level_notice,
  837. "Receive multicast socket recv buffer size (%d bytes).\n", recvbuf_size);
  838. }
  839. res = getsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  840. if (res == 0) {
  841. log_printf (instance->totemnet_log_level_notice,
  842. "Transmit multicast socket send buffer size (%d bytes).\n", sendbuf_size);
  843. }
  844. /*
  845. * Join group membership on socket
  846. */
  847. totemip_totemip_to_sockaddr_convert(mcast_address, instance->totem_interface->ip_port, &mcast_ss, &addrlen);
  848. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &boundto_ss, &addrlen);
  849. switch ( bindnet_address->family ) {
  850. case AF_INET:
  851. memset(&mreq, 0, sizeof(mreq));
  852. mreq.imr_multiaddr.s_addr = mcast_sin->sin_addr.s_addr;
  853. mreq.imr_interface.s_addr = boundto_sin->sin_addr.s_addr;
  854. res = setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  855. &mreq, sizeof (mreq));
  856. if (res == -1) {
  857. perror ("join ipv4 multicast group failed");
  858. return (-1);
  859. }
  860. break;
  861. case AF_INET6:
  862. memset(&mreq6, 0, sizeof(mreq6));
  863. memcpy(&mreq6.ipv6mr_multiaddr, &mcast_sin6->sin6_addr, sizeof(struct in6_addr));
  864. mreq6.ipv6mr_interface = interface_num;
  865. res = setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_JOIN_GROUP,
  866. &mreq6, sizeof (mreq6));
  867. if (res == -1) {
  868. perror ("join ipv6 multicast group failed");
  869. return (-1);
  870. }
  871. break;
  872. }
  873. /*
  874. * Turn on multicast loopback
  875. */
  876. flag = 1;
  877. switch ( bindnet_address->family ) {
  878. case AF_INET:
  879. res = setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_LOOP,
  880. &flag, sizeof (flag));
  881. break;
  882. case AF_INET6:
  883. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
  884. &flag, sizeof (flag));
  885. }
  886. if (res == -1) {
  887. perror ("turn off loopback");
  888. return (-1);
  889. }
  890. /*
  891. * Set multicast packets TTL
  892. */
  893. if ( bindnet_address->family == AF_INET6 )
  894. {
  895. flag = 255;
  896. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
  897. &flag, sizeof (flag));
  898. if (res == -1) {
  899. perror ("setp mcast hops");
  900. return (-1);
  901. }
  902. }
  903. /*
  904. * Bind to a specific interface for multicast send and receive
  905. */
  906. switch ( bindnet_address->family ) {
  907. case AF_INET:
  908. if (setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_IF,
  909. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  910. perror ("cannot select interface");
  911. return (-1);
  912. }
  913. if (setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_MULTICAST_IF,
  914. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  915. perror ("cannot select interface");
  916. return (-1);
  917. }
  918. break;
  919. case AF_INET6:
  920. if (setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  921. &interface_num, sizeof (interface_num)) < 0) {
  922. perror ("cannot select interface");
  923. return (-1);
  924. }
  925. if (setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  926. &interface_num, sizeof (interface_num)) < 0) {
  927. perror ("cannot select interface");
  928. return (-1);
  929. }
  930. break;
  931. }
  932. return 0;
  933. }
  934. static int totemnet_build_sockets (
  935. struct totemnet_instance *instance,
  936. struct totem_ip_address *mcast_address,
  937. struct totem_ip_address *bindnet_address,
  938. struct totemnet_socket *sockets,
  939. struct totem_ip_address *bound_to)
  940. {
  941. int interface_num;
  942. int interface_up;
  943. int res;
  944. /*
  945. * Determine the ip address bound to and the interface name
  946. */
  947. res = netif_determine (instance,
  948. bindnet_address,
  949. bound_to,
  950. &interface_up,
  951. &interface_num);
  952. if (res == -1) {
  953. return (-1);
  954. }
  955. totemip_copy(&instance->my_id, bound_to);
  956. res = totemnet_build_sockets_ip (instance, mcast_address,
  957. bindnet_address, sockets, bound_to, interface_num);
  958. /* We only send out of the token socket */
  959. totemnet_traffic_control_set(instance, sockets->token);
  960. return res;
  961. }
  962. /*
  963. * Totem Network interface - also does encryption/decryption
  964. * depends on poll abstraction, POSIX, IPV4
  965. */
  966. /*
  967. * Create an instance
  968. */
  969. int totemnet_initialize (
  970. hdb_handle_t poll_handle,
  971. hdb_handle_t *handle,
  972. struct totem_config *totem_config,
  973. int interface_no,
  974. void *context,
  975. void (*deliver_fn) (
  976. void *context,
  977. const void *msg,
  978. size_t msg_len),
  979. void (*iface_change_fn) (
  980. void *context,
  981. const struct totem_ip_address *iface_address))
  982. {
  983. struct totemnet_instance *instance;
  984. unsigned int res;
  985. res = hdb_handle_create (&totemnet_instance_database,
  986. sizeof (struct totemnet_instance), handle);
  987. if (res != 0) {
  988. goto error_exit;
  989. }
  990. res = hdb_handle_get (&totemnet_instance_database, *handle,
  991. (void *)&instance);
  992. if (res != 0) {
  993. goto error_destroy;
  994. }
  995. totemnet_instance_initialize (instance);
  996. instance->totem_config = totem_config;
  997. /*
  998. * Configure logging
  999. */
  1000. instance->totemnet_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  1001. instance->totemnet_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  1002. instance->totemnet_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  1003. instance->totemnet_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  1004. instance->totemnet_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  1005. instance->totemnet_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1006. instance->totemnet_log_printf = totem_config->totem_logging_configuration.log_printf;
  1007. /*
  1008. * Initialize random number generator for later use to generate salt
  1009. */
  1010. memcpy (instance->totemnet_private_key, totem_config->private_key,
  1011. totem_config->private_key_len);
  1012. instance->totemnet_private_key_len = totem_config->private_key_len;
  1013. rng_make_prng (128, PRNG_SOBER, &instance->totemnet_prng_state, NULL);
  1014. /*
  1015. * Initialize local variables for totemnet
  1016. */
  1017. instance->totem_interface = &totem_config->interfaces[interface_no];
  1018. totemip_copy (&instance->mcast_address, &instance->totem_interface->mcast_addr);
  1019. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  1020. /*
  1021. * If threaded send requested, initialize thread group data structure
  1022. */
  1023. if (totem_config->threads) {
  1024. worker_thread_group_init (
  1025. &instance->worker_thread_group,
  1026. totem_config->threads, 128,
  1027. sizeof (struct work_item),
  1028. sizeof (struct totemnet_mcast_thread_state),
  1029. totemnet_mcast_thread_state_constructor,
  1030. totemnet_mcast_worker_fn);
  1031. }
  1032. instance->totemnet_poll_handle = poll_handle;
  1033. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  1034. instance->context = context;
  1035. instance->totemnet_deliver_fn = deliver_fn;
  1036. instance->totemnet_iface_change_fn = iface_change_fn;
  1037. instance->handle = *handle;
  1038. rng_make_prng (128, PRNG_SOBER, &instance->totemnet_prng_state, NULL);
  1039. totemip_localhost (instance->mcast_address.family, &localhost);
  1040. netif_down_check (instance);
  1041. error_exit:
  1042. hdb_handle_put (&totemnet_instance_database, *handle);
  1043. return (0);
  1044. error_destroy:
  1045. hdb_handle_destroy (&totemnet_instance_database, *handle);
  1046. return (-1);
  1047. }
  1048. int totemnet_processor_count_set (
  1049. hdb_handle_t handle,
  1050. int processor_count)
  1051. {
  1052. struct totemnet_instance *instance;
  1053. int res = 0;
  1054. res = hdb_handle_get (&totemnet_instance_database, handle,
  1055. (void *)&instance);
  1056. if (res != 0) {
  1057. res = ENOENT;
  1058. goto error_exit;
  1059. }
  1060. instance->my_memb_entries = processor_count;
  1061. poll_timer_delete (instance->totemnet_poll_handle,
  1062. instance->timer_netif_check_timeout);
  1063. if (processor_count == 1) {
  1064. poll_timer_add (instance->totemnet_poll_handle,
  1065. instance->totem_config->downcheck_timeout,
  1066. (void *)instance,
  1067. timer_function_netif_check_timeout,
  1068. &instance->timer_netif_check_timeout);
  1069. }
  1070. hdb_handle_put (&totemnet_instance_database, handle);
  1071. error_exit:
  1072. return (res);
  1073. }
  1074. int totemnet_recv_flush (hdb_handle_t handle)
  1075. {
  1076. struct totemnet_instance *instance;
  1077. struct pollfd ufd;
  1078. int nfds;
  1079. int res = 0;
  1080. res = hdb_handle_get (&totemnet_instance_database, handle,
  1081. (void *)&instance);
  1082. if (res != 0) {
  1083. res = ENOENT;
  1084. goto error_exit;
  1085. }
  1086. instance->flushing = 1;
  1087. do {
  1088. ufd.fd = instance->totemnet_sockets.mcast_recv;
  1089. ufd.events = POLLIN;
  1090. nfds = poll (&ufd, 1, 0);
  1091. if (nfds == 1 && ufd.revents & POLLIN) {
  1092. net_deliver_fn (0, instance->totemnet_sockets.mcast_recv,
  1093. ufd.revents, instance);
  1094. }
  1095. } while (nfds == 1);
  1096. instance->flushing = 0;
  1097. hdb_handle_put (&totemnet_instance_database, handle);
  1098. error_exit:
  1099. return (res);
  1100. }
  1101. int totemnet_send_flush (hdb_handle_t handle)
  1102. {
  1103. struct totemnet_instance *instance;
  1104. int res = 0;
  1105. res = hdb_handle_get (&totemnet_instance_database, handle,
  1106. (void *)&instance);
  1107. if (res != 0) {
  1108. res = ENOENT;
  1109. goto error_exit;
  1110. }
  1111. worker_thread_group_wait (&instance->worker_thread_group);
  1112. hdb_handle_put (&totemnet_instance_database, handle);
  1113. error_exit:
  1114. return (res);
  1115. }
  1116. int totemnet_token_send (
  1117. hdb_handle_t handle,
  1118. const struct iovec *iovec,
  1119. unsigned int iov_len)
  1120. {
  1121. struct totemnet_instance *instance;
  1122. int res = 0;
  1123. res = hdb_handle_get (&totemnet_instance_database, handle,
  1124. (void *)&instance);
  1125. if (res != 0) {
  1126. res = ENOENT;
  1127. goto error_exit;
  1128. }
  1129. ucast_sendmsg (instance, &instance->token_target, iovec, iov_len);
  1130. hdb_handle_put (&totemnet_instance_database, handle);
  1131. error_exit:
  1132. return (res);
  1133. }
  1134. int totemnet_mcast_flush_send (
  1135. hdb_handle_t handle,
  1136. const struct iovec *iovec,
  1137. unsigned int iov_len)
  1138. {
  1139. struct totemnet_instance *instance;
  1140. int res = 0;
  1141. res = hdb_handle_get (&totemnet_instance_database, handle,
  1142. (void *)&instance);
  1143. if (res != 0) {
  1144. res = ENOENT;
  1145. goto error_exit;
  1146. }
  1147. mcast_sendmsg (instance, iovec, iov_len);
  1148. hdb_handle_put (&totemnet_instance_database, handle);
  1149. error_exit:
  1150. return (res);
  1151. }
  1152. int totemnet_mcast_noflush_send (
  1153. hdb_handle_t handle,
  1154. const struct iovec *iovec,
  1155. unsigned int iov_len)
  1156. {
  1157. struct totemnet_instance *instance;
  1158. struct work_item work_item;
  1159. int res = 0;
  1160. res = hdb_handle_get (&totemnet_instance_database, handle,
  1161. (void *)&instance);
  1162. if (res != 0) {
  1163. res = ENOENT;
  1164. goto error_exit;
  1165. }
  1166. if (instance->totem_config->threads) {
  1167. memcpy (&work_item.iovec[0], iovec, iov_len * sizeof (struct iovec));
  1168. work_item.iov_len = iov_len;
  1169. work_item.instance = instance;
  1170. worker_thread_group_work_add (&instance->worker_thread_group,
  1171. &work_item);
  1172. } else {
  1173. mcast_sendmsg (instance, iovec, iov_len);
  1174. }
  1175. hdb_handle_put (&totemnet_instance_database, handle);
  1176. error_exit:
  1177. return (res);
  1178. }
  1179. extern int totemnet_iface_check (hdb_handle_t handle)
  1180. {
  1181. struct totemnet_instance *instance;
  1182. int res = 0;
  1183. res = hdb_handle_get (&totemnet_instance_database, handle,
  1184. (void *)&instance);
  1185. if (res != 0) {
  1186. res = ENOENT;
  1187. goto error_exit;
  1188. }
  1189. timer_function_netif_check_timeout (instance);
  1190. hdb_handle_put (&totemnet_instance_database, handle);
  1191. error_exit:
  1192. return (res);
  1193. }
  1194. extern void totemnet_net_mtu_adjust (struct totem_config *totem_config)
  1195. {
  1196. #define UDPIP_HEADER_SIZE (20 + 8) /* 20 bytes for ip 8 bytes for udp */
  1197. if (totem_config->secauth == 1) {
  1198. totem_config->net_mtu -= sizeof (struct security_header) +
  1199. UDPIP_HEADER_SIZE;
  1200. } else {
  1201. totem_config->net_mtu -= UDPIP_HEADER_SIZE;
  1202. }
  1203. }
  1204. const char *totemnet_iface_print (hdb_handle_t handle) {
  1205. struct totemnet_instance *instance;
  1206. int res = 0;
  1207. const char *ret_char;
  1208. res = hdb_handle_get (&totemnet_instance_database, handle,
  1209. (void *)&instance);
  1210. if (res != 0) {
  1211. ret_char = "Invalid totemnet handle";
  1212. goto error_exit;
  1213. }
  1214. ret_char = totemip_print (&instance->my_id);
  1215. hdb_handle_put (&totemnet_instance_database, handle);
  1216. error_exit:
  1217. return (ret_char);
  1218. }
  1219. int totemnet_iface_get (
  1220. hdb_handle_t handle,
  1221. struct totem_ip_address *addr)
  1222. {
  1223. struct totemnet_instance *instance;
  1224. unsigned int res;
  1225. res = hdb_handle_get (&totemnet_instance_database, handle,
  1226. (void *)&instance);
  1227. if (res != 0) {
  1228. goto error_exit;
  1229. }
  1230. memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
  1231. hdb_handle_put (&totemnet_instance_database, handle);
  1232. error_exit:
  1233. return (res);
  1234. }
  1235. int totemnet_token_target_set (
  1236. hdb_handle_t handle,
  1237. const struct totem_ip_address *token_target)
  1238. {
  1239. struct totemnet_instance *instance;
  1240. unsigned int res;
  1241. res = hdb_handle_get (&totemnet_instance_database, handle,
  1242. (void *)&instance);
  1243. if (res != 0) {
  1244. goto error_exit;
  1245. }
  1246. memcpy (&instance->token_target, token_target,
  1247. sizeof (struct totem_ip_address));
  1248. hdb_handle_put (&totemnet_instance_database, handle);
  1249. error_exit:
  1250. return (res);
  1251. }