totemudpu.c 48 KB

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