totemnet.c 38 KB

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