zserv.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991
  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,
  552. rib ? rib->vrf_id : VRF_DEFAULT);
  553. stream_put_in_addr (s, &addr);
  554. if (rib)
  555. {
  556. stream_putc (s, rib->distance);
  557. stream_putl (s, rib->metric);
  558. num = 0;
  559. nump = stream_get_endp(s); /* remember position for nexthop_num */
  560. stream_putc (s, 0); /* reserve room for nexthop_num */
  561. /* Only non-recursive routes are elegible to resolve the nexthop we
  562. * are looking up. Therefore, we will just iterate over the top
  563. * chain of nexthops. */
  564. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  565. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
  566. {
  567. stream_putc (s, nexthop->type);
  568. switch (nexthop->type)
  569. {
  570. case ZEBRA_NEXTHOP_IPV4:
  571. stream_put_in_addr (s, &nexthop->gate.ipv4);
  572. break;
  573. case ZEBRA_NEXTHOP_IPV4_IFINDEX:
  574. stream_put_in_addr (s, &nexthop->gate.ipv4);
  575. stream_putl (s, nexthop->ifindex);
  576. break;
  577. case ZEBRA_NEXTHOP_IFINDEX:
  578. case ZEBRA_NEXTHOP_IFNAME:
  579. stream_putl (s, nexthop->ifindex);
  580. break;
  581. default:
  582. /* do nothing */
  583. break;
  584. }
  585. num++;
  586. }
  587. stream_putc_at (s, nump, num); /* store nexthop_num */
  588. }
  589. else
  590. {
  591. stream_putc (s, 0); /* distance */
  592. stream_putl (s, 0); /* metric */
  593. stream_putc (s, 0); /* nexthop_num */
  594. }
  595. stream_putw_at (s, 0, stream_get_endp (s));
  596. return zebra_server_send_message(client);
  597. }
  598. static int
  599. zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p,
  600. vrf_id_t vrf_id)
  601. {
  602. struct stream *s;
  603. struct rib *rib;
  604. unsigned long nump;
  605. u_char num;
  606. struct nexthop *nexthop;
  607. /* Lookup nexthop. */
  608. rib = rib_lookup_ipv4 (p, vrf_id);
  609. /* Get output stream. */
  610. s = client->obuf;
  611. stream_reset (s);
  612. /* Fill in result. */
  613. zserv_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP, vrf_id);
  614. stream_put_in_addr (s, &p->prefix);
  615. if (rib)
  616. {
  617. stream_putl (s, rib->metric);
  618. num = 0;
  619. nump = stream_get_endp(s);
  620. stream_putc (s, 0);
  621. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  622. if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
  623. {
  624. stream_putc (s, nexthop->type);
  625. switch (nexthop->type)
  626. {
  627. case ZEBRA_NEXTHOP_IPV4:
  628. stream_put_in_addr (s, &nexthop->gate.ipv4);
  629. break;
  630. case ZEBRA_NEXTHOP_IPV4_IFINDEX:
  631. stream_put_in_addr (s, &nexthop->gate.ipv4);
  632. stream_putl (s, nexthop->ifindex);
  633. break;
  634. case ZEBRA_NEXTHOP_IFINDEX:
  635. case ZEBRA_NEXTHOP_IFNAME:
  636. stream_putl (s, nexthop->ifindex);
  637. break;
  638. default:
  639. /* do nothing */
  640. break;
  641. }
  642. num++;
  643. }
  644. stream_putc_at (s, nump, num);
  645. }
  646. else
  647. {
  648. stream_putl (s, 0);
  649. stream_putc (s, 0);
  650. }
  651. stream_putw_at (s, 0, stream_get_endp (s));
  652. return zebra_server_send_message(client);
  653. }
  654. /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
  655. int
  656. zsend_router_id_update (struct zserv *client, struct prefix *p,
  657. vrf_id_t vrf_id)
  658. {
  659. struct stream *s;
  660. int blen;
  661. /* Check this client need interface information. */
  662. if (! vrf_bitmap_check (client->ridinfo, vrf_id))
  663. return 0;
  664. s = client->obuf;
  665. stream_reset (s);
  666. /* Message type. */
  667. zserv_create_header (s, ZEBRA_ROUTER_ID_UPDATE, vrf_id);
  668. /* Prefix information. */
  669. stream_putc (s, p->family);
  670. blen = prefix_blen (p);
  671. stream_put (s, &p->u.prefix, blen);
  672. stream_putc (s, p->prefixlen);
  673. /* Write packet size. */
  674. stream_putw_at (s, 0, stream_get_endp (s));
  675. return zebra_server_send_message(client);
  676. }
  677. /* Register zebra server interface information. Send current all
  678. interface and address information. */
  679. static int
  680. zread_interface_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
  681. {
  682. struct listnode *ifnode, *ifnnode;
  683. struct listnode *cnode, *cnnode;
  684. struct interface *ifp;
  685. struct connected *c;
  686. /* Interface information is needed. */
  687. vrf_bitmap_set (client->ifinfo, vrf_id);
  688. for (ALL_LIST_ELEMENTS (vrf_iflist (vrf_id), ifnode, ifnnode, ifp))
  689. {
  690. /* Skip pseudo interface. */
  691. if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
  692. continue;
  693. if (zsend_interface_add (client, ifp) < 0)
  694. return -1;
  695. for (ALL_LIST_ELEMENTS (ifp->connected, cnode, cnnode, c))
  696. {
  697. if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL) &&
  698. (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client,
  699. ifp, c) < 0))
  700. return -1;
  701. }
  702. }
  703. return 0;
  704. }
  705. /* Unregister zebra server interface information. */
  706. static int
  707. zread_interface_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
  708. {
  709. vrf_bitmap_unset (client->ifinfo, vrf_id);
  710. return 0;
  711. }
  712. /* This function support multiple nexthop. */
  713. /*
  714. * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
  715. * add kernel route.
  716. */
  717. static int
  718. zread_ipv4_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
  719. {
  720. int i;
  721. struct rib *rib;
  722. struct prefix_ipv4 p;
  723. u_char message;
  724. struct in_addr nexthop;
  725. u_char nexthop_num;
  726. u_char nexthop_type;
  727. struct stream *s;
  728. ifindex_t ifindex;
  729. u_char ifname_len;
  730. safi_t safi;
  731. /* Get input stream. */
  732. s = client->ibuf;
  733. /* Allocate new rib. */
  734. rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
  735. /* Type, flags, message. */
  736. rib->type = stream_getc (s);
  737. rib->flags = stream_getc (s);
  738. message = stream_getc (s);
  739. safi = stream_getw (s);
  740. rib->uptime = time (NULL);
  741. /* IPv4 prefix. */
  742. memset (&p, 0, sizeof (struct prefix_ipv4));
  743. p.family = AF_INET;
  744. p.prefixlen = stream_getc (s);
  745. stream_get (&p.prefix, s, PSIZE (p.prefixlen));
  746. /* VRF ID */
  747. rib->vrf_id = vrf_id;
  748. /* Nexthop parse. */
  749. if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
  750. {
  751. nexthop_num = stream_getc (s);
  752. for (i = 0; i < nexthop_num; i++)
  753. {
  754. nexthop_type = stream_getc (s);
  755. switch (nexthop_type)
  756. {
  757. case ZEBRA_NEXTHOP_IFINDEX:
  758. ifindex = stream_getl (s);
  759. nexthop_ifindex_add (rib, ifindex);
  760. break;
  761. case ZEBRA_NEXTHOP_IFNAME:
  762. ifname_len = stream_getc (s);
  763. stream_forward_getp (s, ifname_len);
  764. break;
  765. case ZEBRA_NEXTHOP_IPV4:
  766. nexthop.s_addr = stream_get_ipv4 (s);
  767. nexthop_ipv4_add (rib, &nexthop, NULL);
  768. break;
  769. case ZEBRA_NEXTHOP_IPV4_IFINDEX:
  770. nexthop.s_addr = stream_get_ipv4 (s);
  771. ifindex = stream_getl (s);
  772. nexthop_ipv4_ifindex_add (rib, &nexthop, NULL, ifindex);
  773. break;
  774. case ZEBRA_NEXTHOP_IPV6:
  775. stream_forward_getp (s, IPV6_MAX_BYTELEN);
  776. break;
  777. case ZEBRA_NEXTHOP_BLACKHOLE:
  778. nexthop_blackhole_add (rib);
  779. break;
  780. }
  781. }
  782. }
  783. /* Distance. */
  784. if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
  785. rib->distance = stream_getc (s);
  786. /* Metric. */
  787. if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
  788. rib->metric = stream_getl (s);
  789. if (CHECK_FLAG (message, ZAPI_MESSAGE_MTU))
  790. rib->mtu = stream_getl (s);
  791. /* Table */
  792. rib->table=zebrad.rtm_table_default;
  793. rib_add_ipv4_multipath (&p, rib, safi);
  794. return 0;
  795. }
  796. /* Zebra server IPv4 prefix delete function. */
  797. static int
  798. zread_ipv4_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
  799. {
  800. int i;
  801. struct stream *s;
  802. struct zapi_ipv4 api;
  803. struct in_addr nexthop, *nexthop_p;
  804. unsigned long ifindex;
  805. struct prefix_ipv4 p;
  806. u_char nexthop_num;
  807. u_char nexthop_type;
  808. u_char ifname_len;
  809. s = client->ibuf;
  810. ifindex = 0;
  811. nexthop.s_addr = 0;
  812. nexthop_p = NULL;
  813. /* Type, flags, message. */
  814. api.type = stream_getc (s);
  815. api.flags = stream_getc (s);
  816. api.message = stream_getc (s);
  817. api.safi = stream_getw (s);
  818. /* IPv4 prefix. */
  819. memset (&p, 0, sizeof (struct prefix_ipv4));
  820. p.family = AF_INET;
  821. p.prefixlen = stream_getc (s);
  822. stream_get (&p.prefix, s, PSIZE (p.prefixlen));
  823. /* Nexthop, ifindex, distance, metric. */
  824. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
  825. {
  826. nexthop_num = stream_getc (s);
  827. for (i = 0; i < nexthop_num; i++)
  828. {
  829. nexthop_type = stream_getc (s);
  830. switch (nexthop_type)
  831. {
  832. case ZEBRA_NEXTHOP_IFINDEX:
  833. ifindex = stream_getl (s);
  834. break;
  835. case ZEBRA_NEXTHOP_IFNAME:
  836. ifname_len = stream_getc (s);
  837. stream_forward_getp (s, ifname_len);
  838. break;
  839. case ZEBRA_NEXTHOP_IPV4:
  840. nexthop.s_addr = stream_get_ipv4 (s);
  841. nexthop_p = &nexthop;
  842. break;
  843. case ZEBRA_NEXTHOP_IPV4_IFINDEX:
  844. nexthop.s_addr = stream_get_ipv4 (s);
  845. nexthop_p = &nexthop;
  846. ifindex = stream_getl (s);
  847. break;
  848. case ZEBRA_NEXTHOP_IPV6:
  849. stream_forward_getp (s, IPV6_MAX_BYTELEN);
  850. break;
  851. }
  852. }
  853. }
  854. /* Distance. */
  855. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
  856. api.distance = stream_getc (s);
  857. else
  858. api.distance = 0;
  859. /* Metric. */
  860. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
  861. api.metric = stream_getl (s);
  862. else
  863. api.metric = 0;
  864. rib_delete_ipv4 (api.type, api.flags, &p, nexthop_p, ifindex,
  865. vrf_id, api.safi);
  866. return 0;
  867. }
  868. /* Nexthop lookup for IPv4. */
  869. static int
  870. zread_ipv4_nexthop_lookup (struct zserv *client, u_short length,
  871. vrf_id_t vrf_id)
  872. {
  873. struct in_addr addr;
  874. char buf[BUFSIZ];
  875. addr.s_addr = stream_get_ipv4 (client->ibuf);
  876. if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
  877. zlog_debug("%s: looking up %s", __func__,
  878. inet_ntop (AF_INET, &addr, buf, BUFSIZ));
  879. return zsend_ipv4_nexthop_lookup (client, addr, vrf_id);
  880. }
  881. /* MRIB Nexthop lookup for IPv4. */
  882. static int
  883. zread_ipv4_nexthop_lookup_mrib (struct zserv *client, u_short length,
  884. vrf_id_t vrf_id)
  885. {
  886. struct in_addr addr;
  887. struct rib *rib;
  888. addr.s_addr = stream_get_ipv4 (client->ibuf);
  889. rib = rib_match_ipv4_multicast (addr, NULL, vrf_id);
  890. return zsend_ipv4_nexthop_lookup_mrib (client, addr, rib);
  891. }
  892. /* Nexthop lookup for IPv4. */
  893. static int
  894. zread_ipv4_import_lookup (struct zserv *client, u_short length,
  895. vrf_id_t vrf_id)
  896. {
  897. struct prefix_ipv4 p;
  898. p.family = AF_INET;
  899. p.prefixlen = stream_getc (client->ibuf);
  900. p.prefix.s_addr = stream_get_ipv4 (client->ibuf);
  901. return zsend_ipv4_import_lookup (client, &p, vrf_id);
  902. }
  903. #ifdef HAVE_IPV6
  904. /* Zebra server IPv6 prefix add function. */
  905. static int
  906. zread_ipv6_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
  907. {
  908. int i;
  909. struct stream *s;
  910. struct zapi_ipv6 api;
  911. struct in6_addr nexthop;
  912. unsigned long ifindex;
  913. struct prefix_ipv6 p;
  914. s = client->ibuf;
  915. ifindex = 0;
  916. memset (&nexthop, 0, sizeof (struct in6_addr));
  917. /* Type, flags, message. */
  918. api.type = stream_getc (s);
  919. api.flags = stream_getc (s);
  920. api.message = stream_getc (s);
  921. api.safi = stream_getw (s);
  922. /* IPv4 prefix. */
  923. memset (&p, 0, sizeof (struct prefix_ipv6));
  924. p.family = AF_INET6;
  925. p.prefixlen = stream_getc (s);
  926. stream_get (&p.prefix, s, PSIZE (p.prefixlen));
  927. /* Nexthop, ifindex, distance, metric. */
  928. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
  929. {
  930. u_char nexthop_type;
  931. api.nexthop_num = stream_getc (s);
  932. for (i = 0; i < api.nexthop_num; i++)
  933. {
  934. nexthop_type = stream_getc (s);
  935. switch (nexthop_type)
  936. {
  937. case ZEBRA_NEXTHOP_IPV6:
  938. stream_get (&nexthop, s, 16);
  939. break;
  940. case ZEBRA_NEXTHOP_IFINDEX:
  941. ifindex = stream_getl (s);
  942. break;
  943. }
  944. }
  945. }
  946. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
  947. api.distance = stream_getc (s);
  948. else
  949. api.distance = 0;
  950. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
  951. api.metric = stream_getl (s);
  952. else
  953. api.metric = 0;
  954. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_MTU))
  955. api.mtu = stream_getl (s);
  956. else
  957. api.mtu = 0;
  958. if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
  959. rib_add_ipv6 (api.type, api.flags, &p, NULL, ifindex,
  960. vrf_id, zebrad.rtm_table_default, api.metric,
  961. api.mtu, api.distance, api.safi);
  962. else
  963. rib_add_ipv6 (api.type, api.flags, &p, &nexthop, ifindex,
  964. vrf_id, zebrad.rtm_table_default, api.metric,
  965. api.mtu, api.distance, api.safi);
  966. return 0;
  967. }
  968. /* Zebra server IPv6 prefix delete function. */
  969. static int
  970. zread_ipv6_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
  971. {
  972. int i;
  973. struct stream *s;
  974. struct zapi_ipv6 api;
  975. struct in6_addr nexthop;
  976. unsigned long ifindex;
  977. struct prefix_ipv6 p;
  978. s = client->ibuf;
  979. ifindex = 0;
  980. memset (&nexthop, 0, sizeof (struct in6_addr));
  981. /* Type, flags, message. */
  982. api.type = stream_getc (s);
  983. api.flags = stream_getc (s);
  984. api.message = stream_getc (s);
  985. api.safi = stream_getw (s);
  986. /* IPv4 prefix. */
  987. memset (&p, 0, sizeof (struct prefix_ipv6));
  988. p.family = AF_INET6;
  989. p.prefixlen = stream_getc (s);
  990. stream_get (&p.prefix, s, PSIZE (p.prefixlen));
  991. /* Nexthop, ifindex, distance, metric. */
  992. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
  993. {
  994. u_char nexthop_type;
  995. api.nexthop_num = stream_getc (s);
  996. for (i = 0; i < api.nexthop_num; i++)
  997. {
  998. nexthop_type = stream_getc (s);
  999. switch (nexthop_type)
  1000. {
  1001. case ZEBRA_NEXTHOP_IPV6:
  1002. stream_get (&nexthop, s, 16);
  1003. break;
  1004. case ZEBRA_NEXTHOP_IFINDEX:
  1005. ifindex = stream_getl (s);
  1006. break;
  1007. }
  1008. }
  1009. }
  1010. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
  1011. api.distance = stream_getc (s);
  1012. else
  1013. api.distance = 0;
  1014. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
  1015. api.metric = stream_getl (s);
  1016. else
  1017. api.metric = 0;
  1018. if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
  1019. rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, vrf_id,
  1020. api.safi);
  1021. else
  1022. rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, vrf_id,
  1023. api.safi);
  1024. return 0;
  1025. }
  1026. static int
  1027. zread_ipv6_nexthop_lookup (struct zserv *client, u_short length,
  1028. vrf_id_t vrf_id)
  1029. {
  1030. struct in6_addr addr;
  1031. char buf[BUFSIZ];
  1032. stream_get (&addr, client->ibuf, 16);
  1033. if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
  1034. zlog_debug("%s: looking up %s", __func__,
  1035. inet_ntop (AF_INET6, &addr, buf, BUFSIZ));
  1036. return zsend_ipv6_nexthop_lookup (client, &addr, vrf_id);
  1037. }
  1038. #endif /* HAVE_IPV6 */
  1039. /* Register zebra server router-id information. Send current router-id */
  1040. static int
  1041. zread_router_id_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
  1042. {
  1043. struct prefix p;
  1044. /* Router-id information is needed. */
  1045. vrf_bitmap_set (client->ridinfo, vrf_id);
  1046. router_id_get (&p, vrf_id);
  1047. return zsend_router_id_update (client, &p, vrf_id);
  1048. }
  1049. /* Unregister zebra server router-id information. */
  1050. static int
  1051. zread_router_id_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
  1052. {
  1053. vrf_bitmap_unset (client->ridinfo, vrf_id);
  1054. return 0;
  1055. }
  1056. /* Tie up route-type and client->sock */
  1057. static void
  1058. zread_hello (struct zserv *client)
  1059. {
  1060. /* type of protocol (lib/zebra.h) */
  1061. u_char proto;
  1062. proto = stream_getc (client->ibuf);
  1063. /* accept only dynamic routing protocols */
  1064. if ((proto < ZEBRA_ROUTE_MAX)
  1065. && (proto > ZEBRA_ROUTE_STATIC))
  1066. {
  1067. zlog_notice ("client %d says hello and bids fair to announce only %s routes",
  1068. client->sock, zebra_route_string(proto));
  1069. /* if route-type was binded by other client */
  1070. if (route_type_oaths[proto])
  1071. zlog_warn ("sender of %s routes changed %c->%c",
  1072. zebra_route_string(proto), route_type_oaths[proto],
  1073. client->sock);
  1074. route_type_oaths[proto] = client->sock;
  1075. }
  1076. }
  1077. /* Unregister all information in a VRF. */
  1078. static int
  1079. zread_vrf_unregister (struct zserv *client, u_short length, vrf_id_t vrf_id)
  1080. {
  1081. int i;
  1082. for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
  1083. vrf_bitmap_unset (client->redist[i], vrf_id);
  1084. vrf_bitmap_unset (client->redist_default, vrf_id);
  1085. vrf_bitmap_unset (client->ifinfo, vrf_id);
  1086. vrf_bitmap_unset (client->ridinfo, vrf_id);
  1087. return 0;
  1088. }
  1089. /* If client sent routes of specific type, zebra removes it
  1090. * and returns number of deleted routes.
  1091. */
  1092. static void
  1093. zebra_score_rib (int client_sock)
  1094. {
  1095. int i;
  1096. for (i = ZEBRA_ROUTE_RIP; i < ZEBRA_ROUTE_MAX; i++)
  1097. if (client_sock == route_type_oaths[i])
  1098. {
  1099. zlog_notice ("client %d disconnected. %lu %s routes removed from the rib",
  1100. client_sock, rib_score_proto (i), zebra_route_string (i));
  1101. route_type_oaths[i] = 0;
  1102. break;
  1103. }
  1104. }
  1105. /* Close zebra client. */
  1106. static void
  1107. zebra_client_close (struct zserv *client)
  1108. {
  1109. /* Close file descriptor. */
  1110. if (client->sock)
  1111. {
  1112. close (client->sock);
  1113. zebra_score_rib (client->sock);
  1114. client->sock = -1;
  1115. }
  1116. /* Free stream buffers. */
  1117. if (client->ibuf)
  1118. stream_free (client->ibuf);
  1119. if (client->obuf)
  1120. stream_free (client->obuf);
  1121. if (client->wb)
  1122. buffer_free(client->wb);
  1123. /* Release threads. */
  1124. if (client->t_read)
  1125. thread_cancel (client->t_read);
  1126. if (client->t_write)
  1127. thread_cancel (client->t_write);
  1128. if (client->t_suicide)
  1129. thread_cancel (client->t_suicide);
  1130. /* Free client structure. */
  1131. listnode_delete (zebrad.client_list, client);
  1132. XFREE (0, client);
  1133. }
  1134. /* Make new client. */
  1135. static void
  1136. zebra_client_create (int sock)
  1137. {
  1138. struct zserv *client;
  1139. int i;
  1140. client = XCALLOC (MTYPE_TMP, sizeof (struct zserv));
  1141. /* Make client input/output buffer. */
  1142. client->sock = sock;
  1143. client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
  1144. client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
  1145. client->wb = buffer_new(0);
  1146. /* Set table number. */
  1147. client->rtm_table = zebrad.rtm_table_default;
  1148. /* Initialize flags */
  1149. for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
  1150. client->redist[i] = vrf_bitmap_init ();
  1151. client->redist_default = vrf_bitmap_init ();
  1152. client->ifinfo = vrf_bitmap_init ();
  1153. client->ridinfo = vrf_bitmap_init ();
  1154. /* Add this client to linked list. */
  1155. listnode_add (zebrad.client_list, client);
  1156. /* Make new read thread. */
  1157. zebra_event (ZEBRA_READ, sock, client);
  1158. }
  1159. /* Handler of zebra service request. */
  1160. static int
  1161. zebra_client_read (struct thread *thread)
  1162. {
  1163. int sock;
  1164. struct zserv *client;
  1165. size_t already;
  1166. uint16_t length, command;
  1167. uint8_t marker, version;
  1168. vrf_id_t vrf_id;
  1169. /* Get thread data. Reset reading thread because I'm running. */
  1170. sock = THREAD_FD (thread);
  1171. client = THREAD_ARG (thread);
  1172. client->t_read = NULL;
  1173. if (client->t_suicide)
  1174. {
  1175. zebra_client_close(client);
  1176. return -1;
  1177. }
  1178. /* Read length and command (if we don't have it already). */
  1179. if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE)
  1180. {
  1181. ssize_t nbyte;
  1182. if (((nbyte = stream_read_try (client->ibuf, sock,
  1183. ZEBRA_HEADER_SIZE-already)) == 0) ||
  1184. (nbyte == -1))
  1185. {
  1186. if (IS_ZEBRA_DEBUG_EVENT)
  1187. zlog_debug ("connection closed socket [%d]", sock);
  1188. zebra_client_close (client);
  1189. return -1;
  1190. }
  1191. if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
  1192. {
  1193. /* Try again later. */
  1194. zebra_event (ZEBRA_READ, sock, client);
  1195. return 0;
  1196. }
  1197. already = ZEBRA_HEADER_SIZE;
  1198. }
  1199. /* Reset to read from the beginning of the incoming packet. */
  1200. stream_set_getp(client->ibuf, 0);
  1201. /* Fetch header values */
  1202. length = stream_getw (client->ibuf);
  1203. marker = stream_getc (client->ibuf);
  1204. version = stream_getc (client->ibuf);
  1205. vrf_id = stream_getw (client->ibuf);
  1206. command = stream_getw (client->ibuf);
  1207. if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
  1208. {
  1209. zlog_err("%s: socket %d version mismatch, marker %d, version %d",
  1210. __func__, sock, marker, version);
  1211. zebra_client_close (client);
  1212. return -1;
  1213. }
  1214. if (length < ZEBRA_HEADER_SIZE)
  1215. {
  1216. zlog_warn("%s: socket %d message length %u is less than header size %d",
  1217. __func__, sock, length, ZEBRA_HEADER_SIZE);
  1218. zebra_client_close (client);
  1219. return -1;
  1220. }
  1221. if (length > STREAM_SIZE(client->ibuf))
  1222. {
  1223. zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
  1224. __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf));
  1225. zebra_client_close (client);
  1226. return -1;
  1227. }
  1228. /* Read rest of data. */
  1229. if (already < length)
  1230. {
  1231. ssize_t nbyte;
  1232. if (((nbyte = stream_read_try (client->ibuf, sock,
  1233. length-already)) == 0) ||
  1234. (nbyte == -1))
  1235. {
  1236. if (IS_ZEBRA_DEBUG_EVENT)
  1237. zlog_debug ("connection closed [%d] when reading zebra data", sock);
  1238. zebra_client_close (client);
  1239. return -1;
  1240. }
  1241. if (nbyte != (ssize_t)(length-already))
  1242. {
  1243. /* Try again later. */
  1244. zebra_event (ZEBRA_READ, sock, client);
  1245. return 0;
  1246. }
  1247. }
  1248. length -= ZEBRA_HEADER_SIZE;
  1249. /* Debug packet information. */
  1250. if (IS_ZEBRA_DEBUG_EVENT)
  1251. zlog_debug ("zebra message comes from socket [%d]", sock);
  1252. if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
  1253. zlog_debug ("zebra message received [%s] %d in VRF %u",
  1254. zserv_command_string (command), length, vrf_id);
  1255. switch (command)
  1256. {
  1257. case ZEBRA_ROUTER_ID_ADD:
  1258. zread_router_id_add (client, length, vrf_id);
  1259. break;
  1260. case ZEBRA_ROUTER_ID_DELETE:
  1261. zread_router_id_delete (client, length, vrf_id);
  1262. break;
  1263. case ZEBRA_INTERFACE_ADD:
  1264. zread_interface_add (client, length, vrf_id);
  1265. break;
  1266. case ZEBRA_INTERFACE_DELETE:
  1267. zread_interface_delete (client, length, vrf_id);
  1268. break;
  1269. case ZEBRA_IPV4_ROUTE_ADD:
  1270. zread_ipv4_add (client, length, vrf_id);
  1271. break;
  1272. case ZEBRA_IPV4_ROUTE_DELETE:
  1273. zread_ipv4_delete (client, length, vrf_id);
  1274. break;
  1275. #ifdef HAVE_IPV6
  1276. case ZEBRA_IPV6_ROUTE_ADD:
  1277. zread_ipv6_add (client, length, vrf_id);
  1278. break;
  1279. case ZEBRA_IPV6_ROUTE_DELETE:
  1280. zread_ipv6_delete (client, length, vrf_id);
  1281. break;
  1282. #endif /* HAVE_IPV6 */
  1283. case ZEBRA_REDISTRIBUTE_ADD:
  1284. zebra_redistribute_add (command, client, length, vrf_id);
  1285. break;
  1286. case ZEBRA_REDISTRIBUTE_DELETE:
  1287. zebra_redistribute_delete (command, client, length, vrf_id);
  1288. break;
  1289. case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
  1290. zebra_redistribute_default_add (command, client, length, vrf_id);
  1291. break;
  1292. case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
  1293. zebra_redistribute_default_delete (command, client, length, vrf_id);
  1294. break;
  1295. case ZEBRA_IPV4_NEXTHOP_LOOKUP:
  1296. zread_ipv4_nexthop_lookup (client, length, vrf_id);
  1297. break;
  1298. case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB:
  1299. zread_ipv4_nexthop_lookup_mrib (client, length, vrf_id);
  1300. break;
  1301. #ifdef HAVE_IPV6
  1302. case ZEBRA_IPV6_NEXTHOP_LOOKUP:
  1303. zread_ipv6_nexthop_lookup (client, length, vrf_id);
  1304. break;
  1305. #endif /* HAVE_IPV6 */
  1306. case ZEBRA_IPV4_IMPORT_LOOKUP:
  1307. zread_ipv4_import_lookup (client, length, vrf_id);
  1308. break;
  1309. case ZEBRA_HELLO:
  1310. zread_hello (client);
  1311. break;
  1312. case ZEBRA_VRF_UNREGISTER:
  1313. zread_vrf_unregister (client, length, vrf_id);
  1314. break;
  1315. default:
  1316. zlog_info ("Zebra received unknown command %d", command);
  1317. break;
  1318. }
  1319. if (client->t_suicide)
  1320. {
  1321. /* No need to wait for thread callback, just kill immediately. */
  1322. zebra_client_close(client);
  1323. return -1;
  1324. }
  1325. stream_reset (client->ibuf);
  1326. zebra_event (ZEBRA_READ, sock, client);
  1327. return 0;
  1328. }
  1329. /* Accept code of zebra server socket. */
  1330. static int
  1331. zebra_accept (struct thread *thread)
  1332. {
  1333. int accept_sock;
  1334. int client_sock;
  1335. struct sockaddr_in client;
  1336. socklen_t len;
  1337. accept_sock = THREAD_FD (thread);
  1338. /* Reregister myself. */
  1339. zebra_event (ZEBRA_SERV, accept_sock, NULL);
  1340. len = sizeof (struct sockaddr_in);
  1341. client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
  1342. if (client_sock < 0)
  1343. {
  1344. zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno));
  1345. return -1;
  1346. }
  1347. /* Make client socket non-blocking. */
  1348. set_nonblocking(client_sock);
  1349. /* Create new zebra client. */
  1350. zebra_client_create (client_sock);
  1351. return 0;
  1352. }
  1353. #ifdef HAVE_TCP_ZEBRA
  1354. /* Make zebra's server socket. */
  1355. static void
  1356. zebra_serv ()
  1357. {
  1358. int ret;
  1359. int accept_sock;
  1360. struct sockaddr_in addr;
  1361. accept_sock = socket (AF_INET, SOCK_STREAM, 0);
  1362. if (accept_sock < 0)
  1363. {
  1364. zlog_warn ("Can't create zserv stream socket: %s",
  1365. safe_strerror (errno));
  1366. zlog_warn ("zebra can't provice full functionality due to above error");
  1367. return;
  1368. }
  1369. memset (&route_type_oaths, 0, sizeof (route_type_oaths));
  1370. memset (&addr, 0, sizeof (struct sockaddr_in));
  1371. addr.sin_family = AF_INET;
  1372. addr.sin_port = htons (ZEBRA_PORT);
  1373. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  1374. addr.sin_len = sizeof (struct sockaddr_in);
  1375. #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
  1376. addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
  1377. sockopt_reuseaddr (accept_sock);
  1378. sockopt_reuseport (accept_sock);
  1379. if ( zserv_privs.change(ZPRIVS_RAISE) )
  1380. zlog (NULL, LOG_ERR, "Can't raise privileges");
  1381. ret = bind (accept_sock, (struct sockaddr *)&addr,
  1382. sizeof (struct sockaddr_in));
  1383. if (ret < 0)
  1384. {
  1385. zlog_warn ("Can't bind to stream socket: %s",
  1386. safe_strerror (errno));
  1387. zlog_warn ("zebra can't provice full functionality due to above error");
  1388. close (accept_sock); /* Avoid sd leak. */
  1389. return;
  1390. }
  1391. if ( zserv_privs.change(ZPRIVS_LOWER) )
  1392. zlog (NULL, LOG_ERR, "Can't lower privileges");
  1393. ret = listen (accept_sock, 1);
  1394. if (ret < 0)
  1395. {
  1396. zlog_warn ("Can't listen to stream socket: %s",
  1397. safe_strerror (errno));
  1398. zlog_warn ("zebra can't provice full functionality due to above error");
  1399. close (accept_sock); /* Avoid sd leak. */
  1400. return;
  1401. }
  1402. zebra_event (ZEBRA_SERV, accept_sock, NULL);
  1403. }
  1404. #else /* HAVE_TCP_ZEBRA */
  1405. /* For sockaddr_un. */
  1406. #include <sys/un.h>
  1407. /* zebra server UNIX domain socket. */
  1408. static void
  1409. zebra_serv_un (const char *path)
  1410. {
  1411. int ret;
  1412. int sock, len;
  1413. struct sockaddr_un serv;
  1414. mode_t old_mask;
  1415. /* First of all, unlink existing socket */
  1416. unlink (path);
  1417. /* Set umask */
  1418. old_mask = umask (0077);
  1419. /* Make UNIX domain socket. */
  1420. sock = socket (AF_UNIX, SOCK_STREAM, 0);
  1421. if (sock < 0)
  1422. {
  1423. zlog_warn ("Can't create zserv unix socket: %s",
  1424. safe_strerror (errno));
  1425. zlog_warn ("zebra can't provide full functionality due to above error");
  1426. return;
  1427. }
  1428. memset (&route_type_oaths, 0, sizeof (route_type_oaths));
  1429. /* Make server socket. */
  1430. memset (&serv, 0, sizeof (struct sockaddr_un));
  1431. serv.sun_family = AF_UNIX;
  1432. strncpy (serv.sun_path, path, strlen (path));
  1433. #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
  1434. len = serv.sun_len = SUN_LEN(&serv);
  1435. #else
  1436. len = sizeof (serv.sun_family) + strlen (serv.sun_path);
  1437. #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
  1438. ret = bind (sock, (struct sockaddr *) &serv, len);
  1439. if (ret < 0)
  1440. {
  1441. zlog_warn ("Can't bind to unix socket %s: %s",
  1442. path, safe_strerror (errno));
  1443. zlog_warn ("zebra can't provide full functionality due to above error");
  1444. close (sock);
  1445. return;
  1446. }
  1447. ret = listen (sock, 5);
  1448. if (ret < 0)
  1449. {
  1450. zlog_warn ("Can't listen to unix socket %s: %s",
  1451. path, safe_strerror (errno));
  1452. zlog_warn ("zebra can't provide full functionality due to above error");
  1453. close (sock);
  1454. return;
  1455. }
  1456. umask (old_mask);
  1457. zebra_event (ZEBRA_SERV, sock, NULL);
  1458. }
  1459. #endif /* HAVE_TCP_ZEBRA */
  1460. static void
  1461. zebra_event (enum event event, int sock, struct zserv *client)
  1462. {
  1463. switch (event)
  1464. {
  1465. case ZEBRA_SERV:
  1466. thread_add_read (zebrad.master, zebra_accept, client, sock);
  1467. break;
  1468. case ZEBRA_READ:
  1469. client->t_read =
  1470. thread_add_read (zebrad.master, zebra_client_read, client, sock);
  1471. break;
  1472. case ZEBRA_WRITE:
  1473. /**/
  1474. break;
  1475. }
  1476. }
  1477. /* Display default rtm_table for all clients. */
  1478. DEFUN (show_table,
  1479. show_table_cmd,
  1480. "show table",
  1481. SHOW_STR
  1482. "default routing table to use for all clients\n")
  1483. {
  1484. vty_out (vty, "table %d%s", zebrad.rtm_table_default,
  1485. VTY_NEWLINE);
  1486. return CMD_SUCCESS;
  1487. }
  1488. DEFUN (config_table,
  1489. config_table_cmd,
  1490. "table TABLENO",
  1491. "Configure target kernel routing table\n"
  1492. "TABLE integer\n")
  1493. {
  1494. zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10);
  1495. return CMD_SUCCESS;
  1496. }
  1497. DEFUN (ip_forwarding,
  1498. ip_forwarding_cmd,
  1499. "ip forwarding",
  1500. IP_STR
  1501. "Turn on IP forwarding")
  1502. {
  1503. int ret;
  1504. ret = ipforward ();
  1505. if (ret == 0)
  1506. ret = ipforward_on ();
  1507. if (ret == 0)
  1508. {
  1509. vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
  1510. return CMD_WARNING;
  1511. }
  1512. return CMD_SUCCESS;
  1513. }
  1514. DEFUN (no_ip_forwarding,
  1515. no_ip_forwarding_cmd,
  1516. "no ip forwarding",
  1517. NO_STR
  1518. IP_STR
  1519. "Turn off IP forwarding")
  1520. {
  1521. int ret;
  1522. ret = ipforward ();
  1523. if (ret != 0)
  1524. ret = ipforward_off ();
  1525. if (ret != 0)
  1526. {
  1527. vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
  1528. return CMD_WARNING;
  1529. }
  1530. return CMD_SUCCESS;
  1531. }
  1532. /* This command is for debugging purpose. */
  1533. DEFUN (show_zebra_client,
  1534. show_zebra_client_cmd,
  1535. "show zebra client",
  1536. SHOW_STR
  1537. "Zebra information"
  1538. "Client information")
  1539. {
  1540. struct listnode *node;
  1541. struct zserv *client;
  1542. for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
  1543. vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE);
  1544. return CMD_SUCCESS;
  1545. }
  1546. /* Table configuration write function. */
  1547. static int
  1548. config_write_table (struct vty *vty)
  1549. {
  1550. if (zebrad.rtm_table_default)
  1551. vty_out (vty, "table %d%s", zebrad.rtm_table_default,
  1552. VTY_NEWLINE);
  1553. return 0;
  1554. }
  1555. /* table node for routing tables. */
  1556. static struct cmd_node table_node =
  1557. {
  1558. TABLE_NODE,
  1559. "", /* This node has no interface. */
  1560. 1
  1561. };
  1562. /* Only display ip forwarding is enabled or not. */
  1563. DEFUN (show_ip_forwarding,
  1564. show_ip_forwarding_cmd,
  1565. "show ip forwarding",
  1566. SHOW_STR
  1567. IP_STR
  1568. "IP forwarding status\n")
  1569. {
  1570. int ret;
  1571. ret = ipforward ();
  1572. if (ret == 0)
  1573. vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
  1574. else
  1575. vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
  1576. return CMD_SUCCESS;
  1577. }
  1578. #ifdef HAVE_IPV6
  1579. /* Only display ipv6 forwarding is enabled or not. */
  1580. DEFUN (show_ipv6_forwarding,
  1581. show_ipv6_forwarding_cmd,
  1582. "show ipv6 forwarding",
  1583. SHOW_STR
  1584. "IPv6 information\n"
  1585. "Forwarding status\n")
  1586. {
  1587. int ret;
  1588. ret = ipforward_ipv6 ();
  1589. switch (ret)
  1590. {
  1591. case -1:
  1592. vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
  1593. break;
  1594. case 0:
  1595. vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
  1596. break;
  1597. case 1:
  1598. vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
  1599. break;
  1600. default:
  1601. vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
  1602. break;
  1603. }
  1604. return CMD_SUCCESS;
  1605. }
  1606. DEFUN (ipv6_forwarding,
  1607. ipv6_forwarding_cmd,
  1608. "ipv6 forwarding",
  1609. IPV6_STR
  1610. "Turn on IPv6 forwarding")
  1611. {
  1612. int ret;
  1613. ret = ipforward_ipv6 ();
  1614. if (ret == 0)
  1615. ret = ipforward_ipv6_on ();
  1616. if (ret == 0)
  1617. {
  1618. vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
  1619. return CMD_WARNING;
  1620. }
  1621. return CMD_SUCCESS;
  1622. }
  1623. DEFUN (no_ipv6_forwarding,
  1624. no_ipv6_forwarding_cmd,
  1625. "no ipv6 forwarding",
  1626. NO_STR
  1627. IPV6_STR
  1628. "Turn off IPv6 forwarding")
  1629. {
  1630. int ret;
  1631. ret = ipforward_ipv6 ();
  1632. if (ret != 0)
  1633. ret = ipforward_ipv6_off ();
  1634. if (ret != 0)
  1635. {
  1636. vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
  1637. return CMD_WARNING;
  1638. }
  1639. return CMD_SUCCESS;
  1640. }
  1641. #endif /* HAVE_IPV6 */
  1642. /* IPForwarding configuration write function. */
  1643. static int
  1644. config_write_forwarding (struct vty *vty)
  1645. {
  1646. /* FIXME: Find better place for that. */
  1647. router_id_write (vty);
  1648. if (ipforward ())
  1649. vty_out (vty, "ip forwarding%s", VTY_NEWLINE);
  1650. #ifdef HAVE_IPV6
  1651. if (ipforward_ipv6 ())
  1652. vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE);
  1653. #endif /* HAVE_IPV6 */
  1654. vty_out (vty, "!%s", VTY_NEWLINE);
  1655. return 0;
  1656. }
  1657. /* table node for routing tables. */
  1658. static struct cmd_node forwarding_node =
  1659. {
  1660. FORWARDING_NODE,
  1661. "", /* This node has no interface. */
  1662. 1
  1663. };
  1664. #ifdef HAVE_FPM
  1665. /* function to write the fpm config info */
  1666. static int
  1667. config_write_fpm (struct vty *vty)
  1668. {
  1669. return
  1670. fpm_remote_srv_write (vty);
  1671. }
  1672. /* Zebra node */
  1673. static struct cmd_node zebra_node =
  1674. {
  1675. ZEBRA_NODE,
  1676. "",
  1677. 1
  1678. };
  1679. #endif
  1680. /* Initialisation of zebra and installation of commands. */
  1681. void
  1682. zebra_init (void)
  1683. {
  1684. /* Client list init. */
  1685. zebrad.client_list = list_new ();
  1686. /* Install configuration write function. */
  1687. install_node (&table_node, config_write_table);
  1688. install_node (&forwarding_node, config_write_forwarding);
  1689. #ifdef HAVE_FPM
  1690. install_node (&zebra_node, config_write_fpm);
  1691. #endif
  1692. install_element (VIEW_NODE, &show_ip_forwarding_cmd);
  1693. install_element (ENABLE_NODE, &show_ip_forwarding_cmd);
  1694. install_element (CONFIG_NODE, &ip_forwarding_cmd);
  1695. install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
  1696. install_element (ENABLE_NODE, &show_zebra_client_cmd);
  1697. #ifdef HAVE_NETLINK
  1698. install_element (VIEW_NODE, &show_table_cmd);
  1699. install_element (ENABLE_NODE, &show_table_cmd);
  1700. install_element (CONFIG_NODE, &config_table_cmd);
  1701. #endif /* HAVE_NETLINK */
  1702. #ifdef HAVE_IPV6
  1703. install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
  1704. install_element (ENABLE_NODE, &show_ipv6_forwarding_cmd);
  1705. install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
  1706. install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
  1707. #endif /* HAVE_IPV6 */
  1708. /* Route-map */
  1709. zebra_route_map_init ();
  1710. }
  1711. /* Make zebra server socket, wiping any existing one (see bug #403). */
  1712. void
  1713. zebra_zserv_socket_init (char *path)
  1714. {
  1715. #ifdef HAVE_TCP_ZEBRA
  1716. zebra_serv ();
  1717. #else
  1718. zebra_serv_un (path ? path : ZEBRA_SERV_PATH);
  1719. #endif /* HAVE_TCP_ZEBRA */
  1720. }