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