zserv.c 43 KB

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