4
0

totemnet.c 38 KB

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