zclient.c 28 KB

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