zclient.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  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. * XXX: No attention paid to alignment.
  449. */
  450. int
  451. zapi_ipv4_route (u_char cmd, struct zclient *zclient, struct prefix_ipv4 *p,
  452. struct zapi_ipv4 *api)
  453. {
  454. int i;
  455. int psize;
  456. struct stream *s;
  457. /* Reset stream. */
  458. s = zclient->obuf;
  459. stream_reset (s);
  460. zclient_create_header (s, cmd, api->vrf_id);
  461. /* Put type and nexthop. */
  462. stream_putc (s, api->type);
  463. stream_putc (s, api->flags);
  464. stream_putc (s, api->message);
  465. stream_putw (s, api->safi);
  466. /* Put prefix information. */
  467. psize = PSIZE (p->prefixlen);
  468. stream_putc (s, p->prefixlen);
  469. stream_write (s, (u_char *) & p->prefix, psize);
  470. /* Nexthop, ifindex, distance and metric information. */
  471. if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
  472. {
  473. if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE))
  474. {
  475. stream_putc (s, 1);
  476. stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE);
  477. /* XXX assert(api->nexthop_num == 0); */
  478. /* XXX assert(api->ifindex_num == 0); */
  479. }
  480. else
  481. stream_putc (s, api->nexthop_num + api->ifindex_num);
  482. for (i = 0; i < api->nexthop_num; i++)
  483. {
  484. stream_putc (s, ZEBRA_NEXTHOP_IPV4);
  485. stream_put_in_addr (s, api->nexthop[i]);
  486. }
  487. for (i = 0; i < api->ifindex_num; i++)
  488. {
  489. stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
  490. stream_putl (s, api->ifindex[i]);
  491. }
  492. }
  493. if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
  494. stream_putc (s, api->distance);
  495. if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
  496. stream_putl (s, api->metric);
  497. if (CHECK_FLAG (api->message, ZAPI_MESSAGE_MTU))
  498. stream_putl (s, api->mtu);
  499. /* Put length at the first point of the stream. */
  500. stream_putw_at (s, 0, stream_get_endp (s));
  501. return zclient_send_message(zclient);
  502. }
  503. #ifdef HAVE_IPV6
  504. int
  505. zapi_ipv6_route (u_char cmd, struct zclient *zclient, struct prefix_ipv6 *p,
  506. struct zapi_ipv6 *api)
  507. {
  508. int i;
  509. int psize;
  510. struct stream *s;
  511. /* Reset stream. */
  512. s = zclient->obuf;
  513. stream_reset (s);
  514. zclient_create_header (s, cmd, api->vrf_id);
  515. /* Put type and nexthop. */
  516. stream_putc (s, api->type);
  517. stream_putc (s, api->flags);
  518. stream_putc (s, api->message);
  519. stream_putw (s, api->safi);
  520. /* Put prefix information. */
  521. psize = PSIZE (p->prefixlen);
  522. stream_putc (s, p->prefixlen);
  523. stream_write (s, (u_char *)&p->prefix, psize);
  524. /* Nexthop, ifindex, distance and metric information. */
  525. if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
  526. {
  527. stream_putc (s, api->nexthop_num + api->ifindex_num);
  528. for (i = 0; i < api->nexthop_num; i++)
  529. {
  530. stream_putc (s, ZEBRA_NEXTHOP_IPV6);
  531. stream_write (s, (u_char *)api->nexthop[i], 16);
  532. }
  533. for (i = 0; i < api->ifindex_num; i++)
  534. {
  535. stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
  536. stream_putl (s, api->ifindex[i]);
  537. }
  538. }
  539. if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
  540. stream_putc (s, api->distance);
  541. if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
  542. stream_putl (s, api->metric);
  543. if (CHECK_FLAG (api->message, ZAPI_MESSAGE_MTU))
  544. stream_putl (s, api->mtu);
  545. /* Put length at the first point of the stream. */
  546. stream_putw_at (s, 0, stream_get_endp (s));
  547. return zclient_send_message(zclient);
  548. }
  549. #endif /* HAVE_IPV6 */
  550. /*
  551. * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
  552. * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
  553. * then set/unset redist[type] in the client handle (a struct zserv) for the
  554. * sending client
  555. */
  556. int
  557. zebra_redistribute_send (int command, struct zclient *zclient, int type,
  558. vrf_id_t vrf_id)
  559. {
  560. struct stream *s;
  561. s = zclient->obuf;
  562. stream_reset(s);
  563. zclient_create_header (s, command, vrf_id);
  564. stream_putc (s, type);
  565. stream_putw_at (s, 0, stream_get_endp (s));
  566. return zclient_send_message(zclient);
  567. }
  568. /* Get prefix in ZServ format; family should be filled in on prefix */
  569. static void
  570. zclient_stream_get_prefix (struct stream *s, struct prefix *p)
  571. {
  572. size_t plen = prefix_blen (p);
  573. u_char c;
  574. p->prefixlen = 0;
  575. if (plen == 0)
  576. return;
  577. stream_get (&p->u.prefix, s, plen);
  578. c = stream_getc(s);
  579. p->prefixlen = MIN(plen * 8, c);
  580. }
  581. /* Router-id update from zebra daemon. */
  582. void
  583. zebra_router_id_update_read (struct stream *s, struct prefix *rid)
  584. {
  585. /* Fetch interface address. */
  586. rid->family = stream_getc (s);
  587. zclient_stream_get_prefix (s, rid);
  588. }
  589. /* Interface addition from zebra daemon. */
  590. /*
  591. * The format of the message sent with type ZEBRA_INTERFACE_ADD or
  592. * ZEBRA_INTERFACE_DELETE from zebra to the client is:
  593. * 0 1 2 3
  594. * 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
  595. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  596. * | ifname |
  597. * | |
  598. * | |
  599. * | |
  600. * | |
  601. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  602. * | ifindex |
  603. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  604. * | status |
  605. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  606. * | if_flags |
  607. * | |
  608. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  609. * | metric |
  610. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  611. * | ifmtu |
  612. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  613. * | ifmtu6 |
  614. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  615. * | bandwidth |
  616. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  617. * | Link Layer Type |
  618. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  619. * | Harware Address Length |
  620. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  621. * | Hardware Address if HW lenght different from 0 |
  622. * | ... max INTERFACE_HWADDR_MAX |
  623. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  624. * | Link_params? | Whether a link-params follows: 1 or 0.
  625. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  626. * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
  627. * | .... (struct if_link_params). |
  628. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  629. */
  630. struct interface *
  631. zebra_interface_add_read (struct stream *s, vrf_id_t vrf_id)
  632. {
  633. struct interface *ifp;
  634. char ifname_tmp[INTERFACE_NAMSIZ];
  635. /* Read interface name. */
  636. stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
  637. /* Lookup/create interface by name. */
  638. ifp = if_get_by_name_len_vrf (ifname_tmp,
  639. strnlen (ifname_tmp, INTERFACE_NAMSIZ),
  640. vrf_id);
  641. zebra_interface_if_set_value (s, ifp);
  642. return ifp;
  643. }
  644. /*
  645. * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
  646. * from zebra server. The format of this message is the same as
  647. * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
  648. * comments for zebra_interface_add_read), except that no sockaddr_dl
  649. * is sent at the tail of the message.
  650. */
  651. struct interface *
  652. zebra_interface_state_read (struct stream *s, vrf_id_t vrf_id)
  653. {
  654. struct interface *ifp;
  655. char ifname_tmp[INTERFACE_NAMSIZ];
  656. /* Read interface name. */
  657. stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
  658. /* Lookup this by interface index. */
  659. ifp = if_lookup_by_name_len_vrf (ifname_tmp,
  660. strnlen (ifname_tmp, INTERFACE_NAMSIZ),
  661. vrf_id);
  662. /* If such interface does not exist, indicate an error */
  663. if (! ifp)
  664. return NULL;
  665. zebra_interface_if_set_value (s, ifp);
  666. return ifp;
  667. }
  668. static void
  669. link_params_set_value(struct stream *s, struct if_link_params *iflp)
  670. {
  671. if (iflp == NULL)
  672. return;
  673. iflp->lp_status = stream_getl (s);
  674. iflp->te_metric = stream_getl (s);
  675. iflp->max_bw = stream_getf (s);
  676. iflp->max_rsv_bw = stream_getf (s);
  677. uint32_t bwclassnum = stream_getl (s);
  678. {
  679. unsigned int i;
  680. for (i = 0; i < bwclassnum && i < MAX_CLASS_TYPE; i++)
  681. iflp->unrsv_bw[i] = stream_getf (s);
  682. if (i < bwclassnum)
  683. zlog_err ("%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
  684. " - outdated library?",
  685. __func__, bwclassnum, MAX_CLASS_TYPE);
  686. }
  687. iflp->admin_grp = stream_getl (s);
  688. iflp->rmt_as = stream_getl (s);
  689. iflp->rmt_ip.s_addr = stream_get_ipv4 (s);
  690. iflp->av_delay = stream_getl (s);
  691. iflp->min_delay = stream_getl (s);
  692. iflp->max_delay = stream_getl (s);
  693. iflp->delay_var = stream_getl (s);
  694. iflp->pkt_loss = stream_getf (s);
  695. iflp->res_bw = stream_getf (s);
  696. iflp->ava_bw = stream_getf (s);
  697. iflp->use_bw = stream_getf (s);
  698. }
  699. struct interface *
  700. zebra_interface_link_params_read (struct stream *s)
  701. {
  702. struct if_link_params *iflp;
  703. uint32_t ifindex = stream_getl (s);
  704. struct interface *ifp = if_lookup_by_index (ifindex);
  705. if (ifp == NULL || s == NULL)
  706. {
  707. zlog_err ("%s: unknown ifindex %u, shouldn't happen",
  708. __func__, ifindex);
  709. return NULL;
  710. }
  711. if ((iflp = if_link_params_get (ifp)) == NULL)
  712. return NULL;
  713. link_params_set_value(s, iflp);
  714. return ifp;
  715. }
  716. void
  717. zebra_interface_if_set_value (struct stream *s, struct interface *ifp)
  718. {
  719. u_char link_params_status = 0;
  720. /* Read interface's index. */
  721. ifp->ifindex = stream_getl (s);
  722. ifp->status = stream_getc (s);
  723. /* Read interface's value. */
  724. ifp->flags = stream_getq (s);
  725. ifp->metric = stream_getl (s);
  726. ifp->mtu = stream_getl (s);
  727. ifp->mtu6 = stream_getl (s);
  728. ifp->bandwidth = stream_getl (s);
  729. ifp->ll_type = stream_getl (s);
  730. ifp->hw_addr_len = stream_getl (s);
  731. if (ifp->hw_addr_len)
  732. stream_get (ifp->hw_addr, s, MIN(ifp->hw_addr_len, INTERFACE_HWADDR_MAX));
  733. /* Read Traffic Engineering status */
  734. link_params_status = stream_getc (s);
  735. /* Then, Traffic Engineering parameters if any */
  736. if (link_params_status)
  737. {
  738. struct if_link_params *iflp = if_link_params_get (ifp);
  739. link_params_set_value(s, iflp);
  740. }
  741. }
  742. size_t
  743. zebra_interface_link_params_write (struct stream *s, struct interface *ifp)
  744. {
  745. size_t w;
  746. struct if_link_params *iflp;
  747. int i;
  748. if (s == NULL || ifp == NULL || ifp->link_params == NULL)
  749. return 0;
  750. iflp = ifp->link_params;
  751. w = 0;
  752. w += stream_putl (s, iflp->lp_status);
  753. w += stream_putl (s, iflp->te_metric);
  754. w += stream_putf (s, iflp->max_bw);
  755. w += stream_putf (s, iflp->max_rsv_bw);
  756. w += stream_putl (s, MAX_CLASS_TYPE);
  757. for (i = 0; i < MAX_CLASS_TYPE; i++)
  758. w += stream_putf (s, iflp->unrsv_bw[i]);
  759. w += stream_putl (s, iflp->admin_grp);
  760. w += stream_putl (s, iflp->rmt_as);
  761. w += stream_put_in_addr (s, &iflp->rmt_ip);
  762. w += stream_putl (s, iflp->av_delay);
  763. w += stream_putl (s, iflp->min_delay);
  764. w += stream_putl (s, iflp->max_delay);
  765. w += stream_putl (s, iflp->delay_var);
  766. w += stream_putf (s, iflp->pkt_loss);
  767. w += stream_putf (s, iflp->res_bw);
  768. w += stream_putf (s, iflp->ava_bw);
  769. w += stream_putf (s, iflp->use_bw);
  770. return w;
  771. }
  772. /*
  773. * format of message for address additon is:
  774. * 0
  775. * 0 1 2 3 4 5 6 7
  776. * +-+-+-+-+-+-+-+-+
  777. * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
  778. * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
  779. * | |
  780. * + +
  781. * | ifindex |
  782. * + +
  783. * | |
  784. * + +
  785. * | |
  786. * +-+-+-+-+-+-+-+-+
  787. * | ifc_flags | flags for connected address
  788. * +-+-+-+-+-+-+-+-+
  789. * | addr_family |
  790. * +-+-+-+-+-+-+-+-+
  791. * | addr... |
  792. * : :
  793. * | |
  794. * +-+-+-+-+-+-+-+-+
  795. * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
  796. * +-+-+-+-+-+-+-+-+
  797. * | daddr.. |
  798. * : :
  799. * | |
  800. * +-+-+-+-+-+-+-+-+
  801. */
  802. static int
  803. memconstant(const void *s, int c, size_t n)
  804. {
  805. const u_char *p = s;
  806. while (n-- > 0)
  807. if (*p++ != c)
  808. return 0;
  809. return 1;
  810. }
  811. struct connected *
  812. zebra_interface_address_read (int type, struct stream *s, vrf_id_t vrf_id)
  813. {
  814. ifindex_t ifindex;
  815. struct interface *ifp;
  816. struct connected *ifc;
  817. struct prefix p, d, *dp;
  818. int plen;
  819. u_char ifc_flags;
  820. memset (&p, 0, sizeof(p));
  821. memset (&d, 0, sizeof(d));
  822. /* Get interface index. */
  823. ifindex = stream_getl (s);
  824. /* Lookup index. */
  825. ifp = if_lookup_by_index_vrf (ifindex, vrf_id);
  826. if (ifp == NULL)
  827. {
  828. zlog_warn ("zebra_interface_address_read(%s): "
  829. "Can't find interface by ifindex: %d ",
  830. (type == ZEBRA_INTERFACE_ADDRESS_ADD? "ADD" : "DELETE"),
  831. ifindex);
  832. return NULL;
  833. }
  834. /* Fetch flag. */
  835. ifc_flags = stream_getc (s);
  836. /* Fetch interface address. */
  837. d.family = p.family = stream_getc (s);
  838. plen = prefix_blen (&d);
  839. zclient_stream_get_prefix (s, &p);
  840. /* Fetch destination address. */
  841. stream_get (&d.u.prefix, s, plen);
  842. /* N.B. NULL destination pointers are encoded as all zeroes */
  843. dp = memconstant(&d.u.prefix,0,plen) ? NULL : &d;
  844. if (type == ZEBRA_INTERFACE_ADDRESS_ADD)
  845. {
  846. /* N.B. NULL destination pointers are encoded as all zeroes */
  847. ifc = connected_add_by_prefix(ifp, &p, dp);
  848. if (ifc != NULL)
  849. {
  850. ifc->flags = ifc_flags;
  851. if (ifc->destination)
  852. ifc->destination->prefixlen = ifc->address->prefixlen;
  853. else if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER))
  854. {
  855. /* carp interfaces on OpenBSD with 0.0.0.0/0 as "peer" */
  856. char buf[PREFIX_STRLEN];
  857. zlog_warn("warning: interface %s address %s "
  858. "with peer flag set, but no peer address!",
  859. ifp->name,
  860. prefix2str (ifc->address, buf, sizeof buf));
  861. UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
  862. }
  863. }
  864. }
  865. else
  866. {
  867. assert (type == ZEBRA_INTERFACE_ADDRESS_DELETE);
  868. ifc = connected_delete_by_prefix(ifp, &p);
  869. }
  870. return ifc;
  871. }
  872. /* Zebra client message read function. */
  873. static int
  874. zclient_read (struct thread *thread)
  875. {
  876. size_t already;
  877. uint16_t length, command;
  878. uint8_t marker, version;
  879. vrf_id_t vrf_id;
  880. struct zclient *zclient;
  881. /* Get socket to zebra. */
  882. zclient = THREAD_ARG (thread);
  883. zclient->t_read = NULL;
  884. /* Read zebra header (if we don't have it already). */
  885. if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE)
  886. {
  887. ssize_t nbyte;
  888. if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
  889. ZEBRA_HEADER_SIZE-already)) == 0) ||
  890. (nbyte == -1))
  891. {
  892. if (zclient_debug)
  893. zlog_debug ("zclient connection closed socket [%d].", zclient->sock);
  894. return zclient_failed(zclient);
  895. }
  896. if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
  897. {
  898. /* Try again later. */
  899. zclient_event (ZCLIENT_READ, zclient);
  900. return 0;
  901. }
  902. already = ZEBRA_HEADER_SIZE;
  903. }
  904. /* Reset to read from the beginning of the incoming packet. */
  905. stream_set_getp(zclient->ibuf, 0);
  906. /* Fetch header values. */
  907. length = stream_getw (zclient->ibuf);
  908. marker = stream_getc (zclient->ibuf);
  909. version = stream_getc (zclient->ibuf);
  910. vrf_id = stream_getw (zclient->ibuf);
  911. command = stream_getw (zclient->ibuf);
  912. if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
  913. {
  914. zlog_err("%s: socket %d version mismatch, marker %d, version %d",
  915. __func__, zclient->sock, marker, version);
  916. return zclient_failed(zclient);
  917. }
  918. if (length < ZEBRA_HEADER_SIZE)
  919. {
  920. zlog_err("%s: socket %d message length %u is less than %d ",
  921. __func__, zclient->sock, length, ZEBRA_HEADER_SIZE);
  922. return zclient_failed(zclient);
  923. }
  924. /* Length check. */
  925. if (length > STREAM_SIZE(zclient->ibuf))
  926. {
  927. struct stream *ns;
  928. zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...",
  929. __func__, length, (u_long)STREAM_SIZE(zclient->ibuf));
  930. ns = stream_new(length);
  931. stream_copy(ns, zclient->ibuf);
  932. stream_free (zclient->ibuf);
  933. zclient->ibuf = ns;
  934. }
  935. /* Read rest of zebra packet. */
  936. if (already < length)
  937. {
  938. ssize_t nbyte;
  939. if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
  940. length-already)) == 0) ||
  941. (nbyte == -1))
  942. {
  943. if (zclient_debug)
  944. zlog_debug("zclient connection closed socket [%d].", zclient->sock);
  945. return zclient_failed(zclient);
  946. }
  947. if (nbyte != (ssize_t)(length-already))
  948. {
  949. /* Try again later. */
  950. zclient_event (ZCLIENT_READ, zclient);
  951. return 0;
  952. }
  953. }
  954. length -= ZEBRA_HEADER_SIZE;
  955. if (zclient_debug)
  956. zlog_debug("zclient 0x%p command 0x%x VRF %u\n", (void *)zclient, command, vrf_id);
  957. switch (command)
  958. {
  959. case ZEBRA_ROUTER_ID_UPDATE:
  960. if (zclient->router_id_update)
  961. (*zclient->router_id_update) (command, zclient, length, vrf_id);
  962. break;
  963. case ZEBRA_INTERFACE_ADD:
  964. if (zclient->interface_add)
  965. (*zclient->interface_add) (command, zclient, length, vrf_id);
  966. break;
  967. case ZEBRA_INTERFACE_DELETE:
  968. if (zclient->interface_delete)
  969. (*zclient->interface_delete) (command, zclient, length, vrf_id);
  970. break;
  971. case ZEBRA_INTERFACE_ADDRESS_ADD:
  972. if (zclient->interface_address_add)
  973. (*zclient->interface_address_add) (command, zclient, length, vrf_id);
  974. break;
  975. case ZEBRA_INTERFACE_ADDRESS_DELETE:
  976. if (zclient->interface_address_delete)
  977. (*zclient->interface_address_delete) (command, zclient, length, vrf_id);
  978. break;
  979. case ZEBRA_INTERFACE_UP:
  980. if (zclient->interface_up)
  981. (*zclient->interface_up) (command, zclient, length, vrf_id);
  982. break;
  983. case ZEBRA_INTERFACE_DOWN:
  984. if (zclient->interface_down)
  985. (*zclient->interface_down) (command, zclient, length, vrf_id);
  986. break;
  987. case ZEBRA_IPV4_ROUTE_ADD:
  988. if (zclient->ipv4_route_add)
  989. (*zclient->ipv4_route_add) (command, zclient, length, vrf_id);
  990. break;
  991. case ZEBRA_IPV4_ROUTE_DELETE:
  992. if (zclient->ipv4_route_delete)
  993. (*zclient->ipv4_route_delete) (command, zclient, length, vrf_id);
  994. break;
  995. case ZEBRA_IPV6_ROUTE_ADD:
  996. if (zclient->ipv6_route_add)
  997. (*zclient->ipv6_route_add) (command, zclient, length, vrf_id);
  998. break;
  999. case ZEBRA_IPV6_ROUTE_DELETE:
  1000. if (zclient->ipv6_route_delete)
  1001. (*zclient->ipv6_route_delete) (command, zclient, length, vrf_id);
  1002. break;
  1003. case ZEBRA_INTERFACE_LINK_PARAMS:
  1004. if (zclient->interface_link_params)
  1005. (*zclient->interface_link_params) (command, zclient, length);
  1006. case ZEBRA_NEXTHOP_UPDATE:
  1007. if (zclient->nexthop_update)
  1008. (*zclient->nexthop_update) (command, zclient, length, vrf_id);
  1009. break;
  1010. default:
  1011. break;
  1012. }
  1013. if (zclient->sock < 0)
  1014. /* Connection was closed during packet processing. */
  1015. return -1;
  1016. /* Register read thread. */
  1017. stream_reset(zclient->ibuf);
  1018. zclient_event (ZCLIENT_READ, zclient);
  1019. return 0;
  1020. }
  1021. void
  1022. zclient_redistribute (int command, struct zclient *zclient, int type,
  1023. vrf_id_t vrf_id)
  1024. {
  1025. if (command == ZEBRA_REDISTRIBUTE_ADD)
  1026. {
  1027. if (vrf_bitmap_check (zclient->redist[type], vrf_id))
  1028. return;
  1029. vrf_bitmap_set (zclient->redist[type], vrf_id);
  1030. }
  1031. else
  1032. {
  1033. if (!vrf_bitmap_check (zclient->redist[type], vrf_id))
  1034. return;
  1035. vrf_bitmap_unset (zclient->redist[type], vrf_id);
  1036. }
  1037. if (zclient->sock > 0)
  1038. zebra_redistribute_send (command, zclient, type, vrf_id);
  1039. }
  1040. void
  1041. zclient_redistribute_default (int command, struct zclient *zclient,
  1042. vrf_id_t vrf_id)
  1043. {
  1044. if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD)
  1045. {
  1046. if (vrf_bitmap_check (zclient->default_information, vrf_id))
  1047. return;
  1048. vrf_bitmap_set (zclient->default_information, vrf_id);
  1049. }
  1050. else
  1051. {
  1052. if (!vrf_bitmap_check (zclient->default_information, vrf_id))
  1053. return;
  1054. vrf_bitmap_unset (zclient->default_information, vrf_id);
  1055. }
  1056. if (zclient->sock > 0)
  1057. zebra_message_send (zclient, command, vrf_id);
  1058. }
  1059. static void
  1060. zclient_event (enum event event, struct zclient *zclient)
  1061. {
  1062. switch (event)
  1063. {
  1064. case ZCLIENT_SCHEDULE:
  1065. if (! zclient->t_connect)
  1066. zclient->t_connect =
  1067. thread_add_event (zclient->master, zclient_connect, zclient, 0);
  1068. break;
  1069. case ZCLIENT_CONNECT:
  1070. if (zclient->fail >= 10)
  1071. return;
  1072. if (zclient_debug)
  1073. zlog_debug ("zclient connect schedule interval is %d",
  1074. zclient->fail < 3 ? 10 : 60);
  1075. if (! zclient->t_connect)
  1076. zclient->t_connect =
  1077. thread_add_timer (zclient->master, zclient_connect, zclient,
  1078. zclient->fail < 3 ? 10 : 60);
  1079. break;
  1080. case ZCLIENT_READ:
  1081. zclient->t_read =
  1082. thread_add_read (zclient->master, zclient_read, zclient, zclient->sock);
  1083. break;
  1084. }
  1085. }
  1086. const char *zclient_serv_path_get()
  1087. {
  1088. return zclient_serv_path ? zclient_serv_path : ZEBRA_SERV_PATH;
  1089. }
  1090. void
  1091. zclient_serv_path_set (char *path)
  1092. {
  1093. struct stat sb;
  1094. /* reset */
  1095. zclient_serv_path = NULL;
  1096. /* test if `path' is socket. don't set it otherwise. */
  1097. if (stat(path, &sb) == -1)
  1098. {
  1099. zlog_warn ("%s: zebra socket `%s' does not exist", __func__, path);
  1100. return;
  1101. }
  1102. if ((sb.st_mode & S_IFMT) != S_IFSOCK)
  1103. {
  1104. zlog_warn ("%s: `%s' is not unix socket, sir", __func__, path);
  1105. return;
  1106. }
  1107. /* it seems that path is unix socket */
  1108. zclient_serv_path = path;
  1109. }