totemudpu.c 43 KB

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