zclient.c 27 KB

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