zserv.c 51 KB

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