zserv.c 59 KB

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