totemudpu.c 45 KB

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