4
0

totemudpu.c 43 KB

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