totemudp.c 54 KB

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