totemnet.c 39 KB

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