zserv.c 46 KB

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