totemudp.c 58 KB

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