totemnet.c 41 KB

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