totemudpu.c 44 KB

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