totemudp.c 50 KB

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