zserv.c 49 KB

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