totemnet.c 37 KB

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