zclient.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  1. /* Zebra's client library.
  2. * Copyright (C) 1999 Kunihiro Ishiguro
  3. * Copyright (C) 2005 Andrew J. Schorr
  4. *
  5. * This file is part of GNU Zebra.
  6. *
  7. * GNU Zebra is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published
  9. * by the Free Software Foundation; either version 2, or (at your
  10. * option) any later version.
  11. *
  12. * GNU Zebra is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Zebra; see the file COPYING. If not, write to the
  19. * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  20. * MA 02111-1307, USA.
  21. */
  22. #include <zebra.h>
  23. #include "prefix.h"
  24. #include "stream.h"
  25. #include "buffer.h"
  26. #include "network.h"
  27. #include "if.h"
  28. #include "log.h"
  29. #include "thread.h"
  30. #include "zclient.h"
  31. #include "memory.h"
  32. #include "table.h"
  33. /* Zebra client events. */
  34. enum event {ZCLIENT_SCHEDULE, ZCLIENT_READ, ZCLIENT_CONNECT};
  35. /* Prototype for event manager. */
  36. static void zclient_event (enum event, struct zclient *);
  37. const char *zclient_serv_path = NULL;
  38. /* This file local debug flag. */
  39. int zclient_debug = 0;
  40. /* Allocate zclient structure. */
  41. struct zclient *
  42. zclient_new (struct thread_master *master)
  43. {
  44. struct zclient *zclient;
  45. zclient = XCALLOC (MTYPE_ZCLIENT, sizeof (struct zclient));
  46. zclient->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
  47. zclient->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
  48. zclient->wb = buffer_new(0);
  49. zclient->master = master;
  50. return zclient;
  51. }
  52. /* This function is only called when exiting, because
  53. many parts of the code do not check for I/O errors, so they could
  54. reference an invalid pointer if the structure was ever freed.
  55. Free zclient structure. */
  56. void
  57. zclient_free (struct zclient *zclient)
  58. {
  59. if (zclient->ibuf)
  60. stream_free(zclient->ibuf);
  61. if (zclient->obuf)
  62. stream_free(zclient->obuf);
  63. if (zclient->wb)
  64. buffer_free(zclient->wb);
  65. XFREE (MTYPE_ZCLIENT, zclient);
  66. }
  67. /* Initialize zebra client. Argument redist_default is unwanted
  68. redistribute route type. */
  69. void
  70. zclient_init (struct zclient *zclient, int redist_default)
  71. {
  72. int i;
  73. /* Enable zebra client connection by default. */
  74. zclient->enable = 1;
  75. /* Set -1 to the default socket value. */
  76. zclient->sock = -1;
  77. /* Clear redistribution flags. */
  78. for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
  79. zclient->redist[i] = vrf_bitmap_init ();
  80. /* Set unwanted redistribute route. bgpd does not need BGP route
  81. redistribution. */
  82. zclient->redist_default = redist_default;
  83. /* Set default-information redistribute to zero. */
  84. zclient->default_information = vrf_bitmap_init ();
  85. /* Schedule first zclient connection. */
  86. if (zclient_debug)
  87. zlog_debug ("zclient start scheduled");
  88. zclient_event (ZCLIENT_SCHEDULE, zclient);
  89. }
  90. /* Stop zebra client services. */
  91. void
  92. zclient_stop (struct zclient *zclient)
  93. {
  94. int i;
  95. if (zclient_debug)
  96. zlog_debug ("zclient stopped");
  97. /* Stop threads. */
  98. THREAD_OFF(zclient->t_read);
  99. THREAD_OFF(zclient->t_connect);
  100. THREAD_OFF(zclient->t_write);
  101. /* Reset streams. */
  102. stream_reset(zclient->ibuf);
  103. stream_reset(zclient->obuf);
  104. /* Empty the write buffer. */
  105. buffer_reset(zclient->wb);
  106. /* Close socket. */
  107. if (zclient->sock >= 0)
  108. {
  109. close (zclient->sock);
  110. zclient->sock = -1;
  111. }
  112. zclient->fail = 0;
  113. for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
  114. {
  115. vrf_bitmap_free(zclient->redist[i]);
  116. zclient->redist[i] = VRF_BITMAP_NULL;
  117. }
  118. vrf_bitmap_free(zclient->default_information);
  119. zclient->default_information = VRF_BITMAP_NULL;
  120. }
  121. void
  122. zclient_reset (struct zclient *zclient)
  123. {
  124. zclient_stop (zclient);
  125. zclient_init (zclient, zclient->redist_default);
  126. }
  127. #ifdef HAVE_TCP_ZEBRA
  128. /* Make socket to zebra daemon. Return zebra socket. */
  129. static int
  130. zclient_socket(void)
  131. {
  132. int sock;
  133. int ret;
  134. struct sockaddr_in serv;
  135. /* We should think about IPv6 connection. */
  136. sock = socket (AF_INET, SOCK_STREAM, 0);
  137. if (sock < 0)
  138. return -1;
  139. /* Make server socket. */
  140. memset (&serv, 0, sizeof (struct sockaddr_in));
  141. serv.sin_family = AF_INET;
  142. serv.sin_port = htons (ZEBRA_PORT);
  143. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  144. serv.sin_len = sizeof (struct sockaddr_in);
  145. #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
  146. serv.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
  147. /* Connect to zebra. */
  148. ret = connect (sock, (struct sockaddr *) &serv, sizeof (serv));
  149. if (ret < 0)
  150. {
  151. zlog_warn ("%s connect failure: %d", __PRETTY_FUNCTION__, errno);
  152. close (sock);
  153. return -1;
  154. }
  155. return sock;
  156. }
  157. #else
  158. /* For sockaddr_un. */
  159. #include <sys/un.h>
  160. static int
  161. zclient_socket_un (const char *path)
  162. {
  163. int ret;
  164. int sock, len;
  165. struct sockaddr_un addr;
  166. sock = socket (AF_UNIX, SOCK_STREAM, 0);
  167. if (sock < 0)
  168. return -1;
  169. /* Make server socket. */
  170. memset (&addr, 0, sizeof (struct sockaddr_un));
  171. addr.sun_family = AF_UNIX;
  172. strncpy (addr.sun_path, path, strlen (path));
  173. #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
  174. len = addr.sun_len = SUN_LEN(&addr);
  175. #else
  176. len = sizeof (addr.sun_family) + strlen (addr.sun_path);
  177. #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
  178. ret = connect (sock, (struct sockaddr *) &addr, len);
  179. if (ret < 0)
  180. {
  181. zlog_warn ("%s connect failure: %d", __PRETTY_FUNCTION__, errno);
  182. close (sock);
  183. return -1;
  184. }
  185. return sock;
  186. }
  187. #endif /* HAVE_TCP_ZEBRA */
  188. /**
  189. * Connect to zebra daemon.
  190. * @param zclient a pointer to zclient structure
  191. * @return socket fd just to make sure that connection established
  192. * @see zclient_init
  193. * @see zclient_new
  194. */
  195. int
  196. zclient_socket_connect (struct zclient *zclient)
  197. {
  198. #ifdef HAVE_TCP_ZEBRA
  199. zclient->sock = zclient_socket ();
  200. #else
  201. zclient->sock = zclient_socket_un (zclient_serv_path_get());
  202. #endif
  203. return zclient->sock;
  204. }
  205. static int
  206. zclient_failed(struct zclient *zclient)
  207. {
  208. zclient->fail++;
  209. zclient_stop(zclient);
  210. zclient_event(ZCLIENT_CONNECT, zclient);
  211. return -1;
  212. }
  213. static int
  214. zclient_flush_data(struct thread *thread)
  215. {
  216. struct zclient *zclient = THREAD_ARG(thread);
  217. zclient->t_write = NULL;
  218. if (zclient->sock < 0)
  219. return -1;
  220. switch (buffer_flush_available(zclient->wb, zclient->sock))
  221. {
  222. case BUFFER_ERROR:
  223. zlog_warn("%s: buffer_flush_available failed on zclient fd %d, closing",
  224. __func__, zclient->sock);
  225. return zclient_failed(zclient);
  226. break;
  227. case BUFFER_PENDING:
  228. zclient->t_write = thread_add_write (zclient->master, zclient_flush_data,
  229. zclient, zclient->sock);
  230. break;
  231. case BUFFER_EMPTY:
  232. break;
  233. }
  234. return 0;
  235. }
  236. int
  237. zclient_send_message(struct zclient *zclient)
  238. {
  239. if (zclient->sock < 0)
  240. return -1;
  241. switch (buffer_write(zclient->wb, zclient->sock, STREAM_DATA(zclient->obuf),
  242. stream_get_endp(zclient->obuf)))
  243. {
  244. case BUFFER_ERROR:
  245. zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
  246. __func__, zclient->sock);
  247. return zclient_failed(zclient);
  248. break;
  249. case BUFFER_EMPTY:
  250. THREAD_OFF(zclient->t_write);
  251. break;
  252. case BUFFER_PENDING:
  253. THREAD_WRITE_ON (zclient->master, zclient->t_write,
  254. zclient_flush_data, zclient, zclient->sock);
  255. break;
  256. }
  257. return 0;
  258. }
  259. void
  260. zclient_create_header (struct stream *s, uint16_t command, vrf_id_t vrf_id)
  261. {
  262. /* length placeholder, caller can update */
  263. stream_putw (s, ZEBRA_HEADER_SIZE);
  264. stream_putc (s, ZEBRA_HEADER_MARKER);
  265. stream_putc (s, ZSERV_VERSION);
  266. stream_putw (s, vrf_id);
  267. stream_putw (s, command);
  268. }
  269. int
  270. zclient_read_header (struct stream *s, int sock, u_int16_t *size, u_char *marker,
  271. u_char *version, u_int16_t *vrf_id, u_int16_t *cmd)
  272. {
  273. if (stream_read (s, sock, ZEBRA_HEADER_SIZE) != ZEBRA_HEADER_SIZE)
  274. return -1;
  275. *size = stream_getw (s) - ZEBRA_HEADER_SIZE;
  276. *marker = stream_getc (s);
  277. *version = stream_getc (s);
  278. *vrf_id = stream_getw (s);
  279. *cmd = stream_getw (s);
  280. if (*version != ZSERV_VERSION || *marker != ZEBRA_HEADER_MARKER)
  281. {
  282. zlog_err("%s: socket %d version mismatch, marker %d, version %d",
  283. __func__, sock, *marker, *version);
  284. return -1;
  285. }
  286. if (*size && stream_read (s, sock, *size) != *size)
  287. return -1;
  288. return 0;
  289. }
  290. /* Send simple Zebra message. */
  291. static int
  292. zebra_message_send (struct zclient *zclient, int command, vrf_id_t vrf_id)
  293. {
  294. struct stream *s;
  295. /* Get zclient output buffer. */
  296. s = zclient->obuf;
  297. stream_reset (s);
  298. /* Send very simple command only Zebra message. */
  299. zclient_create_header (s, command, vrf_id);
  300. return zclient_send_message(zclient);
  301. }
  302. static int
  303. zebra_hello_send (struct zclient *zclient)
  304. {
  305. struct stream *s;
  306. if (zclient->redist_default)
  307. {
  308. s = zclient->obuf;
  309. stream_reset (s);
  310. /* The VRF ID in the HELLO message is always 0. */
  311. zclient_create_header (s, ZEBRA_HELLO, VRF_DEFAULT);
  312. stream_putc (s, zclient->redist_default);
  313. stream_putw_at (s, 0, stream_get_endp (s));
  314. return zclient_send_message(zclient);
  315. }
  316. return 0;
  317. }
  318. /* Send requests to zebra daemon for the information in a VRF. */
  319. void
  320. zclient_send_requests (struct zclient *zclient, vrf_id_t vrf_id)
  321. {
  322. int i;
  323. /* zclient is disabled. */
  324. if (! zclient->enable)
  325. return;
  326. /* If not connected to the zebra yet. */
  327. if (zclient->sock < 0)
  328. return;
  329. if (zclient_debug)
  330. zlog_debug ("%s: send messages for VRF %u", __func__, vrf_id);
  331. /* We need router-id information. */
  332. zebra_message_send (zclient, ZEBRA_ROUTER_ID_ADD, vrf_id);
  333. /* We need interface information. */
  334. zebra_message_send (zclient, ZEBRA_INTERFACE_ADD, vrf_id);
  335. /* Set unwanted redistribute route. */
  336. vrf_bitmap_set (zclient->redist[zclient->redist_default], vrf_id);
  337. /* Flush all redistribute request. */
  338. for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
  339. if (i != zclient->redist_default &&
  340. vrf_bitmap_check (zclient->redist[i], vrf_id))
  341. zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, i, vrf_id);
  342. /* If default information is needed. */
  343. if (vrf_bitmap_check (zclient->default_information, VRF_DEFAULT))
  344. zebra_message_send (zclient, ZEBRA_REDISTRIBUTE_DEFAULT_ADD, vrf_id);
  345. }
  346. /* Make connection to zebra daemon. */
  347. int
  348. zclient_start (struct zclient *zclient)
  349. {
  350. if (zclient_debug)
  351. zlog_debug ("zclient_start is called");
  352. /* zclient is disabled. */
  353. if (! zclient->enable)
  354. return 0;
  355. /* If already connected to the zebra. */
  356. if (zclient->sock >= 0)
  357. return 0;
  358. /* Check connect thread. */
  359. if (zclient->t_connect)
  360. return 0;
  361. /*
  362. * If we fail to connect to the socket on initialization,
  363. * Let's wait a second and see if we can reconnect.
  364. * Cause if we don't connect, we never attempt to
  365. * reconnect. On startup if zebra is slow we
  366. * can get into this situation.
  367. */
  368. while (zclient_socket_connect(zclient) < 0 && zclient->fail < 5)
  369. {
  370. if (zclient_debug)
  371. zlog_debug ("zclient connection fail");
  372. zclient->fail++;
  373. sleep (1);
  374. }
  375. if (zclient->sock < 0)
  376. {
  377. zclient_event (ZCLIENT_CONNECT, zclient);
  378. return -1;
  379. }
  380. if (set_nonblocking(zclient->sock) < 0)
  381. zlog_warn("%s: set_nonblocking(%d) failed", __func__, zclient->sock);
  382. /* Clear fail count. */
  383. zclient->fail = 0;
  384. if (zclient_debug)
  385. zlog_debug ("zclient connect success with socket [%d]", zclient->sock);
  386. /* Create read thread. */
  387. zclient_event (ZCLIENT_READ, zclient);
  388. zebra_hello_send (zclient);
  389. /* Inform the successful connection. */
  390. if (zclient->zebra_connected)
  391. (*zclient->zebra_connected) (zclient);
  392. return 0;
  393. }
  394. /* This function is a wrapper function for calling zclient_start from
  395. timer or event thread. */
  396. static int
  397. zclient_connect (struct thread *t)
  398. {
  399. struct zclient *zclient;
  400. zclient = THREAD_ARG (t);
  401. zclient->t_connect = NULL;
  402. if (zclient_debug)
  403. zlog_debug ("zclient_connect is called");
  404. return zclient_start (zclient);
  405. }
  406. /*
  407. * "xdr_encode"-like interface that allows daemon (client) to send
  408. * a message to zebra server for a route that needs to be
  409. * added/deleted to the kernel. Info about the route is specified
  410. * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
  411. * the info down the zclient socket using the stream_* functions.
  412. *
  413. * The corresponding read ("xdr_decode") function on the server
  414. * side is zread_ipv4_add()/zread_ipv4_delete().
  415. *
  416. * 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 A B C D E F
  417. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  418. * | Length (2) | Command | Route Type |
  419. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  420. * | ZEBRA Flags | Message Flags | Prefix length |
  421. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  422. * | Destination IPv4 Prefix for route |
  423. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  424. * | Nexthop count |
  425. * +-+-+-+-+-+-+-+-+
  426. *
  427. *
  428. * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
  429. * described, as per the Nexthop count. Each nexthop described as:
  430. *
  431. * +-+-+-+-+-+-+-+-+
  432. * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
  433. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  434. * | IPv4 Nexthop address or Interface Index number |
  435. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  436. *
  437. * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or
  438. * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_
  439. * nexthop information is provided, and the message describes a prefix
  440. * to blackhole or reject route.
  441. *
  442. * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
  443. * byte value.
  444. *
  445. * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
  446. * byte value.
  447. *
  448. * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
  449. *
  450. * XXX: No attention paid to alignment.
  451. */
  452. int
  453. zapi_ipv4_route (u_char cmd, struct zclient *zclient, struct prefix_ipv4 *p,
  454. struct zapi_ipv4 *api)
  455. {
  456. int i;
  457. int psize;
  458. struct stream *s;
  459. /* Reset stream. */
  460. s = zclient->obuf;
  461. stream_reset (s);
  462. zclient_create_header (s, cmd, api->vrf_id);
  463. /* Put type and nexthop. */
  464. stream_putc (s, api->type);
  465. stream_putc (s, api->flags);
  466. stream_putc (s, api->message);
  467. stream_putw (s, api->safi);
  468. /* Put prefix information. */
  469. psize = PSIZE (p->prefixlen);
  470. stream_putc (s, p->prefixlen);
  471. stream_write (s, (u_char *) & p->prefix, psize);
  472. /* Nexthop, ifindex, distance and metric information. */
  473. if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
  474. {
  475. if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE))
  476. {
  477. stream_putc (s, 1);
  478. stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE);
  479. /* XXX assert(api->nexthop_num == 0); */
  480. /* XXX assert(api->ifindex_num == 0); */
  481. }
  482. else
  483. stream_putc (s, api->nexthop_num + api->ifindex_num);
  484. for (i = 0; i < api->nexthop_num; i++)
  485. {
  486. stream_putc (s, ZEBRA_NEXTHOP_IPV4);
  487. stream_put_in_addr (s, api->nexthop[i]);
  488. }
  489. for (i = 0; i < api->ifindex_num; i++)
  490. {
  491. stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
  492. stream_putl (s, api->ifindex[i]);
  493. }
  494. }
  495. if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
  496. stream_putc (s, api->distance);
  497. if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
  498. stream_putl (s, api->metric);
  499. if (CHECK_FLAG (api->message, ZAPI_MESSAGE_MTU))
  500. stream_putl (s, api->mtu);
  501. if (CHECK_FLAG (api->message, ZAPI_MESSAGE_TAG))
  502. stream_putl (s, api->tag);
  503. /* Put length at the first point of the stream. */
  504. stream_putw_at (s, 0, stream_get_endp (s));
  505. return zclient_send_message(zclient);
  506. }
  507. #ifdef HAVE_IPV6
  508. int
  509. zapi_ipv6_route (u_char cmd, struct zclient *zclient, struct prefix_ipv6 *p,
  510. struct zapi_ipv6 *api)
  511. {
  512. int i;
  513. int psize;
  514. struct stream *s;
  515. /* Reset stream. */
  516. s = zclient->obuf;
  517. stream_reset (s);
  518. zclient_create_header (s, cmd, api->vrf_id);
  519. /* Put type and nexthop. */
  520. stream_putc (s, api->type);
  521. stream_putc (s, api->flags);
  522. stream_putc (s, api->message);
  523. stream_putw (s, api->safi);
  524. /* Put prefix information. */
  525. psize = PSIZE (p->prefixlen);
  526. stream_putc (s, p->prefixlen);
  527. stream_write (s, (u_char *)&p->prefix, psize);
  528. /* Nexthop, ifindex, distance and metric information. */
  529. if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
  530. {
  531. stream_putc (s, api->nexthop_num + api->ifindex_num);
  532. for (i = 0; i < api->nexthop_num; i++)
  533. {
  534. stream_putc (s, ZEBRA_NEXTHOP_IPV6);
  535. stream_write (s, (u_char *)api->nexthop[i], 16);
  536. }
  537. for (i = 0; i < api->ifindex_num; i++)
  538. {
  539. stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
  540. stream_putl (s, api->ifindex[i]);
  541. }
  542. }
  543. if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
  544. stream_putc (s, api->distance);
  545. if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
  546. stream_putl (s, api->metric);
  547. if (CHECK_FLAG (api->message, ZAPI_MESSAGE_MTU))
  548. stream_putl (s, api->mtu);
  549. if (CHECK_FLAG (api->message, ZAPI_MESSAGE_TAG))
  550. stream_putl (s, api->tag);
  551. /* Put length at the first point of the stream. */
  552. stream_putw_at (s, 0, stream_get_endp (s));
  553. return zclient_send_message(zclient);
  554. }
  555. #endif /* HAVE_IPV6 */
  556. /*
  557. * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
  558. * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
  559. * then set/unset redist[type] in the client handle (a struct zserv) for the
  560. * sending client
  561. */
  562. int
  563. zebra_redistribute_send (int command, struct zclient *zclient, int type,
  564. vrf_id_t vrf_id)
  565. {
  566. struct stream *s;
  567. s = zclient->obuf;
  568. stream_reset(s);
  569. zclient_create_header (s, command, vrf_id);
  570. stream_putc (s, type);
  571. stream_putw_at (s, 0, stream_get_endp (s));
  572. return zclient_send_message(zclient);
  573. }
  574. /* Get prefix in ZServ format; family should be filled in on prefix */
  575. static void
  576. zclient_stream_get_prefix (struct stream *s, struct prefix *p)
  577. {
  578. size_t plen = prefix_blen (p);
  579. u_char c;
  580. p->prefixlen = 0;
  581. if (plen == 0)
  582. return;
  583. stream_get (&p->u.prefix, s, plen);
  584. c = stream_getc(s);
  585. p->prefixlen = MIN(plen * 8, c);
  586. }
  587. /* Router-id update from zebra daemon. */
  588. void
  589. zebra_router_id_update_read (struct stream *s, struct prefix *rid)
  590. {
  591. /* Fetch interface address. */
  592. rid->family = stream_getc (s);
  593. zclient_stream_get_prefix (s, rid);
  594. }
  595. /* Interface addition from zebra daemon. */
  596. /*
  597. * The format of the message sent with type ZEBRA_INTERFACE_ADD or
  598. * ZEBRA_INTERFACE_DELETE from zebra to the client is:
  599. * 0 1 2 3
  600. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  601. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  602. * | ifname |
  603. * | |
  604. * | |
  605. * | |
  606. * | |
  607. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  608. * | ifindex |
  609. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  610. * | status |
  611. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  612. * | if_flags |
  613. * | |
  614. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  615. * | metric |
  616. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  617. * | ifmtu |
  618. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  619. * | ifmtu6 |
  620. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  621. * | bandwidth |
  622. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  623. * | Link Layer Type |
  624. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  625. * | Harware Address Length |
  626. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  627. * | Hardware Address if HW lenght different from 0 |
  628. * | ... max INTERFACE_HWADDR_MAX |
  629. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  630. * | Link_params? | Whether a link-params follows: 1 or 0.
  631. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  632. * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
  633. * | .... (struct if_link_params). |
  634. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  635. */
  636. struct interface *
  637. zebra_interface_add_read (struct stream *s, vrf_id_t vrf_id)
  638. {
  639. struct interface *ifp;
  640. char ifname_tmp[INTERFACE_NAMSIZ];
  641. /* Read interface name. */
  642. stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
  643. /* Lookup/create interface by name. */
  644. ifp = if_get_by_name_len_vrf (ifname_tmp,
  645. strnlen (ifname_tmp, INTERFACE_NAMSIZ),
  646. vrf_id);
  647. zebra_interface_if_set_value (s, ifp);
  648. return ifp;
  649. }
  650. /*
  651. * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
  652. * from zebra server. The format of this message is the same as
  653. * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
  654. * comments for zebra_interface_add_read), except that no sockaddr_dl
  655. * is sent at the tail of the message.
  656. */
  657. struct interface *
  658. zebra_interface_state_read (struct stream *s, vrf_id_t vrf_id)
  659. {
  660. struct interface *ifp;
  661. char ifname_tmp[INTERFACE_NAMSIZ];
  662. /* Read interface name. */
  663. stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
  664. /* Lookup this by interface index. */
  665. ifp = if_lookup_by_name_len_vrf (ifname_tmp,
  666. strnlen (ifname_tmp, INTERFACE_NAMSIZ),
  667. vrf_id);
  668. /* If such interface does not exist, indicate an error */
  669. if (! ifp)
  670. return NULL;
  671. zebra_interface_if_set_value (s, ifp);
  672. return ifp;
  673. }
  674. static void
  675. link_params_set_value(struct stream *s, struct if_link_params *iflp)
  676. {
  677. if (iflp == NULL)
  678. return;
  679. iflp->lp_status = stream_getl (s);
  680. iflp->te_metric = stream_getl (s);
  681. iflp->max_bw = stream_getf (s);
  682. iflp->max_rsv_bw = stream_getf (s);
  683. uint32_t bwclassnum = stream_getl (s);
  684. {
  685. unsigned int i;
  686. for (i = 0; i < bwclassnum && i < MAX_CLASS_TYPE; i++)
  687. iflp->unrsv_bw[i] = stream_getf (s);
  688. if (i < bwclassnum)
  689. zlog_err ("%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
  690. " - outdated library?",
  691. __func__, bwclassnum, MAX_CLASS_TYPE);
  692. }
  693. iflp->admin_grp = stream_getl (s);
  694. iflp->rmt_as = stream_getl (s);
  695. iflp->rmt_ip.s_addr = stream_get_ipv4 (s);
  696. iflp->av_delay = stream_getl (s);
  697. iflp->min_delay = stream_getl (s);
  698. iflp->max_delay = stream_getl (s);
  699. iflp->delay_var = stream_getl (s);
  700. iflp->pkt_loss = stream_getf (s);
  701. iflp->res_bw = stream_getf (s);
  702. iflp->ava_bw = stream_getf (s);
  703. iflp->use_bw = stream_getf (s);
  704. }
  705. struct interface *
  706. zebra_interface_link_params_read (struct stream *s)
  707. {
  708. struct if_link_params *iflp;
  709. uint32_t ifindex = stream_getl (s);
  710. struct interface *ifp = if_lookup_by_index (ifindex);
  711. if (ifp == NULL || s == NULL)
  712. {
  713. zlog_err ("%s: unknown ifindex %u, shouldn't happen",
  714. __func__, ifindex);
  715. return NULL;
  716. }
  717. if ((iflp = if_link_params_get (ifp)) == NULL)
  718. return NULL;
  719. link_params_set_value(s, iflp);
  720. return ifp;
  721. }
  722. void
  723. zebra_interface_if_set_value (struct stream *s, struct interface *ifp)
  724. {
  725. u_char link_params_status = 0;
  726. /* Read interface's index. */
  727. ifp->ifindex = stream_getl (s);
  728. ifp->status = stream_getc (s);
  729. /* Read interface's value. */
  730. ifp->flags = stream_getq (s);
  731. ifp->metric = stream_getl (s);
  732. ifp->mtu = stream_getl (s);
  733. ifp->mtu6 = stream_getl (s);
  734. ifp->bandwidth = stream_getl (s);
  735. ifp->ll_type = stream_getl (s);
  736. ifp->hw_addr_len = stream_getl (s);
  737. if (ifp->hw_addr_len)
  738. stream_get (ifp->hw_addr, s, MIN(ifp->hw_addr_len, INTERFACE_HWADDR_MAX));
  739. /* Read Traffic Engineering status */
  740. link_params_status = stream_getc (s);
  741. /* Then, Traffic Engineering parameters if any */
  742. if (link_params_status)
  743. {
  744. struct if_link_params *iflp = if_link_params_get (ifp);
  745. link_params_set_value(s, iflp);
  746. }
  747. }
  748. size_t
  749. zebra_interface_link_params_write (struct stream *s, struct interface *ifp)
  750. {
  751. size_t w;
  752. struct if_link_params *iflp;
  753. int i;
  754. if (s == NULL || ifp == NULL || ifp->link_params == NULL)
  755. return 0;
  756. iflp = ifp->link_params;
  757. w = 0;
  758. w += stream_putl (s, iflp->lp_status);
  759. w += stream_putl (s, iflp->te_metric);
  760. w += stream_putf (s, iflp->max_bw);
  761. w += stream_putf (s, iflp->max_rsv_bw);
  762. w += stream_putl (s, MAX_CLASS_TYPE);
  763. for (i = 0; i < MAX_CLASS_TYPE; i++)
  764. w += stream_putf (s, iflp->unrsv_bw[i]);
  765. w += stream_putl (s, iflp->admin_grp);
  766. w += stream_putl (s, iflp->rmt_as);
  767. w += stream_put_in_addr (s, &iflp->rmt_ip);
  768. w += stream_putl (s, iflp->av_delay);
  769. w += stream_putl (s, iflp->min_delay);
  770. w += stream_putl (s, iflp->max_delay);
  771. w += stream_putl (s, iflp->delay_var);
  772. w += stream_putf (s, iflp->pkt_loss);
  773. w += stream_putf (s, iflp->res_bw);
  774. w += stream_putf (s, iflp->ava_bw);
  775. w += stream_putf (s, iflp->use_bw);
  776. return w;
  777. }
  778. /*
  779. * format of message for address additon is:
  780. * 0
  781. * 0 1 2 3 4 5 6 7
  782. * +-+-+-+-+-+-+-+-+
  783. * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
  784. * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
  785. * | |
  786. * + +
  787. * | ifindex |
  788. * + +
  789. * | |
  790. * + +
  791. * | |
  792. * +-+-+-+-+-+-+-+-+
  793. * | ifc_flags | flags for connected address
  794. * +-+-+-+-+-+-+-+-+
  795. * | addr_family |
  796. * +-+-+-+-+-+-+-+-+
  797. * | addr... |
  798. * : :
  799. * | |
  800. * +-+-+-+-+-+-+-+-+
  801. * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
  802. * +-+-+-+-+-+-+-+-+
  803. * | daddr.. |
  804. * : :
  805. * | |
  806. * +-+-+-+-+-+-+-+-+
  807. */
  808. static int
  809. memconstant(const void *s, int c, size_t n)
  810. {
  811. const u_char *p = s;
  812. while (n-- > 0)
  813. if (*p++ != c)
  814. return 0;
  815. return 1;
  816. }
  817. struct connected *
  818. zebra_interface_address_read (int type, struct stream *s, vrf_id_t vrf_id)
  819. {
  820. ifindex_t ifindex;
  821. struct interface *ifp;
  822. struct connected *ifc;
  823. struct prefix p, d, *dp;
  824. int plen;
  825. u_char ifc_flags;
  826. memset (&p, 0, sizeof(p));
  827. memset (&d, 0, sizeof(d));
  828. /* Get interface index. */
  829. ifindex = stream_getl (s);
  830. /* Lookup index. */
  831. ifp = if_lookup_by_index_vrf (ifindex, vrf_id);
  832. if (ifp == NULL)
  833. {
  834. zlog_warn ("zebra_interface_address_read(%s): "
  835. "Can't find interface by ifindex: %d ",
  836. (type == ZEBRA_INTERFACE_ADDRESS_ADD? "ADD" : "DELETE"),
  837. ifindex);
  838. return NULL;
  839. }
  840. /* Fetch flag. */
  841. ifc_flags = stream_getc (s);
  842. /* Fetch interface address. */
  843. d.family = p.family = stream_getc (s);
  844. plen = prefix_blen (&d);
  845. zclient_stream_get_prefix (s, &p);
  846. /* Fetch destination address. */
  847. stream_get (&d.u.prefix, s, plen);
  848. /* N.B. NULL destination pointers are encoded as all zeroes */
  849. dp = memconstant(&d.u.prefix,0,plen) ? NULL : &d;
  850. if (type == ZEBRA_INTERFACE_ADDRESS_ADD)
  851. {
  852. /* N.B. NULL destination pointers are encoded as all zeroes */
  853. ifc = connected_add_by_prefix(ifp, &p, dp);
  854. if (ifc != NULL)
  855. {
  856. ifc->flags = ifc_flags;
  857. if (ifc->destination)
  858. ifc->destination->prefixlen = ifc->address->prefixlen;
  859. else if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER))
  860. {
  861. /* carp interfaces on OpenBSD with 0.0.0.0/0 as "peer" */
  862. char buf[PREFIX_STRLEN];
  863. zlog_warn("warning: interface %s address %s "
  864. "with peer flag set, but no peer address!",
  865. ifp->name,
  866. prefix2str (ifc->address, buf, sizeof buf));
  867. UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
  868. }
  869. }
  870. }
  871. else
  872. {
  873. assert (type == ZEBRA_INTERFACE_ADDRESS_DELETE);
  874. ifc = connected_delete_by_prefix(ifp, &p);
  875. }
  876. return ifc;
  877. }
  878. /* Zebra client message read function. */
  879. static int
  880. zclient_read (struct thread *thread)
  881. {
  882. size_t already;
  883. uint16_t length, command;
  884. uint8_t marker, version;
  885. vrf_id_t vrf_id;
  886. struct zclient *zclient;
  887. /* Get socket to zebra. */
  888. zclient = THREAD_ARG (thread);
  889. zclient->t_read = NULL;
  890. /* Read zebra header (if we don't have it already). */
  891. if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE)
  892. {
  893. ssize_t nbyte;
  894. if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
  895. ZEBRA_HEADER_SIZE-already)) == 0) ||
  896. (nbyte == -1))
  897. {
  898. if (zclient_debug)
  899. zlog_debug ("zclient connection closed socket [%d].", zclient->sock);
  900. return zclient_failed(zclient);
  901. }
  902. if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
  903. {
  904. /* Try again later. */
  905. zclient_event (ZCLIENT_READ, zclient);
  906. return 0;
  907. }
  908. already = ZEBRA_HEADER_SIZE;
  909. }
  910. /* Reset to read from the beginning of the incoming packet. */
  911. stream_set_getp(zclient->ibuf, 0);
  912. /* Fetch header values. */
  913. length = stream_getw (zclient->ibuf);
  914. marker = stream_getc (zclient->ibuf);
  915. version = stream_getc (zclient->ibuf);
  916. vrf_id = stream_getw (zclient->ibuf);
  917. command = stream_getw (zclient->ibuf);
  918. if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
  919. {
  920. zlog_err("%s: socket %d version mismatch, marker %d, version %d",
  921. __func__, zclient->sock, marker, version);
  922. return zclient_failed(zclient);
  923. }
  924. if (length < ZEBRA_HEADER_SIZE)
  925. {
  926. zlog_err("%s: socket %d message length %u is less than %d ",
  927. __func__, zclient->sock, length, ZEBRA_HEADER_SIZE);
  928. return zclient_failed(zclient);
  929. }
  930. /* Length check. */
  931. if (length > STREAM_SIZE(zclient->ibuf))
  932. {
  933. struct stream *ns;
  934. zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...",
  935. __func__, length, (u_long)STREAM_SIZE(zclient->ibuf));
  936. ns = stream_new(length);
  937. stream_copy(ns, zclient->ibuf);
  938. stream_free (zclient->ibuf);
  939. zclient->ibuf = ns;
  940. }
  941. /* Read rest of zebra packet. */
  942. if (already < length)
  943. {
  944. ssize_t nbyte;
  945. if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
  946. length-already)) == 0) ||
  947. (nbyte == -1))
  948. {
  949. if (zclient_debug)
  950. zlog_debug("zclient connection closed socket [%d].", zclient->sock);
  951. return zclient_failed(zclient);
  952. }
  953. if (nbyte != (ssize_t)(length-already))
  954. {
  955. /* Try again later. */
  956. zclient_event (ZCLIENT_READ, zclient);
  957. return 0;
  958. }
  959. }
  960. length -= ZEBRA_HEADER_SIZE;
  961. if (zclient_debug)
  962. zlog_debug("zclient 0x%p command 0x%x VRF %u\n", (void *)zclient, command, vrf_id);
  963. switch (command)
  964. {
  965. case ZEBRA_ROUTER_ID_UPDATE:
  966. if (zclient->router_id_update)
  967. (*zclient->router_id_update) (command, zclient, length, vrf_id);
  968. break;
  969. case ZEBRA_INTERFACE_ADD:
  970. if (zclient->interface_add)
  971. (*zclient->interface_add) (command, zclient, length, vrf_id);
  972. break;
  973. case ZEBRA_INTERFACE_DELETE:
  974. if (zclient->interface_delete)
  975. (*zclient->interface_delete) (command, zclient, length, vrf_id);
  976. break;
  977. case ZEBRA_INTERFACE_ADDRESS_ADD:
  978. if (zclient->interface_address_add)
  979. (*zclient->interface_address_add) (command, zclient, length, vrf_id);
  980. break;
  981. case ZEBRA_INTERFACE_ADDRESS_DELETE:
  982. if (zclient->interface_address_delete)
  983. (*zclient->interface_address_delete) (command, zclient, length, vrf_id);
  984. break;
  985. case ZEBRA_INTERFACE_UP:
  986. if (zclient->interface_up)
  987. (*zclient->interface_up) (command, zclient, length, vrf_id);
  988. break;
  989. case ZEBRA_INTERFACE_DOWN:
  990. if (zclient->interface_down)
  991. (*zclient->interface_down) (command, zclient, length, vrf_id);
  992. break;
  993. case ZEBRA_IPV4_ROUTE_ADD:
  994. if (zclient->ipv4_route_add)
  995. (*zclient->ipv4_route_add) (command, zclient, length, vrf_id);
  996. break;
  997. case ZEBRA_IPV4_ROUTE_DELETE:
  998. if (zclient->ipv4_route_delete)
  999. (*zclient->ipv4_route_delete) (command, zclient, length, vrf_id);
  1000. break;
  1001. case ZEBRA_IPV6_ROUTE_ADD:
  1002. if (zclient->ipv6_route_add)
  1003. (*zclient->ipv6_route_add) (command, zclient, length, vrf_id);
  1004. break;
  1005. case ZEBRA_IPV6_ROUTE_DELETE:
  1006. if (zclient->ipv6_route_delete)
  1007. (*zclient->ipv6_route_delete) (command, zclient, length, vrf_id);
  1008. break;
  1009. case ZEBRA_INTERFACE_LINK_PARAMS:
  1010. if (zclient->interface_link_params)
  1011. (*zclient->interface_link_params) (command, zclient, length);
  1012. case ZEBRA_NEXTHOP_UPDATE:
  1013. if (zclient->nexthop_update)
  1014. (*zclient->nexthop_update) (command, zclient, length, vrf_id);
  1015. break;
  1016. default:
  1017. break;
  1018. }
  1019. if (zclient->sock < 0)
  1020. /* Connection was closed during packet processing. */
  1021. return -1;
  1022. /* Register read thread. */
  1023. stream_reset(zclient->ibuf);
  1024. zclient_event (ZCLIENT_READ, zclient);
  1025. return 0;
  1026. }
  1027. void
  1028. zclient_redistribute (int command, struct zclient *zclient, int type,
  1029. vrf_id_t vrf_id)
  1030. {
  1031. if (command == ZEBRA_REDISTRIBUTE_ADD)
  1032. {
  1033. if (vrf_bitmap_check (zclient->redist[type], vrf_id))
  1034. return;
  1035. vrf_bitmap_set (zclient->redist[type], vrf_id);
  1036. }
  1037. else
  1038. {
  1039. if (!vrf_bitmap_check (zclient->redist[type], vrf_id))
  1040. return;
  1041. vrf_bitmap_unset (zclient->redist[type], vrf_id);
  1042. }
  1043. if (zclient->sock > 0)
  1044. zebra_redistribute_send (command, zclient, type, vrf_id);
  1045. }
  1046. void
  1047. zclient_redistribute_default (int command, struct zclient *zclient,
  1048. vrf_id_t vrf_id)
  1049. {
  1050. if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD)
  1051. {
  1052. if (vrf_bitmap_check (zclient->default_information, vrf_id))
  1053. return;
  1054. vrf_bitmap_set (zclient->default_information, vrf_id);
  1055. }
  1056. else
  1057. {
  1058. if (!vrf_bitmap_check (zclient->default_information, vrf_id))
  1059. return;
  1060. vrf_bitmap_unset (zclient->default_information, vrf_id);
  1061. }
  1062. if (zclient->sock > 0)
  1063. zebra_message_send (zclient, command, vrf_id);
  1064. }
  1065. static void
  1066. zclient_event (enum event event, struct zclient *zclient)
  1067. {
  1068. switch (event)
  1069. {
  1070. case ZCLIENT_SCHEDULE:
  1071. if (! zclient->t_connect)
  1072. zclient->t_connect =
  1073. thread_add_event (zclient->master, zclient_connect, zclient, 0);
  1074. break;
  1075. case ZCLIENT_CONNECT:
  1076. if (zclient->fail >= 10)
  1077. return;
  1078. if (zclient_debug)
  1079. zlog_debug ("zclient connect schedule interval is %d",
  1080. zclient->fail < 3 ? 10 : 60);
  1081. if (! zclient->t_connect)
  1082. zclient->t_connect =
  1083. thread_add_timer (zclient->master, zclient_connect, zclient,
  1084. zclient->fail < 3 ? 10 : 60);
  1085. break;
  1086. case ZCLIENT_READ:
  1087. zclient->t_read =
  1088. thread_add_read (zclient->master, zclient_read, zclient, zclient->sock);
  1089. break;
  1090. }
  1091. }
  1092. const char *zclient_serv_path_get()
  1093. {
  1094. return zclient_serv_path ? zclient_serv_path : ZEBRA_SERV_PATH;
  1095. }
  1096. void
  1097. zclient_serv_path_set (char *path)
  1098. {
  1099. struct stat sb;
  1100. /* reset */
  1101. zclient_serv_path = NULL;
  1102. /* test if `path' is socket. don't set it otherwise. */
  1103. if (stat(path, &sb) == -1)
  1104. {
  1105. zlog_warn ("%s: zebra socket `%s' does not exist", __func__, path);
  1106. return;
  1107. }
  1108. if ((sb.st_mode & S_IFMT) != S_IFSOCK)
  1109. {
  1110. zlog_warn ("%s: `%s' is not unix socket, sir", __func__, path);
  1111. return;
  1112. }
  1113. /* it seems that path is unix socket */
  1114. zclient_serv_path = path;
  1115. }