totemnet.c 38 KB

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