zclient.c 27 KB

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