totemudpu.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720
  1. /*
  2. * Copyright (c) 2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2009 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <config.h>
  35. #include <assert.h>
  36. #include <sys/mman.h>
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include <sys/socket.h>
  40. #include <netdb.h>
  41. #include <sys/un.h>
  42. #include <sys/ioctl.h>
  43. #include <sys/param.h>
  44. #include <netinet/in.h>
  45. #include <arpa/inet.h>
  46. #include <unistd.h>
  47. #include <fcntl.h>
  48. #include <stdlib.h>
  49. #include <stdio.h>
  50. #include <errno.h>
  51. #include <sched.h>
  52. #include <time.h>
  53. #include <sys/time.h>
  54. #include <sys/poll.h>
  55. #include <limits.h>
  56. #include <qb/qbdefs.h>
  57. #include <qb/qbloop.h>
  58. #include <corosync/sq.h>
  59. #include <corosync/list.h>
  60. #include <corosync/hdb.h>
  61. #include <corosync/swab.h>
  62. #define LOGSYS_UTILS_ONLY 1
  63. #include <corosync/engine/logsys.h>
  64. #include "totemudpu.h"
  65. #include "crypto.h"
  66. #include "util.h"
  67. #ifdef HAVE_LIBNSS
  68. #include <nss.h>
  69. #include <pk11pub.h>
  70. #include <pkcs11.h>
  71. #include <prerror.h>
  72. #endif
  73. #ifndef MSG_NOSIGNAL
  74. #define MSG_NOSIGNAL 0
  75. #endif
  76. #define MCAST_SOCKET_BUFFER_SIZE (TRANSMITS_ALLOWED * FRAME_SIZE_MAX)
  77. #define NETIF_STATE_REPORT_UP 1
  78. #define NETIF_STATE_REPORT_DOWN 2
  79. #define BIND_STATE_UNBOUND 0
  80. #define BIND_STATE_REGULAR 1
  81. #define BIND_STATE_LOOPBACK 2
  82. #define HMAC_HASH_SIZE 20
  83. struct security_header {
  84. unsigned char hash_digest[HMAC_HASH_SIZE]; /* The hash *MUST* be first in the data structure */
  85. unsigned char salt[16]; /* random number */
  86. char msg[0];
  87. } __attribute__((packed));
  88. struct totemudpu_member {
  89. struct list_head list;
  90. struct totem_ip_address member;
  91. int fd;
  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. qb_loop_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. int level,
  126. int subsys,
  127. const char *function,
  128. const char *file,
  129. int line,
  130. const char *format,
  131. ...)__attribute__((format(printf, 6, 7)));
  132. void *udpu_context;
  133. char iov_buffer[FRAME_SIZE_MAX];
  134. struct iovec totemudpu_iov_recv;
  135. struct list_head member_list;
  136. int stats_sent;
  137. int stats_recv;
  138. int stats_delv;
  139. int stats_remcasts;
  140. int stats_orf_token;
  141. struct timeval stats_tv_start;
  142. struct totem_ip_address my_id;
  143. int firstrun;
  144. qb_loop_timer_handle timer_netif_check_timeout;
  145. unsigned int my_memb_entries;
  146. struct totem_config *totem_config;
  147. struct totem_ip_address token_target;
  148. int token_socket;
  149. };
  150. struct work_item {
  151. const void *msg;
  152. unsigned int msg_len;
  153. struct totemudpu_instance *instance;
  154. };
  155. static int totemudpu_build_sockets (
  156. struct totemudpu_instance *instance,
  157. struct totem_ip_address *bindnet_address,
  158. struct totem_ip_address *bound_to);
  159. static struct totem_ip_address localhost;
  160. static void totemudpu_instance_initialize (struct totemudpu_instance *instance)
  161. {
  162. memset (instance, 0, sizeof (struct totemudpu_instance));
  163. instance->netif_state_report = NETIF_STATE_REPORT_UP | NETIF_STATE_REPORT_DOWN;
  164. instance->totemudpu_iov_recv.iov_base = instance->iov_buffer;
  165. instance->totemudpu_iov_recv.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  166. /*
  167. * There is always atleast 1 processor
  168. */
  169. instance->my_memb_entries = 1;
  170. list_init (&instance->member_list);
  171. }
  172. #define log_printf(level, format, args...) \
  173. do { \
  174. instance->totemudpu_log_printf ( \
  175. level, instance->totemudpu_subsys_id, \
  176. __FUNCTION__, __FILE__, __LINE__, \
  177. (const char *)format, ##args); \
  178. } while (0);
  179. #define LOGSYS_PERROR(err_num, level, fmt, args...) \
  180. do { \
  181. char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
  182. const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
  183. instance->totemudpu_log_printf ( \
  184. level, instance->totemudpu_subsys_id, \
  185. __FUNCTION__, __FILE__, __LINE__, \
  186. fmt ": %s (%d)\n", ##args, _error_ptr, err_num); \
  187. } while(0)
  188. static int authenticate_and_decrypt_sober (
  189. struct totemudpu_instance *instance,
  190. struct iovec *iov,
  191. unsigned int iov_len)
  192. {
  193. unsigned char keys[48];
  194. struct security_header *header = (struct security_header *)iov[0].iov_base;
  195. prng_state keygen_prng_state;
  196. prng_state stream_prng_state;
  197. unsigned char *hmac_key = &keys[32];
  198. unsigned char *cipher_key = &keys[16];
  199. unsigned char *initial_vector = &keys[0];
  200. unsigned char digest_comparison[HMAC_HASH_SIZE];
  201. unsigned long len;
  202. /*
  203. * Generate MAC, CIPHER, IV keys from private key
  204. */
  205. memset (keys, 0, sizeof (keys));
  206. sober128_start (&keygen_prng_state);
  207. sober128_add_entropy (instance->totemudpu_private_key,
  208. instance->totemudpu_private_key_len, &keygen_prng_state);
  209. sober128_add_entropy (header->salt, sizeof (header->salt), &keygen_prng_state);
  210. sober128_read (keys, sizeof (keys), &keygen_prng_state);
  211. /*
  212. * Setup stream cipher
  213. */
  214. sober128_start (&stream_prng_state);
  215. sober128_add_entropy (cipher_key, 16, &stream_prng_state);
  216. sober128_add_entropy (initial_vector, 16, &stream_prng_state);
  217. /*
  218. * Authenticate contents of message
  219. */
  220. hmac_init (&instance->totemudpu_hmac_state, DIGEST_SHA1, hmac_key, 16);
  221. hmac_process (&instance->totemudpu_hmac_state,
  222. (unsigned char *)iov->iov_base + HMAC_HASH_SIZE,
  223. iov->iov_len - HMAC_HASH_SIZE);
  224. len = hash_descriptor[DIGEST_SHA1]->hashsize;
  225. assert (HMAC_HASH_SIZE >= len);
  226. hmac_done (&instance->totemudpu_hmac_state, digest_comparison, &len);
  227. if (memcmp (digest_comparison, header->hash_digest, len) != 0) {
  228. return (-1);
  229. }
  230. /*
  231. * Decrypt the contents of the message with the cipher key
  232. */
  233. sober128_read ((unsigned char*)iov->iov_base +
  234. sizeof (struct security_header),
  235. iov->iov_len - sizeof (struct security_header),
  236. &stream_prng_state);
  237. return (0);
  238. }
  239. static void init_sober_crypto(
  240. struct totemudpu_instance *instance)
  241. {
  242. log_printf(instance->totemudpu_log_level_notice,
  243. "Initializing transmit/receive security: libtomcrypt SOBER128/SHA1HMAC (mode 0).\n");
  244. rng_make_prng (128, PRNG_SOBER, &instance->totemudpu_prng_state, NULL);
  245. }
  246. #ifdef HAVE_LIBNSS
  247. static unsigned char *copy_from_iovec(
  248. const struct iovec *iov,
  249. unsigned int iov_len,
  250. size_t *buf_size)
  251. {
  252. int i;
  253. size_t bufptr;
  254. size_t buflen = 0;
  255. unsigned char *newbuf;
  256. for (i=0; i<iov_len; i++)
  257. buflen += iov[i].iov_len;
  258. newbuf = malloc(buflen);
  259. if (!newbuf)
  260. return NULL;
  261. bufptr=0;
  262. for (i=0; i<iov_len; i++) {
  263. memcpy(newbuf+bufptr, iov[i].iov_base, iov[i].iov_len);
  264. bufptr += iov[i].iov_len;
  265. }
  266. *buf_size = buflen;
  267. return newbuf;
  268. }
  269. static void copy_to_iovec(
  270. struct iovec *iov,
  271. unsigned int iov_len,
  272. const unsigned char *buf,
  273. size_t buf_size)
  274. {
  275. int i;
  276. size_t copylen;
  277. size_t bufptr = 0;
  278. bufptr=0;
  279. for (i=0; i<iov_len; i++) {
  280. copylen = iov[i].iov_len;
  281. if (bufptr + copylen > buf_size) {
  282. copylen = buf_size - bufptr;
  283. }
  284. memcpy(iov[i].iov_base, buf+bufptr, copylen);
  285. bufptr += copylen;
  286. if (iov[i].iov_len != copylen) {
  287. iov[i].iov_len = copylen;
  288. return;
  289. }
  290. }
  291. }
  292. static void init_nss_crypto(
  293. struct totemudpu_instance *instance)
  294. {
  295. PK11SlotInfo* aes_slot = NULL;
  296. PK11SlotInfo* sha1_slot = NULL;
  297. SECItem key_item;
  298. SECStatus rv;
  299. log_printf(instance->totemudpu_log_level_notice,
  300. "Initializing transmit/receive security: NSS AES128CBC/SHA1HMAC (mode 1).\n");
  301. rv = NSS_NoDB_Init(".");
  302. if (rv != SECSuccess)
  303. {
  304. log_printf(instance->totemudpu_log_level_security, "NSS initialization failed (err %d)\n",
  305. PR_GetError());
  306. goto out;
  307. }
  308. aes_slot = PK11_GetBestSlot(instance->totem_config->crypto_crypt_type, NULL);
  309. if (aes_slot == NULL)
  310. {
  311. log_printf(instance->totemudpu_log_level_security, "Unable to find security slot (err %d)\n",
  312. PR_GetError());
  313. goto out;
  314. }
  315. sha1_slot = PK11_GetBestSlot(CKM_SHA_1_HMAC, NULL);
  316. if (sha1_slot == NULL)
  317. {
  318. log_printf(instance->totemudpu_log_level_security, "Unable to find security slot (err %d)\n",
  319. PR_GetError());
  320. goto out;
  321. }
  322. /*
  323. * Make the private key into a SymKey that we can use
  324. */
  325. key_item.type = siBuffer;
  326. key_item.data = instance->totem_config->private_key;
  327. key_item.len = 32; /* Use 128 bits */
  328. instance->nss_sym_key = PK11_ImportSymKey(aes_slot,
  329. instance->totem_config->crypto_crypt_type,
  330. PK11_OriginUnwrap, CKA_ENCRYPT|CKA_DECRYPT,
  331. &key_item, NULL);
  332. if (instance->nss_sym_key == NULL)
  333. {
  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. instance->nss_sym_key_sign = PK11_ImportSymKey(sha1_slot,
  339. CKM_SHA_1_HMAC,
  340. PK11_OriginUnwrap, CKA_SIGN,
  341. &key_item, NULL);
  342. if (instance->nss_sym_key_sign == NULL) {
  343. log_printf(instance->totemudpu_log_level_security, "Failure to import key into NSS (err %d)\n",
  344. PR_GetError());
  345. goto out;
  346. }
  347. out:
  348. return;
  349. }
  350. static int encrypt_and_sign_nss (
  351. struct totemudpu_instance *instance,
  352. unsigned char *buf,
  353. size_t *buf_len,
  354. const struct iovec *iovec,
  355. unsigned int iov_len)
  356. {
  357. PK11Context* enc_context = NULL;
  358. SECStatus rv1, rv2;
  359. int tmp1_outlen;
  360. unsigned int tmp2_outlen;
  361. unsigned char *inbuf;
  362. unsigned char *data;
  363. unsigned char *outdata;
  364. size_t datalen;
  365. SECItem no_params;
  366. SECItem iv_item;
  367. struct security_header *header;
  368. SECItem *nss_sec_param;
  369. unsigned char nss_iv_data[16];
  370. SECStatus rv;
  371. no_params.type = siBuffer;
  372. no_params.data = 0;
  373. no_params.len = 0;
  374. tmp1_outlen = tmp2_outlen = 0;
  375. inbuf = copy_from_iovec(iovec, iov_len, &datalen);
  376. if (!inbuf) {
  377. log_printf(instance->totemudpu_log_level_security, "malloc error copying buffer from iovec\n");
  378. return -1;
  379. }
  380. data = inbuf + sizeof (struct security_header);
  381. datalen -= sizeof (struct security_header);
  382. outdata = buf + sizeof (struct security_header);
  383. header = (struct security_header *)buf;
  384. rv = PK11_GenerateRandom (
  385. nss_iv_data,
  386. sizeof (nss_iv_data));
  387. if (rv != SECSuccess) {
  388. log_printf(instance->totemudpu_log_level_security,
  389. "Failure to generate a random number %d\n",
  390. PR_GetError());
  391. }
  392. memcpy(header->salt, nss_iv_data, sizeof(nss_iv_data));
  393. iv_item.type = siBuffer;
  394. iv_item.data = nss_iv_data;
  395. iv_item.len = sizeof (nss_iv_data);
  396. nss_sec_param = PK11_ParamFromIV (
  397. instance->totem_config->crypto_crypt_type,
  398. &iv_item);
  399. if (nss_sec_param == NULL) {
  400. log_printf(instance->totemudpu_log_level_security,
  401. "Failure to set up PKCS11 param (err %d)\n",
  402. PR_GetError());
  403. free (inbuf);
  404. return (-1);
  405. }
  406. /*
  407. * Create cipher context for encryption
  408. */
  409. enc_context = PK11_CreateContextBySymKey (
  410. instance->totem_config->crypto_crypt_type,
  411. CKA_ENCRYPT,
  412. instance->nss_sym_key,
  413. nss_sec_param);
  414. if (!enc_context) {
  415. char err[1024];
  416. PR_GetErrorText(err);
  417. err[PR_GetErrorTextLength()] = 0;
  418. log_printf(instance->totemudpu_log_level_security,
  419. "PK11_CreateContext failed (encrypt) crypt_type=%d (err %d): %s\n",
  420. instance->totem_config->crypto_crypt_type,
  421. PR_GetError(), err);
  422. free(inbuf);
  423. return -1;
  424. }
  425. rv1 = PK11_CipherOp(enc_context, outdata,
  426. &tmp1_outlen, FRAME_SIZE_MAX - sizeof(struct security_header),
  427. data, datalen);
  428. rv2 = PK11_DigestFinal(enc_context, outdata + tmp1_outlen, &tmp2_outlen,
  429. FRAME_SIZE_MAX - tmp1_outlen);
  430. PK11_DestroyContext(enc_context, PR_TRUE);
  431. *buf_len = tmp1_outlen + tmp2_outlen;
  432. free(inbuf);
  433. // memcpy(&outdata[*buf_len], nss_iv_data, sizeof(nss_iv_data));
  434. if (rv1 != SECSuccess || rv2 != SECSuccess)
  435. goto out;
  436. /* Now do the digest */
  437. enc_context = PK11_CreateContextBySymKey(CKM_SHA_1_HMAC,
  438. CKA_SIGN, instance->nss_sym_key_sign, &no_params);
  439. if (!enc_context) {
  440. char err[1024];
  441. PR_GetErrorText(err);
  442. err[PR_GetErrorTextLength()] = 0;
  443. log_printf(instance->totemudpu_log_level_security, "encrypt: PK11_CreateContext failed (digest) err %d: %s\n",
  444. PR_GetError(), err);
  445. return -1;
  446. }
  447. PK11_DigestBegin(enc_context);
  448. rv1 = PK11_DigestOp(enc_context, outdata - 16, *buf_len + 16);
  449. rv2 = PK11_DigestFinal(enc_context, header->hash_digest, &tmp2_outlen, sizeof(header->hash_digest));
  450. PK11_DestroyContext(enc_context, PR_TRUE);
  451. if (rv1 != SECSuccess || rv2 != SECSuccess)
  452. goto out;
  453. *buf_len = *buf_len + sizeof(struct security_header);
  454. SECITEM_FreeItem(nss_sec_param, PR_TRUE);
  455. return 0;
  456. out:
  457. return -1;
  458. }
  459. static int authenticate_and_decrypt_nss (
  460. struct totemudpu_instance *instance,
  461. struct iovec *iov,
  462. unsigned int iov_len)
  463. {
  464. PK11Context* enc_context = NULL;
  465. SECStatus rv1, rv2;
  466. int tmp1_outlen;
  467. unsigned int tmp2_outlen;
  468. unsigned char outbuf[FRAME_SIZE_MAX];
  469. unsigned char digest[HMAC_HASH_SIZE];
  470. unsigned char *outdata;
  471. int result_len;
  472. unsigned char *data;
  473. unsigned char *inbuf;
  474. size_t datalen;
  475. struct security_header *header = (struct security_header *)iov[0].iov_base;
  476. SECItem no_params;
  477. SECItem ivdata;
  478. no_params.type = siBuffer;
  479. no_params.data = 0;
  480. no_params.len = 0;
  481. tmp1_outlen = tmp2_outlen = 0;
  482. if (iov_len > 1) {
  483. inbuf = copy_from_iovec(iov, iov_len, &datalen);
  484. if (!inbuf) {
  485. log_printf(instance->totemudpu_log_level_security, "malloc error copying buffer from iovec\n");
  486. return -1;
  487. }
  488. }
  489. else {
  490. inbuf = (unsigned char *)iov[0].iov_base;
  491. datalen = iov[0].iov_len;
  492. }
  493. data = inbuf + sizeof (struct security_header) - 16;
  494. datalen = datalen - sizeof (struct security_header) + 16;
  495. outdata = outbuf + sizeof (struct security_header);
  496. /* Check the digest */
  497. enc_context = PK11_CreateContextBySymKey (
  498. CKM_SHA_1_HMAC, CKA_SIGN,
  499. instance->nss_sym_key_sign,
  500. &no_params);
  501. if (!enc_context) {
  502. char err[1024];
  503. PR_GetErrorText(err);
  504. err[PR_GetErrorTextLength()] = 0;
  505. log_printf(instance->totemudpu_log_level_security, "PK11_CreateContext failed (check digest) err %d: %s\n",
  506. PR_GetError(), err);
  507. free (inbuf);
  508. return -1;
  509. }
  510. PK11_DigestBegin(enc_context);
  511. rv1 = PK11_DigestOp(enc_context, data, datalen);
  512. rv2 = PK11_DigestFinal(enc_context, digest, &tmp2_outlen, sizeof(digest));
  513. PK11_DestroyContext(enc_context, PR_TRUE);
  514. if (rv1 != SECSuccess || rv2 != SECSuccess) {
  515. log_printf(instance->totemudpu_log_level_security, "Digest check failed\n");
  516. return -1;
  517. }
  518. if (memcmp(digest, header->hash_digest, tmp2_outlen) != 0) {
  519. log_printf(instance->totemudpu_log_level_error, "Digest does not match\n");
  520. return -1;
  521. }
  522. /*
  523. * Get rid of salt
  524. */
  525. data += 16;
  526. datalen -= 16;
  527. /* Create cipher context for decryption */
  528. ivdata.type = siBuffer;
  529. ivdata.data = header->salt;
  530. ivdata.len = sizeof(header->salt);
  531. enc_context = PK11_CreateContextBySymKey(
  532. instance->totem_config->crypto_crypt_type,
  533. CKA_DECRYPT,
  534. instance->nss_sym_key, &ivdata);
  535. if (!enc_context) {
  536. log_printf(instance->totemudpu_log_level_security,
  537. "PK11_CreateContext (decrypt) failed (err %d)\n",
  538. PR_GetError());
  539. return -1;
  540. }
  541. rv1 = PK11_CipherOp(enc_context, outdata, &tmp1_outlen,
  542. sizeof(outbuf) - sizeof (struct security_header),
  543. data, datalen);
  544. if (rv1 != SECSuccess) {
  545. log_printf(instance->totemudpu_log_level_security,
  546. "PK11_CipherOp (decrypt) failed (err %d)\n",
  547. PR_GetError());
  548. }
  549. rv2 = PK11_DigestFinal(enc_context, outdata + tmp1_outlen, &tmp2_outlen,
  550. sizeof(outbuf) - tmp1_outlen);
  551. PK11_DestroyContext(enc_context, PR_TRUE);
  552. result_len = tmp1_outlen + tmp2_outlen + sizeof (struct security_header);
  553. /* Copy it back to the buffer */
  554. copy_to_iovec(iov, iov_len, outbuf, result_len);
  555. if (iov_len > 1)
  556. free(inbuf);
  557. if (rv1 != SECSuccess || rv2 != SECSuccess)
  558. return -1;
  559. return 0;
  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. qb_loop_poll_del (instance->totemudpu_poll_handle,
  890. instance->token_socket);
  891. }
  892. return (res);
  893. }
  894. static int net_deliver_fn (
  895. int fd,
  896. int revents,
  897. void *data)
  898. {
  899. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  900. struct msghdr msg_recv;
  901. struct iovec *iovec;
  902. struct sockaddr_storage system_from;
  903. int bytes_received;
  904. int res = 0;
  905. unsigned char *msg_offset;
  906. unsigned int size_delv;
  907. iovec = &instance->totemudpu_iov_recv;
  908. /*
  909. * Receive datagram
  910. */
  911. msg_recv.msg_name = &system_from;
  912. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  913. msg_recv.msg_iov = iovec;
  914. msg_recv.msg_iovlen = 1;
  915. #if !defined(COROSYNC_SOLARIS)
  916. msg_recv.msg_control = 0;
  917. msg_recv.msg_controllen = 0;
  918. msg_recv.msg_flags = 0;
  919. #else
  920. msg_recv.msg_accrights = NULL;
  921. msg_recv.msg_accrightslen = 0;
  922. #endif
  923. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  924. if (bytes_received == -1) {
  925. return (0);
  926. } else {
  927. instance->stats_recv += bytes_received;
  928. }
  929. if ((instance->totem_config->secauth == 1) &&
  930. (bytes_received < sizeof (struct security_header))) {
  931. log_printf (instance->totemudpu_log_level_security, "Received message is too short... ignoring %d.\n", bytes_received);
  932. return (0);
  933. }
  934. iovec->iov_len = bytes_received;
  935. if (instance->totem_config->secauth == 1) {
  936. /*
  937. * Authenticate and if authenticated, decrypt datagram
  938. */
  939. res = authenticate_and_decrypt (instance, iovec, 1);
  940. if (res == -1) {
  941. log_printf (instance->totemudpu_log_level_security, "Received message has invalid digest... ignoring.\n");
  942. log_printf (instance->totemudpu_log_level_security,
  943. "Invalid packet data\n");
  944. iovec->iov_len = FRAME_SIZE_MAX;
  945. return 0;
  946. }
  947. msg_offset = (unsigned char *)iovec->iov_base +
  948. sizeof (struct security_header);
  949. size_delv = bytes_received - sizeof (struct security_header);
  950. } else {
  951. msg_offset = (void *)iovec->iov_base;
  952. size_delv = bytes_received;
  953. }
  954. /*
  955. * Handle incoming message
  956. */
  957. instance->totemudpu_deliver_fn (
  958. instance->context,
  959. msg_offset,
  960. size_delv);
  961. iovec->iov_len = FRAME_SIZE_MAX;
  962. return (0);
  963. }
  964. static int netif_determine (
  965. struct totemudpu_instance *instance,
  966. struct totem_ip_address *bindnet,
  967. struct totem_ip_address *bound_to,
  968. int *interface_up,
  969. int *interface_num)
  970. {
  971. int res;
  972. res = totemip_iface_check (bindnet, bound_to,
  973. interface_up, interface_num,
  974. instance->totem_config->clear_node_high_bit);
  975. return (res);
  976. }
  977. /*
  978. * If the interface is up, the sockets for totem are built. If the interface is down
  979. * this function is requeued in the timer list to retry building the sockets later.
  980. */
  981. static void timer_function_netif_check_timeout (
  982. void *data)
  983. {
  984. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  985. int interface_up;
  986. int interface_num;
  987. struct totem_ip_address *bind_address;
  988. /*
  989. * Build sockets for every interface
  990. */
  991. netif_determine (instance,
  992. &instance->totem_interface->bindnet,
  993. &instance->totem_interface->boundto,
  994. &interface_up, &interface_num);
  995. /*
  996. * If the network interface isn't back up and we are already
  997. * in loopback mode, add timer to check again and return
  998. */
  999. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  1000. interface_up == 0) ||
  1001. (instance->my_memb_entries == 1 &&
  1002. instance->netif_bind_state == BIND_STATE_REGULAR &&
  1003. interface_up == 1)) {
  1004. qb_loop_timer_add (instance->totemudpu_poll_handle,
  1005. QB_LOOP_MED,
  1006. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  1007. (void *)instance,
  1008. timer_function_netif_check_timeout,
  1009. &instance->timer_netif_check_timeout);
  1010. /*
  1011. * Add a timer to check for a downed regular interface
  1012. */
  1013. return;
  1014. }
  1015. if (instance->token_socket > 0) {
  1016. close (instance->token_socket);
  1017. qb_loop_poll_del (instance->totemudpu_poll_handle,
  1018. instance->token_socket);
  1019. }
  1020. if (interface_up == 0) {
  1021. /*
  1022. * Interface is not up
  1023. */
  1024. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  1025. bind_address = &localhost;
  1026. /*
  1027. * Add a timer to retry building interfaces and request memb_gather_enter
  1028. */
  1029. qb_loop_timer_add (instance->totemudpu_poll_handle,
  1030. QB_LOOP_MED,
  1031. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  1032. (void *)instance,
  1033. timer_function_netif_check_timeout,
  1034. &instance->timer_netif_check_timeout);
  1035. } else {
  1036. /*
  1037. * Interface is up
  1038. */
  1039. instance->netif_bind_state = BIND_STATE_REGULAR;
  1040. bind_address = &instance->totem_interface->bindnet;
  1041. }
  1042. /*
  1043. * Create and bind the multicast and unicast sockets
  1044. */
  1045. totemudpu_build_sockets (instance,
  1046. bind_address,
  1047. &instance->totem_interface->boundto);
  1048. qb_loop_poll_add (instance->totemudpu_poll_handle,
  1049. QB_LOOP_MED,
  1050. instance->token_socket,
  1051. POLLIN, instance, net_deliver_fn);
  1052. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  1053. /*
  1054. * This reports changes in the interface to the user and totemsrp
  1055. */
  1056. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  1057. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  1058. log_printf (instance->totemudpu_log_level_notice,
  1059. "The network interface [%s] is now up.\n",
  1060. totemip_print (&instance->totem_interface->boundto));
  1061. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  1062. instance->totemudpu_iface_change_fn (instance->context, &instance->my_id);
  1063. }
  1064. /*
  1065. * Add a timer to check for interface going down in single membership
  1066. */
  1067. if (instance->my_memb_entries == 1) {
  1068. qb_loop_timer_add (instance->totemudpu_poll_handle,
  1069. QB_LOOP_MED,
  1070. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  1071. (void *)instance,
  1072. timer_function_netif_check_timeout,
  1073. &instance->timer_netif_check_timeout);
  1074. }
  1075. } else {
  1076. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  1077. log_printf (instance->totemudpu_log_level_notice,
  1078. "The network interface is down.\n");
  1079. instance->totemudpu_iface_change_fn (instance->context, &instance->my_id);
  1080. }
  1081. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  1082. }
  1083. }
  1084. /* Set the socket priority to INTERACTIVE to ensure
  1085. that our messages don't get queued behind anything else */
  1086. static void totemudpu_traffic_control_set(struct totemudpu_instance *instance, int sock)
  1087. {
  1088. #ifdef SO_PRIORITY
  1089. int prio = 6; /* TC_PRIO_INTERACTIVE */
  1090. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int))) {
  1091. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1092. "Could not set traffic priority");
  1093. }
  1094. #endif
  1095. }
  1096. static int totemudpu_build_sockets_ip (
  1097. struct totemudpu_instance *instance,
  1098. struct totem_ip_address *bindnet_address,
  1099. struct totem_ip_address *bound_to,
  1100. int interface_num)
  1101. {
  1102. struct sockaddr_storage sockaddr;
  1103. int addrlen;
  1104. int res;
  1105. unsigned int recvbuf_size;
  1106. unsigned int optlen = sizeof (recvbuf_size);
  1107. /*
  1108. * Setup unicast socket
  1109. */
  1110. instance->token_socket = socket (bindnet_address->family, SOCK_DGRAM, 0);
  1111. if (instance->token_socket == -1) {
  1112. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1113. "socket() failed");
  1114. return (-1);
  1115. }
  1116. totemip_nosigpipe (instance->token_socket);
  1117. res = fcntl (instance->token_socket, F_SETFL, O_NONBLOCK);
  1118. if (res == -1) {
  1119. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1120. "Could not set non-blocking operation on token socket");
  1121. return (-1);
  1122. }
  1123. /*
  1124. * Bind to unicast socket used for token send/receives
  1125. * This has the side effect of binding to the correct interface
  1126. */
  1127. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  1128. res = bind (instance->token_socket, (struct sockaddr *)&sockaddr, addrlen);
  1129. if (res == -1) {
  1130. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1131. "bind token socket failed");
  1132. return (-1);
  1133. }
  1134. /*
  1135. * the token_socket can receive many messages. Allow a large number
  1136. * of receive messages on this socket
  1137. */
  1138. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  1139. res = setsockopt (instance->token_socket, SOL_SOCKET, SO_RCVBUF,
  1140. &recvbuf_size, optlen);
  1141. if (res == -1) {
  1142. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  1143. "Could not set recvbuf size");
  1144. }
  1145. return 0;
  1146. }
  1147. static int totemudpu_build_sockets (
  1148. struct totemudpu_instance *instance,
  1149. struct totem_ip_address *bindnet_address,
  1150. struct totem_ip_address *bound_to)
  1151. {
  1152. int interface_num;
  1153. int interface_up;
  1154. int res;
  1155. /*
  1156. * Determine the ip address bound to and the interface name
  1157. */
  1158. res = netif_determine (instance,
  1159. bindnet_address,
  1160. bound_to,
  1161. &interface_up,
  1162. &interface_num);
  1163. if (res == -1) {
  1164. return (-1);
  1165. }
  1166. totemip_copy(&instance->my_id, bound_to);
  1167. res = totemudpu_build_sockets_ip (instance,
  1168. bindnet_address, bound_to, interface_num);
  1169. /* We only send out of the token socket */
  1170. totemudpu_traffic_control_set(instance, instance->token_socket);
  1171. return res;
  1172. }
  1173. /*
  1174. * Totem Network interface - also does encryption/decryption
  1175. * depends on poll abstraction, POSIX, IPV4
  1176. */
  1177. /*
  1178. * Create an instance
  1179. */
  1180. int totemudpu_initialize (
  1181. qb_loop_t *poll_handle,
  1182. void **udpu_context,
  1183. struct totem_config *totem_config,
  1184. int interface_no,
  1185. void *context,
  1186. void (*deliver_fn) (
  1187. void *context,
  1188. const void *msg,
  1189. unsigned int msg_len),
  1190. void (*iface_change_fn) (
  1191. void *context,
  1192. const struct totem_ip_address *iface_address),
  1193. void (*target_set_completed) (
  1194. void *context))
  1195. {
  1196. struct totemudpu_instance *instance;
  1197. instance = malloc (sizeof (struct totemudpu_instance));
  1198. if (instance == NULL) {
  1199. return (-1);
  1200. }
  1201. totemudpu_instance_initialize (instance);
  1202. instance->totem_config = totem_config;
  1203. /*
  1204. * Configure logging
  1205. */
  1206. instance->totemudpu_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  1207. instance->totemudpu_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  1208. instance->totemudpu_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  1209. instance->totemudpu_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  1210. instance->totemudpu_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  1211. instance->totemudpu_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1212. instance->totemudpu_log_printf = totem_config->totem_logging_configuration.log_printf;
  1213. /*
  1214. * Initialize random number generator for later use to generate salt
  1215. */
  1216. memcpy (instance->totemudpu_private_key, totem_config->private_key,
  1217. totem_config->private_key_len);
  1218. instance->totemudpu_private_key_len = totem_config->private_key_len;
  1219. init_crypto(instance);
  1220. /*
  1221. * Initialize local variables for totemudpu
  1222. */
  1223. instance->totem_interface = &totem_config->interfaces[interface_no];
  1224. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  1225. instance->totemudpu_poll_handle = poll_handle;
  1226. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  1227. instance->context = context;
  1228. instance->totemudpu_deliver_fn = deliver_fn;
  1229. instance->totemudpu_iface_change_fn = iface_change_fn;
  1230. instance->totemudpu_target_set_completed = target_set_completed;
  1231. totemip_localhost (AF_INET, &localhost);
  1232. localhost.nodeid = instance->totem_config->node_id;
  1233. /*
  1234. * RRP layer isn't ready to receive message because it hasn't
  1235. * initialized yet. Add short timer to check the interfaces.
  1236. */
  1237. qb_loop_timer_add (instance->totemudpu_poll_handle,
  1238. QB_LOOP_MED,
  1239. 100*QB_TIME_NS_IN_MSEC,
  1240. (void *)instance,
  1241. timer_function_netif_check_timeout,
  1242. &instance->timer_netif_check_timeout);
  1243. *udpu_context = instance;
  1244. return (0);
  1245. }
  1246. void *totemudpu_buffer_alloc (void)
  1247. {
  1248. return malloc (FRAME_SIZE_MAX);
  1249. }
  1250. void totemudpu_buffer_release (void *ptr)
  1251. {
  1252. return free (ptr);
  1253. }
  1254. int totemudpu_processor_count_set (
  1255. void *udpu_context,
  1256. int processor_count)
  1257. {
  1258. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1259. int res = 0;
  1260. instance->my_memb_entries = processor_count;
  1261. qb_loop_timer_del (instance->totemudpu_poll_handle,
  1262. instance->timer_netif_check_timeout);
  1263. if (processor_count == 1) {
  1264. qb_loop_timer_add (instance->totemudpu_poll_handle,
  1265. QB_LOOP_MED,
  1266. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  1267. (void *)instance,
  1268. timer_function_netif_check_timeout,
  1269. &instance->timer_netif_check_timeout);
  1270. }
  1271. return (res);
  1272. }
  1273. int totemudpu_recv_flush (void *udpu_context)
  1274. {
  1275. int res = 0;
  1276. return (res);
  1277. }
  1278. int totemudpu_send_flush (void *udpu_context)
  1279. {
  1280. int res = 0;
  1281. return (res);
  1282. }
  1283. int totemudpu_token_send (
  1284. void *udpu_context,
  1285. const void *msg,
  1286. unsigned int msg_len)
  1287. {
  1288. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1289. int res = 0;
  1290. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  1291. return (res);
  1292. }
  1293. int totemudpu_mcast_flush_send (
  1294. void *udpu_context,
  1295. const void *msg,
  1296. unsigned int msg_len)
  1297. {
  1298. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1299. int res = 0;
  1300. mcast_sendmsg (instance, msg, msg_len);
  1301. return (res);
  1302. }
  1303. int totemudpu_mcast_noflush_send (
  1304. void *udpu_context,
  1305. const void *msg,
  1306. unsigned int msg_len)
  1307. {
  1308. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1309. int res = 0;
  1310. mcast_sendmsg (instance, msg, msg_len);
  1311. return (res);
  1312. }
  1313. extern int totemudpu_iface_check (void *udpu_context)
  1314. {
  1315. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1316. int res = 0;
  1317. timer_function_netif_check_timeout (instance);
  1318. return (res);
  1319. }
  1320. extern void totemudpu_net_mtu_adjust (void *udpu_context, struct totem_config *totem_config)
  1321. {
  1322. #define UDPIP_HEADER_SIZE (20 + 8) /* 20 bytes for ip 8 bytes for udp */
  1323. if (totem_config->secauth == 1) {
  1324. totem_config->net_mtu -= sizeof (struct security_header) +
  1325. UDPIP_HEADER_SIZE;
  1326. } else {
  1327. totem_config->net_mtu -= UDPIP_HEADER_SIZE;
  1328. }
  1329. }
  1330. const char *totemudpu_iface_print (void *udpu_context) {
  1331. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1332. const char *ret_char;
  1333. ret_char = totemip_print (&instance->my_id);
  1334. return (ret_char);
  1335. }
  1336. int totemudpu_iface_get (
  1337. void *udpu_context,
  1338. struct totem_ip_address *addr)
  1339. {
  1340. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1341. int res = 0;
  1342. memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
  1343. return (res);
  1344. }
  1345. int totemudpu_token_target_set (
  1346. void *udpu_context,
  1347. const struct totem_ip_address *token_target)
  1348. {
  1349. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1350. int res = 0;
  1351. memcpy (&instance->token_target, token_target,
  1352. sizeof (struct totem_ip_address));
  1353. instance->totemudpu_target_set_completed (instance->context);
  1354. return (res);
  1355. }
  1356. extern int totemudpu_recv_mcast_empty (
  1357. void *udpu_context)
  1358. {
  1359. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1360. unsigned int res;
  1361. struct sockaddr_storage system_from;
  1362. struct msghdr msg_recv;
  1363. struct pollfd ufd;
  1364. int nfds;
  1365. int msg_processed = 0;
  1366. /*
  1367. * Receive datagram
  1368. */
  1369. msg_recv.msg_name = &system_from;
  1370. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  1371. msg_recv.msg_iov = &instance->totemudpu_iov_recv;
  1372. msg_recv.msg_iovlen = 1;
  1373. #if !defined(COROSYNC_SOLARIS)
  1374. msg_recv.msg_control = 0;
  1375. msg_recv.msg_controllen = 0;
  1376. msg_recv.msg_flags = 0;
  1377. #else
  1378. msg_recv.msg_accrights = NULL;
  1379. msg_recv.msg_accrightslen = 0;
  1380. #endif
  1381. do {
  1382. ufd.fd = instance->token_socket;
  1383. ufd.events = POLLIN;
  1384. nfds = poll (&ufd, 1, 0);
  1385. if (nfds == 1 && ufd.revents & POLLIN) {
  1386. res = recvmsg (instance->token_socket, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  1387. if (res != -1) {
  1388. msg_processed = 1;
  1389. } else {
  1390. msg_processed = -1;
  1391. }
  1392. }
  1393. } while (nfds == 1);
  1394. return (msg_processed);
  1395. }
  1396. int totemudpu_member_add (
  1397. void *udpu_context,
  1398. const struct totem_ip_address *member)
  1399. {
  1400. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1401. struct totemudpu_member *new_member;
  1402. int res;
  1403. unsigned int sendbuf_size;
  1404. unsigned int optlen = sizeof (sendbuf_size);
  1405. new_member = malloc (sizeof (struct totemudpu_member));
  1406. if (new_member == NULL) {
  1407. return (-1);
  1408. }
  1409. list_init (&new_member->list);
  1410. list_add_tail (&new_member->list, &instance->member_list);
  1411. memcpy (&new_member->member, member, sizeof (struct totem_ip_address));
  1412. new_member->fd = socket (member->family, SOCK_DGRAM, 0);
  1413. if (new_member->fd == -1) {
  1414. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1415. "Could not create socket for new member");
  1416. return (-1);
  1417. }
  1418. totemip_nosigpipe (new_member->fd);
  1419. res = fcntl (new_member->fd, F_SETFL, O_NONBLOCK);
  1420. if (res == -1) {
  1421. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1422. "Could not set non-blocking operation on token socket");
  1423. return (-1);
  1424. }
  1425. /*
  1426. * These sockets are used to send multicast messages, so their buffers
  1427. * should be large
  1428. */
  1429. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  1430. res = setsockopt (new_member->fd, SOL_SOCKET, SO_SNDBUF,
  1431. &sendbuf_size, optlen);
  1432. if (res == -1) {
  1433. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  1434. "Could not set sendbuf size");
  1435. }
  1436. return (0);
  1437. }
  1438. int totemudpu_member_remove (
  1439. void *udpu_context,
  1440. const struct totem_ip_address *token_target)
  1441. {
  1442. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1443. instance = NULL;
  1444. return (0);
  1445. }