totemnet.c 39 KB

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