totemnet.c 52 KB

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