zclient.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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. extern struct thread_master *master;
  38. const char *zclient_serv_path = NULL;
  39. /* This file local debug flag. */
  40. int zclient_debug = 0;
  41. /* Allocate zclient structure. */
  42. struct zclient *
  43. zclient_new ()
  44. {
  45. struct zclient *zclient;
  46. zclient = XCALLOC (MTYPE_ZCLIENT, sizeof (struct zclient));
  47. zclient->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
  48. zclient->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
  49. zclient->wb = buffer_new(0);
  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(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(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. /* Send simple Zebra message. */
  260. static int
  261. zebra_message_send (struct zclient *zclient, int command, vrf_id_t vrf_id)
  262. {
  263. struct stream *s;
  264. /* Get zclient output buffer. */
  265. s = zclient->obuf;
  266. stream_reset (s);
  267. /* Send very simple command only Zebra message. */
  268. zclient_create_header (s, command, vrf_id);
  269. return zclient_send_message(zclient);
  270. }
  271. static int
  272. zebra_hello_send (struct zclient *zclient)
  273. {
  274. struct stream *s;
  275. if (zclient->redist_default)
  276. {
  277. s = zclient->obuf;
  278. stream_reset (s);
  279. /* The VRF ID in the HELLO message is always 0. */
  280. zclient_create_header (s, ZEBRA_HELLO, VRF_DEFAULT);
  281. stream_putc (s, zclient->redist_default);
  282. stream_putw_at (s, 0, stream_get_endp (s));
  283. return zclient_send_message(zclient);
  284. }
  285. return 0;
  286. }
  287. /* Send requests to zebra daemon for the information in a VRF. */
  288. void
  289. zclient_send_requests (struct zclient *zclient, vrf_id_t vrf_id)
  290. {
  291. int i;
  292. /* zclient is disabled. */
  293. if (! zclient->enable)
  294. return;
  295. /* If not connected to the zebra yet. */
  296. if (zclient->sock < 0)
  297. return;
  298. if (zclient_debug)
  299. zlog_debug ("%s: send messages for VRF %u", __func__, vrf_id);
  300. /* We need router-id information. */
  301. zebra_message_send (zclient, ZEBRA_ROUTER_ID_ADD, vrf_id);
  302. /* We need interface information. */
  303. zebra_message_send (zclient, ZEBRA_INTERFACE_ADD, vrf_id);
  304. /* Set unwanted redistribute route. */
  305. vrf_bitmap_set (zclient->redist[zclient->redist_default], vrf_id);
  306. /* Flush all redistribute request. */
  307. for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
  308. if (i != zclient->redist_default &&
  309. vrf_bitmap_check (zclient->redist[i], vrf_id))
  310. zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, i, vrf_id);
  311. /* If default information is needed. */
  312. if (vrf_bitmap_check (zclient->default_information, VRF_DEFAULT))
  313. zebra_message_send (zclient, ZEBRA_REDISTRIBUTE_DEFAULT_ADD, vrf_id);
  314. }
  315. /* Make connection to zebra daemon. */
  316. int
  317. zclient_start (struct zclient *zclient)
  318. {
  319. if (zclient_debug)
  320. zlog_debug ("zclient_start is called");
  321. /* zclient is disabled. */
  322. if (! zclient->enable)
  323. return 0;
  324. /* If already connected to the zebra. */
  325. if (zclient->sock >= 0)
  326. return 0;
  327. /* Check connect thread. */
  328. if (zclient->t_connect)
  329. return 0;
  330. if (zclient_socket_connect(zclient) < 0)
  331. {
  332. if (zclient_debug)
  333. zlog_debug ("zclient connection fail");
  334. zclient->fail++;
  335. zclient_event (ZCLIENT_CONNECT, zclient);
  336. return -1;
  337. }
  338. if (set_nonblocking(zclient->sock) < 0)
  339. zlog_warn("%s: set_nonblocking(%d) failed", __func__, zclient->sock);
  340. /* Clear fail count. */
  341. zclient->fail = 0;
  342. if (zclient_debug)
  343. zlog_debug ("zclient connect success with socket [%d]", zclient->sock);
  344. /* Create read thread. */
  345. zclient_event (ZCLIENT_READ, zclient);
  346. zebra_hello_send (zclient);
  347. /* Inform the successful connection. */
  348. if (zclient->zebra_connected)
  349. (*zclient->zebra_connected) (zclient);
  350. return 0;
  351. }
  352. /* This function is a wrapper function for calling zclient_start from
  353. timer or event thread. */
  354. static int
  355. zclient_connect (struct thread *t)
  356. {
  357. struct zclient *zclient;
  358. zclient = THREAD_ARG (t);
  359. zclient->t_connect = NULL;
  360. if (zclient_debug)
  361. zlog_debug ("zclient_connect is called");
  362. return zclient_start (zclient);
  363. }
  364. /*
  365. * "xdr_encode"-like interface that allows daemon (client) to send
  366. * a message to zebra server for a route that needs to be
  367. * added/deleted to the kernel. Info about the route is specified
  368. * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
  369. * the info down the zclient socket using the stream_* functions.
  370. *
  371. * The corresponding read ("xdr_decode") function on the server
  372. * side is zread_ipv4_add()/zread_ipv4_delete().
  373. *
  374. * 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
  375. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  376. * | Length (2) | Command | Route Type |
  377. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  378. * | ZEBRA Flags | Message Flags | Prefix length |
  379. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  380. * | Destination IPv4 Prefix for route |
  381. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  382. * | Nexthop count |
  383. * +-+-+-+-+-+-+-+-+
  384. *
  385. *
  386. * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
  387. * described, as per the Nexthop count. Each nexthop described as:
  388. *
  389. * +-+-+-+-+-+-+-+-+
  390. * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
  391. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  392. * | IPv4 Nexthop address or Interface Index number |
  393. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  394. *
  395. * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or
  396. * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_
  397. * nexthop information is provided, and the message describes a prefix
  398. * to blackhole or reject route.
  399. *
  400. * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
  401. * byte value.
  402. *
  403. * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
  404. * byte value.
  405. *
  406. * XXX: No attention paid to alignment.
  407. */
  408. int
  409. zapi_ipv4_route (u_char cmd, struct zclient *zclient, struct prefix_ipv4 *p,
  410. struct zapi_ipv4 *api)
  411. {
  412. int i;
  413. int psize;
  414. struct stream *s;
  415. /* Reset stream. */
  416. s = zclient->obuf;
  417. stream_reset (s);
  418. zclient_create_header (s, cmd, api->vrf_id);
  419. /* Put type and nexthop. */
  420. stream_putc (s, api->type);
  421. stream_putc (s, api->flags);
  422. stream_putc (s, api->message);
  423. stream_putw (s, api->safi);
  424. /* Put prefix information. */
  425. psize = PSIZE (p->prefixlen);
  426. stream_putc (s, p->prefixlen);
  427. stream_write (s, (u_char *) & p->prefix, psize);
  428. /* Nexthop, ifindex, distance and metric information. */
  429. if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
  430. {
  431. if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE))
  432. {
  433. stream_putc (s, 1);
  434. stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE);
  435. /* XXX assert(api->nexthop_num == 0); */
  436. /* XXX assert(api->ifindex_num == 0); */
  437. }
  438. else
  439. stream_putc (s, api->nexthop_num + api->ifindex_num);
  440. for (i = 0; i < api->nexthop_num; i++)
  441. {
  442. stream_putc (s, ZEBRA_NEXTHOP_IPV4);
  443. stream_put_in_addr (s, api->nexthop[i]);
  444. }
  445. for (i = 0; i < api->ifindex_num; i++)
  446. {
  447. stream_putc (s, ZEBRA_NEXTHOP_IFINDEX);
  448. stream_putl (s, api->ifindex[i]);
  449. }
  450. }
  451. if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE))
  452. stream_putc (s, api->distance);
  453. if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC))
  454. stream_putl (s, api->metric);
  455. /* Put length at the first point of the stream. */
  456. stream_putw_at (s, 0, stream_get_endp (s));
  457. return zclient_send_message(zclient);
  458. }
  459. #ifdef HAVE_IPV6
  460. int
  461. zapi_ipv6_route (u_char cmd, struct zclient *zclient, struct prefix_ipv6 *p,
  462. struct zapi_ipv6 *api)
  463. {
  464. int i;
  465. int psize;
  466. struct stream *s;
  467. /* Reset stream. */
  468. s = zclient->obuf;
  469. stream_reset (s);
  470. zclient_create_header (s, cmd, api->vrf_id);
  471. /* Put type and nexthop. */
  472. stream_putc (s, api->type);
  473. stream_putc (s, api->flags);
  474. stream_putc (s, api->message);
  475. stream_putw (s, api->safi);
  476. /* Put prefix information. */
  477. psize = PSIZE (p->prefixlen);
  478. stream_putc (s, p->prefixlen);
  479. stream_write (s, (u_char *)&p->prefix, psize);
  480. /* Nexthop, ifindex, distance and metric information. */
  481. if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP))
  482. {
  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_IPV6);
  487. stream_write (s, (u_char *)api->nexthop[i], 16);
  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. /* 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. #endif /* HAVE_IPV6 */
  504. /*
  505. * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
  506. * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
  507. * then set/unset redist[type] in the client handle (a struct zserv) for the
  508. * sending client
  509. */
  510. int
  511. zebra_redistribute_send (int command, struct zclient *zclient, int type,
  512. vrf_id_t vrf_id)
  513. {
  514. struct stream *s;
  515. s = zclient->obuf;
  516. stream_reset(s);
  517. zclient_create_header (s, command, vrf_id);
  518. stream_putc (s, type);
  519. stream_putw_at (s, 0, stream_get_endp (s));
  520. return zclient_send_message(zclient);
  521. }
  522. /* Router-id update from zebra daemon. */
  523. void
  524. zebra_router_id_update_read (struct stream *s, struct prefix *rid)
  525. {
  526. int plen;
  527. /* Fetch interface address. */
  528. rid->family = stream_getc (s);
  529. plen = prefix_blen (rid);
  530. stream_get (&rid->u.prefix, s, plen);
  531. rid->prefixlen = stream_getc (s);
  532. }
  533. /* Interface addition from zebra daemon. */
  534. /*
  535. * The format of the message sent with type ZEBRA_INTERFACE_ADD or
  536. * ZEBRA_INTERFACE_DELETE from zebra to the client is:
  537. * 0 1 2 3
  538. * 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
  539. * +-+-+-+-+-+-+-+-+
  540. * | type |
  541. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  542. * | ifname |
  543. * | |
  544. * | |
  545. * | |
  546. * | |
  547. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  548. * | ifindex |
  549. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  550. * | if_flags |
  551. * | |
  552. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  553. * | metric |
  554. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  555. * | ifmtu |
  556. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  557. * | ifmtu6 |
  558. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  559. * | bandwidth |
  560. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  561. * | sockaddr_dl |
  562. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  563. */
  564. struct interface *
  565. zebra_interface_add_read (struct stream *s, vrf_id_t vrf_id)
  566. {
  567. struct interface *ifp;
  568. char ifname_tmp[INTERFACE_NAMSIZ];
  569. /* Read interface name. */
  570. stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
  571. /* Lookup/create interface by name. */
  572. ifp = if_get_by_name_len_vrf (ifname_tmp,
  573. strnlen (ifname_tmp, INTERFACE_NAMSIZ),
  574. vrf_id);
  575. zebra_interface_if_set_value (s, ifp);
  576. return ifp;
  577. }
  578. /*
  579. * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
  580. * from zebra server. The format of this message is the same as
  581. * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
  582. * comments for zebra_interface_add_read), except that no sockaddr_dl
  583. * is sent at the tail of the message.
  584. */
  585. struct interface *
  586. zebra_interface_state_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 this by interface index. */
  593. ifp = if_lookup_by_name_len_vrf (ifname_tmp,
  594. strnlen (ifname_tmp, INTERFACE_NAMSIZ),
  595. vrf_id);
  596. /* If such interface does not exist, indicate an error */
  597. if (! ifp)
  598. return NULL;
  599. zebra_interface_if_set_value (s, ifp);
  600. return ifp;
  601. }
  602. /*
  603. * format of message for address additon is:
  604. * 0
  605. * 0 1 2 3 4 5 6 7
  606. * +-+-+-+-+-+-+-+-+
  607. * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
  608. * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
  609. * | |
  610. * + +
  611. * | ifindex |
  612. * + +
  613. * | |
  614. * + +
  615. * | |
  616. * +-+-+-+-+-+-+-+-+
  617. * | ifc_flags | flags for connected address
  618. * +-+-+-+-+-+-+-+-+
  619. * | addr_family |
  620. * +-+-+-+-+-+-+-+-+
  621. * | addr... |
  622. * : :
  623. * | |
  624. * +-+-+-+-+-+-+-+-+
  625. * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
  626. * +-+-+-+-+-+-+-+-+
  627. * | daddr.. |
  628. * : :
  629. * | |
  630. * +-+-+-+-+-+-+-+-+
  631. *
  632. */
  633. void
  634. zebra_interface_if_set_value (struct stream *s, struct interface *ifp)
  635. {
  636. /* Read interface's index. */
  637. ifp->ifindex = stream_getl (s);
  638. ifp->status = stream_getc (s);
  639. /* Read interface's value. */
  640. ifp->flags = stream_getq (s);
  641. ifp->metric = stream_getl (s);
  642. ifp->mtu = stream_getl (s);
  643. ifp->mtu6 = stream_getl (s);
  644. ifp->bandwidth = stream_getl (s);
  645. #ifdef HAVE_STRUCT_SOCKADDR_DL
  646. stream_get (&ifp->sdl, s, sizeof (ifp->sdl_storage));
  647. #else
  648. ifp->hw_addr_len = stream_getl (s);
  649. if (ifp->hw_addr_len)
  650. stream_get (ifp->hw_addr, s, ifp->hw_addr_len);
  651. #endif /* HAVE_STRUCT_SOCKADDR_DL */
  652. }
  653. static int
  654. memconstant(const void *s, int c, size_t n)
  655. {
  656. const u_char *p = s;
  657. while (n-- > 0)
  658. if (*p++ != c)
  659. return 0;
  660. return 1;
  661. }
  662. struct connected *
  663. zebra_interface_address_read (int type, struct stream *s, vrf_id_t vrf_id)
  664. {
  665. unsigned int ifindex;
  666. struct interface *ifp;
  667. struct connected *ifc;
  668. struct prefix p, d;
  669. int family;
  670. int plen;
  671. u_char ifc_flags;
  672. memset (&p, 0, sizeof(p));
  673. memset (&d, 0, sizeof(d));
  674. /* Get interface index. */
  675. ifindex = stream_getl (s);
  676. /* Lookup index. */
  677. ifp = if_lookup_by_index_vrf (ifindex, vrf_id);
  678. if (ifp == NULL)
  679. {
  680. zlog_warn ("zebra_interface_address_read(%s): "
  681. "Can't find interface by ifindex: %d ",
  682. (type == ZEBRA_INTERFACE_ADDRESS_ADD? "ADD" : "DELETE"),
  683. ifindex);
  684. return NULL;
  685. }
  686. /* Fetch flag. */
  687. ifc_flags = stream_getc (s);
  688. /* Fetch interface address. */
  689. family = p.family = stream_getc (s);
  690. plen = prefix_blen (&p);
  691. stream_get (&p.u.prefix, s, plen);
  692. p.prefixlen = stream_getc (s);
  693. /* Fetch destination address. */
  694. stream_get (&d.u.prefix, s, plen);
  695. d.family = family;
  696. if (type == ZEBRA_INTERFACE_ADDRESS_ADD)
  697. {
  698. /* N.B. NULL destination pointers are encoded as all zeroes */
  699. ifc = connected_add_by_prefix(ifp, &p,(memconstant(&d.u.prefix,0,plen) ?
  700. NULL : &d));
  701. if (ifc != NULL)
  702. {
  703. ifc->flags = ifc_flags;
  704. if (ifc->destination)
  705. ifc->destination->prefixlen = ifc->address->prefixlen;
  706. else if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER))
  707. {
  708. /* carp interfaces on OpenBSD with 0.0.0.0/0 as "peer" */
  709. char buf[PREFIX_STRLEN];
  710. zlog_warn("warning: interface %s address %s "
  711. "with peer flag set, but no peer address!",
  712. ifp->name,
  713. prefix2str (ifc->address, buf, sizeof buf));
  714. UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
  715. }
  716. }
  717. }
  718. else
  719. {
  720. assert (type == ZEBRA_INTERFACE_ADDRESS_DELETE);
  721. ifc = connected_delete_by_prefix(ifp, &p);
  722. }
  723. return ifc;
  724. }
  725. /* Zebra client message read function. */
  726. static int
  727. zclient_read (struct thread *thread)
  728. {
  729. size_t already;
  730. uint16_t length, command;
  731. uint8_t marker, version;
  732. vrf_id_t vrf_id;
  733. struct zclient *zclient;
  734. /* Get socket to zebra. */
  735. zclient = THREAD_ARG (thread);
  736. zclient->t_read = NULL;
  737. /* Read zebra header (if we don't have it already). */
  738. if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE)
  739. {
  740. ssize_t nbyte;
  741. if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
  742. ZEBRA_HEADER_SIZE-already)) == 0) ||
  743. (nbyte == -1))
  744. {
  745. if (zclient_debug)
  746. zlog_debug ("zclient connection closed socket [%d].", zclient->sock);
  747. return zclient_failed(zclient);
  748. }
  749. if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
  750. {
  751. /* Try again later. */
  752. zclient_event (ZCLIENT_READ, zclient);
  753. return 0;
  754. }
  755. already = ZEBRA_HEADER_SIZE;
  756. }
  757. /* Reset to read from the beginning of the incoming packet. */
  758. stream_set_getp(zclient->ibuf, 0);
  759. /* Fetch header values. */
  760. length = stream_getw (zclient->ibuf);
  761. marker = stream_getc (zclient->ibuf);
  762. version = stream_getc (zclient->ibuf);
  763. vrf_id = stream_getw (zclient->ibuf);
  764. command = stream_getw (zclient->ibuf);
  765. if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
  766. {
  767. zlog_err("%s: socket %d version mismatch, marker %d, version %d",
  768. __func__, zclient->sock, marker, version);
  769. return zclient_failed(zclient);
  770. }
  771. if (length < ZEBRA_HEADER_SIZE)
  772. {
  773. zlog_err("%s: socket %d message length %u is less than %d ",
  774. __func__, zclient->sock, length, ZEBRA_HEADER_SIZE);
  775. return zclient_failed(zclient);
  776. }
  777. /* Length check. */
  778. if (length > STREAM_SIZE(zclient->ibuf))
  779. {
  780. struct stream *ns;
  781. zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...",
  782. __func__, length, (u_long)STREAM_SIZE(zclient->ibuf));
  783. ns = stream_new(length);
  784. stream_copy(ns, zclient->ibuf);
  785. stream_free (zclient->ibuf);
  786. zclient->ibuf = ns;
  787. }
  788. /* Read rest of zebra packet. */
  789. if (already < length)
  790. {
  791. ssize_t nbyte;
  792. if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock,
  793. length-already)) == 0) ||
  794. (nbyte == -1))
  795. {
  796. if (zclient_debug)
  797. zlog_debug("zclient connection closed socket [%d].", zclient->sock);
  798. return zclient_failed(zclient);
  799. }
  800. if (nbyte != (ssize_t)(length-already))
  801. {
  802. /* Try again later. */
  803. zclient_event (ZCLIENT_READ, zclient);
  804. return 0;
  805. }
  806. }
  807. length -= ZEBRA_HEADER_SIZE;
  808. if (zclient_debug)
  809. zlog_debug("zclient 0x%p command 0x%x VRF %u\n", (void *)zclient, command, vrf_id);
  810. switch (command)
  811. {
  812. case ZEBRA_ROUTER_ID_UPDATE:
  813. if (zclient->router_id_update)
  814. (*zclient->router_id_update) (command, zclient, length, vrf_id);
  815. break;
  816. case ZEBRA_INTERFACE_ADD:
  817. if (zclient->interface_add)
  818. (*zclient->interface_add) (command, zclient, length, vrf_id);
  819. break;
  820. case ZEBRA_INTERFACE_DELETE:
  821. if (zclient->interface_delete)
  822. (*zclient->interface_delete) (command, zclient, length, vrf_id);
  823. break;
  824. case ZEBRA_INTERFACE_ADDRESS_ADD:
  825. if (zclient->interface_address_add)
  826. (*zclient->interface_address_add) (command, zclient, length, vrf_id);
  827. break;
  828. case ZEBRA_INTERFACE_ADDRESS_DELETE:
  829. if (zclient->interface_address_delete)
  830. (*zclient->interface_address_delete) (command, zclient, length, vrf_id);
  831. break;
  832. case ZEBRA_INTERFACE_UP:
  833. if (zclient->interface_up)
  834. (*zclient->interface_up) (command, zclient, length, vrf_id);
  835. break;
  836. case ZEBRA_INTERFACE_DOWN:
  837. if (zclient->interface_down)
  838. (*zclient->interface_down) (command, zclient, length, vrf_id);
  839. break;
  840. case ZEBRA_IPV4_ROUTE_ADD:
  841. if (zclient->ipv4_route_add)
  842. (*zclient->ipv4_route_add) (command, zclient, length, vrf_id);
  843. break;
  844. case ZEBRA_IPV4_ROUTE_DELETE:
  845. if (zclient->ipv4_route_delete)
  846. (*zclient->ipv4_route_delete) (command, zclient, length, vrf_id);
  847. break;
  848. case ZEBRA_IPV6_ROUTE_ADD:
  849. if (zclient->ipv6_route_add)
  850. (*zclient->ipv6_route_add) (command, zclient, length, vrf_id);
  851. break;
  852. case ZEBRA_IPV6_ROUTE_DELETE:
  853. if (zclient->ipv6_route_delete)
  854. (*zclient->ipv6_route_delete) (command, zclient, length, vrf_id);
  855. break;
  856. default:
  857. break;
  858. }
  859. if (zclient->sock < 0)
  860. /* Connection was closed during packet processing. */
  861. return -1;
  862. /* Register read thread. */
  863. stream_reset(zclient->ibuf);
  864. zclient_event (ZCLIENT_READ, zclient);
  865. return 0;
  866. }
  867. void
  868. zclient_redistribute (int command, struct zclient *zclient, int type,
  869. vrf_id_t vrf_id)
  870. {
  871. if (command == ZEBRA_REDISTRIBUTE_ADD)
  872. {
  873. if (vrf_bitmap_check (zclient->redist[type], vrf_id))
  874. return;
  875. vrf_bitmap_set (zclient->redist[type], vrf_id);
  876. }
  877. else
  878. {
  879. if (!vrf_bitmap_check (zclient->redist[type], vrf_id))
  880. return;
  881. vrf_bitmap_unset (zclient->redist[type], vrf_id);
  882. }
  883. if (zclient->sock > 0)
  884. zebra_redistribute_send (command, zclient, type, vrf_id);
  885. }
  886. void
  887. zclient_redistribute_default (int command, struct zclient *zclient,
  888. vrf_id_t vrf_id)
  889. {
  890. if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD)
  891. {
  892. if (vrf_bitmap_check (zclient->default_information, vrf_id))
  893. return;
  894. vrf_bitmap_set (zclient->default_information, vrf_id);
  895. }
  896. else
  897. {
  898. if (!vrf_bitmap_check (zclient->default_information, vrf_id))
  899. return;
  900. vrf_bitmap_unset (zclient->default_information, vrf_id);
  901. }
  902. if (zclient->sock > 0)
  903. zebra_message_send (zclient, command, vrf_id);
  904. }
  905. static void
  906. zclient_event (enum event event, struct zclient *zclient)
  907. {
  908. switch (event)
  909. {
  910. case ZCLIENT_SCHEDULE:
  911. if (! zclient->t_connect)
  912. zclient->t_connect =
  913. thread_add_event (master, zclient_connect, zclient, 0);
  914. break;
  915. case ZCLIENT_CONNECT:
  916. if (zclient->fail >= 10)
  917. return;
  918. if (zclient_debug)
  919. zlog_debug ("zclient connect schedule interval is %d",
  920. zclient->fail < 3 ? 10 : 60);
  921. if (! zclient->t_connect)
  922. zclient->t_connect =
  923. thread_add_timer (master, zclient_connect, zclient,
  924. zclient->fail < 3 ? 10 : 60);
  925. break;
  926. case ZCLIENT_READ:
  927. zclient->t_read =
  928. thread_add_read (master, zclient_read, zclient, zclient->sock);
  929. break;
  930. }
  931. }
  932. const char *zclient_serv_path_get()
  933. {
  934. return zclient_serv_path ? zclient_serv_path : ZEBRA_SERV_PATH;
  935. }
  936. void
  937. zclient_serv_path_set (char *path)
  938. {
  939. struct stat sb;
  940. /* reset */
  941. zclient_serv_path = NULL;
  942. /* test if `path' is socket. don't set it otherwise. */
  943. if (stat(path, &sb) == -1)
  944. {
  945. zlog_warn ("%s: zebra socket `%s' does not exist", __func__, path);
  946. return;
  947. }
  948. if ((sb.st_mode & S_IFMT) != S_IFSOCK)
  949. {
  950. zlog_warn ("%s: `%s' is not unix socket, sir", __func__, path);
  951. return;
  952. }
  953. /* it seems that path is unix socket */
  954. zclient_serv_path = path;
  955. }