4
0

totemnet.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054
  1. /*
  2. * Copyright (c) 2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2009 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  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 <config.h>
  35. #include <assert.h>
  36. #include <pthread.h>
  37. #include <sys/mman.h>
  38. #include <sys/types.h>
  39. #include <sys/stat.h>
  40. #include <sys/socket.h>
  41. #include <netdb.h>
  42. #include <sys/un.h>
  43. #include <sys/ioctl.h>
  44. #include <sys/param.h>
  45. #include <netinet/in.h>
  46. #include <arpa/inet.h>
  47. #include <unistd.h>
  48. #include <fcntl.h>
  49. #include <stdlib.h>
  50. #include <stdio.h>
  51. #include <errno.h>
  52. #include <sched.h>
  53. #include <time.h>
  54. #include <sys/time.h>
  55. #include <sys/poll.h>
  56. #include <corosync/sq.h>
  57. #include <corosync/list.h>
  58. #include <corosync/hdb.h>
  59. #include <corosync/swab.h>
  60. #include <corosync/totem/coropoll.h>
  61. #include "totemnet.h"
  62. #include "wthread.h"
  63. #include "crypto.h"
  64. #ifdef HAVE_LIBNSS
  65. #include <nss.h>
  66. #include <pk11pub.h>
  67. #include <pkcs11.h>
  68. #include <prerror.h>
  69. #endif
  70. #define MCAST_SOCKET_BUFFER_SIZE (TRANSMITS_ALLOWED * FRAME_SIZE_MAX)
  71. #define NETIF_STATE_REPORT_UP 1
  72. #define NETIF_STATE_REPORT_DOWN 2
  73. #define BIND_STATE_UNBOUND 0
  74. #define BIND_STATE_REGULAR 1
  75. #define BIND_STATE_LOOPBACK 2
  76. #define HMAC_HASH_SIZE 20
  77. struct security_header {
  78. unsigned char hash_digest[HMAC_HASH_SIZE]; /* The hash *MUST* be first in the data structure */
  79. unsigned char salt[16]; /* random number */
  80. char msg[0];
  81. } __attribute__((packed));
  82. struct totemnet_mcast_thread_state {
  83. unsigned char iobuf[FRAME_SIZE_MAX];
  84. prng_state prng_state;
  85. };
  86. struct totemnet_socket {
  87. int mcast_recv;
  88. int mcast_send;
  89. int token;
  90. };
  91. struct totemnet_instance {
  92. hmac_state totemnet_hmac_state;
  93. prng_state totemnet_prng_state;
  94. #ifdef HAVE_LIBNSS
  95. PK11SymKey *nss_sym_key;
  96. PK11SymKey *nss_sym_key_sign;
  97. #endif
  98. unsigned char totemnet_private_key[1024];
  99. unsigned int totemnet_private_key_len;
  100. hdb_handle_t totemnet_poll_handle;
  101. struct totem_interface *totem_interface;
  102. int netif_state_report;
  103. int netif_bind_state;
  104. struct worker_thread_group worker_thread_group;
  105. void *context;
  106. void (*totemnet_deliver_fn) (
  107. void *context,
  108. const void *msg,
  109. unsigned int msg_len);
  110. void (*totemnet_iface_change_fn) (
  111. void *context,
  112. const struct totem_ip_address *iface_address);
  113. /*
  114. * Function and data used to log messages
  115. */
  116. int totemnet_log_level_security;
  117. int totemnet_log_level_error;
  118. int totemnet_log_level_warning;
  119. int totemnet_log_level_notice;
  120. int totemnet_log_level_debug;
  121. int totemnet_subsys_id;
  122. void (*totemnet_log_printf) (int subsys,
  123. const char *function,
  124. const char *file,
  125. int line, unsigned int level,
  126. unsigned int tag,
  127. const char *format,
  128. ...)__attribute__((format(printf, 7, 8)));
  129. hdb_handle_t handle;
  130. char iov_buffer[FRAME_SIZE_MAX];
  131. char iov_buffer_flush[FRAME_SIZE_MAX];
  132. struct iovec totemnet_iov_recv;
  133. struct iovec totemnet_iov_recv_flush;
  134. struct totemnet_socket totemnet_sockets;
  135. struct totem_ip_address mcast_address;
  136. int stats_sent;
  137. int stats_recv;
  138. int stats_delv;
  139. int stats_remcasts;
  140. int stats_orf_token;
  141. struct timeval stats_tv_start;
  142. struct totem_ip_address my_id;
  143. int firstrun;
  144. poll_timer_handle timer_netif_check_timeout;
  145. unsigned int my_memb_entries;
  146. int flushing;
  147. struct totem_config *totem_config;
  148. struct totem_ip_address token_target;
  149. };
  150. struct work_item {
  151. const void *msg;
  152. unsigned int msg_len;
  153. struct totemnet_instance *instance;
  154. };
  155. static void netif_down_check (struct totemnet_instance *instance);
  156. static int totemnet_build_sockets (
  157. struct totemnet_instance *instance,
  158. struct totem_ip_address *bindnet_address,
  159. struct totem_ip_address *mcastaddress,
  160. struct totemnet_socket *sockets,
  161. struct totem_ip_address *bound_to);
  162. static struct totem_ip_address localhost;
  163. DECLARE_HDB_DATABASE (totemnet_instance_database,NULL);
  164. static void totemnet_instance_initialize (struct totemnet_instance *instance)
  165. {
  166. memset (instance, 0, sizeof (struct totemnet_instance));
  167. instance->netif_state_report = NETIF_STATE_REPORT_UP | NETIF_STATE_REPORT_DOWN;
  168. instance->totemnet_iov_recv.iov_base = instance->iov_buffer;
  169. instance->totemnet_iov_recv.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  170. instance->totemnet_iov_recv_flush.iov_base = instance->iov_buffer_flush;
  171. instance->totemnet_iov_recv_flush.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  172. /*
  173. * There is always atleast 1 processor
  174. */
  175. instance->my_memb_entries = 1;
  176. }
  177. #define log_printf(level, format, args...) \
  178. do { \
  179. instance->totemnet_log_printf (instance->totemnet_subsys_id, \
  180. __FUNCTION__, __FILE__, __LINE__, \
  181. level, 0, (const char *)format, ##args); \
  182. } while (0);
  183. static int authenticate_and_decrypt_sober (
  184. struct totemnet_instance *instance,
  185. struct iovec *iov,
  186. unsigned int iov_len)
  187. {
  188. unsigned char keys[48];
  189. struct security_header *header = iov[0].iov_base;
  190. prng_state keygen_prng_state;
  191. prng_state stream_prng_state;
  192. unsigned char *hmac_key = &keys[32];
  193. unsigned char *cipher_key = &keys[16];
  194. unsigned char *initial_vector = &keys[0];
  195. unsigned char digest_comparison[HMAC_HASH_SIZE];
  196. unsigned long len;
  197. /*
  198. * Generate MAC, CIPHER, IV keys from private key
  199. */
  200. memset (keys, 0, sizeof (keys));
  201. sober128_start (&keygen_prng_state);
  202. sober128_add_entropy (instance->totemnet_private_key,
  203. instance->totemnet_private_key_len, &keygen_prng_state);
  204. sober128_add_entropy (header->salt, sizeof (header->salt), &keygen_prng_state);
  205. sober128_read (keys, sizeof (keys), &keygen_prng_state);
  206. /*
  207. * Setup stream cipher
  208. */
  209. sober128_start (&stream_prng_state);
  210. sober128_add_entropy (cipher_key, 16, &stream_prng_state);
  211. sober128_add_entropy (initial_vector, 16, &stream_prng_state);
  212. /*
  213. * Authenticate contents of message
  214. */
  215. hmac_init (&instance->totemnet_hmac_state, DIGEST_SHA1, hmac_key, 16);
  216. hmac_process (&instance->totemnet_hmac_state,
  217. (unsigned char *)iov->iov_base + HMAC_HASH_SIZE,
  218. iov->iov_len - HMAC_HASH_SIZE);
  219. len = hash_descriptor[DIGEST_SHA1]->hashsize;
  220. assert (HMAC_HASH_SIZE >= len);
  221. hmac_done (&instance->totemnet_hmac_state, digest_comparison, &len);
  222. if (memcmp (digest_comparison, header->hash_digest, len) != 0) {
  223. return (-1);
  224. }
  225. /*
  226. * Decrypt the contents of the message with the cipher key
  227. */
  228. sober128_read ((unsigned char*)iov->iov_base +
  229. sizeof (struct security_header),
  230. iov->iov_len - sizeof (struct security_header),
  231. &stream_prng_state);
  232. return (0);
  233. }
  234. static void init_sober_crypto(
  235. struct totemnet_instance *instance)
  236. {
  237. log_printf(instance->totemnet_log_level_notice,
  238. "Initializing transmit/receive security: libtomcrypt SOBER128/SHA1HMAC (mode 0).\n");
  239. rng_make_prng (128, PRNG_SOBER, &instance->totemnet_prng_state, NULL);
  240. }
  241. #ifdef HAVE_LIBNSS
  242. static unsigned char *copy_from_iovec(
  243. const struct iovec *iov,
  244. unsigned int iov_len,
  245. size_t *buf_size)
  246. {
  247. int i;
  248. size_t bufptr;
  249. size_t buflen = 0;
  250. unsigned char *newbuf;
  251. for (i=0; i<iov_len; i++)
  252. buflen += iov[i].iov_len;
  253. newbuf = malloc(buflen);
  254. if (!newbuf)
  255. return NULL;
  256. bufptr=0;
  257. for (i=0; i<iov_len; i++) {
  258. memcpy(newbuf+bufptr, iov[i].iov_base, iov[i].iov_len);
  259. bufptr += iov[i].iov_len;
  260. }
  261. *buf_size = buflen;
  262. return newbuf;
  263. }
  264. static void copy_to_iovec(
  265. struct iovec *iov,
  266. unsigned int iov_len,
  267. const unsigned char *buf,
  268. size_t buf_size)
  269. {
  270. int i;
  271. size_t copylen;
  272. size_t bufptr = 0;
  273. bufptr=0;
  274. for (i=0; i<iov_len; i++) {
  275. copylen = iov[i].iov_len;
  276. if (bufptr + copylen > buf_size) {
  277. copylen = buf_size - bufptr;
  278. }
  279. memcpy(iov[i].iov_base, buf+bufptr, copylen);
  280. bufptr += copylen;
  281. if (iov[i].iov_len != copylen) {
  282. iov[i].iov_len = copylen;
  283. return;
  284. }
  285. }
  286. }
  287. static void init_nss_crypto(
  288. struct totemnet_instance *instance)
  289. {
  290. PK11SlotInfo* aes_slot = NULL;
  291. PK11SlotInfo* sha1_slot = NULL;
  292. SECItem key_item;
  293. SECStatus rv;
  294. log_printf(instance->totemnet_log_level_notice,
  295. "Initializing transmit/receive security: NSS AES128CBC/SHA1HMAC (mode 1).\n");
  296. rv = NSS_NoDB_Init(".");
  297. if (rv != SECSuccess)
  298. {
  299. log_printf(instance->totemnet_log_level_security, "NSS initialization failed (err %d)\n",
  300. PR_GetError());
  301. goto out;
  302. }
  303. aes_slot = PK11_GetBestSlot(instance->totem_config->crypto_crypt_type, NULL);
  304. if (aes_slot == NULL)
  305. {
  306. log_printf(instance->totemnet_log_level_security, "Unable to find security slot (err %d)\n",
  307. PR_GetError());
  308. goto out;
  309. }
  310. sha1_slot = PK11_GetBestSlot(CKM_SHA_1_HMAC, NULL);
  311. if (sha1_slot == NULL)
  312. {
  313. log_printf(instance->totemnet_log_level_security, "Unable to find security slot (err %d)\n",
  314. PR_GetError());
  315. goto out;
  316. }
  317. /*
  318. * Make the private key into a SymKey that we can use
  319. */
  320. key_item.type = siBuffer;
  321. key_item.data = instance->totem_config->private_key;
  322. key_item.len = 32; /* Use 128 bits */
  323. instance->nss_sym_key = PK11_ImportSymKey(aes_slot,
  324. instance->totem_config->crypto_crypt_type,
  325. PK11_OriginUnwrap, CKA_ENCRYPT|CKA_DECRYPT,
  326. &key_item, NULL);
  327. if (instance->nss_sym_key == NULL)
  328. {
  329. log_printf(instance->totemnet_log_level_security, "Failure to import key into NSS (err %d)\n",
  330. PR_GetError());
  331. goto out;
  332. }
  333. instance->nss_sym_key_sign = PK11_ImportSymKey(sha1_slot,
  334. CKM_SHA_1_HMAC,
  335. PK11_OriginUnwrap, CKA_SIGN,
  336. &key_item, NULL);
  337. if (instance->nss_sym_key_sign == NULL) {
  338. log_printf(instance->totemnet_log_level_security, "Failure to import key into NSS (err %d)\n",
  339. PR_GetError());
  340. goto out;
  341. }
  342. out:
  343. return;
  344. }
  345. static int encrypt_and_sign_nss (
  346. struct totemnet_instance *instance,
  347. unsigned char *buf,
  348. size_t *buf_len,
  349. const struct iovec *iovec,
  350. unsigned int iov_len)
  351. {
  352. PK11Context* enc_context = NULL;
  353. SECStatus rv1, rv2;
  354. int tmp1_outlen;
  355. unsigned int tmp2_outlen;
  356. unsigned char *inbuf;
  357. unsigned char *data;
  358. unsigned char *outdata;
  359. size_t datalen;
  360. SECItem no_params;
  361. SECItem iv_item;
  362. struct security_header *header;
  363. SECItem *nss_sec_param;
  364. unsigned char nss_iv_data[16];
  365. SECStatus rv;
  366. no_params.type = siBuffer;
  367. no_params.data = 0;
  368. no_params.len = 0;
  369. tmp1_outlen = tmp2_outlen = 0;
  370. inbuf = copy_from_iovec(iovec, iov_len, &datalen);
  371. if (!inbuf) {
  372. log_printf(instance->totemnet_log_level_security, "malloc error copying buffer from iovec\n");
  373. return -1;
  374. }
  375. data = inbuf + sizeof (struct security_header);
  376. datalen -= sizeof (struct security_header);
  377. outdata = buf + sizeof (struct security_header);
  378. header = (struct security_header *)buf;
  379. rv = PK11_GenerateRandom (
  380. nss_iv_data,
  381. sizeof (nss_iv_data));
  382. if (rv != SECSuccess) {
  383. log_printf(instance->totemnet_log_level_security,
  384. "Failure to generate a random number %d\n",
  385. PR_GetError());
  386. }
  387. memcpy(header->salt, nss_iv_data, sizeof(nss_iv_data));
  388. iv_item.type = siBuffer;
  389. iv_item.data = nss_iv_data;
  390. iv_item.len = sizeof (nss_iv_data);
  391. nss_sec_param = PK11_ParamFromIV (
  392. instance->totem_config->crypto_crypt_type,
  393. &iv_item);
  394. if (nss_sec_param == NULL) {
  395. log_printf(instance->totemnet_log_level_security,
  396. "Failure to set up PKCS11 param (err %d)\n",
  397. PR_GetError());
  398. goto out;
  399. }
  400. /*
  401. * Create cipher context for encryption
  402. */
  403. enc_context = PK11_CreateContextBySymKey (
  404. instance->totem_config->crypto_crypt_type,
  405. CKA_ENCRYPT,
  406. instance->nss_sym_key,
  407. nss_sec_param);
  408. if (!enc_context) {
  409. char err[1024];
  410. PR_GetErrorText(err);
  411. err[PR_GetErrorTextLength()] = 0;
  412. log_printf(instance->totemnet_log_level_security,
  413. "PK11_CreateContext failed (encrypt) crypt_type=%d (err %d): %s\n",
  414. instance->totem_config->crypto_crypt_type,
  415. PR_GetError(), err);
  416. return -1;
  417. }
  418. rv1 = PK11_CipherOp(enc_context, outdata,
  419. &tmp1_outlen, FRAME_SIZE_MAX - sizeof(struct security_header),
  420. data, datalen);
  421. rv2 = PK11_DigestFinal(enc_context, outdata + tmp1_outlen, &tmp2_outlen,
  422. FRAME_SIZE_MAX - tmp1_outlen);
  423. PK11_DestroyContext(enc_context, PR_TRUE);
  424. *buf_len = tmp1_outlen + tmp2_outlen;
  425. free(inbuf);
  426. // memcpy(&outdata[*buf_len], nss_iv_data, sizeof(nss_iv_data));
  427. if (rv1 != SECSuccess || rv2 != SECSuccess)
  428. goto out;
  429. /* Now do the digest */
  430. enc_context = PK11_CreateContextBySymKey(CKM_SHA_1_HMAC,
  431. CKA_SIGN, instance->nss_sym_key_sign, &no_params);
  432. if (!enc_context) {
  433. char err[1024];
  434. PR_GetErrorText(err);
  435. err[PR_GetErrorTextLength()] = 0;
  436. log_printf(instance->totemnet_log_level_security, "encrypt: PK11_CreateContext failed (digest) err %d: %s\n",
  437. PR_GetError(), err);
  438. return -1;
  439. }
  440. PK11_DigestBegin(enc_context);
  441. rv1 = PK11_DigestOp(enc_context, outdata - 16, *buf_len + 16);
  442. rv2 = PK11_DigestFinal(enc_context, header->hash_digest, &tmp2_outlen, sizeof(header->hash_digest));
  443. PK11_DestroyContext(enc_context, PR_TRUE);
  444. if (rv1 != SECSuccess || rv2 != SECSuccess)
  445. goto out;
  446. *buf_len = *buf_len + sizeof(struct security_header);
  447. SECITEM_FreeItem(nss_sec_param, PR_TRUE);
  448. return 0;
  449. out:
  450. return -1;
  451. }
  452. static int authenticate_and_decrypt_nss (
  453. struct totemnet_instance *instance,
  454. struct iovec *iov,
  455. unsigned int iov_len)
  456. {
  457. PK11Context* enc_context = NULL;
  458. SECStatus rv1, rv2;
  459. int tmp1_outlen;
  460. unsigned int tmp2_outlen;
  461. unsigned char outbuf[FRAME_SIZE_MAX];
  462. unsigned char digest[HMAC_HASH_SIZE];
  463. unsigned char *outdata;
  464. int result_len;
  465. unsigned char *data;
  466. unsigned char *inbuf;
  467. size_t datalen;
  468. struct security_header *header = iov[0].iov_base;
  469. SECItem no_params;
  470. SECItem ivdata;
  471. no_params.type = siBuffer;
  472. no_params.data = 0;
  473. no_params.len = 0;
  474. tmp1_outlen = tmp2_outlen = 0;
  475. if (iov_len > 1) {
  476. inbuf = copy_from_iovec(iov, iov_len, &datalen);
  477. if (!inbuf) {
  478. log_printf(instance->totemnet_log_level_security, "malloc error copying buffer from iovec\n");
  479. return -1;
  480. }
  481. }
  482. else {
  483. inbuf = iov[0].iov_base;
  484. datalen = iov[0].iov_len;
  485. }
  486. data = inbuf + sizeof (struct security_header) - 16;
  487. datalen = datalen - sizeof (struct security_header) + 16;
  488. outdata = outbuf + sizeof (struct security_header);
  489. /* Check the digest */
  490. enc_context = PK11_CreateContextBySymKey (
  491. CKM_SHA_1_HMAC, CKA_SIGN,
  492. instance->nss_sym_key_sign,
  493. &no_params);
  494. if (!enc_context) {
  495. char err[1024];
  496. PR_GetErrorText(err);
  497. err[PR_GetErrorTextLength()] = 0;
  498. log_printf(instance->totemnet_log_level_security, "PK11_CreateContext failed (check digest) err %d: %s\n",
  499. PR_GetError(), err);
  500. return -1;
  501. }
  502. PK11_DigestBegin(enc_context);
  503. rv1 = PK11_DigestOp(enc_context, data, datalen);
  504. rv2 = PK11_DigestFinal(enc_context, digest, &tmp2_outlen, sizeof(digest));
  505. PK11_DestroyContext(enc_context, PR_TRUE);
  506. if (rv1 != SECSuccess || rv2 != SECSuccess) {
  507. log_printf(instance->totemnet_log_level_security, "Digest check failed\n");
  508. return -1;
  509. }
  510. if (memcmp(digest, header->hash_digest, tmp2_outlen) != 0) {
  511. log_printf(instance->totemnet_log_level_error, "Digest does not match\n");
  512. return -1;
  513. }
  514. /*
  515. * Get rid of salt
  516. */
  517. data += 16;
  518. datalen -= 16;
  519. /* Create cipher context for decryption */
  520. ivdata.type = siBuffer;
  521. ivdata.data = header->salt;
  522. ivdata.len = sizeof(header->salt);
  523. enc_context = PK11_CreateContextBySymKey(
  524. instance->totem_config->crypto_crypt_type,
  525. CKA_DECRYPT,
  526. instance->nss_sym_key, &ivdata);
  527. if (!enc_context) {
  528. log_printf(instance->totemnet_log_level_security,
  529. "PK11_CreateContext (decrypt) failed (err %d)\n",
  530. PR_GetError());
  531. return -1;
  532. }
  533. rv1 = PK11_CipherOp(enc_context, outdata, &tmp1_outlen,
  534. sizeof(outbuf) - sizeof (struct security_header),
  535. data, datalen);
  536. if (rv1 != SECSuccess) {
  537. log_printf(instance->totemnet_log_level_security,
  538. "PK11_CipherOp (decrypt) failed (err %d)\n",
  539. PR_GetError());
  540. }
  541. rv2 = PK11_DigestFinal(enc_context, outdata + tmp1_outlen, &tmp2_outlen,
  542. sizeof(outbuf) - tmp1_outlen);
  543. PK11_DestroyContext(enc_context, PR_TRUE);
  544. result_len = tmp1_outlen + tmp2_outlen + sizeof (struct security_header);
  545. /* Copy it back to the buffer */
  546. copy_to_iovec(iov, iov_len, outbuf, result_len);
  547. if (iov_len > 1)
  548. free(inbuf);
  549. if (rv1 != SECSuccess || rv2 != SECSuccess)
  550. return -1;
  551. return 0;
  552. }
  553. #endif
  554. static int encrypt_and_sign_sober (
  555. struct totemnet_instance *instance,
  556. unsigned char *buf,
  557. size_t *buf_len,
  558. const struct iovec *iovec,
  559. unsigned int iov_len)
  560. {
  561. int i;
  562. unsigned char *addr;
  563. unsigned char keys[48];
  564. struct security_header *header;
  565. unsigned char *hmac_key = &keys[32];
  566. unsigned char *cipher_key = &keys[16];
  567. unsigned char *initial_vector = &keys[0];
  568. unsigned long len;
  569. size_t outlen = 0;
  570. hmac_state hmac_st;
  571. prng_state keygen_prng_state;
  572. prng_state stream_prng_state;
  573. prng_state *prng_state_in = &instance->totemnet_prng_state;
  574. header = (struct security_header *)buf;
  575. addr = buf + sizeof (struct security_header);
  576. memset (keys, 0, sizeof (keys));
  577. memset (header->salt, 0, sizeof (header->salt));
  578. /*
  579. * Generate MAC, CIPHER, IV keys from private key
  580. */
  581. sober128_read (header->salt, sizeof (header->salt), prng_state_in);
  582. sober128_start (&keygen_prng_state);
  583. sober128_add_entropy (instance->totemnet_private_key,
  584. instance->totemnet_private_key_len,
  585. &keygen_prng_state);
  586. sober128_add_entropy (header->salt, sizeof (header->salt),
  587. &keygen_prng_state);
  588. sober128_read (keys, sizeof (keys), &keygen_prng_state);
  589. /*
  590. * Setup stream cipher
  591. */
  592. sober128_start (&stream_prng_state);
  593. sober128_add_entropy (cipher_key, 16, &stream_prng_state);
  594. sober128_add_entropy (initial_vector, 16, &stream_prng_state);
  595. outlen = sizeof (struct security_header);
  596. /*
  597. * Copy remainder of message, then encrypt it
  598. */
  599. for (i = 1; i < iov_len; i++) {
  600. memcpy (addr, iovec[i].iov_base, iovec[i].iov_len);
  601. addr += iovec[i].iov_len;
  602. outlen += iovec[i].iov_len;
  603. }
  604. /*
  605. * Encrypt message by XORing stream cipher data
  606. */
  607. sober128_read (buf + sizeof (struct security_header),
  608. outlen - sizeof (struct security_header),
  609. &stream_prng_state);
  610. memset (&hmac_st, 0, sizeof (hmac_st));
  611. /*
  612. * Sign the contents of the message with the hmac key and store signature in message
  613. */
  614. hmac_init (&hmac_st, DIGEST_SHA1, hmac_key, 16);
  615. hmac_process (&hmac_st,
  616. buf + HMAC_HASH_SIZE,
  617. outlen - HMAC_HASH_SIZE);
  618. len = hash_descriptor[DIGEST_SHA1]->hashsize;
  619. hmac_done (&hmac_st, header->hash_digest, &len);
  620. *buf_len = outlen;
  621. return 0;
  622. }
  623. static int encrypt_and_sign_worker (
  624. struct totemnet_instance *instance,
  625. unsigned char *buf,
  626. size_t *buf_len,
  627. const struct iovec *iovec,
  628. unsigned int iov_len)
  629. {
  630. if (instance->totem_config->crypto_type == TOTEM_CRYPTO_SOBER ||
  631. instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_OLD)
  632. return encrypt_and_sign_sober(instance, buf, buf_len, iovec, iov_len);
  633. #ifdef HAVE_LIBNSS
  634. if (instance->totem_config->crypto_type == TOTEM_CRYPTO_NSS)
  635. return encrypt_and_sign_nss(instance, buf, buf_len, iovec, iov_len);
  636. #endif
  637. return -1;
  638. }
  639. static int authenticate_and_decrypt (
  640. struct totemnet_instance *instance,
  641. struct iovec *iov,
  642. unsigned int iov_len)
  643. {
  644. unsigned char type;
  645. unsigned char *endbuf = iov[iov_len-1].iov_base;
  646. int res = -1;
  647. /*
  648. * Get the encryption type and remove it from the buffer
  649. */
  650. type = endbuf[iov[iov_len-1].iov_len-1];
  651. iov[iov_len-1].iov_len -= 1;
  652. if (type == TOTEM_CRYPTO_SOBER)
  653. res = authenticate_and_decrypt_sober(instance, iov, iov_len);
  654. /*
  655. * Only try higher crypto options if NEW has been requested
  656. */
  657. if (instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_NEW) {
  658. #ifdef HAVE_LIBNSS
  659. if (type == TOTEM_CRYPTO_NSS)
  660. res = authenticate_and_decrypt_nss(instance, iov, iov_len);
  661. #endif
  662. }
  663. /*
  664. * If it failed, then try decrypting the whole packet as it might be
  665. * from aisexec
  666. */
  667. if (res == -1) {
  668. iov[iov_len-1].iov_len += 1;
  669. res = authenticate_and_decrypt_sober(instance, iov, iov_len);
  670. }
  671. return res;
  672. }
  673. static void init_crypto(
  674. struct totemnet_instance *instance)
  675. {
  676. /*
  677. * If we are expecting NEW crypto type then initialise all available
  678. * crypto options. For OLD then we only need SOBER128.
  679. */
  680. init_sober_crypto(instance);
  681. if (instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_OLD)
  682. return;
  683. #ifdef HAVE_LIBNSS
  684. init_nss_crypto(instance);
  685. #endif
  686. }
  687. int totemnet_crypto_set (hdb_handle_t handle,
  688. unsigned int type)
  689. {
  690. struct totemnet_instance *instance;
  691. int res = 0;
  692. res = hdb_handle_get (&totemnet_instance_database, handle,
  693. (void *)&instance);
  694. if (res != 0) {
  695. res = ENOENT;
  696. goto error_exit;
  697. }
  698. /*
  699. * Can't set crypto type if OLD is selected
  700. */
  701. if (instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_OLD) {
  702. res = -1;
  703. } else {
  704. /*
  705. * Validate crypto algorithm
  706. */
  707. switch (type) {
  708. case TOTEM_CRYPTO_SOBER:
  709. log_printf(instance->totemnet_log_level_security,
  710. "Transmit security set to: libtomcrypt SOBER128/SHA1HMAC (mode 0)");
  711. break;
  712. case TOTEM_CRYPTO_NSS:
  713. log_printf(instance->totemnet_log_level_security,
  714. "Transmit security set to: NSS AES128CBC/SHA1HMAC (mode 1)");
  715. break;
  716. default:
  717. res = -1;
  718. break;
  719. }
  720. }
  721. hdb_handle_put (&totemnet_instance_database, handle);
  722. error_exit:
  723. return res;
  724. }
  725. static inline void ucast_sendmsg (
  726. struct totemnet_instance *instance,
  727. struct totem_ip_address *system_to,
  728. const void *msg,
  729. unsigned int msg_len)
  730. {
  731. struct msghdr msg_ucast;
  732. int res = 0;
  733. size_t buf_len;
  734. unsigned char sheader[sizeof (struct security_header)];
  735. unsigned char encrypt_data[FRAME_SIZE_MAX];
  736. struct iovec iovec_encrypt[2];
  737. const struct iovec *iovec_sendmsg;
  738. struct sockaddr_storage sockaddr;
  739. struct iovec iovec;
  740. unsigned int iov_len;
  741. int addrlen;
  742. if (instance->totem_config->secauth == 1) {
  743. iovec_encrypt[0].iov_base = sheader;
  744. iovec_encrypt[0].iov_len = sizeof (struct security_header);
  745. iovec_encrypt[1].iov_base = (void *)msg;
  746. iovec_encrypt[1].iov_len = msg_len;
  747. /*
  748. * Encrypt and digest the message
  749. */
  750. encrypt_and_sign_worker (
  751. instance,
  752. encrypt_data,
  753. &buf_len,
  754. iovec_encrypt,
  755. 2);
  756. if (instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_NEW) {
  757. encrypt_data[buf_len++] = instance->totem_config->crypto_type;
  758. }
  759. else {
  760. encrypt_data[buf_len++] = 0;
  761. }
  762. iovec_encrypt[0].iov_base = encrypt_data;
  763. iovec_encrypt[0].iov_len = buf_len;
  764. iovec_sendmsg = &iovec_encrypt[0];
  765. iov_len = 1;
  766. } else {
  767. iovec.iov_base = (void *)msg;
  768. iovec.iov_len = msg_len;
  769. iovec_sendmsg = &iovec;
  770. iov_len = 1;
  771. }
  772. /*
  773. * Build unicast message
  774. */
  775. totemip_totemip_to_sockaddr_convert(system_to,
  776. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  777. msg_ucast.msg_name = &sockaddr;
  778. msg_ucast.msg_namelen = addrlen;
  779. msg_ucast.msg_iov = (void *) iovec_sendmsg;
  780. msg_ucast.msg_iovlen = iov_len;
  781. #if !defined(COROSYNC_SOLARIS)
  782. msg_ucast.msg_control = 0;
  783. msg_ucast.msg_controllen = 0;
  784. msg_ucast.msg_flags = 0;
  785. #endif
  786. /*
  787. * Transmit multicast message
  788. * An error here is recovered by totemsrp
  789. */
  790. res = sendmsg (instance->totemnet_sockets.mcast_send, &msg_ucast,
  791. MSG_NOSIGNAL);
  792. }
  793. static inline void mcast_sendmsg (
  794. struct totemnet_instance *instance,
  795. const void *msg,
  796. unsigned int msg_len)
  797. {
  798. struct msghdr msg_mcast;
  799. int res = 0;
  800. size_t buf_len;
  801. unsigned char sheader[sizeof (struct security_header)];
  802. unsigned char encrypt_data[FRAME_SIZE_MAX];
  803. struct iovec iovec_encrypt[2];
  804. struct iovec iovec;
  805. const struct iovec *iovec_sendmsg;
  806. struct sockaddr_storage sockaddr;
  807. unsigned int iov_len;
  808. int addrlen;
  809. if (instance->totem_config->secauth == 1) {
  810. iovec_encrypt[0].iov_base = sheader;
  811. iovec_encrypt[0].iov_len = sizeof (struct security_header);
  812. iovec_encrypt[1].iov_base = (void *)msg;
  813. iovec_encrypt[1].iov_len = msg_len;
  814. /*
  815. * Encrypt and digest the message
  816. */
  817. encrypt_and_sign_worker (
  818. instance,
  819. encrypt_data,
  820. &buf_len,
  821. iovec_encrypt,
  822. 2);
  823. if (instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_NEW) {
  824. encrypt_data[buf_len++] = instance->totem_config->crypto_type;
  825. }
  826. else {
  827. encrypt_data[buf_len++] = 0;
  828. }
  829. iovec_encrypt[0].iov_base = encrypt_data;
  830. iovec_encrypt[0].iov_len = buf_len;
  831. iovec_sendmsg = &iovec_encrypt[0];
  832. iov_len = 1;
  833. } else {
  834. iovec.iov_base = (void *)msg;
  835. iovec.iov_len = msg_len;
  836. iovec_sendmsg = &iovec;
  837. iov_len = 1;
  838. }
  839. /*
  840. * Build multicast message
  841. */
  842. totemip_totemip_to_sockaddr_convert(&instance->mcast_address,
  843. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  844. msg_mcast.msg_name = &sockaddr;
  845. msg_mcast.msg_namelen = addrlen;
  846. msg_mcast.msg_iov = (void *) iovec_sendmsg;
  847. msg_mcast.msg_iovlen = iov_len;
  848. #if !defined(COROSYNC_SOLARIS)
  849. msg_mcast.msg_control = 0;
  850. msg_mcast.msg_controllen = 0;
  851. msg_mcast.msg_flags = 0;
  852. #endif
  853. /*
  854. * Transmit multicast message
  855. * An error here is recovered by totemsrp
  856. */
  857. res = sendmsg (instance->totemnet_sockets.mcast_send, &msg_mcast,
  858. MSG_NOSIGNAL);
  859. }
  860. static void totemnet_mcast_thread_state_constructor (
  861. void *totemnet_mcast_thread_state_in)
  862. {
  863. struct totemnet_mcast_thread_state *totemnet_mcast_thread_state =
  864. (struct totemnet_mcast_thread_state *)totemnet_mcast_thread_state_in;
  865. memset (totemnet_mcast_thread_state, 0,
  866. sizeof (totemnet_mcast_thread_state));
  867. rng_make_prng (128, PRNG_SOBER,
  868. &totemnet_mcast_thread_state->prng_state, NULL);
  869. }
  870. static void totemnet_mcast_worker_fn (void *thread_state, void *work_item_in)
  871. {
  872. struct work_item *work_item = (struct work_item *)work_item_in;
  873. struct totemnet_mcast_thread_state *totemnet_mcast_thread_state =
  874. (struct totemnet_mcast_thread_state *)thread_state;
  875. struct totemnet_instance *instance = work_item->instance;
  876. struct msghdr msg_mcast;
  877. unsigned char sheader[sizeof (struct security_header)];
  878. int res = 0;
  879. size_t buf_len;
  880. struct iovec iovec_enc[2];
  881. struct iovec iovec;
  882. struct sockaddr_storage sockaddr;
  883. int addrlen;
  884. if (instance->totem_config->secauth == 1) {
  885. iovec_enc[0].iov_base = sheader;
  886. iovec_enc[0].iov_len = sizeof (struct security_header);
  887. iovec_enc[1].iov_base = (void *)work_item->msg;
  888. iovec_enc[1].iov_len = work_item->msg_len;
  889. /*
  890. * Encrypt and digest the message
  891. */
  892. encrypt_and_sign_worker (
  893. instance,
  894. totemnet_mcast_thread_state->iobuf,
  895. &buf_len,
  896. iovec_enc, 2);
  897. iovec.iov_base = totemnet_mcast_thread_state->iobuf;
  898. iovec.iov_len = buf_len;
  899. } else {
  900. iovec.iov_base = (void *)work_item->msg;
  901. iovec.iov_len = work_item->msg_len;
  902. }
  903. totemip_totemip_to_sockaddr_convert(&instance->mcast_address,
  904. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  905. msg_mcast.msg_name = &sockaddr;
  906. msg_mcast.msg_namelen = addrlen;
  907. msg_mcast.msg_iov = &iovec;
  908. msg_mcast.msg_iovlen = 1;
  909. #if !defined(COROSYNC_SOLARIS)
  910. msg_mcast.msg_control = 0;
  911. msg_mcast.msg_controllen = 0;
  912. msg_mcast.msg_flags = 0;
  913. #endif
  914. /*
  915. * Transmit multicast message
  916. * An error here is recovered by totemnet
  917. */
  918. res = sendmsg (instance->totemnet_sockets.mcast_send, &msg_mcast,
  919. MSG_NOSIGNAL);
  920. }
  921. int totemnet_finalize (
  922. hdb_handle_t handle)
  923. {
  924. struct totemnet_instance *instance;
  925. int res = 0;
  926. res = hdb_handle_get (&totemnet_instance_database, handle,
  927. (void *)&instance);
  928. if (res != 0) {
  929. res = ENOENT;
  930. goto error_exit;
  931. }
  932. worker_thread_group_exit (&instance->worker_thread_group);
  933. if (instance->totemnet_sockets.mcast_recv > 0) {
  934. close (instance->totemnet_sockets.mcast_recv);
  935. poll_dispatch_delete (instance->totemnet_poll_handle,
  936. instance->totemnet_sockets.mcast_recv);
  937. }
  938. if (instance->totemnet_sockets.mcast_send > 0) {
  939. close (instance->totemnet_sockets.mcast_send);
  940. }
  941. if (instance->totemnet_sockets.token > 0) {
  942. close (instance->totemnet_sockets.token);
  943. poll_dispatch_delete (instance->totemnet_poll_handle,
  944. instance->totemnet_sockets.token);
  945. }
  946. hdb_handle_put (&totemnet_instance_database, handle);
  947. error_exit:
  948. return (res);
  949. }
  950. /*
  951. * Only designed to work with a message with one iov
  952. */
  953. static int net_deliver_fn (
  954. hdb_handle_t handle,
  955. int fd,
  956. int revents,
  957. void *data)
  958. {
  959. struct totemnet_instance *instance = (struct totemnet_instance *)data;
  960. struct msghdr msg_recv;
  961. struct iovec *iovec;
  962. struct security_header *security_header;
  963. struct sockaddr_storage system_from;
  964. int bytes_received;
  965. int res = 0;
  966. unsigned char *msg_offset;
  967. unsigned int size_delv;
  968. if (instance->flushing == 1) {
  969. iovec = &instance->totemnet_iov_recv_flush;
  970. } else {
  971. iovec = &instance->totemnet_iov_recv;
  972. }
  973. /*
  974. * Receive datagram
  975. */
  976. msg_recv.msg_name = &system_from;
  977. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  978. msg_recv.msg_iov = iovec;
  979. msg_recv.msg_iovlen = 1;
  980. #if !defined(COROSYNC_SOLARIS)
  981. msg_recv.msg_control = 0;
  982. msg_recv.msg_controllen = 0;
  983. msg_recv.msg_flags = 0;
  984. #endif
  985. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  986. if (bytes_received == -1) {
  987. return (0);
  988. } else {
  989. instance->stats_recv += bytes_received;
  990. }
  991. if ((instance->totem_config->secauth == 1) &&
  992. (bytes_received < sizeof (struct security_header))) {
  993. log_printf (instance->totemnet_log_level_security, "Received message is too short... ignoring %d.\n", bytes_received);
  994. return (0);
  995. }
  996. security_header = (struct security_header *)iovec->iov_base;
  997. iovec->iov_len = bytes_received;
  998. if (instance->totem_config->secauth == 1) {
  999. /*
  1000. * Authenticate and if authenticated, decrypt datagram
  1001. */
  1002. res = authenticate_and_decrypt (instance, iovec, 1);
  1003. if (res == -1) {
  1004. log_printf (instance->totemnet_log_level_security, "Received message has invalid digest... ignoring.\n");
  1005. log_printf (instance->totemnet_log_level_security,
  1006. "Invalid packet data\n");
  1007. iovec->iov_len = FRAME_SIZE_MAX;
  1008. return 0;
  1009. }
  1010. msg_offset = (unsigned char *)iovec->iov_base +
  1011. sizeof (struct security_header);
  1012. size_delv = bytes_received - sizeof (struct security_header);
  1013. } else {
  1014. msg_offset = iovec->iov_base;
  1015. size_delv = bytes_received;
  1016. }
  1017. /*
  1018. * Handle incoming message
  1019. */
  1020. instance->totemnet_deliver_fn (
  1021. instance->context,
  1022. msg_offset,
  1023. size_delv);
  1024. iovec->iov_len = FRAME_SIZE_MAX;
  1025. return (0);
  1026. }
  1027. static int netif_determine (
  1028. struct totemnet_instance *instance,
  1029. struct totem_ip_address *bindnet,
  1030. struct totem_ip_address *bound_to,
  1031. int *interface_up,
  1032. int *interface_num)
  1033. {
  1034. int res;
  1035. res = totemip_iface_check (bindnet, bound_to,
  1036. interface_up, interface_num,
  1037. instance->totem_config->clear_node_high_bit);
  1038. return (res);
  1039. }
  1040. /*
  1041. * If the interface is up, the sockets for totem are built. If the interface is down
  1042. * this function is requeued in the timer list to retry building the sockets later.
  1043. */
  1044. static void timer_function_netif_check_timeout (
  1045. void *data)
  1046. {
  1047. struct totemnet_instance *instance = (struct totemnet_instance *)data;
  1048. int res;
  1049. int interface_up;
  1050. int interface_num;
  1051. struct totem_ip_address *bind_address;
  1052. /*
  1053. * Build sockets for every interface
  1054. */
  1055. netif_determine (instance,
  1056. &instance->totem_interface->bindnet,
  1057. &instance->totem_interface->boundto,
  1058. &interface_up, &interface_num);
  1059. /*
  1060. * If the network interface isn't back up and we are already
  1061. * in loopback mode, add timer to check again and return
  1062. */
  1063. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  1064. interface_up == 0) ||
  1065. (instance->my_memb_entries == 1 &&
  1066. instance->netif_bind_state == BIND_STATE_REGULAR &&
  1067. interface_up == 1)) {
  1068. poll_timer_add (instance->totemnet_poll_handle,
  1069. instance->totem_config->downcheck_timeout,
  1070. (void *)instance,
  1071. timer_function_netif_check_timeout,
  1072. &instance->timer_netif_check_timeout);
  1073. /*
  1074. * Add a timer to check for a downed regular interface
  1075. */
  1076. return;
  1077. }
  1078. if (instance->totemnet_sockets.mcast_recv > 0) {
  1079. close (instance->totemnet_sockets.mcast_recv);
  1080. poll_dispatch_delete (instance->totemnet_poll_handle,
  1081. instance->totemnet_sockets.mcast_recv);
  1082. }
  1083. if (instance->totemnet_sockets.mcast_send > 0) {
  1084. close (instance->totemnet_sockets.mcast_send);
  1085. }
  1086. if (instance->totemnet_sockets.token > 0) {
  1087. close (instance->totemnet_sockets.token);
  1088. poll_dispatch_delete (instance->totemnet_poll_handle,
  1089. instance->totemnet_sockets.token);
  1090. }
  1091. if (interface_up == 0) {
  1092. /*
  1093. * Interface is not up
  1094. */
  1095. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  1096. bind_address = &localhost;
  1097. /*
  1098. * Add a timer to retry building interfaces and request memb_gather_enter
  1099. */
  1100. poll_timer_add (instance->totemnet_poll_handle,
  1101. instance->totem_config->downcheck_timeout,
  1102. (void *)instance,
  1103. timer_function_netif_check_timeout,
  1104. &instance->timer_netif_check_timeout);
  1105. } else {
  1106. /*
  1107. * Interface is up
  1108. */
  1109. instance->netif_bind_state = BIND_STATE_REGULAR;
  1110. bind_address = &instance->totem_interface->bindnet;
  1111. }
  1112. /*
  1113. * Create and bind the multicast and unicast sockets
  1114. */
  1115. res = totemnet_build_sockets (instance,
  1116. &instance->mcast_address,
  1117. bind_address,
  1118. &instance->totemnet_sockets,
  1119. &instance->totem_interface->boundto);
  1120. poll_dispatch_add (
  1121. instance->totemnet_poll_handle,
  1122. instance->totemnet_sockets.mcast_recv,
  1123. POLLIN, instance, net_deliver_fn);
  1124. poll_dispatch_add (
  1125. instance->totemnet_poll_handle,
  1126. instance->totemnet_sockets.token,
  1127. POLLIN, instance, net_deliver_fn);
  1128. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  1129. /*
  1130. * This reports changes in the interface to the user and totemsrp
  1131. */
  1132. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  1133. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  1134. log_printf (instance->totemnet_log_level_notice,
  1135. "The network interface [%s] is now up.\n",
  1136. totemip_print (&instance->totem_interface->boundto));
  1137. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  1138. instance->totemnet_iface_change_fn (instance->context, &instance->my_id);
  1139. }
  1140. /*
  1141. * Add a timer to check for interface going down in single membership
  1142. */
  1143. if (instance->my_memb_entries == 1) {
  1144. poll_timer_add (instance->totemnet_poll_handle,
  1145. instance->totem_config->downcheck_timeout,
  1146. (void *)instance,
  1147. timer_function_netif_check_timeout,
  1148. &instance->timer_netif_check_timeout);
  1149. }
  1150. } else {
  1151. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  1152. log_printf (instance->totemnet_log_level_notice,
  1153. "The network interface is down.\n");
  1154. instance->totemnet_iface_change_fn (instance->context, &instance->my_id);
  1155. }
  1156. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  1157. }
  1158. }
  1159. /*
  1160. * Check if an interface is down and reconfigure
  1161. * totemnet waiting for it to come back up
  1162. */
  1163. static void netif_down_check (struct totemnet_instance *instance)
  1164. {
  1165. timer_function_netif_check_timeout (instance);
  1166. }
  1167. /* Set the socket priority to INTERACTIVE to ensure
  1168. that our messages don't get queued behind anything else */
  1169. static void totemnet_traffic_control_set(struct totemnet_instance *instance, int sock)
  1170. {
  1171. #ifdef SO_PRIORITY
  1172. int prio = 6; /* TC_PRIO_INTERACTIVE */
  1173. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int)))
  1174. log_printf (instance->totemnet_log_level_warning, "Could not set traffic priority. (%s)\n", strerror (errno));
  1175. #endif
  1176. }
  1177. static int totemnet_build_sockets_ip (
  1178. struct totemnet_instance *instance,
  1179. struct totem_ip_address *mcast_address,
  1180. struct totem_ip_address *bindnet_address,
  1181. struct totemnet_socket *sockets,
  1182. struct totem_ip_address *bound_to,
  1183. int interface_num)
  1184. {
  1185. struct sockaddr_storage sockaddr;
  1186. struct ipv6_mreq mreq6;
  1187. struct ip_mreq mreq;
  1188. struct sockaddr_storage mcast_ss, boundto_ss;
  1189. struct sockaddr_in6 *mcast_sin6 = (struct sockaddr_in6 *)&mcast_ss;
  1190. struct sockaddr_in *mcast_sin = (struct sockaddr_in *)&mcast_ss;
  1191. struct sockaddr_in *boundto_sin = (struct sockaddr_in *)&boundto_ss;
  1192. unsigned int sendbuf_size;
  1193. unsigned int recvbuf_size;
  1194. unsigned int optlen = sizeof (sendbuf_size);
  1195. int addrlen;
  1196. int res;
  1197. int flag;
  1198. /*
  1199. * Create multicast recv socket
  1200. */
  1201. sockets->mcast_recv = socket (bindnet_address->family, SOCK_DGRAM, 0);
  1202. if (sockets->mcast_recv == -1) {
  1203. perror ("socket");
  1204. return (-1);
  1205. }
  1206. totemip_nosigpipe (sockets->mcast_recv);
  1207. res = fcntl (sockets->mcast_recv, F_SETFL, O_NONBLOCK);
  1208. if (res == -1) {
  1209. log_printf (instance->totemnet_log_level_warning, "Could not set non-blocking operation on multicast socket: %s\n", strerror (errno));
  1210. return (-1);
  1211. }
  1212. /*
  1213. * Force reuse
  1214. */
  1215. flag = 1;
  1216. if ( setsockopt(sockets->mcast_recv, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  1217. perror("setsockopt reuseaddr");
  1218. return (-1);
  1219. }
  1220. /*
  1221. * Bind to multicast socket used for multicast receives
  1222. */
  1223. totemip_totemip_to_sockaddr_convert(mcast_address,
  1224. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  1225. res = bind (sockets->mcast_recv, (struct sockaddr *)&sockaddr, addrlen);
  1226. if (res == -1) {
  1227. perror ("bind mcast recv socket failed");
  1228. return (-1);
  1229. }
  1230. /*
  1231. * Setup mcast send socket
  1232. */
  1233. sockets->mcast_send = socket (bindnet_address->family, SOCK_DGRAM, 0);
  1234. if (sockets->mcast_send == -1) {
  1235. perror ("socket");
  1236. return (-1);
  1237. }
  1238. totemip_nosigpipe (sockets->mcast_send);
  1239. res = fcntl (sockets->mcast_send, F_SETFL, O_NONBLOCK);
  1240. if (res == -1) {
  1241. log_printf (instance->totemnet_log_level_warning, "Could not set non-blocking operation on multicast socket: %s\n", strerror (errno));
  1242. return (-1);
  1243. }
  1244. /*
  1245. * Force reuse
  1246. */
  1247. flag = 1;
  1248. if ( setsockopt(sockets->mcast_send, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  1249. perror("setsockopt reuseaddr");
  1250. return (-1);
  1251. }
  1252. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port - 1,
  1253. &sockaddr, &addrlen);
  1254. res = bind (sockets->mcast_send, (struct sockaddr *)&sockaddr, addrlen);
  1255. if (res == -1) {
  1256. perror ("bind mcast send socket failed");
  1257. return (-1);
  1258. }
  1259. /*
  1260. * Setup unicast socket
  1261. */
  1262. sockets->token = socket (bindnet_address->family, SOCK_DGRAM, 0);
  1263. if (sockets->token == -1) {
  1264. perror ("socket2");
  1265. return (-1);
  1266. }
  1267. totemip_nosigpipe (sockets->token);
  1268. res = fcntl (sockets->token, F_SETFL, O_NONBLOCK);
  1269. if (res == -1) {
  1270. log_printf (instance->totemnet_log_level_warning, "Could not set non-blocking operation on token socket: %s\n", strerror (errno));
  1271. return (-1);
  1272. }
  1273. /*
  1274. * Force reuse
  1275. */
  1276. flag = 1;
  1277. if ( setsockopt(sockets->token, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  1278. perror("setsockopt reuseaddr");
  1279. return (-1);
  1280. }
  1281. /*
  1282. * Bind to unicast socket used for token send/receives
  1283. * This has the side effect of binding to the correct interface
  1284. */
  1285. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  1286. res = bind (sockets->token, (struct sockaddr *)&sockaddr, addrlen);
  1287. if (res == -1) {
  1288. perror ("bind token socket failed");
  1289. return (-1);
  1290. }
  1291. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  1292. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  1293. /*
  1294. * Set buffer sizes to avoid overruns
  1295. */
  1296. res = setsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  1297. res = setsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  1298. res = getsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  1299. if (res == 0) {
  1300. log_printf (instance->totemnet_log_level_notice,
  1301. "Receive multicast socket recv buffer size (%d bytes).\n", recvbuf_size);
  1302. }
  1303. res = getsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  1304. if (res == 0) {
  1305. log_printf (instance->totemnet_log_level_notice,
  1306. "Transmit multicast socket send buffer size (%d bytes).\n", sendbuf_size);
  1307. }
  1308. /*
  1309. * Join group membership on socket
  1310. */
  1311. totemip_totemip_to_sockaddr_convert(mcast_address, instance->totem_interface->ip_port, &mcast_ss, &addrlen);
  1312. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &boundto_ss, &addrlen);
  1313. if (instance->totem_config->broadcast_use == 1) {
  1314. unsigned int broadcast = 1;
  1315. if ((setsockopt(sockets->mcast_recv, SOL_SOCKET,
  1316. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  1317. perror("setting broadcast option");
  1318. exit(1);
  1319. }
  1320. if ((setsockopt(sockets->mcast_send, SOL_SOCKET,
  1321. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  1322. perror("setting broadcast option");
  1323. exit(1);
  1324. }
  1325. } else {
  1326. switch (bindnet_address->family) {
  1327. case AF_INET:
  1328. memset(&mreq, 0, sizeof(mreq));
  1329. mreq.imr_multiaddr.s_addr = mcast_sin->sin_addr.s_addr;
  1330. mreq.imr_interface.s_addr = boundto_sin->sin_addr.s_addr;
  1331. res = setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  1332. &mreq, sizeof (mreq));
  1333. if (res == -1) {
  1334. perror ("join ipv4 multicast group failed");
  1335. return (-1);
  1336. }
  1337. break;
  1338. case AF_INET6:
  1339. memset(&mreq6, 0, sizeof(mreq6));
  1340. memcpy(&mreq6.ipv6mr_multiaddr, &mcast_sin6->sin6_addr, sizeof(struct in6_addr));
  1341. mreq6.ipv6mr_interface = interface_num;
  1342. res = setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_JOIN_GROUP,
  1343. &mreq6, sizeof (mreq6));
  1344. if (res == -1) {
  1345. perror ("join ipv6 multicast group failed");
  1346. return (-1);
  1347. }
  1348. break;
  1349. }
  1350. }
  1351. /*
  1352. * Turn on multicast loopback
  1353. */
  1354. flag = 1;
  1355. switch ( bindnet_address->family ) {
  1356. case AF_INET:
  1357. res = setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_LOOP,
  1358. &flag, sizeof (flag));
  1359. break;
  1360. case AF_INET6:
  1361. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
  1362. &flag, sizeof (flag));
  1363. }
  1364. if (res == -1) {
  1365. perror ("turn off loopback");
  1366. return (-1);
  1367. }
  1368. /*
  1369. * Set multicast packets TTL
  1370. */
  1371. if ( bindnet_address->family == AF_INET6 )
  1372. {
  1373. flag = 255;
  1374. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
  1375. &flag, sizeof (flag));
  1376. if (res == -1) {
  1377. perror ("setp mcast hops");
  1378. return (-1);
  1379. }
  1380. }
  1381. /*
  1382. * Bind to a specific interface for multicast send and receive
  1383. */
  1384. switch ( bindnet_address->family ) {
  1385. case AF_INET:
  1386. if (setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_IF,
  1387. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  1388. perror ("cannot select interface");
  1389. return (-1);
  1390. }
  1391. if (setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_MULTICAST_IF,
  1392. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  1393. perror ("cannot select interface");
  1394. return (-1);
  1395. }
  1396. break;
  1397. case AF_INET6:
  1398. if (setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  1399. &interface_num, sizeof (interface_num)) < 0) {
  1400. perror ("cannot select interface");
  1401. return (-1);
  1402. }
  1403. if (setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  1404. &interface_num, sizeof (interface_num)) < 0) {
  1405. perror ("cannot select interface");
  1406. return (-1);
  1407. }
  1408. break;
  1409. }
  1410. return 0;
  1411. }
  1412. static int totemnet_build_sockets (
  1413. struct totemnet_instance *instance,
  1414. struct totem_ip_address *mcast_address,
  1415. struct totem_ip_address *bindnet_address,
  1416. struct totemnet_socket *sockets,
  1417. struct totem_ip_address *bound_to)
  1418. {
  1419. int interface_num;
  1420. int interface_up;
  1421. int res;
  1422. /*
  1423. * Determine the ip address bound to and the interface name
  1424. */
  1425. res = netif_determine (instance,
  1426. bindnet_address,
  1427. bound_to,
  1428. &interface_up,
  1429. &interface_num);
  1430. if (res == -1) {
  1431. return (-1);
  1432. }
  1433. totemip_copy(&instance->my_id, bound_to);
  1434. res = totemnet_build_sockets_ip (instance, mcast_address,
  1435. bindnet_address, sockets, bound_to, interface_num);
  1436. /* We only send out of the token socket */
  1437. totemnet_traffic_control_set(instance, sockets->token);
  1438. return res;
  1439. }
  1440. /*
  1441. * Totem Network interface - also does encryption/decryption
  1442. * depends on poll abstraction, POSIX, IPV4
  1443. */
  1444. /*
  1445. * Create an instance
  1446. */
  1447. int totemnet_initialize (
  1448. hdb_handle_t poll_handle,
  1449. hdb_handle_t *handle,
  1450. struct totem_config *totem_config,
  1451. int interface_no,
  1452. void *context,
  1453. void (*deliver_fn) (
  1454. void *context,
  1455. const void *msg,
  1456. unsigned int msg_len),
  1457. void (*iface_change_fn) (
  1458. void *context,
  1459. const struct totem_ip_address *iface_address))
  1460. {
  1461. struct totemnet_instance *instance;
  1462. unsigned int res;
  1463. res = hdb_handle_create (&totemnet_instance_database,
  1464. sizeof (struct totemnet_instance), handle);
  1465. if (res != 0) {
  1466. goto error_exit;
  1467. }
  1468. res = hdb_handle_get (&totemnet_instance_database, *handle,
  1469. (void *)&instance);
  1470. if (res != 0) {
  1471. goto error_destroy;
  1472. }
  1473. totemnet_instance_initialize (instance);
  1474. instance->totem_config = totem_config;
  1475. /*
  1476. * Configure logging
  1477. */
  1478. instance->totemnet_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  1479. instance->totemnet_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  1480. instance->totemnet_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  1481. instance->totemnet_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  1482. instance->totemnet_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  1483. instance->totemnet_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1484. instance->totemnet_log_printf = totem_config->totem_logging_configuration.log_printf;
  1485. /*
  1486. * Initialize random number generator for later use to generate salt
  1487. */
  1488. memcpy (instance->totemnet_private_key, totem_config->private_key,
  1489. totem_config->private_key_len);
  1490. instance->totemnet_private_key_len = totem_config->private_key_len;
  1491. init_crypto(instance);
  1492. /*
  1493. * Initialize local variables for totemnet
  1494. */
  1495. instance->totem_interface = &totem_config->interfaces[interface_no];
  1496. totemip_copy (&instance->mcast_address, &instance->totem_interface->mcast_addr);
  1497. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  1498. /*
  1499. * If threaded send requested, initialize thread group data structure
  1500. */
  1501. if (totem_config->threads) {
  1502. worker_thread_group_init (
  1503. &instance->worker_thread_group,
  1504. totem_config->threads, 128,
  1505. sizeof (struct work_item),
  1506. sizeof (struct totemnet_mcast_thread_state),
  1507. totemnet_mcast_thread_state_constructor,
  1508. totemnet_mcast_worker_fn);
  1509. }
  1510. instance->totemnet_poll_handle = poll_handle;
  1511. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  1512. instance->context = context;
  1513. instance->totemnet_deliver_fn = deliver_fn;
  1514. instance->totemnet_iface_change_fn = iface_change_fn;
  1515. instance->handle = *handle;
  1516. totemip_localhost (instance->mcast_address.family, &localhost);
  1517. netif_down_check (instance);
  1518. error_exit:
  1519. hdb_handle_put (&totemnet_instance_database, *handle);
  1520. return (0);
  1521. error_destroy:
  1522. hdb_handle_destroy (&totemnet_instance_database, *handle);
  1523. return (-1);
  1524. }
  1525. int totemnet_processor_count_set (
  1526. hdb_handle_t handle,
  1527. int processor_count)
  1528. {
  1529. struct totemnet_instance *instance;
  1530. int res = 0;
  1531. res = hdb_handle_get (&totemnet_instance_database, handle,
  1532. (void *)&instance);
  1533. if (res != 0) {
  1534. res = ENOENT;
  1535. goto error_exit;
  1536. }
  1537. instance->my_memb_entries = processor_count;
  1538. poll_timer_delete (instance->totemnet_poll_handle,
  1539. instance->timer_netif_check_timeout);
  1540. if (processor_count == 1) {
  1541. poll_timer_add (instance->totemnet_poll_handle,
  1542. instance->totem_config->downcheck_timeout,
  1543. (void *)instance,
  1544. timer_function_netif_check_timeout,
  1545. &instance->timer_netif_check_timeout);
  1546. }
  1547. hdb_handle_put (&totemnet_instance_database, handle);
  1548. error_exit:
  1549. return (res);
  1550. }
  1551. int totemnet_recv_flush (hdb_handle_t handle)
  1552. {
  1553. struct totemnet_instance *instance;
  1554. struct pollfd ufd;
  1555. int nfds;
  1556. int res = 0;
  1557. res = hdb_handle_get (&totemnet_instance_database, handle,
  1558. (void *)&instance);
  1559. if (res != 0) {
  1560. res = ENOENT;
  1561. goto error_exit;
  1562. }
  1563. instance->flushing = 1;
  1564. do {
  1565. ufd.fd = instance->totemnet_sockets.mcast_recv;
  1566. ufd.events = POLLIN;
  1567. nfds = poll (&ufd, 1, 0);
  1568. if (nfds == 1 && ufd.revents & POLLIN) {
  1569. net_deliver_fn (0, instance->totemnet_sockets.mcast_recv,
  1570. ufd.revents, instance);
  1571. }
  1572. } while (nfds == 1);
  1573. instance->flushing = 0;
  1574. hdb_handle_put (&totemnet_instance_database, handle);
  1575. error_exit:
  1576. return (res);
  1577. }
  1578. int totemnet_send_flush (hdb_handle_t handle)
  1579. {
  1580. struct totemnet_instance *instance;
  1581. int res = 0;
  1582. res = hdb_handle_get (&totemnet_instance_database, handle,
  1583. (void *)&instance);
  1584. if (res != 0) {
  1585. res = ENOENT;
  1586. goto error_exit;
  1587. }
  1588. worker_thread_group_wait (&instance->worker_thread_group);
  1589. hdb_handle_put (&totemnet_instance_database, handle);
  1590. error_exit:
  1591. return (res);
  1592. }
  1593. int totemnet_token_send (
  1594. hdb_handle_t handle,
  1595. const void *msg,
  1596. unsigned int msg_len)
  1597. {
  1598. struct totemnet_instance *instance;
  1599. int res = 0;
  1600. res = hdb_handle_get (&totemnet_instance_database, handle,
  1601. (void *)&instance);
  1602. if (res != 0) {
  1603. res = ENOENT;
  1604. goto error_exit;
  1605. }
  1606. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  1607. hdb_handle_put (&totemnet_instance_database, handle);
  1608. error_exit:
  1609. return (res);
  1610. }
  1611. int totemnet_mcast_flush_send (
  1612. hdb_handle_t handle,
  1613. const void *msg,
  1614. unsigned int msg_len)
  1615. {
  1616. struct totemnet_instance *instance;
  1617. int res = 0;
  1618. res = hdb_handle_get (&totemnet_instance_database, handle,
  1619. (void *)&instance);
  1620. if (res != 0) {
  1621. res = ENOENT;
  1622. goto error_exit;
  1623. }
  1624. mcast_sendmsg (instance, msg, msg_len);
  1625. hdb_handle_put (&totemnet_instance_database, handle);
  1626. error_exit:
  1627. return (res);
  1628. }
  1629. int totemnet_mcast_noflush_send (
  1630. hdb_handle_t handle,
  1631. const void *msg,
  1632. unsigned int msg_len)
  1633. {
  1634. struct totemnet_instance *instance;
  1635. struct work_item work_item;
  1636. int res = 0;
  1637. res = hdb_handle_get (&totemnet_instance_database, handle,
  1638. (void *)&instance);
  1639. if (res != 0) {
  1640. res = ENOENT;
  1641. goto error_exit;
  1642. }
  1643. if (instance->totem_config->threads) {
  1644. work_item.msg = msg;
  1645. work_item.msg_len = msg_len;
  1646. work_item.instance = instance;
  1647. worker_thread_group_work_add (&instance->worker_thread_group,
  1648. &work_item);
  1649. } else {
  1650. mcast_sendmsg (instance, msg, msg_len);
  1651. }
  1652. hdb_handle_put (&totemnet_instance_database, handle);
  1653. error_exit:
  1654. return (res);
  1655. }
  1656. extern int totemnet_iface_check (hdb_handle_t handle)
  1657. {
  1658. struct totemnet_instance *instance;
  1659. int res = 0;
  1660. res = hdb_handle_get (&totemnet_instance_database, handle,
  1661. (void *)&instance);
  1662. if (res != 0) {
  1663. res = ENOENT;
  1664. goto error_exit;
  1665. }
  1666. timer_function_netif_check_timeout (instance);
  1667. hdb_handle_put (&totemnet_instance_database, handle);
  1668. error_exit:
  1669. return (res);
  1670. }
  1671. extern void totemnet_net_mtu_adjust (struct totem_config *totem_config)
  1672. {
  1673. #define UDPIP_HEADER_SIZE (20 + 8) /* 20 bytes for ip 8 bytes for udp */
  1674. if (totem_config->secauth == 1) {
  1675. totem_config->net_mtu -= sizeof (struct security_header) +
  1676. UDPIP_HEADER_SIZE;
  1677. } else {
  1678. totem_config->net_mtu -= UDPIP_HEADER_SIZE;
  1679. }
  1680. }
  1681. const char *totemnet_iface_print (hdb_handle_t handle) {
  1682. struct totemnet_instance *instance;
  1683. int res = 0;
  1684. const char *ret_char;
  1685. res = hdb_handle_get (&totemnet_instance_database, handle,
  1686. (void *)&instance);
  1687. if (res != 0) {
  1688. ret_char = "Invalid totemnet handle";
  1689. goto error_exit;
  1690. }
  1691. ret_char = totemip_print (&instance->my_id);
  1692. hdb_handle_put (&totemnet_instance_database, handle);
  1693. error_exit:
  1694. return (ret_char);
  1695. }
  1696. int totemnet_iface_get (
  1697. hdb_handle_t handle,
  1698. struct totem_ip_address *addr)
  1699. {
  1700. struct totemnet_instance *instance;
  1701. unsigned int res;
  1702. res = hdb_handle_get (&totemnet_instance_database, handle,
  1703. (void *)&instance);
  1704. if (res != 0) {
  1705. goto error_exit;
  1706. }
  1707. memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
  1708. hdb_handle_put (&totemnet_instance_database, handle);
  1709. error_exit:
  1710. return (res);
  1711. }
  1712. int totemnet_token_target_set (
  1713. hdb_handle_t handle,
  1714. const struct totem_ip_address *token_target)
  1715. {
  1716. struct totemnet_instance *instance;
  1717. unsigned int res;
  1718. res = hdb_handle_get (&totemnet_instance_database, handle,
  1719. (void *)&instance);
  1720. if (res != 0) {
  1721. goto error_exit;
  1722. }
  1723. memcpy (&instance->token_target, token_target,
  1724. sizeof (struct totem_ip_address));
  1725. hdb_handle_put (&totemnet_instance_database, handle);
  1726. error_exit:
  1727. return (res);
  1728. }