zserv.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968
  1. /* Zebra daemon server routine.
  2. * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
  3. *
  4. * This file is part of GNU Zebra.
  5. *
  6. * GNU Zebra is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2, or (at your option) any
  9. * later version.
  10. *
  11. * GNU Zebra is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GNU Zebra; see the file COPYING. If not, write to the
  18. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. */
  21. #include <zebra.h>
  22. #include "prefix.h"
  23. #include "command.h"
  24. #include "if.h"
  25. #include "thread.h"
  26. #include "stream.h"
  27. #include "memory.h"
  28. #include "table.h"
  29. #include "rib.h"
  30. #include "network.h"
  31. #include "sockunion.h"
  32. #include "log.h"
  33. #include "zclient.h"
  34. #include "privs.h"
  35. #include "network.h"
  36. #include "buffer.h"
  37. #include "vrf.h"
  38. #include "zebra/zserv.h"
  39. #include "zebra/router-id.h"
  40. #include "zebra/redistribute.h"
  41. #include "zebra/debug.h"
  42. #include "zebra/ipforward.h"
  43. /* Event list of zebra. */
  44. enum event { ZEBRA_SERV, ZEBRA_READ, ZEBRA_WRITE };
  45. extern struct zebra_t zebrad;
  46. static void zebra_event (enum event event, int sock, struct zserv *client);
  47. extern struct zebra_privs_t zserv_privs;
  48. static void zebra_client_close (struct zserv *client);
  49. static int
  50. zserv_delayed_close(struct thread *thread)
  51. {
  52. struct zserv *client = THREAD_ARG(thread);
  53. client->t_suicide = NULL;
  54. zebra_client_close(client);
  55. return 0;
  56. }
  57. /* When client connects, it sends hello message
  58. * with promise to send zebra routes of specific type.
  59. * Zebra stores a socket fd of the client into
  60. * this array. And use it to clean up routes that
  61. * client didn't remove for some reasons after closing
  62. * connection.
  63. */
  64. static int route_type_oaths[ZEBRA_ROUTE_MAX];
  65. static int
  66. zserv_flush_data(struct thread *thread)
  67. {
  68. struct zserv *client = THREAD_ARG(thread);
  69. client->t_write = NULL;
  70. if (client->t_suicide)
  71. {
  72. zebra_client_close(client);
  73. return -1;
  74. }
  75. switch (buffer_flush_available(client->wb, client->sock))
  76. {
  77. case BUFFER_ERROR:
  78. zlog_warn("%s: buffer_flush_available failed on zserv client fd %d, "
  79. "closing", __func__, client->sock);
  80. zebra_client_close(client);
  81. break;
  82. case BUFFER_PENDING:
  83. client->t_write = thread_add_write(zebrad.master, zserv_flush_data,
  84. client, client->sock);
  85. break;
  86. case BUFFER_EMPTY:
  87. break;
  88. }
  89. return 0;
  90. }
  91. static int
  92. zebra_server_send_message(struct zserv *client)
  93. {
  94. if (client->t_suicide)
  95. return -1;
  96. switch (buffer_write(client->wb, client->sock, STREAM_DATA(client->obuf),
  97. stream_get_endp(client->obuf)))
  98. {
  99. case BUFFER_ERROR:
  100. zlog_warn("%s: buffer_write failed to zserv client fd %d, closing",
  101. __func__, client->sock);
  102. /* Schedule a delayed close since many of the functions that call this
  103. one do not check the return code. They do not allow for the
  104. possibility that an I/O error may have caused the client to be
  105. deleted. */
  106. client->t_suicide = thread_add_event(zebrad.master, zserv_delayed_close,
  107. client, 0);
  108. return -1;
  109. case BUFFER_EMPTY:
  110. THREAD_OFF(client->t_write);
  111. break;
  112. case BUFFER_PENDING:
  113. THREAD_WRITE_ON(zebrad.master, client->t_write,
  114. zserv_flush_data, client, client->sock);
  115. break;
  116. }
  117. return 0;
  118. }
  119. static void
  120. zserv_create_header (struct stream *s, uint16_t cmd, vrf_id_t vrf_id)
  121. {
  122. /* length placeholder, caller can update */
  123. stream_putw (s, ZEBRA_HEADER_SIZE);
  124. stream_putc (s, ZEBRA_HEADER_MARKER);
  125. stream_putc (s, ZSERV_VERSION);
  126. stream_putw (s, vrf_id);
  127. stream_putw (s, cmd);
  128. }
  129. static void
  130. zserv_encode_interface (struct stream *s, struct interface *ifp)
  131. {
  132. /* Interface information. */
  133. stream_put (s, ifp->name, INTERFACE_NAMSIZ);
  134. stream_putl (s, ifp->ifindex);
  135. stream_putc (s, ifp->status);
  136. stream_putq (s, ifp->flags);
  137. stream_putl (s, ifp->metric);
  138. stream_putl (s, ifp->mtu);
  139. stream_putl (s, ifp->mtu6);
  140. stream_putl (s, ifp->bandwidth);
  141. stream_putl (s, ifp->ll_type);
  142. stream_putl (s, ifp->hw_addr_len);
  143. if (ifp->hw_addr_len)
  144. stream_put (s, ifp->hw_addr, ifp->hw_addr_len);
  145. /* Write packet size. */
  146. stream_putw_at (s, 0, stream_get_endp (s));
  147. }
  148. /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
  149. /*
  150. * This function is called in the following situations:
  151. * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
  152. * from the client.
  153. * - at startup, when zebra figures out the available interfaces
  154. * - when an interface is added (where support for
  155. * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
  156. * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
  157. * received)
  158. */
  159. int
  160. zsend_interface_add (struct zserv *client, struct interface *ifp)
  161. {
  162. struct stream *s;
  163. /* Check this client need interface information. */
  164. if (! vrf_bitmap_check (client->ifinfo, ifp->vrf_id))
  165. return 0;
  166. s = client->obuf;
  167. stream_reset (s);
  168. zserv_create_header (s, ZEBRA_INTERFACE_ADD, ifp->vrf_id);
  169. zserv_encode_interface (s, ifp);
  170. return zebra_server_send_message(client);
  171. }
  172. /* Interface deletion from zebra daemon. */
  173. int
  174. zsend_interface_delete (struct zserv *client, struct interface *ifp)
  175. {
  176. struct stream *s;
  177. /* Check this client need interface information. */
  178. if (! vrf_bitmap_check (client->ifinfo, ifp->vrf_id))
  179. return 0;
  180. s = client->obuf;
  181. stream_reset (s);
  182. zserv_create_header (s, ZEBRA_INTERFACE_DELETE, ifp->vrf_id);
  183. zserv_encode_interface (s, ifp);
  184. return zebra_server_send_message (client);
  185. }
  186. /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
  187. * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
  188. *
  189. * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
  190. * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
  191. * from the client, after the ZEBRA_INTERFACE_ADD has been
  192. * sent from zebra to the client
  193. * - redistribute new address info to all clients in the following situations
  194. * - at startup, when zebra figures out the available interfaces
  195. * - when an interface is added (where support for
  196. * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
  197. * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
  198. * received)
  199. * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
  200. * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
  201. * - when an RTM_NEWADDR message is received from the kernel,
  202. *
  203. * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
  204. *
  205. * zsend_interface_address(DELETE)
  206. * ^
  207. * |
  208. * zebra_interface_address_delete_update
  209. * ^ ^ ^
  210. * | | if_delete_update
  211. * | |
  212. * ip_address_uninstall connected_delete_ipv4
  213. * [ipv6_addresss_uninstall] [connected_delete_ipv6]
  214. * ^ ^
  215. * | |
  216. * | RTM_NEWADDR on routing/netlink socket
  217. * |
  218. * vty commands:
  219. * "no ip address A.B.C.D/M [label LINE]"
  220. * "no ip address A.B.C.D/M secondary"
  221. * ["no ipv6 address X:X::X:X/M"]
  222. *
  223. */
  224. int
  225. zsend_interface_address (int cmd, struct zserv *client,
  226. struct interface *ifp, struct connected *ifc)
  227. {
  228. int blen;
  229. struct stream *s;
  230. struct prefix *p;
  231. /* Check this client need interface information. */
  232. if (! vrf_bitmap_check (client->ifinfo, ifp->vrf_id))
  233. return 0;
  234. s = client->obuf;
  235. stream_reset (s);
  236. zserv_create_header (s, cmd, ifp->vrf_id);
  237. stream_putl (s, ifp->ifindex);
  238. /* Interface address flag. */
  239. stream_putc (s, ifc->flags);
  240. /* Prefix information. */
  241. p = ifc->address;
  242. stream_putc (s, p->family);
  243. blen = prefix_blen (p);
  244. stream_put (s, &p->u.prefix, blen);
  245. /*
  246. * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
  247. * but zebra_interface_address_delete_read() in the gnu version
  248. * expects to find it
  249. */
  250. stream_putc (s, p->prefixlen);
  251. /* Destination. */
  252. p = ifc->destination;
  253. if (p)
  254. stream_put (s, &p->u.prefix, blen);
  255. else
  256. stream_put (s, NULL, blen);
  257. /* Write packet size. */
  258. stream_putw_at (s, 0, stream_get_endp (s));
  259. return zebra_server_send_message(client);
  260. }
  261. /*
  262. * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
  263. * ZEBRA_INTERFACE_DOWN.
  264. *
  265. * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
  266. * the clients in one of 2 situations:
  267. * - an if_up is detected e.g., as a result of an RTM_IFINFO message
  268. * - a vty command modifying the bandwidth of an interface is received.
  269. * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
  270. */
  271. int
  272. zsend_interface_update (int cmd, struct zserv *client, struct interface *ifp)
  273. {
  274. struct stream *s;
  275. /* Check this client need interface information. */
  276. if (! vrf_bitmap_check (client->ifinfo, ifp->vrf_id))
  277. return 0;
  278. s = client->obuf;
  279. stream_reset (s);
  280. zserv_create_header (s, cmd, ifp->vrf_id);
  281. zserv_encode_interface (s, ifp);
  282. return zebra_server_send_message(client);
  283. }
  284. /*
  285. * The zebra server sends the clients a ZEBRA_IPV4_ROUTE_ADD or a
  286. * ZEBRA_IPV6_ROUTE_ADD via zsend_route_multipath in the following
  287. * situations:
  288. * - when the client starts up, and requests default information
  289. * by sending a ZEBRA_REDISTRIBUTE_DEFAULT_ADD to the zebra server, in the
  290. * - case of rip, ripngd, ospfd and ospf6d, when the client sends a
  291. * ZEBRA_REDISTRIBUTE_ADD as a result of the "redistribute" vty cmd,
  292. * - when the zebra server redistributes routes after it updates its rib
  293. *
  294. * The zebra server sends clients a ZEBRA_IPV4_ROUTE_DELETE or a
  295. * ZEBRA_IPV6_ROUTE_DELETE via zsend_route_multipath when:
  296. * - a "ip route" or "ipv6 route" vty command is issued, a prefix is
  297. * - deleted from zebra's rib, and this info
  298. * has to be redistributed to the clients
  299. *
  300. * XXX The ZEBRA_IPV*_ROUTE_ADD message is also sent by the client to the
  301. * zebra server when the client wants to tell the zebra server to add a
  302. * route to the kernel (zapi_ipv4_add etc. ). Since it's essentially the
  303. * same message being sent back and forth, this function and
  304. * zapi_ipv{4,6}_{add, delete} should be re-written to avoid code
  305. * duplication.
  306. */
  307. int
  308. zsend_route_multipath (int cmd, struct zserv *client, struct prefix *p,
  309. struct rib *rib)
  310. {
  311. int psize;
  312. struct stream *s;
  313. struct nexthop *nexthop;
  314. unsigned long nhnummark = 0, messmark = 0;
  315. int nhnum = 0;
  316. u_char zapi_flags = 0;
  317. /* Check this client need this route. */
  318. if (!vrf_bitmap_check (client->redist[rib->type], rib->vrf_id) &&
  319. !(is_default (p) &&
  320. vrf_bitmap_check (client->redist_default, rib->vrf_id)))
  321. return 0;
  322. s = client->obuf;
  323. stream_reset (s);
  324. zserv_create_header (s, cmd, rib->vrf_id);
  325. /* Put type and nexthop. */
  326. stream_putc (s, rib->type);
  327. stream_putc (s, rib->flags);
  328. /* marker for message flags field */
  329. messmark = stream_get_endp (s);
  330. stream_putc (s, 0);
  331. /* Prefix. */
  332. psize = PSIZE (p->prefixlen);
  333. stream_putc (s, p->prefixlen);
  334. stream_write (s, (u_char *) & p->u.prefix, psize);
  335. /*
  336. * XXX The message format sent by zebra below does not match the format
  337. * of the corresponding message expected by the zebra server
  338. * itself (e.g., see zread_ipv4_add). The nexthop_num is not set correctly,
  339. * (is there a bug on the client side if more than one segment is sent?)
  340. * nexthop ZEBRA_NEXTHOP_IPV4 is never set, ZEBRA_NEXTHOP_IFINDEX
  341. * is hard-coded.
  342. */
  343. /* Nexthop */
  344. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  345. {
  346. if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
  347. {
  348. SET_FLAG (zapi_flags, ZAPI_MESSAGE_NEXTHOP);
  349. SET_FLAG (zapi_flags, ZAPI_MESSAGE_IFINDEX);
  350. if (nhnummark == 0)
  351. {
  352. nhnummark = stream_get_endp (s);
  353. stream_putc (s, 1); /* placeholder */
  354. }
  355. nhnum++;
  356. switch(nexthop->type)
  357. {
  358. case NEXTHOP_TYPE_IPV4:
  359. case NEXTHOP_TYPE_IPV4_IFINDEX:
  360. stream_put_in_addr (s, &nexthop->gate.ipv4);
  361. break;
  362. #ifdef HAVE_IPV6
  363. case NEXTHOP_TYPE_IPV6:
  364. case NEXTHOP_TYPE_IPV6_IFINDEX:
  365. case NEXTHOP_TYPE_IPV6_IFNAME:
  366. stream_write (s, (u_char *) &nexthop->gate.ipv6, 16);
  367. break;
  368. #endif
  369. default:
  370. if (cmd == ZEBRA_IPV4_ROUTE_ADD
  371. || cmd == ZEBRA_IPV4_ROUTE_DELETE)
  372. {
  373. struct in_addr empty;
  374. memset (&empty, 0, sizeof (struct in_addr));
  375. stream_write (s, (u_char *) &empty, IPV4_MAX_BYTELEN);
  376. }
  377. else
  378. {
  379. struct in6_addr empty;
  380. memset (&empty, 0, sizeof (struct in6_addr));
  381. stream_write (s, (u_char *) &empty, IPV6_MAX_BYTELEN);
  382. }
  383. }
  384. /* Interface index. */
  385. stream_putc (s, 1);
  386. stream_putl (s, nexthop->ifindex);
  387. break;
  388. }
  389. }
  390. /* Metric */
  391. if (cmd == ZEBRA_IPV4_ROUTE_ADD || cmd == ZEBRA_IPV6_ROUTE_ADD)
  392. {
  393. SET_FLAG (zapi_flags, ZAPI_MESSAGE_DISTANCE);
  394. stream_putc (s, rib->distance);
  395. SET_FLAG (zapi_flags, ZAPI_MESSAGE_METRIC);
  396. stream_putl (s, rib->metric);
  397. SET_FLAG (zapi_flags, ZAPI_MESSAGE_MTU);
  398. stream_putl (s, rib->mtu);
  399. }
  400. /* write real message flags value */
  401. stream_putc_at (s, messmark, zapi_flags);
  402. /* Write next-hop number */
  403. if (nhnummark)
  404. stream_putc_at (s, nhnummark, nhnum);
  405. /* Write packet size. */
  406. stream_putw_at (s, 0, stream_get_endp (s));
  407. return zebra_server_send_message(client);
  408. }
  409. #ifdef HAVE_IPV6
  410. static int
  411. zsend_ipv6_nexthop_lookup (struct zserv *client, struct in6_addr *addr,
  412. vrf_id_t vrf_id)
  413. {
  414. struct stream *s;
  415. struct rib *rib;
  416. unsigned long nump;
  417. u_char num;
  418. struct nexthop *nexthop;
  419. /* Lookup nexthop. */
  420. rib = rib_match_ipv6 (addr, vrf_id);
  421. /* Get output stream. */
  422. s = client->obuf;
  423. stream_reset (s);
  424. /* Fill in result. */
  425. zserv_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP, vrf_id);
  426. stream_put (s, addr, 16);
  427. if (rib)
  428. {
  429. stream_putl (s, rib->metric);
  430. num = 0;
  431. nump = stream_get_endp(s);
  432. stream_putc (s, 0);
  433. /* Only non-recursive routes are elegible to resolve nexthop we
  434. * are looking up. Therefore, we will just iterate over the top
  435. * chain of nexthops. */
  436. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  437. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
  438. {
  439. stream_putc (s, nexthop->type);
  440. switch (nexthop->type)
  441. {
  442. case ZEBRA_NEXTHOP_IPV6:
  443. stream_put (s, &nexthop->gate.ipv6, 16);
  444. break;
  445. case ZEBRA_NEXTHOP_IPV6_IFINDEX:
  446. case ZEBRA_NEXTHOP_IPV6_IFNAME:
  447. stream_put (s, &nexthop->gate.ipv6, 16);
  448. stream_putl (s, nexthop->ifindex);
  449. break;
  450. case ZEBRA_NEXTHOP_IFINDEX:
  451. case ZEBRA_NEXTHOP_IFNAME:
  452. stream_putl (s, nexthop->ifindex);
  453. break;
  454. default:
  455. /* do nothing */
  456. break;
  457. }
  458. num++;
  459. }
  460. stream_putc_at (s, nump, num);
  461. }
  462. else
  463. {
  464. stream_putl (s, 0);
  465. stream_putc (s, 0);
  466. }
  467. stream_putw_at (s, 0, stream_get_endp (s));
  468. return zebra_server_send_message(client);
  469. }
  470. #endif /* HAVE_IPV6 */
  471. static int
  472. zsend_ipv4_nexthop_lookup (struct zserv *client, struct in_addr addr,
  473. vrf_id_t vrf_id)
  474. {
  475. struct stream *s;
  476. struct rib *rib;
  477. unsigned long nump;
  478. u_char num;
  479. struct nexthop *nexthop;
  480. /* Lookup nexthop - eBGP excluded */
  481. rib = rib_match_ipv4_safi (addr, SAFI_UNICAST, 1, NULL, vrf_id);
  482. /* Get output stream. */
  483. s = client->obuf;
  484. stream_reset (s);
  485. /* Fill in result. */
  486. zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP, vrf_id);
  487. stream_put_in_addr (s, &addr);
  488. if (rib)
  489. {
  490. if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
  491. zlog_debug("%s: Matching rib entry found.", __func__);
  492. stream_putl (s, rib->metric);
  493. num = 0;
  494. nump = stream_get_endp(s);
  495. stream_putc (s, 0);
  496. /* Only non-recursive routes are elegible to resolve the nexthop we
  497. * are looking up. Therefore, we will just iterate over the top
  498. * chain of nexthops. */
  499. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  500. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
  501. {
  502. stream_putc (s, nexthop->type);
  503. switch (nexthop->type)
  504. {
  505. case ZEBRA_NEXTHOP_IPV4:
  506. stream_put_in_addr (s, &nexthop->gate.ipv4);
  507. break;
  508. case ZEBRA_NEXTHOP_IPV4_IFINDEX:
  509. stream_put_in_addr (s, &nexthop->gate.ipv4);
  510. stream_putl (s, nexthop->ifindex);
  511. break;
  512. case ZEBRA_NEXTHOP_IFINDEX:
  513. case ZEBRA_NEXTHOP_IFNAME:
  514. stream_putl (s, nexthop->ifindex);
  515. break;
  516. default:
  517. /* do nothing */
  518. break;
  519. }
  520. num++;
  521. }
  522. stream_putc_at (s, nump, num);
  523. }
  524. else
  525. {
  526. if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
  527. zlog_debug("%s: No matching rib entry found.", __func__);
  528. stream_putl (s, 0);
  529. stream_putc (s, 0);
  530. }
  531. stream_putw_at (s, 0, stream_get_endp (s));
  532. return zebra_server_send_message(client);
  533. }
  534. /*
  535. Modified version of zsend_ipv4_nexthop_lookup():
  536. Query unicast rib if nexthop is not found on mrib.
  537. Returns both route metric and protocol distance.
  538. */
  539. static int
  540. zsend_ipv4_nexthop_lookup_mrib (struct zserv *client, struct in_addr addr,
  541. struct rib *rib)
  542. {
  543. struct stream *s;
  544. unsigned long nump;
  545. u_char num;
  546. struct nexthop *nexthop;
  547. /* Get output stream. */
  548. s = client->obuf;
  549. stream_reset (s);
  550. /* Fill in result. */
  551. zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB, rib->vrf_id);
  552. stream_put_in_addr (s, &addr);
  553. if (rib)
  554. {
  555. stream_putc (s, rib->distance);
  556. stream_putl (s, rib->metric);
  557. num = 0;
  558. nump = stream_get_endp(s); /* remember position for nexthop_num */
  559. stream_putc (s, 0); /* reserve room for nexthop_num */
  560. /* Only non-recursive routes are elegible to resolve the nexthop we
  561. * are looking up. Therefore, we will just iterate over the top
  562. * chain of nexthops. */
  563. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  564. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
  565. {
  566. stream_putc (s, nexthop->type);
  567. switch (nexthop->type)
  568. {
  569. case ZEBRA_NEXTHOP_IPV4:
  570. stream_put_in_addr (s, &nexthop->gate.ipv4);
  571. break;
  572. case ZEBRA_NEXTHOP_IPV4_IFINDEX:
  573. stream_put_in_addr (s, &nexthop->gate.ipv4);
  574. stream_putl (s, nexthop->ifindex);
  575. break;
  576. case ZEBRA_NEXTHOP_IFINDEX:
  577. case ZEBRA_NEXTHOP_IFNAME:
  578. stream_putl (s, nexthop->ifindex);
  579. break;
  580. default:
  581. /* do nothing */
  582. break;
  583. }
  584. num++;
  585. }
  586. stream_putc_at (s, nump, num); /* store nexthop_num */
  587. }
  588. else
  589. {
  590. stream_putc (s, 0); /* distance */
  591. stream_putl (s, 0); /* metric */
  592. stream_putc (s, 0); /* nexthop_num */
  593. }
  594. stream_putw_at (s, 0, stream_get_endp (s));
  595. return zebra_server_send_message(client);
  596. }
  597. static int
  598. zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p,
  599. vrf_id_t vrf_id)
  600. {
  601. struct stream *s;
  602. struct rib *rib;
  603. unsigned long nump;
  604. u_char num;
  605. struct nexthop *nexthop;
  606. /* Lookup nexthop. */
  607. rib = rib_lookup_ipv4 (p, vrf_id);
  608. /* Get output stream. */
  609. s = client->obuf;
  610. stream_reset (s);
  611. /* Fill in result. */
  612. zserv_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP, vrf_id);
  613. stream_put_in_addr (s, &p->prefix);
  614. if (rib)
  615. {
  616. stream_putl (s, rib->metric);
  617. num = 0;
  618. nump = stream_get_endp(s);
  619. stream_putc (s, 0);
  620. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  621. if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
  622. {
  623. stream_putc (s, nexthop->type);
  624. switch (nexthop->type)
  625. {
  626. case ZEBRA_NEXTHOP_IPV4:
  627. stream_put_in_addr (s, &nexthop->gate.ipv4);
  628. break;
  629. case ZEBRA_NEXTHOP_IPV4_IFINDEX:
  630. stream_put_in_addr (s, &nexthop->gate.ipv4);
  631. stream_putl (s, nexthop->ifindex);
  632. break;
  633. case ZEBRA_NEXTHOP_IFINDEX:
  634. case ZEBRA_NEXTHOP_IFNAME:
  635. stream_putl (s, nexthop->ifindex);
  636. break;
  637. default:
  638. /* do nothing */
  639. break;
  640. }
  641. num++;
  642. }
  643. stream_putc_at (s, nump, num);
  644. }
  645. else
  646. {
  647. stream_putl (s, 0);
  648. stream_putc (s, 0);
  649. }
  650. stream_putw_at (s, 0, stream_get_endp (s));
  651. return zebra_server_send_message(client);
  652. }
  653. /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
  654. int
  655. zsend_router_id_update (struct zserv *client, struct prefix *p,
  656. vrf_id_t vrf_id)
  657. {
  658. struct stream *s;
  659. int blen;
  660. /* Check this client need interface information. */
  661. if (! vrf_bitmap_check (client->ridinfo, vrf_id))
  662. return 0;
  663. s = client->obuf;
  664. stream_reset (s);
  665. /* Message type. */
  666. zserv_create_header (s, ZEBRA_ROUTER_ID_UPDATE, vrf_id);
  667. /* Prefix information. */
  668. stream_putc (s, p->family);
  669. blen = prefix_blen (p);
  670. stream_put (s, &p->u.prefix, blen);
  671. stream_putc (s, p->prefixlen);
  672. /* Write packet size. */
  673. stream_putw_at (s, 0, stream_get_endp (s));
  674. return zebra_server_send_message(client);
  675. }
  676. /* Register zebra server interface information. Send current all
  677. interface and address information. */
  678. static int
  679. zread_interface_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
  680. {
  681. struct listnode *ifnode, *ifnnode;
  682. struct listnode *cnode, *cnnode;
  683. struct interface *ifp;
  684. struct connected *c;
  685. /* Interface information is needed. */
  686. vrf_bitmap_set (client->ifinfo, vrf_id);
  687. for (ALL_LIST_ELEMENTS (vrf_iflist (vrf_id), ifnode, ifnnode, ifp))
  688. {
  689. /* Skip pseudo interface. */
  690. if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
  691. continue;
  692. if (zsend_interface_add (client, ifp) < 0)
  693. return -1;
  694. for (ALL_LIST_ELEMENTS (ifp->connected, cnode, cnnode, c))
  695. {
  696. if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL) &&
  697. (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client,
  698. ifp, c) < 0))
  699. return -1;
  700. }
  701. }
  702. return 0;
  703. }
  704. /* Unregister zebra server interface information. */
  705. static int
  706. zread_interface_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
  707. {
  708. vrf_bitmap_unset (client->ifinfo, vrf_id);
  709. return 0;
  710. }
  711. /* This function support multiple nexthop. */
  712. /*
  713. * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
  714. * add kernel route.
  715. */
  716. static int
  717. zread_ipv4_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
  718. {
  719. int i;
  720. struct rib *rib;
  721. struct prefix_ipv4 p;
  722. u_char message;
  723. struct in_addr nexthop;
  724. u_char nexthop_num;
  725. u_char nexthop_type;
  726. struct stream *s;
  727. ifindex_t ifindex;
  728. u_char ifname_len;
  729. safi_t safi;
  730. /* Get input stream. */
  731. s = client->ibuf;
  732. /* Allocate new rib. */
  733. rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
  734. /* Type, flags, message. */
  735. rib->type = stream_getc (s);
  736. rib->flags = stream_getc (s);
  737. message = stream_getc (s);
  738. safi = stream_getw (s);
  739. rib->uptime = time (NULL);
  740. /* IPv4 prefix. */
  741. memset (&p, 0, sizeof (struct prefix_ipv4));
  742. p.family = AF_INET;
  743. p.prefixlen = stream_getc (s);
  744. stream_get (&p.prefix, s, PSIZE (p.prefixlen));
  745. /* VRF ID */
  746. rib->vrf_id = vrf_id;
  747. /* Nexthop parse. */
  748. if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
  749. {
  750. nexthop_num = stream_getc (s);
  751. for (i = 0; i < nexthop_num; i++)
  752. {
  753. nexthop_type = stream_getc (s);
  754. switch (nexthop_type)
  755. {
  756. case ZEBRA_NEXTHOP_IFINDEX:
  757. ifindex = stream_getl (s);
  758. nexthop_ifindex_add (rib, ifindex);
  759. break;
  760. case ZEBRA_NEXTHOP_IFNAME:
  761. ifname_len = stream_getc (s);
  762. stream_forward_getp (s, ifname_len);
  763. break;
  764. case ZEBRA_NEXTHOP_IPV4:
  765. nexthop.s_addr = stream_get_ipv4 (s);
  766. nexthop_ipv4_add (rib, &nexthop, NULL);
  767. break;
  768. case ZEBRA_NEXTHOP_IPV4_IFINDEX:
  769. nexthop.s_addr = stream_get_ipv4 (s);
  770. ifindex = stream_getl (s);
  771. nexthop_ipv4_ifindex_add (rib, &nexthop, NULL, ifindex);
  772. break;
  773. case ZEBRA_NEXTHOP_IPV6:
  774. stream_forward_getp (s, IPV6_MAX_BYTELEN);
  775. break;
  776. case ZEBRA_NEXTHOP_BLACKHOLE:
  777. nexthop_blackhole_add (rib);
  778. break;
  779. }
  780. }
  781. }
  782. /* Distance. */
  783. if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
  784. rib->distance = stream_getc (s);
  785. /* Metric. */
  786. if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
  787. rib->metric = stream_getl (s);
  788. if (CHECK_FLAG (message, ZAPI_MESSAGE_MTU))
  789. rib->mtu = stream_getl (s);
  790. /* Table */
  791. rib->table=zebrad.rtm_table_default;
  792. rib_add_ipv4_multipath (&p, rib, safi);
  793. return 0;
  794. }
  795. /* Zebra server IPv4 prefix delete function. */
  796. static int
  797. zread_ipv4_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
  798. {
  799. int i;
  800. struct stream *s;
  801. struct zapi_ipv4 api;
  802. struct in_addr nexthop, *nexthop_p;
  803. unsigned long ifindex;
  804. struct prefix_ipv4 p;
  805. u_char nexthop_num;
  806. u_char nexthop_type;
  807. u_char ifname_len;
  808. s = client->ibuf;
  809. ifindex = 0;
  810. nexthop.s_addr = 0;
  811. nexthop_p = NULL;
  812. /* Type, flags, message. */
  813. api.type = stream_getc (s);
  814. api.flags = stream_getc (s);
  815. api.message = stream_getc (s);
  816. api.safi = stream_getw (s);
  817. /* IPv4 prefix. */
  818. memset (&p, 0, sizeof (struct prefix_ipv4));
  819. p.family = AF_INET;
  820. p.prefixlen = stream_getc (s);
  821. stream_get (&p.prefix, s, PSIZE (p.prefixlen));
  822. /* Nexthop, ifindex, distance, metric. */
  823. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
  824. {
  825. nexthop_num = stream_getc (s);
  826. for (i = 0; i < nexthop_num; i++)
  827. {
  828. nexthop_type = stream_getc (s);
  829. switch (nexthop_type)
  830. {
  831. case ZEBRA_NEXTHOP_IFINDEX:
  832. ifindex = stream_getl (s);
  833. break;
  834. case ZEBRA_NEXTHOP_IFNAME:
  835. ifname_len = stream_getc (s);
  836. stream_forward_getp (s, ifname_len);
  837. break;
  838. case ZEBRA_NEXTHOP_IPV4:
  839. nexthop.s_addr = stream_get_ipv4 (s);
  840. nexthop_p = &nexthop;
  841. break;
  842. case ZEBRA_NEXTHOP_IPV4_IFINDEX:
  843. nexthop.s_addr = stream_get_ipv4 (s);
  844. nexthop_p = &nexthop;
  845. ifindex = stream_getl (s);
  846. break;
  847. case ZEBRA_NEXTHOP_IPV6:
  848. stream_forward_getp (s, IPV6_MAX_BYTELEN);
  849. break;
  850. }
  851. }
  852. }
  853. /* Distance. */
  854. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
  855. api.distance = stream_getc (s);
  856. else
  857. api.distance = 0;
  858. /* Metric. */
  859. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
  860. api.metric = stream_getl (s);
  861. else
  862. api.metric = 0;
  863. rib_delete_ipv4 (api.type, api.flags, &p, nexthop_p, ifindex,
  864. vrf_id, api.safi);
  865. return 0;
  866. }
  867. /* Nexthop lookup for IPv4. */
  868. static int
  869. zread_ipv4_nexthop_lookup (struct zserv *client, u_short length,
  870. vrf_id_t vrf_id)
  871. {
  872. struct in_addr addr;
  873. char buf[BUFSIZ];
  874. addr.s_addr = stream_get_ipv4 (client->ibuf);
  875. if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
  876. zlog_debug("%s: looking up %s", __func__,
  877. inet_ntop (AF_INET, &addr, buf, BUFSIZ));
  878. return zsend_ipv4_nexthop_lookup (client, addr, vrf_id);
  879. }
  880. /* MRIB Nexthop lookup for IPv4. */
  881. static int
  882. zread_ipv4_nexthop_lookup_mrib (struct zserv *client, u_short length,
  883. vrf_id_t vrf_id)
  884. {
  885. struct in_addr addr;
  886. struct rib *rib;
  887. addr.s_addr = stream_get_ipv4 (client->ibuf);
  888. rib = rib_match_ipv4_multicast (addr, NULL, vrf_id);
  889. return zsend_ipv4_nexthop_lookup_mrib (client, addr, rib);
  890. }
  891. /* Nexthop lookup for IPv4. */
  892. static int
  893. zread_ipv4_import_lookup (struct zserv *client, u_short length,
  894. vrf_id_t vrf_id)
  895. {
  896. struct prefix_ipv4 p;
  897. p.family = AF_INET;
  898. p.prefixlen = stream_getc (client->ibuf);
  899. p.prefix.s_addr = stream_get_ipv4 (client->ibuf);
  900. return zsend_ipv4_import_lookup (client, &p, vrf_id);
  901. }
  902. #ifdef HAVE_IPV6
  903. /* Zebra server IPv6 prefix add function. */
  904. static int
  905. zread_ipv6_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
  906. {
  907. int i;
  908. struct stream *s;
  909. struct zapi_ipv6 api;
  910. struct in6_addr nexthop;
  911. unsigned long ifindex;
  912. struct prefix_ipv6 p;
  913. s = client->ibuf;
  914. ifindex = 0;
  915. memset (&nexthop, 0, sizeof (struct in6_addr));
  916. /* Type, flags, message. */
  917. api.type = stream_getc (s);
  918. api.flags = stream_getc (s);
  919. api.message = stream_getc (s);
  920. api.safi = stream_getw (s);
  921. /* IPv4 prefix. */
  922. memset (&p, 0, sizeof (struct prefix_ipv6));
  923. p.family = AF_INET6;
  924. p.prefixlen = stream_getc (s);
  925. stream_get (&p.prefix, s, PSIZE (p.prefixlen));
  926. /* Nexthop, ifindex, distance, metric. */
  927. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
  928. {
  929. u_char nexthop_type;
  930. api.nexthop_num = stream_getc (s);
  931. for (i = 0; i < api.nexthop_num; i++)
  932. {
  933. nexthop_type = stream_getc (s);
  934. switch (nexthop_type)
  935. {
  936. case ZEBRA_NEXTHOP_IPV6:
  937. stream_get (&nexthop, s, 16);
  938. break;
  939. case ZEBRA_NEXTHOP_IFINDEX:
  940. ifindex = stream_getl (s);
  941. break;
  942. }
  943. }
  944. }
  945. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
  946. api.distance = stream_getc (s);
  947. else
  948. api.distance = 0;
  949. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
  950. api.metric = stream_getl (s);
  951. else
  952. api.metric = 0;
  953. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_MTU))
  954. api.mtu = stream_getl (s);
  955. else
  956. api.mtu = 0;
  957. if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
  958. rib_add_ipv6 (api.type, api.flags, &p, NULL, ifindex,
  959. vrf_id, zebrad.rtm_table_default, api.metric,
  960. api.mtu, api.distance, api.safi);
  961. else
  962. rib_add_ipv6 (api.type, api.flags, &p, &nexthop, ifindex,
  963. vrf_id, zebrad.rtm_table_default, api.metric,
  964. api.mtu, api.distance, api.safi);
  965. return 0;
  966. }
  967. /* Zebra server IPv6 prefix delete function. */
  968. static int
  969. zread_ipv6_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
  970. {
  971. int i;
  972. struct stream *s;
  973. struct zapi_ipv6 api;
  974. struct in6_addr nexthop;
  975. unsigned long ifindex;
  976. struct prefix_ipv6 p;
  977. s = client->ibuf;
  978. ifindex = 0;
  979. memset (&nexthop, 0, sizeof (struct in6_addr));
  980. /* Type, flags, message. */
  981. api.type = stream_getc (s);
  982. api.flags = stream_getc (s);
  983. api.message = stream_getc (s);
  984. api.safi = stream_getw (s);
  985. /* IPv4 prefix. */
  986. memset (&p, 0, sizeof (struct prefix_ipv6));
  987. p.family = AF_INET6;
  988. p.prefixlen = stream_getc (s);
  989. stream_get (&p.prefix, s, PSIZE (p.prefixlen));
  990. /* Nexthop, ifindex, distance, metric. */
  991. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
  992. {
  993. u_char nexthop_type;
  994. api.nexthop_num = stream_getc (s);
  995. for (i = 0; i < api.nexthop_num; i++)
  996. {
  997. nexthop_type = stream_getc (s);
  998. switch (nexthop_type)
  999. {
  1000. case ZEBRA_NEXTHOP_IPV6:
  1001. stream_get (&nexthop, s, 16);
  1002. break;
  1003. case ZEBRA_NEXTHOP_IFINDEX:
  1004. ifindex = stream_getl (s);
  1005. break;
  1006. }
  1007. }
  1008. }
  1009. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
  1010. api.distance = stream_getc (s);
  1011. else
  1012. api.distance = 0;
  1013. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
  1014. api.metric = stream_getl (s);
  1015. else
  1016. api.metric = 0;
  1017. if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
  1018. rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, vrf_id,
  1019. api.safi);
  1020. else
  1021. rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, vrf_id,
  1022. api.safi);
  1023. return 0;
  1024. }
  1025. static int
  1026. zread_ipv6_nexthop_lookup (struct zserv *client, u_short length,
  1027. vrf_id_t vrf_id)
  1028. {
  1029. struct in6_addr addr;
  1030. char buf[BUFSIZ];
  1031. stream_get (&addr, client->ibuf, 16);
  1032. if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
  1033. zlog_debug("%s: looking up %s", __func__,
  1034. inet_ntop (AF_INET6, &addr, buf, BUFSIZ));
  1035. return zsend_ipv6_nexthop_lookup (client, &addr, vrf_id);
  1036. }
  1037. #endif /* HAVE_IPV6 */
  1038. /* Register zebra server router-id information. Send current router-id */
  1039. static int
  1040. zread_router_id_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
  1041. {
  1042. struct prefix p;
  1043. /* Router-id information is needed. */
  1044. vrf_bitmap_set (client->ridinfo, vrf_id);
  1045. router_id_get (&p, vrf_id);
  1046. return zsend_router_id_update (client, &p, vrf_id);
  1047. }
  1048. /* Unregister zebra server router-id information. */
  1049. static int
  1050. zread_router_id_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
  1051. {
  1052. vrf_bitmap_unset (client->ridinfo, vrf_id);
  1053. return 0;
  1054. }
  1055. /* Tie up route-type and client->sock */
  1056. static void
  1057. zread_hello (struct zserv *client)
  1058. {
  1059. /* type of protocol (lib/zebra.h) */
  1060. u_char proto;
  1061. proto = stream_getc (client->ibuf);
  1062. /* accept only dynamic routing protocols */
  1063. if ((proto < ZEBRA_ROUTE_MAX)
  1064. && (proto > ZEBRA_ROUTE_STATIC))
  1065. {
  1066. zlog_notice ("client %d says hello and bids fair to announce only %s routes",
  1067. client->sock, zebra_route_string(proto));
  1068. /* if route-type was binded by other client */
  1069. if (route_type_oaths[proto])
  1070. zlog_warn ("sender of %s routes changed %c->%c",
  1071. zebra_route_string(proto), route_type_oaths[proto],
  1072. client->sock);
  1073. route_type_oaths[proto] = client->sock;
  1074. }
  1075. }
  1076. /* Unregister all information in a VRF. */
  1077. static int
  1078. zread_vrf_unregister (struct zserv *client, u_short length, vrf_id_t vrf_id)
  1079. {
  1080. int i;
  1081. for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
  1082. vrf_bitmap_unset (client->redist[i], vrf_id);
  1083. vrf_bitmap_unset (client->redist_default, vrf_id);
  1084. vrf_bitmap_unset (client->ifinfo, vrf_id);
  1085. vrf_bitmap_unset (client->ridinfo, vrf_id);
  1086. return 0;
  1087. }
  1088. /* If client sent routes of specific type, zebra removes it
  1089. * and returns number of deleted routes.
  1090. */
  1091. static void
  1092. zebra_score_rib (int client_sock)
  1093. {
  1094. int i;
  1095. for (i = ZEBRA_ROUTE_RIP; i < ZEBRA_ROUTE_MAX; i++)
  1096. if (client_sock == route_type_oaths[i])
  1097. {
  1098. zlog_notice ("client %d disconnected. %lu %s routes removed from the rib",
  1099. client_sock, rib_score_proto (i), zebra_route_string (i));
  1100. route_type_oaths[i] = 0;
  1101. break;
  1102. }
  1103. }
  1104. /* Close zebra client. */
  1105. static void
  1106. zebra_client_close (struct zserv *client)
  1107. {
  1108. /* Close file descriptor. */
  1109. if (client->sock)
  1110. {
  1111. close (client->sock);
  1112. zebra_score_rib (client->sock);
  1113. client->sock = -1;
  1114. }
  1115. /* Free stream buffers. */
  1116. if (client->ibuf)
  1117. stream_free (client->ibuf);
  1118. if (client->obuf)
  1119. stream_free (client->obuf);
  1120. if (client->wb)
  1121. buffer_free(client->wb);
  1122. /* Release threads. */
  1123. if (client->t_read)
  1124. thread_cancel (client->t_read);
  1125. if (client->t_write)
  1126. thread_cancel (client->t_write);
  1127. if (client->t_suicide)
  1128. thread_cancel (client->t_suicide);
  1129. /* Free client structure. */
  1130. listnode_delete (zebrad.client_list, client);
  1131. XFREE (0, client);
  1132. }
  1133. /* Make new client. */
  1134. static void
  1135. zebra_client_create (int sock)
  1136. {
  1137. struct zserv *client;
  1138. int i;
  1139. client = XCALLOC (0, sizeof (struct zserv));
  1140. /* Make client input/output buffer. */
  1141. client->sock = sock;
  1142. client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
  1143. client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
  1144. client->wb = buffer_new(0);
  1145. /* Set table number. */
  1146. client->rtm_table = zebrad.rtm_table_default;
  1147. /* Initialize flags */
  1148. for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
  1149. client->redist[i] = vrf_bitmap_init ();
  1150. client->redist_default = vrf_bitmap_init ();
  1151. client->ifinfo = vrf_bitmap_init ();
  1152. client->ridinfo = vrf_bitmap_init ();
  1153. /* Add this client to linked list. */
  1154. listnode_add (zebrad.client_list, client);
  1155. /* Make new read thread. */
  1156. zebra_event (ZEBRA_READ, sock, client);
  1157. }
  1158. /* Handler of zebra service request. */
  1159. static int
  1160. zebra_client_read (struct thread *thread)
  1161. {
  1162. int sock;
  1163. struct zserv *client;
  1164. size_t already;
  1165. uint16_t length, command;
  1166. uint8_t marker, version;
  1167. vrf_id_t vrf_id;
  1168. /* Get thread data. Reset reading thread because I'm running. */
  1169. sock = THREAD_FD (thread);
  1170. client = THREAD_ARG (thread);
  1171. client->t_read = NULL;
  1172. if (client->t_suicide)
  1173. {
  1174. zebra_client_close(client);
  1175. return -1;
  1176. }
  1177. /* Read length and command (if we don't have it already). */
  1178. if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE)
  1179. {
  1180. ssize_t nbyte;
  1181. if (((nbyte = stream_read_try (client->ibuf, sock,
  1182. ZEBRA_HEADER_SIZE-already)) == 0) ||
  1183. (nbyte == -1))
  1184. {
  1185. if (IS_ZEBRA_DEBUG_EVENT)
  1186. zlog_debug ("connection closed socket [%d]", sock);
  1187. zebra_client_close (client);
  1188. return -1;
  1189. }
  1190. if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
  1191. {
  1192. /* Try again later. */
  1193. zebra_event (ZEBRA_READ, sock, client);
  1194. return 0;
  1195. }
  1196. already = ZEBRA_HEADER_SIZE;
  1197. }
  1198. /* Reset to read from the beginning of the incoming packet. */
  1199. stream_set_getp(client->ibuf, 0);
  1200. /* Fetch header values */
  1201. length = stream_getw (client->ibuf);
  1202. marker = stream_getc (client->ibuf);
  1203. version = stream_getc (client->ibuf);
  1204. vrf_id = stream_getw (client->ibuf);
  1205. command = stream_getw (client->ibuf);
  1206. if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
  1207. {
  1208. zlog_err("%s: socket %d version mismatch, marker %d, version %d",
  1209. __func__, sock, marker, version);
  1210. zebra_client_close (client);
  1211. return -1;
  1212. }
  1213. if (length < ZEBRA_HEADER_SIZE)
  1214. {
  1215. zlog_warn("%s: socket %d message length %u is less than header size %d",
  1216. __func__, sock, length, ZEBRA_HEADER_SIZE);
  1217. zebra_client_close (client);
  1218. return -1;
  1219. }
  1220. if (length > STREAM_SIZE(client->ibuf))
  1221. {
  1222. zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
  1223. __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf));
  1224. zebra_client_close (client);
  1225. return -1;
  1226. }
  1227. /* Read rest of data. */
  1228. if (already < length)
  1229. {
  1230. ssize_t nbyte;
  1231. if (((nbyte = stream_read_try (client->ibuf, sock,
  1232. length-already)) == 0) ||
  1233. (nbyte == -1))
  1234. {
  1235. if (IS_ZEBRA_DEBUG_EVENT)
  1236. zlog_debug ("connection closed [%d] when reading zebra data", sock);
  1237. zebra_client_close (client);
  1238. return -1;
  1239. }
  1240. if (nbyte != (ssize_t)(length-already))
  1241. {
  1242. /* Try again later. */
  1243. zebra_event (ZEBRA_READ, sock, client);
  1244. return 0;
  1245. }
  1246. }
  1247. length -= ZEBRA_HEADER_SIZE;
  1248. /* Debug packet information. */
  1249. if (IS_ZEBRA_DEBUG_EVENT)
  1250. zlog_debug ("zebra message comes from socket [%d]", sock);
  1251. if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
  1252. zlog_debug ("zebra message received [%s] %d in VRF %u",
  1253. zserv_command_string (command), length, vrf_id);
  1254. switch (command)
  1255. {
  1256. case ZEBRA_ROUTER_ID_ADD:
  1257. zread_router_id_add (client, length, vrf_id);
  1258. break;
  1259. case ZEBRA_ROUTER_ID_DELETE:
  1260. zread_router_id_delete (client, length, vrf_id);
  1261. break;
  1262. case ZEBRA_INTERFACE_ADD:
  1263. zread_interface_add (client, length, vrf_id);
  1264. break;
  1265. case ZEBRA_INTERFACE_DELETE:
  1266. zread_interface_delete (client, length, vrf_id);
  1267. break;
  1268. case ZEBRA_IPV4_ROUTE_ADD:
  1269. zread_ipv4_add (client, length, vrf_id);
  1270. break;
  1271. case ZEBRA_IPV4_ROUTE_DELETE:
  1272. zread_ipv4_delete (client, length, vrf_id);
  1273. break;
  1274. #ifdef HAVE_IPV6
  1275. case ZEBRA_IPV6_ROUTE_ADD:
  1276. zread_ipv6_add (client, length, vrf_id);
  1277. break;
  1278. case ZEBRA_IPV6_ROUTE_DELETE:
  1279. zread_ipv6_delete (client, length, vrf_id);
  1280. break;
  1281. #endif /* HAVE_IPV6 */
  1282. case ZEBRA_REDISTRIBUTE_ADD:
  1283. zebra_redistribute_add (command, client, length, vrf_id);
  1284. break;
  1285. case ZEBRA_REDISTRIBUTE_DELETE:
  1286. zebra_redistribute_delete (command, client, length, vrf_id);
  1287. break;
  1288. case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
  1289. zebra_redistribute_default_add (command, client, length, vrf_id);
  1290. break;
  1291. case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
  1292. zebra_redistribute_default_delete (command, client, length, vrf_id);
  1293. break;
  1294. case ZEBRA_IPV4_NEXTHOP_LOOKUP:
  1295. zread_ipv4_nexthop_lookup (client, length, vrf_id);
  1296. break;
  1297. case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB:
  1298. zread_ipv4_nexthop_lookup_mrib (client, length, vrf_id);
  1299. break;
  1300. #ifdef HAVE_IPV6
  1301. case ZEBRA_IPV6_NEXTHOP_LOOKUP:
  1302. zread_ipv6_nexthop_lookup (client, length, vrf_id);
  1303. break;
  1304. #endif /* HAVE_IPV6 */
  1305. case ZEBRA_IPV4_IMPORT_LOOKUP:
  1306. zread_ipv4_import_lookup (client, length, vrf_id);
  1307. break;
  1308. case ZEBRA_HELLO:
  1309. zread_hello (client);
  1310. break;
  1311. case ZEBRA_VRF_UNREGISTER:
  1312. zread_vrf_unregister (client, length, vrf_id);
  1313. break;
  1314. default:
  1315. zlog_info ("Zebra received unknown command %d", command);
  1316. break;
  1317. }
  1318. if (client->t_suicide)
  1319. {
  1320. /* No need to wait for thread callback, just kill immediately. */
  1321. zebra_client_close(client);
  1322. return -1;
  1323. }
  1324. stream_reset (client->ibuf);
  1325. zebra_event (ZEBRA_READ, sock, client);
  1326. return 0;
  1327. }
  1328. /* Accept code of zebra server socket. */
  1329. static int
  1330. zebra_accept (struct thread *thread)
  1331. {
  1332. int accept_sock;
  1333. int client_sock;
  1334. struct sockaddr_in client;
  1335. socklen_t len;
  1336. accept_sock = THREAD_FD (thread);
  1337. /* Reregister myself. */
  1338. zebra_event (ZEBRA_SERV, accept_sock, NULL);
  1339. len = sizeof (struct sockaddr_in);
  1340. client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
  1341. if (client_sock < 0)
  1342. {
  1343. zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno));
  1344. return -1;
  1345. }
  1346. /* Make client socket non-blocking. */
  1347. set_nonblocking(client_sock);
  1348. /* Create new zebra client. */
  1349. zebra_client_create (client_sock);
  1350. return 0;
  1351. }
  1352. #ifdef HAVE_TCP_ZEBRA
  1353. /* Make zebra's server socket. */
  1354. static void
  1355. zebra_serv ()
  1356. {
  1357. int ret;
  1358. int accept_sock;
  1359. struct sockaddr_in addr;
  1360. accept_sock = socket (AF_INET, SOCK_STREAM, 0);
  1361. if (accept_sock < 0)
  1362. {
  1363. zlog_warn ("Can't create zserv stream socket: %s",
  1364. safe_strerror (errno));
  1365. zlog_warn ("zebra can't provice full functionality due to above error");
  1366. return;
  1367. }
  1368. memset (&route_type_oaths, 0, sizeof (route_type_oaths));
  1369. memset (&addr, 0, sizeof (struct sockaddr_in));
  1370. addr.sin_family = AF_INET;
  1371. addr.sin_port = htons (ZEBRA_PORT);
  1372. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  1373. addr.sin_len = sizeof (struct sockaddr_in);
  1374. #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
  1375. addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
  1376. sockopt_reuseaddr (accept_sock);
  1377. sockopt_reuseport (accept_sock);
  1378. if ( zserv_privs.change(ZPRIVS_RAISE) )
  1379. zlog (NULL, LOG_ERR, "Can't raise privileges");
  1380. ret = bind (accept_sock, (struct sockaddr *)&addr,
  1381. sizeof (struct sockaddr_in));
  1382. if (ret < 0)
  1383. {
  1384. zlog_warn ("Can't bind to stream socket: %s",
  1385. safe_strerror (errno));
  1386. zlog_warn ("zebra can't provice full functionality due to above error");
  1387. close (accept_sock); /* Avoid sd leak. */
  1388. return;
  1389. }
  1390. if ( zserv_privs.change(ZPRIVS_LOWER) )
  1391. zlog (NULL, LOG_ERR, "Can't lower privileges");
  1392. ret = listen (accept_sock, 1);
  1393. if (ret < 0)
  1394. {
  1395. zlog_warn ("Can't listen to stream socket: %s",
  1396. safe_strerror (errno));
  1397. zlog_warn ("zebra can't provice full functionality due to above error");
  1398. close (accept_sock); /* Avoid sd leak. */
  1399. return;
  1400. }
  1401. zebra_event (ZEBRA_SERV, accept_sock, NULL);
  1402. }
  1403. #else /* HAVE_TCP_ZEBRA */
  1404. /* For sockaddr_un. */
  1405. #include <sys/un.h>
  1406. /* zebra server UNIX domain socket. */
  1407. static void
  1408. zebra_serv_un (const char *path)
  1409. {
  1410. int ret;
  1411. int sock, len;
  1412. struct sockaddr_un serv;
  1413. mode_t old_mask;
  1414. /* First of all, unlink existing socket */
  1415. unlink (path);
  1416. /* Set umask */
  1417. old_mask = umask (0077);
  1418. /* Make UNIX domain socket. */
  1419. sock = socket (AF_UNIX, SOCK_STREAM, 0);
  1420. if (sock < 0)
  1421. {
  1422. zlog_warn ("Can't create zserv unix socket: %s",
  1423. safe_strerror (errno));
  1424. zlog_warn ("zebra can't provide full functionality due to above error");
  1425. return;
  1426. }
  1427. memset (&route_type_oaths, 0, sizeof (route_type_oaths));
  1428. /* Make server socket. */
  1429. memset (&serv, 0, sizeof (struct sockaddr_un));
  1430. serv.sun_family = AF_UNIX;
  1431. strncpy (serv.sun_path, path, strlen (path));
  1432. #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
  1433. len = serv.sun_len = SUN_LEN(&serv);
  1434. #else
  1435. len = sizeof (serv.sun_family) + strlen (serv.sun_path);
  1436. #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
  1437. ret = bind (sock, (struct sockaddr *) &serv, len);
  1438. if (ret < 0)
  1439. {
  1440. zlog_warn ("Can't bind to unix socket %s: %s",
  1441. path, safe_strerror (errno));
  1442. zlog_warn ("zebra can't provide full functionality due to above error");
  1443. close (sock);
  1444. return;
  1445. }
  1446. ret = listen (sock, 5);
  1447. if (ret < 0)
  1448. {
  1449. zlog_warn ("Can't listen to unix socket %s: %s",
  1450. path, safe_strerror (errno));
  1451. zlog_warn ("zebra can't provide full functionality due to above error");
  1452. close (sock);
  1453. return;
  1454. }
  1455. umask (old_mask);
  1456. zebra_event (ZEBRA_SERV, sock, NULL);
  1457. }
  1458. #endif /* HAVE_TCP_ZEBRA */
  1459. static void
  1460. zebra_event (enum event event, int sock, struct zserv *client)
  1461. {
  1462. switch (event)
  1463. {
  1464. case ZEBRA_SERV:
  1465. thread_add_read (zebrad.master, zebra_accept, client, sock);
  1466. break;
  1467. case ZEBRA_READ:
  1468. client->t_read =
  1469. thread_add_read (zebrad.master, zebra_client_read, client, sock);
  1470. break;
  1471. case ZEBRA_WRITE:
  1472. /**/
  1473. break;
  1474. }
  1475. }
  1476. /* Display default rtm_table for all clients. */
  1477. DEFUN (show_table,
  1478. show_table_cmd,
  1479. "show table",
  1480. SHOW_STR
  1481. "default routing table to use for all clients\n")
  1482. {
  1483. vty_out (vty, "table %d%s", zebrad.rtm_table_default,
  1484. VTY_NEWLINE);
  1485. return CMD_SUCCESS;
  1486. }
  1487. DEFUN (config_table,
  1488. config_table_cmd,
  1489. "table TABLENO",
  1490. "Configure target kernel routing table\n"
  1491. "TABLE integer\n")
  1492. {
  1493. zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10);
  1494. return CMD_SUCCESS;
  1495. }
  1496. DEFUN (ip_forwarding,
  1497. ip_forwarding_cmd,
  1498. "ip forwarding",
  1499. IP_STR
  1500. "Turn on IP forwarding")
  1501. {
  1502. int ret;
  1503. ret = ipforward ();
  1504. if (ret == 0)
  1505. ret = ipforward_on ();
  1506. if (ret == 0)
  1507. {
  1508. vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
  1509. return CMD_WARNING;
  1510. }
  1511. return CMD_SUCCESS;
  1512. }
  1513. DEFUN (no_ip_forwarding,
  1514. no_ip_forwarding_cmd,
  1515. "no ip forwarding",
  1516. NO_STR
  1517. IP_STR
  1518. "Turn off IP forwarding")
  1519. {
  1520. int ret;
  1521. ret = ipforward ();
  1522. if (ret != 0)
  1523. ret = ipforward_off ();
  1524. if (ret != 0)
  1525. {
  1526. vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
  1527. return CMD_WARNING;
  1528. }
  1529. return CMD_SUCCESS;
  1530. }
  1531. /* This command is for debugging purpose. */
  1532. DEFUN (show_zebra_client,
  1533. show_zebra_client_cmd,
  1534. "show zebra client",
  1535. SHOW_STR
  1536. "Zebra information"
  1537. "Client information")
  1538. {
  1539. struct listnode *node;
  1540. struct zserv *client;
  1541. for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
  1542. vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE);
  1543. return CMD_SUCCESS;
  1544. }
  1545. /* Table configuration write function. */
  1546. static int
  1547. config_write_table (struct vty *vty)
  1548. {
  1549. if (zebrad.rtm_table_default)
  1550. vty_out (vty, "table %d%s", zebrad.rtm_table_default,
  1551. VTY_NEWLINE);
  1552. return 0;
  1553. }
  1554. /* table node for routing tables. */
  1555. static struct cmd_node table_node =
  1556. {
  1557. TABLE_NODE,
  1558. "", /* This node has no interface. */
  1559. 1
  1560. };
  1561. /* Only display ip forwarding is enabled or not. */
  1562. DEFUN (show_ip_forwarding,
  1563. show_ip_forwarding_cmd,
  1564. "show ip forwarding",
  1565. SHOW_STR
  1566. IP_STR
  1567. "IP forwarding status\n")
  1568. {
  1569. int ret;
  1570. ret = ipforward ();
  1571. if (ret == 0)
  1572. vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
  1573. else
  1574. vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
  1575. return CMD_SUCCESS;
  1576. }
  1577. #ifdef HAVE_IPV6
  1578. /* Only display ipv6 forwarding is enabled or not. */
  1579. DEFUN (show_ipv6_forwarding,
  1580. show_ipv6_forwarding_cmd,
  1581. "show ipv6 forwarding",
  1582. SHOW_STR
  1583. "IPv6 information\n"
  1584. "Forwarding status\n")
  1585. {
  1586. int ret;
  1587. ret = ipforward_ipv6 ();
  1588. switch (ret)
  1589. {
  1590. case -1:
  1591. vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
  1592. break;
  1593. case 0:
  1594. vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
  1595. break;
  1596. case 1:
  1597. vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
  1598. break;
  1599. default:
  1600. vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
  1601. break;
  1602. }
  1603. return CMD_SUCCESS;
  1604. }
  1605. DEFUN (ipv6_forwarding,
  1606. ipv6_forwarding_cmd,
  1607. "ipv6 forwarding",
  1608. IPV6_STR
  1609. "Turn on IPv6 forwarding")
  1610. {
  1611. int ret;
  1612. ret = ipforward_ipv6 ();
  1613. if (ret == 0)
  1614. ret = ipforward_ipv6_on ();
  1615. if (ret == 0)
  1616. {
  1617. vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
  1618. return CMD_WARNING;
  1619. }
  1620. return CMD_SUCCESS;
  1621. }
  1622. DEFUN (no_ipv6_forwarding,
  1623. no_ipv6_forwarding_cmd,
  1624. "no ipv6 forwarding",
  1625. NO_STR
  1626. IPV6_STR
  1627. "Turn off IPv6 forwarding")
  1628. {
  1629. int ret;
  1630. ret = ipforward_ipv6 ();
  1631. if (ret != 0)
  1632. ret = ipforward_ipv6_off ();
  1633. if (ret != 0)
  1634. {
  1635. vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
  1636. return CMD_WARNING;
  1637. }
  1638. return CMD_SUCCESS;
  1639. }
  1640. #endif /* HAVE_IPV6 */
  1641. /* IPForwarding configuration write function. */
  1642. static int
  1643. config_write_forwarding (struct vty *vty)
  1644. {
  1645. /* FIXME: Find better place for that. */
  1646. router_id_write (vty);
  1647. if (ipforward ())
  1648. vty_out (vty, "ip forwarding%s", VTY_NEWLINE);
  1649. #ifdef HAVE_IPV6
  1650. if (ipforward_ipv6 ())
  1651. vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE);
  1652. #endif /* HAVE_IPV6 */
  1653. vty_out (vty, "!%s", VTY_NEWLINE);
  1654. return 0;
  1655. }
  1656. /* table node for routing tables. */
  1657. static struct cmd_node forwarding_node =
  1658. {
  1659. FORWARDING_NODE,
  1660. "", /* This node has no interface. */
  1661. 1
  1662. };
  1663. /* Initialisation of zebra and installation of commands. */
  1664. void
  1665. zebra_init (void)
  1666. {
  1667. /* Client list init. */
  1668. zebrad.client_list = list_new ();
  1669. /* Install configuration write function. */
  1670. install_node (&table_node, config_write_table);
  1671. install_node (&forwarding_node, config_write_forwarding);
  1672. install_element (VIEW_NODE, &show_ip_forwarding_cmd);
  1673. install_element (ENABLE_NODE, &show_ip_forwarding_cmd);
  1674. install_element (CONFIG_NODE, &ip_forwarding_cmd);
  1675. install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
  1676. install_element (ENABLE_NODE, &show_zebra_client_cmd);
  1677. #ifdef HAVE_NETLINK
  1678. install_element (VIEW_NODE, &show_table_cmd);
  1679. install_element (ENABLE_NODE, &show_table_cmd);
  1680. install_element (CONFIG_NODE, &config_table_cmd);
  1681. #endif /* HAVE_NETLINK */
  1682. #ifdef HAVE_IPV6
  1683. install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
  1684. install_element (ENABLE_NODE, &show_ipv6_forwarding_cmd);
  1685. install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
  1686. install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
  1687. #endif /* HAVE_IPV6 */
  1688. /* Route-map */
  1689. zebra_route_map_init ();
  1690. }
  1691. /* Make zebra server socket, wiping any existing one (see bug #403). */
  1692. void
  1693. zebra_zserv_socket_init (char *path)
  1694. {
  1695. #ifdef HAVE_TCP_ZEBRA
  1696. zebra_serv ();
  1697. #else
  1698. zebra_serv_un (path ? path : ZEBRA_SERV_PATH);
  1699. #endif /* HAVE_TCP_ZEBRA */
  1700. }