ripngd.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026
  1. /* RIPng daemon
  2. * Copyright (C) 1998, 1999 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 Free
  18. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. * 02111-1307, USA.
  20. */
  21. #include <zebra.h>
  22. #include "prefix.h"
  23. #include "filter.h"
  24. #include "log.h"
  25. #include "thread.h"
  26. #include "memory.h"
  27. #include "if.h"
  28. #include "stream.h"
  29. #include "table.h"
  30. #include "command.h"
  31. #include "sockopt.h"
  32. #include "distribute.h"
  33. #include "plist.h"
  34. #include "routemap.h"
  35. #include "if_rmap.h"
  36. #include "privs.h"
  37. #include "ripngd/ripngd.h"
  38. #include "ripngd/ripng_route.h"
  39. #include "ripngd/ripng_debug.h"
  40. #include "ripngd/ripng_nexthop.h"
  41. /* RIPng structure which includes many parameters related to RIPng
  42. protocol. If ripng couldn't active or ripng doesn't configured,
  43. ripng->fd must be negative value. */
  44. struct ripng *ripng = NULL;
  45. enum
  46. {
  47. ripng_all_route,
  48. ripng_changed_route,
  49. };
  50. extern struct zebra_privs_t ripngd_privs;
  51. /* Prototypes. */
  52. void
  53. ripng_output_process (struct interface *, struct sockaddr_in6 *, int);
  54. int
  55. ripng_triggered_update (struct thread *);
  56. /* RIPng next hop specification. */
  57. struct ripng_nexthop
  58. {
  59. enum ripng_nexthop_type
  60. {
  61. RIPNG_NEXTHOP_UNSPEC,
  62. RIPNG_NEXTHOP_ADDRESS
  63. } flag;
  64. struct in6_addr address;
  65. };
  66. static int
  67. ripng_route_rte (struct ripng_info *rinfo)
  68. {
  69. return (rinfo->type == ZEBRA_ROUTE_RIPNG && rinfo->sub_type == RIPNG_ROUTE_RTE);
  70. }
  71. /* Allocate new ripng information. */
  72. struct ripng_info *
  73. ripng_info_new ()
  74. {
  75. struct ripng_info *new;
  76. new = XCALLOC (MTYPE_RIPNG_ROUTE, sizeof (struct ripng_info));
  77. return new;
  78. }
  79. /* Free ripng information. */
  80. void
  81. ripng_info_free (struct ripng_info *rinfo)
  82. {
  83. XFREE (MTYPE_RIPNG_ROUTE, rinfo);
  84. }
  85. /* Create ripng socket. */
  86. static int
  87. ripng_make_socket (void)
  88. {
  89. int ret;
  90. int sock;
  91. struct sockaddr_in6 ripaddr;
  92. sock = socket (AF_INET6, SOCK_DGRAM, 0);
  93. if (sock < 0)
  94. {
  95. zlog (NULL, LOG_ERR, "Can't make ripng socket");
  96. return sock;
  97. }
  98. ret = setsockopt_so_recvbuf (sock, 8096);
  99. if (ret < 0)
  100. return ret;
  101. ret = setsockopt_ipv6_pktinfo (sock, 1);
  102. if (ret < 0)
  103. return ret;
  104. ret = setsockopt_ipv6_multicast_hops (sock, 255);
  105. if (ret < 0)
  106. return ret;
  107. ret = setsockopt_ipv6_multicast_loop (sock, 0);
  108. if (ret < 0)
  109. return ret;
  110. ret = setsockopt_ipv6_hoplimit (sock, 1);
  111. if (ret < 0)
  112. return ret;
  113. memset (&ripaddr, 0, sizeof (ripaddr));
  114. ripaddr.sin6_family = AF_INET6;
  115. #ifdef SIN6_LEN
  116. ripaddr.sin6_len = sizeof (struct sockaddr_in6);
  117. #endif /* SIN6_LEN */
  118. ripaddr.sin6_port = htons (RIPNG_PORT_DEFAULT);
  119. if (ripngd_privs.change (ZPRIVS_RAISE))
  120. zlog_err ("ripng_make_socket: could not raise privs");
  121. ret = bind (sock, (struct sockaddr *) &ripaddr, sizeof (ripaddr));
  122. if (ret < 0)
  123. {
  124. zlog (NULL, LOG_ERR, "Can't bind ripng socket: %s.", safe_strerror (errno));
  125. if (ripngd_privs.change (ZPRIVS_LOWER))
  126. zlog_err ("ripng_make_socket: could not lower privs");
  127. return ret;
  128. }
  129. if (ripngd_privs.change (ZPRIVS_LOWER))
  130. zlog_err ("ripng_make_socket: could not lower privs");
  131. return sock;
  132. }
  133. /* Send RIPng packet. */
  134. int
  135. ripng_send_packet (caddr_t buf, int bufsize, struct sockaddr_in6 *to,
  136. struct interface *ifp)
  137. {
  138. int ret;
  139. struct msghdr msg;
  140. struct iovec iov;
  141. struct cmsghdr *cmsgptr;
  142. char adata [256];
  143. struct in6_pktinfo *pkt;
  144. struct sockaddr_in6 addr;
  145. if (IS_RIPNG_DEBUG_SEND) {
  146. if (to)
  147. zlog_debug ("send to %s", inet6_ntoa (to->sin6_addr));
  148. zlog_debug (" send interface %s", ifp->name);
  149. zlog_debug (" send packet size %d", bufsize);
  150. }
  151. memset (&addr, 0, sizeof (struct sockaddr_in6));
  152. addr.sin6_family = AF_INET6;
  153. #ifdef SIN6_LEN
  154. addr.sin6_len = sizeof (struct sockaddr_in6);
  155. #endif /* SIN6_LEN */
  156. addr.sin6_flowinfo = htonl (RIPNG_PRIORITY_DEFAULT);
  157. /* When destination is specified. */
  158. if (to != NULL)
  159. {
  160. addr.sin6_addr = to->sin6_addr;
  161. addr.sin6_port = to->sin6_port;
  162. }
  163. else
  164. {
  165. inet_pton(AF_INET6, RIPNG_GROUP, &addr.sin6_addr);
  166. addr.sin6_port = htons (RIPNG_PORT_DEFAULT);
  167. }
  168. msg.msg_name = (void *) &addr;
  169. msg.msg_namelen = sizeof (struct sockaddr_in6);
  170. msg.msg_iov = &iov;
  171. msg.msg_iovlen = 1;
  172. msg.msg_control = (void *) adata;
  173. msg.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
  174. iov.iov_base = buf;
  175. iov.iov_len = bufsize;
  176. cmsgptr = (struct cmsghdr *)adata;
  177. cmsgptr->cmsg_len = CMSG_LEN(sizeof (struct in6_pktinfo));
  178. cmsgptr->cmsg_level = IPPROTO_IPV6;
  179. cmsgptr->cmsg_type = IPV6_PKTINFO;
  180. pkt = (struct in6_pktinfo *) CMSG_DATA (cmsgptr);
  181. memset (&pkt->ipi6_addr, 0, sizeof (struct in6_addr));
  182. pkt->ipi6_ifindex = ifp->ifindex;
  183. ret = sendmsg (ripng->sock, &msg, 0);
  184. if (ret < 0) {
  185. if (to)
  186. zlog_err ("RIPng send fail on %s to %s: %s", ifp->name,
  187. inet6_ntoa (to->sin6_addr), safe_strerror (errno));
  188. else
  189. zlog_err ("RIPng send fail on %s: %s", ifp->name, safe_strerror (errno));
  190. }
  191. return ret;
  192. }
  193. /* Receive UDP RIPng packet from socket. */
  194. static int
  195. ripng_recv_packet (int sock, u_char *buf, int bufsize,
  196. struct sockaddr_in6 *from, unsigned int *ifindex,
  197. int *hoplimit)
  198. {
  199. int ret;
  200. struct msghdr msg;
  201. struct iovec iov;
  202. struct cmsghdr *cmsgptr;
  203. struct in6_addr dst;
  204. /* Ancillary data. This store cmsghdr and in6_pktinfo. But at this
  205. point I can't determine size of cmsghdr */
  206. char adata[1024];
  207. /* Fill in message and iovec. */
  208. msg.msg_name = (void *) from;
  209. msg.msg_namelen = sizeof (struct sockaddr_in6);
  210. msg.msg_iov = &iov;
  211. msg.msg_iovlen = 1;
  212. msg.msg_control = (void *) adata;
  213. msg.msg_controllen = sizeof adata;
  214. iov.iov_base = buf;
  215. iov.iov_len = bufsize;
  216. /* If recvmsg fail return minus value. */
  217. ret = recvmsg (sock, &msg, 0);
  218. if (ret < 0)
  219. return ret;
  220. for (cmsgptr = ZCMSG_FIRSTHDR(&msg); cmsgptr != NULL;
  221. cmsgptr = CMSG_NXTHDR(&msg, cmsgptr))
  222. {
  223. /* I want interface index which this packet comes from. */
  224. if (cmsgptr->cmsg_level == IPPROTO_IPV6 &&
  225. cmsgptr->cmsg_type == IPV6_PKTINFO)
  226. {
  227. struct in6_pktinfo *ptr;
  228. ptr = (struct in6_pktinfo *) CMSG_DATA (cmsgptr);
  229. *ifindex = ptr->ipi6_ifindex;
  230. dst = ptr->ipi6_addr;
  231. if (*ifindex == 0)
  232. zlog_warn ("Interface index returned by IPV6_PKTINFO is zero");
  233. }
  234. /* Incoming packet's multicast hop limit. */
  235. if (cmsgptr->cmsg_level == IPPROTO_IPV6 &&
  236. cmsgptr->cmsg_type == IPV6_HOPLIMIT)
  237. *hoplimit = *((int *) CMSG_DATA (cmsgptr));
  238. }
  239. /* Hoplimit check shold be done when destination address is
  240. multicast address. */
  241. if (! IN6_IS_ADDR_MULTICAST (&dst))
  242. *hoplimit = -1;
  243. return ret;
  244. }
  245. /* Dump rip packet */
  246. void
  247. ripng_packet_dump (struct ripng_packet *packet, int size, const char *sndrcv)
  248. {
  249. caddr_t lim;
  250. struct rte *rte;
  251. const char *command_str;
  252. /* Set command string. */
  253. if (packet->command == RIPNG_REQUEST)
  254. command_str = "request";
  255. else if (packet->command == RIPNG_RESPONSE)
  256. command_str = "response";
  257. else
  258. command_str = "unknown";
  259. /* Dump packet header. */
  260. zlog_debug ("%s %s version %d packet size %d",
  261. sndrcv, command_str, packet->version, size);
  262. /* Dump each routing table entry. */
  263. rte = packet->rte;
  264. for (lim = (caddr_t) packet + size; (caddr_t) rte < lim; rte++)
  265. {
  266. if (rte->metric == RIPNG_METRIC_NEXTHOP)
  267. zlog_debug (" nexthop %s/%d", inet6_ntoa (rte->addr), rte->prefixlen);
  268. else
  269. zlog_debug (" %s/%d metric %d tag %d",
  270. inet6_ntoa (rte->addr), rte->prefixlen,
  271. rte->metric, ntohs (rte->tag));
  272. }
  273. }
  274. /* RIPng next hop address RTE (Route Table Entry). */
  275. static void
  276. ripng_nexthop_rte (struct rte *rte,
  277. struct sockaddr_in6 *from,
  278. struct ripng_nexthop *nexthop)
  279. {
  280. char buf[INET6_BUFSIZ];
  281. /* Logging before checking RTE. */
  282. if (IS_RIPNG_DEBUG_RECV)
  283. zlog_debug ("RIPng nexthop RTE address %s tag %d prefixlen %d",
  284. inet6_ntoa (rte->addr), ntohs (rte->tag), rte->prefixlen);
  285. /* RFC2080 2.1.1 Next Hop:
  286. The route tag and prefix length in the next hop RTE must be
  287. set to zero on sending and ignored on receiption. */
  288. if (ntohs (rte->tag) != 0)
  289. zlog_warn ("RIPng nexthop RTE with non zero tag value %d from %s",
  290. ntohs (rte->tag), inet6_ntoa (from->sin6_addr));
  291. if (rte->prefixlen != 0)
  292. zlog_warn ("RIPng nexthop RTE with non zero prefixlen value %d from %s",
  293. rte->prefixlen, inet6_ntoa (from->sin6_addr));
  294. /* Specifying a value of 0:0:0:0:0:0:0:0 in the prefix field of a
  295. next hop RTE indicates that the next hop address should be the
  296. originator of the RIPng advertisement. An address specified as a
  297. next hop must be a link-local address. */
  298. if (IN6_IS_ADDR_UNSPECIFIED (&rte->addr))
  299. {
  300. nexthop->flag = RIPNG_NEXTHOP_UNSPEC;
  301. memset (&nexthop->address, 0, sizeof (struct in6_addr));
  302. return;
  303. }
  304. if (IN6_IS_ADDR_LINKLOCAL (&rte->addr))
  305. {
  306. nexthop->flag = RIPNG_NEXTHOP_ADDRESS;
  307. IPV6_ADDR_COPY (&nexthop->address, &rte->addr);
  308. return;
  309. }
  310. /* The purpose of the next hop RTE is to eliminate packets being
  311. routed through extra hops in the system. It is particularly useful
  312. when RIPng is not being run on all of the routers on a network.
  313. Note that next hop RTE is "advisory". That is, if the provided
  314. information is ignored, a possibly sub-optimal, but absolutely
  315. valid, route may be taken. If the received next hop address is not
  316. a link-local address, it should be treated as 0:0:0:0:0:0:0:0. */
  317. zlog_warn ("RIPng nexthop RTE with non link-local address %s from %s",
  318. inet6_ntoa (rte->addr),
  319. inet_ntop (AF_INET6, &from->sin6_addr, buf, INET6_BUFSIZ));
  320. nexthop->flag = RIPNG_NEXTHOP_UNSPEC;
  321. memset (&nexthop->address, 0, sizeof (struct in6_addr));
  322. return;
  323. }
  324. /* If ifp has same link-local address then return 1. */
  325. static int
  326. ripng_lladdr_check (struct interface *ifp, struct in6_addr *addr)
  327. {
  328. struct listnode *node;
  329. struct connected *connected;
  330. struct prefix *p;
  331. for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, connected))
  332. {
  333. p = connected->address;
  334. if (p->family == AF_INET6 &&
  335. IN6_IS_ADDR_LINKLOCAL (&p->u.prefix6) &&
  336. IN6_ARE_ADDR_EQUAL (&p->u.prefix6, addr))
  337. return 1;
  338. }
  339. return 0;
  340. }
  341. /* RIPng route garbage collect timer. */
  342. static int
  343. ripng_garbage_collect (struct thread *t)
  344. {
  345. struct ripng_info *rinfo;
  346. struct route_node *rp;
  347. rinfo = THREAD_ARG (t);
  348. rinfo->t_garbage_collect = NULL;
  349. /* Off timeout timer. */
  350. RIPNG_TIMER_OFF (rinfo->t_timeout);
  351. /* Get route_node pointer. */
  352. rp = rinfo->rp;
  353. /* Unlock route_node. */
  354. rp->info = NULL;
  355. route_unlock_node (rp);
  356. /* Free RIPng routing information. */
  357. ripng_info_free (rinfo);
  358. return 0;
  359. }
  360. /* Timeout RIPng routes. */
  361. static int
  362. ripng_timeout (struct thread *t)
  363. {
  364. struct ripng_info *rinfo;
  365. struct route_node *rp;
  366. rinfo = THREAD_ARG (t);
  367. rinfo->t_timeout = NULL;
  368. /* Get route_node pointer. */
  369. rp = rinfo->rp;
  370. /* - The garbage-collection timer is set for 120 seconds. */
  371. RIPNG_TIMER_ON (rinfo->t_garbage_collect, ripng_garbage_collect,
  372. ripng->garbage_time);
  373. /* Delete this route from the kernel. */
  374. ripng_zebra_ipv6_delete ((struct prefix_ipv6 *)&rp->p, &rinfo->nexthop,
  375. rinfo->ifindex);
  376. /* - The metric for the route is set to 16 (infinity). This causes
  377. the route to be removed from service. */
  378. rinfo->metric = RIPNG_METRIC_INFINITY;
  379. rinfo->flags &= ~RIPNG_RTF_FIB;
  380. /* Aggregate count decrement. */
  381. ripng_aggregate_decrement (rp, rinfo);
  382. /* - The route change flag is to indicate that this entry has been
  383. changed. */
  384. rinfo->flags |= RIPNG_RTF_CHANGED;
  385. /* - The output process is signalled to trigger a response. */
  386. ripng_event (RIPNG_TRIGGERED_UPDATE, 0);
  387. return 0;
  388. }
  389. static void
  390. ripng_timeout_update (struct ripng_info *rinfo)
  391. {
  392. if (rinfo->metric != RIPNG_METRIC_INFINITY)
  393. {
  394. RIPNG_TIMER_OFF (rinfo->t_timeout);
  395. RIPNG_TIMER_ON (rinfo->t_timeout, ripng_timeout, ripng->timeout_time);
  396. }
  397. }
  398. static int
  399. ripng_incoming_filter (struct prefix_ipv6 *p, struct ripng_interface *ri)
  400. {
  401. struct distribute *dist;
  402. struct access_list *alist;
  403. struct prefix_list *plist;
  404. /* Input distribute-list filtering. */
  405. if (ri->list[RIPNG_FILTER_IN])
  406. {
  407. if (access_list_apply (ri->list[RIPNG_FILTER_IN],
  408. (struct prefix *) p) == FILTER_DENY)
  409. {
  410. if (IS_RIPNG_DEBUG_PACKET)
  411. zlog_debug ("%s/%d filtered by distribute in",
  412. inet6_ntoa (p->prefix), p->prefixlen);
  413. return -1;
  414. }
  415. }
  416. if (ri->prefix[RIPNG_FILTER_IN])
  417. {
  418. if (prefix_list_apply (ri->prefix[RIPNG_FILTER_IN],
  419. (struct prefix *) p) == PREFIX_DENY)
  420. {
  421. if (IS_RIPNG_DEBUG_PACKET)
  422. zlog_debug ("%s/%d filtered by prefix-list in",
  423. inet6_ntoa (p->prefix), p->prefixlen);
  424. return -1;
  425. }
  426. }
  427. /* All interface filter check. */
  428. dist = distribute_lookup (NULL);
  429. if (dist)
  430. {
  431. if (dist->list[DISTRIBUTE_IN])
  432. {
  433. alist = access_list_lookup (AFI_IP6, dist->list[DISTRIBUTE_IN]);
  434. if (alist)
  435. {
  436. if (access_list_apply (alist,
  437. (struct prefix *) p) == FILTER_DENY)
  438. {
  439. if (IS_RIPNG_DEBUG_PACKET)
  440. zlog_debug ("%s/%d filtered by distribute in",
  441. inet6_ntoa (p->prefix), p->prefixlen);
  442. return -1;
  443. }
  444. }
  445. }
  446. if (dist->prefix[DISTRIBUTE_IN])
  447. {
  448. plist = prefix_list_lookup (AFI_IP6, dist->prefix[DISTRIBUTE_IN]);
  449. if (plist)
  450. {
  451. if (prefix_list_apply (plist,
  452. (struct prefix *) p) == PREFIX_DENY)
  453. {
  454. if (IS_RIPNG_DEBUG_PACKET)
  455. zlog_debug ("%s/%d filtered by prefix-list in",
  456. inet6_ntoa (p->prefix), p->prefixlen);
  457. return -1;
  458. }
  459. }
  460. }
  461. }
  462. return 0;
  463. }
  464. static int
  465. ripng_outgoing_filter (struct prefix_ipv6 *p, struct ripng_interface *ri)
  466. {
  467. struct distribute *dist;
  468. struct access_list *alist;
  469. struct prefix_list *plist;
  470. if (ri->list[RIPNG_FILTER_OUT])
  471. {
  472. if (access_list_apply (ri->list[RIPNG_FILTER_OUT],
  473. (struct prefix *) p) == FILTER_DENY)
  474. {
  475. if (IS_RIPNG_DEBUG_PACKET)
  476. zlog_debug ("%s/%d is filtered by distribute out",
  477. inet6_ntoa (p->prefix), p->prefixlen);
  478. return -1;
  479. }
  480. }
  481. if (ri->prefix[RIPNG_FILTER_OUT])
  482. {
  483. if (prefix_list_apply (ri->prefix[RIPNG_FILTER_OUT],
  484. (struct prefix *) p) == PREFIX_DENY)
  485. {
  486. if (IS_RIPNG_DEBUG_PACKET)
  487. zlog_debug ("%s/%d is filtered by prefix-list out",
  488. inet6_ntoa (p->prefix), p->prefixlen);
  489. return -1;
  490. }
  491. }
  492. /* All interface filter check. */
  493. dist = distribute_lookup (NULL);
  494. if (dist)
  495. {
  496. if (dist->list[DISTRIBUTE_OUT])
  497. {
  498. alist = access_list_lookup (AFI_IP6, dist->list[DISTRIBUTE_OUT]);
  499. if (alist)
  500. {
  501. if (access_list_apply (alist,
  502. (struct prefix *) p) == FILTER_DENY)
  503. {
  504. if (IS_RIPNG_DEBUG_PACKET)
  505. zlog_debug ("%s/%d filtered by distribute out",
  506. inet6_ntoa (p->prefix), p->prefixlen);
  507. return -1;
  508. }
  509. }
  510. }
  511. if (dist->prefix[DISTRIBUTE_OUT])
  512. {
  513. plist = prefix_list_lookup (AFI_IP6, dist->prefix[DISTRIBUTE_OUT]);
  514. if (plist)
  515. {
  516. if (prefix_list_apply (plist,
  517. (struct prefix *) p) == PREFIX_DENY)
  518. {
  519. if (IS_RIPNG_DEBUG_PACKET)
  520. zlog_debug ("%s/%d filtered by prefix-list out",
  521. inet6_ntoa (p->prefix), p->prefixlen);
  522. return -1;
  523. }
  524. }
  525. }
  526. }
  527. return 0;
  528. }
  529. /* Process RIPng route according to RFC2080. */
  530. static void
  531. ripng_route_process (struct rte *rte, struct sockaddr_in6 *from,
  532. struct ripng_nexthop *ripng_nexthop,
  533. struct interface *ifp)
  534. {
  535. int ret;
  536. struct prefix_ipv6 p;
  537. struct route_node *rp;
  538. struct ripng_info *rinfo;
  539. struct ripng_interface *ri;
  540. struct in6_addr *nexthop;
  541. u_char oldmetric;
  542. int same = 0;
  543. /* Make prefix structure. */
  544. memset (&p, 0, sizeof (struct prefix_ipv6));
  545. p.family = AF_INET6;
  546. /* p.prefix = rte->addr; */
  547. IPV6_ADDR_COPY (&p.prefix, &rte->addr);
  548. p.prefixlen = rte->prefixlen;
  549. /* Make sure mask is applied. */
  550. /* XXX We have to check the prefix is valid or not before call
  551. apply_mask_ipv6. */
  552. apply_mask_ipv6 (&p);
  553. /* Apply input filters. */
  554. ri = ifp->info;
  555. ret = ripng_incoming_filter (&p, ri);
  556. if (ret < 0)
  557. return;
  558. /* Modify entry. */
  559. if (ri->routemap[RIPNG_FILTER_IN])
  560. {
  561. int ret;
  562. struct ripng_info newinfo;
  563. memset (&newinfo, 0, sizeof (struct ripng_info));
  564. newinfo.type = ZEBRA_ROUTE_RIPNG;
  565. newinfo.sub_type = RIPNG_ROUTE_RTE;
  566. if (ripng_nexthop->flag == RIPNG_NEXTHOP_ADDRESS)
  567. newinfo.nexthop = ripng_nexthop->address;
  568. else
  569. newinfo.nexthop = from->sin6_addr;
  570. newinfo.from = from->sin6_addr;
  571. newinfo.ifindex = ifp->ifindex;
  572. newinfo.metric = rte->metric;
  573. newinfo.metric_out = rte->metric; /* XXX */
  574. newinfo.tag = ntohs(rte->tag); /* XXX */
  575. ret = route_map_apply (ri->routemap[RIPNG_FILTER_IN],
  576. (struct prefix *)&p, RMAP_RIPNG, &newinfo);
  577. if (ret == RMAP_DENYMATCH)
  578. {
  579. if (IS_RIPNG_DEBUG_PACKET)
  580. zlog_debug ("RIPng %s/%d is filtered by route-map in",
  581. inet6_ntoa (p.prefix), p.prefixlen);
  582. return;
  583. }
  584. /* Get back the object */
  585. if (ripng_nexthop->flag == RIPNG_NEXTHOP_ADDRESS) {
  586. if (! IPV6_ADDR_SAME(&newinfo.nexthop, &ripng_nexthop->address) ) {
  587. /* the nexthop get changed by the routemap */
  588. if (IN6_IS_ADDR_LINKLOCAL(&newinfo.nexthop))
  589. ripng_nexthop->address = newinfo.nexthop;
  590. else
  591. ripng_nexthop->address = in6addr_any;
  592. }
  593. } else {
  594. if (! IPV6_ADDR_SAME(&newinfo.nexthop, &from->sin6_addr) ) {
  595. /* the nexthop get changed by the routemap */
  596. if (IN6_IS_ADDR_LINKLOCAL(&newinfo.nexthop)) {
  597. ripng_nexthop->flag = RIPNG_NEXTHOP_ADDRESS;
  598. ripng_nexthop->address = newinfo.nexthop;
  599. }
  600. }
  601. }
  602. rte->tag = htons(newinfo.tag_out); /* XXX */
  603. rte->metric = newinfo.metric_out; /* XXX: the routemap uses the metric_out field */
  604. }
  605. /* Once the entry has been validated, update the metric by
  606. * adding the cost of the network on wich the message
  607. * arrived. If the result is greater than infinity, use infinity
  608. * (RFC2453 Sec. 3.9.2)
  609. **/
  610. /* Zebra ripngd can handle offset-list in. */
  611. ret = ripng_offset_list_apply_in (&p, ifp, &rte->metric);
  612. /* If offset-list does not modify the metric use interface's
  613. * one. */
  614. if (! ret)
  615. rte->metric += ifp->metric;
  616. if (rte->metric > RIPNG_METRIC_INFINITY)
  617. rte->metric = RIPNG_METRIC_INFINITY;
  618. /* Set nexthop pointer. */
  619. if (ripng_nexthop->flag == RIPNG_NEXTHOP_ADDRESS)
  620. nexthop = &ripng_nexthop->address;
  621. else
  622. nexthop = &from->sin6_addr;
  623. /* Lookup RIPng routing table. */
  624. rp = route_node_get (ripng->table, (struct prefix *) &p);
  625. /* Sanity check */
  626. rinfo = rp->info;
  627. if (rinfo)
  628. {
  629. /* Redistributed route check. */
  630. if (rinfo->type != ZEBRA_ROUTE_RIPNG
  631. && rinfo->metric != RIPNG_METRIC_INFINITY)
  632. return;
  633. /* Local static route. */
  634. if (rinfo->type == ZEBRA_ROUTE_RIPNG
  635. && ((rinfo->sub_type == RIPNG_ROUTE_STATIC) ||
  636. (rinfo->sub_type == RIPNG_ROUTE_DEFAULT))
  637. && rinfo->metric != RIPNG_METRIC_INFINITY)
  638. return;
  639. }
  640. if (rp->info == NULL)
  641. {
  642. /* Now, check to see whether there is already an explicit route
  643. for the destination prefix. If there is no such route, add
  644. this route to the routing table, unless the metric is
  645. infinity (there is no point in adding a route which
  646. unusable). */
  647. if (rte->metric != RIPNG_METRIC_INFINITY)
  648. {
  649. rinfo = ripng_info_new ();
  650. /* - Setting the destination prefix and length to those in
  651. the RTE. */
  652. rp->info = rinfo;
  653. rinfo->rp = rp;
  654. /* - Setting the metric to the newly calculated metric (as
  655. described above). */
  656. rinfo->metric = rte->metric;
  657. rinfo->tag = ntohs (rte->tag);
  658. /* - Set the next hop address to be the address of the router
  659. from which the datagram came or the next hop address
  660. specified by a next hop RTE. */
  661. IPV6_ADDR_COPY (&rinfo->nexthop, nexthop);
  662. IPV6_ADDR_COPY (&rinfo->from, &from->sin6_addr);
  663. rinfo->ifindex = ifp->ifindex;
  664. /* - Initialize the timeout for the route. If the
  665. garbage-collection timer is running for this route, stop it. */
  666. ripng_timeout_update (rinfo);
  667. /* - Set the route change flag. */
  668. rinfo->flags |= RIPNG_RTF_CHANGED;
  669. /* - Signal the output process to trigger an update (see section
  670. 2.5). */
  671. ripng_event (RIPNG_TRIGGERED_UPDATE, 0);
  672. /* Finally, route goes into the kernel. */
  673. rinfo->type = ZEBRA_ROUTE_RIPNG;
  674. rinfo->sub_type = RIPNG_ROUTE_RTE;
  675. ripng_zebra_ipv6_add (&p, &rinfo->nexthop, rinfo->ifindex,
  676. rinfo->metric);
  677. rinfo->flags |= RIPNG_RTF_FIB;
  678. /* Aggregate check. */
  679. ripng_aggregate_increment (rp, rinfo);
  680. }
  681. }
  682. else
  683. {
  684. rinfo = rp->info;
  685. /* If there is an existing route, compare the next hop address
  686. to the address of the router from which the datagram came.
  687. If this datagram is from the same router as the existing
  688. route, reinitialize the timeout. */
  689. same = (IN6_ARE_ADDR_EQUAL (&rinfo->from, &from->sin6_addr)
  690. && (rinfo->ifindex == ifp->ifindex));
  691. if (same)
  692. ripng_timeout_update (rinfo);
  693. /* Next, compare the metrics. If the datagram is from the same
  694. router as the existing route, and the new metric is different
  695. than the old one; or, if the new metric is lower than the old
  696. one; do the following actions: */
  697. if ((same && rinfo->metric != rte->metric) ||
  698. rte->metric < rinfo->metric)
  699. {
  700. /* - Adopt the route from the datagram. That is, put the
  701. new metric in, and adjust the next hop address (if
  702. necessary). */
  703. oldmetric = rinfo->metric;
  704. rinfo->metric = rte->metric;
  705. rinfo->tag = ntohs (rte->tag);
  706. IPV6_ADDR_COPY (&rinfo->from, &from->sin6_addr);
  707. rinfo->ifindex = ifp->ifindex;
  708. /* Should a new route to this network be established
  709. while the garbage-collection timer is running, the
  710. new route will replace the one that is about to be
  711. deleted. In this case the garbage-collection timer
  712. must be cleared. */
  713. if (oldmetric == RIPNG_METRIC_INFINITY &&
  714. rinfo->metric < RIPNG_METRIC_INFINITY)
  715. {
  716. rinfo->type = ZEBRA_ROUTE_RIPNG;
  717. rinfo->sub_type = RIPNG_ROUTE_RTE;
  718. RIPNG_TIMER_OFF (rinfo->t_garbage_collect);
  719. if (! IPV6_ADDR_SAME (&rinfo->nexthop, nexthop))
  720. IPV6_ADDR_COPY (&rinfo->nexthop, nexthop);
  721. ripng_zebra_ipv6_add (&p, nexthop, ifp->ifindex, rinfo->metric);
  722. rinfo->flags |= RIPNG_RTF_FIB;
  723. /* The aggregation counter needs to be updated because
  724. the prefixes, which are into the gc, have been
  725. removed from the aggregator (see ripng_timout). */
  726. ripng_aggregate_increment (rp, rinfo);
  727. }
  728. /* Update nexthop and/or metric value. */
  729. if (oldmetric != RIPNG_METRIC_INFINITY)
  730. {
  731. ripng_zebra_ipv6_delete (&p, &rinfo->nexthop, rinfo->ifindex);
  732. ripng_zebra_ipv6_add (&p, nexthop, ifp->ifindex, rinfo->metric);
  733. rinfo->flags |= RIPNG_RTF_FIB;
  734. if (! IPV6_ADDR_SAME (&rinfo->nexthop, nexthop))
  735. IPV6_ADDR_COPY (&rinfo->nexthop, nexthop);
  736. }
  737. /* - Set the route change flag and signal the output process
  738. to trigger an update. */
  739. rinfo->flags |= RIPNG_RTF_CHANGED;
  740. ripng_event (RIPNG_TRIGGERED_UPDATE, 0);
  741. /* - If the new metric is infinity, start the deletion
  742. process (described above); */
  743. if (rinfo->metric == RIPNG_METRIC_INFINITY)
  744. {
  745. /* If the new metric is infinity, the deletion process
  746. begins for the route, which is no longer used for
  747. routing packets. Note that the deletion process is
  748. started only when the metric is first set to
  749. infinity. If the metric was already infinity, then a
  750. new deletion process is not started. */
  751. if (oldmetric != RIPNG_METRIC_INFINITY)
  752. {
  753. /* - The garbage-collection timer is set for 120 seconds. */
  754. RIPNG_TIMER_ON (rinfo->t_garbage_collect,
  755. ripng_garbage_collect, ripng->garbage_time);
  756. RIPNG_TIMER_OFF (rinfo->t_timeout);
  757. /* - The metric for the route is set to 16
  758. (infinity). This causes the route to be removed
  759. from service.*/
  760. ripng_zebra_ipv6_delete (&p, &rinfo->nexthop, rinfo->ifindex);
  761. rinfo->flags &= ~RIPNG_RTF_FIB;
  762. /* Aggregate count decrement. */
  763. ripng_aggregate_decrement (rp, rinfo);
  764. /* - The route change flag is to indicate that this
  765. entry has been changed. */
  766. /* - The output process is signalled to trigger a
  767. response. */
  768. ; /* Above processes are already done previously. */
  769. }
  770. }
  771. else
  772. {
  773. /* otherwise, re-initialize the timeout. */
  774. ripng_timeout_update (rinfo);
  775. }
  776. }
  777. /* Unlock tempolary lock of the route. */
  778. route_unlock_node (rp);
  779. }
  780. }
  781. /* Add redistributed route to RIPng table. */
  782. void
  783. ripng_redistribute_add (int type, int sub_type, struct prefix_ipv6 *p,
  784. unsigned int ifindex, struct in6_addr *nexthop)
  785. {
  786. struct route_node *rp;
  787. struct ripng_info *rinfo;
  788. /* Redistribute route */
  789. if (IN6_IS_ADDR_LINKLOCAL (&p->prefix))
  790. return;
  791. if (IN6_IS_ADDR_LOOPBACK (&p->prefix))
  792. return;
  793. #if defined (MUSICA) || defined (LINUX)
  794. /* XXX As long as the RIPng redistribution is applied to all the connected
  795. * routes, one needs to filter the ::/96 prefixes.
  796. * However it could be a wanted case, it will be removed soon.
  797. */
  798. if ((IN6_IS_ADDR_V4COMPAT(&p->prefix)) ||
  799. (IN6_IS_ADDR_UNSPECIFIED (&p->prefix) && (p->prefixlen == 96)))
  800. return;
  801. #endif /* MUSICA or LINUX */
  802. rp = route_node_get (ripng->table, (struct prefix *) p);
  803. rinfo = rp->info;
  804. if (rinfo)
  805. {
  806. if (rinfo->type == ZEBRA_ROUTE_CONNECT
  807. && rinfo->sub_type == RIPNG_ROUTE_INTERFACE
  808. && rinfo->metric != RIPNG_METRIC_INFINITY) {
  809. route_unlock_node (rp);
  810. return;
  811. }
  812. /* Manually configured RIPng route check.
  813. * They have the precedence on all the other entries.
  814. **/
  815. if (rinfo->type == ZEBRA_ROUTE_RIPNG
  816. && ((rinfo->sub_type == RIPNG_ROUTE_STATIC) ||
  817. (rinfo->sub_type == RIPNG_ROUTE_DEFAULT)) ) {
  818. if (type != ZEBRA_ROUTE_RIPNG || ((sub_type != RIPNG_ROUTE_STATIC) &&
  819. (sub_type != RIPNG_ROUTE_DEFAULT))) {
  820. route_unlock_node (rp);
  821. return;
  822. }
  823. }
  824. RIPNG_TIMER_OFF (rinfo->t_timeout);
  825. RIPNG_TIMER_OFF (rinfo->t_garbage_collect);
  826. /* Tells the other daemons about the deletion of
  827. * this RIPng route
  828. **/
  829. if (ripng_route_rte (rinfo))
  830. ripng_zebra_ipv6_delete ((struct prefix_ipv6 *)&rp->p, &rinfo->nexthop,
  831. rinfo->metric);
  832. rp->info = NULL;
  833. ripng_info_free (rinfo);
  834. route_unlock_node (rp);
  835. }
  836. rinfo = ripng_info_new ();
  837. rinfo->type = type;
  838. rinfo->sub_type = sub_type;
  839. rinfo->ifindex = ifindex;
  840. rinfo->metric = 1;
  841. rinfo->rp = rp;
  842. if (nexthop && IN6_IS_ADDR_LINKLOCAL(nexthop))
  843. rinfo->nexthop = *nexthop;
  844. rinfo->flags |= RIPNG_RTF_FIB;
  845. rp->info = rinfo;
  846. /* Aggregate check. */
  847. ripng_aggregate_increment (rp, rinfo);
  848. rinfo->flags |= RIPNG_RTF_CHANGED;
  849. if (IS_RIPNG_DEBUG_EVENT) {
  850. if (!nexthop)
  851. zlog_debug ("Redistribute new prefix %s/%d on the interface %s",
  852. inet6_ntoa(p->prefix), p->prefixlen,
  853. ifindex2ifname(ifindex));
  854. else
  855. zlog_debug ("Redistribute new prefix %s/%d with nexthop %s on the interface %s",
  856. inet6_ntoa(p->prefix), p->prefixlen, inet6_ntoa(*nexthop),
  857. ifindex2ifname(ifindex));
  858. }
  859. ripng_event (RIPNG_TRIGGERED_UPDATE, 0);
  860. }
  861. /* Delete redistributed route to RIPng table. */
  862. void
  863. ripng_redistribute_delete (int type, int sub_type, struct prefix_ipv6 *p,
  864. unsigned int ifindex)
  865. {
  866. struct route_node *rp;
  867. struct ripng_info *rinfo;
  868. if (IN6_IS_ADDR_LINKLOCAL (&p->prefix))
  869. return;
  870. if (IN6_IS_ADDR_LOOPBACK (&p->prefix))
  871. return;
  872. #if defined (MUSICA) || defined (LINUX)
  873. /* XXX As long as the RIPng redistribution is applied to all the connected
  874. * routes, one needs to filter the ::/96 prefixes.
  875. * However it could be a wanted case, it will be removed soon.
  876. */
  877. if ((IN6_IS_ADDR_V4COMPAT(&p->prefix)) ||
  878. (IN6_IS_ADDR_UNSPECIFIED (&p->prefix) && (p->prefixlen == 96)))
  879. return;
  880. #endif /* MUSICA or LINUX */
  881. rp = route_node_lookup (ripng->table, (struct prefix *) p);
  882. if (rp)
  883. {
  884. rinfo = rp->info;
  885. if (rinfo != NULL
  886. && rinfo->type == type
  887. && rinfo->sub_type == sub_type
  888. && rinfo->ifindex == ifindex)
  889. {
  890. /* Perform poisoned reverse. */
  891. rinfo->metric = RIPNG_METRIC_INFINITY;
  892. RIPNG_TIMER_ON (rinfo->t_garbage_collect,
  893. ripng_garbage_collect, ripng->garbage_time);
  894. RIPNG_TIMER_OFF (rinfo->t_timeout);
  895. /* Aggregate count decrement. */
  896. ripng_aggregate_decrement (rp, rinfo);
  897. rinfo->flags |= RIPNG_RTF_CHANGED;
  898. if (IS_RIPNG_DEBUG_EVENT)
  899. zlog_debug ("Poisone %s/%d on the interface %s with an infinity metric [delete]",
  900. inet6_ntoa(p->prefix), p->prefixlen,
  901. ifindex2ifname(ifindex));
  902. ripng_event (RIPNG_TRIGGERED_UPDATE, 0);
  903. }
  904. }
  905. }
  906. /* Withdraw redistributed route. */
  907. void
  908. ripng_redistribute_withdraw (int type)
  909. {
  910. struct route_node *rp;
  911. struct ripng_info *rinfo;
  912. if (!ripng)
  913. return;
  914. for (rp = route_top (ripng->table); rp; rp = route_next (rp))
  915. if ((rinfo = rp->info) != NULL)
  916. {
  917. if ((rinfo->type == type)
  918. && (rinfo->sub_type != RIPNG_ROUTE_INTERFACE))
  919. {
  920. /* Perform poisoned reverse. */
  921. rinfo->metric = RIPNG_METRIC_INFINITY;
  922. RIPNG_TIMER_ON (rinfo->t_garbage_collect,
  923. ripng_garbage_collect, ripng->garbage_time);
  924. RIPNG_TIMER_OFF (rinfo->t_timeout);
  925. /* Aggregate count decrement. */
  926. ripng_aggregate_decrement (rp, rinfo);
  927. rinfo->flags |= RIPNG_RTF_CHANGED;
  928. if (IS_RIPNG_DEBUG_EVENT) {
  929. struct prefix_ipv6 *p = (struct prefix_ipv6 *) &rp->p;
  930. zlog_debug ("Poisone %s/%d on the interface %s [withdraw]",
  931. inet6_ntoa(p->prefix), p->prefixlen,
  932. ifindex2ifname(rinfo->ifindex));
  933. }
  934. ripng_event (RIPNG_TRIGGERED_UPDATE, 0);
  935. }
  936. }
  937. }
  938. /* RIP routing information. */
  939. static void
  940. ripng_response_process (struct ripng_packet *packet, int size,
  941. struct sockaddr_in6 *from, struct interface *ifp,
  942. int hoplimit)
  943. {
  944. caddr_t lim;
  945. struct rte *rte;
  946. struct ripng_nexthop nexthop;
  947. /* RFC2080 2.4.2 Response Messages:
  948. The Response must be ignored if it is not from the RIPng port. */
  949. if (ntohs (from->sin6_port) != RIPNG_PORT_DEFAULT)
  950. {
  951. zlog_warn ("RIPng packet comes from non RIPng port %d from %s",
  952. ntohs (from->sin6_port), inet6_ntoa (from->sin6_addr));
  953. ripng_peer_bad_packet (from);
  954. return;
  955. }
  956. /* The datagram's IPv6 source address should be checked to see
  957. whether the datagram is from a valid neighbor; the source of the
  958. datagram must be a link-local address. */
  959. if (! IN6_IS_ADDR_LINKLOCAL(&from->sin6_addr))
  960. {
  961. zlog_warn ("RIPng packet comes from non link local address %s",
  962. inet6_ntoa (from->sin6_addr));
  963. ripng_peer_bad_packet (from);
  964. return;
  965. }
  966. /* It is also worth checking to see whether the response is from one
  967. of the router's own addresses. Interfaces on broadcast networks
  968. may receive copies of their own multicasts immediately. If a
  969. router processes its own output as new input, confusion is likely,
  970. and such datagrams must be ignored. */
  971. if (ripng_lladdr_check (ifp, &from->sin6_addr))
  972. {
  973. zlog_warn ("RIPng packet comes from my own link local address %s",
  974. inet6_ntoa (from->sin6_addr));
  975. ripng_peer_bad_packet (from);
  976. return;
  977. }
  978. /* As an additional check, periodic advertisements must have their
  979. hop counts set to 255, and inbound, multicast packets sent from the
  980. RIPng port (i.e. periodic advertisement or triggered update
  981. packets) must be examined to ensure that the hop count is 255. */
  982. if (hoplimit >= 0 && hoplimit != 255)
  983. {
  984. zlog_warn ("RIPng packet comes with non 255 hop count %d from %s",
  985. hoplimit, inet6_ntoa (from->sin6_addr));
  986. ripng_peer_bad_packet (from);
  987. return;
  988. }
  989. /* Update RIPng peer. */
  990. ripng_peer_update (from, packet->version);
  991. /* Reset nexthop. */
  992. memset (&nexthop, 0, sizeof (struct ripng_nexthop));
  993. nexthop.flag = RIPNG_NEXTHOP_UNSPEC;
  994. /* Set RTE pointer. */
  995. rte = packet->rte;
  996. for (lim = ((caddr_t) packet) + size; (caddr_t) rte < lim; rte++)
  997. {
  998. /* First of all, we have to check this RTE is next hop RTE or
  999. not. Next hop RTE is completely different with normal RTE so
  1000. we need special treatment. */
  1001. if (rte->metric == RIPNG_METRIC_NEXTHOP)
  1002. {
  1003. ripng_nexthop_rte (rte, from, &nexthop);
  1004. continue;
  1005. }
  1006. /* RTE information validation. */
  1007. /* - is the destination prefix valid (e.g., not a multicast
  1008. prefix and not a link-local address) A link-local address
  1009. should never be present in an RTE. */
  1010. if (IN6_IS_ADDR_MULTICAST (&rte->addr))
  1011. {
  1012. zlog_warn ("Destination prefix is a multicast address %s/%d [%d]",
  1013. inet6_ntoa (rte->addr), rte->prefixlen, rte->metric);
  1014. ripng_peer_bad_route (from);
  1015. continue;
  1016. }
  1017. if (IN6_IS_ADDR_LINKLOCAL (&rte->addr))
  1018. {
  1019. zlog_warn ("Destination prefix is a link-local address %s/%d [%d]",
  1020. inet6_ntoa (rte->addr), rte->prefixlen, rte->metric);
  1021. ripng_peer_bad_route (from);
  1022. continue;
  1023. }
  1024. if (IN6_IS_ADDR_LOOPBACK (&rte->addr))
  1025. {
  1026. zlog_warn ("Destination prefix is a loopback address %s/%d [%d]",
  1027. inet6_ntoa (rte->addr), rte->prefixlen, rte->metric);
  1028. ripng_peer_bad_route (from);
  1029. continue;
  1030. }
  1031. /* - is the prefix length valid (i.e., between 0 and 128,
  1032. inclusive) */
  1033. if (rte->prefixlen > 128)
  1034. {
  1035. zlog_warn ("Invalid prefix length %s/%d from %s%%%s",
  1036. inet6_ntoa (rte->addr), rte->prefixlen,
  1037. inet6_ntoa (from->sin6_addr), ifp->name);
  1038. ripng_peer_bad_route (from);
  1039. continue;
  1040. }
  1041. /* - is the metric valid (i.e., between 1 and 16, inclusive) */
  1042. if (! (rte->metric >= 1 && rte->metric <= 16))
  1043. {
  1044. zlog_warn ("Invalid metric %d from %s%%%s", rte->metric,
  1045. inet6_ntoa (from->sin6_addr), ifp->name);
  1046. ripng_peer_bad_route (from);
  1047. continue;
  1048. }
  1049. /* Vincent: XXX Should we compute the direclty reachable nexthop
  1050. * for our RIPng network ?
  1051. **/
  1052. /* Routing table updates. */
  1053. ripng_route_process (rte, from, &nexthop, ifp);
  1054. }
  1055. }
  1056. /* Response to request message. */
  1057. static void
  1058. ripng_request_process (struct ripng_packet *packet,int size,
  1059. struct sockaddr_in6 *from, struct interface *ifp)
  1060. {
  1061. caddr_t lim;
  1062. struct rte *rte;
  1063. struct prefix_ipv6 p;
  1064. struct route_node *rp;
  1065. struct ripng_info *rinfo;
  1066. struct ripng_interface *ri;
  1067. /* Does not reponse to the requests on the loopback interfaces */
  1068. if (if_is_loopback (ifp))
  1069. return;
  1070. /* Check RIPng process is enabled on this interface. */
  1071. ri = ifp->info;
  1072. if (! ri->running)
  1073. return;
  1074. /* When passive interface is specified, suppress responses */
  1075. if (ri->passive)
  1076. return;
  1077. /* RIPng peer update. */
  1078. ripng_peer_update (from, packet->version);
  1079. lim = ((caddr_t) packet) + size;
  1080. rte = packet->rte;
  1081. /* The Request is processed entry by entry. If there are no
  1082. entries, no response is given. */
  1083. if (lim == (caddr_t) rte)
  1084. return;
  1085. /* There is one special case. If there is exactly one entry in the
  1086. request, and it has a destination prefix of zero, a prefix length
  1087. of zero, and a metric of infinity (i.e., 16), then this is a
  1088. request to send the entire routing table. In that case, a call
  1089. is made to the output process to send the routing table to the
  1090. requesting address/port. */
  1091. if (lim == ((caddr_t) (rte + 1)) &&
  1092. IN6_IS_ADDR_UNSPECIFIED (&rte->addr) &&
  1093. rte->prefixlen == 0 &&
  1094. rte->metric == RIPNG_METRIC_INFINITY)
  1095. {
  1096. /* All route with split horizon */
  1097. ripng_output_process (ifp, from, ripng_all_route);
  1098. }
  1099. else
  1100. {
  1101. /* Except for this special case, processing is quite simple.
  1102. Examine the list of RTEs in the Request one by one. For each
  1103. entry, look up the destination in the router's routing
  1104. database and, if there is a route, put that route's metric in
  1105. the metric field of the RTE. If there is no explicit route
  1106. to the specified destination, put infinity in the metric
  1107. field. Once all the entries have been filled in, change the
  1108. command from Request to Response and send the datagram back
  1109. to the requestor. */
  1110. memset (&p, 0, sizeof (struct prefix_ipv6));
  1111. p.family = AF_INET6;
  1112. for (; ((caddr_t) rte) < lim; rte++)
  1113. {
  1114. p.prefix = rte->addr;
  1115. p.prefixlen = rte->prefixlen;
  1116. apply_mask_ipv6 (&p);
  1117. rp = route_node_lookup (ripng->table, (struct prefix *) &p);
  1118. if (rp)
  1119. {
  1120. rinfo = rp->info;
  1121. rte->metric = rinfo->metric;
  1122. route_unlock_node (rp);
  1123. }
  1124. else
  1125. rte->metric = RIPNG_METRIC_INFINITY;
  1126. }
  1127. packet->command = RIPNG_RESPONSE;
  1128. ripng_send_packet ((caddr_t) packet, size, from, ifp);
  1129. }
  1130. }
  1131. /* First entry point of reading RIPng packet. */
  1132. static int
  1133. ripng_read (struct thread *thread)
  1134. {
  1135. int len;
  1136. int sock;
  1137. struct sockaddr_in6 from;
  1138. struct ripng_packet *packet;
  1139. unsigned int ifindex;
  1140. struct interface *ifp;
  1141. int hoplimit = -1;
  1142. /* Check ripng is active and alive. */
  1143. assert (ripng != NULL);
  1144. assert (ripng->sock >= 0);
  1145. /* Fetch thread data and set read pointer to empty for event
  1146. managing. `sock' sould be same as ripng->sock. */
  1147. sock = THREAD_FD (thread);
  1148. ripng->t_read = NULL;
  1149. /* Add myself to the next event. */
  1150. ripng_event (RIPNG_READ, sock);
  1151. /* Read RIPng packet. */
  1152. len = ripng_recv_packet (sock, STREAM_DATA (ripng->ibuf),
  1153. STREAM_SIZE (ripng->ibuf), &from, &ifindex,
  1154. &hoplimit);
  1155. if (len < 0)
  1156. {
  1157. zlog_warn ("RIPng recvfrom failed: %s.", safe_strerror (errno));
  1158. return len;
  1159. }
  1160. /* Check RTE boundary. RTE size (Packet length - RIPng header size
  1161. (4)) must be multiple size of one RTE size (20). */
  1162. if (((len - 4) % 20) != 0)
  1163. {
  1164. zlog_warn ("RIPng invalid packet size %d from %s", len,
  1165. inet6_ntoa (from.sin6_addr));
  1166. ripng_peer_bad_packet (&from);
  1167. return 0;
  1168. }
  1169. packet = (struct ripng_packet *) STREAM_DATA (ripng->ibuf);
  1170. ifp = if_lookup_by_index (ifindex);
  1171. /* RIPng packet received. */
  1172. if (IS_RIPNG_DEBUG_EVENT)
  1173. zlog_debug ("RIPng packet received from %s port %d on %s",
  1174. inet6_ntoa (from.sin6_addr), ntohs (from.sin6_port),
  1175. ifp ? ifp->name : "unknown");
  1176. /* Logging before packet checking. */
  1177. if (IS_RIPNG_DEBUG_RECV)
  1178. ripng_packet_dump (packet, len, "RECV");
  1179. /* Packet comes from unknown interface. */
  1180. if (ifp == NULL)
  1181. {
  1182. zlog_warn ("RIPng packet comes from unknown interface %d", ifindex);
  1183. return 0;
  1184. }
  1185. /* Packet version mismatch checking. */
  1186. if (packet->version != ripng->version)
  1187. {
  1188. zlog_warn ("RIPng packet version %d doesn't fit to my version %d",
  1189. packet->version, ripng->version);
  1190. ripng_peer_bad_packet (&from);
  1191. return 0;
  1192. }
  1193. /* Process RIPng packet. */
  1194. switch (packet->command)
  1195. {
  1196. case RIPNG_REQUEST:
  1197. ripng_request_process (packet, len, &from, ifp);
  1198. break;
  1199. case RIPNG_RESPONSE:
  1200. ripng_response_process (packet, len, &from, ifp, hoplimit);
  1201. break;
  1202. default:
  1203. zlog_warn ("Invalid RIPng command %d", packet->command);
  1204. ripng_peer_bad_packet (&from);
  1205. break;
  1206. }
  1207. return 0;
  1208. }
  1209. /* Walk down the RIPng routing table then clear changed flag. */
  1210. static void
  1211. ripng_clear_changed_flag (void)
  1212. {
  1213. struct route_node *rp;
  1214. struct ripng_info *rinfo;
  1215. for (rp = route_top (ripng->table); rp; rp = route_next (rp))
  1216. if ((rinfo = rp->info) != NULL)
  1217. if (rinfo->flags & RIPNG_RTF_CHANGED)
  1218. rinfo->flags &= ~RIPNG_RTF_CHANGED;
  1219. }
  1220. /* Regular update of RIPng route. Send all routing formation to RIPng
  1221. enabled interface. */
  1222. static int
  1223. ripng_update (struct thread *t)
  1224. {
  1225. struct listnode *node;
  1226. struct interface *ifp;
  1227. struct ripng_interface *ri;
  1228. /* Clear update timer thread. */
  1229. ripng->t_update = NULL;
  1230. /* Logging update event. */
  1231. if (IS_RIPNG_DEBUG_EVENT)
  1232. zlog_debug ("RIPng update timer expired!");
  1233. /* Supply routes to each interface. */
  1234. for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
  1235. {
  1236. ri = ifp->info;
  1237. if (if_is_loopback (ifp) || ! if_is_up (ifp))
  1238. continue;
  1239. if (! ri->running)
  1240. continue;
  1241. /* When passive interface is specified, suppress announce to the
  1242. interface. */
  1243. if (ri->passive)
  1244. continue;
  1245. #if RIPNG_ADVANCED
  1246. if (ri->ri_send == RIPNG_SEND_OFF)
  1247. {
  1248. if (IS_RIPNG_DEBUG_EVENT)
  1249. zlog (NULL, LOG_DEBUG,
  1250. "[Event] RIPng send to if %d is suppressed by config",
  1251. ifp->ifindex);
  1252. continue;
  1253. }
  1254. #endif /* RIPNG_ADVANCED */
  1255. ripng_output_process (ifp, NULL, ripng_all_route);
  1256. }
  1257. /* Triggered updates may be suppressed if a regular update is due by
  1258. the time the triggered update would be sent. */
  1259. if (ripng->t_triggered_interval)
  1260. {
  1261. thread_cancel (ripng->t_triggered_interval);
  1262. ripng->t_triggered_interval = NULL;
  1263. }
  1264. ripng->trigger = 0;
  1265. /* Reset flush event. */
  1266. ripng_event (RIPNG_UPDATE_EVENT, 0);
  1267. return 0;
  1268. }
  1269. /* Triggered update interval timer. */
  1270. static int
  1271. ripng_triggered_interval (struct thread *t)
  1272. {
  1273. ripng->t_triggered_interval = NULL;
  1274. if (ripng->trigger)
  1275. {
  1276. ripng->trigger = 0;
  1277. ripng_triggered_update (t);
  1278. }
  1279. return 0;
  1280. }
  1281. /* Execute triggered update. */
  1282. int
  1283. ripng_triggered_update (struct thread *t)
  1284. {
  1285. struct listnode *node;
  1286. struct interface *ifp;
  1287. struct ripng_interface *ri;
  1288. int interval;
  1289. ripng->t_triggered_update = NULL;
  1290. /* Cancel interval timer. */
  1291. if (ripng->t_triggered_interval)
  1292. {
  1293. thread_cancel (ripng->t_triggered_interval);
  1294. ripng->t_triggered_interval = NULL;
  1295. }
  1296. ripng->trigger = 0;
  1297. /* Logging triggered update. */
  1298. if (IS_RIPNG_DEBUG_EVENT)
  1299. zlog_debug ("RIPng triggered update!");
  1300. /* Split Horizon processing is done when generating triggered
  1301. updates as well as normal updates (see section 2.6). */
  1302. for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
  1303. {
  1304. ri = ifp->info;
  1305. if (if_is_loopback (ifp) || ! if_is_up (ifp))
  1306. continue;
  1307. if (! ri->running)
  1308. continue;
  1309. /* When passive interface is specified, suppress announce to the
  1310. interface. */
  1311. if (ri->passive)
  1312. continue;
  1313. ripng_output_process (ifp, NULL, ripng_changed_route);
  1314. }
  1315. /* Once all of the triggered updates have been generated, the route
  1316. change flags should be cleared. */
  1317. ripng_clear_changed_flag ();
  1318. /* After a triggered update is sent, a timer should be set for a
  1319. random interval between 1 and 5 seconds. If other changes that
  1320. would trigger updates occur before the timer expires, a single
  1321. update is triggered when the timer expires. */
  1322. interval = (random () % 5) + 1;
  1323. ripng->t_triggered_interval =
  1324. thread_add_timer (master, ripng_triggered_interval, NULL, interval);
  1325. return 0;
  1326. }
  1327. /* Write routing table entry to the stream and return next index of
  1328. the routing table entry in the stream. */
  1329. int
  1330. ripng_write_rte (int num, struct stream *s, struct prefix_ipv6 *p,
  1331. struct in6_addr *nexthop, u_int16_t tag, u_char metric)
  1332. {
  1333. /* RIPng packet header. */
  1334. if (num == 0)
  1335. {
  1336. stream_putc (s, RIPNG_RESPONSE);
  1337. stream_putc (s, RIPNG_V1);
  1338. stream_putw (s, 0);
  1339. }
  1340. /* Write routing table entry. */
  1341. if (!nexthop)
  1342. stream_write (s, (u_char *) &p->prefix, sizeof (struct in6_addr));
  1343. else
  1344. stream_write (s, (u_char *) nexthop, sizeof (struct in6_addr));
  1345. stream_putw (s, tag);
  1346. if (p)
  1347. stream_putc (s, p->prefixlen);
  1348. else
  1349. stream_putc (s, 0);
  1350. stream_putc (s, metric);
  1351. return ++num;
  1352. }
  1353. /* Send RESPONSE message to specified destination. */
  1354. void
  1355. ripng_output_process (struct interface *ifp, struct sockaddr_in6 *to,
  1356. int route_type)
  1357. {
  1358. int ret;
  1359. struct route_node *rp;
  1360. struct ripng_info *rinfo;
  1361. struct ripng_interface *ri;
  1362. struct ripng_aggregate *aggregate;
  1363. struct prefix_ipv6 *p;
  1364. struct list * ripng_rte_list;
  1365. if (IS_RIPNG_DEBUG_EVENT) {
  1366. if (to)
  1367. zlog_debug ("RIPng update routes to neighbor %s",
  1368. inet6_ntoa(to->sin6_addr));
  1369. else
  1370. zlog_debug ("RIPng update routes on interface %s", ifp->name);
  1371. }
  1372. /* Get RIPng interface. */
  1373. ri = ifp->info;
  1374. ripng_rte_list = ripng_rte_new();
  1375. for (rp = route_top (ripng->table); rp; rp = route_next (rp))
  1376. {
  1377. if ((rinfo = rp->info) != NULL && rinfo->suppress == 0)
  1378. {
  1379. /* If no route-map are applied, the RTE will be these following
  1380. * informations.
  1381. */
  1382. p = (struct prefix_ipv6 *) &rp->p;
  1383. rinfo->metric_out = rinfo->metric;
  1384. rinfo->tag_out = rinfo->tag;
  1385. memset(&rinfo->nexthop_out, 0, sizeof(rinfo->nexthop_out));
  1386. /* In order to avoid some local loops,
  1387. * if the RIPng route has a nexthop via this interface, keep the nexthop,
  1388. * otherwise set it to 0. The nexthop should not be propagated
  1389. * beyond the local broadcast/multicast area in order
  1390. * to avoid an IGP multi-level recursive look-up.
  1391. */
  1392. if (rinfo->ifindex == ifp->ifindex)
  1393. rinfo->nexthop_out = rinfo->nexthop;
  1394. /* Apply output filters. */
  1395. ret = ripng_outgoing_filter (p, ri);
  1396. if (ret < 0)
  1397. continue;
  1398. /* Changed route only output. */
  1399. if (route_type == ripng_changed_route &&
  1400. (! (rinfo->flags & RIPNG_RTF_CHANGED)))
  1401. continue;
  1402. /* Split horizon. */
  1403. if (ri->split_horizon == RIPNG_SPLIT_HORIZON)
  1404. {
  1405. /* We perform split horizon for RIPng routes. */
  1406. if ((rinfo->type == ZEBRA_ROUTE_RIPNG) &&
  1407. rinfo->ifindex == ifp->ifindex)
  1408. continue;
  1409. }
  1410. /* Preparation for route-map. */
  1411. rinfo->metric_set = 0;
  1412. /* nexthop_out,
  1413. * metric_out
  1414. * and tag_out are already initialized.
  1415. */
  1416. /* Interface route-map */
  1417. if (ri->routemap[RIPNG_FILTER_OUT])
  1418. {
  1419. int ret;
  1420. ret = route_map_apply (ri->routemap[RIPNG_FILTER_OUT],
  1421. (struct prefix *) p, RMAP_RIPNG,
  1422. rinfo);
  1423. if (ret == RMAP_DENYMATCH)
  1424. {
  1425. if (IS_RIPNG_DEBUG_PACKET)
  1426. zlog_debug ("RIPng %s/%d is filtered by route-map out",
  1427. inet6_ntoa (p->prefix), p->prefixlen);
  1428. continue;
  1429. }
  1430. }
  1431. /* Redistribute route-map. */
  1432. if (ripng->route_map[rinfo->type].name)
  1433. {
  1434. int ret;
  1435. ret = route_map_apply (ripng->route_map[rinfo->type].map,
  1436. (struct prefix *) p, RMAP_RIPNG,
  1437. rinfo);
  1438. if (ret == RMAP_DENYMATCH)
  1439. {
  1440. if (IS_RIPNG_DEBUG_PACKET)
  1441. zlog_debug ("RIPng %s/%d is filtered by route-map",
  1442. inet6_ntoa (p->prefix), p->prefixlen);
  1443. continue;
  1444. }
  1445. }
  1446. /* When the route-map does not set metric. */
  1447. if (! rinfo->metric_set)
  1448. {
  1449. /* If the redistribute metric is set. */
  1450. if (ripng->route_map[rinfo->type].metric_config
  1451. && rinfo->metric != RIPNG_METRIC_INFINITY)
  1452. {
  1453. rinfo->metric_out = ripng->route_map[rinfo->type].metric;
  1454. }
  1455. else
  1456. {
  1457. /* If the route is not connected or localy generated
  1458. one, use default-metric value */
  1459. if (rinfo->type != ZEBRA_ROUTE_RIPNG
  1460. && rinfo->type != ZEBRA_ROUTE_CONNECT
  1461. && rinfo->metric != RIPNG_METRIC_INFINITY)
  1462. rinfo->metric_out = ripng->default_metric;
  1463. }
  1464. }
  1465. /* Apply offset-list */
  1466. if (rinfo->metric_out != RIPNG_METRIC_INFINITY)
  1467. ripng_offset_list_apply_out (p, ifp, &rinfo->metric_out);
  1468. if (rinfo->metric_out > RIPNG_METRIC_INFINITY)
  1469. rinfo->metric_out = RIPNG_METRIC_INFINITY;
  1470. /* Perform split-horizon with poisoned reverse
  1471. * for RIPng routes.
  1472. **/
  1473. if (ri->split_horizon == RIPNG_SPLIT_HORIZON_POISONED_REVERSE) {
  1474. if ((rinfo->type == ZEBRA_ROUTE_RIPNG) &&
  1475. rinfo->ifindex == ifp->ifindex)
  1476. rinfo->metric_out = RIPNG_METRIC_INFINITY;
  1477. }
  1478. /* Add RTE to the list */
  1479. ripng_rte_add(ripng_rte_list, p, rinfo, NULL);
  1480. }
  1481. /* Process the aggregated RTE entry */
  1482. if ((aggregate = rp->aggregate) != NULL &&
  1483. aggregate->count > 0 &&
  1484. aggregate->suppress == 0)
  1485. {
  1486. /* If no route-map are applied, the RTE will be these following
  1487. * informations.
  1488. */
  1489. p = (struct prefix_ipv6 *) &rp->p;
  1490. aggregate->metric_set = 0;
  1491. aggregate->metric_out = aggregate->metric;
  1492. aggregate->tag_out = aggregate->tag;
  1493. memset(&aggregate->nexthop_out, 0, sizeof(aggregate->nexthop_out));
  1494. /* Apply output filters.*/
  1495. ret = ripng_outgoing_filter (p, ri);
  1496. if (ret < 0)
  1497. continue;
  1498. /* Interface route-map */
  1499. if (ri->routemap[RIPNG_FILTER_OUT])
  1500. {
  1501. int ret;
  1502. struct ripng_info newinfo;
  1503. /* let's cast the aggregate structure to ripng_info */
  1504. memset (&newinfo, 0, sizeof (struct ripng_info));
  1505. /* the nexthop is :: */
  1506. newinfo.metric = aggregate->metric;
  1507. newinfo.metric_out = aggregate->metric_out;
  1508. newinfo.tag = aggregate->tag;
  1509. newinfo.tag_out = aggregate->tag_out;
  1510. ret = route_map_apply (ri->routemap[RIPNG_FILTER_OUT],
  1511. (struct prefix *) p, RMAP_RIPNG,
  1512. &newinfo);
  1513. if (ret == RMAP_DENYMATCH)
  1514. {
  1515. if (IS_RIPNG_DEBUG_PACKET)
  1516. zlog_debug ("RIPng %s/%d is filtered by route-map out",
  1517. inet6_ntoa (p->prefix), p->prefixlen);
  1518. continue;
  1519. }
  1520. aggregate->metric_out = newinfo.metric_out;
  1521. aggregate->tag_out = newinfo.tag_out;
  1522. if (IN6_IS_ADDR_LINKLOCAL(&newinfo.nexthop_out))
  1523. aggregate->nexthop_out = newinfo.nexthop_out;
  1524. }
  1525. /* There is no redistribute routemap for the aggregated RTE */
  1526. /* Changed route only output. */
  1527. /* XXX, vincent, in order to increase time convergence,
  1528. * it should be announced if a child has changed.
  1529. */
  1530. if (route_type == ripng_changed_route)
  1531. continue;
  1532. /* Apply offset-list */
  1533. if (aggregate->metric_out != RIPNG_METRIC_INFINITY)
  1534. ripng_offset_list_apply_out (p, ifp, &aggregate->metric_out);
  1535. if (aggregate->metric_out > RIPNG_METRIC_INFINITY)
  1536. aggregate->metric_out = RIPNG_METRIC_INFINITY;
  1537. /* Add RTE to the list */
  1538. ripng_rte_add(ripng_rte_list, p, NULL, aggregate);
  1539. }
  1540. }
  1541. /* Flush the list */
  1542. ripng_rte_send(ripng_rte_list, ifp, to);
  1543. ripng_rte_free(ripng_rte_list);
  1544. }
  1545. /* Create new RIPng instance and set it to global variable. */
  1546. static int
  1547. ripng_create (void)
  1548. {
  1549. /* ripng should be NULL. */
  1550. assert (ripng == NULL);
  1551. /* Allocaste RIPng instance. */
  1552. ripng = XCALLOC (MTYPE_RIPNG, sizeof (struct ripng));
  1553. /* Default version and timer values. */
  1554. ripng->version = RIPNG_V1;
  1555. ripng->update_time = RIPNG_UPDATE_TIMER_DEFAULT;
  1556. ripng->timeout_time = RIPNG_TIMEOUT_TIMER_DEFAULT;
  1557. ripng->garbage_time = RIPNG_GARBAGE_TIMER_DEFAULT;
  1558. ripng->default_metric = RIPNG_DEFAULT_METRIC_DEFAULT;
  1559. /* Make buffer. */
  1560. ripng->ibuf = stream_new (RIPNG_MAX_PACKET_SIZE * 5);
  1561. ripng->obuf = stream_new (RIPNG_MAX_PACKET_SIZE);
  1562. /* Initialize RIPng routig table. */
  1563. ripng->table = route_table_init ();
  1564. ripng->route = route_table_init ();
  1565. ripng->aggregate = route_table_init ();
  1566. /* Make socket. */
  1567. ripng->sock = ripng_make_socket ();
  1568. if (ripng->sock < 0)
  1569. return ripng->sock;
  1570. /* Threads. */
  1571. ripng_event (RIPNG_READ, ripng->sock);
  1572. ripng_event (RIPNG_UPDATE_EVENT, 1);
  1573. return 0;
  1574. }
  1575. /* Send RIPng request to the interface. */
  1576. int
  1577. ripng_request (struct interface *ifp)
  1578. {
  1579. struct rte *rte;
  1580. struct ripng_packet ripng_packet;
  1581. /* In default ripd doesn't send RIP_REQUEST to the loopback interface. */
  1582. if (if_is_loopback(ifp))
  1583. return 0;
  1584. /* If interface is down, don't send RIP packet. */
  1585. if (! if_is_up (ifp))
  1586. return 0;
  1587. if (IS_RIPNG_DEBUG_EVENT)
  1588. zlog_debug ("RIPng send request to %s", ifp->name);
  1589. memset (&ripng_packet, 0, sizeof (ripng_packet));
  1590. ripng_packet.command = RIPNG_REQUEST;
  1591. ripng_packet.version = RIPNG_V1;
  1592. rte = ripng_packet.rte;
  1593. rte->metric = RIPNG_METRIC_INFINITY;
  1594. return ripng_send_packet ((caddr_t) &ripng_packet, sizeof (ripng_packet),
  1595. NULL, ifp);
  1596. }
  1597. static int
  1598. ripng_update_jitter (int time)
  1599. {
  1600. return ((rand () % (time + 1)) - (time / 2));
  1601. }
  1602. void
  1603. ripng_event (enum ripng_event event, int sock)
  1604. {
  1605. int jitter = 0;
  1606. switch (event)
  1607. {
  1608. case RIPNG_READ:
  1609. if (!ripng->t_read)
  1610. ripng->t_read = thread_add_read (master, ripng_read, NULL, sock);
  1611. break;
  1612. case RIPNG_UPDATE_EVENT:
  1613. if (ripng->t_update)
  1614. {
  1615. thread_cancel (ripng->t_update);
  1616. ripng->t_update = NULL;
  1617. }
  1618. /* Update timer jitter. */
  1619. jitter = ripng_update_jitter (ripng->update_time);
  1620. ripng->t_update =
  1621. thread_add_timer (master, ripng_update, NULL,
  1622. sock ? 2 : ripng->update_time + jitter);
  1623. break;
  1624. case RIPNG_TRIGGERED_UPDATE:
  1625. if (ripng->t_triggered_interval)
  1626. ripng->trigger = 1;
  1627. else if (! ripng->t_triggered_update)
  1628. ripng->t_triggered_update =
  1629. thread_add_event (master, ripng_triggered_update, NULL, 0);
  1630. break;
  1631. default:
  1632. break;
  1633. }
  1634. }
  1635. /* Print out routes update time. */
  1636. static void
  1637. ripng_vty_out_uptime (struct vty *vty, struct ripng_info *rinfo)
  1638. {
  1639. time_t clock;
  1640. struct tm *tm;
  1641. #define TIME_BUF 25
  1642. char timebuf [TIME_BUF];
  1643. struct thread *thread;
  1644. if ((thread = rinfo->t_timeout) != NULL)
  1645. {
  1646. clock = thread_timer_remain_second (thread);
  1647. tm = gmtime (&clock);
  1648. strftime (timebuf, TIME_BUF, "%M:%S", tm);
  1649. vty_out (vty, "%5s", timebuf);
  1650. }
  1651. else if ((thread = rinfo->t_garbage_collect) != NULL)
  1652. {
  1653. clock = thread_timer_remain_second (thread);
  1654. tm = gmtime (&clock);
  1655. strftime (timebuf, TIME_BUF, "%M:%S", tm);
  1656. vty_out (vty, "%5s", timebuf);
  1657. }
  1658. }
  1659. static char *
  1660. ripng_route_subtype_print (struct ripng_info *rinfo)
  1661. {
  1662. static char str[3];
  1663. memset(str, 0, 3);
  1664. if (rinfo->suppress)
  1665. strcat(str, "S");
  1666. switch (rinfo->sub_type)
  1667. {
  1668. case RIPNG_ROUTE_RTE:
  1669. strcat(str, "n");
  1670. break;
  1671. case RIPNG_ROUTE_STATIC:
  1672. strcat(str, "s");
  1673. break;
  1674. case RIPNG_ROUTE_DEFAULT:
  1675. strcat(str, "d");
  1676. break;
  1677. case RIPNG_ROUTE_REDISTRIBUTE:
  1678. strcat(str, "r");
  1679. break;
  1680. case RIPNG_ROUTE_INTERFACE:
  1681. strcat(str, "i");
  1682. break;
  1683. default:
  1684. strcat(str, "?");
  1685. break;
  1686. }
  1687. return str;
  1688. }
  1689. DEFUN (show_ipv6_ripng,
  1690. show_ipv6_ripng_cmd,
  1691. "show ipv6 ripng",
  1692. SHOW_STR
  1693. IPV6_STR
  1694. "Show RIPng routes\n")
  1695. {
  1696. struct route_node *rp;
  1697. struct ripng_info *rinfo;
  1698. struct ripng_aggregate *aggregate;
  1699. struct prefix_ipv6 *p;
  1700. int len;
  1701. if (! ripng)
  1702. return CMD_SUCCESS;
  1703. /* Header of display. */
  1704. vty_out (vty, "Codes: R - RIPng, C - connected, S - Static, O - OSPF, B - BGP%s"
  1705. "Sub-codes:%s"
  1706. " (n) - normal, (s) - static, (d) - default, (r) - redistribute,%s"
  1707. " (i) - interface, (a/S) - aggregated/Suppressed%s%s"
  1708. " Network Next Hop Via Metric Tag Time%s",
  1709. VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE,
  1710. VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
  1711. for (rp = route_top (ripng->table); rp; rp = route_next (rp))
  1712. {
  1713. if ((aggregate = rp->aggregate) != NULL)
  1714. {
  1715. p = (struct prefix_ipv6 *) &rp->p;
  1716. #ifdef DEBUG
  1717. len = vty_out (vty, "R(a) %d/%d %s/%d ",
  1718. aggregate->count, aggregate->suppress,
  1719. inet6_ntoa (p->prefix), p->prefixlen);
  1720. #else
  1721. len = vty_out (vty, "R(a) %s/%d ",
  1722. inet6_ntoa (p->prefix), p->prefixlen);
  1723. #endif /* DEBUG */
  1724. vty_out (vty, "%s", VTY_NEWLINE);
  1725. vty_out (vty, "%*s", 18, " ");
  1726. vty_out (vty, "%*s", 28, " ");
  1727. vty_out (vty, "self %2d %3d%s", aggregate->metric,
  1728. aggregate->tag,
  1729. VTY_NEWLINE);
  1730. }
  1731. if ((rinfo = rp->info) != NULL)
  1732. {
  1733. p = (struct prefix_ipv6 *) &rp->p;
  1734. #ifdef DEBUG
  1735. len = vty_out (vty, "%c(%s) 0/%d %s/%d ",
  1736. zebra_route_char(rinfo->type),
  1737. ripng_route_subtype_print(rinfo),
  1738. rinfo->suppress,
  1739. inet6_ntoa (p->prefix), p->prefixlen);
  1740. #else
  1741. len = vty_out (vty, "%c(%s) %s/%d ",
  1742. zebra_route_char(rinfo->type),
  1743. ripng_route_subtype_print(rinfo),
  1744. inet6_ntoa (p->prefix), p->prefixlen);
  1745. #endif /* DEBUG */
  1746. vty_out (vty, "%s", VTY_NEWLINE);
  1747. vty_out (vty, "%*s", 18, " ");
  1748. len = vty_out (vty, "%s", inet6_ntoa (rinfo->nexthop));
  1749. len = 28 - len;
  1750. if (len > 0)
  1751. len = vty_out (vty, "%*s", len, " ");
  1752. /* from */
  1753. if ((rinfo->type == ZEBRA_ROUTE_RIPNG) &&
  1754. (rinfo->sub_type == RIPNG_ROUTE_RTE))
  1755. {
  1756. len = vty_out (vty, "%s", ifindex2ifname(rinfo->ifindex));
  1757. } else if (rinfo->metric == RIPNG_METRIC_INFINITY)
  1758. {
  1759. len = vty_out (vty, "kill");
  1760. } else
  1761. len = vty_out (vty, "self");
  1762. len = 9 - len;
  1763. if (len > 0)
  1764. vty_out (vty, "%*s", len, " ");
  1765. vty_out (vty, " %2d %3d ",
  1766. rinfo->metric, rinfo->tag);
  1767. /* time */
  1768. if ((rinfo->type == ZEBRA_ROUTE_RIPNG) &&
  1769. (rinfo->sub_type == RIPNG_ROUTE_RTE))
  1770. {
  1771. /* RTE from remote RIP routers */
  1772. ripng_vty_out_uptime (vty, rinfo);
  1773. } else if (rinfo->metric == RIPNG_METRIC_INFINITY)
  1774. {
  1775. /* poisonous reversed routes (gc) */
  1776. ripng_vty_out_uptime (vty, rinfo);
  1777. }
  1778. vty_out (vty, "%s", VTY_NEWLINE);
  1779. }
  1780. }
  1781. return CMD_SUCCESS;
  1782. }
  1783. DEFUN (show_ipv6_ripng_status,
  1784. show_ipv6_ripng_status_cmd,
  1785. "show ipv6 ripng status",
  1786. SHOW_STR
  1787. IPV6_STR
  1788. "Show RIPng routes\n"
  1789. "IPv6 routing protocol process parameters and statistics\n")
  1790. {
  1791. struct listnode *node;
  1792. struct interface *ifp;
  1793. if (! ripng)
  1794. return CMD_SUCCESS;
  1795. vty_out (vty, "Routing Protocol is \"RIPng\"%s", VTY_NEWLINE);
  1796. vty_out (vty, " Sending updates every %ld seconds with +/-50%%,",
  1797. ripng->update_time);
  1798. vty_out (vty, " next due in %lu seconds%s",
  1799. thread_timer_remain_second (ripng->t_update),
  1800. VTY_NEWLINE);
  1801. vty_out (vty, " Timeout after %ld seconds,", ripng->timeout_time);
  1802. vty_out (vty, " garbage collect after %ld seconds%s", ripng->garbage_time,
  1803. VTY_NEWLINE);
  1804. /* Filtering status show. */
  1805. config_show_distribute (vty);
  1806. /* Default metric information. */
  1807. vty_out (vty, " Default redistribution metric is %d%s",
  1808. ripng->default_metric, VTY_NEWLINE);
  1809. /* Redistribute information. */
  1810. vty_out (vty, " Redistributing:");
  1811. ripng_redistribute_write (vty, 0);
  1812. vty_out (vty, "%s", VTY_NEWLINE);
  1813. vty_out (vty, " Default version control: send version %d,", ripng->version);
  1814. vty_out (vty, " receive version %d %s", ripng->version,
  1815. VTY_NEWLINE);
  1816. vty_out (vty, " Interface Send Recv%s", VTY_NEWLINE);
  1817. for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
  1818. {
  1819. struct ripng_interface *ri;
  1820. ri = ifp->info;
  1821. if (ri->enable_network || ri->enable_interface)
  1822. {
  1823. vty_out (vty, " %-17s%-3d %-3d%s", ifp->name,
  1824. ripng->version,
  1825. ripng->version,
  1826. VTY_NEWLINE);
  1827. }
  1828. }
  1829. vty_out (vty, " Routing for Networks:%s", VTY_NEWLINE);
  1830. ripng_network_write (vty, 0);
  1831. vty_out (vty, " Routing Information Sources:%s", VTY_NEWLINE);
  1832. vty_out (vty, " Gateway BadPackets BadRoutes Distance Last Update%s", VTY_NEWLINE);
  1833. ripng_peer_display (vty);
  1834. return CMD_SUCCESS;
  1835. }
  1836. DEFUN (router_ripng,
  1837. router_ripng_cmd,
  1838. "router ripng",
  1839. "Enable a routing process\n"
  1840. "Make RIPng instance command\n")
  1841. {
  1842. int ret;
  1843. vty->node = RIPNG_NODE;
  1844. if (!ripng)
  1845. {
  1846. ret = ripng_create ();
  1847. /* Notice to user we couldn't create RIPng. */
  1848. if (ret < 0)
  1849. {
  1850. zlog_warn ("can't create RIPng");
  1851. return CMD_WARNING;
  1852. }
  1853. }
  1854. return CMD_SUCCESS;
  1855. }
  1856. DEFUN (no_router_ripng,
  1857. no_router_ripng_cmd,
  1858. "no router ripng",
  1859. NO_STR
  1860. "Enable a routing process\n"
  1861. "Make RIPng instance command\n")
  1862. {
  1863. if(ripng)
  1864. ripng_clean();
  1865. return CMD_SUCCESS;
  1866. }
  1867. DEFUN (ripng_route,
  1868. ripng_route_cmd,
  1869. "route IPV6ADDR",
  1870. "Static route setup\n"
  1871. "Set static RIPng route announcement\n")
  1872. {
  1873. int ret;
  1874. struct prefix_ipv6 p;
  1875. struct route_node *rp;
  1876. ret = str2prefix_ipv6 (argv[0], (struct prefix_ipv6 *)&p);
  1877. if (ret <= 0)
  1878. {
  1879. vty_out (vty, "Malformed address%s", VTY_NEWLINE);
  1880. return CMD_WARNING;
  1881. }
  1882. apply_mask_ipv6 (&p);
  1883. rp = route_node_get (ripng->route, (struct prefix *) &p);
  1884. if (rp->info)
  1885. {
  1886. vty_out (vty, "There is already same static route.%s", VTY_NEWLINE);
  1887. route_unlock_node (rp);
  1888. return CMD_WARNING;
  1889. }
  1890. rp->info = (void *)1;
  1891. ripng_redistribute_add (ZEBRA_ROUTE_RIPNG, RIPNG_ROUTE_STATIC, &p, 0, NULL);
  1892. return CMD_SUCCESS;
  1893. }
  1894. DEFUN (no_ripng_route,
  1895. no_ripng_route_cmd,
  1896. "no route IPV6ADDR",
  1897. NO_STR
  1898. "Static route setup\n"
  1899. "Delete static RIPng route announcement\n")
  1900. {
  1901. int ret;
  1902. struct prefix_ipv6 p;
  1903. struct route_node *rp;
  1904. ret = str2prefix_ipv6 (argv[0], (struct prefix_ipv6 *)&p);
  1905. if (ret <= 0)
  1906. {
  1907. vty_out (vty, "Malformed address%s", VTY_NEWLINE);
  1908. return CMD_WARNING;
  1909. }
  1910. apply_mask_ipv6 (&p);
  1911. rp = route_node_lookup (ripng->route, (struct prefix *) &p);
  1912. if (! rp)
  1913. {
  1914. vty_out (vty, "Can't find static route.%s", VTY_NEWLINE);
  1915. return CMD_WARNING;
  1916. }
  1917. ripng_redistribute_delete (ZEBRA_ROUTE_RIPNG, RIPNG_ROUTE_STATIC, &p, 0);
  1918. route_unlock_node (rp);
  1919. rp->info = NULL;
  1920. route_unlock_node (rp);
  1921. return CMD_SUCCESS;
  1922. }
  1923. DEFUN (ripng_aggregate_address,
  1924. ripng_aggregate_address_cmd,
  1925. "aggregate-address X:X::X:X/M",
  1926. "Set aggregate RIPng route announcement\n"
  1927. "Aggregate network\n")
  1928. {
  1929. int ret;
  1930. struct prefix p;
  1931. struct route_node *node;
  1932. ret = str2prefix_ipv6 (argv[0], (struct prefix_ipv6 *)&p);
  1933. if (ret <= 0)
  1934. {
  1935. vty_out (vty, "Malformed address%s", VTY_NEWLINE);
  1936. return CMD_WARNING;
  1937. }
  1938. /* Check aggregate alredy exist or not. */
  1939. node = route_node_get (ripng->aggregate, &p);
  1940. if (node->info)
  1941. {
  1942. vty_out (vty, "There is already same aggregate route.%s", VTY_NEWLINE);
  1943. route_unlock_node (node);
  1944. return CMD_WARNING;
  1945. }
  1946. node->info = (void *)1;
  1947. ripng_aggregate_add (&p);
  1948. return CMD_SUCCESS;
  1949. }
  1950. DEFUN (no_ripng_aggregate_address,
  1951. no_ripng_aggregate_address_cmd,
  1952. "no aggregate-address X:X::X:X/M",
  1953. NO_STR
  1954. "Delete aggregate RIPng route announcement\n"
  1955. "Aggregate network")
  1956. {
  1957. int ret;
  1958. struct prefix p;
  1959. struct route_node *rn;
  1960. ret = str2prefix_ipv6 (argv[0], (struct prefix_ipv6 *) &p);
  1961. if (ret <= 0)
  1962. {
  1963. vty_out (vty, "Malformed address%s", VTY_NEWLINE);
  1964. return CMD_WARNING;
  1965. }
  1966. rn = route_node_lookup (ripng->aggregate, &p);
  1967. if (! rn)
  1968. {
  1969. vty_out (vty, "Can't find aggregate route.%s", VTY_NEWLINE);
  1970. return CMD_WARNING;
  1971. }
  1972. route_unlock_node (rn);
  1973. rn->info = NULL;
  1974. route_unlock_node (rn);
  1975. ripng_aggregate_delete (&p);
  1976. return CMD_SUCCESS;
  1977. }
  1978. DEFUN (ripng_default_metric,
  1979. ripng_default_metric_cmd,
  1980. "default-metric <1-16>",
  1981. "Set a metric of redistribute routes\n"
  1982. "Default metric\n")
  1983. {
  1984. if (ripng)
  1985. {
  1986. ripng->default_metric = atoi (argv[0]);
  1987. }
  1988. return CMD_SUCCESS;
  1989. }
  1990. DEFUN (no_ripng_default_metric,
  1991. no_ripng_default_metric_cmd,
  1992. "no default-metric",
  1993. NO_STR
  1994. "Set a metric of redistribute routes\n"
  1995. "Default metric\n")
  1996. {
  1997. if (ripng)
  1998. {
  1999. ripng->default_metric = RIPNG_DEFAULT_METRIC_DEFAULT;
  2000. }
  2001. return CMD_SUCCESS;
  2002. }
  2003. ALIAS (no_ripng_default_metric,
  2004. no_ripng_default_metric_val_cmd,
  2005. "no default-metric <1-16>",
  2006. NO_STR
  2007. "Set a metric of redistribute routes\n"
  2008. "Default metric\n")
  2009. #if 0
  2010. /* RIPng update timer setup. */
  2011. DEFUN (ripng_update_timer,
  2012. ripng_update_timer_cmd,
  2013. "update-timer SECOND",
  2014. "Set RIPng update timer in seconds\n"
  2015. "Seconds\n")
  2016. {
  2017. unsigned long update;
  2018. char *endptr = NULL;
  2019. update = strtoul (argv[0], &endptr, 10);
  2020. if (update == ULONG_MAX || *endptr != '\0')
  2021. {
  2022. vty_out (vty, "update timer value error%s", VTY_NEWLINE);
  2023. return CMD_WARNING;
  2024. }
  2025. ripng->update_time = update;
  2026. ripng_event (RIPNG_UPDATE_EVENT, 0);
  2027. return CMD_SUCCESS;
  2028. }
  2029. DEFUN (no_ripng_update_timer,
  2030. no_ripng_update_timer_cmd,
  2031. "no update-timer SECOND",
  2032. NO_STR
  2033. "Unset RIPng update timer in seconds\n"
  2034. "Seconds\n")
  2035. {
  2036. ripng->update_time = RIPNG_UPDATE_TIMER_DEFAULT;
  2037. ripng_event (RIPNG_UPDATE_EVENT, 0);
  2038. return CMD_SUCCESS;
  2039. }
  2040. /* RIPng timeout timer setup. */
  2041. DEFUN (ripng_timeout_timer,
  2042. ripng_timeout_timer_cmd,
  2043. "timeout-timer SECOND",
  2044. "Set RIPng timeout timer in seconds\n"
  2045. "Seconds\n")
  2046. {
  2047. unsigned long timeout;
  2048. char *endptr = NULL;
  2049. timeout = strtoul (argv[0], &endptr, 10);
  2050. if (timeout == ULONG_MAX || *endptr != '\0')
  2051. {
  2052. vty_out (vty, "timeout timer value error%s", VTY_NEWLINE);
  2053. return CMD_WARNING;
  2054. }
  2055. ripng->timeout_time = timeout;
  2056. return CMD_SUCCESS;
  2057. }
  2058. DEFUN (no_ripng_timeout_timer,
  2059. no_ripng_timeout_timer_cmd,
  2060. "no timeout-timer SECOND",
  2061. NO_STR
  2062. "Unset RIPng timeout timer in seconds\n"
  2063. "Seconds\n")
  2064. {
  2065. ripng->timeout_time = RIPNG_TIMEOUT_TIMER_DEFAULT;
  2066. return CMD_SUCCESS;
  2067. }
  2068. /* RIPng garbage timer setup. */
  2069. DEFUN (ripng_garbage_timer,
  2070. ripng_garbage_timer_cmd,
  2071. "garbage-timer SECOND",
  2072. "Set RIPng garbage timer in seconds\n"
  2073. "Seconds\n")
  2074. {
  2075. unsigned long garbage;
  2076. char *endptr = NULL;
  2077. garbage = strtoul (argv[0], &endptr, 10);
  2078. if (garbage == ULONG_MAX || *endptr != '\0')
  2079. {
  2080. vty_out (vty, "garbage timer value error%s", VTY_NEWLINE);
  2081. return CMD_WARNING;
  2082. }
  2083. ripng->garbage_time = garbage;
  2084. return CMD_SUCCESS;
  2085. }
  2086. DEFUN (no_ripng_garbage_timer,
  2087. no_ripng_garbage_timer_cmd,
  2088. "no garbage-timer SECOND",
  2089. NO_STR
  2090. "Unset RIPng garbage timer in seconds\n"
  2091. "Seconds\n")
  2092. {
  2093. ripng->garbage_time = RIPNG_GARBAGE_TIMER_DEFAULT;
  2094. return CMD_SUCCESS;
  2095. }
  2096. #endif /* 0 */
  2097. DEFUN (ripng_timers,
  2098. ripng_timers_cmd,
  2099. "timers basic <0-65535> <0-65535> <0-65535>",
  2100. "RIPng timers setup\n"
  2101. "Basic timer\n"
  2102. "Routing table update timer value in second. Default is 30.\n"
  2103. "Routing information timeout timer. Default is 180.\n"
  2104. "Garbage collection timer. Default is 120.\n")
  2105. {
  2106. unsigned long update;
  2107. unsigned long timeout;
  2108. unsigned long garbage;
  2109. char *endptr = NULL;
  2110. update = strtoul (argv[0], &endptr, 10);
  2111. if (update == ULONG_MAX || *endptr != '\0')
  2112. {
  2113. vty_out (vty, "update timer value error%s", VTY_NEWLINE);
  2114. return CMD_WARNING;
  2115. }
  2116. timeout = strtoul (argv[1], &endptr, 10);
  2117. if (timeout == ULONG_MAX || *endptr != '\0')
  2118. {
  2119. vty_out (vty, "timeout timer value error%s", VTY_NEWLINE);
  2120. return CMD_WARNING;
  2121. }
  2122. garbage = strtoul (argv[2], &endptr, 10);
  2123. if (garbage == ULONG_MAX || *endptr != '\0')
  2124. {
  2125. vty_out (vty, "garbage timer value error%s", VTY_NEWLINE);
  2126. return CMD_WARNING;
  2127. }
  2128. /* Set each timer value. */
  2129. ripng->update_time = update;
  2130. ripng->timeout_time = timeout;
  2131. ripng->garbage_time = garbage;
  2132. /* Reset update timer thread. */
  2133. ripng_event (RIPNG_UPDATE_EVENT, 0);
  2134. return CMD_SUCCESS;
  2135. }
  2136. DEFUN (no_ripng_timers,
  2137. no_ripng_timers_cmd,
  2138. "no timers basic",
  2139. NO_STR
  2140. "RIPng timers setup\n"
  2141. "Basic timer\n")
  2142. {
  2143. /* Set each timer value to the default. */
  2144. ripng->update_time = RIPNG_UPDATE_TIMER_DEFAULT;
  2145. ripng->timeout_time = RIPNG_TIMEOUT_TIMER_DEFAULT;
  2146. ripng->garbage_time = RIPNG_GARBAGE_TIMER_DEFAULT;
  2147. /* Reset update timer thread. */
  2148. ripng_event (RIPNG_UPDATE_EVENT, 0);
  2149. return CMD_SUCCESS;
  2150. }
  2151. ALIAS (no_ripng_timers,
  2152. no_ripng_timers_val_cmd,
  2153. "no timers basic <0-65535> <0-65535> <0-65535>",
  2154. NO_STR
  2155. "RIPng timers setup\n"
  2156. "Basic timer\n"
  2157. "Routing table update timer value in second. Default is 30.\n"
  2158. "Routing information timeout timer. Default is 180.\n"
  2159. "Garbage collection timer. Default is 120.\n")
  2160. DEFUN (show_ipv6_protocols, show_ipv6_protocols_cmd,
  2161. "show ipv6 protocols",
  2162. SHOW_STR
  2163. IPV6_STR
  2164. "Routing protocol information")
  2165. {
  2166. if (! ripng)
  2167. return CMD_SUCCESS;
  2168. vty_out (vty, "Routing Protocol is \"ripng\"%s", VTY_NEWLINE);
  2169. vty_out (vty, "Sending updates every %ld seconds, next due in %d seconds%s",
  2170. ripng->update_time, 0,
  2171. VTY_NEWLINE);
  2172. vty_out (vty, "Timerout after %ld seconds, garbage correct %ld%s",
  2173. ripng->timeout_time,
  2174. ripng->garbage_time,
  2175. VTY_NEWLINE);
  2176. vty_out (vty, "Outgoing update filter list for all interfaces is not set");
  2177. vty_out (vty, "Incoming update filter list for all interfaces is not set");
  2178. return CMD_SUCCESS;
  2179. }
  2180. /* Please be carefull to use this command. */
  2181. DEFUN (ripng_default_information_originate,
  2182. ripng_default_information_originate_cmd,
  2183. "default-information originate",
  2184. "Default route information\n"
  2185. "Distribute default route\n")
  2186. {
  2187. struct prefix_ipv6 p;
  2188. if (! ripng ->default_information) {
  2189. ripng->default_information = 1;
  2190. str2prefix_ipv6 ("::/0", &p);
  2191. ripng_redistribute_add (ZEBRA_ROUTE_RIPNG, RIPNG_ROUTE_DEFAULT, &p, 0, NULL);
  2192. }
  2193. return CMD_SUCCESS;
  2194. }
  2195. DEFUN (no_ripng_default_information_originate,
  2196. no_ripng_default_information_originate_cmd,
  2197. "no default-information originate",
  2198. NO_STR
  2199. "Default route information\n"
  2200. "Distribute default route\n")
  2201. {
  2202. struct prefix_ipv6 p;
  2203. if (ripng->default_information) {
  2204. ripng->default_information = 0;
  2205. str2prefix_ipv6 ("::/0", &p);
  2206. ripng_redistribute_delete (ZEBRA_ROUTE_RIPNG, RIPNG_ROUTE_DEFAULT, &p, 0);
  2207. }
  2208. return CMD_SUCCESS;
  2209. }
  2210. /* RIPng configuration write function. */
  2211. static int
  2212. ripng_config_write (struct vty *vty)
  2213. {
  2214. int ripng_network_write (struct vty *, int);
  2215. void ripng_redistribute_write (struct vty *, int);
  2216. int write = 0;
  2217. struct route_node *rp;
  2218. if (ripng)
  2219. {
  2220. /* RIPng router. */
  2221. vty_out (vty, "router ripng%s", VTY_NEWLINE);
  2222. if (ripng->default_information)
  2223. vty_out (vty, " default-information originate%s", VTY_NEWLINE);
  2224. ripng_network_write (vty, 1);
  2225. /* RIPng default metric configuration */
  2226. if (ripng->default_metric != RIPNG_DEFAULT_METRIC_DEFAULT)
  2227. vty_out (vty, " default-metric %d%s",
  2228. ripng->default_metric, VTY_NEWLINE);
  2229. ripng_redistribute_write (vty, 1);
  2230. /* RIP offset-list configuration. */
  2231. config_write_ripng_offset_list (vty);
  2232. /* RIPng aggregate routes. */
  2233. for (rp = route_top (ripng->aggregate); rp; rp = route_next (rp))
  2234. if (rp->info != NULL)
  2235. vty_out (vty, " aggregate-address %s/%d%s",
  2236. inet6_ntoa (rp->p.u.prefix6),
  2237. rp->p.prefixlen,
  2238. VTY_NEWLINE);
  2239. /* RIPng static routes. */
  2240. for (rp = route_top (ripng->route); rp; rp = route_next (rp))
  2241. if (rp->info != NULL)
  2242. vty_out (vty, " route %s/%d%s", inet6_ntoa (rp->p.u.prefix6),
  2243. rp->p.prefixlen,
  2244. VTY_NEWLINE);
  2245. /* RIPng timers configuration. */
  2246. if (ripng->update_time != RIPNG_UPDATE_TIMER_DEFAULT ||
  2247. ripng->timeout_time != RIPNG_TIMEOUT_TIMER_DEFAULT ||
  2248. ripng->garbage_time != RIPNG_GARBAGE_TIMER_DEFAULT)
  2249. {
  2250. vty_out (vty, " timers basic %ld %ld %ld%s",
  2251. ripng->update_time,
  2252. ripng->timeout_time,
  2253. ripng->garbage_time,
  2254. VTY_NEWLINE);
  2255. }
  2256. #if 0
  2257. if (ripng->update_time != RIPNG_UPDATE_TIMER_DEFAULT)
  2258. vty_out (vty, " update-timer %d%s", ripng->update_time,
  2259. VTY_NEWLINE);
  2260. if (ripng->timeout_time != RIPNG_TIMEOUT_TIMER_DEFAULT)
  2261. vty_out (vty, " timeout-timer %d%s", ripng->timeout_time,
  2262. VTY_NEWLINE);
  2263. if (ripng->garbage_time != RIPNG_GARBAGE_TIMER_DEFAULT)
  2264. vty_out (vty, " garbage-timer %d%s", ripng->garbage_time,
  2265. VTY_NEWLINE);
  2266. #endif /* 0 */
  2267. write += config_write_distribute (vty);
  2268. write += config_write_if_rmap (vty);
  2269. write++;
  2270. }
  2271. return write;
  2272. }
  2273. /* RIPng node structure. */
  2274. struct cmd_node cmd_ripng_node =
  2275. {
  2276. RIPNG_NODE,
  2277. "%s(config-router)# ",
  2278. 1,
  2279. };
  2280. static void
  2281. ripng_distribute_update (struct distribute *dist)
  2282. {
  2283. struct interface *ifp;
  2284. struct ripng_interface *ri;
  2285. struct access_list *alist;
  2286. struct prefix_list *plist;
  2287. if (! dist->ifname)
  2288. return;
  2289. ifp = if_lookup_by_name (dist->ifname);
  2290. if (ifp == NULL)
  2291. return;
  2292. ri = ifp->info;
  2293. if (dist->list[DISTRIBUTE_IN])
  2294. {
  2295. alist = access_list_lookup (AFI_IP6, dist->list[DISTRIBUTE_IN]);
  2296. if (alist)
  2297. ri->list[RIPNG_FILTER_IN] = alist;
  2298. else
  2299. ri->list[RIPNG_FILTER_IN] = NULL;
  2300. }
  2301. else
  2302. ri->list[RIPNG_FILTER_IN] = NULL;
  2303. if (dist->list[DISTRIBUTE_OUT])
  2304. {
  2305. alist = access_list_lookup (AFI_IP6, dist->list[DISTRIBUTE_OUT]);
  2306. if (alist)
  2307. ri->list[RIPNG_FILTER_OUT] = alist;
  2308. else
  2309. ri->list[RIPNG_FILTER_OUT] = NULL;
  2310. }
  2311. else
  2312. ri->list[RIPNG_FILTER_OUT] = NULL;
  2313. if (dist->prefix[DISTRIBUTE_IN])
  2314. {
  2315. plist = prefix_list_lookup (AFI_IP6, dist->prefix[DISTRIBUTE_IN]);
  2316. if (plist)
  2317. ri->prefix[RIPNG_FILTER_IN] = plist;
  2318. else
  2319. ri->prefix[RIPNG_FILTER_IN] = NULL;
  2320. }
  2321. else
  2322. ri->prefix[RIPNG_FILTER_IN] = NULL;
  2323. if (dist->prefix[DISTRIBUTE_OUT])
  2324. {
  2325. plist = prefix_list_lookup (AFI_IP6, dist->prefix[DISTRIBUTE_OUT]);
  2326. if (plist)
  2327. ri->prefix[RIPNG_FILTER_OUT] = plist;
  2328. else
  2329. ri->prefix[RIPNG_FILTER_OUT] = NULL;
  2330. }
  2331. else
  2332. ri->prefix[RIPNG_FILTER_OUT] = NULL;
  2333. }
  2334. void
  2335. ripng_distribute_update_interface (struct interface *ifp)
  2336. {
  2337. struct distribute *dist;
  2338. dist = distribute_lookup (ifp->name);
  2339. if (dist)
  2340. ripng_distribute_update (dist);
  2341. }
  2342. /* Update all interface's distribute list. */
  2343. static void
  2344. ripng_distribute_update_all (struct prefix_list *notused)
  2345. {
  2346. struct interface *ifp;
  2347. struct listnode *node;
  2348. for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
  2349. ripng_distribute_update_interface (ifp);
  2350. }
  2351. static void
  2352. ripng_distribute_update_all_wrapper (struct access_list *notused)
  2353. {
  2354. ripng_distribute_update_all(NULL);
  2355. }
  2356. /* delete all the added ripng routes. */
  2357. void
  2358. ripng_clean()
  2359. {
  2360. int i;
  2361. struct route_node *rp;
  2362. struct ripng_info *rinfo;
  2363. if (ripng) {
  2364. /* Clear RIPng routes */
  2365. for (rp = route_top (ripng->table); rp; rp = route_next (rp)) {
  2366. if ((rinfo = rp->info) != NULL) {
  2367. if ((rinfo->type == ZEBRA_ROUTE_RIPNG) &&
  2368. (rinfo->sub_type == RIPNG_ROUTE_RTE))
  2369. ripng_zebra_ipv6_delete ((struct prefix_ipv6 *)&rp->p,
  2370. &rinfo->nexthop, rinfo->metric);
  2371. RIPNG_TIMER_OFF (rinfo->t_timeout);
  2372. RIPNG_TIMER_OFF (rinfo->t_garbage_collect);
  2373. rp->info = NULL;
  2374. route_unlock_node (rp);
  2375. ripng_info_free(rinfo);
  2376. }
  2377. }
  2378. /* Cancel the RIPng timers */
  2379. RIPNG_TIMER_OFF (ripng->t_update);
  2380. RIPNG_TIMER_OFF (ripng->t_triggered_update);
  2381. RIPNG_TIMER_OFF (ripng->t_triggered_interval);
  2382. /* Cancel the read thread */
  2383. if (ripng->t_read) {
  2384. thread_cancel (ripng->t_read);
  2385. ripng->t_read = NULL;
  2386. }
  2387. /* Close the RIPng socket */
  2388. if (ripng->sock >= 0) {
  2389. close(ripng->sock);
  2390. ripng->sock = -1;
  2391. }
  2392. /* Static RIPng route configuration. */
  2393. for (rp = route_top (ripng->route); rp; rp = route_next (rp))
  2394. if (rp->info) {
  2395. rp->info = NULL;
  2396. route_unlock_node (rp);
  2397. }
  2398. /* RIPng aggregated prefixes */
  2399. for (rp = route_top (ripng->aggregate); rp; rp = route_next (rp))
  2400. if (rp->info) {
  2401. rp->info = NULL;
  2402. route_unlock_node (rp);
  2403. }
  2404. for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
  2405. if (ripng->route_map[i].name)
  2406. free (ripng->route_map[i].name);
  2407. XFREE (MTYPE_ROUTE_TABLE, ripng->table);
  2408. XFREE (MTYPE_ROUTE_TABLE, ripng->route);
  2409. XFREE (MTYPE_ROUTE_TABLE, ripng->aggregate);
  2410. XFREE (MTYPE_RIPNG, ripng);
  2411. ripng = NULL;
  2412. } /* if (ripng) */
  2413. ripng_clean_network();
  2414. ripng_passive_interface_clean ();
  2415. ripng_offset_clean ();
  2416. ripng_interface_clean ();
  2417. ripng_redistribute_clean ();
  2418. }
  2419. /* Reset all values to the default settings. */
  2420. void
  2421. ripng_reset ()
  2422. {
  2423. /* Call ripd related reset functions. */
  2424. ripng_debug_reset ();
  2425. ripng_route_map_reset ();
  2426. /* Call library reset functions. */
  2427. vty_reset ();
  2428. access_list_reset ();
  2429. prefix_list_reset ();
  2430. distribute_list_reset ();
  2431. ripng_interface_reset ();
  2432. ripng_zclient_reset ();
  2433. }
  2434. static void
  2435. ripng_if_rmap_update (struct if_rmap *if_rmap)
  2436. {
  2437. struct interface *ifp;
  2438. struct ripng_interface *ri;
  2439. struct route_map *rmap;
  2440. ifp = if_lookup_by_name (if_rmap->ifname);
  2441. if (ifp == NULL)
  2442. return;
  2443. ri = ifp->info;
  2444. if (if_rmap->routemap[IF_RMAP_IN])
  2445. {
  2446. rmap = route_map_lookup_by_name (if_rmap->routemap[IF_RMAP_IN]);
  2447. if (rmap)
  2448. ri->routemap[IF_RMAP_IN] = rmap;
  2449. else
  2450. ri->routemap[IF_RMAP_IN] = NULL;
  2451. }
  2452. else
  2453. ri->routemap[RIPNG_FILTER_IN] = NULL;
  2454. if (if_rmap->routemap[IF_RMAP_OUT])
  2455. {
  2456. rmap = route_map_lookup_by_name (if_rmap->routemap[IF_RMAP_OUT]);
  2457. if (rmap)
  2458. ri->routemap[IF_RMAP_OUT] = rmap;
  2459. else
  2460. ri->routemap[IF_RMAP_OUT] = NULL;
  2461. }
  2462. else
  2463. ri->routemap[RIPNG_FILTER_OUT] = NULL;
  2464. }
  2465. void
  2466. ripng_if_rmap_update_interface (struct interface *ifp)
  2467. {
  2468. struct if_rmap *if_rmap;
  2469. if_rmap = if_rmap_lookup (ifp->name);
  2470. if (if_rmap)
  2471. ripng_if_rmap_update (if_rmap);
  2472. }
  2473. static void
  2474. ripng_routemap_update_redistribute (void)
  2475. {
  2476. int i;
  2477. if (ripng)
  2478. {
  2479. for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
  2480. {
  2481. if (ripng->route_map[i].name)
  2482. ripng->route_map[i].map =
  2483. route_map_lookup_by_name (ripng->route_map[i].name);
  2484. }
  2485. }
  2486. }
  2487. static void
  2488. ripng_routemap_update (const char *unused)
  2489. {
  2490. struct interface *ifp;
  2491. struct listnode *node;
  2492. for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
  2493. ripng_if_rmap_update_interface (ifp);
  2494. ripng_routemap_update_redistribute ();
  2495. }
  2496. /* Initialize ripng structure and set commands. */
  2497. void
  2498. ripng_init ()
  2499. {
  2500. /* Randomize. */
  2501. srand (time (NULL));
  2502. /* Install RIPNG_NODE. */
  2503. install_node (&cmd_ripng_node, ripng_config_write);
  2504. /* Install ripng commands. */
  2505. install_element (VIEW_NODE, &show_ipv6_ripng_cmd);
  2506. install_element (VIEW_NODE, &show_ipv6_ripng_status_cmd);
  2507. install_element (ENABLE_NODE, &show_ipv6_ripng_cmd);
  2508. install_element (ENABLE_NODE, &show_ipv6_ripng_status_cmd);
  2509. install_element (CONFIG_NODE, &router_ripng_cmd);
  2510. install_element (CONFIG_NODE, &no_router_ripng_cmd);
  2511. install_default (RIPNG_NODE);
  2512. install_element (RIPNG_NODE, &ripng_route_cmd);
  2513. install_element (RIPNG_NODE, &no_ripng_route_cmd);
  2514. install_element (RIPNG_NODE, &ripng_aggregate_address_cmd);
  2515. install_element (RIPNG_NODE, &no_ripng_aggregate_address_cmd);
  2516. install_element (RIPNG_NODE, &ripng_default_metric_cmd);
  2517. install_element (RIPNG_NODE, &no_ripng_default_metric_cmd);
  2518. install_element (RIPNG_NODE, &no_ripng_default_metric_val_cmd);
  2519. install_element (RIPNG_NODE, &ripng_timers_cmd);
  2520. install_element (RIPNG_NODE, &no_ripng_timers_cmd);
  2521. install_element (RIPNG_NODE, &no_ripng_timers_val_cmd);
  2522. #if 0
  2523. install_element (RIPNG_NODE, &ripng_update_timer_cmd);
  2524. install_element (RIPNG_NODE, &no_ripng_update_timer_cmd);
  2525. install_element (RIPNG_NODE, &ripng_timeout_timer_cmd);
  2526. install_element (RIPNG_NODE, &no_ripng_timeout_timer_cmd);
  2527. install_element (RIPNG_NODE, &ripng_garbage_timer_cmd);
  2528. install_element (RIPNG_NODE, &no_ripng_garbage_timer_cmd);
  2529. #endif /* 0 */
  2530. install_element (RIPNG_NODE, &ripng_default_information_originate_cmd);
  2531. install_element (RIPNG_NODE, &no_ripng_default_information_originate_cmd);
  2532. ripng_if_init ();
  2533. ripng_debug_init ();
  2534. /* Access list install. */
  2535. access_list_init ();
  2536. access_list_add_hook (ripng_distribute_update_all_wrapper);
  2537. access_list_delete_hook (ripng_distribute_update_all_wrapper);
  2538. /* Prefix list initialize.*/
  2539. prefix_list_init ();
  2540. prefix_list_add_hook (ripng_distribute_update_all);
  2541. prefix_list_delete_hook (ripng_distribute_update_all);
  2542. /* Distribute list install. */
  2543. distribute_list_init (RIPNG_NODE);
  2544. distribute_list_add_hook (ripng_distribute_update);
  2545. distribute_list_delete_hook (ripng_distribute_update);
  2546. /* Route-map for interface. */
  2547. ripng_route_map_init ();
  2548. ripng_offset_init ();
  2549. route_map_add_hook (ripng_routemap_update);
  2550. route_map_delete_hook (ripng_routemap_update);
  2551. if_rmap_init (RIPNG_NODE);
  2552. if_rmap_hook_add (ripng_if_rmap_update);
  2553. if_rmap_hook_delete (ripng_if_rmap_update);
  2554. }