4
0

totemudp.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010
  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 security_header *security_header;
  981. struct sockaddr_storage system_from;
  982. int bytes_received;
  983. int res = 0;
  984. unsigned char *msg_offset;
  985. unsigned int size_delv;
  986. iovec = &instance->totemudp_iov_recv;
  987. /*
  988. * Receive datagram
  989. */
  990. msg_recv.msg_name = &system_from;
  991. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  992. msg_recv.msg_iov = iovec;
  993. msg_recv.msg_iovlen = 1;
  994. #if !defined(COROSYNC_SOLARIS)
  995. msg_recv.msg_control = 0;
  996. msg_recv.msg_controllen = 0;
  997. msg_recv.msg_flags = 0;
  998. #else
  999. msg_recv.msg_accrights = NULL;
  1000. msg_recv.msg_accrightslen = 0;
  1001. #endif
  1002. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  1003. if (bytes_received == -1) {
  1004. return (0);
  1005. } else {
  1006. instance->stats_recv += bytes_received;
  1007. }
  1008. if ((instance->totem_config->secauth == 1) &&
  1009. (bytes_received < sizeof (struct security_header))) {
  1010. log_printf (instance->totemudp_log_level_security, "Received message is too short... ignoring %d.\n", bytes_received);
  1011. return (0);
  1012. }
  1013. security_header = (struct security_header *)iovec->iov_base;
  1014. iovec->iov_len = bytes_received;
  1015. if (instance->totem_config->secauth == 1) {
  1016. /*
  1017. * Authenticate and if authenticated, decrypt datagram
  1018. */
  1019. res = authenticate_and_decrypt (instance, iovec, 1);
  1020. if (res == -1) {
  1021. log_printf (instance->totemudp_log_level_security, "Received message has invalid digest... ignoring.\n");
  1022. log_printf (instance->totemudp_log_level_security,
  1023. "Invalid packet data\n");
  1024. iovec->iov_len = FRAME_SIZE_MAX;
  1025. return 0;
  1026. }
  1027. msg_offset = (unsigned char *)iovec->iov_base +
  1028. sizeof (struct security_header);
  1029. size_delv = bytes_received - sizeof (struct security_header);
  1030. } else {
  1031. msg_offset = (void *)iovec->iov_base;
  1032. size_delv = bytes_received;
  1033. }
  1034. /*
  1035. * Handle incoming message
  1036. */
  1037. instance->totemudp_deliver_fn (
  1038. instance->context,
  1039. msg_offset,
  1040. size_delv);
  1041. iovec->iov_len = FRAME_SIZE_MAX;
  1042. return (0);
  1043. }
  1044. static int netif_determine (
  1045. struct totemudp_instance *instance,
  1046. struct totem_ip_address *bindnet,
  1047. struct totem_ip_address *bound_to,
  1048. int *interface_up,
  1049. int *interface_num)
  1050. {
  1051. int res;
  1052. res = totemip_iface_check (bindnet, bound_to,
  1053. interface_up, interface_num,
  1054. instance->totem_config->clear_node_high_bit);
  1055. return (res);
  1056. }
  1057. /*
  1058. * If the interface is up, the sockets for totem are built. If the interface is down
  1059. * this function is requeued in the timer list to retry building the sockets later.
  1060. */
  1061. static void timer_function_netif_check_timeout (
  1062. void *data)
  1063. {
  1064. struct totemudp_instance *instance = (struct totemudp_instance *)data;
  1065. int res;
  1066. int interface_up;
  1067. int interface_num;
  1068. struct totem_ip_address *bind_address;
  1069. /*
  1070. * Build sockets for every interface
  1071. */
  1072. netif_determine (instance,
  1073. &instance->totem_interface->bindnet,
  1074. &instance->totem_interface->boundto,
  1075. &interface_up, &interface_num);
  1076. /*
  1077. * If the network interface isn't back up and we are already
  1078. * in loopback mode, add timer to check again and return
  1079. */
  1080. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  1081. interface_up == 0) ||
  1082. (instance->my_memb_entries == 1 &&
  1083. instance->netif_bind_state == BIND_STATE_REGULAR &&
  1084. interface_up == 1)) {
  1085. poll_timer_add (instance->totemudp_poll_handle,
  1086. instance->totem_config->downcheck_timeout,
  1087. (void *)instance,
  1088. timer_function_netif_check_timeout,
  1089. &instance->timer_netif_check_timeout);
  1090. /*
  1091. * Add a timer to check for a downed regular interface
  1092. */
  1093. return;
  1094. }
  1095. if (instance->totemudp_sockets.mcast_recv > 0) {
  1096. close (instance->totemudp_sockets.mcast_recv);
  1097. poll_dispatch_delete (instance->totemudp_poll_handle,
  1098. instance->totemudp_sockets.mcast_recv);
  1099. }
  1100. if (instance->totemudp_sockets.mcast_send > 0) {
  1101. close (instance->totemudp_sockets.mcast_send);
  1102. }
  1103. if (instance->totemudp_sockets.token > 0) {
  1104. close (instance->totemudp_sockets.token);
  1105. poll_dispatch_delete (instance->totemudp_poll_handle,
  1106. instance->totemudp_sockets.token);
  1107. }
  1108. if (interface_up == 0) {
  1109. /*
  1110. * Interface is not up
  1111. */
  1112. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  1113. bind_address = &localhost;
  1114. /*
  1115. * Add a timer to retry building interfaces and request memb_gather_enter
  1116. */
  1117. poll_timer_add (instance->totemudp_poll_handle,
  1118. instance->totem_config->downcheck_timeout,
  1119. (void *)instance,
  1120. timer_function_netif_check_timeout,
  1121. &instance->timer_netif_check_timeout);
  1122. } else {
  1123. /*
  1124. * Interface is up
  1125. */
  1126. instance->netif_bind_state = BIND_STATE_REGULAR;
  1127. bind_address = &instance->totem_interface->bindnet;
  1128. }
  1129. /*
  1130. * Create and bind the multicast and unicast sockets
  1131. */
  1132. res = totemudp_build_sockets (instance,
  1133. &instance->mcast_address,
  1134. bind_address,
  1135. &instance->totemudp_sockets,
  1136. &instance->totem_interface->boundto);
  1137. poll_dispatch_add (
  1138. instance->totemudp_poll_handle,
  1139. instance->totemudp_sockets.mcast_recv,
  1140. POLLIN, instance, net_deliver_fn);
  1141. poll_dispatch_add (
  1142. instance->totemudp_poll_handle,
  1143. instance->totemudp_sockets.token,
  1144. POLLIN, instance, net_deliver_fn);
  1145. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  1146. /*
  1147. * This reports changes in the interface to the user and totemsrp
  1148. */
  1149. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  1150. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  1151. log_printf (instance->totemudp_log_level_notice,
  1152. "The network interface [%s] is now up.\n",
  1153. totemip_print (&instance->totem_interface->boundto));
  1154. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  1155. instance->totemudp_iface_change_fn (instance->context, &instance->my_id);
  1156. }
  1157. /*
  1158. * Add a timer to check for interface going down in single membership
  1159. */
  1160. if (instance->my_memb_entries == 1) {
  1161. poll_timer_add (instance->totemudp_poll_handle,
  1162. instance->totem_config->downcheck_timeout,
  1163. (void *)instance,
  1164. timer_function_netif_check_timeout,
  1165. &instance->timer_netif_check_timeout);
  1166. }
  1167. } else {
  1168. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  1169. log_printf (instance->totemudp_log_level_notice,
  1170. "The network interface is down.\n");
  1171. instance->totemudp_iface_change_fn (instance->context, &instance->my_id);
  1172. }
  1173. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  1174. }
  1175. }
  1176. /* Set the socket priority to INTERACTIVE to ensure
  1177. that our messages don't get queued behind anything else */
  1178. static void totemudp_traffic_control_set(struct totemudp_instance *instance, int sock)
  1179. {
  1180. #ifdef SO_PRIORITY
  1181. int prio = 6; /* TC_PRIO_INTERACTIVE */
  1182. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int))) {
  1183. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning, "Could not set traffic priority");
  1184. }
  1185. #endif
  1186. }
  1187. static int totemudp_build_sockets_ip (
  1188. struct totemudp_instance *instance,
  1189. struct totem_ip_address *mcast_address,
  1190. struct totem_ip_address *bindnet_address,
  1191. struct totemudp_socket *sockets,
  1192. struct totem_ip_address *bound_to,
  1193. int interface_num)
  1194. {
  1195. struct sockaddr_storage sockaddr;
  1196. struct ipv6_mreq mreq6;
  1197. struct ip_mreq mreq;
  1198. struct sockaddr_storage mcast_ss, boundto_ss;
  1199. struct sockaddr_in6 *mcast_sin6 = (struct sockaddr_in6 *)&mcast_ss;
  1200. struct sockaddr_in *mcast_sin = (struct sockaddr_in *)&mcast_ss;
  1201. struct sockaddr_in *boundto_sin = (struct sockaddr_in *)&boundto_ss;
  1202. unsigned int sendbuf_size;
  1203. unsigned int recvbuf_size;
  1204. unsigned int optlen = sizeof (sendbuf_size);
  1205. int addrlen;
  1206. int res;
  1207. int flag;
  1208. /*
  1209. * Create multicast recv socket
  1210. */
  1211. sockets->mcast_recv = socket (bindnet_address->family, SOCK_DGRAM, 0);
  1212. if (sockets->mcast_recv == -1) {
  1213. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1214. "socket() failed");
  1215. return (-1);
  1216. }
  1217. totemip_nosigpipe (sockets->mcast_recv);
  1218. res = fcntl (sockets->mcast_recv, F_SETFL, O_NONBLOCK);
  1219. if (res == -1) {
  1220. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1221. "Could not set non-blocking operation on multicast socket");
  1222. return (-1);
  1223. }
  1224. /*
  1225. * Force reuse
  1226. */
  1227. flag = 1;
  1228. if ( setsockopt(sockets->mcast_recv, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  1229. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1230. "setsockopt(SO_REUSEADDR) failed");
  1231. return (-1);
  1232. }
  1233. /*
  1234. * Bind to multicast socket used for multicast receives
  1235. */
  1236. totemip_totemip_to_sockaddr_convert(mcast_address,
  1237. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  1238. res = bind (sockets->mcast_recv, (struct sockaddr *)&sockaddr, addrlen);
  1239. if (res == -1) {
  1240. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1241. "Unable to bind the socket to receive multicast packets");
  1242. return (-1);
  1243. }
  1244. /*
  1245. * Setup mcast send socket
  1246. */
  1247. sockets->mcast_send = socket (bindnet_address->family, SOCK_DGRAM, 0);
  1248. if (sockets->mcast_send == -1) {
  1249. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1250. "socket() failed");
  1251. return (-1);
  1252. }
  1253. totemip_nosigpipe (sockets->mcast_send);
  1254. res = fcntl (sockets->mcast_send, F_SETFL, O_NONBLOCK);
  1255. if (res == -1) {
  1256. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1257. "Could not set non-blocking operation on multicast socket");
  1258. return (-1);
  1259. }
  1260. /*
  1261. * Force reuse
  1262. */
  1263. flag = 1;
  1264. if ( setsockopt(sockets->mcast_send, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  1265. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1266. "setsockopt(SO_REUSEADDR) failed");
  1267. return (-1);
  1268. }
  1269. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port - 1,
  1270. &sockaddr, &addrlen);
  1271. res = bind (sockets->mcast_send, (struct sockaddr *)&sockaddr, addrlen);
  1272. if (res == -1) {
  1273. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1274. "Unable to bind the socket to send multicast packets");
  1275. return (-1);
  1276. }
  1277. /*
  1278. * Setup unicast socket
  1279. */
  1280. sockets->token = socket (bindnet_address->family, SOCK_DGRAM, 0);
  1281. if (sockets->token == -1) {
  1282. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1283. "socket() failed");
  1284. return (-1);
  1285. }
  1286. totemip_nosigpipe (sockets->token);
  1287. res = fcntl (sockets->token, F_SETFL, O_NONBLOCK);
  1288. if (res == -1) {
  1289. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1290. "Could not set non-blocking operation on token socket");
  1291. return (-1);
  1292. }
  1293. /*
  1294. * Force reuse
  1295. */
  1296. flag = 1;
  1297. if ( setsockopt(sockets->token, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  1298. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1299. "setsockopt(SO_REUSEADDR) failed");
  1300. return (-1);
  1301. }
  1302. /*
  1303. * Bind to unicast socket used for token send/receives
  1304. * This has the side effect of binding to the correct interface
  1305. */
  1306. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  1307. res = bind (sockets->token, (struct sockaddr *)&sockaddr, addrlen);
  1308. if (res == -1) {
  1309. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1310. "Unable to bind UDP unicast socket");
  1311. return (-1);
  1312. }
  1313. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  1314. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  1315. /*
  1316. * Set buffer sizes to avoid overruns
  1317. */
  1318. res = setsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  1319. res = setsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  1320. res = getsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  1321. if (res == 0) {
  1322. log_printf (instance->totemudp_log_level_debug,
  1323. "Receive multicast socket recv buffer size (%d bytes).\n", recvbuf_size);
  1324. }
  1325. res = getsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  1326. if (res == 0) {
  1327. log_printf (instance->totemudp_log_level_debug,
  1328. "Transmit multicast socket send buffer size (%d bytes).\n", sendbuf_size);
  1329. }
  1330. /*
  1331. * Join group membership on socket
  1332. */
  1333. totemip_totemip_to_sockaddr_convert(mcast_address, instance->totem_interface->ip_port, &mcast_ss, &addrlen);
  1334. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &boundto_ss, &addrlen);
  1335. if (instance->totem_config->broadcast_use == 1) {
  1336. unsigned int broadcast = 1;
  1337. if ((setsockopt(sockets->mcast_recv, SOL_SOCKET,
  1338. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  1339. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1340. "setting broadcast option failed");
  1341. return (-1);
  1342. }
  1343. if ((setsockopt(sockets->mcast_send, SOL_SOCKET,
  1344. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  1345. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1346. "setting broadcast option failed");
  1347. return (-1);
  1348. }
  1349. } else {
  1350. switch (bindnet_address->family) {
  1351. case AF_INET:
  1352. memset(&mreq, 0, sizeof(mreq));
  1353. mreq.imr_multiaddr.s_addr = mcast_sin->sin_addr.s_addr;
  1354. mreq.imr_interface.s_addr = boundto_sin->sin_addr.s_addr;
  1355. res = setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  1356. &mreq, sizeof (mreq));
  1357. if (res == -1) {
  1358. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1359. "join ipv4 multicast group failed");
  1360. return (-1);
  1361. }
  1362. break;
  1363. case AF_INET6:
  1364. memset(&mreq6, 0, sizeof(mreq6));
  1365. memcpy(&mreq6.ipv6mr_multiaddr, &mcast_sin6->sin6_addr, sizeof(struct in6_addr));
  1366. mreq6.ipv6mr_interface = interface_num;
  1367. res = setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_JOIN_GROUP,
  1368. &mreq6, sizeof (mreq6));
  1369. if (res == -1) {
  1370. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1371. "join ipv6 multicast group failed");
  1372. return (-1);
  1373. }
  1374. break;
  1375. }
  1376. }
  1377. /*
  1378. * Turn on multicast loopback
  1379. */
  1380. flag = 1;
  1381. switch ( bindnet_address->family ) {
  1382. case AF_INET:
  1383. res = setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_LOOP,
  1384. &flag, sizeof (flag));
  1385. break;
  1386. case AF_INET6:
  1387. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
  1388. &flag, sizeof (flag));
  1389. }
  1390. if (res == -1) {
  1391. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1392. "Unable to turn on multicast loopback");
  1393. return (-1);
  1394. }
  1395. /*
  1396. * Set multicast packets TTL
  1397. */
  1398. if ( bindnet_address->family == AF_INET6 )
  1399. {
  1400. flag = 255;
  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. }
  1409. /*
  1410. * Bind to a specific interface for multicast send and receive
  1411. */
  1412. switch ( bindnet_address->family ) {
  1413. case AF_INET:
  1414. if (setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_IF,
  1415. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  1416. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1417. "cannot select interface for multicast packets (send)");
  1418. return (-1);
  1419. }
  1420. if (setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_MULTICAST_IF,
  1421. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  1422. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1423. "cannot select interface for multicast packets (recv)");
  1424. return (-1);
  1425. }
  1426. break;
  1427. case AF_INET6:
  1428. if (setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  1429. &interface_num, sizeof (interface_num)) < 0) {
  1430. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1431. "cannot select interface for multicast packets (send v6)");
  1432. return (-1);
  1433. }
  1434. if (setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  1435. &interface_num, sizeof (interface_num)) < 0) {
  1436. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1437. "cannot select interface for multicast packets (recv v6)");
  1438. return (-1);
  1439. }
  1440. break;
  1441. }
  1442. return 0;
  1443. }
  1444. static int totemudp_build_sockets (
  1445. struct totemudp_instance *instance,
  1446. struct totem_ip_address *mcast_address,
  1447. struct totem_ip_address *bindnet_address,
  1448. struct totemudp_socket *sockets,
  1449. struct totem_ip_address *bound_to)
  1450. {
  1451. int interface_num;
  1452. int interface_up;
  1453. int res;
  1454. /*
  1455. * Determine the ip address bound to and the interface name
  1456. */
  1457. res = netif_determine (instance,
  1458. bindnet_address,
  1459. bound_to,
  1460. &interface_up,
  1461. &interface_num);
  1462. if (res == -1) {
  1463. return (-1);
  1464. }
  1465. totemip_copy(&instance->my_id, bound_to);
  1466. res = totemudp_build_sockets_ip (instance, mcast_address,
  1467. bindnet_address, sockets, bound_to, interface_num);
  1468. /* We only send out of the token socket */
  1469. totemudp_traffic_control_set(instance, sockets->token);
  1470. return res;
  1471. }
  1472. /*
  1473. * Totem Network interface - also does encryption/decryption
  1474. * depends on poll abstraction, POSIX, IPV4
  1475. */
  1476. /*
  1477. * Create an instance
  1478. */
  1479. int totemudp_initialize (
  1480. hdb_handle_t poll_handle,
  1481. void **udp_context,
  1482. struct totem_config *totem_config,
  1483. int interface_no,
  1484. void *context,
  1485. void (*deliver_fn) (
  1486. void *context,
  1487. const void *msg,
  1488. unsigned int msg_len),
  1489. void (*iface_change_fn) (
  1490. void *context,
  1491. const struct totem_ip_address *iface_address),
  1492. void (*target_set_completed) (
  1493. void *context))
  1494. {
  1495. struct totemudp_instance *instance;
  1496. instance = malloc (sizeof (struct totemudp_instance));
  1497. if (instance == NULL) {
  1498. return (-1);
  1499. }
  1500. totemudp_instance_initialize (instance);
  1501. instance->totem_config = totem_config;
  1502. /*
  1503. * Configure logging
  1504. */
  1505. instance->totemudp_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  1506. instance->totemudp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  1507. instance->totemudp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  1508. instance->totemudp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  1509. instance->totemudp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  1510. instance->totemudp_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1511. instance->totemudp_log_printf = totem_config->totem_logging_configuration.log_printf;
  1512. /*
  1513. * Initialize random number generator for later use to generate salt
  1514. */
  1515. memcpy (instance->totemudp_private_key, totem_config->private_key,
  1516. totem_config->private_key_len);
  1517. instance->totemudp_private_key_len = totem_config->private_key_len;
  1518. init_crypto(instance);
  1519. /*
  1520. * Initialize local variables for totemudp
  1521. */
  1522. instance->totem_interface = &totem_config->interfaces[interface_no];
  1523. totemip_copy (&instance->mcast_address, &instance->totem_interface->mcast_addr);
  1524. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  1525. /*
  1526. * If threaded send requested, initialize thread group data structure
  1527. */
  1528. if (totem_config->threads) {
  1529. worker_thread_group_init (
  1530. &instance->worker_thread_group,
  1531. totem_config->threads, 128,
  1532. sizeof (struct work_item),
  1533. sizeof (struct totemudp_mcast_thread_state),
  1534. totemudp_mcast_thread_state_constructor,
  1535. totemudp_mcast_worker_fn);
  1536. }
  1537. instance->totemudp_poll_handle = poll_handle;
  1538. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  1539. instance->context = context;
  1540. instance->totemudp_deliver_fn = deliver_fn;
  1541. instance->totemudp_iface_change_fn = iface_change_fn;
  1542. instance->totemudp_target_set_completed = target_set_completed;
  1543. totemip_localhost (instance->mcast_address.family, &localhost);
  1544. localhost.nodeid = instance->totem_config->node_id;
  1545. /*
  1546. * RRP layer isn't ready to receive message because it hasn't
  1547. * initialized yet. Add short timer to check the interfaces.
  1548. */
  1549. poll_timer_add (instance->totemudp_poll_handle,
  1550. 100,
  1551. (void *)instance,
  1552. timer_function_netif_check_timeout,
  1553. &instance->timer_netif_check_timeout);
  1554. *udp_context = instance;
  1555. return (0);
  1556. }
  1557. int totemudp_processor_count_set (
  1558. void *udp_context,
  1559. int processor_count)
  1560. {
  1561. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1562. int res = 0;
  1563. instance->my_memb_entries = processor_count;
  1564. poll_timer_delete (instance->totemudp_poll_handle,
  1565. instance->timer_netif_check_timeout);
  1566. if (processor_count == 1) {
  1567. poll_timer_add (instance->totemudp_poll_handle,
  1568. instance->totem_config->downcheck_timeout,
  1569. (void *)instance,
  1570. timer_function_netif_check_timeout,
  1571. &instance->timer_netif_check_timeout);
  1572. }
  1573. return (res);
  1574. }
  1575. int totemudp_send_flush (void *udp_context)
  1576. {
  1577. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1578. int res = 0;
  1579. worker_thread_group_wait (&instance->worker_thread_group);
  1580. return (res);
  1581. }
  1582. int totemudp_token_send (
  1583. void *udp_context,
  1584. const void *msg,
  1585. unsigned int msg_len)
  1586. {
  1587. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1588. int res = 0;
  1589. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  1590. return (res);
  1591. }
  1592. int totemudp_mcast_flush_send (
  1593. void *udp_context,
  1594. const void *msg,
  1595. unsigned int msg_len)
  1596. {
  1597. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1598. int res = 0;
  1599. mcast_sendmsg (instance, msg, msg_len);
  1600. return (res);
  1601. }
  1602. int totemudp_mcast_noflush_send (
  1603. void *udp_context,
  1604. const void *msg,
  1605. unsigned int msg_len)
  1606. {
  1607. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1608. struct work_item work_item;
  1609. int res = 0;
  1610. if (instance->totem_config->threads) {
  1611. work_item.msg = msg;
  1612. work_item.msg_len = msg_len;
  1613. work_item.instance = instance;
  1614. worker_thread_group_work_add (&instance->worker_thread_group,
  1615. &work_item);
  1616. } else {
  1617. mcast_sendmsg (instance, msg, msg_len);
  1618. }
  1619. return (res);
  1620. }
  1621. extern int totemudp_iface_check (void *udp_context)
  1622. {
  1623. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1624. int res = 0;
  1625. timer_function_netif_check_timeout (instance);
  1626. return (res);
  1627. }
  1628. extern void totemudp_net_mtu_adjust (void *udp_context, struct totem_config *totem_config)
  1629. {
  1630. #define UDPIP_HEADER_SIZE (20 + 8) /* 20 bytes for ip 8 bytes for udp */
  1631. if (totem_config->secauth == 1) {
  1632. totem_config->net_mtu -= sizeof (struct security_header) +
  1633. UDPIP_HEADER_SIZE;
  1634. } else {
  1635. totem_config->net_mtu -= UDPIP_HEADER_SIZE;
  1636. }
  1637. }
  1638. const char *totemudp_iface_print (void *udp_context) {
  1639. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1640. const char *ret_char;
  1641. ret_char = totemip_print (&instance->my_id);
  1642. return (ret_char);
  1643. }
  1644. int totemudp_iface_get (
  1645. void *udp_context,
  1646. struct totem_ip_address *addr)
  1647. {
  1648. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1649. int res = 0;
  1650. memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
  1651. return (res);
  1652. }
  1653. int totemudp_token_target_set (
  1654. void *udp_context,
  1655. const struct totem_ip_address *token_target)
  1656. {
  1657. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1658. int res = 0;
  1659. memcpy (&instance->token_target, token_target,
  1660. sizeof (struct totem_ip_address));
  1661. instance->totemudp_target_set_completed (instance->context);
  1662. return (res);
  1663. }
  1664. extern int totemudp_recv_mcast_empty (
  1665. void *udp_context)
  1666. {
  1667. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1668. unsigned int res;
  1669. struct sockaddr_storage system_from;
  1670. struct msghdr msg_recv;
  1671. struct pollfd ufd;
  1672. int nfds;
  1673. int msg_processed = 0;
  1674. /*
  1675. * Receive datagram
  1676. */
  1677. msg_recv.msg_name = &system_from;
  1678. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  1679. msg_recv.msg_iov = &instance->totemudp_iov_recv_flush;
  1680. msg_recv.msg_iovlen = 1;
  1681. #if !defined(COROSYNC_SOLARIS)
  1682. msg_recv.msg_control = 0;
  1683. msg_recv.msg_controllen = 0;
  1684. msg_recv.msg_flags = 0;
  1685. #else
  1686. msg_recv.msg_accrights = NULL;
  1687. msg_recv.msg_accrightslen = 0;
  1688. #endif
  1689. do {
  1690. ufd.fd = instance->totemudp_sockets.mcast_recv;
  1691. ufd.events = POLLIN;
  1692. nfds = poll (&ufd, 1, 0);
  1693. if (nfds == 1 && ufd.revents & POLLIN) {
  1694. res = recvmsg (instance->totemudp_sockets.mcast_recv, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  1695. if (res != -1) {
  1696. msg_processed = 1;
  1697. } else {
  1698. msg_processed = -1;
  1699. }
  1700. }
  1701. } while (nfds == 1);
  1702. return (msg_processed);
  1703. }