totemnet.c 50 KB

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