totemudpu.c 43 KB

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