totemudpu.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779
  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 <sys/mman.h>
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include <sys/socket.h>
  40. #include <netdb.h>
  41. #include <sys/un.h>
  42. #include <sys/ioctl.h>
  43. #include <sys/param.h>
  44. #include <netinet/in.h>
  45. #include <arpa/inet.h>
  46. #include <unistd.h>
  47. #include <fcntl.h>
  48. #include <stdlib.h>
  49. #include <stdio.h>
  50. #include <errno.h>
  51. #include <sched.h>
  52. #include <time.h>
  53. #include <sys/time.h>
  54. #include <sys/poll.h>
  55. #include <limits.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. #define LOGSYS_UTILS_ONLY 1
  62. #include <corosync/engine/logsys.h>
  63. #include "totemudpu.h"
  64. #include "crypto.h"
  65. #include "util.h"
  66. #ifdef HAVE_LIBNSS
  67. #include <nss.h>
  68. #include <pk11pub.h>
  69. #include <pkcs11.h>
  70. #include <prerror.h>
  71. #endif
  72. #ifndef MSG_NOSIGNAL
  73. #define MSG_NOSIGNAL 0
  74. #endif
  75. #define MCAST_SOCKET_BUFFER_SIZE (TRANSMITS_ALLOWED * FRAME_SIZE_MAX)
  76. #define NETIF_STATE_REPORT_UP 1
  77. #define NETIF_STATE_REPORT_DOWN 2
  78. #define BIND_STATE_UNBOUND 0
  79. #define BIND_STATE_REGULAR 1
  80. #define BIND_STATE_LOOPBACK 2
  81. #define HMAC_HASH_SIZE 20
  82. struct security_header {
  83. unsigned char hash_digest[HMAC_HASH_SIZE]; /* The hash *MUST* be first in the data structure */
  84. unsigned char salt[16]; /* random number */
  85. char msg[0];
  86. } __attribute__((packed));
  87. struct totemudpu_member {
  88. struct list_head list;
  89. struct totem_ip_address member;
  90. int fd;
  91. };
  92. struct totemudpu_instance {
  93. hmac_state totemudpu_hmac_state;
  94. prng_state totemudpu_prng_state;
  95. #ifdef HAVE_LIBNSS
  96. PK11SymKey *nss_sym_key;
  97. PK11SymKey *nss_sym_key_sign;
  98. #endif
  99. unsigned char totemudpu_private_key[1024];
  100. unsigned int totemudpu_private_key_len;
  101. hdb_handle_t totemudpu_poll_handle;
  102. struct totem_interface *totem_interface;
  103. int netif_state_report;
  104. int netif_bind_state;
  105. void *context;
  106. void (*totemudpu_deliver_fn) (
  107. void *context,
  108. const void *msg,
  109. unsigned int msg_len);
  110. void (*totemudpu_iface_change_fn) (
  111. void *context,
  112. const struct totem_ip_address *iface_address);
  113. void (*totemudpu_target_set_completed) (void *context);
  114. /*
  115. * Function and data used to log messages
  116. */
  117. int totemudpu_log_level_security;
  118. int totemudpu_log_level_error;
  119. int totemudpu_log_level_warning;
  120. int totemudpu_log_level_notice;
  121. int totemudpu_log_level_debug;
  122. int totemudpu_subsys_id;
  123. void (*totemudpu_log_printf) (
  124. unsigned int rec_ident,
  125. const char *function,
  126. const char *file,
  127. int line,
  128. const char *format,
  129. ...)__attribute__((format(printf, 5, 6)));
  130. void *udpu_context;
  131. char iov_buffer[FRAME_SIZE_MAX];
  132. struct iovec totemudpu_iov_recv;
  133. struct list_head member_list;
  134. int stats_sent;
  135. int stats_recv;
  136. int stats_delv;
  137. int stats_remcasts;
  138. int stats_orf_token;
  139. struct timeval stats_tv_start;
  140. struct totem_ip_address my_id;
  141. int firstrun;
  142. poll_timer_handle timer_netif_check_timeout;
  143. unsigned int my_memb_entries;
  144. struct totem_config *totem_config;
  145. struct totem_ip_address token_target;
  146. int token_socket;
  147. };
  148. struct work_item {
  149. const void *msg;
  150. unsigned int msg_len;
  151. struct totemudpu_instance *instance;
  152. };
  153. static int totemudpu_build_sockets (
  154. struct totemudpu_instance *instance,
  155. struct totem_ip_address *bindnet_address,
  156. struct totem_ip_address *bound_to);
  157. static int totemudpu_create_sending_socket(
  158. void *udpu_context,
  159. const struct totem_ip_address *member);
  160. int totemudpu_member_list_rebind_ip (
  161. void *udpu_context);
  162. static struct totem_ip_address localhost;
  163. static void totemudpu_instance_initialize (struct totemudpu_instance *instance)
  164. {
  165. memset (instance, 0, sizeof (struct totemudpu_instance));
  166. instance->netif_state_report = NETIF_STATE_REPORT_UP | NETIF_STATE_REPORT_DOWN;
  167. instance->totemudpu_iov_recv.iov_base = instance->iov_buffer;
  168. instance->totemudpu_iov_recv.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  169. /*
  170. * There is always atleast 1 processor
  171. */
  172. instance->my_memb_entries = 1;
  173. list_init (&instance->member_list);
  174. }
  175. #define log_printf(level, format, args...) \
  176. do { \
  177. instance->totemudpu_log_printf ( \
  178. LOGSYS_ENCODE_RECID(level, \
  179. instance->totemudpu_subsys_id, \
  180. LOGSYS_RECID_LOG), \
  181. __FUNCTION__, __FILE__, __LINE__, \
  182. (const char *)format, ##args); \
  183. } while (0);
  184. static int authenticate_and_decrypt_sober (
  185. struct totemudpu_instance *instance,
  186. struct iovec *iov,
  187. unsigned int iov_len)
  188. {
  189. unsigned char keys[48];
  190. struct security_header *header = (struct security_header *)iov[0].iov_base;
  191. prng_state keygen_prng_state;
  192. prng_state stream_prng_state;
  193. unsigned char *hmac_key = &keys[32];
  194. unsigned char *cipher_key = &keys[16];
  195. unsigned char *initial_vector = &keys[0];
  196. unsigned char digest_comparison[HMAC_HASH_SIZE];
  197. unsigned long len;
  198. /*
  199. * Generate MAC, CIPHER, IV keys from private key
  200. */
  201. memset (keys, 0, sizeof (keys));
  202. sober128_start (&keygen_prng_state);
  203. sober128_add_entropy (instance->totemudpu_private_key,
  204. instance->totemudpu_private_key_len, &keygen_prng_state);
  205. sober128_add_entropy (header->salt, sizeof (header->salt), &keygen_prng_state);
  206. sober128_read (keys, sizeof (keys), &keygen_prng_state);
  207. /*
  208. * Setup stream cipher
  209. */
  210. sober128_start (&stream_prng_state);
  211. sober128_add_entropy (cipher_key, 16, &stream_prng_state);
  212. sober128_add_entropy (initial_vector, 16, &stream_prng_state);
  213. /*
  214. * Authenticate contents of message
  215. */
  216. hmac_init (&instance->totemudpu_hmac_state, DIGEST_SHA1, hmac_key, 16);
  217. hmac_process (&instance->totemudpu_hmac_state,
  218. (unsigned char *)iov->iov_base + HMAC_HASH_SIZE,
  219. iov->iov_len - HMAC_HASH_SIZE);
  220. len = hash_descriptor[DIGEST_SHA1]->hashsize;
  221. assert (HMAC_HASH_SIZE >= len);
  222. hmac_done (&instance->totemudpu_hmac_state, digest_comparison, &len);
  223. if (memcmp (digest_comparison, header->hash_digest, len) != 0) {
  224. return (-1);
  225. }
  226. /*
  227. * Decrypt the contents of the message with the cipher key
  228. */
  229. sober128_read ((unsigned char*)iov->iov_base +
  230. sizeof (struct security_header),
  231. iov->iov_len - sizeof (struct security_header),
  232. &stream_prng_state);
  233. return (0);
  234. }
  235. static void init_sober_crypto(
  236. struct totemudpu_instance *instance)
  237. {
  238. log_printf(instance->totemudpu_log_level_notice,
  239. "Initializing transmit/receive security: libtomcrypt SOBER128/SHA1HMAC (mode 0).\n");
  240. rng_make_prng (128, PRNG_SOBER, &instance->totemudpu_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 totemudpu_instance *instance)
  290. {
  291. PK11SlotInfo* aes_slot = NULL;
  292. PK11SlotInfo* sha1_slot = NULL;
  293. SECItem key_item;
  294. SECStatus rv;
  295. log_printf(instance->totemudpu_log_level_notice,
  296. "Initializing transmit/receive security: NSS AES128CBC/SHA1HMAC (mode 1).\n");
  297. rv = NSS_NoDB_Init(".");
  298. if (rv != SECSuccess)
  299. {
  300. log_printf(instance->totemudpu_log_level_security, "NSS initialization failed (err %d)\n",
  301. PR_GetError());
  302. goto out;
  303. }
  304. aes_slot = PK11_GetBestSlot(instance->totem_config->crypto_crypt_type, NULL);
  305. if (aes_slot == NULL)
  306. {
  307. log_printf(instance->totemudpu_log_level_security, "Unable to find security slot (err %d)\n",
  308. PR_GetError());
  309. goto out;
  310. }
  311. sha1_slot = PK11_GetBestSlot(CKM_SHA_1_HMAC, NULL);
  312. if (sha1_slot == NULL)
  313. {
  314. log_printf(instance->totemudpu_log_level_security, "Unable to find security slot (err %d)\n",
  315. PR_GetError());
  316. goto out;
  317. }
  318. /*
  319. * Make the private key into a SymKey that we can use
  320. */
  321. key_item.type = siBuffer;
  322. key_item.data = instance->totem_config->private_key;
  323. key_item.len = 32; /* Use 128 bits */
  324. instance->nss_sym_key = PK11_ImportSymKey(aes_slot,
  325. instance->totem_config->crypto_crypt_type,
  326. PK11_OriginUnwrap, CKA_ENCRYPT|CKA_DECRYPT,
  327. &key_item, NULL);
  328. if (instance->nss_sym_key == NULL)
  329. {
  330. log_printf(instance->totemudpu_log_level_security, "Failure to import key into NSS (err %d)\n",
  331. PR_GetError());
  332. goto out;
  333. }
  334. instance->nss_sym_key_sign = PK11_ImportSymKey(sha1_slot,
  335. CKM_SHA_1_HMAC,
  336. PK11_OriginUnwrap, CKA_SIGN,
  337. &key_item, NULL);
  338. if (instance->nss_sym_key_sign == NULL) {
  339. log_printf(instance->totemudpu_log_level_security, "Failure to import key into NSS (err %d)\n",
  340. PR_GetError());
  341. goto out;
  342. }
  343. out:
  344. return;
  345. }
  346. static int encrypt_and_sign_nss (
  347. struct totemudpu_instance *instance,
  348. unsigned char *buf,
  349. size_t *buf_len,
  350. const struct iovec *iovec,
  351. unsigned int iov_len)
  352. {
  353. PK11Context* enc_context = NULL;
  354. SECStatus rv1, rv2;
  355. int tmp1_outlen;
  356. unsigned int tmp2_outlen;
  357. unsigned char *inbuf;
  358. unsigned char *data;
  359. unsigned char *outdata;
  360. size_t datalen;
  361. SECItem no_params;
  362. SECItem iv_item;
  363. struct security_header *header;
  364. SECItem *nss_sec_param;
  365. unsigned char nss_iv_data[16];
  366. SECStatus rv;
  367. no_params.type = siBuffer;
  368. no_params.data = 0;
  369. no_params.len = 0;
  370. tmp1_outlen = tmp2_outlen = 0;
  371. inbuf = copy_from_iovec(iovec, iov_len, &datalen);
  372. if (!inbuf) {
  373. log_printf(instance->totemudpu_log_level_security, "malloc error copying buffer from iovec\n");
  374. goto out;
  375. }
  376. data = inbuf + sizeof (struct security_header);
  377. datalen -= sizeof (struct security_header);
  378. outdata = buf + sizeof (struct security_header);
  379. header = (struct security_header *)buf;
  380. rv = PK11_GenerateRandom (
  381. nss_iv_data,
  382. sizeof (nss_iv_data));
  383. if (rv != SECSuccess) {
  384. log_printf(instance->totemudpu_log_level_security,
  385. "Failure to generate a random number %d\n",
  386. PR_GetError());
  387. goto out;
  388. }
  389. memcpy(header->salt, nss_iv_data, sizeof(nss_iv_data));
  390. iv_item.type = siBuffer;
  391. iv_item.data = nss_iv_data;
  392. iv_item.len = sizeof (nss_iv_data);
  393. nss_sec_param = PK11_ParamFromIV (
  394. instance->totem_config->crypto_crypt_type,
  395. &iv_item);
  396. if (nss_sec_param == NULL) {
  397. log_printf(instance->totemudpu_log_level_security,
  398. "Failure to set up PKCS11 param (err %d)\n",
  399. PR_GetError());
  400. free (inbuf);
  401. goto out;
  402. }
  403. /*
  404. * Create cipher context for encryption
  405. */
  406. enc_context = PK11_CreateContextBySymKey (
  407. instance->totem_config->crypto_crypt_type,
  408. CKA_ENCRYPT,
  409. instance->nss_sym_key,
  410. nss_sec_param);
  411. if (!enc_context) {
  412. char err[1024];
  413. PR_GetErrorText(err);
  414. err[PR_GetErrorTextLength()] = 0;
  415. log_printf(instance->totemudpu_log_level_security,
  416. "PK11_CreateContext failed (encrypt) crypt_type=%d (err %d): %s\n",
  417. instance->totem_config->crypto_crypt_type,
  418. PR_GetError(), err);
  419. free(inbuf);
  420. goto sec_out;
  421. }
  422. rv1 = PK11_CipherOp(enc_context, outdata,
  423. &tmp1_outlen, FRAME_SIZE_MAX - sizeof(struct security_header),
  424. data, datalen);
  425. rv2 = PK11_DigestFinal(enc_context, outdata + tmp1_outlen, &tmp2_outlen,
  426. FRAME_SIZE_MAX - tmp1_outlen);
  427. PK11_DestroyContext(enc_context, PR_TRUE);
  428. *buf_len = tmp1_outlen + tmp2_outlen;
  429. free(inbuf);
  430. // memcpy(&outdata[*buf_len], nss_iv_data, sizeof(nss_iv_data));
  431. if (rv1 != SECSuccess || rv2 != SECSuccess)
  432. goto sec_out;
  433. /* Now do the digest */
  434. enc_context = PK11_CreateContextBySymKey(CKM_SHA_1_HMAC,
  435. CKA_SIGN, instance->nss_sym_key_sign, &no_params);
  436. if (!enc_context) {
  437. char err[1024];
  438. PR_GetErrorText(err);
  439. err[PR_GetErrorTextLength()] = 0;
  440. log_printf(instance->totemudpu_log_level_security, "encrypt: PK11_CreateContext failed (digest) err %d: %s\n",
  441. PR_GetError(), err);
  442. goto sec_out;
  443. }
  444. PK11_DigestBegin(enc_context);
  445. rv1 = PK11_DigestOp(enc_context, outdata - 16, *buf_len + 16);
  446. rv2 = PK11_DigestFinal(enc_context, header->hash_digest, &tmp2_outlen, sizeof(header->hash_digest));
  447. PK11_DestroyContext(enc_context, PR_TRUE);
  448. if (rv1 != SECSuccess || rv2 != SECSuccess)
  449. goto sec_out;
  450. *buf_len = *buf_len + sizeof(struct security_header);
  451. SECITEM_FreeItem(nss_sec_param, PR_TRUE);
  452. return 0;
  453. sec_out:
  454. if (nss_sec_param != NULL) {
  455. SECITEM_FreeItem(nss_sec_param, PR_TRUE);
  456. }
  457. out:
  458. return -1;
  459. }
  460. static int authenticate_and_decrypt_nss (
  461. struct totemudpu_instance *instance,
  462. struct iovec *iov,
  463. unsigned int iov_len)
  464. {
  465. PK11Context* enc_context = NULL;
  466. SECStatus rv1, rv2;
  467. int tmp1_outlen;
  468. unsigned int tmp2_outlen;
  469. unsigned char outbuf[FRAME_SIZE_MAX];
  470. unsigned char digest[HMAC_HASH_SIZE];
  471. unsigned char *outdata;
  472. int result_len;
  473. unsigned char *data;
  474. unsigned char *inbuf;
  475. size_t datalen;
  476. struct security_header *header = (struct security_header *)iov[0].iov_base;
  477. SECItem no_params;
  478. SECItem ivdata;
  479. no_params.type = siBuffer;
  480. no_params.data = 0;
  481. no_params.len = 0;
  482. tmp1_outlen = tmp2_outlen = 0;
  483. if (iov_len > 1) {
  484. inbuf = copy_from_iovec(iov, iov_len, &datalen);
  485. if (!inbuf) {
  486. log_printf(instance->totemudpu_log_level_security, "malloc error copying buffer from iovec\n");
  487. return -1;
  488. }
  489. }
  490. else {
  491. inbuf = (unsigned char *)iov[0].iov_base;
  492. datalen = iov[0].iov_len;
  493. }
  494. data = inbuf + sizeof (struct security_header) - 16;
  495. datalen = datalen - sizeof (struct security_header) + 16;
  496. outdata = outbuf + sizeof (struct security_header);
  497. /* Check the digest */
  498. enc_context = PK11_CreateContextBySymKey (
  499. CKM_SHA_1_HMAC, CKA_SIGN,
  500. instance->nss_sym_key_sign,
  501. &no_params);
  502. if (!enc_context) {
  503. char err[1024];
  504. PR_GetErrorText(err);
  505. err[PR_GetErrorTextLength()] = 0;
  506. log_printf(instance->totemudpu_log_level_security, "PK11_CreateContext failed (check digest) err %d: %s\n",
  507. PR_GetError(), err);
  508. goto out;
  509. }
  510. PK11_DigestBegin(enc_context);
  511. rv1 = PK11_DigestOp(enc_context, data, datalen);
  512. rv2 = PK11_DigestFinal(enc_context, digest, &tmp2_outlen, sizeof(digest));
  513. PK11_DestroyContext(enc_context, PR_TRUE);
  514. if (rv1 != SECSuccess || rv2 != SECSuccess) {
  515. log_printf(instance->totemudpu_log_level_security, "Digest check failed\n");
  516. goto out;
  517. }
  518. if (memcmp(digest, header->hash_digest, tmp2_outlen) != 0) {
  519. log_printf(instance->totemudpu_log_level_error, "Digest does not match\n");
  520. goto out;
  521. }
  522. /*
  523. * Get rid of salt
  524. */
  525. data += 16;
  526. datalen -= 16;
  527. /* Create cipher context for decryption */
  528. ivdata.type = siBuffer;
  529. ivdata.data = header->salt;
  530. ivdata.len = sizeof(header->salt);
  531. enc_context = PK11_CreateContextBySymKey(
  532. instance->totem_config->crypto_crypt_type,
  533. CKA_DECRYPT,
  534. instance->nss_sym_key, &ivdata);
  535. if (!enc_context) {
  536. log_printf(instance->totemudpu_log_level_security,
  537. "PK11_CreateContext (decrypt) failed (err %d)\n",
  538. PR_GetError());
  539. goto out;
  540. }
  541. rv1 = PK11_CipherOp(enc_context, outdata, &tmp1_outlen,
  542. sizeof(outbuf) - sizeof (struct security_header),
  543. data, datalen);
  544. if (rv1 != SECSuccess) {
  545. log_printf(instance->totemudpu_log_level_security,
  546. "PK11_CipherOp (decrypt) failed (err %d)\n",
  547. PR_GetError());
  548. }
  549. rv2 = PK11_DigestFinal(enc_context, outdata + tmp1_outlen, &tmp2_outlen,
  550. sizeof(outbuf) - tmp1_outlen);
  551. PK11_DestroyContext(enc_context, PR_TRUE);
  552. result_len = tmp1_outlen + tmp2_outlen + sizeof (struct security_header);
  553. /* Copy it back to the buffer */
  554. copy_to_iovec(iov, iov_len, outbuf, result_len);
  555. if (iov_len > 1)
  556. free(inbuf);
  557. if (rv1 != SECSuccess || rv2 != SECSuccess)
  558. return -1;
  559. return 0;
  560. out:
  561. if (iov_len > 1 && inbuf != NULL) {
  562. free (inbuf);
  563. }
  564. return (-1);
  565. }
  566. #endif
  567. static int encrypt_and_sign_sober (
  568. struct totemudpu_instance *instance,
  569. unsigned char *buf,
  570. size_t *buf_len,
  571. const struct iovec *iovec,
  572. unsigned int iov_len)
  573. {
  574. int i;
  575. unsigned char *addr;
  576. unsigned char keys[48];
  577. struct security_header *header;
  578. unsigned char *hmac_key = &keys[32];
  579. unsigned char *cipher_key = &keys[16];
  580. unsigned char *initial_vector = &keys[0];
  581. unsigned long len;
  582. size_t outlen = 0;
  583. hmac_state hmac_st;
  584. prng_state keygen_prng_state;
  585. prng_state stream_prng_state;
  586. prng_state *prng_state_in = &instance->totemudpu_prng_state;
  587. header = (struct security_header *)buf;
  588. addr = buf + sizeof (struct security_header);
  589. memset (keys, 0, sizeof (keys));
  590. memset (header->salt, 0, sizeof (header->salt));
  591. /*
  592. * Generate MAC, CIPHER, IV keys from private key
  593. */
  594. sober128_read (header->salt, sizeof (header->salt), prng_state_in);
  595. sober128_start (&keygen_prng_state);
  596. sober128_add_entropy (instance->totemudpu_private_key,
  597. instance->totemudpu_private_key_len,
  598. &keygen_prng_state);
  599. sober128_add_entropy (header->salt, sizeof (header->salt),
  600. &keygen_prng_state);
  601. sober128_read (keys, sizeof (keys), &keygen_prng_state);
  602. /*
  603. * Setup stream cipher
  604. */
  605. sober128_start (&stream_prng_state);
  606. sober128_add_entropy (cipher_key, 16, &stream_prng_state);
  607. sober128_add_entropy (initial_vector, 16, &stream_prng_state);
  608. outlen = sizeof (struct security_header);
  609. /*
  610. * Copy remainder of message, then encrypt it
  611. */
  612. for (i = 1; i < iov_len; i++) {
  613. memcpy (addr, iovec[i].iov_base, iovec[i].iov_len);
  614. addr += iovec[i].iov_len;
  615. outlen += iovec[i].iov_len;
  616. }
  617. /*
  618. * Encrypt message by XORing stream cipher data
  619. */
  620. sober128_read (buf + sizeof (struct security_header),
  621. outlen - sizeof (struct security_header),
  622. &stream_prng_state);
  623. memset (&hmac_st, 0, sizeof (hmac_st));
  624. /*
  625. * Sign the contents of the message with the hmac key and store signature in message
  626. */
  627. hmac_init (&hmac_st, DIGEST_SHA1, hmac_key, 16);
  628. hmac_process (&hmac_st,
  629. buf + HMAC_HASH_SIZE,
  630. outlen - HMAC_HASH_SIZE);
  631. len = hash_descriptor[DIGEST_SHA1]->hashsize;
  632. hmac_done (&hmac_st, header->hash_digest, &len);
  633. *buf_len = outlen;
  634. return 0;
  635. }
  636. static int encrypt_and_sign_worker (
  637. struct totemudpu_instance *instance,
  638. unsigned char *buf,
  639. size_t *buf_len,
  640. const struct iovec *iovec,
  641. unsigned int iov_len)
  642. {
  643. if (instance->totem_config->crypto_type == TOTEM_CRYPTO_SOBER ||
  644. instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_OLD)
  645. return encrypt_and_sign_sober(instance, buf, buf_len, iovec, iov_len);
  646. #ifdef HAVE_LIBNSS
  647. if (instance->totem_config->crypto_type == TOTEM_CRYPTO_NSS)
  648. return encrypt_and_sign_nss(instance, buf, buf_len, iovec, iov_len);
  649. #endif
  650. return -1;
  651. }
  652. static int authenticate_and_decrypt (
  653. struct totemudpu_instance *instance,
  654. struct iovec *iov,
  655. unsigned int iov_len)
  656. {
  657. unsigned char type;
  658. unsigned char *endbuf = (unsigned char *)iov[iov_len-1].iov_base;
  659. int res = -1;
  660. /*
  661. * Get the encryption type and remove it from the buffer
  662. */
  663. type = endbuf[iov[iov_len-1].iov_len-1];
  664. iov[iov_len-1].iov_len -= 1;
  665. if (type == TOTEM_CRYPTO_SOBER)
  666. res = authenticate_and_decrypt_sober(instance, iov, iov_len);
  667. /*
  668. * Only try higher crypto options if NEW has been requested
  669. */
  670. if (instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_NEW) {
  671. #ifdef HAVE_LIBNSS
  672. if (type == TOTEM_CRYPTO_NSS)
  673. res = authenticate_and_decrypt_nss(instance, iov, iov_len);
  674. #endif
  675. }
  676. /*
  677. * If it failed, then try decrypting the whole packet as it might be
  678. * from aisexec
  679. */
  680. if (res == -1) {
  681. iov[iov_len-1].iov_len += 1;
  682. res = authenticate_and_decrypt_sober(instance, iov, iov_len);
  683. }
  684. return res;
  685. }
  686. static void init_crypto(
  687. struct totemudpu_instance *instance)
  688. {
  689. /*
  690. * If we are expecting NEW crypto type then initialise all available
  691. * crypto options. For OLD then we only need SOBER128.
  692. */
  693. init_sober_crypto(instance);
  694. if (instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_OLD)
  695. return;
  696. #ifdef HAVE_LIBNSS
  697. init_nss_crypto(instance);
  698. #endif
  699. }
  700. int totemudpu_crypto_set (
  701. void *udpu_context,
  702. unsigned int type)
  703. {
  704. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  705. int res = 0;
  706. /*
  707. * Can't set crypto type if OLD is selected
  708. */
  709. if (instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_OLD) {
  710. res = -1;
  711. } else {
  712. /*
  713. * Validate crypto algorithm
  714. */
  715. switch (type) {
  716. case TOTEM_CRYPTO_SOBER:
  717. log_printf(instance->totemudpu_log_level_security,
  718. "Transmit security set to: libtomcrypt SOBER128/SHA1HMAC (mode 0)");
  719. break;
  720. case TOTEM_CRYPTO_NSS:
  721. log_printf(instance->totemudpu_log_level_security,
  722. "Transmit security set to: NSS AES128CBC/SHA1HMAC (mode 1)");
  723. break;
  724. default:
  725. res = -1;
  726. break;
  727. }
  728. }
  729. return (res);
  730. }
  731. static inline void ucast_sendmsg (
  732. struct totemudpu_instance *instance,
  733. struct totem_ip_address *system_to,
  734. const void *msg,
  735. unsigned int msg_len)
  736. {
  737. struct msghdr msg_ucast;
  738. int res = 0;
  739. size_t buf_len;
  740. unsigned char sheader[sizeof (struct security_header)];
  741. unsigned char encrypt_data[FRAME_SIZE_MAX];
  742. struct iovec iovec_encrypt[2];
  743. const struct iovec *iovec_sendmsg;
  744. struct sockaddr_storage sockaddr;
  745. struct iovec iovec;
  746. unsigned int iov_len;
  747. int addrlen;
  748. if (instance->totem_config->secauth == 1) {
  749. iovec_encrypt[0].iov_base = (void *)sheader;
  750. iovec_encrypt[0].iov_len = sizeof (struct security_header);
  751. iovec_encrypt[1].iov_base = (void *)msg;
  752. iovec_encrypt[1].iov_len = msg_len;
  753. /*
  754. * Encrypt and digest the message
  755. */
  756. encrypt_and_sign_worker (
  757. instance,
  758. encrypt_data,
  759. &buf_len,
  760. iovec_encrypt,
  761. 2);
  762. if (instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_NEW) {
  763. encrypt_data[buf_len++] = instance->totem_config->crypto_type;
  764. }
  765. else {
  766. encrypt_data[buf_len++] = 0;
  767. }
  768. iovec_encrypt[0].iov_base = (void *)encrypt_data;
  769. iovec_encrypt[0].iov_len = buf_len;
  770. iovec_sendmsg = &iovec_encrypt[0];
  771. iov_len = 1;
  772. } else {
  773. iovec.iov_base = (void *)msg;
  774. iovec.iov_len = msg_len;
  775. iovec_sendmsg = &iovec;
  776. iov_len = 1;
  777. }
  778. /*
  779. * Build unicast message
  780. */
  781. totemip_totemip_to_sockaddr_convert(system_to,
  782. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  783. msg_ucast.msg_name = &sockaddr;
  784. msg_ucast.msg_namelen = addrlen;
  785. msg_ucast.msg_iov = (void *) iovec_sendmsg;
  786. msg_ucast.msg_iovlen = iov_len;
  787. #if !defined(COROSYNC_SOLARIS)
  788. msg_ucast.msg_control = 0;
  789. msg_ucast.msg_controllen = 0;
  790. msg_ucast.msg_flags = 0;
  791. #else
  792. msg_ucast.msg_accrights = NULL;
  793. msg_ucast.msg_accrightslen = 0;
  794. #endif
  795. /*
  796. * Transmit unicast message
  797. * An error here is recovered by totemsrp
  798. */
  799. res = sendmsg (instance->token_socket, &msg_ucast, MSG_NOSIGNAL);
  800. if (res < 0) {
  801. LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
  802. "sendmsg(ucast) failed (non-critical)");
  803. }
  804. }
  805. static inline void mcast_sendmsg (
  806. struct totemudpu_instance *instance,
  807. const void *msg,
  808. unsigned int msg_len)
  809. {
  810. struct msghdr msg_mcast;
  811. int res = 0;
  812. size_t buf_len;
  813. unsigned char sheader[sizeof (struct security_header)];
  814. unsigned char encrypt_data[FRAME_SIZE_MAX];
  815. struct iovec iovec_encrypt[2];
  816. struct iovec iovec;
  817. const struct iovec *iovec_sendmsg;
  818. struct sockaddr_storage sockaddr;
  819. unsigned int iov_len;
  820. int addrlen;
  821. struct list_head *list;
  822. struct totemudpu_member *member;
  823. if (instance->totem_config->secauth == 1) {
  824. iovec_encrypt[0].iov_base = (void *)sheader;
  825. iovec_encrypt[0].iov_len = sizeof (struct security_header);
  826. iovec_encrypt[1].iov_base = (void *)msg;
  827. iovec_encrypt[1].iov_len = msg_len;
  828. /*
  829. * Encrypt and digest the message
  830. */
  831. encrypt_and_sign_worker (
  832. instance,
  833. encrypt_data,
  834. &buf_len,
  835. iovec_encrypt,
  836. 2);
  837. if (instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_NEW) {
  838. encrypt_data[buf_len++] = instance->totem_config->crypto_type;
  839. }
  840. else {
  841. encrypt_data[buf_len++] = 0;
  842. }
  843. iovec_encrypt[0].iov_base = (void *)encrypt_data;
  844. iovec_encrypt[0].iov_len = buf_len;
  845. iovec_sendmsg = &iovec_encrypt[0];
  846. iov_len = 1;
  847. } else {
  848. iovec.iov_base = (void *)msg;
  849. iovec.iov_len = msg_len;
  850. iovec_sendmsg = &iovec;
  851. iov_len = 1;
  852. }
  853. /*
  854. * Build multicast message
  855. */
  856. for (list = instance->member_list.next;
  857. list != &instance->member_list;
  858. list = list->next) {
  859. member = list_entry (list,
  860. struct totemudpu_member,
  861. list);
  862. totemip_totemip_to_sockaddr_convert(&member->member,
  863. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  864. msg_mcast.msg_name = &sockaddr;
  865. msg_mcast.msg_namelen = addrlen;
  866. msg_mcast.msg_iov = (void *) iovec_sendmsg;
  867. msg_mcast.msg_iovlen = iov_len;
  868. #if !defined(COROSYNC_SOLARIS)
  869. msg_mcast.msg_control = 0;
  870. msg_mcast.msg_controllen = 0;
  871. msg_mcast.msg_flags = 0;
  872. #else
  873. msg_mcast.msg_accrights = NULL;
  874. msg_mcast.msg_accrightslen = 0;
  875. #endif
  876. /*
  877. * Transmit multicast message
  878. * An error here is recovered by totemsrp
  879. */
  880. res = sendmsg (member->fd, &msg_mcast, MSG_NOSIGNAL);
  881. if (res < 0) {
  882. LOGSYS_PERROR (errno, instance->totemudpu_log_level_debug,
  883. "sendmsg(mcast) failed (non-critical)");
  884. }
  885. }
  886. }
  887. int totemudpu_finalize (
  888. void *udpu_context)
  889. {
  890. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  891. int res = 0;
  892. if (instance->token_socket > 0) {
  893. close (instance->token_socket);
  894. poll_dispatch_delete (instance->totemudpu_poll_handle,
  895. instance->token_socket);
  896. }
  897. return (res);
  898. }
  899. static int net_deliver_fn (
  900. hdb_handle_t handle,
  901. int fd,
  902. int revents,
  903. void *data)
  904. {
  905. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  906. struct msghdr msg_recv;
  907. struct iovec *iovec;
  908. struct security_header *security_header;
  909. struct sockaddr_storage system_from;
  910. int bytes_received;
  911. int res = 0;
  912. unsigned char *msg_offset;
  913. unsigned int size_delv;
  914. iovec = &instance->totemudpu_iov_recv;
  915. /*
  916. * Receive datagram
  917. */
  918. msg_recv.msg_name = &system_from;
  919. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  920. msg_recv.msg_iov = iovec;
  921. msg_recv.msg_iovlen = 1;
  922. #if !defined(COROSYNC_SOLARIS)
  923. msg_recv.msg_control = 0;
  924. msg_recv.msg_controllen = 0;
  925. msg_recv.msg_flags = 0;
  926. #else
  927. msg_recv.msg_accrights = NULL;
  928. msg_recv.msg_accrightslen = 0;
  929. #endif
  930. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  931. if (bytes_received == -1) {
  932. return (0);
  933. } else {
  934. instance->stats_recv += bytes_received;
  935. }
  936. if ((instance->totem_config->secauth == 1) &&
  937. (bytes_received < sizeof (struct security_header))) {
  938. log_printf (instance->totemudpu_log_level_security, "Received message is too short... ignoring %d.\n", bytes_received);
  939. return (0);
  940. }
  941. security_header = (struct security_header *)iovec->iov_base;
  942. iovec->iov_len = bytes_received;
  943. if (instance->totem_config->secauth == 1) {
  944. /*
  945. * Authenticate and if authenticated, decrypt datagram
  946. */
  947. res = authenticate_and_decrypt (instance, iovec, 1);
  948. if (res == -1) {
  949. log_printf (instance->totemudpu_log_level_security, "Received message has invalid digest... ignoring.\n");
  950. log_printf (instance->totemudpu_log_level_security,
  951. "Invalid packet data\n");
  952. iovec->iov_len = FRAME_SIZE_MAX;
  953. return 0;
  954. }
  955. msg_offset = (unsigned char *)iovec->iov_base +
  956. sizeof (struct security_header);
  957. size_delv = bytes_received - sizeof (struct security_header);
  958. } else {
  959. msg_offset = (void *)iovec->iov_base;
  960. size_delv = bytes_received;
  961. }
  962. /*
  963. * Handle incoming message
  964. */
  965. instance->totemudpu_deliver_fn (
  966. instance->context,
  967. msg_offset,
  968. size_delv);
  969. iovec->iov_len = FRAME_SIZE_MAX;
  970. return (0);
  971. }
  972. static int netif_determine (
  973. struct totemudpu_instance *instance,
  974. struct totem_ip_address *bindnet,
  975. struct totem_ip_address *bound_to,
  976. int *interface_up,
  977. int *interface_num)
  978. {
  979. int res;
  980. res = totemip_iface_check (bindnet, bound_to,
  981. interface_up, interface_num,
  982. instance->totem_config->clear_node_high_bit);
  983. return (res);
  984. }
  985. /*
  986. * If the interface is up, the sockets for totem are built. If the interface is down
  987. * this function is requeued in the timer list to retry building the sockets later.
  988. */
  989. static void timer_function_netif_check_timeout (
  990. void *data)
  991. {
  992. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  993. int res;
  994. int interface_up;
  995. int interface_num;
  996. struct totem_ip_address *bind_address;
  997. /*
  998. * Build sockets for every interface
  999. */
  1000. netif_determine (instance,
  1001. &instance->totem_interface->bindnet,
  1002. &instance->totem_interface->boundto,
  1003. &interface_up, &interface_num);
  1004. /*
  1005. * If the network interface isn't back up and we are already
  1006. * in loopback mode, add timer to check again and return
  1007. */
  1008. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  1009. interface_up == 0) ||
  1010. (instance->my_memb_entries == 1 &&
  1011. instance->netif_bind_state == BIND_STATE_REGULAR &&
  1012. interface_up == 1)) {
  1013. poll_timer_add (instance->totemudpu_poll_handle,
  1014. instance->totem_config->downcheck_timeout,
  1015. (void *)instance,
  1016. timer_function_netif_check_timeout,
  1017. &instance->timer_netif_check_timeout);
  1018. /*
  1019. * Add a timer to check for a downed regular interface
  1020. */
  1021. return;
  1022. }
  1023. if (instance->token_socket > 0) {
  1024. close (instance->token_socket);
  1025. poll_dispatch_delete (instance->totemudpu_poll_handle,
  1026. instance->token_socket);
  1027. }
  1028. if (interface_up == 0) {
  1029. /*
  1030. * Interface is not up
  1031. */
  1032. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  1033. bind_address = &localhost;
  1034. /*
  1035. * Add a timer to retry building interfaces and request memb_gather_enter
  1036. */
  1037. poll_timer_add (instance->totemudpu_poll_handle,
  1038. instance->totem_config->downcheck_timeout,
  1039. (void *)instance,
  1040. timer_function_netif_check_timeout,
  1041. &instance->timer_netif_check_timeout);
  1042. } else {
  1043. /*
  1044. * Interface is up
  1045. */
  1046. instance->netif_bind_state = BIND_STATE_REGULAR;
  1047. bind_address = &instance->totem_interface->bindnet;
  1048. }
  1049. /*
  1050. * Create and bind the multicast and unicast sockets
  1051. */
  1052. res = totemudpu_build_sockets (instance,
  1053. bind_address,
  1054. &instance->totem_interface->boundto);
  1055. poll_dispatch_add (
  1056. instance->totemudpu_poll_handle,
  1057. instance->token_socket,
  1058. POLLIN, instance, net_deliver_fn);
  1059. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  1060. /*
  1061. * This reports changes in the interface to the user and totemsrp
  1062. */
  1063. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  1064. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  1065. log_printf (instance->totemudpu_log_level_notice,
  1066. "The network interface [%s] is now up.\n",
  1067. totemip_print (&instance->totem_interface->boundto));
  1068. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  1069. instance->totemudpu_iface_change_fn (instance->context, &instance->my_id);
  1070. }
  1071. /*
  1072. * Add a timer to check for interface going down in single membership
  1073. */
  1074. if (instance->my_memb_entries == 1) {
  1075. poll_timer_add (instance->totemudpu_poll_handle,
  1076. instance->totem_config->downcheck_timeout,
  1077. (void *)instance,
  1078. timer_function_netif_check_timeout,
  1079. &instance->timer_netif_check_timeout);
  1080. }
  1081. } else {
  1082. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  1083. log_printf (instance->totemudpu_log_level_notice,
  1084. "The network interface is down.\n");
  1085. instance->totemudpu_iface_change_fn (instance->context, &instance->my_id);
  1086. }
  1087. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  1088. }
  1089. }
  1090. /* Set the socket priority to INTERACTIVE to ensure
  1091. that our messages don't get queued behind anything else */
  1092. static void totemudpu_traffic_control_set(struct totemudpu_instance *instance, int sock)
  1093. {
  1094. #ifdef SO_PRIORITY
  1095. int prio = 6; /* TC_PRIO_INTERACTIVE */
  1096. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int))) {
  1097. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1098. "Could not set traffic priority");
  1099. }
  1100. #endif
  1101. }
  1102. static int totemudpu_build_sockets_ip (
  1103. struct totemudpu_instance *instance,
  1104. struct totem_ip_address *bindnet_address,
  1105. struct totem_ip_address *bound_to,
  1106. int interface_num)
  1107. {
  1108. struct sockaddr_storage sockaddr;
  1109. int addrlen;
  1110. int res;
  1111. unsigned int recvbuf_size;
  1112. unsigned int optlen = sizeof (recvbuf_size);
  1113. /*
  1114. * Setup unicast socket
  1115. */
  1116. instance->token_socket = socket (bindnet_address->family, SOCK_DGRAM, 0);
  1117. if (instance->token_socket == -1) {
  1118. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1119. "socket() failed");
  1120. return (-1);
  1121. }
  1122. totemip_nosigpipe (instance->token_socket);
  1123. res = fcntl (instance->token_socket, F_SETFL, O_NONBLOCK);
  1124. if (res == -1) {
  1125. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1126. "Could not set non-blocking operation on token socket");
  1127. return (-1);
  1128. }
  1129. /*
  1130. * Bind to unicast socket used for token send/receives
  1131. * This has the side effect of binding to the correct interface
  1132. */
  1133. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  1134. res = bind (instance->token_socket, (struct sockaddr *)&sockaddr, addrlen);
  1135. if (res == -1) {
  1136. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1137. "bind token socket failed");
  1138. return (-1);
  1139. }
  1140. /*
  1141. * the token_socket can receive many messages. Allow a large number
  1142. * of receive messages on this socket
  1143. */
  1144. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  1145. res = setsockopt (instance->token_socket, SOL_SOCKET, SO_RCVBUF,
  1146. &recvbuf_size, optlen);
  1147. if (res == -1) {
  1148. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  1149. "Could not set recvbuf size");
  1150. }
  1151. return 0;
  1152. }
  1153. static int totemudpu_build_sockets (
  1154. struct totemudpu_instance *instance,
  1155. struct totem_ip_address *bindnet_address,
  1156. struct totem_ip_address *bound_to)
  1157. {
  1158. int interface_num;
  1159. int interface_up;
  1160. int res;
  1161. /*
  1162. * Determine the ip address bound to and the interface name
  1163. */
  1164. res = netif_determine (instance,
  1165. bindnet_address,
  1166. bound_to,
  1167. &interface_up,
  1168. &interface_num);
  1169. if (res == -1) {
  1170. return (-1);
  1171. }
  1172. totemip_copy(&instance->my_id, bound_to);
  1173. res = totemudpu_build_sockets_ip (instance,
  1174. bindnet_address, bound_to, interface_num);
  1175. /* We only send out of the token socket */
  1176. totemudpu_traffic_control_set(instance, instance->token_socket);
  1177. /*
  1178. * Rebind all members to new ips
  1179. */
  1180. totemudpu_member_list_rebind_ip(instance);
  1181. return res;
  1182. }
  1183. /*
  1184. * Totem Network interface - also does encryption/decryption
  1185. * depends on poll abstraction, POSIX, IPV4
  1186. */
  1187. /*
  1188. * Create an instance
  1189. */
  1190. int totemudpu_initialize (
  1191. hdb_handle_t poll_handle,
  1192. void **udpu_context,
  1193. struct totem_config *totem_config,
  1194. int interface_no,
  1195. void *context,
  1196. void (*deliver_fn) (
  1197. void *context,
  1198. const void *msg,
  1199. unsigned int msg_len),
  1200. void (*iface_change_fn) (
  1201. void *context,
  1202. const struct totem_ip_address *iface_address),
  1203. void (*target_set_completed) (
  1204. void *context))
  1205. {
  1206. struct totemudpu_instance *instance;
  1207. instance = malloc (sizeof (struct totemudpu_instance));
  1208. if (instance == NULL) {
  1209. return (-1);
  1210. }
  1211. totemudpu_instance_initialize (instance);
  1212. instance->totem_config = totem_config;
  1213. /*
  1214. * Configure logging
  1215. */
  1216. instance->totemudpu_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  1217. instance->totemudpu_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  1218. instance->totemudpu_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  1219. instance->totemudpu_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  1220. instance->totemudpu_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  1221. instance->totemudpu_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1222. instance->totemudpu_log_printf = totem_config->totem_logging_configuration.log_printf;
  1223. /*
  1224. * Initialize random number generator for later use to generate salt
  1225. */
  1226. memcpy (instance->totemudpu_private_key, totem_config->private_key,
  1227. totem_config->private_key_len);
  1228. instance->totemudpu_private_key_len = totem_config->private_key_len;
  1229. init_crypto(instance);
  1230. /*
  1231. * Initialize local variables for totemudpu
  1232. */
  1233. instance->totem_interface = &totem_config->interfaces[interface_no];
  1234. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  1235. instance->totemudpu_poll_handle = poll_handle;
  1236. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  1237. instance->context = context;
  1238. instance->totemudpu_deliver_fn = deliver_fn;
  1239. instance->totemudpu_iface_change_fn = iface_change_fn;
  1240. instance->totemudpu_target_set_completed = target_set_completed;
  1241. totemip_localhost (AF_INET, &localhost);
  1242. localhost.nodeid = instance->totem_config->node_id;
  1243. /*
  1244. * RRP layer isn't ready to receive message because it hasn't
  1245. * initialized yet. Add short timer to check the interfaces.
  1246. */
  1247. poll_timer_add (instance->totemudpu_poll_handle,
  1248. 100,
  1249. (void *)instance,
  1250. timer_function_netif_check_timeout,
  1251. &instance->timer_netif_check_timeout);
  1252. *udpu_context = instance;
  1253. return (0);
  1254. }
  1255. int totemudpu_processor_count_set (
  1256. void *udpu_context,
  1257. int processor_count)
  1258. {
  1259. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1260. int res = 0;
  1261. instance->my_memb_entries = processor_count;
  1262. poll_timer_delete (instance->totemudpu_poll_handle,
  1263. instance->timer_netif_check_timeout);
  1264. if (processor_count == 1) {
  1265. poll_timer_add (instance->totemudpu_poll_handle,
  1266. instance->totem_config->downcheck_timeout,
  1267. (void *)instance,
  1268. timer_function_netif_check_timeout,
  1269. &instance->timer_netif_check_timeout);
  1270. }
  1271. return (res);
  1272. }
  1273. int totemudpu_recv_flush (void *udpu_context)
  1274. {
  1275. int res = 0;
  1276. return (res);
  1277. }
  1278. int totemudpu_send_flush (void *udpu_context)
  1279. {
  1280. int res = 0;
  1281. return (res);
  1282. }
  1283. int totemudpu_token_send (
  1284. void *udpu_context,
  1285. const void *msg,
  1286. unsigned int msg_len)
  1287. {
  1288. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1289. int res = 0;
  1290. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  1291. return (res);
  1292. }
  1293. int totemudpu_mcast_flush_send (
  1294. void *udpu_context,
  1295. const void *msg,
  1296. unsigned int msg_len)
  1297. {
  1298. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1299. int res = 0;
  1300. mcast_sendmsg (instance, msg, msg_len);
  1301. return (res);
  1302. }
  1303. int totemudpu_mcast_noflush_send (
  1304. void *udpu_context,
  1305. const void *msg,
  1306. unsigned int msg_len)
  1307. {
  1308. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1309. int res = 0;
  1310. mcast_sendmsg (instance, msg, msg_len);
  1311. return (res);
  1312. }
  1313. extern int totemudpu_iface_check (void *udpu_context)
  1314. {
  1315. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1316. int res = 0;
  1317. timer_function_netif_check_timeout (instance);
  1318. return (res);
  1319. }
  1320. extern void totemudpu_net_mtu_adjust (void *udpu_context, struct totem_config *totem_config)
  1321. {
  1322. #define UDPIP_HEADER_SIZE (20 + 8) /* 20 bytes for ip 8 bytes for udp */
  1323. if (totem_config->secauth == 1) {
  1324. totem_config->net_mtu -= sizeof (struct security_header) +
  1325. UDPIP_HEADER_SIZE;
  1326. } else {
  1327. totem_config->net_mtu -= UDPIP_HEADER_SIZE;
  1328. }
  1329. }
  1330. const char *totemudpu_iface_print (void *udpu_context) {
  1331. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1332. const char *ret_char;
  1333. ret_char = totemip_print (&instance->my_id);
  1334. return (ret_char);
  1335. }
  1336. int totemudpu_iface_get (
  1337. void *udpu_context,
  1338. struct totem_ip_address *addr)
  1339. {
  1340. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1341. int res = 0;
  1342. memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
  1343. return (res);
  1344. }
  1345. int totemudpu_token_target_set (
  1346. void *udpu_context,
  1347. const struct totem_ip_address *token_target)
  1348. {
  1349. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1350. int res = 0;
  1351. memcpy (&instance->token_target, token_target,
  1352. sizeof (struct totem_ip_address));
  1353. instance->totemudpu_target_set_completed (instance->context);
  1354. return (res);
  1355. }
  1356. extern int totemudpu_recv_mcast_empty (
  1357. void *udpu_context)
  1358. {
  1359. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1360. unsigned int res;
  1361. struct sockaddr_storage system_from;
  1362. struct msghdr msg_recv;
  1363. struct pollfd ufd;
  1364. int nfds;
  1365. int msg_processed = 0;
  1366. /*
  1367. * Receive datagram
  1368. */
  1369. msg_recv.msg_name = &system_from;
  1370. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  1371. msg_recv.msg_iov = &instance->totemudpu_iov_recv;
  1372. msg_recv.msg_iovlen = 1;
  1373. #if !defined(COROSYNC_SOLARIS)
  1374. msg_recv.msg_control = 0;
  1375. msg_recv.msg_controllen = 0;
  1376. msg_recv.msg_flags = 0;
  1377. #else
  1378. msg_recv.msg_accrights = NULL;
  1379. msg_recv.msg_accrightslen = 0;
  1380. #endif
  1381. do {
  1382. ufd.fd = instance->token_socket;
  1383. ufd.events = POLLIN;
  1384. nfds = poll (&ufd, 1, 0);
  1385. if (nfds == 1 && ufd.revents & POLLIN) {
  1386. res = recvmsg (instance->token_socket, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  1387. if (res != -1) {
  1388. msg_processed = 1;
  1389. } else {
  1390. msg_processed = -1;
  1391. }
  1392. }
  1393. } while (nfds == 1);
  1394. return (msg_processed);
  1395. }
  1396. static int totemudpu_create_sending_socket(
  1397. void *udpu_context,
  1398. const struct totem_ip_address *member)
  1399. {
  1400. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1401. int fd;
  1402. int res;
  1403. unsigned int sendbuf_size;
  1404. unsigned int optlen = sizeof (sendbuf_size);
  1405. struct sockaddr_storage sockaddr;
  1406. int addrlen;
  1407. fd = socket (member->family, SOCK_DGRAM, 0);
  1408. if (fd == -1) {
  1409. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1410. "Could not create socket for new member");
  1411. return (-1);
  1412. }
  1413. totemip_nosigpipe (fd);
  1414. res = fcntl (fd, F_SETFL, O_NONBLOCK);
  1415. if (res == -1) {
  1416. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1417. "Could not set non-blocking operation on token socket");
  1418. return (-1);
  1419. }
  1420. /*
  1421. * These sockets are used to send multicast messages, so their buffers
  1422. * should be large
  1423. */
  1424. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  1425. res = setsockopt (fd, SOL_SOCKET, SO_SNDBUF,
  1426. &sendbuf_size, optlen);
  1427. if (res == -1) {
  1428. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  1429. "Could not set sendbuf size");
  1430. }
  1431. /*
  1432. * Bind to sending interface
  1433. */
  1434. totemip_totemip_to_sockaddr_convert(&instance->my_id, 0, &sockaddr, &addrlen);
  1435. res = bind (fd, (struct sockaddr *)&sockaddr, addrlen);
  1436. if (res == -1) {
  1437. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1438. "bind token socket failed");
  1439. return (-1);
  1440. }
  1441. return (fd);
  1442. }
  1443. int totemudpu_member_add (
  1444. void *udpu_context,
  1445. const struct totem_ip_address *member)
  1446. {
  1447. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1448. struct totemudpu_member *new_member;
  1449. new_member = malloc (sizeof (struct totemudpu_member));
  1450. if (new_member == NULL) {
  1451. return (-1);
  1452. }
  1453. log_printf (LOGSYS_LEVEL_NOTICE, "adding new UDPU member {%s}",
  1454. totemip_print(member));
  1455. list_init (&new_member->list);
  1456. list_add_tail (&new_member->list, &instance->member_list);
  1457. memcpy (&new_member->member, member, sizeof (struct totem_ip_address));
  1458. new_member->fd = totemudpu_create_sending_socket(udpu_context, member);
  1459. return (0);
  1460. }
  1461. int totemudpu_member_remove (
  1462. void *udpu_context,
  1463. const struct totem_ip_address *token_target)
  1464. {
  1465. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1466. instance = NULL;
  1467. return (0);
  1468. }
  1469. int totemudpu_member_list_rebind_ip (
  1470. void *udpu_context)
  1471. {
  1472. struct list_head *list;
  1473. struct totemudpu_member *member;
  1474. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1475. for (list = instance->member_list.next;
  1476. list != &instance->member_list;
  1477. list = list->next) {
  1478. member = list_entry (list,
  1479. struct totemudpu_member,
  1480. list);
  1481. if (member->fd > 0) {
  1482. close (member->fd);
  1483. }
  1484. member->fd = totemudpu_create_sending_socket(udpu_context, &member->member);
  1485. }
  1486. return (0);
  1487. }