zserv.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021
  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. 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. 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. int
  149. zsend_interface_add (struct zserv *client, struct interface *ifp)
  150. {
  151. struct stream *s;
  152. /* Check this client need interface information. */
  153. if (! client->ifinfo)
  154. return -1;
  155. s = client->obuf;
  156. stream_reset (s);
  157. /* Place holder for size. */
  158. stream_putw (s, 0);
  159. /* Message type. */
  160. stream_putc (s, ZEBRA_INTERFACE_ADD);
  161. /* Interface information. */
  162. stream_put (s, ifp->name, INTERFACE_NAMSIZ);
  163. stream_putl (s, ifp->ifindex);
  164. stream_putc (s, ifp->status);
  165. stream_putl (s, ifp->flags);
  166. stream_putl (s, ifp->metric);
  167. stream_putl (s, ifp->mtu);
  168. stream_putl (s, ifp->bandwidth);
  169. #ifdef HAVE_SOCKADDR_DL
  170. stream_put (s, &ifp->sdl, sizeof (ifp->sdl));
  171. #else
  172. stream_putl (s, ifp->hw_addr_len);
  173. if (ifp->hw_addr_len)
  174. stream_put (s, ifp->hw_addr, ifp->hw_addr_len);
  175. #endif /* HAVE_SOCKADDR_DL */
  176. /* Write packet size. */
  177. stream_putw_at (s, 0, stream_get_endp (s));
  178. zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
  179. return 0;
  180. }
  181. /* Interface deletion from zebra daemon. */
  182. int
  183. zsend_interface_delete (struct zserv *client, struct interface *ifp)
  184. {
  185. struct stream *s;
  186. /* Check this client need interface information. */
  187. if (! client->ifinfo)
  188. return -1;
  189. s = client->obuf;
  190. stream_reset (s);
  191. /* Packet length placeholder. */
  192. stream_putw (s, 0);
  193. /* Interface information. */
  194. stream_putc (s, ZEBRA_INTERFACE_DELETE);
  195. stream_put (s, ifp->name, INTERFACE_NAMSIZ);
  196. stream_putl (s, ifp->ifindex);
  197. stream_putc (s, ifp->status);
  198. stream_putl (s, ifp->flags);
  199. stream_putl (s, ifp->metric);
  200. stream_putl (s, ifp->mtu);
  201. stream_putl (s, ifp->bandwidth);
  202. /* Write packet length. */
  203. stream_putw_at (s, 0, stream_get_endp (s));
  204. zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
  205. return 0;
  206. }
  207. /* Interface address is added. Send ZEBRA_INTERFACE_ADDRESS_ADD to the
  208. client. */
  209. int
  210. zsend_interface_address_add (struct zserv *client, struct interface *ifp,
  211. struct connected *ifc)
  212. {
  213. int blen;
  214. struct stream *s;
  215. struct prefix *p;
  216. /* Check this client need interface information. */
  217. if (! client->ifinfo)
  218. return -1;
  219. s = client->obuf;
  220. stream_reset (s);
  221. /* Place holder for size. */
  222. stream_putw (s, 0);
  223. stream_putc (s, ZEBRA_INTERFACE_ADDRESS_ADD);
  224. stream_putl (s, ifp->ifindex);
  225. /* Interface address flag. */
  226. stream_putc (s, ifc->flags);
  227. /* Prefix information. */
  228. p = ifc->address;
  229. stream_putc (s, p->family);
  230. blen = prefix_blen (p);
  231. stream_put (s, &p->u.prefix, blen);
  232. stream_putc (s, p->prefixlen);
  233. /* Destination. */
  234. p = ifc->destination;
  235. if (p)
  236. stream_put (s, &p->u.prefix, blen);
  237. else
  238. stream_put (s, NULL, blen);
  239. /* Write packet size. */
  240. stream_putw_at (s, 0, stream_get_endp (s));
  241. zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
  242. return 0;
  243. }
  244. /* Interface address is deleted. Send ZEBRA_INTERFACE_ADDRESS_DELETE
  245. to the client. */
  246. int
  247. zsend_interface_address_delete (struct zserv *client, struct interface *ifp,
  248. struct connected *ifc)
  249. {
  250. int blen;
  251. struct stream *s;
  252. struct prefix *p;
  253. /* Check this client need interface information. */
  254. if (! client->ifinfo)
  255. return -1;
  256. s = client->obuf;
  257. stream_reset (s);
  258. /* Place holder for size. */
  259. stream_putw (s, 0);
  260. stream_putc (s, ZEBRA_INTERFACE_ADDRESS_DELETE);
  261. stream_putl (s, ifp->ifindex);
  262. /* Interface address flag. */
  263. stream_putc (s, ifc->flags);
  264. /* Prefix information. */
  265. p = ifc->address;
  266. stream_putc (s, p->family);
  267. blen = prefix_blen (p);
  268. stream_put (s, &p->u.prefix, blen);
  269. p = ifc->destination;
  270. if (p)
  271. stream_put (s, &p->u.prefix, blen);
  272. else
  273. stream_put (s, NULL, blen);
  274. /* Write packet size. */
  275. stream_putw_at (s, 0, stream_get_endp (s));
  276. zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
  277. return 0;
  278. }
  279. int
  280. zsend_interface_up (struct zserv *client, struct interface *ifp)
  281. {
  282. struct stream *s;
  283. /* Check this client need interface information. */
  284. if (! client->ifinfo)
  285. return -1;
  286. s = client->obuf;
  287. stream_reset (s);
  288. /* Place holder for size. */
  289. stream_putw (s, 0);
  290. /* Zebra command. */
  291. stream_putc (s, ZEBRA_INTERFACE_UP);
  292. /* Interface information. */
  293. stream_put (s, ifp->name, INTERFACE_NAMSIZ);
  294. stream_putl (s, ifp->ifindex);
  295. stream_putc (s, ifp->status);
  296. stream_putl (s, ifp->flags);
  297. stream_putl (s, ifp->metric);
  298. stream_putl (s, ifp->mtu);
  299. stream_putl (s, ifp->bandwidth);
  300. /* Write packet size. */
  301. stream_putw_at (s, 0, stream_get_endp (s));
  302. zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
  303. return 0;
  304. }
  305. int
  306. zsend_interface_down (struct zserv *client, struct interface *ifp)
  307. {
  308. struct stream *s;
  309. /* Check this client need interface information. */
  310. if (! client->ifinfo)
  311. return -1;
  312. s = client->obuf;
  313. stream_reset (s);
  314. /* Place holder for size. */
  315. stream_putw (s, 0);
  316. /* Zebra command. */
  317. stream_putc (s, ZEBRA_INTERFACE_DOWN);
  318. /* Interface information. */
  319. stream_put (s, ifp->name, INTERFACE_NAMSIZ);
  320. stream_putl (s, ifp->ifindex);
  321. stream_putc (s, ifp->status);
  322. stream_putl (s, ifp->flags);
  323. stream_putl (s, ifp->metric);
  324. stream_putl (s, ifp->mtu);
  325. stream_putl (s, ifp->bandwidth);
  326. /* Write packet size. */
  327. stream_putw_at (s, 0, stream_get_endp (s));
  328. zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
  329. return 0;
  330. }
  331. int
  332. zsend_ipv4_add_multipath (struct zserv *client, struct prefix *p,
  333. struct rib *rib)
  334. {
  335. int psize;
  336. struct stream *s;
  337. struct nexthop *nexthop;
  338. struct in_addr empty;
  339. empty.s_addr = 0;
  340. s = client->obuf;
  341. stream_reset (s);
  342. /* Place holder for size. */
  343. stream_putw (s, 0);
  344. /* Put command, type and nexthop. */
  345. stream_putc (s, ZEBRA_IPV4_ROUTE_ADD);
  346. stream_putc (s, rib->type);
  347. stream_putc (s, rib->flags);
  348. stream_putc (s, ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_IFINDEX | ZAPI_MESSAGE_METRIC);
  349. /* Prefix. */
  350. psize = PSIZE (p->prefixlen);
  351. stream_putc (s, p->prefixlen);
  352. stream_write (s, (u_char *)&p->u.prefix, psize);
  353. /* Nexthop */
  354. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  355. {
  356. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
  357. {
  358. stream_putc (s, 1);
  359. /* XXX: Waht's about NEXTHOP_TYPE_IPV4_IFNAME ? */
  360. if (nexthop->type == NEXTHOP_TYPE_IPV4
  361. || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
  362. stream_put_in_addr (s, &nexthop->gate.ipv4);
  363. else
  364. stream_put_in_addr (s, &empty);
  365. /* Interface index. */
  366. stream_putc (s, 1);
  367. stream_putl (s, nexthop->ifindex);
  368. break;
  369. }
  370. }
  371. /* Metric */
  372. stream_putl (s, rib->metric);
  373. /* Write packet size. */
  374. stream_putw_at (s, 0, stream_get_endp (s));
  375. zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
  376. return 0;
  377. }
  378. int
  379. zsend_ipv4_delete_multipath (struct zserv *client, struct prefix *p,
  380. struct rib *rib)
  381. {
  382. int psize;
  383. struct stream *s;
  384. struct nexthop *nexthop;
  385. struct in_addr empty;
  386. empty.s_addr = 0;
  387. s = client->obuf;
  388. stream_reset (s);
  389. /* Place holder for size. */
  390. stream_putw (s, 0);
  391. /* Put command, type and nexthop. */
  392. stream_putc (s, ZEBRA_IPV4_ROUTE_DELETE);
  393. stream_putc (s, rib->type);
  394. stream_putc (s, rib->flags);
  395. stream_putc (s, ZAPI_MESSAGE_NEXTHOP|ZAPI_MESSAGE_IFINDEX);
  396. /* Prefix. */
  397. psize = PSIZE (p->prefixlen);
  398. stream_putc (s, p->prefixlen);
  399. stream_write (s, (u_char *)&p->u.prefix, psize);
  400. /* Nexthop */
  401. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  402. {
  403. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
  404. {
  405. stream_putc (s, 1);
  406. if (nexthop->type == NEXTHOP_TYPE_IPV4)
  407. stream_put_in_addr (s, &nexthop->gate.ipv4);
  408. else
  409. stream_put_in_addr (s, &empty);
  410. /* Interface index. */
  411. stream_putc (s, 1);
  412. stream_putl (s, nexthop->ifindex);
  413. break;
  414. }
  415. }
  416. /* Write packet size. */
  417. stream_putw_at (s, 0, stream_get_endp (s));
  418. zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
  419. return 0;
  420. }
  421. #if 0
  422. #warning oldies
  423. int
  424. zsend_ipv4_add (struct zserv *client, int type, int flags,
  425. struct prefix_ipv4 *p, struct in_addr *nexthop,
  426. unsigned int ifindex)
  427. {
  428. int psize;
  429. struct stream *s;
  430. s = client->obuf;
  431. stream_reset (s);
  432. /* Place holder for size. */
  433. stream_putw (s, 0);
  434. /* Put command, type and nexthop. */
  435. stream_putc (s, ZEBRA_IPV4_ROUTE_ADD);
  436. stream_putc (s, type);
  437. stream_putc (s, flags);
  438. stream_putc (s, ZAPI_MESSAGE_NEXTHOP|ZAPI_MESSAGE_IFINDEX);
  439. /* Prefix. */
  440. psize = PSIZE (p->prefixlen);
  441. stream_putc (s, p->prefixlen);
  442. stream_write (s, (u_char *)&p->prefix, psize);
  443. /* Nexthop */
  444. stream_putc (s, 1);
  445. stream_put_in_addr (s, nexthop);
  446. /* Interface index. */
  447. stream_putc (s, 1);
  448. stream_putl (s, ifindex);
  449. /* Write packet size. */
  450. stream_putw_at (s, 0, stream_get_endp (s));
  451. zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
  452. return 0;
  453. }
  454. int
  455. zsend_ipv4_delete (struct zserv *client, int type, int flags,
  456. struct prefix_ipv4 *p, struct in_addr *nexthop,
  457. unsigned int ifindex)
  458. {
  459. int psize;
  460. struct stream *s;
  461. s = client->obuf;
  462. stream_reset (s);
  463. /* Place holder for size. */
  464. stream_putw (s, 0);
  465. /* Put command, type and nexthop. */
  466. stream_putc (s, ZEBRA_IPV4_ROUTE_DELETE);
  467. stream_putc (s, type);
  468. stream_putc (s, flags);
  469. stream_putc (s, ZAPI_MESSAGE_NEXTHOP|ZAPI_MESSAGE_IFINDEX);
  470. /* Prefix. */
  471. psize = PSIZE (p->prefixlen);
  472. stream_putc (s, p->prefixlen);
  473. stream_write (s, (u_char *)&p->prefix, psize);
  474. /* Nexthop */
  475. stream_putc (s, 1);
  476. stream_put_in_addr (s, nexthop);
  477. /* Interface index. */
  478. stream_putc (s, 1);
  479. stream_putl (s, ifindex);
  480. /* Write packet size. */
  481. stream_putw_at (s, 0, stream_get_endp (s));
  482. zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
  483. return 0;
  484. }
  485. #endif /* oldies */
  486. #ifdef HAVE_IPV6
  487. #if 0
  488. #warning oldies
  489. int
  490. zsend_ipv6_add (struct zserv *client, int type, int flags,
  491. struct prefix_ipv6 *p, struct in6_addr *nexthop,
  492. unsigned int ifindex)
  493. {
  494. int psize;
  495. struct stream *s;
  496. s = client->obuf;
  497. stream_reset (s);
  498. /* Place holder for size. */
  499. stream_putw (s, 0);
  500. /* Put command, type and nexthop. */
  501. stream_putc (s, ZEBRA_IPV6_ROUTE_ADD);
  502. stream_putc (s, type);
  503. stream_putc (s, flags);
  504. stream_putc (s, ZAPI_MESSAGE_NEXTHOP|ZAPI_MESSAGE_IFINDEX);
  505. /* Prefix. */
  506. psize = PSIZE (p->prefixlen);
  507. stream_putc (s, p->prefixlen);
  508. stream_write (s, (u_char *)&p->prefix, psize);
  509. /* Nexthop */
  510. stream_putc (s, 1);
  511. stream_write (s, (u_char *)nexthop, 16);
  512. /* Interface index. */
  513. stream_putc (s, 1);
  514. stream_putl (s, ifindex);
  515. /* Write packet size. */
  516. stream_putw_at (s, 0, stream_get_endp (s));
  517. zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
  518. return 0;
  519. }
  520. #endif /* oldies */
  521. int
  522. zsend_ipv6_add_multipath (struct zserv *client, struct prefix *p,
  523. struct rib *rib)
  524. {
  525. int psize;
  526. struct stream *s;
  527. struct nexthop *nexthop;
  528. struct in6_addr empty;
  529. memset (&empty, 0, sizeof (struct in6_addr));
  530. s = client->obuf;
  531. stream_reset (s);
  532. /* Place holder for size. */
  533. stream_putw (s, 0);
  534. /* Put command, type and nexthop. */
  535. stream_putc (s, ZEBRA_IPV6_ROUTE_ADD);
  536. stream_putc (s, rib->type);
  537. stream_putc (s, rib->flags);
  538. stream_putc (s, ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_IFINDEX | ZAPI_MESSAGE_METRIC);
  539. /* Prefix. */
  540. psize = PSIZE (p->prefixlen);
  541. stream_putc (s, p->prefixlen);
  542. stream_write (s, (u_char *) &p->u.prefix, psize);
  543. /* Nexthop */
  544. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  545. {
  546. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
  547. {
  548. stream_putc (s, 1);
  549. if (nexthop->type == NEXTHOP_TYPE_IPV6
  550. || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX
  551. || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME)
  552. stream_write (s, (u_char *) &nexthop->gate.ipv6, 16);
  553. else
  554. stream_write (s, (u_char *) &empty, 16);
  555. /* Interface index. */
  556. stream_putc (s, 1);
  557. stream_putl (s, nexthop->ifindex);
  558. break;
  559. }
  560. }
  561. /* Metric */
  562. stream_putl (s, rib->metric);
  563. /* Write packet size. */
  564. stream_putw_at (s, 0, stream_get_endp (s));
  565. zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
  566. return 0;
  567. }
  568. #if 0
  569. #warning oldies
  570. int
  571. zsend_ipv6_delete (struct zserv *client, int type, int flags,
  572. struct prefix_ipv6 *p, struct in6_addr *nexthop,
  573. unsigned int ifindex)
  574. {
  575. int psize;
  576. struct stream *s;
  577. s = client->obuf;
  578. stream_reset (s);
  579. /* Place holder for size. */
  580. stream_putw (s, 0);
  581. /* Put command, type and nexthop. */
  582. stream_putc (s, ZEBRA_IPV6_ROUTE_DELETE);
  583. stream_putc (s, type);
  584. stream_putc (s, flags);
  585. stream_putc (s, ZAPI_MESSAGE_NEXTHOP|ZAPI_MESSAGE_IFINDEX);
  586. /* Prefix. */
  587. psize = PSIZE (p->prefixlen);
  588. stream_putc (s, p->prefixlen);
  589. stream_write (s, (u_char *)&p->prefix, psize);
  590. /* Nexthop */
  591. stream_putc (s, 1);
  592. stream_write (s, (u_char *)nexthop, 16);
  593. /* Interface index. */
  594. stream_putc (s, 1);
  595. stream_putl (s, ifindex);
  596. /* Write packet size. */
  597. stream_putw_at (s, 0, stream_get_endp (s));
  598. zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
  599. return 0;
  600. }
  601. #endif /* oldies */
  602. int
  603. zsend_ipv6_delete_multipath (struct zserv *client, struct prefix *p,
  604. struct rib *rib)
  605. {
  606. int psize;
  607. struct stream *s;
  608. struct nexthop *nexthop;
  609. struct in6_addr empty;
  610. memset (&empty, 0, sizeof (struct in6_addr));
  611. s = client->obuf;
  612. stream_reset (s);
  613. /* Place holder for size. */
  614. stream_putw (s, 0);
  615. /* Put command, type and nexthop. */
  616. stream_putc (s, ZEBRA_IPV6_ROUTE_DELETE);
  617. stream_putc (s, rib->type);
  618. stream_putc (s, rib->flags);
  619. stream_putc (s, ZAPI_MESSAGE_NEXTHOP|ZAPI_MESSAGE_IFINDEX);
  620. /* Prefix. */
  621. psize = PSIZE (p->prefixlen);
  622. stream_putc (s, p->prefixlen);
  623. stream_write (s, (u_char *)&p->u.prefix, psize);
  624. /* Nexthop */
  625. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  626. {
  627. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
  628. {
  629. stream_putc (s, 1);
  630. if (nexthop->type == NEXTHOP_TYPE_IPV6)
  631. stream_write (s, (u_char *) &nexthop->gate.ipv6, 16);
  632. else
  633. stream_write (s, (u_char *) &empty, 16);
  634. /* Interface index. */
  635. stream_putc (s, 1);
  636. stream_putl (s, nexthop->ifindex);
  637. break;
  638. }
  639. }
  640. /* Write packet size. */
  641. stream_putw_at (s, 0, stream_get_endp (s));
  642. zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
  643. return 0;
  644. }
  645. int
  646. zsend_ipv6_nexthop_lookup (struct zserv *client, struct in6_addr *addr)
  647. {
  648. struct stream *s;
  649. struct rib *rib;
  650. unsigned long nump;
  651. u_char num;
  652. struct nexthop *nexthop;
  653. /* Lookup nexthop. */
  654. rib = rib_match_ipv6 (addr);
  655. /* Get output stream. */
  656. s = client->obuf;
  657. stream_reset (s);
  658. /* Fill in result. */
  659. stream_putw (s, 0);
  660. stream_putc (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
  661. stream_put (s, &addr, 16);
  662. if (rib)
  663. {
  664. stream_putl (s, rib->metric);
  665. num = 0;
  666. nump = s->putp;
  667. stream_putc (s, 0);
  668. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  669. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
  670. {
  671. stream_putc (s, nexthop->type);
  672. switch (nexthop->type)
  673. {
  674. case ZEBRA_NEXTHOP_IPV6:
  675. stream_put (s, &nexthop->gate.ipv6, 16);
  676. break;
  677. case ZEBRA_NEXTHOP_IPV6_IFINDEX:
  678. case ZEBRA_NEXTHOP_IPV6_IFNAME:
  679. stream_put (s, &nexthop->gate.ipv6, 16);
  680. stream_putl (s, nexthop->ifindex);
  681. break;
  682. case ZEBRA_NEXTHOP_IFINDEX:
  683. case ZEBRA_NEXTHOP_IFNAME:
  684. stream_putl (s, nexthop->ifindex);
  685. break;
  686. default:
  687. /* do nothing */
  688. break;
  689. }
  690. num++;
  691. }
  692. stream_putc_at (s, nump, num);
  693. }
  694. else
  695. {
  696. stream_putl (s, 0);
  697. stream_putc (s, 0);
  698. }
  699. stream_putw_at (s, 0, stream_get_endp (s));
  700. zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
  701. return 0;
  702. }
  703. #endif /* HAVE_IPV6 */
  704. int
  705. zsend_ipv4_nexthop_lookup (struct zserv *client, struct in_addr addr)
  706. {
  707. struct stream *s;
  708. struct rib *rib;
  709. unsigned long nump;
  710. u_char num;
  711. struct nexthop *nexthop;
  712. /* Lookup nexthop. */
  713. rib = rib_match_ipv4 (addr);
  714. /* Get output stream. */
  715. s = client->obuf;
  716. stream_reset (s);
  717. /* Fill in result. */
  718. stream_putw (s, 0);
  719. stream_putc (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
  720. stream_put_in_addr (s, &addr);
  721. if (rib)
  722. {
  723. stream_putl (s, rib->metric);
  724. num = 0;
  725. nump = s->putp;
  726. stream_putc (s, 0);
  727. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  728. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
  729. {
  730. stream_putc (s, nexthop->type);
  731. switch (nexthop->type)
  732. {
  733. case ZEBRA_NEXTHOP_IPV4:
  734. stream_put_in_addr (s, &nexthop->gate.ipv4);
  735. break;
  736. case ZEBRA_NEXTHOP_IFINDEX:
  737. case ZEBRA_NEXTHOP_IFNAME:
  738. stream_putl (s, nexthop->ifindex);
  739. break;
  740. default:
  741. /* do nothing */
  742. break;
  743. }
  744. num++;
  745. }
  746. stream_putc_at (s, nump, num);
  747. }
  748. else
  749. {
  750. stream_putl (s, 0);
  751. stream_putc (s, 0);
  752. }
  753. stream_putw_at (s, 0, stream_get_endp (s));
  754. zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
  755. return 0;
  756. }
  757. int
  758. zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p)
  759. {
  760. struct stream *s;
  761. struct rib *rib;
  762. unsigned long nump;
  763. u_char num;
  764. struct nexthop *nexthop;
  765. /* Lookup nexthop. */
  766. rib = rib_lookup_ipv4 (p);
  767. /* Get output stream. */
  768. s = client->obuf;
  769. stream_reset (s);
  770. /* Fill in result. */
  771. stream_putw (s, 0);
  772. stream_putc (s, ZEBRA_IPV4_IMPORT_LOOKUP);
  773. stream_put_in_addr (s, &p->prefix);
  774. if (rib)
  775. {
  776. stream_putl (s, rib->metric);
  777. num = 0;
  778. nump = s->putp;
  779. stream_putc (s, 0);
  780. for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
  781. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
  782. {
  783. stream_putc (s, nexthop->type);
  784. switch (nexthop->type)
  785. {
  786. case ZEBRA_NEXTHOP_IPV4:
  787. stream_put_in_addr (s, &nexthop->gate.ipv4);
  788. break;
  789. case ZEBRA_NEXTHOP_IFINDEX:
  790. case ZEBRA_NEXTHOP_IFNAME:
  791. stream_putl (s, nexthop->ifindex);
  792. break;
  793. default:
  794. /* do nothing */
  795. break;
  796. }
  797. num++;
  798. }
  799. stream_putc_at (s, nump, num);
  800. }
  801. else
  802. {
  803. stream_putl (s, 0);
  804. stream_putc (s, 0);
  805. }
  806. stream_putw_at (s, 0, stream_get_endp (s));
  807. zebra_server_send_message (client->sock, s->data, stream_get_endp (s));
  808. return 0;
  809. }
  810. /* Register zebra server interface information. Send current all
  811. interface and address information. */
  812. void
  813. zread_interface_add (struct zserv *client, u_short length)
  814. {
  815. listnode ifnode;
  816. listnode cnode;
  817. struct interface *ifp;
  818. struct connected *c;
  819. /* Interface information is needed. */
  820. client->ifinfo = 1;
  821. for (ifnode = listhead (iflist); ifnode; ifnode = nextnode (ifnode))
  822. {
  823. ifp = getdata (ifnode);
  824. /* Skip pseudo interface. */
  825. if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
  826. continue;
  827. zsend_interface_add (client, ifp);
  828. for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
  829. {
  830. c = getdata (cnode);
  831. if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL))
  832. zsend_interface_address_add (client, ifp, c);
  833. }
  834. }
  835. }
  836. /* Unregister zebra server interface information. */
  837. void
  838. zread_interface_delete (struct zserv *client, u_short length)
  839. {
  840. client->ifinfo = 0;
  841. }
  842. /* This function support multiple nexthop. */
  843. void
  844. zread_ipv4_add (struct zserv *client, u_short length)
  845. {
  846. int i;
  847. struct rib *rib;
  848. struct prefix_ipv4 p;
  849. u_char message;
  850. struct in_addr nexthop;
  851. u_char nexthop_num;
  852. u_char nexthop_type;
  853. struct stream *s;
  854. unsigned int ifindex;
  855. u_char ifname_len;
  856. /* Get input stream. */
  857. s = client->ibuf;
  858. /* Allocate new rib. */
  859. rib = XMALLOC (MTYPE_RIB, sizeof (struct rib));
  860. memset (rib, 0, sizeof (struct rib));
  861. /* Type, flags, message. */
  862. rib->type = stream_getc (s);
  863. rib->flags = stream_getc (s);
  864. message = stream_getc (s);
  865. rib->uptime = time (NULL);
  866. /* IPv4 prefix. */
  867. memset (&p, 0, sizeof (struct prefix_ipv4));
  868. p.family = AF_INET;
  869. p.prefixlen = stream_getc (s);
  870. stream_get (&p.prefix, s, PSIZE (p.prefixlen));
  871. /* Nexthop parse. */
  872. if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
  873. {
  874. nexthop_num = stream_getc (s);
  875. for (i = 0; i < nexthop_num; i++)
  876. {
  877. nexthop_type = stream_getc (s);
  878. switch (nexthop_type)
  879. {
  880. case ZEBRA_NEXTHOP_IFINDEX:
  881. ifindex = stream_getl (s);
  882. nexthop_ifindex_add (rib, ifindex);
  883. break;
  884. case ZEBRA_NEXTHOP_IFNAME:
  885. ifname_len = stream_getc (s);
  886. stream_forward (s, ifname_len);
  887. break;
  888. case ZEBRA_NEXTHOP_IPV4:
  889. nexthop.s_addr = stream_get_ipv4 (s);
  890. nexthop_ipv4_add (rib, &nexthop);
  891. break;
  892. case ZEBRA_NEXTHOP_IPV6:
  893. stream_forward (s, IPV6_MAX_BYTELEN);
  894. break;
  895. case ZEBRA_NEXTHOP_BLACKHOLE:
  896. nexthop_blackhole_add (rib);
  897. break;
  898. }
  899. }
  900. }
  901. /* Distance. */
  902. if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
  903. rib->distance = stream_getc (s);
  904. /* Metric. */
  905. if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
  906. rib->metric = stream_getl (s);
  907. rib_add_ipv4_multipath (&p, rib);
  908. }
  909. /* Zebra server IPv4 prefix delete function. */
  910. void
  911. zread_ipv4_delete (struct zserv *client, u_short length)
  912. {
  913. int i;
  914. struct stream *s;
  915. struct zapi_ipv4 api;
  916. struct in_addr nexthop;
  917. unsigned long ifindex;
  918. struct prefix_ipv4 p;
  919. u_char nexthop_num;
  920. u_char nexthop_type;
  921. u_char ifname_len;
  922. s = client->ibuf;
  923. ifindex = 0;
  924. nexthop.s_addr = 0;
  925. /* Type, flags, message. */
  926. api.type = stream_getc (s);
  927. api.flags = stream_getc (s);
  928. api.message = stream_getc (s);
  929. /* IPv4 prefix. */
  930. memset (&p, 0, sizeof (struct prefix_ipv4));
  931. p.family = AF_INET;
  932. p.prefixlen = stream_getc (s);
  933. stream_get (&p.prefix, s, PSIZE (p.prefixlen));
  934. /* Nexthop, ifindex, distance, metric. */
  935. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
  936. {
  937. nexthop_num = stream_getc (s);
  938. for (i = 0; i < nexthop_num; i++)
  939. {
  940. nexthop_type = stream_getc (s);
  941. switch (nexthop_type)
  942. {
  943. case ZEBRA_NEXTHOP_IFINDEX:
  944. ifindex = stream_getl (s);
  945. break;
  946. case ZEBRA_NEXTHOP_IFNAME:
  947. ifname_len = stream_getc (s);
  948. stream_forward (s, ifname_len);
  949. break;
  950. case ZEBRA_NEXTHOP_IPV4:
  951. nexthop.s_addr = stream_get_ipv4 (s);
  952. break;
  953. case ZEBRA_NEXTHOP_IPV6:
  954. stream_forward (s, IPV6_MAX_BYTELEN);
  955. break;
  956. }
  957. }
  958. }
  959. /* Distance. */
  960. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
  961. api.distance = stream_getc (s);
  962. else
  963. api.distance = 0;
  964. /* Metric. */
  965. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
  966. api.metric = stream_getl (s);
  967. else
  968. api.metric = 0;
  969. rib_delete_ipv4 (api.type, api.flags, &p, &nexthop, ifindex,
  970. client->rtm_table);
  971. }
  972. /* Nexthop lookup for IPv4. */
  973. void
  974. zread_ipv4_nexthop_lookup (struct zserv *client, u_short length)
  975. {
  976. struct in_addr addr;
  977. addr.s_addr = stream_get_ipv4 (client->ibuf);
  978. zsend_ipv4_nexthop_lookup (client, addr);
  979. }
  980. /* Nexthop lookup for IPv4. */
  981. void
  982. zread_ipv4_import_lookup (struct zserv *client, u_short length)
  983. {
  984. struct prefix_ipv4 p;
  985. p.family = AF_INET;
  986. p.prefixlen = stream_getc (client->ibuf);
  987. p.prefix.s_addr = stream_get_ipv4 (client->ibuf);
  988. zsend_ipv4_import_lookup (client, &p);
  989. }
  990. #ifdef HAVE_IPV6
  991. /* Zebra server IPv6 prefix add function. */
  992. void
  993. zread_ipv6_add (struct zserv *client, u_short length)
  994. {
  995. int i;
  996. struct stream *s;
  997. struct zapi_ipv6 api;
  998. struct in6_addr nexthop;
  999. unsigned long ifindex;
  1000. struct prefix_ipv6 p;
  1001. s = client->ibuf;
  1002. ifindex = 0;
  1003. memset (&nexthop, 0, sizeof (struct in6_addr));
  1004. /* Type, flags, message. */
  1005. api.type = stream_getc (s);
  1006. api.flags = stream_getc (s);
  1007. api.message = stream_getc (s);
  1008. /* IPv4 prefix. */
  1009. memset (&p, 0, sizeof (struct prefix_ipv6));
  1010. p.family = AF_INET6;
  1011. p.prefixlen = stream_getc (s);
  1012. stream_get (&p.prefix, s, PSIZE (p.prefixlen));
  1013. /* Nexthop, ifindex, distance, metric. */
  1014. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
  1015. {
  1016. u_char nexthop_type;
  1017. api.nexthop_num = stream_getc (s);
  1018. for (i = 0; i < api.nexthop_num; i++)
  1019. {
  1020. nexthop_type = stream_getc (s);
  1021. switch (nexthop_type)
  1022. {
  1023. case ZEBRA_NEXTHOP_IPV6:
  1024. stream_get (&nexthop, s, 16);
  1025. break;
  1026. case ZEBRA_NEXTHOP_IFINDEX:
  1027. ifindex = stream_getl (s);
  1028. break;
  1029. }
  1030. }
  1031. }
  1032. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
  1033. api.distance = stream_getc (s);
  1034. else
  1035. api.distance = 0;
  1036. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
  1037. api.metric = stream_getl (s);
  1038. else
  1039. api.metric = 0;
  1040. if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
  1041. rib_add_ipv6 (api.type, api.flags, &p, NULL, ifindex, 0);
  1042. else
  1043. rib_add_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, 0);
  1044. }
  1045. /* Zebra server IPv6 prefix delete function. */
  1046. void
  1047. zread_ipv6_delete (struct zserv *client, u_short length)
  1048. {
  1049. int i;
  1050. struct stream *s;
  1051. struct zapi_ipv6 api;
  1052. struct in6_addr nexthop;
  1053. unsigned long ifindex;
  1054. struct prefix_ipv6 p;
  1055. s = client->ibuf;
  1056. ifindex = 0;
  1057. memset (&nexthop, 0, sizeof (struct in6_addr));
  1058. /* Type, flags, message. */
  1059. api.type = stream_getc (s);
  1060. api.flags = stream_getc (s);
  1061. api.message = stream_getc (s);
  1062. /* IPv4 prefix. */
  1063. memset (&p, 0, sizeof (struct prefix_ipv6));
  1064. p.family = AF_INET6;
  1065. p.prefixlen = stream_getc (s);
  1066. stream_get (&p.prefix, s, PSIZE (p.prefixlen));
  1067. /* Nexthop, ifindex, distance, metric. */
  1068. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
  1069. {
  1070. u_char nexthop_type;
  1071. api.nexthop_num = stream_getc (s);
  1072. for (i = 0; i < api.nexthop_num; i++)
  1073. {
  1074. nexthop_type = stream_getc (s);
  1075. switch (nexthop_type)
  1076. {
  1077. case ZEBRA_NEXTHOP_IPV6:
  1078. stream_get (&nexthop, s, 16);
  1079. break;
  1080. case ZEBRA_NEXTHOP_IFINDEX:
  1081. ifindex = stream_getl (s);
  1082. break;
  1083. }
  1084. }
  1085. }
  1086. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
  1087. api.distance = stream_getc (s);
  1088. else
  1089. api.distance = 0;
  1090. if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
  1091. api.metric = stream_getl (s);
  1092. else
  1093. api.metric = 0;
  1094. if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
  1095. rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, 0);
  1096. else
  1097. rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, 0);
  1098. }
  1099. void
  1100. zebra_read_ipv6 (int command, struct zserv *client, u_short length)
  1101. {
  1102. u_char type;
  1103. u_char flags;
  1104. struct in6_addr nexthop, *gate;
  1105. u_char *lim;
  1106. u_char *pnt;
  1107. unsigned int ifindex;
  1108. pnt = stream_pnt (client->ibuf);
  1109. lim = pnt + length;
  1110. type = stream_getc (client->ibuf);
  1111. flags = stream_getc (client->ibuf);
  1112. stream_get (&nexthop, client->ibuf, sizeof (struct in6_addr));
  1113. while (stream_pnt (client->ibuf) < lim)
  1114. {
  1115. int size;
  1116. struct prefix_ipv6 p;
  1117. ifindex = stream_getl (client->ibuf);
  1118. memset (&p, 0, sizeof (struct prefix_ipv6));
  1119. p.family = AF_INET6;
  1120. p.prefixlen = stream_getc (client->ibuf);
  1121. size = PSIZE(p.prefixlen);
  1122. stream_get (&p.prefix, client->ibuf, size);
  1123. if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
  1124. gate = NULL;
  1125. else
  1126. gate = &nexthop;
  1127. if (command == ZEBRA_IPV6_ROUTE_ADD)
  1128. rib_add_ipv6 (type, flags, &p, gate, ifindex, 0);
  1129. else
  1130. rib_delete_ipv6 (type, flags, &p, gate, ifindex, 0);
  1131. }
  1132. }
  1133. void
  1134. zread_ipv6_nexthop_lookup (struct zserv *client, u_short length)
  1135. {
  1136. struct in6_addr addr;
  1137. char buf[BUFSIZ];
  1138. stream_get (&addr, client->ibuf, 16);
  1139. printf ("DEBUG %s\n", inet_ntop (AF_INET6, &addr, buf, BUFSIZ));
  1140. zsend_ipv6_nexthop_lookup (client, &addr);
  1141. }
  1142. #endif /* HAVE_IPV6 */
  1143. /* Close zebra client. */
  1144. void
  1145. zebra_client_close (struct zserv *client)
  1146. {
  1147. /* Close file descriptor. */
  1148. if (client->sock)
  1149. {
  1150. close (client->sock);
  1151. client->sock = -1;
  1152. }
  1153. /* Free stream buffers. */
  1154. if (client->ibuf)
  1155. stream_free (client->ibuf);
  1156. if (client->obuf)
  1157. stream_free (client->obuf);
  1158. /* Release threads. */
  1159. if (client->t_read)
  1160. thread_cancel (client->t_read);
  1161. if (client->t_write)
  1162. thread_cancel (client->t_write);
  1163. /* Free client structure. */
  1164. listnode_delete (zebrad.client_list, client);
  1165. XFREE (0, client);
  1166. }
  1167. /* Make new client. */
  1168. void
  1169. zebra_client_create (int sock)
  1170. {
  1171. struct zserv *client;
  1172. client = XCALLOC (0, sizeof (struct zserv));
  1173. /* Make client input/output buffer. */
  1174. client->sock = sock;
  1175. client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
  1176. client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
  1177. /* Set table number. */
  1178. client->rtm_table = zebrad.rtm_table_default;
  1179. /* Add this client to linked list. */
  1180. listnode_add (zebrad.client_list, client);
  1181. /* Make new read thread. */
  1182. zebra_event (ZEBRA_READ, sock, client);
  1183. }
  1184. /* Handler of zebra service request. */
  1185. int
  1186. zebra_client_read (struct thread *thread)
  1187. {
  1188. int sock;
  1189. struct zserv *client;
  1190. int nbyte;
  1191. u_short length;
  1192. u_char command;
  1193. /* Get thread data. Reset reading thread because I'm running. */
  1194. sock = THREAD_FD (thread);
  1195. client = THREAD_ARG (thread);
  1196. client->t_read = NULL;
  1197. /* Read length and command. */
  1198. nbyte = stream_read (client->ibuf, sock, 3);
  1199. if (nbyte <= 0)
  1200. {
  1201. if (IS_ZEBRA_DEBUG_EVENT)
  1202. zlog_info ("connection closed socket [%d]", sock);
  1203. zebra_client_close (client);
  1204. return -1;
  1205. }
  1206. length = stream_getw (client->ibuf);
  1207. command = stream_getc (client->ibuf);
  1208. if (length < 3)
  1209. {
  1210. if (IS_ZEBRA_DEBUG_EVENT)
  1211. zlog_info ("length %d is less than 3 ", length);
  1212. zebra_client_close (client);
  1213. return -1;
  1214. }
  1215. length -= 3;
  1216. /* Read rest of data. */
  1217. if (length)
  1218. {
  1219. nbyte = stream_read (client->ibuf, sock, length);
  1220. if (nbyte <= 0)
  1221. {
  1222. if (IS_ZEBRA_DEBUG_EVENT)
  1223. zlog_info ("connection closed [%d] when reading zebra data", sock);
  1224. zebra_client_close (client);
  1225. return -1;
  1226. }
  1227. }
  1228. /* Debug packet information. */
  1229. if (IS_ZEBRA_DEBUG_EVENT)
  1230. zlog_info ("zebra message comes from socket [%d]", sock);
  1231. if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
  1232. zlog_info ("zebra message received [%s] %d",
  1233. zebra_command_str[command], length);
  1234. switch (command)
  1235. {
  1236. case ZEBRA_INTERFACE_ADD:
  1237. zread_interface_add (client, length);
  1238. break;
  1239. case ZEBRA_INTERFACE_DELETE:
  1240. zread_interface_delete (client, length);
  1241. break;
  1242. case ZEBRA_IPV4_ROUTE_ADD:
  1243. zread_ipv4_add (client, length);
  1244. break;
  1245. case ZEBRA_IPV4_ROUTE_DELETE:
  1246. zread_ipv4_delete (client, length);
  1247. break;
  1248. #ifdef HAVE_IPV6
  1249. case ZEBRA_IPV6_ROUTE_ADD:
  1250. zread_ipv6_add (client, length);
  1251. break;
  1252. case ZEBRA_IPV6_ROUTE_DELETE:
  1253. zread_ipv6_delete (client, length);
  1254. break;
  1255. #endif /* HAVE_IPV6 */
  1256. case ZEBRA_REDISTRIBUTE_ADD:
  1257. zebra_redistribute_add (command, client, length);
  1258. break;
  1259. case ZEBRA_REDISTRIBUTE_DELETE:
  1260. zebra_redistribute_delete (command, client, length);
  1261. break;
  1262. case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
  1263. zebra_redistribute_default_add (command, client, length);
  1264. break;
  1265. case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
  1266. zebra_redistribute_default_delete (command, client, length);
  1267. break;
  1268. case ZEBRA_IPV4_NEXTHOP_LOOKUP:
  1269. zread_ipv4_nexthop_lookup (client, length);
  1270. break;
  1271. #ifdef HAVE_IPV6
  1272. case ZEBRA_IPV6_NEXTHOP_LOOKUP:
  1273. zread_ipv6_nexthop_lookup (client, length);
  1274. break;
  1275. #endif /* HAVE_IPV6 */
  1276. case ZEBRA_IPV4_IMPORT_LOOKUP:
  1277. zread_ipv4_import_lookup (client, length);
  1278. break;
  1279. default:
  1280. zlog_info ("Zebra received unknown command %d", command);
  1281. break;
  1282. }
  1283. stream_reset (client->ibuf);
  1284. zebra_event (ZEBRA_READ, sock, client);
  1285. return 0;
  1286. }
  1287. /* Write output buffer to the socket. */
  1288. void
  1289. zebra_write (struct thread *thread)
  1290. {
  1291. int sock;
  1292. struct zserv *client;
  1293. /* Thread treatment. */
  1294. sock = THREAD_FD (thread);
  1295. client = THREAD_ARG (thread);
  1296. client->t_write = NULL;
  1297. stream_flush (client->obuf, sock);
  1298. }
  1299. /* Accept code of zebra server socket. */
  1300. int
  1301. zebra_accept (struct thread *thread)
  1302. {
  1303. int val;
  1304. int accept_sock;
  1305. int client_sock;
  1306. struct sockaddr_in client;
  1307. socklen_t len;
  1308. accept_sock = THREAD_FD (thread);
  1309. len = sizeof (struct sockaddr_in);
  1310. client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
  1311. if (client_sock < 0)
  1312. {
  1313. zlog_warn ("Can't accept zebra socket: %s", strerror (errno));
  1314. return -1;
  1315. }
  1316. /* Make client socket non-blocking. */
  1317. val = fcntl (client_sock, F_GETFL, 0);
  1318. fcntl (client_sock, F_SETFL, (val | O_NONBLOCK));
  1319. /* Create new zebra client. */
  1320. zebra_client_create (client_sock);
  1321. /* Register myself. */
  1322. zebra_event (ZEBRA_SERV, accept_sock, NULL);
  1323. return 0;
  1324. }
  1325. /* Make zebra's server socket. */
  1326. void
  1327. zebra_serv ()
  1328. {
  1329. int ret;
  1330. int accept_sock;
  1331. struct sockaddr_in addr;
  1332. accept_sock = socket (AF_INET, SOCK_STREAM, 0);
  1333. if (accept_sock < 0)
  1334. {
  1335. zlog_warn ("Can't bind to socket: %s", strerror (errno));
  1336. zlog_warn ("zebra can't provice full functionality due to above error");
  1337. return;
  1338. }
  1339. memset (&addr, 0, sizeof (struct sockaddr_in));
  1340. addr.sin_family = AF_INET;
  1341. addr.sin_port = htons (ZEBRA_PORT);
  1342. #ifdef HAVE_SIN_LEN
  1343. addr.sin_len = sizeof (struct sockaddr_in);
  1344. #endif /* HAVE_SIN_LEN */
  1345. addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
  1346. sockopt_reuseaddr (accept_sock);
  1347. sockopt_reuseport (accept_sock);
  1348. if ( zserv_privs.change(ZPRIVS_RAISE) )
  1349. zlog (NULL, LOG_ERR, "Can't raise privileges");
  1350. ret = bind (accept_sock, (struct sockaddr *)&addr,
  1351. sizeof (struct sockaddr_in));
  1352. if (ret < 0)
  1353. {
  1354. zlog_warn ("Can't bind to socket: %s", strerror (errno));
  1355. zlog_warn ("zebra can't provice full functionality due to above error");
  1356. close (accept_sock); /* Avoid sd leak. */
  1357. return;
  1358. }
  1359. if ( zserv_privs.change(ZPRIVS_LOWER) )
  1360. zlog (NULL, LOG_ERR, "Can't lower privileges");
  1361. ret = listen (accept_sock, 1);
  1362. if (ret < 0)
  1363. {
  1364. zlog_warn ("Can't listen to socket: %s", strerror (errno));
  1365. zlog_warn ("zebra can't provice full functionality due to above error");
  1366. close (accept_sock); /* Avoid sd leak. */
  1367. return;
  1368. }
  1369. zebra_event (ZEBRA_SERV, accept_sock, NULL);
  1370. }
  1371. /* For sockaddr_un. */
  1372. #include <sys/un.h>
  1373. /* zebra server UNIX domain socket. */
  1374. void
  1375. zebra_serv_un (char *path)
  1376. {
  1377. int ret;
  1378. int sock, len;
  1379. struct sockaddr_un serv;
  1380. mode_t old_mask;
  1381. /* First of all, unlink existing socket */
  1382. unlink (path);
  1383. /* Set umask */
  1384. old_mask = umask (0077);
  1385. /* Make UNIX domain socket. */
  1386. sock = socket (AF_UNIX, SOCK_STREAM, 0);
  1387. if (sock < 0)
  1388. {
  1389. perror ("sock");
  1390. return;
  1391. }
  1392. /* Make server socket. */
  1393. memset (&serv, 0, sizeof (struct sockaddr_un));
  1394. serv.sun_family = AF_UNIX;
  1395. strncpy (serv.sun_path, path, strlen (path));
  1396. #ifdef HAVE_SUN_LEN
  1397. len = serv.sun_len = SUN_LEN(&serv);
  1398. #else
  1399. len = sizeof (serv.sun_family) + strlen (serv.sun_path);
  1400. #endif /* HAVE_SUN_LEN */
  1401. ret = bind (sock, (struct sockaddr *) &serv, len);
  1402. if (ret < 0)
  1403. {
  1404. perror ("bind");
  1405. close (sock);
  1406. return;
  1407. }
  1408. ret = listen (sock, 5);
  1409. if (ret < 0)
  1410. {
  1411. perror ("listen");
  1412. close (sock);
  1413. return;
  1414. }
  1415. umask (old_mask);
  1416. zebra_event (ZEBRA_SERV, sock, NULL);
  1417. }
  1418. void
  1419. zebra_event (enum event event, int sock, struct zserv *client)
  1420. {
  1421. switch (event)
  1422. {
  1423. case ZEBRA_SERV:
  1424. thread_add_read (zebrad.master, zebra_accept, client, sock);
  1425. break;
  1426. case ZEBRA_READ:
  1427. client->t_read =
  1428. thread_add_read (zebrad.master, zebra_client_read, client, sock);
  1429. break;
  1430. case ZEBRA_WRITE:
  1431. /**/
  1432. break;
  1433. }
  1434. }
  1435. /* Display default rtm_table for all clients. */
  1436. DEFUN (show_table,
  1437. show_table_cmd,
  1438. "show table",
  1439. SHOW_STR
  1440. "default routing table to use for all clients\n")
  1441. {
  1442. vty_out (vty, "table %d%s", zebrad.rtm_table_default,
  1443. VTY_NEWLINE);
  1444. return CMD_SUCCESS;
  1445. }
  1446. DEFUN (config_table,
  1447. config_table_cmd,
  1448. "table TABLENO",
  1449. "Configure target kernel routing table\n"
  1450. "TABLE integer\n")
  1451. {
  1452. zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10);
  1453. return CMD_SUCCESS;
  1454. }
  1455. DEFUN (ip_forwarding,
  1456. ip_forwarding_cmd,
  1457. "ip forwarding",
  1458. IP_STR
  1459. "Turn on IP forwarding")
  1460. {
  1461. int ret;
  1462. ret = ipforward ();
  1463. if (ret != 0)
  1464. {
  1465. vty_out (vty, "IP forwarding is already on%s", VTY_NEWLINE);
  1466. return CMD_ERR_NOTHING_TODO;
  1467. }
  1468. ret = ipforward_on ();
  1469. if (ret == 0)
  1470. {
  1471. vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
  1472. return CMD_WARNING;
  1473. }
  1474. return CMD_SUCCESS;
  1475. }
  1476. DEFUN (no_ip_forwarding,
  1477. no_ip_forwarding_cmd,
  1478. "no ip forwarding",
  1479. NO_STR
  1480. IP_STR
  1481. "Turn off IP forwarding")
  1482. {
  1483. int ret;
  1484. ret = ipforward ();
  1485. if (ret == 0)
  1486. {
  1487. vty_out (vty, "IP forwarding is already off%s", VTY_NEWLINE);
  1488. return CMD_ERR_NOTHING_TODO;
  1489. }
  1490. ret = ipforward_off ();
  1491. if (ret != 0)
  1492. {
  1493. vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
  1494. return CMD_WARNING;
  1495. }
  1496. return CMD_SUCCESS;
  1497. }
  1498. /* This command is for debugging purpose. */
  1499. DEFUN (show_zebra_client,
  1500. show_zebra_client_cmd,
  1501. "show zebra client",
  1502. SHOW_STR
  1503. "Zebra information"
  1504. "Client information")
  1505. {
  1506. listnode node;
  1507. struct zserv *client;
  1508. for (node = listhead (zebrad.client_list); node; nextnode (node))
  1509. {
  1510. client = getdata (node);
  1511. vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE);
  1512. }
  1513. return CMD_SUCCESS;
  1514. }
  1515. /* Table configuration write function. */
  1516. int
  1517. config_write_table (struct vty *vty)
  1518. {
  1519. if (zebrad.rtm_table_default)
  1520. vty_out (vty, "table %d%s", zebrad.rtm_table_default,
  1521. VTY_NEWLINE);
  1522. return 0;
  1523. }
  1524. /* table node for routing tables. */
  1525. struct cmd_node table_node =
  1526. {
  1527. TABLE_NODE,
  1528. "", /* This node has no interface. */
  1529. 1
  1530. };
  1531. /* Only display ip forwarding is enabled or not. */
  1532. DEFUN (show_ip_forwarding,
  1533. show_ip_forwarding_cmd,
  1534. "show ip forwarding",
  1535. SHOW_STR
  1536. IP_STR
  1537. "IP forwarding status\n")
  1538. {
  1539. int ret;
  1540. ret = ipforward ();
  1541. if (ret == 0)
  1542. vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
  1543. else
  1544. vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
  1545. return CMD_SUCCESS;
  1546. }
  1547. #ifdef HAVE_IPV6
  1548. /* Only display ipv6 forwarding is enabled or not. */
  1549. DEFUN (show_ipv6_forwarding,
  1550. show_ipv6_forwarding_cmd,
  1551. "show ipv6 forwarding",
  1552. SHOW_STR
  1553. "IPv6 information\n"
  1554. "Forwarding status\n")
  1555. {
  1556. int ret;
  1557. ret = ipforward_ipv6 ();
  1558. switch (ret)
  1559. {
  1560. case -1:
  1561. vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
  1562. break;
  1563. case 0:
  1564. vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
  1565. break;
  1566. case 1:
  1567. vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
  1568. break;
  1569. default:
  1570. vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
  1571. break;
  1572. }
  1573. return CMD_SUCCESS;
  1574. }
  1575. DEFUN (ipv6_forwarding,
  1576. ipv6_forwarding_cmd,
  1577. "ipv6 forwarding",
  1578. IPV6_STR
  1579. "Turn on IPv6 forwarding")
  1580. {
  1581. int ret;
  1582. ret = ipforward_ipv6_on ();
  1583. if (ret != 0)
  1584. {
  1585. vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
  1586. return CMD_WARNING;
  1587. }
  1588. return CMD_SUCCESS;
  1589. }
  1590. DEFUN (no_ipv6_forwarding,
  1591. no_ipv6_forwarding_cmd,
  1592. "no ipv6 forwarding",
  1593. NO_STR
  1594. IPV6_STR
  1595. "Turn off IPv6 forwarding")
  1596. {
  1597. int ret;
  1598. ret = ipforward_ipv6_off ();
  1599. if (ret != 0)
  1600. {
  1601. vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
  1602. return CMD_WARNING;
  1603. }
  1604. return CMD_SUCCESS;
  1605. }
  1606. #endif /* HAVE_IPV6 */
  1607. /* IPForwarding configuration write function. */
  1608. int
  1609. config_write_forwarding (struct vty *vty)
  1610. {
  1611. if (! ipforward ())
  1612. vty_out (vty, "no ip forwarding%s", VTY_NEWLINE);
  1613. #ifdef HAVE_IPV6
  1614. if (! ipforward_ipv6 ())
  1615. vty_out (vty, "no ipv6 forwarding%s", VTY_NEWLINE);
  1616. #endif /* HAVE_IPV6 */
  1617. vty_out (vty, "!%s", VTY_NEWLINE);
  1618. return 0;
  1619. }
  1620. /* table node for routing tables. */
  1621. struct cmd_node forwarding_node =
  1622. {
  1623. FORWARDING_NODE,
  1624. "", /* This node has no interface. */
  1625. 1
  1626. };
  1627. /* Initialisation of zebra and installation of commands. */
  1628. void
  1629. zebra_init ()
  1630. {
  1631. /* Client list init. */
  1632. zebrad.client_list = list_new ();
  1633. /* Forwarding is on by default. */
  1634. ipforward_on ();
  1635. #ifdef HAVE_IPV6
  1636. ipforward_ipv6_on ();
  1637. #endif /* HAVE_IPV6 */
  1638. /* Make zebra server socket. */
  1639. #ifdef HAVE_TCP_ZEBRA
  1640. zebra_serv ();
  1641. #else
  1642. zebra_serv_un (ZEBRA_SERV_PATH);
  1643. #endif /* HAVE_TCP_ZEBRA */
  1644. /* Install configuration write function. */
  1645. install_node (&table_node, config_write_table);
  1646. install_node (&forwarding_node, config_write_forwarding);
  1647. install_element (VIEW_NODE, &show_ip_forwarding_cmd);
  1648. install_element (ENABLE_NODE, &show_ip_forwarding_cmd);
  1649. install_element (CONFIG_NODE, &ip_forwarding_cmd);
  1650. install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
  1651. install_element (ENABLE_NODE, &show_zebra_client_cmd);
  1652. #ifdef HAVE_NETLINK
  1653. install_element (VIEW_NODE, &show_table_cmd);
  1654. install_element (ENABLE_NODE, &show_table_cmd);
  1655. install_element (CONFIG_NODE, &config_table_cmd);
  1656. #endif /* HAVE_NETLINK */
  1657. #ifdef HAVE_IPV6
  1658. install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
  1659. install_element (ENABLE_NODE, &show_ipv6_forwarding_cmd);
  1660. install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
  1661. install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
  1662. #endif /* HAVE_IPV6 */
  1663. FIFO_INIT(&message_queue);
  1664. t_write = NULL;
  1665. }