totemnet.c 39 KB

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