totemudpu.c 46 KB

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