totemnet.c 51 KB

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