zclient.c 30 KB

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