totemnet.c 38 KB

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