zclient.c 27 KB

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