totemudp.c 53 KB

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