totemnet.c 38 KB

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