totemnet.c 51 KB

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