4
0

totemnet.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510
  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,
  114. const char *function,
  115. const char *file,
  116. int line, unsigned int level,
  117. const char *format,
  118. ...)__attribute__((format(printf, 6, 7)));
  119. hdb_handle_t handle;
  120. char iov_buffer[FRAME_SIZE_MAX];
  121. char iov_buffer_flush[FRAME_SIZE_MAX];
  122. struct iovec totemnet_iov_recv;
  123. struct iovec totemnet_iov_recv_flush;
  124. struct totemnet_socket totemnet_sockets;
  125. struct totem_ip_address mcast_address;
  126. int stats_sent;
  127. int stats_recv;
  128. int stats_delv;
  129. int stats_remcasts;
  130. int stats_orf_token;
  131. struct timeval stats_tv_start;
  132. struct totem_ip_address my_id;
  133. int firstrun;
  134. poll_timer_handle timer_netif_check_timeout;
  135. unsigned int my_memb_entries;
  136. int flushing;
  137. struct totem_config *totem_config;
  138. struct totem_ip_address token_target;
  139. };
  140. struct work_item {
  141. struct iovec iovec[20];
  142. int iov_len;
  143. struct totemnet_instance *instance;
  144. };
  145. static void netif_down_check (struct totemnet_instance *instance);
  146. static int totemnet_build_sockets (
  147. struct totemnet_instance *instance,
  148. struct totem_ip_address *bindnet_address,
  149. struct totem_ip_address *mcastaddress,
  150. struct totemnet_socket *sockets,
  151. struct totem_ip_address *bound_to);
  152. static struct totem_ip_address localhost;
  153. /*
  154. * All instances in one database
  155. */
  156. static struct hdb_handle_database totemnet_instance_database = {
  157. .handle_count = 0,
  158. .handles = 0,
  159. .iterator = 0,
  160. .mutex = PTHREAD_MUTEX_INITIALIZER
  161. };
  162. static void totemnet_instance_initialize (struct totemnet_instance *instance)
  163. {
  164. memset (instance, 0, sizeof (struct totemnet_instance));
  165. instance->netif_state_report = NETIF_STATE_REPORT_UP | NETIF_STATE_REPORT_DOWN;
  166. instance->totemnet_iov_recv.iov_base = instance->iov_buffer;
  167. instance->totemnet_iov_recv.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  168. instance->totemnet_iov_recv_flush.iov_base = instance->iov_buffer_flush;
  169. instance->totemnet_iov_recv_flush.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  170. /*
  171. * There is always atleast 1 processor
  172. */
  173. instance->my_memb_entries = 1;
  174. }
  175. #define log_printf(level, format, args...) \
  176. do { \
  177. instance->totemnet_log_printf (instance->totemnet_subsys_id, \
  178. __FUNCTION__, __FILE__, __LINE__, \
  179. level, (const char *)format, ##args); \
  180. } while (0);
  181. static int authenticate_and_decrypt (
  182. struct totemnet_instance *instance,
  183. struct iovec *iov)
  184. {
  185. unsigned char keys[48];
  186. struct security_header *header = iov[0].iov_base;
  187. prng_state keygen_prng_state;
  188. prng_state stream_prng_state;
  189. unsigned char *hmac_key = &keys[32];
  190. unsigned char *cipher_key = &keys[16];
  191. unsigned char *initial_vector = &keys[0];
  192. unsigned char digest_comparison[HMAC_HASH_SIZE];
  193. unsigned long len;
  194. /*
  195. * Generate MAC, CIPHER, IV keys from private key
  196. */
  197. memset (keys, 0, sizeof (keys));
  198. sober128_start (&keygen_prng_state);
  199. sober128_add_entropy (instance->totemnet_private_key,
  200. instance->totemnet_private_key_len, &keygen_prng_state);
  201. sober128_add_entropy (header->salt, sizeof (header->salt), &keygen_prng_state);
  202. sober128_read (keys, sizeof (keys), &keygen_prng_state);
  203. /*
  204. * Setup stream cipher
  205. */
  206. sober128_start (&stream_prng_state);
  207. sober128_add_entropy (cipher_key, 16, &stream_prng_state);
  208. sober128_add_entropy (initial_vector, 16, &stream_prng_state);
  209. /*
  210. * Authenticate contents of message
  211. */
  212. hmac_init (&instance->totemnet_hmac_state, DIGEST_SHA1, hmac_key, 16);
  213. hmac_process (&instance->totemnet_hmac_state,
  214. (unsigned char *)iov->iov_base + HMAC_HASH_SIZE,
  215. iov->iov_len - HMAC_HASH_SIZE);
  216. len = hash_descriptor[DIGEST_SHA1]->hashsize;
  217. assert (HMAC_HASH_SIZE >= len);
  218. hmac_done (&instance->totemnet_hmac_state, digest_comparison, &len);
  219. if (memcmp (digest_comparison, header->hash_digest, len) != 0) {
  220. log_printf (instance->totemnet_log_level_security, "Received message has invalid digest... ignoring.\n");
  221. return (-1);
  222. }
  223. /*
  224. * Decrypt the contents of the message with the cipher key
  225. */
  226. sober128_read ((unsigned char*)iov->iov_base +
  227. sizeof (struct security_header),
  228. iov->iov_len - sizeof (struct security_header),
  229. &stream_prng_state);
  230. return (0);
  231. }
  232. static void encrypt_and_sign_worker (
  233. struct totemnet_instance *instance,
  234. unsigned char *buf,
  235. int *buf_len,
  236. struct iovec *iovec,
  237. int iov_len,
  238. prng_state *prng_state_in)
  239. {
  240. int i;
  241. unsigned char *addr;
  242. unsigned char keys[48];
  243. struct security_header *header;
  244. unsigned char *hmac_key = &keys[32];
  245. unsigned char *cipher_key = &keys[16];
  246. unsigned char *initial_vector = &keys[0];
  247. unsigned long len;
  248. int outlen = 0;
  249. hmac_state hmac_state;
  250. prng_state keygen_prng_state;
  251. prng_state stream_prng_state;
  252. header = (struct security_header *)buf;
  253. addr = buf + sizeof (struct security_header);
  254. memset (keys, 0, sizeof (keys));
  255. memset (header->salt, 0, sizeof (header->salt));
  256. /*
  257. * Generate MAC, CIPHER, IV keys from private key
  258. */
  259. sober128_read (header->salt, sizeof (header->salt), prng_state_in);
  260. sober128_start (&keygen_prng_state);
  261. sober128_add_entropy (instance->totemnet_private_key,
  262. instance->totemnet_private_key_len,
  263. &keygen_prng_state);
  264. sober128_add_entropy (header->salt, sizeof (header->salt),
  265. &keygen_prng_state);
  266. sober128_read (keys, sizeof (keys), &keygen_prng_state);
  267. /*
  268. * Setup stream cipher
  269. */
  270. sober128_start (&stream_prng_state);
  271. sober128_add_entropy (cipher_key, 16, &stream_prng_state);
  272. sober128_add_entropy (initial_vector, 16, &stream_prng_state);
  273. outlen = sizeof (struct security_header);
  274. /*
  275. * Copy remainder of message, then encrypt it
  276. */
  277. for (i = 1; i < iov_len; i++) {
  278. memcpy (addr, iovec[i].iov_base, iovec[i].iov_len);
  279. addr += iovec[i].iov_len;
  280. outlen += iovec[i].iov_len;
  281. }
  282. /*
  283. * Encrypt message by XORing stream cipher data
  284. */
  285. sober128_read (buf + sizeof (struct security_header),
  286. outlen - sizeof (struct security_header),
  287. &stream_prng_state);
  288. memset (&hmac_state, 0, sizeof (hmac_state));
  289. /*
  290. * Sign the contents of the message with the hmac key and store signature in message
  291. */
  292. hmac_init (&hmac_state, DIGEST_SHA1, hmac_key, 16);
  293. hmac_process (&hmac_state,
  294. buf + HMAC_HASH_SIZE,
  295. outlen - HMAC_HASH_SIZE);
  296. len = hash_descriptor[DIGEST_SHA1]->hashsize;
  297. hmac_done (&hmac_state, header->hash_digest, &len);
  298. *buf_len = outlen;
  299. }
  300. static inline void ucast_sendmsg (
  301. struct totemnet_instance *instance,
  302. struct totem_ip_address *system_to,
  303. struct iovec *iovec_in,
  304. int iov_len_in)
  305. {
  306. struct msghdr msg_ucast;
  307. int res = 0;
  308. int buf_len;
  309. unsigned char sheader[sizeof (struct security_header)];
  310. unsigned char encrypt_data[FRAME_SIZE_MAX];
  311. struct iovec iovec_encrypt[20];
  312. struct iovec *iovec_sendmsg;
  313. struct sockaddr_storage sockaddr;
  314. int iov_len;
  315. int addrlen;
  316. if (instance->totem_config->secauth == 1) {
  317. iovec_encrypt[0].iov_base = sheader;
  318. iovec_encrypt[0].iov_len = sizeof (struct security_header);
  319. memcpy (&iovec_encrypt[1], &iovec_in[0],
  320. sizeof (struct iovec) * iov_len_in);
  321. /*
  322. * Encrypt and digest the message
  323. */
  324. encrypt_and_sign_worker (
  325. instance,
  326. encrypt_data,
  327. &buf_len,
  328. iovec_encrypt,
  329. iov_len_in + 1,
  330. &instance->totemnet_prng_state);
  331. iovec_encrypt[0].iov_base = encrypt_data;
  332. iovec_encrypt[0].iov_len = buf_len;
  333. iovec_sendmsg = &iovec_encrypt[0];
  334. iov_len = 1;
  335. } else {
  336. iovec_sendmsg = iovec_in;
  337. iov_len = iov_len_in;
  338. }
  339. /*
  340. * Build unicast message
  341. */
  342. totemip_totemip_to_sockaddr_convert(system_to,
  343. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  344. msg_ucast.msg_name = &sockaddr;
  345. msg_ucast.msg_namelen = addrlen;
  346. msg_ucast.msg_iov = iovec_sendmsg;
  347. msg_ucast.msg_iovlen = iov_len;
  348. msg_ucast.msg_control = 0;
  349. msg_ucast.msg_controllen = 0;
  350. msg_ucast.msg_flags = 0;
  351. /*
  352. * Transmit multicast message
  353. * An error here is recovered by totemsrp
  354. */
  355. res = sendmsg (instance->totemnet_sockets.mcast_send, &msg_ucast,
  356. MSG_NOSIGNAL);
  357. }
  358. static inline void mcast_sendmsg (
  359. struct totemnet_instance *instance,
  360. struct iovec *iovec_in,
  361. int iov_len_in)
  362. {
  363. struct msghdr msg_mcast;
  364. int res = 0;
  365. int buf_len;
  366. unsigned char sheader[sizeof (struct security_header)];
  367. unsigned char encrypt_data[FRAME_SIZE_MAX];
  368. struct iovec iovec_encrypt[20];
  369. struct iovec *iovec_sendmsg;
  370. struct sockaddr_storage sockaddr;
  371. int iov_len;
  372. int addrlen;
  373. if (instance->totem_config->secauth == 1) {
  374. iovec_encrypt[0].iov_base = sheader;
  375. iovec_encrypt[0].iov_len = sizeof (struct security_header);
  376. memcpy (&iovec_encrypt[1], &iovec_in[0],
  377. sizeof (struct iovec) * iov_len_in);
  378. /*
  379. * Encrypt and digest the message
  380. */
  381. encrypt_and_sign_worker (
  382. instance,
  383. encrypt_data,
  384. &buf_len,
  385. iovec_encrypt,
  386. iov_len_in + 1,
  387. &instance->totemnet_prng_state);
  388. iovec_encrypt[0].iov_base = encrypt_data;
  389. iovec_encrypt[0].iov_len = buf_len;
  390. iovec_sendmsg = &iovec_encrypt[0];
  391. iov_len = 1;
  392. } else {
  393. iovec_sendmsg = iovec_in;
  394. iov_len = iov_len_in;
  395. }
  396. /*
  397. * Build multicast message
  398. */
  399. totemip_totemip_to_sockaddr_convert(&instance->mcast_address,
  400. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  401. msg_mcast.msg_name = &sockaddr;
  402. msg_mcast.msg_namelen = addrlen;
  403. msg_mcast.msg_iov = iovec_sendmsg;
  404. msg_mcast.msg_iovlen = iov_len;
  405. msg_mcast.msg_control = 0;
  406. msg_mcast.msg_controllen = 0;
  407. msg_mcast.msg_flags = 0;
  408. /*
  409. * Transmit multicast message
  410. * An error here is recovered by totemsrp
  411. */
  412. res = sendmsg (instance->totemnet_sockets.mcast_send, &msg_mcast,
  413. MSG_NOSIGNAL);
  414. }
  415. static void totemnet_mcast_thread_state_constructor (
  416. void *totemnet_mcast_thread_state_in)
  417. {
  418. struct totemnet_mcast_thread_state *totemnet_mcast_thread_state =
  419. (struct totemnet_mcast_thread_state *)totemnet_mcast_thread_state_in;
  420. memset (totemnet_mcast_thread_state, 0,
  421. sizeof (totemnet_mcast_thread_state));
  422. rng_make_prng (128, PRNG_SOBER,
  423. &totemnet_mcast_thread_state->prng_state, NULL);
  424. }
  425. static void totemnet_mcast_worker_fn (void *thread_state, void *work_item_in)
  426. {
  427. struct work_item *work_item = (struct work_item *)work_item_in;
  428. struct totemnet_mcast_thread_state *totemnet_mcast_thread_state =
  429. (struct totemnet_mcast_thread_state *)thread_state;
  430. struct totemnet_instance *instance = work_item->instance;
  431. struct msghdr msg_mcast;
  432. unsigned char sheader[sizeof (struct security_header)];
  433. int res = 0;
  434. int buf_len;
  435. struct iovec iovec_encrypted;
  436. struct iovec *iovec_sendmsg;
  437. struct sockaddr_storage sockaddr;
  438. unsigned int iovs;
  439. int addrlen;
  440. if (instance->totem_config->secauth == 1) {
  441. memmove (&work_item->iovec[1], &work_item->iovec[0],
  442. work_item->iov_len * sizeof (struct iovec));
  443. work_item->iovec[0].iov_base = sheader;
  444. work_item->iovec[0].iov_len = sizeof (struct security_header);
  445. /*
  446. * Encrypt and digest the message
  447. */
  448. encrypt_and_sign_worker (
  449. instance,
  450. totemnet_mcast_thread_state->iobuf, &buf_len,
  451. work_item->iovec, work_item->iov_len + 1,
  452. &totemnet_mcast_thread_state->prng_state);
  453. iovec_sendmsg = &iovec_encrypted;
  454. iovec_sendmsg->iov_base = totemnet_mcast_thread_state->iobuf;
  455. iovec_sendmsg->iov_len = buf_len;
  456. iovs = 1;
  457. } else {
  458. iovec_sendmsg = work_item->iovec;
  459. iovs = work_item->iov_len;
  460. }
  461. totemip_totemip_to_sockaddr_convert(&instance->mcast_address,
  462. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  463. msg_mcast.msg_name = &sockaddr;
  464. msg_mcast.msg_namelen = addrlen;
  465. msg_mcast.msg_iov = iovec_sendmsg;
  466. msg_mcast.msg_iovlen = iovs;
  467. msg_mcast.msg_control = 0;
  468. msg_mcast.msg_controllen = 0;
  469. msg_mcast.msg_flags = 0;
  470. /*
  471. * Transmit multicast message
  472. * An error here is recovered by totemnet
  473. */
  474. res = sendmsg (instance->totemnet_sockets.mcast_send, &msg_mcast,
  475. MSG_NOSIGNAL);
  476. if (res > 0) {
  477. instance->stats_sent += res;
  478. }
  479. }
  480. int totemnet_finalize (
  481. hdb_handle_t handle)
  482. {
  483. struct totemnet_instance *instance;
  484. int res = 0;
  485. res = hdb_handle_get (&totemnet_instance_database, handle,
  486. (void *)&instance);
  487. if (res != 0) {
  488. res = ENOENT;
  489. goto error_exit;
  490. }
  491. worker_thread_group_exit (&instance->worker_thread_group);
  492. hdb_handle_put (&totemnet_instance_database, handle);
  493. error_exit:
  494. return (res);
  495. }
  496. /*
  497. * Only designed to work with a message with one iov
  498. */
  499. static int net_deliver_fn (
  500. hdb_handle_t handle,
  501. int fd,
  502. int revents,
  503. void *data)
  504. {
  505. struct totemnet_instance *instance = (struct totemnet_instance *)data;
  506. struct msghdr msg_recv;
  507. struct iovec *iovec;
  508. struct security_header *security_header;
  509. struct sockaddr_storage system_from;
  510. int bytes_received;
  511. int res = 0;
  512. unsigned char *msg_offset;
  513. unsigned int size_delv;
  514. if (instance->flushing == 1) {
  515. iovec = &instance->totemnet_iov_recv_flush;
  516. } else {
  517. iovec = &instance->totemnet_iov_recv;
  518. }
  519. /*
  520. * Receive datagram
  521. */
  522. msg_recv.msg_name = &system_from;
  523. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  524. msg_recv.msg_iov = iovec;
  525. msg_recv.msg_iovlen = 1;
  526. msg_recv.msg_control = 0;
  527. msg_recv.msg_controllen = 0;
  528. msg_recv.msg_flags = 0;
  529. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  530. if (bytes_received == -1) {
  531. return (0);
  532. } else {
  533. instance->stats_recv += bytes_received;
  534. }
  535. if ((instance->totem_config->secauth == 1) &&
  536. (bytes_received < sizeof (struct security_header))) {
  537. log_printf (instance->totemnet_log_level_security, "Received message is too short... ignoring %d.\n", bytes_received);
  538. return (0);
  539. }
  540. security_header = (struct security_header *)iovec->iov_base;
  541. iovec->iov_len = bytes_received;
  542. if (instance->totem_config->secauth == 1) {
  543. /*
  544. * Authenticate and if authenticated, decrypt datagram
  545. */
  546. res = authenticate_and_decrypt (instance, iovec);
  547. if (res == -1) {
  548. log_printf (instance->totemnet_log_level_security,
  549. "Invalid packet data\n");
  550. iovec->iov_len = FRAME_SIZE_MAX;
  551. return 0;
  552. }
  553. msg_offset = (unsigned char *)iovec->iov_base +
  554. sizeof (struct security_header);
  555. size_delv = bytes_received - sizeof (struct security_header);
  556. } else {
  557. msg_offset = iovec->iov_base;
  558. size_delv = bytes_received;
  559. }
  560. /*
  561. * Handle incoming message
  562. */
  563. instance->totemnet_deliver_fn (
  564. instance->context,
  565. msg_offset,
  566. size_delv);
  567. iovec->iov_len = FRAME_SIZE_MAX;
  568. return (0);
  569. }
  570. static int netif_determine (
  571. struct totemnet_instance *instance,
  572. struct totem_ip_address *bindnet,
  573. struct totem_ip_address *bound_to,
  574. int *interface_up,
  575. int *interface_num)
  576. {
  577. int res;
  578. res = totemip_iface_check (bindnet, bound_to,
  579. interface_up, interface_num,
  580. + 0); // TODO andrew can address this instance->totem_config->clear_node_high_bit);
  581. return (res);
  582. }
  583. /*
  584. * If the interface is up, the sockets for totem are built. If the interface is down
  585. * this function is requeued in the timer list to retry building the sockets later.
  586. */
  587. static void timer_function_netif_check_timeout (
  588. void *data)
  589. {
  590. struct totemnet_instance *instance = (struct totemnet_instance *)data;
  591. int res;
  592. int interface_up;
  593. int interface_num;
  594. struct totem_ip_address *bind_address;
  595. /*
  596. * Build sockets for every interface
  597. */
  598. netif_determine (instance,
  599. &instance->totem_interface->bindnet,
  600. &instance->totem_interface->boundto,
  601. &interface_up, &interface_num);
  602. /*
  603. * If the network interface isn't back up and we are already
  604. * in loopback mode, add timer to check again and return
  605. */
  606. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  607. interface_up == 0) ||
  608. (instance->my_memb_entries == 1 &&
  609. instance->netif_bind_state == BIND_STATE_REGULAR &&
  610. interface_up == 1)) {
  611. poll_timer_add (instance->totemnet_poll_handle,
  612. instance->totem_config->downcheck_timeout,
  613. (void *)instance,
  614. timer_function_netif_check_timeout,
  615. &instance->timer_netif_check_timeout);
  616. /*
  617. * Add a timer to check for a downed regular interface
  618. */
  619. return;
  620. }
  621. if (instance->totemnet_sockets.mcast_recv > 0) {
  622. close (instance->totemnet_sockets.mcast_recv);
  623. poll_dispatch_delete (instance->totemnet_poll_handle,
  624. instance->totemnet_sockets.mcast_recv);
  625. }
  626. if (instance->totemnet_sockets.mcast_send > 0) {
  627. close (instance->totemnet_sockets.mcast_send);
  628. }
  629. if (instance->totemnet_sockets.token > 0) {
  630. close (instance->totemnet_sockets.token);
  631. poll_dispatch_delete (instance->totemnet_poll_handle,
  632. instance->totemnet_sockets.token);
  633. }
  634. if (interface_up == 0) {
  635. /*
  636. * Interface is not up
  637. */
  638. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  639. bind_address = &localhost;
  640. /*
  641. * Add a timer to retry building interfaces and request memb_gather_enter
  642. */
  643. poll_timer_add (instance->totemnet_poll_handle,
  644. instance->totem_config->downcheck_timeout,
  645. (void *)instance,
  646. timer_function_netif_check_timeout,
  647. &instance->timer_netif_check_timeout);
  648. } else {
  649. /*
  650. * Interface is up
  651. */
  652. instance->netif_bind_state = BIND_STATE_REGULAR;
  653. bind_address = &instance->totem_interface->bindnet;
  654. }
  655. /*
  656. * Create and bind the multicast and unicast sockets
  657. */
  658. res = totemnet_build_sockets (instance,
  659. &instance->mcast_address,
  660. bind_address,
  661. &instance->totemnet_sockets,
  662. &instance->totem_interface->boundto);
  663. poll_dispatch_add (
  664. instance->totemnet_poll_handle,
  665. instance->totemnet_sockets.mcast_recv,
  666. POLLIN, instance, net_deliver_fn);
  667. poll_dispatch_add (
  668. instance->totemnet_poll_handle,
  669. instance->totemnet_sockets.token,
  670. POLLIN, instance, net_deliver_fn);
  671. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  672. /*
  673. * This reports changes in the interface to the user and totemsrp
  674. */
  675. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  676. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  677. log_printf (instance->totemnet_log_level_notice,
  678. "The network interface [%s] is now up.\n",
  679. totemip_print (&instance->totem_interface->boundto));
  680. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  681. instance->totemnet_iface_change_fn (instance->context, &instance->my_id);
  682. }
  683. /*
  684. * Add a timer to check for interface going down in single membership
  685. */
  686. if (instance->my_memb_entries == 1) {
  687. poll_timer_add (instance->totemnet_poll_handle,
  688. instance->totem_config->downcheck_timeout,
  689. (void *)instance,
  690. timer_function_netif_check_timeout,
  691. &instance->timer_netif_check_timeout);
  692. }
  693. } else {
  694. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  695. log_printf (instance->totemnet_log_level_notice,
  696. "The network interface is down.\n");
  697. instance->totemnet_iface_change_fn (instance->context, &instance->my_id);
  698. }
  699. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  700. }
  701. }
  702. /*
  703. * Check if an interface is down and reconfigure
  704. * totemnet waiting for it to come back up
  705. */
  706. static void netif_down_check (struct totemnet_instance *instance)
  707. {
  708. timer_function_netif_check_timeout (instance);
  709. }
  710. /* Set the socket priority to INTERACTIVE to ensure
  711. that our messages don't get queued behind anything else */
  712. static void totemnet_traffic_control_set(struct totemnet_instance *instance, int sock)
  713. {
  714. #ifdef SO_PRIORITY
  715. int prio = 6; /* TC_PRIO_INTERACTIVE */
  716. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int)))
  717. log_printf (instance->totemnet_log_level_warning, "Could not set traffic priority. (%s)\n", strerror (errno));
  718. #endif
  719. }
  720. static int totemnet_build_sockets_ip (
  721. struct totemnet_instance *instance,
  722. struct totem_ip_address *mcast_address,
  723. struct totem_ip_address *bindnet_address,
  724. struct totemnet_socket *sockets,
  725. struct totem_ip_address *bound_to,
  726. int interface_num)
  727. {
  728. struct sockaddr_storage sockaddr;
  729. struct ipv6_mreq mreq6;
  730. struct ip_mreq mreq;
  731. struct sockaddr_storage mcast_ss, boundto_ss;
  732. struct sockaddr_in6 *mcast_sin6 = (struct sockaddr_in6 *)&mcast_ss;
  733. struct sockaddr_in *mcast_sin = (struct sockaddr_in *)&mcast_ss;
  734. struct sockaddr_in *boundto_sin = (struct sockaddr_in *)&boundto_ss;
  735. unsigned int sendbuf_size;
  736. unsigned int recvbuf_size;
  737. unsigned int optlen = sizeof (sendbuf_size);
  738. int addrlen;
  739. int res;
  740. int flag;
  741. /*
  742. * Create multicast recv socket
  743. */
  744. sockets->mcast_recv = socket (bindnet_address->family, SOCK_DGRAM, 0);
  745. if (sockets->mcast_recv == -1) {
  746. perror ("socket");
  747. return (-1);
  748. }
  749. totemip_nosigpipe (sockets->mcast_recv);
  750. res = fcntl (sockets->mcast_recv, F_SETFL, O_NONBLOCK);
  751. if (res == -1) {
  752. log_printf (instance->totemnet_log_level_warning, "Could not set non-blocking operation on multicast socket: %s\n", strerror (errno));
  753. return (-1);
  754. }
  755. /*
  756. * Force reuse
  757. */
  758. flag = 1;
  759. if ( setsockopt(sockets->mcast_recv, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  760. perror("setsockopt reuseaddr");
  761. return (-1);
  762. }
  763. /*
  764. * Bind to multicast socket used for multicast receives
  765. */
  766. totemip_totemip_to_sockaddr_convert(mcast_address,
  767. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  768. res = bind (sockets->mcast_recv, (struct sockaddr *)&sockaddr, addrlen);
  769. if (res == -1) {
  770. perror ("bind mcast recv socket failed");
  771. return (-1);
  772. }
  773. /*
  774. * Setup mcast send socket
  775. */
  776. sockets->mcast_send = socket (bindnet_address->family, SOCK_DGRAM, 0);
  777. if (sockets->mcast_send == -1) {
  778. perror ("socket");
  779. return (-1);
  780. }
  781. totemip_nosigpipe (sockets->mcast_send);
  782. res = fcntl (sockets->mcast_send, F_SETFL, O_NONBLOCK);
  783. if (res == -1) {
  784. log_printf (instance->totemnet_log_level_warning, "Could not set non-blocking operation on multicast socket: %s\n", strerror (errno));
  785. return (-1);
  786. }
  787. /*
  788. * Force reuse
  789. */
  790. flag = 1;
  791. if ( setsockopt(sockets->mcast_send, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  792. perror("setsockopt reuseaddr");
  793. return (-1);
  794. }
  795. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port - 1,
  796. &sockaddr, &addrlen);
  797. res = bind (sockets->mcast_send, (struct sockaddr *)&sockaddr, addrlen);
  798. if (res == -1) {
  799. perror ("bind mcast send socket failed");
  800. return (-1);
  801. }
  802. /*
  803. * Setup unicast socket
  804. */
  805. sockets->token = socket (bindnet_address->family, SOCK_DGRAM, 0);
  806. if (sockets->token == -1) {
  807. perror ("socket2");
  808. return (-1);
  809. }
  810. totemip_nosigpipe (sockets->token);
  811. res = fcntl (sockets->token, F_SETFL, O_NONBLOCK);
  812. if (res == -1) {
  813. log_printf (instance->totemnet_log_level_warning, "Could not set non-blocking operation on token socket: %s\n", strerror (errno));
  814. return (-1);
  815. }
  816. /*
  817. * Force reuse
  818. */
  819. flag = 1;
  820. if ( setsockopt(sockets->token, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  821. perror("setsockopt reuseaddr");
  822. return (-1);
  823. }
  824. /*
  825. * Bind to unicast socket used for token send/receives
  826. * This has the side effect of binding to the correct interface
  827. */
  828. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  829. res = bind (sockets->token, (struct sockaddr *)&sockaddr, addrlen);
  830. if (res == -1) {
  831. perror ("bind token socket failed");
  832. return (-1);
  833. }
  834. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  835. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  836. /*
  837. * Set buffer sizes to avoid overruns
  838. */
  839. res = setsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  840. res = setsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  841. res = getsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  842. if (res == 0) {
  843. log_printf (instance->totemnet_log_level_notice,
  844. "Receive multicast socket recv buffer size (%d bytes).\n", recvbuf_size);
  845. }
  846. res = getsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  847. if (res == 0) {
  848. log_printf (instance->totemnet_log_level_notice,
  849. "Transmit multicast socket send buffer size (%d bytes).\n", sendbuf_size);
  850. }
  851. /*
  852. * Join group membership on socket
  853. */
  854. totemip_totemip_to_sockaddr_convert(mcast_address, instance->totem_interface->ip_port, &mcast_ss, &addrlen);
  855. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &boundto_ss, &addrlen);
  856. switch ( bindnet_address->family ) {
  857. case AF_INET:
  858. memset(&mreq, 0, sizeof(mreq));
  859. mreq.imr_multiaddr.s_addr = mcast_sin->sin_addr.s_addr;
  860. mreq.imr_interface.s_addr = boundto_sin->sin_addr.s_addr;
  861. res = setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  862. &mreq, sizeof (mreq));
  863. if (res == -1) {
  864. perror ("join ipv4 multicast group failed");
  865. return (-1);
  866. }
  867. break;
  868. case AF_INET6:
  869. memset(&mreq6, 0, sizeof(mreq6));
  870. memcpy(&mreq6.ipv6mr_multiaddr, &mcast_sin6->sin6_addr, sizeof(struct in6_addr));
  871. mreq6.ipv6mr_interface = interface_num;
  872. res = setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_JOIN_GROUP,
  873. &mreq6, sizeof (mreq6));
  874. if (res == -1) {
  875. perror ("join ipv6 multicast group failed");
  876. return (-1);
  877. }
  878. break;
  879. }
  880. /*
  881. * Turn on multicast loopback
  882. */
  883. flag = 1;
  884. switch ( bindnet_address->family ) {
  885. case AF_INET:
  886. res = setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_LOOP,
  887. &flag, sizeof (flag));
  888. break;
  889. case AF_INET6:
  890. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
  891. &flag, sizeof (flag));
  892. }
  893. if (res == -1) {
  894. perror ("turn off loopback");
  895. return (-1);
  896. }
  897. /*
  898. * Set multicast packets TTL
  899. */
  900. if ( bindnet_address->family == AF_INET6 )
  901. {
  902. flag = 255;
  903. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
  904. &flag, sizeof (flag));
  905. if (res == -1) {
  906. perror ("setp mcast hops");
  907. return (-1);
  908. }
  909. }
  910. /*
  911. * Bind to a specific interface for multicast send and receive
  912. */
  913. switch ( bindnet_address->family ) {
  914. case AF_INET:
  915. if (setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_IF,
  916. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  917. perror ("cannot select interface");
  918. return (-1);
  919. }
  920. if (setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_MULTICAST_IF,
  921. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  922. perror ("cannot select interface");
  923. return (-1);
  924. }
  925. break;
  926. case AF_INET6:
  927. if (setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  928. &interface_num, sizeof (interface_num)) < 0) {
  929. perror ("cannot select interface");
  930. return (-1);
  931. }
  932. if (setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  933. &interface_num, sizeof (interface_num)) < 0) {
  934. perror ("cannot select interface");
  935. return (-1);
  936. }
  937. break;
  938. }
  939. return 0;
  940. }
  941. static int totemnet_build_sockets (
  942. struct totemnet_instance *instance,
  943. struct totem_ip_address *mcast_address,
  944. struct totem_ip_address *bindnet_address,
  945. struct totemnet_socket *sockets,
  946. struct totem_ip_address *bound_to)
  947. {
  948. int interface_num;
  949. int interface_up;
  950. int res;
  951. /*
  952. * Determine the ip address bound to and the interface name
  953. */
  954. res = netif_determine (instance,
  955. bindnet_address,
  956. bound_to,
  957. &interface_up,
  958. &interface_num);
  959. if (res == -1) {
  960. return (-1);
  961. }
  962. totemip_copy(&instance->my_id, bound_to);
  963. res = totemnet_build_sockets_ip (instance, mcast_address,
  964. bindnet_address, sockets, bound_to, interface_num);
  965. /* We only send out of the token socket */
  966. totemnet_traffic_control_set(instance, sockets->token);
  967. return res;
  968. }
  969. /*
  970. * Totem Network interface - also does encryption/decryption
  971. * depends on poll abstraction, POSIX, IPV4
  972. */
  973. /*
  974. * Create an instance
  975. */
  976. int totemnet_initialize (
  977. hdb_handle_t poll_handle,
  978. hdb_handle_t *handle,
  979. struct totem_config *totem_config,
  980. int interface_no,
  981. void *context,
  982. void (*deliver_fn) (
  983. void *context,
  984. void *msg,
  985. int msg_len),
  986. void (*iface_change_fn) (
  987. void *context,
  988. struct totem_ip_address *iface_address))
  989. {
  990. struct totemnet_instance *instance;
  991. unsigned int res;
  992. res = hdb_handle_create (&totemnet_instance_database,
  993. sizeof (struct totemnet_instance), handle);
  994. if (res != 0) {
  995. goto error_exit;
  996. }
  997. res = hdb_handle_get (&totemnet_instance_database, *handle,
  998. (void *)&instance);
  999. if (res != 0) {
  1000. goto error_destroy;
  1001. }
  1002. totemnet_instance_initialize (instance);
  1003. instance->totem_config = totem_config;
  1004. /*
  1005. * Configure logging
  1006. */
  1007. instance->totemnet_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  1008. instance->totemnet_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  1009. instance->totemnet_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  1010. instance->totemnet_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  1011. instance->totemnet_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  1012. instance->totemnet_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  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. hdb_handle_t 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 (hdb_handle_t 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 (hdb_handle_t 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. hdb_handle_t 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. hdb_handle_t 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. hdb_handle_t 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 (hdb_handle_t 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. const char *totemnet_iface_print (hdb_handle_t handle) {
  1212. struct totemnet_instance *instance;
  1213. int res = 0;
  1214. const 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 = 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. hdb_handle_t 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. hdb_handle_t 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. }