totemudp.c 58 KB

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