totemnet.c 54 KB

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