totemnet.c 52 KB

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