totemudp.c 53 KB

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