zserv.c 42 KB

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