totemudpu.c 43 KB

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