zclient.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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. if (zclient_debug)
  95. zlog_debug ("zclient stopped");
  96. /* Stop threads. */
  97. THREAD_OFF(zclient->t_read);
  98. THREAD_OFF(zclient->t_connect);
  99. THREAD_OFF(zclient->t_write);
  100. /* Reset streams. */
  101. stream_reset(zclient->ibuf);
  102. stream_reset(zclient->obuf);
  103. /* Empty the write buffer. */
  104. buffer_reset(zclient->wb);
  105. /* Close socket. */
  106. if (zclient->sock >= 0)
  107. {
  108. close (zclient->sock);
  109. zclient->sock = -1;
  110. }
  111. zclient->fail = 0;
  112. }
  113. void
  114. zclient_reset (struct zclient *zclient)
  115. {
  116. zclient_stop (zclient);
  117. zclient_init (zclient, zclient->redist_default);
  118. }
  119. #ifdef HAVE_TCP_ZEBRA
  120. /* Make socket to zebra daemon. Return zebra socket. */
  121. static int
  122. zclient_socket(void)
  123. {
  124. int sock;
  125. int ret;
  126. struct sockaddr_in serv;
  127. /* We should think about IPv6 connection. */
  128. sock = socket (AF_INET, SOCK_STREAM, 0);
  129. if (sock < 0)
  130. return -1;
  131. /* Make server socket. */
  132. memset (&serv, 0, sizeof (struct sockaddr_in));
  133. serv.sin_family = AF_INET;
  134. serv.sin_port = htons (ZEBRA_PORT);
  135. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  136. serv.sin_len = sizeof (struct sockaddr_in);
  137. #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
  138. serv.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
  139. /* Connect to zebra. */
  140. ret = connect (sock, (struct sockaddr *) &serv, sizeof (serv));
  141. if (ret < 0)
  142. {
  143. close (sock);
  144. return -1;
  145. }
  146. return sock;
  147. }
  148. #else
  149. /* For sockaddr_un. */
  150. #include <sys/un.h>
  151. static int
  152. zclient_socket_un (const char *path)
  153. {
  154. int ret;
  155. int sock, len;
  156. struct sockaddr_un addr;
  157. sock = socket (AF_UNIX, SOCK_STREAM, 0);
  158. if (sock < 0)
  159. return -1;
  160. /* Make server socket. */
  161. memset (&addr, 0, sizeof (struct sockaddr_un));
  162. addr.sun_family = AF_UNIX;
  163. strncpy (addr.sun_path, path, strlen (path));
  164. #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
  165. len = addr.sun_len = SUN_LEN(&addr);
  166. #else
  167. len = sizeof (addr.sun_family) + strlen (addr.sun_path);
  168. #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
  169. ret = connect (sock, (struct sockaddr *) &addr, len);
  170. if (ret < 0)
  171. {
  172. close (sock);
  173. return -1;
  174. }
  175. return sock;
  176. }
  177. #endif /* HAVE_TCP_ZEBRA */
  178. /**
  179. * Connect to zebra daemon.
  180. * @param zclient a pointer to zclient structure
  181. * @return socket fd just to make sure that connection established
  182. * @see zclient_init
  183. * @see zclient_new
  184. */
  185. int
  186. zclient_socket_connect (struct zclient *zclient)
  187. {
  188. #ifdef HAVE_TCP_ZEBRA
  189. zclient->sock = zclient_socket ();
  190. #else
  191. zclient->sock = zclient_socket_un (zclient_serv_path_get());
  192. #endif
  193. return zclient->sock;
  194. }
  195. static int
  196. zclient_failed(struct zclient *zclient)
  197. {
  198. zclient->fail++;
  199. zclient_stop(zclient);
  200. zclient_event(ZCLIENT_CONNECT, zclient);
  201. return -1;
  202. }
  203. static int
  204. zclient_flush_data(struct thread *thread)
  205. {
  206. struct zclient *zclient = THREAD_ARG(thread);
  207. zclient->t_write = NULL;
  208. if (zclient->sock < 0)
  209. return -1;
  210. switch (buffer_flush_available(zclient->wb, zclient->sock))
  211. {
  212. case BUFFER_ERROR:
  213. zlog_warn("%s: buffer_flush_available failed on zclient fd %d, closing",
  214. __func__, zclient->sock);
  215. return zclient_failed(zclient);
  216. break;
  217. case BUFFER_PENDING:
  218. zclient->t_write = thread_add_write (zclient->master, zclient_flush_data,
  219. zclient, zclient->sock);
  220. break;
  221. case BUFFER_EMPTY:
  222. break;
  223. }
  224. return 0;
  225. }
  226. int
  227. zclient_send_message(struct zclient *zclient)
  228. {
  229. if (zclient->sock < 0)
  230. return -1;
  231. switch (buffer_write(zclient->wb, zclient->sock, STREAM_DATA(zclient->obuf),
  232. stream_get_endp(zclient->obuf)))
  233. {
  234. case BUFFER_ERROR:
  235. zlog_warn("%s: buffer_write failed to zclient fd %d, closing",
  236. __func__, zclient->sock);
  237. return zclient_failed(zclient);
  238. break;
  239. case BUFFER_EMPTY:
  240. THREAD_OFF(zclient->t_write);
  241. break;
  242. case BUFFER_PENDING:
  243. THREAD_WRITE_ON (zclient->master, zclient->t_write,
  244. zclient_flush_data, zclient, zclient->sock);
  245. break;
  246. }
  247. return 0;
  248. }
  249. void
  250. zclient_create_header (struct stream *s, uint16_t command, vrf_id_t vrf_id)
  251. {
  252. /* length placeholder, caller can update */
  253. stream_putw (s, ZEBRA_HEADER_SIZE);
  254. stream_putc (s, ZEBRA_HEADER_MARKER);
  255. stream_putc (s, ZSERV_VERSION);
  256. stream_putw (s, vrf_id);
  257. stream_putw (s, command);
  258. }
  259. int
  260. zclient_read_header (struct stream *s, int sock, u_int16_t *size, u_char *marker,
  261. u_char *version, u_int16_t *vrf_id, u_int16_t *cmd)
  262. {
  263. if (stream_read (s, sock, ZEBRA_HEADER_SIZE) != ZEBRA_HEADER_SIZE)
  264. return -1;
  265. *size = stream_getw (s) - ZEBRA_HEADER_SIZE;
  266. *marker = stream_getc (s);
  267. *version = stream_getc (s);
  268. *vrf_id = stream_getw (s);
  269. *cmd = stream_getw (s);
  270. if (*version != ZSERV_VERSION || *marker != ZEBRA_HEADER_MARKER)
  271. {
  272. zlog_err("%s: socket %d version mismatch, marker %d, version %d",
  273. __func__, sock, *marker, *version);
  274. return -1;
  275. }
  276. if (*size && stream_read (s, sock, *size) != *size)
  277. return -1;
  278. return 0;
  279. }
  280. /* Send simple Zebra message. */
  281. static int
  282. zebra_message_send (struct zclient *zclient, int command, vrf_id_t vrf_id)
  283. {
  284. struct stream *s;
  285. /* Get zclient output buffer. */
  286. s = zclient->obuf;
  287. stream_reset (s);
  288. /* Send very simple command only Zebra message. */
  289. zclient_create_header (s, command, vrf_id);
  290. return zclient_send_message(zclient);
  291. }
  292. static int
  293. zebra_hello_send (struct zclient *zclient)
  294. {
  295. struct stream *s;
  296. if (zclient->redist_default)
  297. {
  298. s = zclient->obuf;
  299. stream_reset (s);
  300. /* The VRF ID in the HELLO message is always 0. */
  301. zclient_create_header (s, ZEBRA_HELLO, VRF_DEFAULT);
  302. stream_putc (s, zclient->redist_default);
  303. stream_putw_at (s, 0, stream_get_endp (s));
  304. return zclient_send_message(zclient);
  305. }
  306. return 0;
  307. }
  308. /* Send requests to zebra daemon for the information in a VRF. */
  309. void
  310. zclient_send_requests (struct zclient *zclient, vrf_id_t vrf_id)
  311. {
  312. int i;
  313. /* zclient is disabled. */
  314. if (! zclient->enable)
  315. return;
  316. /* If not connected to the zebra yet. */
  317. if (zclient->sock < 0)
  318. return;
  319. if (zclient_debug)
  320. zlog_debug ("%s: send messages for VRF %u", __func__, vrf_id);
  321. /* We need router-id information. */
  322. zebra_message_send (zclient, ZEBRA_ROUTER_ID_ADD, vrf_id);
  323. /* We need interface information. */
  324. zebra_message_send (zclient, ZEBRA_INTERFACE_ADD, vrf_id);
  325. /* Set unwanted redistribute route. */
  326. vrf_bitmap_set (zclient->redist[zclient->redist_default], vrf_id);
  327. /* Flush all redistribute request. */
  328. for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
  329. if (i != zclient->redist_default &&
  330. vrf_bitmap_check (zclient->redist[i], vrf_id))
  331. zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, i, vrf_id);
  332. /* If default information is needed. */
  333. if (vrf_bitmap_check (zclient->default_information, VRF_DEFAULT))
  334. zebra_message_send (zclient, ZEBRA_REDISTRIBUTE_DEFAULT_ADD, vrf_id);
  335. }
  336. /* Make connection to zebra daemon. */
  337. int
  338. zclient_start (struct zclient *zclient)
  339. {
  340. if (zclient_debug)
  341. zlog_debug ("zclient_start is called");
  342. /* zclient is disabled. */
  343. if (! zclient->enable)
  344. return 0;
  345. /* If already connected to the zebra. */
  346. if (zclient->sock >= 0)
  347. return 0;
  348. /* Check connect thread. */
  349. if (zclient->t_connect)
  350. return 0;
  351. if (zclient_socket_connect(zclient) < 0)
  352. {
  353. if (zclient_debug)
  354. zlog_debug ("zclient connection fail");
  355. zclient->fail++;
  356. zclient_event (ZCLIENT_CONNECT, zclient);
  357. return -1;
  358. }
  359. if (set_nonblocking(zclient->sock) < 0)
  360. zlog_warn("%s: set_nonblocking(%d) failed", __func__, zclient->sock);
  361. /* Clear fail count. */
  362. zclient->fail = 0;
  363. if (zclient_debug)
  364. zlog_debug ("zclient connect success with socket [%d]", zclient->sock);
  365. /* Create read thread. */
  366. zclient_event (ZCLIENT_READ, zclient);
  367. zebra_hello_send (zclient);
  368. /* Inform the successful connection. */
  369. if (zclient->zebra_connected)
  370. (*zclient->zebra_connected) (zclient);
  371. return 0;
  372. }
  373. /* This function is a wrapper function for calling zclient_start from
  374. timer or event thread. */
  375. static int
  376. zclient_connect (struct thread *t)
  377. {
  378. struct zclient *zclient;
  379. zclient = THREAD_ARG (t);
  380. zclient->t_connect = NULL;
  381. if (zclient_debug)
  382. zlog_debug ("zclient_connect is called");
  383. return zclient_start (zclient);
  384. }
  385. /*
  386. * "xdr_encode"-like interface that allows daemon (client) to send
  387. * a message to zebra server for a route that needs to be
  388. * added/deleted to the kernel. Info about the route is specified
  389. * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
  390. * the info down the zclient socket using the stream_* functions.
  391. *
  392. * The corresponding read ("xdr_decode") function on the server
  393. * side is zread_ipv4_add()/zread_ipv4_delete().
  394. *
  395. * 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
  396. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  397. * | Length (2) | Command | Route Type |
  398. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  399. * | ZEBRA Flags | Message Flags | Prefix length |
  400. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  401. * | Destination IPv4 Prefix for route |
  402. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  403. * | Nexthop count |
  404. * +-+-+-+-+-+-+-+-+
  405. *
  406. *
  407. * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
  408. * described, as per the Nexthop count. Each nexthop described as:
  409. *
  410. * +-+-+-+-+-+-+-+-+
  411. * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
  412. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  413. * | IPv4 Nexthop address or Interface Index number |
  414. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  415. *
  416. * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or
  417. * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_
  418. * nexthop information is provided, and the message describes a prefix
  419. * to blackhole or reject route.
  420. *
  421. * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
  422. * byte value.
  423. *
  424. * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
  425. * byte value.
  426. *
  427. * XXX: No attention paid to alignment.
  428. */
  429. int
  430. zapi_ipv4_route (u_char cmd, struct zclient *zclient, struct prefix_ipv4 *p,
  431. struct zapi_ipv4 *api)
  432. {
  433. int i;
  434. int psize;
  435. struct stream *s;
  436. /* Reset stream. */
  437. s = zclient->obuf;
  438. stream_reset (s);
  439. zclient_create_header (s, cmd, api->vrf_id);
  440. /* Put type and nexthop. */
  441. stream_putc (s, api->type);
  442. stream_putc (s, api->flags);
  443. stream_putc (s, api->message);
  444. stream_putw (s, api->safi);
  445. /* Put prefix information. */
  446. psize = PSIZE (p->prefixlen);
  447. stream_putc (s, p->prefixlen);
  448. stream_write (s, (u_char *) & p->prefix, psize);
  449. /* Nexthop, ifindex, distance and metric information. */
  450. if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
  451. {
  452. if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE))
  453. {
  454. stream_putc (s, 1);
  455. stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE);
  456. /* XXX assert(api->nexthop_num == 0); */
  457. /* XXX assert(api->ifindex_num == 0); */
  458. }
  459. else
  460. stream_putc (s, api->nexthop_num + api->ifindex_num);
  461. for (i = 0; i < api->nexthop_num; i++)
  462. {
  463. stream_putc (s, ZEBRA_NEXTHOP_IPV4);
  464. stream_put_in_addr (s, api->nexthop[i]);
  465. }
  466. for (i = 0; i < api->ifindex_num; i++)
  467. {
  468. stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
  469. stream_putl (s, api->ifindex[i]);
  470. }
  471. }
  472. if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
  473. stream_putc (s, api->distance);
  474. if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
  475. stream_putl (s, api->metric);
  476. /* Put length at the first point of the stream. */
  477. stream_putw_at (s, 0, stream_get_endp (s));
  478. return zclient_send_message(zclient);
  479. }
  480. #ifdef HAVE_IPV6
  481. int
  482. zapi_ipv6_route (u_char cmd, struct zclient *zclient, struct prefix_ipv6 *p,
  483. struct zapi_ipv6 *api)
  484. {
  485. int i;
  486. int psize;
  487. struct stream *s;
  488. /* Reset stream. */
  489. s = zclient->obuf;
  490. stream_reset (s);
  491. zclient_create_header (s, cmd, api->vrf_id);
  492. /* Put type and nexthop. */
  493. stream_putc (s, api->type);
  494. stream_putc (s, api->flags);
  495. stream_putc (s, api->message);
  496. stream_putw (s, api->safi);
  497. /* Put prefix information. */
  498. psize = PSIZE (p->prefixlen);
  499. stream_putc (s, p->prefixlen);
  500. stream_write (s, (u_char *)&p->prefix, psize);
  501. /* Nexthop, ifindex, distance and metric information. */
  502. if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
  503. {
  504. stream_putc (s, api->nexthop_num + api->ifindex_num);
  505. for (i = 0; i < api->nexthop_num; i++)
  506. {
  507. stream_putc (s, ZEBRA_NEXTHOP_IPV6);
  508. stream_write (s, (u_char *)api->nexthop[i], 16);
  509. }
  510. for (i = 0; i < api->ifindex_num; i++)
  511. {
  512. stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
  513. stream_putl (s, api->ifindex[i]);
  514. }
  515. }
  516. if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
  517. stream_putc (s, api->distance);
  518. if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
  519. stream_putl (s, api->metric);
  520. /* Put length at the first point of the stream. */
  521. stream_putw_at (s, 0, stream_get_endp (s));
  522. return zclient_send_message(zclient);
  523. }
  524. #endif /* HAVE_IPV6 */
  525. /*
  526. * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
  527. * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
  528. * then set/unset redist[type] in the client handle (a struct zserv) for the
  529. * sending client
  530. */
  531. int
  532. zebra_redistribute_send (int command, struct zclient *zclient, int type,
  533. vrf_id_t vrf_id)
  534. {
  535. struct stream *s;
  536. s = zclient->obuf;
  537. stream_reset(s);
  538. zclient_create_header (s, command, vrf_id);
  539. stream_putc (s, type);
  540. stream_putw_at (s, 0, stream_get_endp (s));
  541. return zclient_send_message(zclient);
  542. }
  543. /* Router-id update from zebra daemon. */
  544. void
  545. zebra_router_id_update_read (struct stream *s, struct prefix *rid)
  546. {
  547. int plen;
  548. /* Fetch interface address. */
  549. rid->family = stream_getc (s);
  550. plen = prefix_blen (rid);
  551. stream_get (&rid->u.prefix, s, plen);
  552. rid->prefixlen = stream_getc (s);
  553. }
  554. /* Interface addition from zebra daemon. */
  555. /*
  556. * The format of the message sent with type ZEBRA_INTERFACE_ADD or
  557. * ZEBRA_INTERFACE_DELETE from zebra to the client is:
  558. * 0 1 2 3
  559. * 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
  560. * +-+-+-+-+-+-+-+-+
  561. * | type |
  562. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  563. * | ifname |
  564. * | |
  565. * | |
  566. * | |
  567. * | |
  568. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  569. * | ifindex |
  570. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  571. * | if_flags |
  572. * | |
  573. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  574. * | metric |
  575. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  576. * | ifmtu |
  577. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  578. * | ifmtu6 |
  579. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  580. * | bandwidth |
  581. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  582. * | sockaddr_dl |
  583. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  584. */
  585. struct interface *
  586. zebra_interface_add_read (struct stream *s, vrf_id_t vrf_id)
  587. {
  588. struct interface *ifp;
  589. char ifname_tmp[INTERFACE_NAMSIZ];
  590. /* Read interface name. */
  591. stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
  592. /* Lookup/create interface by name. */
  593. ifp = if_get_by_name_len_vrf (ifname_tmp,
  594. strnlen (ifname_tmp, INTERFACE_NAMSIZ),
  595. vrf_id);
  596. zebra_interface_if_set_value (s, ifp);
  597. return ifp;
  598. }
  599. /*
  600. * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
  601. * from zebra server. The format of this message is the same as
  602. * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
  603. * comments for zebra_interface_add_read), except that no sockaddr_dl
  604. * is sent at the tail of the message.
  605. */
  606. struct interface *
  607. zebra_interface_state_read (struct stream *s, vrf_id_t vrf_id)
  608. {
  609. struct interface *ifp;
  610. char ifname_tmp[INTERFACE_NAMSIZ];
  611. /* Read interface name. */
  612. stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
  613. /* Lookup this by interface index. */
  614. ifp = if_lookup_by_name_len_vrf (ifname_tmp,
  615. strnlen (ifname_tmp, INTERFACE_NAMSIZ),
  616. vrf_id);
  617. /* If such interface does not exist, indicate an error */
  618. if (! ifp)
  619. return NULL;
  620. zebra_interface_if_set_value (s, ifp);
  621. return ifp;
  622. }
  623. /*
  624. * format of message for address additon is:
  625. * 0
  626. * 0 1 2 3 4 5 6 7
  627. * +-+-+-+-+-+-+-+-+
  628. * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
  629. * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
  630. * | |
  631. * + +
  632. * | ifindex |
  633. * + +
  634. * | |
  635. * + +
  636. * | |
  637. * +-+-+-+-+-+-+-+-+
  638. * | ifc_flags | flags for connected address
  639. * +-+-+-+-+-+-+-+-+
  640. * | addr_family |
  641. * +-+-+-+-+-+-+-+-+
  642. * | addr... |
  643. * : :
  644. * | |
  645. * +-+-+-+-+-+-+-+-+
  646. * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
  647. * +-+-+-+-+-+-+-+-+
  648. * | daddr.. |
  649. * : :
  650. * | |
  651. * +-+-+-+-+-+-+-+-+
  652. *
  653. */
  654. void
  655. zebra_interface_if_set_value (struct stream *s, struct interface *ifp)
  656. {
  657. /* Read interface's index. */
  658. ifp->ifindex = stream_getl (s);
  659. ifp->status = stream_getc (s);
  660. /* Read interface's value. */
  661. ifp->flags = stream_getq (s);
  662. ifp->metric = stream_getl (s);
  663. ifp->mtu = stream_getl (s);
  664. ifp->mtu6 = stream_getl (s);
  665. ifp->bandwidth = stream_getl (s);
  666. #ifdef HAVE_STRUCT_SOCKADDR_DL
  667. stream_get (&ifp->sdl, s, sizeof (ifp->sdl_storage));
  668. #else
  669. ifp->hw_addr_len = stream_getl (s);
  670. if (ifp->hw_addr_len)
  671. stream_get (ifp->hw_addr, s, ifp->hw_addr_len);
  672. #endif /* HAVE_STRUCT_SOCKADDR_DL */
  673. }
  674. static int
  675. memconstant(const void *s, int c, size_t n)
  676. {
  677. const u_char *p = s;
  678. while (n-- > 0)
  679. if (*p++ != c)
  680. return 0;
  681. return 1;
  682. }
  683. struct connected *
  684. zebra_interface_address_read (int type, struct stream *s, vrf_id_t vrf_id)
  685. {
  686. unsigned int ifindex;
  687. struct interface *ifp;
  688. struct connected *ifc;
  689. struct prefix p, d;
  690. int family;
  691. int plen;
  692. u_char ifc_flags;
  693. memset (&p, 0, sizeof(p));
  694. memset (&d, 0, sizeof(d));
  695. /* Get interface index. */
  696. ifindex = stream_getl (s);
  697. /* Lookup index. */
  698. ifp = if_lookup_by_index_vrf (ifindex, vrf_id);
  699. if (ifp == NULL)
  700. {
  701. zlog_warn ("zebra_interface_address_read(%s): "
  702. "Can't find interface by ifindex: %d ",
  703. (type == ZEBRA_INTERFACE_ADDRESS_ADD? "ADD" : "DELETE"),
  704. ifindex);
  705. return NULL;
  706. }
  707. /* Fetch flag. */
  708. ifc_flags = stream_getc (s);
  709. /* Fetch interface address. */
  710. family = p.family = stream_getc (s);
  711. plen = prefix_blen (&p);
  712. stream_get (&p.u.prefix, s, plen);
  713. p.prefixlen = stream_getc (s);
  714. /* Fetch destination address. */
  715. stream_get (&d.u.prefix, s, plen);
  716. d.family = family;
  717. if (type == ZEBRA_INTERFACE_ADDRESS_ADD)
  718. {
  719. /* N.B. NULL destination pointers are encoded as all zeroes */
  720. ifc = connected_add_by_prefix(ifp, &p,(memconstant(&d.u.prefix,0,plen) ?
  721. NULL : &d));
  722. if (ifc != NULL)
  723. {
  724. ifc->flags = ifc_flags;
  725. if (ifc->destination)
  726. ifc->destination->prefixlen = ifc->address->prefixlen;
  727. else if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER))
  728. {
  729. /* carp interfaces on OpenBSD with 0.0.0.0/0 as "peer" */
  730. char buf[PREFIX_STRLEN];
  731. zlog_warn("warning: interface %s address %s "
  732. "with peer flag set, but no peer address!",
  733. ifp->name,
  734. prefix2str (ifc->address, buf, sizeof buf));
  735. UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
  736. }
  737. }
  738. }
  739. else
  740. {
  741. assert (type == ZEBRA_INTERFACE_ADDRESS_DELETE);
  742. ifc = connected_delete_by_prefix(ifp, &p);
  743. }
  744. return ifc;
  745. }
  746. /* Zebra client message read function. */
  747. static int
  748. zclient_read (struct thread *thread)
  749. {
  750. size_t already;
  751. uint16_t length, command;
  752. uint8_t marker, version;
  753. vrf_id_t vrf_id;
  754. struct zclient *zclient;
  755. /* Get socket to zebra. */
  756. zclient = THREAD_ARG (thread);
  757. zclient->t_read = NULL;
  758. /* Read zebra header (if we don't have it already). */
  759. if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE)
  760. {
  761. ssize_t nbyte;
  762. if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
  763. ZEBRA_HEADER_SIZE-already)) == 0) ||
  764. (nbyte == -1))
  765. {
  766. if (zclient_debug)
  767. zlog_debug ("zclient connection closed socket [%d].", zclient->sock);
  768. return zclient_failed(zclient);
  769. }
  770. if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
  771. {
  772. /* Try again later. */
  773. zclient_event (ZCLIENT_READ, zclient);
  774. return 0;
  775. }
  776. already = ZEBRA_HEADER_SIZE;
  777. }
  778. /* Reset to read from the beginning of the incoming packet. */
  779. stream_set_getp(zclient->ibuf, 0);
  780. /* Fetch header values. */
  781. length = stream_getw (zclient->ibuf);
  782. marker = stream_getc (zclient->ibuf);
  783. version = stream_getc (zclient->ibuf);
  784. vrf_id = stream_getw (zclient->ibuf);
  785. command = stream_getw (zclient->ibuf);
  786. if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
  787. {
  788. zlog_err("%s: socket %d version mismatch, marker %d, version %d",
  789. __func__, zclient->sock, marker, version);
  790. return zclient_failed(zclient);
  791. }
  792. if (length < ZEBRA_HEADER_SIZE)
  793. {
  794. zlog_err("%s: socket %d message length %u is less than %d ",
  795. __func__, zclient->sock, length, ZEBRA_HEADER_SIZE);
  796. return zclient_failed(zclient);
  797. }
  798. /* Length check. */
  799. if (length > STREAM_SIZE(zclient->ibuf))
  800. {
  801. struct stream *ns;
  802. zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...",
  803. __func__, length, (u_long)STREAM_SIZE(zclient->ibuf));
  804. ns = stream_new(length);
  805. stream_copy(ns, zclient->ibuf);
  806. stream_free (zclient->ibuf);
  807. zclient->ibuf = ns;
  808. }
  809. /* Read rest of zebra packet. */
  810. if (already < length)
  811. {
  812. ssize_t nbyte;
  813. if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
  814. length-already)) == 0) ||
  815. (nbyte == -1))
  816. {
  817. if (zclient_debug)
  818. zlog_debug("zclient connection closed socket [%d].", zclient->sock);
  819. return zclient_failed(zclient);
  820. }
  821. if (nbyte != (ssize_t)(length-already))
  822. {
  823. /* Try again later. */
  824. zclient_event (ZCLIENT_READ, zclient);
  825. return 0;
  826. }
  827. }
  828. length -= ZEBRA_HEADER_SIZE;
  829. if (zclient_debug)
  830. zlog_debug("zclient 0x%p command 0x%x VRF %u\n", (void *)zclient, command, vrf_id);
  831. switch (command)
  832. {
  833. case ZEBRA_ROUTER_ID_UPDATE:
  834. if (zclient->router_id_update)
  835. (*zclient->router_id_update) (command, zclient, length, vrf_id);
  836. break;
  837. case ZEBRA_INTERFACE_ADD:
  838. if (zclient->interface_add)
  839. (*zclient->interface_add) (command, zclient, length, vrf_id);
  840. break;
  841. case ZEBRA_INTERFACE_DELETE:
  842. if (zclient->interface_delete)
  843. (*zclient->interface_delete) (command, zclient, length, vrf_id);
  844. break;
  845. case ZEBRA_INTERFACE_ADDRESS_ADD:
  846. if (zclient->interface_address_add)
  847. (*zclient->interface_address_add) (command, zclient, length, vrf_id);
  848. break;
  849. case ZEBRA_INTERFACE_ADDRESS_DELETE:
  850. if (zclient->interface_address_delete)
  851. (*zclient->interface_address_delete) (command, zclient, length, vrf_id);
  852. break;
  853. case ZEBRA_INTERFACE_UP:
  854. if (zclient->interface_up)
  855. (*zclient->interface_up) (command, zclient, length, vrf_id);
  856. break;
  857. case ZEBRA_INTERFACE_DOWN:
  858. if (zclient->interface_down)
  859. (*zclient->interface_down) (command, zclient, length, vrf_id);
  860. break;
  861. case ZEBRA_IPV4_ROUTE_ADD:
  862. if (zclient->ipv4_route_add)
  863. (*zclient->ipv4_route_add) (command, zclient, length, vrf_id);
  864. break;
  865. case ZEBRA_IPV4_ROUTE_DELETE:
  866. if (zclient->ipv4_route_delete)
  867. (*zclient->ipv4_route_delete) (command, zclient, length, vrf_id);
  868. break;
  869. case ZEBRA_IPV6_ROUTE_ADD:
  870. if (zclient->ipv6_route_add)
  871. (*zclient->ipv6_route_add) (command, zclient, length, vrf_id);
  872. break;
  873. case ZEBRA_IPV6_ROUTE_DELETE:
  874. if (zclient->ipv6_route_delete)
  875. (*zclient->ipv6_route_delete) (command, zclient, length, vrf_id);
  876. break;
  877. default:
  878. break;
  879. }
  880. if (zclient->sock < 0)
  881. /* Connection was closed during packet processing. */
  882. return -1;
  883. /* Register read thread. */
  884. stream_reset(zclient->ibuf);
  885. zclient_event (ZCLIENT_READ, zclient);
  886. return 0;
  887. }
  888. void
  889. zclient_redistribute (int command, struct zclient *zclient, int type,
  890. vrf_id_t vrf_id)
  891. {
  892. if (command == ZEBRA_REDISTRIBUTE_ADD)
  893. {
  894. if (vrf_bitmap_check (zclient->redist[type], vrf_id))
  895. return;
  896. vrf_bitmap_set (zclient->redist[type], vrf_id);
  897. }
  898. else
  899. {
  900. if (!vrf_bitmap_check (zclient->redist[type], vrf_id))
  901. return;
  902. vrf_bitmap_unset (zclient->redist[type], vrf_id);
  903. }
  904. if (zclient->sock > 0)
  905. zebra_redistribute_send (command, zclient, type, vrf_id);
  906. }
  907. void
  908. zclient_redistribute_default (int command, struct zclient *zclient,
  909. vrf_id_t vrf_id)
  910. {
  911. if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD)
  912. {
  913. if (vrf_bitmap_check (zclient->default_information, vrf_id))
  914. return;
  915. vrf_bitmap_set (zclient->default_information, vrf_id);
  916. }
  917. else
  918. {
  919. if (!vrf_bitmap_check (zclient->default_information, vrf_id))
  920. return;
  921. vrf_bitmap_unset (zclient->default_information, vrf_id);
  922. }
  923. if (zclient->sock > 0)
  924. zebra_message_send (zclient, command, vrf_id);
  925. }
  926. static void
  927. zclient_event (enum event event, struct zclient *zclient)
  928. {
  929. switch (event)
  930. {
  931. case ZCLIENT_SCHEDULE:
  932. if (! zclient->t_connect)
  933. zclient->t_connect =
  934. thread_add_event (zclient->master, zclient_connect, zclient, 0);
  935. break;
  936. case ZCLIENT_CONNECT:
  937. if (zclient->fail >= 10)
  938. return;
  939. if (zclient_debug)
  940. zlog_debug ("zclient connect schedule interval is %d",
  941. zclient->fail < 3 ? 10 : 60);
  942. if (! zclient->t_connect)
  943. zclient->t_connect =
  944. thread_add_timer (zclient->master, zclient_connect, zclient,
  945. zclient->fail < 3 ? 10 : 60);
  946. break;
  947. case ZCLIENT_READ:
  948. zclient->t_read =
  949. thread_add_read (zclient->master, zclient_read, zclient, zclient->sock);
  950. break;
  951. }
  952. }
  953. const char *zclient_serv_path_get()
  954. {
  955. return zclient_serv_path ? zclient_serv_path : ZEBRA_SERV_PATH;
  956. }
  957. void
  958. zclient_serv_path_set (char *path)
  959. {
  960. struct stat sb;
  961. /* reset */
  962. zclient_serv_path = NULL;
  963. /* test if `path' is socket. don't set it otherwise. */
  964. if (stat(path, &sb) == -1)
  965. {
  966. zlog_warn ("%s: zebra socket `%s' does not exist", __func__, path);
  967. return;
  968. }
  969. if ((sb.st_mode & S_IFMT) != S_IFSOCK)
  970. {
  971. zlog_warn ("%s: `%s' is not unix socket, sir", __func__, path);
  972. return;
  973. }
  974. /* it seems that path is unix socket */
  975. zclient_serv_path = path;
  976. }