ripd.c 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076
  1. /* RIP version 1 and 2.
  2. * Copyright (C) 2005 6WIND <alain.ritoux@6wind.com>
  3. * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro <kunihiro@zebra.org>
  4. *
  5. * This file is part of GNU Zebra.
  6. *
  7. * GNU Zebra is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2, or (at your option) any
  10. * later version.
  11. *
  12. * GNU Zebra is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  19. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. * 02111-1307, USA.
  21. */
  22. #include <zebra.h>
  23. #include "if.h"
  24. #include "command.h"
  25. #include "prefix.h"
  26. #include "table.h"
  27. #include "thread.h"
  28. #include "memory.h"
  29. #include "log.h"
  30. #include "stream.h"
  31. #include "filter.h"
  32. #include "sockunion.h"
  33. #include "sockopt.h"
  34. #include "routemap.h"
  35. #include "if_rmap.h"
  36. #include "plist.h"
  37. #include "distribute.h"
  38. #include "md5.h"
  39. #include "keychain.h"
  40. #include "privs.h"
  41. #include "ripd/ripd.h"
  42. #include "ripd/rip_debug.h"
  43. /* UDP receive buffer size */
  44. #define RIP_UDP_RCV_BUF 41600
  45. /* privileges global */
  46. extern struct zebra_privs_t ripd_privs;
  47. /* RIP Structure. */
  48. struct rip *rip = NULL;
  49. /* RIP neighbor address table. */
  50. struct route_table *rip_neighbor_table;
  51. /* RIP route changes. */
  52. long rip_global_route_changes = 0;
  53. /* RIP queries. */
  54. long rip_global_queries = 0;
  55. /* Prototypes. */
  56. static void rip_event (enum rip_event, int);
  57. static void rip_output_process (struct connected *, struct sockaddr_in *, int, u_char);
  58. static int rip_triggered_update (struct thread *);
  59. static int rip_update_jitter (unsigned long);
  60. /* RIP output routes type. */
  61. enum
  62. {
  63. rip_all_route,
  64. rip_changed_route
  65. };
  66. /* RIP command strings. */
  67. struct message rip_msg[] =
  68. {
  69. {RIP_REQUEST, "REQUEST"},
  70. {RIP_RESPONSE, "RESPONSE"},
  71. {RIP_TRACEON, "TRACEON"},
  72. {RIP_TRACEOFF, "TRACEOFF"},
  73. {RIP_POLL, "POLL"},
  74. {RIP_POLL_ENTRY, "POLL ENTRY"},
  75. {0, NULL}
  76. };
  77. /* Utility function to set boradcast option to the socket. */
  78. static int
  79. sockopt_broadcast (int sock)
  80. {
  81. int ret;
  82. int on = 1;
  83. ret = setsockopt (sock, SOL_SOCKET, SO_BROADCAST, (char *) &on, sizeof on);
  84. if (ret < 0)
  85. {
  86. zlog_warn ("can't set sockopt SO_BROADCAST to socket %d", sock);
  87. return -1;
  88. }
  89. return 0;
  90. }
  91. static int
  92. rip_route_rte (struct rip_info *rinfo)
  93. {
  94. return (rinfo->type == ZEBRA_ROUTE_RIP && rinfo->sub_type == RIP_ROUTE_RTE);
  95. }
  96. static struct rip_info *
  97. rip_info_new ()
  98. {
  99. struct rip_info *new;
  100. new = XMALLOC (MTYPE_RIP_INFO, sizeof (struct rip_info));
  101. memset (new, 0, sizeof (struct rip_info));
  102. return new;
  103. }
  104. void
  105. rip_info_free (struct rip_info *rinfo)
  106. {
  107. XFREE (MTYPE_RIP_INFO, rinfo);
  108. }
  109. /* RIP route garbage collect timer. */
  110. static int
  111. rip_garbage_collect (struct thread *t)
  112. {
  113. struct rip_info *rinfo;
  114. struct route_node *rp;
  115. rinfo = THREAD_ARG (t);
  116. rinfo->t_garbage_collect = NULL;
  117. /* Off timeout timer. */
  118. RIP_TIMER_OFF (rinfo->t_timeout);
  119. /* Get route_node pointer. */
  120. rp = rinfo->rp;
  121. /* Unlock route_node. */
  122. rp->info = NULL;
  123. route_unlock_node (rp);
  124. /* Free RIP routing information. */
  125. rip_info_free (rinfo);
  126. return 0;
  127. }
  128. /* Timeout RIP routes. */
  129. static int
  130. rip_timeout (struct thread *t)
  131. {
  132. struct rip_info *rinfo;
  133. struct route_node *rn;
  134. rinfo = THREAD_ARG (t);
  135. rinfo->t_timeout = NULL;
  136. rn = rinfo->rp;
  137. /* - The garbage-collection timer is set for 120 seconds. */
  138. RIP_TIMER_ON (rinfo->t_garbage_collect, rip_garbage_collect,
  139. rip->garbage_time);
  140. rip_zebra_ipv4_delete ((struct prefix_ipv4 *)&rn->p, &rinfo->nexthop,
  141. rinfo->metric);
  142. /* - The metric for the route is set to 16 (infinity). This causes
  143. the route to be removed from service. */
  144. rinfo->metric = RIP_METRIC_INFINITY;
  145. rinfo->flags &= ~RIP_RTF_FIB;
  146. /* - The route change flag is to indicate that this entry has been
  147. changed. */
  148. rinfo->flags |= RIP_RTF_CHANGED;
  149. /* - The output process is signalled to trigger a response. */
  150. rip_event (RIP_TRIGGERED_UPDATE, 0);
  151. return 0;
  152. }
  153. static void
  154. rip_timeout_update (struct rip_info *rinfo)
  155. {
  156. if (rinfo->metric != RIP_METRIC_INFINITY)
  157. {
  158. RIP_TIMER_OFF (rinfo->t_timeout);
  159. RIP_TIMER_ON (rinfo->t_timeout, rip_timeout, rip->timeout_time);
  160. }
  161. }
  162. static int
  163. rip_incoming_filter (struct prefix_ipv4 *p, struct rip_interface *ri)
  164. {
  165. struct distribute *dist;
  166. struct access_list *alist;
  167. struct prefix_list *plist;
  168. /* Input distribute-list filtering. */
  169. if (ri->list[RIP_FILTER_IN])
  170. {
  171. if (access_list_apply (ri->list[RIP_FILTER_IN],
  172. (struct prefix *) p) == FILTER_DENY)
  173. {
  174. if (IS_RIP_DEBUG_PACKET)
  175. zlog_debug ("%s/%d filtered by distribute in",
  176. inet_ntoa (p->prefix), p->prefixlen);
  177. return -1;
  178. }
  179. }
  180. if (ri->prefix[RIP_FILTER_IN])
  181. {
  182. if (prefix_list_apply (ri->prefix[RIP_FILTER_IN],
  183. (struct prefix *) p) == PREFIX_DENY)
  184. {
  185. if (IS_RIP_DEBUG_PACKET)
  186. zlog_debug ("%s/%d filtered by prefix-list in",
  187. inet_ntoa (p->prefix), p->prefixlen);
  188. return -1;
  189. }
  190. }
  191. /* All interface filter check. */
  192. dist = distribute_lookup (NULL);
  193. if (dist)
  194. {
  195. if (dist->list[DISTRIBUTE_IN])
  196. {
  197. alist = access_list_lookup (AFI_IP, dist->list[DISTRIBUTE_IN]);
  198. if (alist)
  199. {
  200. if (access_list_apply (alist,
  201. (struct prefix *) p) == FILTER_DENY)
  202. {
  203. if (IS_RIP_DEBUG_PACKET)
  204. zlog_debug ("%s/%d filtered by distribute in",
  205. inet_ntoa (p->prefix), p->prefixlen);
  206. return -1;
  207. }
  208. }
  209. }
  210. if (dist->prefix[DISTRIBUTE_IN])
  211. {
  212. plist = prefix_list_lookup (AFI_IP, dist->prefix[DISTRIBUTE_IN]);
  213. if (plist)
  214. {
  215. if (prefix_list_apply (plist,
  216. (struct prefix *) p) == PREFIX_DENY)
  217. {
  218. if (IS_RIP_DEBUG_PACKET)
  219. zlog_debug ("%s/%d filtered by prefix-list in",
  220. inet_ntoa (p->prefix), p->prefixlen);
  221. return -1;
  222. }
  223. }
  224. }
  225. }
  226. return 0;
  227. }
  228. static int
  229. rip_outgoing_filter (struct prefix_ipv4 *p, struct rip_interface *ri)
  230. {
  231. struct distribute *dist;
  232. struct access_list *alist;
  233. struct prefix_list *plist;
  234. if (ri->list[RIP_FILTER_OUT])
  235. {
  236. if (access_list_apply (ri->list[RIP_FILTER_OUT],
  237. (struct prefix *) p) == FILTER_DENY)
  238. {
  239. if (IS_RIP_DEBUG_PACKET)
  240. zlog_debug ("%s/%d is filtered by distribute out",
  241. inet_ntoa (p->prefix), p->prefixlen);
  242. return -1;
  243. }
  244. }
  245. if (ri->prefix[RIP_FILTER_OUT])
  246. {
  247. if (prefix_list_apply (ri->prefix[RIP_FILTER_OUT],
  248. (struct prefix *) p) == PREFIX_DENY)
  249. {
  250. if (IS_RIP_DEBUG_PACKET)
  251. zlog_debug ("%s/%d is filtered by prefix-list out",
  252. inet_ntoa (p->prefix), p->prefixlen);
  253. return -1;
  254. }
  255. }
  256. /* All interface filter check. */
  257. dist = distribute_lookup (NULL);
  258. if (dist)
  259. {
  260. if (dist->list[DISTRIBUTE_OUT])
  261. {
  262. alist = access_list_lookup (AFI_IP, dist->list[DISTRIBUTE_OUT]);
  263. if (alist)
  264. {
  265. if (access_list_apply (alist,
  266. (struct prefix *) p) == FILTER_DENY)
  267. {
  268. if (IS_RIP_DEBUG_PACKET)
  269. zlog_debug ("%s/%d filtered by distribute out",
  270. inet_ntoa (p->prefix), p->prefixlen);
  271. return -1;
  272. }
  273. }
  274. }
  275. if (dist->prefix[DISTRIBUTE_OUT])
  276. {
  277. plist = prefix_list_lookup (AFI_IP, dist->prefix[DISTRIBUTE_OUT]);
  278. if (plist)
  279. {
  280. if (prefix_list_apply (plist,
  281. (struct prefix *) p) == PREFIX_DENY)
  282. {
  283. if (IS_RIP_DEBUG_PACKET)
  284. zlog_debug ("%s/%d filtered by prefix-list out",
  285. inet_ntoa (p->prefix), p->prefixlen);
  286. return -1;
  287. }
  288. }
  289. }
  290. }
  291. return 0;
  292. }
  293. /* Check nexthop address validity. */
  294. static int
  295. rip_nexthop_check (struct in_addr *addr)
  296. {
  297. struct listnode *node;
  298. struct listnode *cnode;
  299. struct interface *ifp;
  300. struct connected *ifc;
  301. struct prefix *p;
  302. /* If nexthop address matches local configured address then it is
  303. invalid nexthop. */
  304. for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
  305. {
  306. for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, ifc))
  307. {
  308. p = ifc->address;
  309. if (p->family == AF_INET
  310. && IPV4_ADDR_SAME (&p->u.prefix4, addr))
  311. return -1;
  312. }
  313. }
  314. return 0;
  315. }
  316. /* RIP add route to routing table. */
  317. static void
  318. rip_rte_process (struct rte *rte, struct sockaddr_in *from,
  319. struct interface *ifp)
  320. {
  321. int ret;
  322. struct prefix_ipv4 p;
  323. struct route_node *rp;
  324. struct rip_info *rinfo, rinfotmp;
  325. struct rip_interface *ri;
  326. struct in_addr *nexthop;
  327. u_char oldmetric;
  328. int same = 0;
  329. int route_reuse = 0;
  330. unsigned char old_dist, new_dist;
  331. /* Make prefix structure. */
  332. memset (&p, 0, sizeof (struct prefix_ipv4));
  333. p.family = AF_INET;
  334. p.prefix = rte->prefix;
  335. p.prefixlen = ip_masklen (rte->mask);
  336. /* Make sure mask is applied. */
  337. apply_mask_ipv4 (&p);
  338. /* Apply input filters. */
  339. ri = ifp->info;
  340. ret = rip_incoming_filter (&p, ri);
  341. if (ret < 0)
  342. return;
  343. /* Modify entry according to the interface routemap. */
  344. if (ri->routemap[RIP_FILTER_IN])
  345. {
  346. int ret;
  347. struct rip_info newinfo;
  348. memset (&newinfo, 0, sizeof (newinfo));
  349. newinfo.type = ZEBRA_ROUTE_RIP;
  350. newinfo.sub_type = RIP_ROUTE_RTE;
  351. newinfo.nexthop = rte->nexthop;
  352. newinfo.from = from->sin_addr;
  353. newinfo.ifindex = ifp->ifindex;
  354. newinfo.metric = rte->metric;
  355. newinfo.metric_out = rte->metric; /* XXX */
  356. newinfo.tag = ntohs (rte->tag); /* XXX */
  357. /* The object should be of the type of rip_info */
  358. ret = route_map_apply (ri->routemap[RIP_FILTER_IN],
  359. (struct prefix *) &p, RMAP_RIP, &newinfo);
  360. if (ret == RMAP_DENYMATCH)
  361. {
  362. if (IS_RIP_DEBUG_PACKET)
  363. zlog_debug ("RIP %s/%d is filtered by route-map in",
  364. inet_ntoa (p.prefix), p.prefixlen);
  365. return;
  366. }
  367. /* Get back the object */
  368. rte->nexthop = newinfo.nexthop_out;
  369. rte->tag = htons (newinfo.tag_out); /* XXX */
  370. rte->metric = newinfo.metric_out; /* XXX: the routemap uses the metric_out field */
  371. }
  372. /* Once the entry has been validated, update the metric by
  373. adding the cost of the network on wich the message
  374. arrived. If the result is greater than infinity, use infinity
  375. (RFC2453 Sec. 3.9.2) */
  376. /* Zebra ripd can handle offset-list in. */
  377. ret = rip_offset_list_apply_in (&p, ifp, &rte->metric);
  378. /* If offset-list does not modify the metric use interface's
  379. metric. */
  380. if (!ret)
  381. rte->metric += ifp->metric;
  382. if (rte->metric > RIP_METRIC_INFINITY)
  383. rte->metric = RIP_METRIC_INFINITY;
  384. /* Set nexthop pointer. */
  385. if (rte->nexthop.s_addr == 0)
  386. nexthop = &from->sin_addr;
  387. else
  388. nexthop = &rte->nexthop;
  389. /* Check if nexthop address is myself, then do nothing. */
  390. if (rip_nexthop_check (nexthop) < 0)
  391. {
  392. if (IS_RIP_DEBUG_PACKET)
  393. zlog_debug ("Nexthop address %s is myself", inet_ntoa (*nexthop));
  394. return;
  395. }
  396. /* Get index for the prefix. */
  397. rp = route_node_get (rip->table, (struct prefix *) &p);
  398. /* Check to see whether there is already RIP route on the table. */
  399. rinfo = rp->info;
  400. if (rinfo)
  401. {
  402. /* Local static route. */
  403. if (rinfo->type == ZEBRA_ROUTE_RIP
  404. && ((rinfo->sub_type == RIP_ROUTE_STATIC) ||
  405. (rinfo->sub_type == RIP_ROUTE_DEFAULT))
  406. && rinfo->metric != RIP_METRIC_INFINITY)
  407. {
  408. route_unlock_node (rp);
  409. return;
  410. }
  411. /* Redistributed route check. */
  412. if (rinfo->type != ZEBRA_ROUTE_RIP
  413. && rinfo->metric != RIP_METRIC_INFINITY)
  414. {
  415. /* Fill in a minimaly temporary rip_info structure, for a future
  416. rip_distance_apply() use) */
  417. memset (&rinfotmp, 0, sizeof (rinfotmp));
  418. IPV4_ADDR_COPY (&rinfotmp.from, &from->sin_addr);
  419. rinfotmp.rp = rinfo->rp;
  420. new_dist = rip_distance_apply (&rinfotmp);
  421. new_dist = new_dist ? new_dist : ZEBRA_RIP_DISTANCE_DEFAULT;
  422. old_dist = rinfo->distance;
  423. old_dist = old_dist ? old_dist : ZEBRA_RIP_DISTANCE_DEFAULT;
  424. /* If imported route does not have STRICT precedence,
  425. mark it as a ghost */
  426. if (new_dist > old_dist
  427. || rte->metric == RIP_METRIC_INFINITY)
  428. {
  429. route_unlock_node (rp);
  430. return;
  431. }
  432. else
  433. {
  434. RIP_TIMER_OFF (rinfo->t_timeout);
  435. RIP_TIMER_OFF (rinfo->t_garbage_collect);
  436. rp->info = NULL;
  437. if (rip_route_rte (rinfo))
  438. rip_zebra_ipv4_delete ((struct prefix_ipv4 *)&rp->p,
  439. &rinfo->nexthop, rinfo->metric);
  440. rip_info_free (rinfo);
  441. rinfo = NULL;
  442. route_reuse = 1;
  443. }
  444. }
  445. }
  446. if (!rinfo)
  447. {
  448. /* Now, check to see whether there is already an explicit route
  449. for the destination prefix. If there is no such route, add
  450. this route to the routing table, unless the metric is
  451. infinity (there is no point in adding a route which
  452. unusable). */
  453. if (rte->metric != RIP_METRIC_INFINITY)
  454. {
  455. rinfo = rip_info_new ();
  456. /* - Setting the destination prefix and length to those in
  457. the RTE. */
  458. rinfo->rp = rp;
  459. /* - Setting the metric to the newly calculated metric (as
  460. described above). */
  461. rinfo->metric = rte->metric;
  462. rinfo->tag = ntohs (rte->tag);
  463. /* - Set the next hop address to be the address of the router
  464. from which the datagram came or the next hop address
  465. specified by a next hop RTE. */
  466. IPV4_ADDR_COPY (&rinfo->nexthop, nexthop);
  467. IPV4_ADDR_COPY (&rinfo->from, &from->sin_addr);
  468. rinfo->ifindex = ifp->ifindex;
  469. /* - Initialize the timeout for the route. If the
  470. garbage-collection timer is running for this route, stop it
  471. (see section 2.3 for a discussion of the timers). */
  472. rip_timeout_update (rinfo);
  473. /* - Set the route change flag. */
  474. rinfo->flags |= RIP_RTF_CHANGED;
  475. /* - Signal the output process to trigger an update (see section
  476. 2.5). */
  477. rip_event (RIP_TRIGGERED_UPDATE, 0);
  478. /* Finally, route goes into the kernel. */
  479. rinfo->type = ZEBRA_ROUTE_RIP;
  480. rinfo->sub_type = RIP_ROUTE_RTE;
  481. /* Set distance value. */
  482. rinfo->distance = rip_distance_apply (rinfo);
  483. rp->info = rinfo;
  484. rip_zebra_ipv4_add (&p, &rinfo->nexthop, rinfo->metric,
  485. rinfo->distance);
  486. rinfo->flags |= RIP_RTF_FIB;
  487. }
  488. /* Unlock temporary lock, i.e. same behaviour */
  489. if (route_reuse)
  490. route_unlock_node (rp);
  491. }
  492. else
  493. {
  494. /* Route is there but we are not sure the route is RIP or not. */
  495. rinfo = rp->info;
  496. /* If there is an existing route, compare the next hop address
  497. to the address of the router from which the datagram came.
  498. If this datagram is from the same router as the existing
  499. route, reinitialize the timeout. */
  500. same = (IPV4_ADDR_SAME (&rinfo->from, &from->sin_addr)
  501. && (rinfo->ifindex == ifp->ifindex));
  502. if (same)
  503. rip_timeout_update (rinfo);
  504. /* Fill in a minimaly temporary rip_info structure, for a future
  505. rip_distance_apply() use) */
  506. memset (&rinfotmp, 0, sizeof (rinfotmp));
  507. IPV4_ADDR_COPY (&rinfotmp.from, &from->sin_addr);
  508. rinfotmp.rp = rinfo->rp;
  509. /* Next, compare the metrics. If the datagram is from the same
  510. router as the existing route, and the new metric is different
  511. than the old one; or, if the new metric is lower than the old
  512. one, or if the tag has been changed; or if there is a route
  513. with a lower administrave distance; or an update of the
  514. distance on the actual route; do the following actions: */
  515. if ((same && rinfo->metric != rte->metric)
  516. || (rte->metric < rinfo->metric)
  517. || ((same)
  518. && (rinfo->metric == rte->metric)
  519. && ntohs (rte->tag) != rinfo->tag)
  520. || (rinfo->distance > rip_distance_apply (&rinfotmp))
  521. || ((rinfo->distance != rip_distance_apply (rinfo)) && same))
  522. {
  523. /* - Adopt the route from the datagram. That is, put the
  524. new metric in, and adjust the next hop address (if
  525. necessary). */
  526. oldmetric = rinfo->metric;
  527. rinfo->metric = rte->metric;
  528. rinfo->tag = ntohs (rte->tag);
  529. IPV4_ADDR_COPY (&rinfo->from, &from->sin_addr);
  530. rinfo->ifindex = ifp->ifindex;
  531. rinfo->distance = rip_distance_apply (rinfo);
  532. /* Should a new route to this network be established
  533. while the garbage-collection timer is running, the
  534. new route will replace the one that is about to be
  535. deleted. In this case the garbage-collection timer
  536. must be cleared. */
  537. if (oldmetric == RIP_METRIC_INFINITY &&
  538. rinfo->metric < RIP_METRIC_INFINITY)
  539. {
  540. rinfo->type = ZEBRA_ROUTE_RIP;
  541. rinfo->sub_type = RIP_ROUTE_RTE;
  542. RIP_TIMER_OFF (rinfo->t_garbage_collect);
  543. if (!IPV4_ADDR_SAME (&rinfo->nexthop, nexthop))
  544. IPV4_ADDR_COPY (&rinfo->nexthop, nexthop);
  545. rip_zebra_ipv4_add (&p, nexthop, rinfo->metric,
  546. rinfo->distance);
  547. rinfo->flags |= RIP_RTF_FIB;
  548. }
  549. /* Update nexthop and/or metric value. */
  550. if (oldmetric != RIP_METRIC_INFINITY)
  551. {
  552. rip_zebra_ipv4_delete (&p, &rinfo->nexthop, oldmetric);
  553. rip_zebra_ipv4_add (&p, nexthop, rinfo->metric,
  554. rinfo->distance);
  555. rinfo->flags |= RIP_RTF_FIB;
  556. if (!IPV4_ADDR_SAME (&rinfo->nexthop, nexthop))
  557. IPV4_ADDR_COPY (&rinfo->nexthop, nexthop);
  558. }
  559. /* - Set the route change flag and signal the output process
  560. to trigger an update. */
  561. rinfo->flags |= RIP_RTF_CHANGED;
  562. rip_event (RIP_TRIGGERED_UPDATE, 0);
  563. /* - If the new metric is infinity, start the deletion
  564. process (described above); */
  565. if (rinfo->metric == RIP_METRIC_INFINITY)
  566. {
  567. /* If the new metric is infinity, the deletion process
  568. begins for the route, which is no longer used for
  569. routing packets. Note that the deletion process is
  570. started only when the metric is first set to
  571. infinity. If the metric was already infinity, then a
  572. new deletion process is not started. */
  573. if (oldmetric != RIP_METRIC_INFINITY)
  574. {
  575. /* - The garbage-collection timer is set for 120 seconds. */
  576. RIP_TIMER_ON (rinfo->t_garbage_collect,
  577. rip_garbage_collect, rip->garbage_time);
  578. RIP_TIMER_OFF (rinfo->t_timeout);
  579. /* - The metric for the route is set to 16
  580. (infinity). This causes the route to be removed
  581. from service. */
  582. rip_zebra_ipv4_delete (&p, &rinfo->nexthop, oldmetric);
  583. rinfo->flags &= ~RIP_RTF_FIB;
  584. /* - The route change flag is to indicate that this
  585. entry has been changed. */
  586. /* - The output process is signalled to trigger a
  587. response. */
  588. ; /* Above processes are already done previously. */
  589. }
  590. }
  591. else
  592. {
  593. /* otherwise, re-initialize the timeout. */
  594. rip_timeout_update (rinfo);
  595. }
  596. }
  597. /* Unlock tempolary lock of the route. */
  598. route_unlock_node (rp);
  599. }
  600. }
  601. /* Dump RIP packet */
  602. static void
  603. rip_packet_dump (struct rip_packet *packet, int size, const char *sndrcv)
  604. {
  605. caddr_t lim;
  606. struct rte *rte;
  607. const char *command_str;
  608. char pbuf[BUFSIZ], nbuf[BUFSIZ];
  609. u_char netmask = 0;
  610. u_char *p;
  611. /* Set command string. */
  612. if (packet->command > 0 && packet->command < RIP_COMMAND_MAX)
  613. command_str = lookup (rip_msg, packet->command);
  614. else
  615. command_str = "unknown";
  616. /* Dump packet header. */
  617. zlog_debug ("%s %s version %d packet size %d",
  618. sndrcv, command_str, packet->version, size);
  619. /* Dump each routing table entry. */
  620. rte = packet->rte;
  621. for (lim = (caddr_t) packet + size; (caddr_t) rte < lim; rte++)
  622. {
  623. if (packet->version == RIPv2)
  624. {
  625. netmask = ip_masklen (rte->mask);
  626. if (rte->family == htons (RIP_FAMILY_AUTH))
  627. {
  628. if (rte->tag == htons (RIP_AUTH_SIMPLE_PASSWORD))
  629. {
  630. p = (u_char *)&rte->prefix;
  631. zlog_debug (" family 0x%X type %d auth string: %s",
  632. ntohs (rte->family), ntohs (rte->tag), p);
  633. }
  634. else if (rte->tag == htons (RIP_AUTH_MD5))
  635. {
  636. struct rip_md5_info *md5;
  637. md5 = (struct rip_md5_info *) &packet->rte;
  638. zlog_debug (" family 0x%X type %d (MD5 authentication)",
  639. ntohs (md5->family), ntohs (md5->type));
  640. zlog_debug (" RIP-2 packet len %d Key ID %d"
  641. " Auth Data len %d",
  642. ntohs (md5->packet_len), md5->keyid,
  643. md5->auth_len);
  644. zlog_debug (" Sequence Number %ld",
  645. (u_long) ntohl (md5->sequence));
  646. }
  647. else if (rte->tag == htons (RIP_AUTH_DATA))
  648. {
  649. p = (u_char *)&rte->prefix;
  650. zlog_debug (" family 0x%X type %d (MD5 data)",
  651. ntohs (rte->family), ntohs (rte->tag));
  652. zlog_debug (" MD5: %02X%02X%02X%02X%02X%02X%02X%02X"
  653. "%02X%02X%02X%02X%02X%02X%02X",
  654. p[0], p[1], p[2], p[3], p[4], p[5], p[6],
  655. p[7], p[9], p[10], p[11], p[12], p[13],
  656. p[14], p[15]);
  657. }
  658. else
  659. {
  660. zlog_debug (" family 0x%X type %d (Unknown auth type)",
  661. ntohs (rte->family), ntohs (rte->tag));
  662. }
  663. }
  664. else
  665. zlog_debug (" %s/%d -> %s family %d tag %d metric %ld",
  666. inet_ntop (AF_INET, &rte->prefix, pbuf, BUFSIZ),
  667. netmask, inet_ntop (AF_INET, &rte->nexthop, nbuf,
  668. BUFSIZ), ntohs (rte->family),
  669. ntohs (rte->tag), (u_long) ntohl (rte->metric));
  670. }
  671. else
  672. {
  673. zlog_debug (" %s family %d tag %d metric %ld",
  674. inet_ntop (AF_INET, &rte->prefix, pbuf, BUFSIZ),
  675. ntohs (rte->family), ntohs (rte->tag),
  676. (u_long)ntohl (rte->metric));
  677. }
  678. }
  679. }
  680. /* Check if the destination address is valid (unicast; not net 0
  681. or 127) (RFC2453 Section 3.9.2 - Page 26). But we don't
  682. check net 0 because we accept default route. */
  683. static int
  684. rip_destination_check (struct in_addr addr)
  685. {
  686. u_int32_t destination;
  687. /* Convert to host byte order. */
  688. destination = ntohl (addr.s_addr);
  689. if (IPV4_NET127 (destination))
  690. return 0;
  691. /* Net 0 may match to the default route. */
  692. if (IPV4_NET0 (destination) && destination != 0)
  693. return 0;
  694. /* Unicast address must belong to class A, B, C. */
  695. if (IN_CLASSA (destination))
  696. return 1;
  697. if (IN_CLASSB (destination))
  698. return 1;
  699. if (IN_CLASSC (destination))
  700. return 1;
  701. return 0;
  702. }
  703. /* RIP version 2 authentication. */
  704. static int
  705. rip_auth_simple_password (struct rte *rte, struct sockaddr_in *from,
  706. struct interface *ifp)
  707. {
  708. struct rip_interface *ri;
  709. char *auth_str;
  710. if (IS_RIP_DEBUG_EVENT)
  711. zlog_debug ("RIPv2 simple password authentication from %s",
  712. inet_ntoa (from->sin_addr));
  713. ri = ifp->info;
  714. if (ri->auth_type != RIP_AUTH_SIMPLE_PASSWORD
  715. || rte->tag != htons(RIP_AUTH_SIMPLE_PASSWORD))
  716. return 0;
  717. /* Simple password authentication. */
  718. if (ri->auth_str)
  719. {
  720. auth_str = (char *) &rte->prefix;
  721. if (strncmp (auth_str, ri->auth_str, 16) == 0)
  722. return 1;
  723. }
  724. if (ri->key_chain)
  725. {
  726. struct keychain *keychain;
  727. struct key *key;
  728. keychain = keychain_lookup (ri->key_chain);
  729. if (keychain == NULL)
  730. return 0;
  731. key = key_match_for_accept (keychain, (char *) &rte->prefix);
  732. if (key)
  733. return 1;
  734. }
  735. return 0;
  736. }
  737. /* RIP version 2 authentication with MD5. */
  738. static int
  739. rip_auth_md5 (struct rip_packet *packet, struct sockaddr_in *from,
  740. int length, struct interface *ifp)
  741. {
  742. struct rip_interface *ri;
  743. struct rip_md5_info *md5;
  744. struct rip_md5_data *md5data;
  745. struct keychain *keychain;
  746. struct key *key;
  747. MD5_CTX ctx;
  748. u_char pdigest[RIP_AUTH_MD5_SIZE];
  749. u_char digest[RIP_AUTH_MD5_SIZE];
  750. u_int16_t packet_len;
  751. char *auth_str = NULL;
  752. if (IS_RIP_DEBUG_EVENT)
  753. zlog_debug ("RIPv2 MD5 authentication from %s",
  754. inet_ntoa (from->sin_addr));
  755. ri = ifp->info;
  756. md5 = (struct rip_md5_info *) &packet->rte;
  757. /* Check auth type. */
  758. if (ri->auth_type != RIP_AUTH_MD5 || md5->type != htons(RIP_AUTH_MD5))
  759. return 0;
  760. /* If the authentication length is less than 16, then it must be wrong for
  761. * any interpretation of rfc2082. Some implementations also interpret
  762. * this as RIP_HEADER_SIZE+ RIP_AUTH_MD5_SIZE, aka RIP_AUTH_MD5_COMPAT_SIZE.
  763. */
  764. if ( !((md5->auth_len == RIP_AUTH_MD5_SIZE)
  765. || (md5->auth_len == RIP_AUTH_MD5_COMPAT_SIZE)))
  766. {
  767. if (IS_RIP_DEBUG_EVENT)
  768. zlog_debug ("RIPv2 MD5 authentication, strange authentication "
  769. "length field %d", md5->auth_len);
  770. return 0;
  771. }
  772. /* grab and verify check packet length */
  773. packet_len = ntohs (md5->packet_len);
  774. if (packet_len > (length - RIP_HEADER_SIZE - RIP_AUTH_MD5_SIZE))
  775. {
  776. if (IS_RIP_DEBUG_EVENT)
  777. zlog_debug ("RIPv2 MD5 authentication, packet length field %d "
  778. "greater than received length %d!",
  779. md5->packet_len, length);
  780. return 0;
  781. }
  782. /* retrieve authentication data */
  783. md5data = (struct rip_md5_data *) (((u_char *) packet) + packet_len);
  784. if (ri->key_chain)
  785. {
  786. keychain = keychain_lookup (ri->key_chain);
  787. if (keychain == NULL)
  788. return 0;
  789. key = key_lookup_for_accept (keychain, md5->keyid);
  790. if (key == NULL)
  791. return 0;
  792. auth_str = key->string;
  793. }
  794. if (ri->auth_str)
  795. auth_str = ri->auth_str;
  796. if (! auth_str)
  797. return 0;
  798. /* MD5 digest authentication. */
  799. /* Save digest to pdigest. */
  800. memcpy (pdigest, md5data->digest, RIP_AUTH_MD5_SIZE);
  801. /* Overwrite digest by my secret. */
  802. memset (md5data->digest, 0, RIP_AUTH_MD5_SIZE);
  803. strncpy ((char *)md5data->digest, auth_str, RIP_AUTH_MD5_SIZE);
  804. memset (&ctx, 0, sizeof(ctx));
  805. MD5Init(&ctx);
  806. MD5Update(&ctx, packet, packet_len + md5->auth_len);
  807. MD5Final(digest, &ctx);
  808. if (memcmp (pdigest, digest, RIP_AUTH_MD5_SIZE) == 0)
  809. return packet_len;
  810. else
  811. return 0;
  812. }
  813. /* Pick correct auth string for sends, prepare auth_str buffer for use.
  814. * (left justified and padded).
  815. *
  816. * presumes one of ri or key is valid, and that the auth strings they point
  817. * to are nul terminated. If neither are present, auth_str will be fully
  818. * zero padded.
  819. *
  820. */
  821. static void
  822. rip_auth_prepare_str_send (struct rip_interface *ri, struct key *key,
  823. char *auth_str, int len)
  824. {
  825. assert (ri || key);
  826. memset (auth_str, 0, len);
  827. if (key && key->string)
  828. strncpy (auth_str, key->string, len);
  829. else if (ri->auth_str)
  830. strncpy (auth_str, ri->auth_str, len);
  831. return;
  832. }
  833. /* Write RIPv2 simple password authentication information
  834. *
  835. * auth_str is presumed to be 2 bytes and correctly prepared
  836. * (left justified and zero padded).
  837. */
  838. static void
  839. rip_auth_simple_write (struct stream *s, char *auth_str, int len)
  840. {
  841. assert (s && len == RIP_AUTH_SIMPLE_SIZE);
  842. stream_putw (s, RIP_FAMILY_AUTH);
  843. stream_putw (s, RIP_AUTH_SIMPLE_PASSWORD);
  844. stream_put (s, auth_str, RIP_AUTH_SIMPLE_SIZE);
  845. return;
  846. }
  847. /* write RIPv2 MD5 "authentication header"
  848. * (uses the auth key data field)
  849. *
  850. * Digest offset field is set to 0.
  851. *
  852. * returns: offset of the digest offset field, which must be set when
  853. * length to the auth-data MD5 digest is known.
  854. */
  855. static size_t
  856. rip_auth_md5_ah_write (struct stream *s, struct rip_interface *ri,
  857. struct key *key)
  858. {
  859. size_t len = 0;
  860. assert (s && ri && ri->auth_type == RIP_AUTH_MD5);
  861. /* MD5 authentication. */
  862. stream_putw (s, RIP_FAMILY_AUTH);
  863. stream_putw (s, RIP_AUTH_MD5);
  864. /* MD5 AH digest offset field.
  865. *
  866. * Set to placeholder value here, to true value when RIP-2 Packet length
  867. * is known. Actual value is set in .....().
  868. */
  869. len = stream_get_endp(s);
  870. stream_putw (s, 0);
  871. /* Key ID. */
  872. if (key)
  873. stream_putc (s, key->index % 256);
  874. else
  875. stream_putc (s, 1);
  876. /* Auth Data Len. Set 16 for MD5 authentication data. Older ripds
  877. * however expect RIP_HEADER_SIZE + RIP_AUTH_MD5_SIZE so we allow for this
  878. * to be configurable.
  879. */
  880. stream_putc (s, ri->md5_auth_len);
  881. /* Sequence Number (non-decreasing). */
  882. /* RFC2080: The value used in the sequence number is
  883. arbitrary, but two suggestions are the time of the
  884. message's creation or a simple message counter. */
  885. stream_putl (s, time (NULL));
  886. /* Reserved field must be zero. */
  887. stream_putl (s, 0);
  888. stream_putl (s, 0);
  889. return len;
  890. }
  891. /* If authentication is in used, write the appropriate header
  892. * returns stream offset to which length must later be written
  893. * or 0 if this is not required
  894. */
  895. static size_t
  896. rip_auth_header_write (struct stream *s, struct rip_interface *ri,
  897. struct key *key, char *auth_str, int len)
  898. {
  899. assert (ri->auth_type != RIP_NO_AUTH);
  900. switch (ri->auth_type)
  901. {
  902. case RIP_AUTH_SIMPLE_PASSWORD:
  903. rip_auth_prepare_str_send (ri, key, auth_str, len);
  904. rip_auth_simple_write (s, auth_str, len);
  905. return 0;
  906. case RIP_AUTH_MD5:
  907. return rip_auth_md5_ah_write (s, ri, key);
  908. }
  909. assert (1);
  910. }
  911. /* Write RIPv2 MD5 authentication data trailer */
  912. static void
  913. rip_auth_md5_set (struct stream *s, struct rip_interface *ri, size_t doff,
  914. char *auth_str, int authlen)
  915. {
  916. unsigned long len;
  917. MD5_CTX ctx;
  918. unsigned char digest[RIP_AUTH_MD5_SIZE];
  919. /* Make it sure this interface is configured as MD5
  920. authentication. */
  921. assert ((ri->auth_type == RIP_AUTH_MD5) && (authlen == RIP_AUTH_MD5_SIZE));
  922. assert (doff > 0);
  923. /* Get packet length. */
  924. len = stream_get_endp(s);
  925. /* Check packet length. */
  926. if (len < (RIP_HEADER_SIZE + RIP_RTE_SIZE))
  927. {
  928. zlog_err ("rip_auth_md5_set(): packet length %ld is less than minimum length.", len);
  929. return;
  930. }
  931. /* Set the digest offset length in the header */
  932. stream_putw_at (s, doff, len);
  933. /* Set authentication data. */
  934. stream_putw (s, RIP_FAMILY_AUTH);
  935. stream_putw (s, RIP_AUTH_DATA);
  936. /* Generate a digest for the RIP packet. */
  937. memset(&ctx, 0, sizeof(ctx));
  938. MD5Init(&ctx);
  939. MD5Update(&ctx, s->data, s->endp);
  940. MD5Update(&ctx, auth_str, RIP_AUTH_MD5_SIZE);
  941. MD5Final(digest, &ctx);
  942. /* Copy the digest to the packet. */
  943. stream_write (s, digest, RIP_AUTH_MD5_SIZE);
  944. }
  945. /* RIP routing information. */
  946. static void
  947. rip_response_process (struct rip_packet *packet, int size,
  948. struct sockaddr_in *from, struct connected *ifc)
  949. {
  950. caddr_t lim;
  951. struct rte *rte;
  952. struct prefix_ipv4 ifaddr;
  953. struct prefix_ipv4 ifaddrclass;
  954. int subnetted;
  955. /* We don't know yet. */
  956. subnetted = -1;
  957. /* The Response must be ignored if it is not from the RIP
  958. port. (RFC2453 - Sec. 3.9.2)*/
  959. if (from->sin_port != htons(RIP_PORT_DEFAULT))
  960. {
  961. zlog_info ("response doesn't come from RIP port: %d",
  962. from->sin_port);
  963. rip_peer_bad_packet (from);
  964. return;
  965. }
  966. /* The datagram's IPv4 source address should be checked to see
  967. whether the datagram is from a valid neighbor; the source of the
  968. datagram must be on a directly connected network */
  969. if (! if_valid_neighbor (from->sin_addr))
  970. {
  971. zlog_info ("This datagram doesn't came from a valid neighbor: %s",
  972. inet_ntoa (from->sin_addr));
  973. rip_peer_bad_packet (from);
  974. return;
  975. }
  976. /* It is also worth checking to see whether the response is from one
  977. of the router's own addresses. */
  978. ; /* Alredy done in rip_read () */
  979. /* Update RIP peer. */
  980. rip_peer_update (from, packet->version);
  981. /* Set RTE pointer. */
  982. rte = packet->rte;
  983. for (lim = (caddr_t) packet + size; (caddr_t) rte < lim; rte++)
  984. {
  985. /* RIPv2 authentication check. */
  986. /* If the Address Family Identifier of the first (and only the
  987. first) entry in the message is 0xFFFF, then the remainder of
  988. the entry contains the authentication. */
  989. /* If the packet gets here it means authentication enabled */
  990. /* Check is done in rip_read(). So, just skipping it */
  991. if (packet->version == RIPv2 &&
  992. rte == packet->rte &&
  993. rte->family == htons(RIP_FAMILY_AUTH))
  994. continue;
  995. if (rte->family != htons(AF_INET))
  996. {
  997. /* Address family check. RIP only supports AF_INET. */
  998. zlog_info ("Unsupported family %d from %s.",
  999. ntohs (rte->family), inet_ntoa (from->sin_addr));
  1000. continue;
  1001. }
  1002. /* - is the destination address valid (e.g., unicast; not net 0
  1003. or 127) */
  1004. if (! rip_destination_check (rte->prefix))
  1005. {
  1006. zlog_info ("Network is net 0 or net 127 or it is not unicast network");
  1007. rip_peer_bad_route (from);
  1008. continue;
  1009. }
  1010. /* Convert metric value to host byte order. */
  1011. rte->metric = ntohl (rte->metric);
  1012. /* - is the metric valid (i.e., between 1 and 16, inclusive) */
  1013. if (! (rte->metric >= 1 && rte->metric <= 16))
  1014. {
  1015. zlog_info ("Route's metric is not in the 1-16 range.");
  1016. rip_peer_bad_route (from);
  1017. continue;
  1018. }
  1019. /* RIPv1 does not have nexthop value. */
  1020. if (packet->version == RIPv1 && rte->nexthop.s_addr != 0)
  1021. {
  1022. zlog_info ("RIPv1 packet with nexthop value %s",
  1023. inet_ntoa (rte->nexthop));
  1024. rip_peer_bad_route (from);
  1025. continue;
  1026. }
  1027. /* That is, if the provided information is ignored, a possibly
  1028. sub-optimal, but absolutely valid, route may be taken. If
  1029. the received Next Hop is not directly reachable, it should be
  1030. treated as 0.0.0.0. */
  1031. if (packet->version == RIPv2 && rte->nexthop.s_addr != 0)
  1032. {
  1033. u_int32_t addrval;
  1034. /* Multicast address check. */
  1035. addrval = ntohl (rte->nexthop.s_addr);
  1036. if (IN_CLASSD (addrval))
  1037. {
  1038. zlog_info ("Nexthop %s is multicast address, skip this rte",
  1039. inet_ntoa (rte->nexthop));
  1040. continue;
  1041. }
  1042. if (! if_lookup_address (rte->nexthop))
  1043. {
  1044. struct route_node *rn;
  1045. struct rip_info *rinfo;
  1046. rn = route_node_match_ipv4 (rip->table, &rte->nexthop);
  1047. if (rn)
  1048. {
  1049. rinfo = rn->info;
  1050. if (rinfo->type == ZEBRA_ROUTE_RIP
  1051. && rinfo->sub_type == RIP_ROUTE_RTE)
  1052. {
  1053. if (IS_RIP_DEBUG_EVENT)
  1054. zlog_debug ("Next hop %s is on RIP network. Set nexthop to the packet's originator", inet_ntoa (rte->nexthop));
  1055. rte->nexthop = rinfo->from;
  1056. }
  1057. else
  1058. {
  1059. if (IS_RIP_DEBUG_EVENT)
  1060. zlog_debug ("Next hop %s is not directly reachable. Treat it as 0.0.0.0", inet_ntoa (rte->nexthop));
  1061. rte->nexthop.s_addr = 0;
  1062. }
  1063. route_unlock_node (rn);
  1064. }
  1065. else
  1066. {
  1067. if (IS_RIP_DEBUG_EVENT)
  1068. zlog_debug ("Next hop %s is not directly reachable. Treat it as 0.0.0.0", inet_ntoa (rte->nexthop));
  1069. rte->nexthop.s_addr = 0;
  1070. }
  1071. }
  1072. }
  1073. /* For RIPv1, there won't be a valid netmask.
  1074. This is a best guess at the masks. If everyone was using old
  1075. Ciscos before the 'ip subnet zero' option, it would be almost
  1076. right too :-)
  1077. Cisco summarize ripv1 advertisments to the classful boundary
  1078. (/16 for class B's) except when the RIP packet does to inside
  1079. the classful network in question. */
  1080. if ((packet->version == RIPv1 && rte->prefix.s_addr != 0)
  1081. || (packet->version == RIPv2
  1082. && (rte->prefix.s_addr != 0 && rte->mask.s_addr == 0)))
  1083. {
  1084. u_int32_t destination;
  1085. if (subnetted == -1)
  1086. {
  1087. memcpy (&ifaddr, ifc->address, sizeof (struct prefix_ipv4));
  1088. memcpy (&ifaddrclass, &ifaddr, sizeof (struct prefix_ipv4));
  1089. apply_classful_mask_ipv4 (&ifaddrclass);
  1090. subnetted = 0;
  1091. if (ifaddr.prefixlen > ifaddrclass.prefixlen)
  1092. subnetted = 1;
  1093. }
  1094. destination = ntohl (rte->prefix.s_addr);
  1095. if (IN_CLASSA (destination))
  1096. masklen2ip (8, &rte->mask);
  1097. else if (IN_CLASSB (destination))
  1098. masklen2ip (16, &rte->mask);
  1099. else if (IN_CLASSC (destination))
  1100. masklen2ip (24, &rte->mask);
  1101. if (subnetted == 1)
  1102. masklen2ip (ifaddrclass.prefixlen,
  1103. (struct in_addr *) &destination);
  1104. if ((subnetted == 1) && ((rte->prefix.s_addr & destination) ==
  1105. ifaddrclass.prefix.s_addr))
  1106. {
  1107. masklen2ip (ifaddr.prefixlen, &rte->mask);
  1108. if ((rte->prefix.s_addr & rte->mask.s_addr) != rte->prefix.s_addr)
  1109. masklen2ip (32, &rte->mask);
  1110. if (IS_RIP_DEBUG_EVENT)
  1111. zlog_debug ("Subnetted route %s", inet_ntoa (rte->prefix));
  1112. }
  1113. else
  1114. {
  1115. if ((rte->prefix.s_addr & rte->mask.s_addr) != rte->prefix.s_addr)
  1116. continue;
  1117. }
  1118. if (IS_RIP_DEBUG_EVENT)
  1119. {
  1120. zlog_debug ("Resultant route %s", inet_ntoa (rte->prefix));
  1121. zlog_debug ("Resultant mask %s", inet_ntoa (rte->mask));
  1122. }
  1123. }
  1124. /* In case of RIPv2, if prefix in RTE is not netmask applied one
  1125. ignore the entry. */
  1126. if ((packet->version == RIPv2)
  1127. && (rte->mask.s_addr != 0)
  1128. && ((rte->prefix.s_addr & rte->mask.s_addr) != rte->prefix.s_addr))
  1129. {
  1130. zlog_warn ("RIPv2 address %s is not mask /%d applied one",
  1131. inet_ntoa (rte->prefix), ip_masklen (rte->mask));
  1132. rip_peer_bad_route (from);
  1133. continue;
  1134. }
  1135. /* Default route's netmask is ignored. */
  1136. if (packet->version == RIPv2
  1137. && (rte->prefix.s_addr == 0)
  1138. && (rte->mask.s_addr != 0))
  1139. {
  1140. if (IS_RIP_DEBUG_EVENT)
  1141. zlog_debug ("Default route with non-zero netmask. Set zero to netmask");
  1142. rte->mask.s_addr = 0;
  1143. }
  1144. /* Routing table updates. */
  1145. rip_rte_process (rte, from, ifc->ifp);
  1146. }
  1147. }
  1148. /* Make socket for RIP protocol. */
  1149. static int
  1150. rip_create_socket (struct sockaddr_in *from)
  1151. {
  1152. int ret;
  1153. int sock;
  1154. struct sockaddr_in addr;
  1155. memset (&addr, 0, sizeof (struct sockaddr_in));
  1156. if (!from)
  1157. {
  1158. addr.sin_family = AF_INET;
  1159. addr.sin_addr.s_addr = INADDR_ANY;
  1160. #ifdef HAVE_SINLEN
  1161. addr.sin_len = sizeof (struct sockaddr_in);
  1162. #endif /* HAVE_SINLEN */
  1163. } else {
  1164. memcpy(&addr, from, sizeof(addr));
  1165. }
  1166. /* sending port must always be the RIP port */
  1167. addr.sin_port = htons (RIP_PORT_DEFAULT);
  1168. /* Make datagram socket. */
  1169. sock = socket (AF_INET, SOCK_DGRAM, 0);
  1170. if (sock < 0)
  1171. {
  1172. zlog_err("Cannot create UDP socket: %s", safe_strerror(errno));
  1173. exit (1);
  1174. }
  1175. sockopt_broadcast (sock);
  1176. sockopt_reuseaddr (sock);
  1177. sockopt_reuseport (sock);
  1178. #ifdef RIP_RECVMSG
  1179. setsockopt_pktinfo (sock);
  1180. #endif /* RIP_RECVMSG */
  1181. if (ripd_privs.change (ZPRIVS_RAISE))
  1182. zlog_err ("rip_create_socket: could not raise privs");
  1183. setsockopt_so_recvbuf (sock, RIP_UDP_RCV_BUF);
  1184. if ( (ret = bind (sock, (struct sockaddr *) & addr, sizeof (addr))) < 0)
  1185. {
  1186. int save_errno = errno;
  1187. if (ripd_privs.change (ZPRIVS_LOWER))
  1188. zlog_err ("rip_create_socket: could not lower privs");
  1189. zlog_err("%s: Can't bind socket %d to %s port %d: %s", __func__,
  1190. sock, inet_ntoa(addr.sin_addr),
  1191. (int) ntohs(addr.sin_port),
  1192. safe_strerror(save_errno));
  1193. close (sock);
  1194. return ret;
  1195. }
  1196. if (ripd_privs.change (ZPRIVS_LOWER))
  1197. zlog_err ("rip_create_socket: could not lower privs");
  1198. return sock;
  1199. }
  1200. /* RIP packet send to destination address, on interface denoted by
  1201. * by connected argument. NULL to argument denotes destination should be
  1202. * should be RIP multicast group
  1203. */
  1204. static int
  1205. rip_send_packet (u_char * buf, int size, struct sockaddr_in *to,
  1206. struct connected *ifc)
  1207. {
  1208. int ret, send_sock;
  1209. struct sockaddr_in sin;
  1210. assert (ifc != NULL);
  1211. if (IS_RIP_DEBUG_PACKET)
  1212. {
  1213. #define ADDRESS_SIZE 20
  1214. char dst[ADDRESS_SIZE];
  1215. dst[ADDRESS_SIZE - 1] = '\0';
  1216. if (to)
  1217. {
  1218. strncpy (dst, inet_ntoa(to->sin_addr), ADDRESS_SIZE - 1);
  1219. }
  1220. else
  1221. {
  1222. sin.sin_addr.s_addr = htonl (INADDR_RIP_GROUP);
  1223. strncpy (dst, inet_ntoa(sin.sin_addr), ADDRESS_SIZE - 1);
  1224. }
  1225. #undef ADDRESS_SIZE
  1226. zlog_debug("rip_send_packet %s > %s (%s)",
  1227. inet_ntoa(ifc->address->u.prefix4),
  1228. dst, ifc->ifp->name);
  1229. }
  1230. if ( CHECK_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY) )
  1231. {
  1232. /*
  1233. * ZEBRA_IFA_SECONDARY is set on linux when an interface is configured
  1234. * with multiple addresses on the same subnet: the first address
  1235. * on the subnet is configured "primary", and all subsequent addresses
  1236. * on that subnet are treated as "secondary" addresses.
  1237. * In order to avoid routing-table bloat on other rip listeners,
  1238. * we do not send out RIP packets with ZEBRA_IFA_SECONDARY source addrs.
  1239. * XXX Since Linux is the only system for which the ZEBRA_IFA_SECONDARY
  1240. * flag is set, we would end up sending a packet for a "secondary"
  1241. * source address on non-linux systems.
  1242. */
  1243. if (IS_RIP_DEBUG_PACKET)
  1244. zlog_debug("duplicate dropped");
  1245. return 0;
  1246. }
  1247. /* Make destination address. */
  1248. memset (&sin, 0, sizeof (struct sockaddr_in));
  1249. sin.sin_family = AF_INET;
  1250. #ifdef HAVE_SIN_LEN
  1251. sin.sin_len = sizeof (struct sockaddr_in);
  1252. #endif /* HAVE_SIN_LEN */
  1253. /* When destination is specified, use it's port and address. */
  1254. if (to)
  1255. {
  1256. sin.sin_port = to->sin_port;
  1257. sin.sin_addr = to->sin_addr;
  1258. send_sock = rip->sock;
  1259. }
  1260. else
  1261. {
  1262. struct sockaddr_in from;
  1263. sin.sin_port = htons (RIP_PORT_DEFAULT);
  1264. sin.sin_addr.s_addr = htonl (INADDR_RIP_GROUP);
  1265. /* multicast send should bind to local interface address */
  1266. from.sin_family = AF_INET;
  1267. from.sin_port = htons (RIP_PORT_DEFAULT);
  1268. from.sin_addr = ifc->address->u.prefix4;
  1269. #ifdef HAVE_SIN_LEN
  1270. from.sin_len = sizeof (struct sockaddr_in);
  1271. #endif /* HAVE_SIN_LEN */
  1272. /*
  1273. * we have to open a new socket for each packet because this
  1274. * is the most portable way to bind to a different source
  1275. * ipv4 address for each packet.
  1276. */
  1277. if ( (send_sock = rip_create_socket (&from)) < 0)
  1278. {
  1279. zlog_warn("rip_send_packet could not create socket.");
  1280. return -1;
  1281. }
  1282. rip_interface_multicast_set (send_sock, ifc);
  1283. }
  1284. ret = sendto (send_sock, buf, size, 0, (struct sockaddr *)&sin,
  1285. sizeof (struct sockaddr_in));
  1286. if (IS_RIP_DEBUG_EVENT)
  1287. zlog_debug ("SEND to %s.%d", inet_ntoa(sin.sin_addr),
  1288. ntohs (sin.sin_port));
  1289. if (ret < 0)
  1290. zlog_warn ("can't send packet : %s", safe_strerror (errno));
  1291. if (!to)
  1292. close(send_sock);
  1293. return ret;
  1294. }
  1295. /* Add redistributed route to RIP table. */
  1296. void
  1297. rip_redistribute_add (int type, int sub_type, struct prefix_ipv4 *p,
  1298. unsigned int ifindex, struct in_addr *nexthop,
  1299. unsigned int metric, unsigned char distance)
  1300. {
  1301. int ret;
  1302. struct route_node *rp;
  1303. struct rip_info *rinfo;
  1304. /* Redistribute route */
  1305. ret = rip_destination_check (p->prefix);
  1306. if (! ret)
  1307. return;
  1308. rp = route_node_get (rip->table, (struct prefix *) p);
  1309. rinfo = rp->info;
  1310. if (rinfo)
  1311. {
  1312. if (rinfo->type == ZEBRA_ROUTE_CONNECT
  1313. && rinfo->sub_type == RIP_ROUTE_INTERFACE
  1314. && rinfo->metric != RIP_METRIC_INFINITY)
  1315. {
  1316. route_unlock_node (rp);
  1317. return;
  1318. }
  1319. /* Manually configured RIP route check. */
  1320. if (rinfo->type == ZEBRA_ROUTE_RIP
  1321. && ((rinfo->sub_type == RIP_ROUTE_STATIC) ||
  1322. (rinfo->sub_type == RIP_ROUTE_DEFAULT)) )
  1323. {
  1324. if (type != ZEBRA_ROUTE_RIP || ((sub_type != RIP_ROUTE_STATIC) &&
  1325. (sub_type != RIP_ROUTE_DEFAULT)))
  1326. {
  1327. route_unlock_node (rp);
  1328. return;
  1329. }
  1330. }
  1331. RIP_TIMER_OFF (rinfo->t_timeout);
  1332. RIP_TIMER_OFF (rinfo->t_garbage_collect);
  1333. if (rip_route_rte (rinfo))
  1334. rip_zebra_ipv4_delete ((struct prefix_ipv4 *)&rp->p, &rinfo->nexthop,
  1335. rinfo->metric);
  1336. rp->info = NULL;
  1337. rip_info_free (rinfo);
  1338. route_unlock_node (rp);
  1339. }
  1340. rinfo = rip_info_new ();
  1341. rinfo->type = type;
  1342. rinfo->sub_type = sub_type;
  1343. rinfo->ifindex = ifindex;
  1344. rinfo->metric = 1;
  1345. rinfo->external_metric = metric;
  1346. rinfo->distance = distance;
  1347. rinfo->rp = rp;
  1348. if (nexthop)
  1349. rinfo->nexthop = *nexthop;
  1350. rinfo->flags |= RIP_RTF_FIB;
  1351. rp->info = rinfo;
  1352. rinfo->flags |= RIP_RTF_CHANGED;
  1353. if (IS_RIP_DEBUG_EVENT) {
  1354. if (!nexthop)
  1355. zlog_debug ("Redistribute new prefix %s/%d on the interface %s",
  1356. inet_ntoa(p->prefix), p->prefixlen,
  1357. ifindex2ifname(ifindex));
  1358. else
  1359. zlog_debug ("Redistribute new prefix %s/%d with nexthop %s on the interface %s",
  1360. inet_ntoa(p->prefix), p->prefixlen, inet_ntoa(rinfo->nexthop),
  1361. ifindex2ifname(ifindex));
  1362. }
  1363. rip_event (RIP_TRIGGERED_UPDATE, 0);
  1364. }
  1365. /* Delete redistributed route from RIP table. */
  1366. void
  1367. rip_redistribute_delete (int type, int sub_type, struct prefix_ipv4 *p,
  1368. unsigned int ifindex)
  1369. {
  1370. int ret;
  1371. struct route_node *rp;
  1372. struct rip_info *rinfo;
  1373. ret = rip_destination_check (p->prefix);
  1374. if (! ret)
  1375. return;
  1376. rp = route_node_lookup (rip->table, (struct prefix *) p);
  1377. if (rp)
  1378. {
  1379. rinfo = rp->info;
  1380. if (rinfo != NULL
  1381. && rinfo->type == type
  1382. && rinfo->sub_type == sub_type
  1383. && rinfo->ifindex == ifindex)
  1384. {
  1385. /* Perform poisoned reverse. */
  1386. rinfo->metric = RIP_METRIC_INFINITY;
  1387. RIP_TIMER_ON (rinfo->t_garbage_collect,
  1388. rip_garbage_collect, rip->garbage_time);
  1389. RIP_TIMER_OFF (rinfo->t_timeout);
  1390. rinfo->flags |= RIP_RTF_CHANGED;
  1391. if (IS_RIP_DEBUG_EVENT)
  1392. zlog_debug ("Poisone %s/%d on the interface %s with an infinity metric [delete]",
  1393. inet_ntoa(p->prefix), p->prefixlen,
  1394. ifindex2ifname(ifindex));
  1395. rip_event (RIP_TRIGGERED_UPDATE, 0);
  1396. }
  1397. }
  1398. }
  1399. /* Response to request called from rip_read ().*/
  1400. static void
  1401. rip_request_process (struct rip_packet *packet, int size,
  1402. struct sockaddr_in *from, struct connected *ifc)
  1403. {
  1404. caddr_t lim;
  1405. struct rte *rte;
  1406. struct prefix_ipv4 p;
  1407. struct route_node *rp;
  1408. struct rip_info *rinfo;
  1409. struct rip_interface *ri;
  1410. /* Does not reponse to the requests on the loopback interfaces */
  1411. if (if_is_loopback (ifc->ifp))
  1412. return;
  1413. /* Check RIP process is enabled on this interface. */
  1414. ri = ifc->ifp->info;
  1415. if (! ri->running)
  1416. return;
  1417. /* When passive interface is specified, suppress responses */
  1418. if (ri->passive)
  1419. return;
  1420. /* RIP peer update. */
  1421. rip_peer_update (from, packet->version);
  1422. lim = ((caddr_t) packet) + size;
  1423. rte = packet->rte;
  1424. /* The Request is processed entry by entry. If there are no
  1425. entries, no response is given. */
  1426. if (lim == (caddr_t) rte)
  1427. return;
  1428. /* There is one special case. If there is exactly one entry in the
  1429. request, and it has an address family identifier of zero and a
  1430. metric of infinity (i.e., 16), then this is a request to send the
  1431. entire routing table. */
  1432. if (lim == ((caddr_t) (rte + 1)) &&
  1433. ntohs (rte->family) == 0 &&
  1434. ntohl (rte->metric) == RIP_METRIC_INFINITY)
  1435. {
  1436. struct prefix_ipv4 saddr;
  1437. /* saddr will be used for determining which routes to split-horizon.
  1438. Since the source address we'll pick will be on the same subnet as the
  1439. destination, for the purpose of split-horizoning, we'll
  1440. pretend that "from" is our source address. */
  1441. saddr.family = AF_INET;
  1442. saddr.prefixlen = IPV4_MAX_BITLEN;
  1443. saddr.prefix = from->sin_addr;
  1444. /* All route with split horizon */
  1445. rip_output_process (ifc, from, rip_all_route, packet->version);
  1446. }
  1447. else
  1448. {
  1449. /* Examine the list of RTEs in the Request one by one. For each
  1450. entry, look up the destination in the router's routing
  1451. database and, if there is a route, put that route's metric in
  1452. the metric field of the RTE. If there is no explicit route
  1453. to the specified destination, put infinity in the metric
  1454. field. Once all the entries have been filled in, change the
  1455. command from Request to Response and send the datagram back
  1456. to the requestor. */
  1457. p.family = AF_INET;
  1458. for (; ((caddr_t) rte) < lim; rte++)
  1459. {
  1460. p.prefix = rte->prefix;
  1461. p.prefixlen = ip_masklen (rte->mask);
  1462. apply_mask_ipv4 (&p);
  1463. rp = route_node_lookup (rip->table, (struct prefix *) &p);
  1464. if (rp)
  1465. {
  1466. rinfo = rp->info;
  1467. rte->metric = htonl (rinfo->metric);
  1468. route_unlock_node (rp);
  1469. }
  1470. else
  1471. rte->metric = htonl (RIP_METRIC_INFINITY);
  1472. }
  1473. packet->command = RIP_RESPONSE;
  1474. rip_send_packet ((u_char *)packet, size, from, ifc);
  1475. }
  1476. rip_global_queries++;
  1477. }
  1478. #if RIP_RECVMSG
  1479. /* Set IPv6 packet info to the socket. */
  1480. static int
  1481. setsockopt_pktinfo (int sock)
  1482. {
  1483. int ret;
  1484. int val = 1;
  1485. ret = setsockopt(sock, IPPROTO_IP, IP_PKTINFO, &val, sizeof(val));
  1486. if (ret < 0)
  1487. zlog_warn ("Can't setsockopt IP_PKTINFO : %s", safe_strerror (errno));
  1488. return ret;
  1489. }
  1490. /* Read RIP packet by recvmsg function. */
  1491. int
  1492. rip_recvmsg (int sock, u_char *buf, int size, struct sockaddr_in *from,
  1493. int *ifindex)
  1494. {
  1495. int ret;
  1496. struct msghdr msg;
  1497. struct iovec iov;
  1498. struct cmsghdr *ptr;
  1499. char adata[1024];
  1500. msg.msg_name = (void *) from;
  1501. msg.msg_namelen = sizeof (struct sockaddr_in);
  1502. msg.msg_iov = &iov;
  1503. msg.msg_iovlen = 1;
  1504. msg.msg_control = (void *) adata;
  1505. msg.msg_controllen = sizeof adata;
  1506. iov.iov_base = buf;
  1507. iov.iov_len = size;
  1508. ret = recvmsg (sock, &msg, 0);
  1509. if (ret < 0)
  1510. return ret;
  1511. for (ptr = ZCMSG_FIRSTHDR(&msg); ptr != NULL; ptr = CMSG_NXTHDR(&msg, ptr))
  1512. if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_PKTINFO)
  1513. {
  1514. struct in_pktinfo *pktinfo;
  1515. int i;
  1516. pktinfo = (struct in_pktinfo *) CMSG_DATA (ptr);
  1517. i = pktinfo->ipi_ifindex;
  1518. }
  1519. return ret;
  1520. }
  1521. /* RIP packet read function. */
  1522. int
  1523. rip_read_new (struct thread *t)
  1524. {
  1525. int ret;
  1526. int sock;
  1527. char buf[RIP_PACKET_MAXSIZ];
  1528. struct sockaddr_in from;
  1529. unsigned int ifindex;
  1530. /* Fetch socket then register myself. */
  1531. sock = THREAD_FD (t);
  1532. rip_event (RIP_READ, sock);
  1533. /* Read RIP packet. */
  1534. ret = rip_recvmsg (sock, buf, RIP_PACKET_MAXSIZ, &from, (int *)&ifindex);
  1535. if (ret < 0)
  1536. {
  1537. zlog_warn ("Can't read RIP packet: %s", safe_strerror (errno));
  1538. return ret;
  1539. }
  1540. return ret;
  1541. }
  1542. #endif /* RIP_RECVMSG */
  1543. /* First entry point of RIP packet. */
  1544. static int
  1545. rip_read (struct thread *t)
  1546. {
  1547. int sock;
  1548. int ret;
  1549. int rtenum;
  1550. union rip_buf rip_buf;
  1551. struct rip_packet *packet;
  1552. struct sockaddr_in from;
  1553. int len;
  1554. socklen_t fromlen;
  1555. struct interface *ifp;
  1556. struct connected *ifc;
  1557. struct rip_interface *ri;
  1558. /* Fetch socket then register myself. */
  1559. sock = THREAD_FD (t);
  1560. rip->t_read = NULL;
  1561. /* Add myself to tne next event */
  1562. rip_event (RIP_READ, sock);
  1563. /* RIPd manages only IPv4. */
  1564. memset (&from, 0, sizeof (struct sockaddr_in));
  1565. fromlen = sizeof (struct sockaddr_in);
  1566. len = recvfrom (sock, (char *)&rip_buf.buf, sizeof (rip_buf.buf), 0,
  1567. (struct sockaddr *) &from, &fromlen);
  1568. if (len < 0)
  1569. {
  1570. zlog_info ("recvfrom failed: %s", safe_strerror (errno));
  1571. return len;
  1572. }
  1573. /* Check is this packet comming from myself? */
  1574. if (if_check_address (from.sin_addr))
  1575. {
  1576. if (IS_RIP_DEBUG_PACKET)
  1577. zlog_debug ("ignore packet comes from myself");
  1578. return -1;
  1579. }
  1580. /* Which interface is this packet comes from. */
  1581. ifp = if_lookup_address (from.sin_addr);
  1582. /* RIP packet received */
  1583. if (IS_RIP_DEBUG_EVENT)
  1584. zlog_debug ("RECV packet from %s port %d on %s",
  1585. inet_ntoa (from.sin_addr), ntohs (from.sin_port),
  1586. ifp ? ifp->name : "unknown");
  1587. /* If this packet come from unknown interface, ignore it. */
  1588. if (ifp == NULL)
  1589. {
  1590. zlog_info ("rip_read: cannot find interface for packet from %s port %d",
  1591. inet_ntoa(from.sin_addr), ntohs (from.sin_port));
  1592. return -1;
  1593. }
  1594. ifc = connected_lookup_address (ifp, from.sin_addr);
  1595. if (ifc == NULL)
  1596. {
  1597. zlog_info ("rip_read: cannot find connected address for packet from %s "
  1598. "port %d on interface %s",
  1599. inet_ntoa(from.sin_addr), ntohs (from.sin_port), ifp->name);
  1600. return -1;
  1601. }
  1602. /* Packet length check. */
  1603. if (len < RIP_PACKET_MINSIZ)
  1604. {
  1605. zlog_warn ("packet size %d is smaller than minimum size %d",
  1606. len, RIP_PACKET_MINSIZ);
  1607. rip_peer_bad_packet (&from);
  1608. return len;
  1609. }
  1610. if (len > RIP_PACKET_MAXSIZ)
  1611. {
  1612. zlog_warn ("packet size %d is larger than max size %d",
  1613. len, RIP_PACKET_MAXSIZ);
  1614. rip_peer_bad_packet (&from);
  1615. return len;
  1616. }
  1617. /* Packet alignment check. */
  1618. if ((len - RIP_PACKET_MINSIZ) % 20)
  1619. {
  1620. zlog_warn ("packet size %d is wrong for RIP packet alignment", len);
  1621. rip_peer_bad_packet (&from);
  1622. return len;
  1623. }
  1624. /* Set RTE number. */
  1625. rtenum = ((len - RIP_PACKET_MINSIZ) / 20);
  1626. /* For easy to handle. */
  1627. packet = &rip_buf.rip_packet;
  1628. /* RIP version check. */
  1629. if (packet->version == 0)
  1630. {
  1631. zlog_info ("version 0 with command %d received.", packet->command);
  1632. rip_peer_bad_packet (&from);
  1633. return -1;
  1634. }
  1635. /* Dump RIP packet. */
  1636. if (IS_RIP_DEBUG_RECV)
  1637. rip_packet_dump (packet, len, "RECV");
  1638. /* RIP version adjust. This code should rethink now. RFC1058 says
  1639. that "Version 1 implementations are to ignore this extra data and
  1640. process only the fields specified in this document.". So RIPv3
  1641. packet should be treated as RIPv1 ignoring must be zero field. */
  1642. if (packet->version > RIPv2)
  1643. packet->version = RIPv2;
  1644. /* Is RIP running or is this RIP neighbor ?*/
  1645. ri = ifp->info;
  1646. if (! ri->running && ! rip_neighbor_lookup (&from))
  1647. {
  1648. if (IS_RIP_DEBUG_EVENT)
  1649. zlog_debug ("RIP is not enabled on interface %s.", ifp->name);
  1650. rip_peer_bad_packet (&from);
  1651. return -1;
  1652. }
  1653. /* RIP Version check. */
  1654. if (packet->command == RIP_RESPONSE)
  1655. {
  1656. int vrecv = ((ri->ri_receive == RI_RIP_UNSPEC) ?
  1657. rip->version_recv : ri->ri_receive);
  1658. if (packet->version == RIPv1)
  1659. if (! (vrecv & RIPv1))
  1660. {
  1661. if (IS_RIP_DEBUG_PACKET)
  1662. zlog_debug (" packet's v%d doesn't fit to if version spec",
  1663. packet->version);
  1664. rip_peer_bad_packet (&from);
  1665. return -1;
  1666. }
  1667. if (packet->version == RIPv2)
  1668. if (! (vrecv & RIPv2))
  1669. {
  1670. if (IS_RIP_DEBUG_PACKET)
  1671. zlog_debug (" packet's v%d doesn't fit to if version spec",
  1672. packet->version);
  1673. rip_peer_bad_packet (&from);
  1674. return -1;
  1675. }
  1676. }
  1677. /* RFC2453 5.2 If the router is not configured to authenticate RIP-2
  1678. messages, then RIP-1 and unauthenticated RIP-2 messages will be
  1679. accepted; authenticated RIP-2 messages shall be discarded. */
  1680. if ((ri->auth_type == RIP_NO_AUTH)
  1681. && rtenum
  1682. && (packet->version == RIPv2)
  1683. && (packet->rte->family == htons(RIP_FAMILY_AUTH)))
  1684. {
  1685. if (IS_RIP_DEBUG_EVENT)
  1686. zlog_debug ("packet RIPv%d is dropped because authentication disabled",
  1687. packet->version);
  1688. rip_peer_bad_packet (&from);
  1689. return -1;
  1690. }
  1691. /* If the router is configured to authenticate RIP-2 messages, then
  1692. RIP-1 messages and RIP-2 messages which pass authentication
  1693. testing shall be accepted; unauthenticated and failed
  1694. authentication RIP-2 messages shall be discarded. For maximum
  1695. security, RIP-1 messages should be ignored when authentication is
  1696. in use (see section 4.1); otherwise, the routing information from
  1697. authenticated messages will be propagated by RIP-1 routers in an
  1698. unauthenticated manner. */
  1699. if ((ri->auth_type == RIP_AUTH_SIMPLE_PASSWORD
  1700. || ri->auth_type == RIP_AUTH_MD5) && rtenum)
  1701. {
  1702. /* We follow maximum security. */
  1703. if (packet->version == RIPv1
  1704. && packet->rte->family == htons(RIP_FAMILY_AUTH))
  1705. {
  1706. if (IS_RIP_DEBUG_PACKET)
  1707. zlog_debug
  1708. ("packet RIPv%d is dropped because authentication enabled",
  1709. packet->version);
  1710. rip_peer_bad_packet (&from);
  1711. return -1;
  1712. }
  1713. /* Check RIPv2 authentication. */
  1714. if (packet->version == RIPv2)
  1715. {
  1716. if (packet->rte->family == htons(RIP_FAMILY_AUTH))
  1717. {
  1718. if (packet->rte->tag == htons(RIP_AUTH_SIMPLE_PASSWORD))
  1719. {
  1720. ret = rip_auth_simple_password (packet->rte, &from, ifp);
  1721. if (! ret)
  1722. {
  1723. if (IS_RIP_DEBUG_EVENT)
  1724. zlog_debug
  1725. ("RIPv2 simple password authentication failed");
  1726. rip_peer_bad_packet (&from);
  1727. return -1;
  1728. }
  1729. else
  1730. {
  1731. if (IS_RIP_DEBUG_EVENT)
  1732. zlog_debug
  1733. ("RIPv2 simple password authentication success");
  1734. }
  1735. }
  1736. else if (packet->rte->tag == htons(RIP_AUTH_MD5))
  1737. {
  1738. ret = rip_auth_md5 (packet, &from, len, ifp);
  1739. if (! ret)
  1740. {
  1741. if (IS_RIP_DEBUG_EVENT)
  1742. zlog_debug ("RIPv2 MD5 authentication failed");
  1743. rip_peer_bad_packet (&from);
  1744. return -1;
  1745. }
  1746. else
  1747. {
  1748. if (IS_RIP_DEBUG_EVENT)
  1749. zlog_debug ("RIPv2 MD5 authentication success");
  1750. }
  1751. /* Reset RIP packet length to trim MD5 data. */
  1752. len = ret;
  1753. }
  1754. else
  1755. {
  1756. if (IS_RIP_DEBUG_EVENT)
  1757. zlog_debug ("Unknown authentication type %d",
  1758. ntohs (packet->rte->tag));
  1759. rip_peer_bad_packet (&from);
  1760. return -1;
  1761. }
  1762. }
  1763. else
  1764. {
  1765. /* There is no authentication in the packet. */
  1766. if (ri->auth_str || ri->key_chain)
  1767. {
  1768. if (IS_RIP_DEBUG_EVENT)
  1769. zlog_debug
  1770. ("RIPv2 authentication failed: no authentication in packet");
  1771. rip_peer_bad_packet (&from);
  1772. return -1;
  1773. }
  1774. }
  1775. }
  1776. }
  1777. /* Process each command. */
  1778. switch (packet->command)
  1779. {
  1780. case RIP_RESPONSE:
  1781. rip_response_process (packet, len, &from, ifc);
  1782. break;
  1783. case RIP_REQUEST:
  1784. case RIP_POLL:
  1785. rip_request_process (packet, len, &from, ifc);
  1786. break;
  1787. case RIP_TRACEON:
  1788. case RIP_TRACEOFF:
  1789. zlog_info ("Obsolete command %s received, please sent it to routed",
  1790. lookup (rip_msg, packet->command));
  1791. rip_peer_bad_packet (&from);
  1792. break;
  1793. case RIP_POLL_ENTRY:
  1794. zlog_info ("Obsolete command %s received",
  1795. lookup (rip_msg, packet->command));
  1796. rip_peer_bad_packet (&from);
  1797. break;
  1798. default:
  1799. zlog_info ("Unknown RIP command %d received", packet->command);
  1800. rip_peer_bad_packet (&from);
  1801. break;
  1802. }
  1803. return len;
  1804. }
  1805. /* Write routing table entry to the stream and return next index of
  1806. the routing table entry in the stream. */
  1807. static int
  1808. rip_write_rte (int num, struct stream *s, struct prefix_ipv4 *p,
  1809. u_char version, struct rip_info *rinfo)
  1810. {
  1811. struct in_addr mask;
  1812. /* Write routing table entry. */
  1813. if (version == RIPv1)
  1814. {
  1815. stream_putw (s, AF_INET);
  1816. stream_putw (s, 0);
  1817. stream_put_ipv4 (s, p->prefix.s_addr);
  1818. stream_put_ipv4 (s, 0);
  1819. stream_put_ipv4 (s, 0);
  1820. stream_putl (s, rinfo->metric_out);
  1821. }
  1822. else
  1823. {
  1824. masklen2ip (p->prefixlen, &mask);
  1825. stream_putw (s, AF_INET);
  1826. stream_putw (s, rinfo->tag_out);
  1827. stream_put_ipv4 (s, p->prefix.s_addr);
  1828. stream_put_ipv4 (s, mask.s_addr);
  1829. stream_put_ipv4 (s, rinfo->nexthop_out.s_addr);
  1830. stream_putl (s, rinfo->metric_out);
  1831. }
  1832. return ++num;
  1833. }
  1834. /* Send update to the ifp or spcified neighbor. */
  1835. void
  1836. rip_output_process (struct connected *ifc, struct sockaddr_in *to,
  1837. int route_type, u_char version)
  1838. {
  1839. int ret;
  1840. struct stream *s;
  1841. struct route_node *rp;
  1842. struct rip_info *rinfo;
  1843. struct rip_interface *ri;
  1844. struct prefix_ipv4 *p;
  1845. struct prefix_ipv4 classfull;
  1846. struct prefix_ipv4 ifaddrclass;
  1847. struct key *key = NULL;
  1848. /* this might need to made dynamic if RIP ever supported auth methods
  1849. with larger key string sizes */
  1850. char auth_str[RIP_AUTH_SIMPLE_SIZE];
  1851. size_t doff = 0; /* offset of digest offset field */
  1852. int num = 0;
  1853. int rtemax;
  1854. int subnetted = 0;
  1855. /* Logging output event. */
  1856. if (IS_RIP_DEBUG_EVENT)
  1857. {
  1858. if (to)
  1859. zlog_debug ("update routes to neighbor %s", inet_ntoa (to->sin_addr));
  1860. else
  1861. zlog_debug ("update routes on interface %s ifindex %d",
  1862. ifc->ifp->name, ifc->ifp->ifindex);
  1863. }
  1864. /* Set output stream. */
  1865. s = rip->obuf;
  1866. /* Reset stream and RTE counter. */
  1867. stream_reset (s);
  1868. rtemax = (RIP_PACKET_MAXSIZ - 4) / 20;
  1869. /* Get RIP interface. */
  1870. ri = ifc->ifp->info;
  1871. /* If output interface is in simple password authentication mode, we
  1872. need space for authentication data. */
  1873. if (ri->auth_type == RIP_AUTH_SIMPLE_PASSWORD)
  1874. rtemax -= 1;
  1875. /* If output interface is in MD5 authentication mode, we need space
  1876. for authentication header and data. */
  1877. if (ri->auth_type == RIP_AUTH_MD5)
  1878. rtemax -= 2;
  1879. /* If output interface is in simple password authentication mode
  1880. and string or keychain is specified we need space for auth. data */
  1881. if (ri->auth_type != RIP_NO_AUTH)
  1882. {
  1883. if (ri->key_chain)
  1884. {
  1885. struct keychain *keychain;
  1886. keychain = keychain_lookup (ri->key_chain);
  1887. if (keychain)
  1888. key = key_lookup_for_send (keychain);
  1889. }
  1890. /* to be passed to auth functions later */
  1891. rip_auth_prepare_str_send (ri, key, auth_str, RIP_AUTH_SIMPLE_SIZE);
  1892. }
  1893. if (version == RIPv1)
  1894. {
  1895. memcpy (&ifaddrclass, ifc->address, sizeof (struct prefix_ipv4));
  1896. apply_classful_mask_ipv4 (&ifaddrclass);
  1897. subnetted = 0;
  1898. if (ifc->address->prefixlen > ifaddrclass.prefixlen)
  1899. subnetted = 1;
  1900. }
  1901. for (rp = route_top (rip->table); rp; rp = route_next (rp))
  1902. if ((rinfo = rp->info) != NULL)
  1903. {
  1904. /* For RIPv1, if we are subnetted, output subnets in our network */
  1905. /* that have the same mask as the output "interface". For other */
  1906. /* networks, only the classfull version is output. */
  1907. if (version == RIPv1)
  1908. {
  1909. p = (struct prefix_ipv4 *) &rp->p;
  1910. if (IS_RIP_DEBUG_PACKET)
  1911. zlog_debug("RIPv1 mask check, %s/%d considered for output",
  1912. inet_ntoa (rp->p.u.prefix4), rp->p.prefixlen);
  1913. if (subnetted &&
  1914. prefix_match ((struct prefix *) &ifaddrclass, &rp->p))
  1915. {
  1916. if ((ifc->address->prefixlen != rp->p.prefixlen) &&
  1917. (rp->p.prefixlen != 32))
  1918. continue;
  1919. }
  1920. else
  1921. {
  1922. memcpy (&classfull, &rp->p, sizeof(struct prefix_ipv4));
  1923. apply_classful_mask_ipv4(&classfull);
  1924. if (rp->p.u.prefix4.s_addr != 0 &&
  1925. classfull.prefixlen != rp->p.prefixlen)
  1926. continue;
  1927. }
  1928. if (IS_RIP_DEBUG_PACKET)
  1929. zlog_debug("RIPv1 mask check, %s/%d made it through",
  1930. inet_ntoa (rp->p.u.prefix4), rp->p.prefixlen);
  1931. }
  1932. else
  1933. p = (struct prefix_ipv4 *) &rp->p;
  1934. /* Apply output filters. */
  1935. ret = rip_outgoing_filter (p, ri);
  1936. if (ret < 0)
  1937. continue;
  1938. /* Changed route only output. */
  1939. if (route_type == rip_changed_route &&
  1940. (! (rinfo->flags & RIP_RTF_CHANGED)))
  1941. continue;
  1942. /* Split horizon. */
  1943. /* if (split_horizon == rip_split_horizon) */
  1944. if (ri->split_horizon == RIP_SPLIT_HORIZON)
  1945. {
  1946. /*
  1947. * We perform split horizon for RIP and connected route.
  1948. * For rip routes, we want to suppress the route if we would
  1949. * end up sending the route back on the interface that we
  1950. * learned it from, with a higher metric. For connected routes,
  1951. * we suppress the route if the prefix is a subset of the
  1952. * source address that we are going to use for the packet
  1953. * (in order to handle the case when multiple subnets are
  1954. * configured on the same interface).
  1955. */
  1956. if (rinfo->type == ZEBRA_ROUTE_RIP &&
  1957. rinfo->ifindex == ifc->ifp->ifindex)
  1958. continue;
  1959. if (rinfo->type == ZEBRA_ROUTE_CONNECT &&
  1960. prefix_match((struct prefix *)p, ifc->address))
  1961. continue;
  1962. }
  1963. /* Preparation for route-map. */
  1964. rinfo->metric_set = 0;
  1965. rinfo->nexthop_out.s_addr = 0;
  1966. rinfo->metric_out = rinfo->metric;
  1967. rinfo->tag_out = rinfo->tag;
  1968. rinfo->ifindex_out = ifc->ifp->ifindex;
  1969. /* In order to avoid some local loops,
  1970. * if the RIP route has a nexthop via this interface, keep the nexthop,
  1971. * otherwise set it to 0. The nexthop should not be propagated
  1972. * beyond the local broadcast/multicast area in order
  1973. * to avoid an IGP multi-level recursive look-up.
  1974. * see (4.4)
  1975. */
  1976. if (rinfo->ifindex == ifc->ifp->ifindex)
  1977. rinfo->nexthop_out = rinfo->nexthop;
  1978. /* Interface route-map */
  1979. if (ri->routemap[RIP_FILTER_OUT])
  1980. {
  1981. ret = route_map_apply (ri->routemap[RIP_FILTER_OUT],
  1982. (struct prefix *) p, RMAP_RIP,
  1983. rinfo);
  1984. if (ret == RMAP_DENYMATCH)
  1985. {
  1986. if (IS_RIP_DEBUG_PACKET)
  1987. zlog_debug ("RIP %s/%d is filtered by route-map out",
  1988. inet_ntoa (p->prefix), p->prefixlen);
  1989. continue;
  1990. }
  1991. }
  1992. /* Apply redistribute route map - continue, if deny */
  1993. if (rip->route_map[rinfo->type].name
  1994. && rinfo->sub_type != RIP_ROUTE_INTERFACE)
  1995. {
  1996. ret = route_map_apply (rip->route_map[rinfo->type].map,
  1997. (struct prefix *)p, RMAP_RIP, rinfo);
  1998. if (ret == RMAP_DENYMATCH)
  1999. {
  2000. if (IS_RIP_DEBUG_PACKET)
  2001. zlog_debug ("%s/%d is filtered by route-map",
  2002. inet_ntoa (p->prefix), p->prefixlen);
  2003. continue;
  2004. }
  2005. }
  2006. /* When route-map does not set metric. */
  2007. if (! rinfo->metric_set)
  2008. {
  2009. /* If redistribute metric is set. */
  2010. if (rip->route_map[rinfo->type].metric_config
  2011. && rinfo->metric != RIP_METRIC_INFINITY)
  2012. {
  2013. rinfo->metric_out = rip->route_map[rinfo->type].metric;
  2014. }
  2015. else
  2016. {
  2017. /* If the route is not connected or localy generated
  2018. one, use default-metric value*/
  2019. if (rinfo->type != ZEBRA_ROUTE_RIP
  2020. && rinfo->type != ZEBRA_ROUTE_CONNECT
  2021. && rinfo->metric != RIP_METRIC_INFINITY)
  2022. rinfo->metric_out = rip->default_metric;
  2023. }
  2024. }
  2025. /* Apply offset-list */
  2026. if (rinfo->metric != RIP_METRIC_INFINITY)
  2027. rip_offset_list_apply_out (p, ifc->ifp, &rinfo->metric_out);
  2028. if (rinfo->metric_out > RIP_METRIC_INFINITY)
  2029. rinfo->metric_out = RIP_METRIC_INFINITY;
  2030. /* Perform split-horizon with poisoned reverse
  2031. * for RIP and connected routes.
  2032. **/
  2033. if (ri->split_horizon == RIP_SPLIT_HORIZON_POISONED_REVERSE) {
  2034. /*
  2035. * We perform split horizon for RIP and connected route.
  2036. * For rip routes, we want to suppress the route if we would
  2037. * end up sending the route back on the interface that we
  2038. * learned it from, with a higher metric. For connected routes,
  2039. * we suppress the route if the prefix is a subset of the
  2040. * source address that we are going to use for the packet
  2041. * (in order to handle the case when multiple subnets are
  2042. * configured on the same interface).
  2043. */
  2044. if (rinfo->type == ZEBRA_ROUTE_RIP &&
  2045. rinfo->ifindex == ifc->ifp->ifindex)
  2046. rinfo->metric_out = RIP_METRIC_INFINITY;
  2047. if (rinfo->type == ZEBRA_ROUTE_CONNECT &&
  2048. prefix_match((struct prefix *)p, ifc->address))
  2049. rinfo->metric_out = RIP_METRIC_INFINITY;
  2050. }
  2051. /* Prepare preamble, auth headers, if needs be */
  2052. if (num == 0)
  2053. {
  2054. stream_putc (s, RIP_RESPONSE);
  2055. stream_putc (s, version);
  2056. stream_putw (s, 0);
  2057. /* auth header for !v1 && !no_auth */
  2058. if ( (ri->auth_type != RIP_NO_AUTH) && (version != RIPv1) )
  2059. doff = rip_auth_header_write (s, ri, key, auth_str,
  2060. RIP_AUTH_SIMPLE_SIZE);
  2061. }
  2062. /* Write RTE to the stream. */
  2063. num = rip_write_rte (num, s, p, version, rinfo);
  2064. if (num == rtemax)
  2065. {
  2066. if (version == RIPv2 && ri->auth_type == RIP_AUTH_MD5)
  2067. rip_auth_md5_set (s, ri, doff, auth_str, RIP_AUTH_SIMPLE_SIZE);
  2068. ret = rip_send_packet (STREAM_DATA (s), stream_get_endp (s),
  2069. to, ifc);
  2070. if (ret >= 0 && IS_RIP_DEBUG_SEND)
  2071. rip_packet_dump ((struct rip_packet *)STREAM_DATA (s),
  2072. stream_get_endp(s), "SEND");
  2073. num = 0;
  2074. stream_reset (s);
  2075. }
  2076. }
  2077. /* Flush unwritten RTE. */
  2078. if (num != 0)
  2079. {
  2080. if (version == RIPv2 && ri->auth_type == RIP_AUTH_MD5)
  2081. rip_auth_md5_set (s, ri, doff, auth_str, RIP_AUTH_SIMPLE_SIZE);
  2082. ret = rip_send_packet (STREAM_DATA (s), stream_get_endp (s), to, ifc);
  2083. if (ret >= 0 && IS_RIP_DEBUG_SEND)
  2084. rip_packet_dump ((struct rip_packet *)STREAM_DATA (s),
  2085. stream_get_endp (s), "SEND");
  2086. num = 0;
  2087. stream_reset (s);
  2088. }
  2089. /* Statistics updates. */
  2090. ri->sent_updates++;
  2091. }
  2092. /* Send RIP packet to the interface. */
  2093. static void
  2094. rip_update_interface (struct connected *ifc, u_char version, int route_type)
  2095. {
  2096. struct sockaddr_in to;
  2097. /* When RIP version is 2 and multicast enable interface. */
  2098. if (version == RIPv2 && if_is_multicast (ifc->ifp))
  2099. {
  2100. if (IS_RIP_DEBUG_EVENT)
  2101. zlog_debug ("multicast announce on %s ", ifc->ifp->name);
  2102. rip_output_process (ifc, NULL, route_type, version);
  2103. return;
  2104. }
  2105. /* If we can't send multicast packet, send it with unicast. */
  2106. if (if_is_broadcast (ifc->ifp) || if_is_pointopoint (ifc->ifp))
  2107. {
  2108. if (ifc->address->family == AF_INET)
  2109. {
  2110. /* Destination address and port setting. */
  2111. memset (&to, 0, sizeof (struct sockaddr_in));
  2112. if (ifc->destination)
  2113. /* use specified broadcast or point-to-point destination addr */
  2114. to.sin_addr = ifc->destination->u.prefix4;
  2115. else
  2116. /* calculate the appropriate broadcast address */
  2117. to.sin_addr.s_addr =
  2118. ipv4_broadcast_addr(ifc->address->u.prefix4.s_addr,
  2119. ifc->address->prefixlen);
  2120. to.sin_port = htons (RIP_PORT_DEFAULT);
  2121. if (IS_RIP_DEBUG_EVENT)
  2122. zlog_debug ("%s announce to %s on %s",
  2123. if_is_pointopoint (ifc->ifp) ? "unicast" : "broadcast",
  2124. inet_ntoa (to.sin_addr), ifc->ifp->name);
  2125. rip_output_process (ifc, &to, route_type, version);
  2126. }
  2127. }
  2128. }
  2129. /* Update send to all interface and neighbor. */
  2130. static void
  2131. rip_update_process (int route_type)
  2132. {
  2133. struct listnode *node;
  2134. struct listnode *ifnode, *ifnnode;
  2135. struct connected *connected;
  2136. struct interface *ifp;
  2137. struct rip_interface *ri;
  2138. struct route_node *rp;
  2139. struct sockaddr_in to;
  2140. struct prefix_ipv4 *p;
  2141. /* Send RIP update to each interface. */
  2142. for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
  2143. {
  2144. if (if_is_loopback (ifp))
  2145. continue;
  2146. if (! if_is_operative (ifp))
  2147. continue;
  2148. /* Fetch RIP interface information. */
  2149. ri = ifp->info;
  2150. /* When passive interface is specified, suppress announce to the
  2151. interface. */
  2152. if (ri->passive)
  2153. continue;
  2154. if (ri->running)
  2155. {
  2156. if (IS_RIP_DEBUG_EVENT)
  2157. {
  2158. if (ifp->name)
  2159. zlog_debug ("SEND UPDATE to %s ifindex %d",
  2160. ifp->name, ifp->ifindex);
  2161. else
  2162. zlog_debug ("SEND UPDATE to _unknown_ ifindex %d",
  2163. ifp->ifindex);
  2164. }
  2165. /* send update on each connected network */
  2166. for (ALL_LIST_ELEMENTS (ifp->connected, ifnode, ifnnode, connected))
  2167. {
  2168. struct prefix_ipv4 *ifaddr;
  2169. int done = 0;
  2170. /*
  2171. * If there is no version configuration in the interface,
  2172. * use rip's version setting.
  2173. */
  2174. int vsend = ((ri->ri_send == RI_RIP_UNSPEC) ?
  2175. rip->version_send : ri->ri_send);
  2176. ifaddr = (struct prefix_ipv4 *) connected->address;
  2177. if (ifaddr->family != AF_INET)
  2178. continue;
  2179. if ((vsend & RIPv1) && !done)
  2180. rip_update_interface (connected, RIPv1, route_type);
  2181. if ((vsend & RIPv2) && if_is_multicast(ifp))
  2182. rip_update_interface (connected, RIPv2, route_type);
  2183. done = 1;
  2184. if (!(vsend & RIPv2) || !if_is_multicast(ifp))
  2185. break;
  2186. }
  2187. }
  2188. }
  2189. /* RIP send updates to each neighbor. */
  2190. for (rp = route_top (rip->neighbor); rp; rp = route_next (rp))
  2191. if (rp->info != NULL)
  2192. {
  2193. p = (struct prefix_ipv4 *) &rp->p;
  2194. ifp = if_lookup_address (p->prefix);
  2195. if (! ifp)
  2196. {
  2197. zlog_warn ("Neighbor %s doesnt have connected interface!",
  2198. inet_ntoa (p->prefix));
  2199. continue;
  2200. }
  2201. if ( (connected = connected_lookup_address (ifp, p->prefix)) == NULL)
  2202. {
  2203. zlog_warn ("Neighbor %s doesnt have connected network",
  2204. inet_ntoa (p->prefix));
  2205. continue;
  2206. }
  2207. /* Set destination address and port */
  2208. memset (&to, 0, sizeof (struct sockaddr_in));
  2209. to.sin_addr = p->prefix;
  2210. to.sin_port = htons (RIP_PORT_DEFAULT);
  2211. /* RIP version is rip's configuration. */
  2212. rip_output_process (connected, &to, route_type, rip->version_send);
  2213. }
  2214. }
  2215. /* RIP's periodical timer. */
  2216. static int
  2217. rip_update (struct thread *t)
  2218. {
  2219. /* Clear timer pointer. */
  2220. rip->t_update = NULL;
  2221. if (IS_RIP_DEBUG_EVENT)
  2222. zlog_debug ("update timer fire!");
  2223. /* Process update output. */
  2224. rip_update_process (rip_all_route);
  2225. /* Triggered updates may be suppressed if a regular update is due by
  2226. the time the triggered update would be sent. */
  2227. if (rip->t_triggered_interval)
  2228. {
  2229. thread_cancel (rip->t_triggered_interval);
  2230. rip->t_triggered_interval = NULL;
  2231. }
  2232. rip->trigger = 0;
  2233. /* Register myself. */
  2234. rip_event (RIP_UPDATE_EVENT, 0);
  2235. return 0;
  2236. }
  2237. /* Walk down the RIP routing table then clear changed flag. */
  2238. static void
  2239. rip_clear_changed_flag ()
  2240. {
  2241. struct route_node *rp;
  2242. struct rip_info *rinfo;
  2243. for (rp = route_top (rip->table); rp; rp = route_next (rp))
  2244. if ((rinfo = rp->info) != NULL)
  2245. if (rinfo->flags & RIP_RTF_CHANGED)
  2246. rinfo->flags &= ~RIP_RTF_CHANGED;
  2247. }
  2248. /* Triggered update interval timer. */
  2249. static int
  2250. rip_triggered_interval (struct thread *t)
  2251. {
  2252. int rip_triggered_update (struct thread *);
  2253. rip->t_triggered_interval = NULL;
  2254. if (rip->trigger)
  2255. {
  2256. rip->trigger = 0;
  2257. rip_triggered_update (t);
  2258. }
  2259. return 0;
  2260. }
  2261. /* Execute triggered update. */
  2262. static int
  2263. rip_triggered_update (struct thread *t)
  2264. {
  2265. int interval;
  2266. /* Clear thred pointer. */
  2267. rip->t_triggered_update = NULL;
  2268. /* Cancel interval timer. */
  2269. if (rip->t_triggered_interval)
  2270. {
  2271. thread_cancel (rip->t_triggered_interval);
  2272. rip->t_triggered_interval = NULL;
  2273. }
  2274. rip->trigger = 0;
  2275. /* Logging triggered update. */
  2276. if (IS_RIP_DEBUG_EVENT)
  2277. zlog_debug ("triggered update!");
  2278. /* Split Horizon processing is done when generating triggered
  2279. updates as well as normal updates (see section 2.6). */
  2280. rip_update_process (rip_changed_route);
  2281. /* Once all of the triggered updates have been generated, the route
  2282. change flags should be cleared. */
  2283. rip_clear_changed_flag ();
  2284. /* After a triggered update is sent, a timer should be set for a
  2285. random interval between 1 and 5 seconds. If other changes that
  2286. would trigger updates occur before the timer expires, a single
  2287. update is triggered when the timer expires. */
  2288. interval = (random () % 5) + 1;
  2289. rip->t_triggered_interval =
  2290. thread_add_timer (master, rip_triggered_interval, NULL, interval);
  2291. return 0;
  2292. }
  2293. /* Withdraw redistributed route. */
  2294. void
  2295. rip_redistribute_withdraw (int type)
  2296. {
  2297. struct route_node *rp;
  2298. struct rip_info *rinfo;
  2299. if (!rip)
  2300. return;
  2301. for (rp = route_top (rip->table); rp; rp = route_next (rp))
  2302. if ((rinfo = rp->info) != NULL)
  2303. {
  2304. if (rinfo->type == type
  2305. && rinfo->sub_type != RIP_ROUTE_INTERFACE)
  2306. {
  2307. /* Perform poisoned reverse. */
  2308. rinfo->metric = RIP_METRIC_INFINITY;
  2309. RIP_TIMER_ON (rinfo->t_garbage_collect,
  2310. rip_garbage_collect, rip->garbage_time);
  2311. RIP_TIMER_OFF (rinfo->t_timeout);
  2312. rinfo->flags |= RIP_RTF_CHANGED;
  2313. if (IS_RIP_DEBUG_EVENT) {
  2314. struct prefix_ipv4 *p = (struct prefix_ipv4 *) &rp->p;
  2315. zlog_debug ("Poisone %s/%d on the interface %s with an infinity metric [withdraw]",
  2316. inet_ntoa(p->prefix), p->prefixlen,
  2317. ifindex2ifname(rinfo->ifindex));
  2318. }
  2319. rip_event (RIP_TRIGGERED_UPDATE, 0);
  2320. }
  2321. }
  2322. }
  2323. /* Create new RIP instance and set it to global variable. */
  2324. static int
  2325. rip_create (void)
  2326. {
  2327. rip = XMALLOC (MTYPE_RIP, sizeof (struct rip));
  2328. memset (rip, 0, sizeof (struct rip));
  2329. /* Set initial value. */
  2330. rip->version_send = RI_RIP_VERSION_2;
  2331. rip->version_recv = RI_RIP_VERSION_1_AND_2;
  2332. rip->update_time = RIP_UPDATE_TIMER_DEFAULT;
  2333. rip->timeout_time = RIP_TIMEOUT_TIMER_DEFAULT;
  2334. rip->garbage_time = RIP_GARBAGE_TIMER_DEFAULT;
  2335. rip->default_metric = RIP_DEFAULT_METRIC_DEFAULT;
  2336. /* Initialize RIP routig table. */
  2337. rip->table = route_table_init ();
  2338. rip->route = route_table_init ();
  2339. rip->neighbor = route_table_init ();
  2340. /* Make output stream. */
  2341. rip->obuf = stream_new (1500);
  2342. /* Make socket. */
  2343. rip->sock = rip_create_socket (NULL);
  2344. if (rip->sock < 0)
  2345. return rip->sock;
  2346. /* Create read and timer thread. */
  2347. rip_event (RIP_READ, rip->sock);
  2348. rip_event (RIP_UPDATE_EVENT, 1);
  2349. return 0;
  2350. }
  2351. /* Sned RIP request to the destination. */
  2352. int
  2353. rip_request_send (struct sockaddr_in *to, struct interface *ifp,
  2354. u_char version, struct connected *connected)
  2355. {
  2356. struct rte *rte;
  2357. struct rip_packet rip_packet;
  2358. struct listnode *node, *nnode;
  2359. memset (&rip_packet, 0, sizeof (rip_packet));
  2360. rip_packet.command = RIP_REQUEST;
  2361. rip_packet.version = version;
  2362. rte = rip_packet.rte;
  2363. rte->metric = htonl (RIP_METRIC_INFINITY);
  2364. if (connected)
  2365. {
  2366. /*
  2367. * connected is only sent for ripv1 case, or when
  2368. * interface does not support multicast. Caller loops
  2369. * over each connected address for this case.
  2370. */
  2371. if (rip_send_packet ((u_char *) &rip_packet, sizeof (rip_packet),
  2372. to, connected) != sizeof (rip_packet))
  2373. return -1;
  2374. else
  2375. return sizeof (rip_packet);
  2376. }
  2377. /* send request on each connected network */
  2378. for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, connected))
  2379. {
  2380. struct prefix_ipv4 *p;
  2381. p = (struct prefix_ipv4 *) connected->address;
  2382. if (p->family != AF_INET)
  2383. continue;
  2384. if (rip_send_packet ((u_char *) &rip_packet, sizeof (rip_packet),
  2385. to, connected) != sizeof (rip_packet))
  2386. return -1;
  2387. }
  2388. return sizeof (rip_packet);
  2389. }
  2390. static int
  2391. rip_update_jitter (unsigned long time)
  2392. {
  2393. #define JITTER_BOUND 4
  2394. /* We want to get the jitter to +/- 1/JITTER_BOUND the interval.
  2395. Given that, we cannot let time be less than JITTER_BOUND seconds.
  2396. The RIPv2 RFC says jitter should be small compared to
  2397. update_time. We consider 1/JITTER_BOUND to be small.
  2398. */
  2399. int jitter_input = time;
  2400. int jitter;
  2401. if (jitter_input < JITTER_BOUND)
  2402. jitter_input = JITTER_BOUND;
  2403. jitter = (((rand () % ((jitter_input * 2) + 1)) - jitter_input));
  2404. return jitter/JITTER_BOUND;
  2405. }
  2406. void
  2407. rip_event (enum rip_event event, int sock)
  2408. {
  2409. int jitter = 0;
  2410. switch (event)
  2411. {
  2412. case RIP_READ:
  2413. rip->t_read = thread_add_read (master, rip_read, NULL, sock);
  2414. break;
  2415. case RIP_UPDATE_EVENT:
  2416. if (rip->t_update)
  2417. {
  2418. thread_cancel (rip->t_update);
  2419. rip->t_update = NULL;
  2420. }
  2421. jitter = rip_update_jitter (rip->update_time);
  2422. rip->t_update =
  2423. thread_add_timer (master, rip_update, NULL,
  2424. sock ? 2 : rip->update_time + jitter);
  2425. break;
  2426. case RIP_TRIGGERED_UPDATE:
  2427. if (rip->t_triggered_interval)
  2428. rip->trigger = 1;
  2429. else if (! rip->t_triggered_update)
  2430. rip->t_triggered_update =
  2431. thread_add_event (master, rip_triggered_update, NULL, 0);
  2432. break;
  2433. default:
  2434. break;
  2435. }
  2436. }
  2437. DEFUN (router_rip,
  2438. router_rip_cmd,
  2439. "router rip",
  2440. "Enable a routing process\n"
  2441. "Routing Information Protocol (RIP)\n")
  2442. {
  2443. int ret;
  2444. /* If rip is not enabled before. */
  2445. if (! rip)
  2446. {
  2447. ret = rip_create ();
  2448. if (ret < 0)
  2449. {
  2450. zlog_info ("Can't create RIP");
  2451. return CMD_WARNING;
  2452. }
  2453. }
  2454. vty->node = RIP_NODE;
  2455. vty->index = rip;
  2456. return CMD_SUCCESS;
  2457. }
  2458. DEFUN (no_router_rip,
  2459. no_router_rip_cmd,
  2460. "no router rip",
  2461. NO_STR
  2462. "Enable a routing process\n"
  2463. "Routing Information Protocol (RIP)\n")
  2464. {
  2465. if (rip)
  2466. rip_clean ();
  2467. return CMD_SUCCESS;
  2468. }
  2469. DEFUN (rip_version,
  2470. rip_version_cmd,
  2471. "version <1-2>",
  2472. "Set routing protocol version\n"
  2473. "version\n")
  2474. {
  2475. int version;
  2476. version = atoi (argv[0]);
  2477. if (version != RIPv1 && version != RIPv2)
  2478. {
  2479. vty_out (vty, "invalid rip version %d%s", version,
  2480. VTY_NEWLINE);
  2481. return CMD_WARNING;
  2482. }
  2483. rip->version_send = version;
  2484. rip->version_recv = version;
  2485. return CMD_SUCCESS;
  2486. }
  2487. DEFUN (no_rip_version,
  2488. no_rip_version_cmd,
  2489. "no version",
  2490. NO_STR
  2491. "Set routing protocol version\n")
  2492. {
  2493. /* Set RIP version to the default. */
  2494. rip->version_send = RI_RIP_VERSION_2;
  2495. rip->version_recv = RI_RIP_VERSION_1_AND_2;
  2496. return CMD_SUCCESS;
  2497. }
  2498. ALIAS (no_rip_version,
  2499. no_rip_version_val_cmd,
  2500. "no version <1-2>",
  2501. NO_STR
  2502. "Set routing protocol version\n"
  2503. "version\n")
  2504. DEFUN (rip_route,
  2505. rip_route_cmd,
  2506. "route A.B.C.D/M",
  2507. "RIP static route configuration\n"
  2508. "IP prefix <network>/<length>\n")
  2509. {
  2510. int ret;
  2511. struct prefix_ipv4 p;
  2512. struct route_node *node;
  2513. ret = str2prefix_ipv4 (argv[0], &p);
  2514. if (ret < 0)
  2515. {
  2516. vty_out (vty, "Malformed address%s", VTY_NEWLINE);
  2517. return CMD_WARNING;
  2518. }
  2519. apply_mask_ipv4 (&p);
  2520. /* For router rip configuration. */
  2521. node = route_node_get (rip->route, (struct prefix *) &p);
  2522. if (node->info)
  2523. {
  2524. vty_out (vty, "There is already same static route.%s", VTY_NEWLINE);
  2525. route_unlock_node (node);
  2526. return CMD_WARNING;
  2527. }
  2528. node->info = (char *)"static";
  2529. rip_redistribute_add (ZEBRA_ROUTE_RIP, RIP_ROUTE_STATIC, &p, 0, NULL, 0, 0);
  2530. return CMD_SUCCESS;
  2531. }
  2532. DEFUN (no_rip_route,
  2533. no_rip_route_cmd,
  2534. "no route A.B.C.D/M",
  2535. NO_STR
  2536. "RIP static route configuration\n"
  2537. "IP prefix <network>/<length>\n")
  2538. {
  2539. int ret;
  2540. struct prefix_ipv4 p;
  2541. struct route_node *node;
  2542. ret = str2prefix_ipv4 (argv[0], &p);
  2543. if (ret < 0)
  2544. {
  2545. vty_out (vty, "Malformed address%s", VTY_NEWLINE);
  2546. return CMD_WARNING;
  2547. }
  2548. apply_mask_ipv4 (&p);
  2549. /* For router rip configuration. */
  2550. node = route_node_lookup (rip->route, (struct prefix *) &p);
  2551. if (! node)
  2552. {
  2553. vty_out (vty, "Can't find route %s.%s", argv[0],
  2554. VTY_NEWLINE);
  2555. return CMD_WARNING;
  2556. }
  2557. rip_redistribute_delete (ZEBRA_ROUTE_RIP, RIP_ROUTE_STATIC, &p, 0);
  2558. route_unlock_node (node);
  2559. node->info = NULL;
  2560. route_unlock_node (node);
  2561. return CMD_SUCCESS;
  2562. }
  2563. static void
  2564. rip_update_default_metric ()
  2565. {
  2566. struct route_node *np;
  2567. struct rip_info *rinfo;
  2568. for (np = route_top (rip->table); np; np = route_next (np))
  2569. if ((rinfo = np->info) != NULL)
  2570. if (rinfo->type != ZEBRA_ROUTE_RIP && rinfo->type != ZEBRA_ROUTE_CONNECT)
  2571. rinfo->metric = rip->default_metric;
  2572. }
  2573. DEFUN (rip_default_metric,
  2574. rip_default_metric_cmd,
  2575. "default-metric <1-16>",
  2576. "Set a metric of redistribute routes\n"
  2577. "Default metric\n")
  2578. {
  2579. if (rip)
  2580. {
  2581. rip->default_metric = atoi (argv[0]);
  2582. /* rip_update_default_metric (); */
  2583. }
  2584. return CMD_SUCCESS;
  2585. }
  2586. DEFUN (no_rip_default_metric,
  2587. no_rip_default_metric_cmd,
  2588. "no default-metric",
  2589. NO_STR
  2590. "Set a metric of redistribute routes\n"
  2591. "Default metric\n")
  2592. {
  2593. if (rip)
  2594. {
  2595. rip->default_metric = RIP_DEFAULT_METRIC_DEFAULT;
  2596. /* rip_update_default_metric (); */
  2597. }
  2598. return CMD_SUCCESS;
  2599. }
  2600. ALIAS (no_rip_default_metric,
  2601. no_rip_default_metric_val_cmd,
  2602. "no default-metric <1-16>",
  2603. NO_STR
  2604. "Set a metric of redistribute routes\n"
  2605. "Default metric\n")
  2606. DEFUN (rip_timers,
  2607. rip_timers_cmd,
  2608. "timers basic <5-2147483647> <5-2147483647> <5-2147483647>",
  2609. "Adjust routing timers\n"
  2610. "Basic routing protocol update timers\n"
  2611. "Routing table update timer value in second. Default is 30.\n"
  2612. "Routing information timeout timer. Default is 180.\n"
  2613. "Garbage collection timer. Default is 120.\n")
  2614. {
  2615. unsigned long update;
  2616. unsigned long timeout;
  2617. unsigned long garbage;
  2618. char *endptr = NULL;
  2619. unsigned long RIP_TIMER_MAX = 2147483647;
  2620. unsigned long RIP_TIMER_MIN = 5;
  2621. update = strtoul (argv[0], &endptr, 10);
  2622. if (update > RIP_TIMER_MAX || update < RIP_TIMER_MIN || *endptr != '\0')
  2623. {
  2624. vty_out (vty, "update timer value error%s", VTY_NEWLINE);
  2625. return CMD_WARNING;
  2626. }
  2627. timeout = strtoul (argv[1], &endptr, 10);
  2628. if (timeout > RIP_TIMER_MAX || timeout < RIP_TIMER_MIN || *endptr != '\0')
  2629. {
  2630. vty_out (vty, "timeout timer value error%s", VTY_NEWLINE);
  2631. return CMD_WARNING;
  2632. }
  2633. garbage = strtoul (argv[2], &endptr, 10);
  2634. if (garbage > RIP_TIMER_MAX || garbage < RIP_TIMER_MIN || *endptr != '\0')
  2635. {
  2636. vty_out (vty, "garbage timer value error%s", VTY_NEWLINE);
  2637. return CMD_WARNING;
  2638. }
  2639. /* Set each timer value. */
  2640. rip->update_time = update;
  2641. rip->timeout_time = timeout;
  2642. rip->garbage_time = garbage;
  2643. /* Reset update timer thread. */
  2644. rip_event (RIP_UPDATE_EVENT, 0);
  2645. return CMD_SUCCESS;
  2646. }
  2647. DEFUN (no_rip_timers,
  2648. no_rip_timers_cmd,
  2649. "no timers basic",
  2650. NO_STR
  2651. "Adjust routing timers\n"
  2652. "Basic routing protocol update timers\n")
  2653. {
  2654. /* Set each timer value to the default. */
  2655. rip->update_time = RIP_UPDATE_TIMER_DEFAULT;
  2656. rip->timeout_time = RIP_TIMEOUT_TIMER_DEFAULT;
  2657. rip->garbage_time = RIP_GARBAGE_TIMER_DEFAULT;
  2658. /* Reset update timer thread. */
  2659. rip_event (RIP_UPDATE_EVENT, 0);
  2660. return CMD_SUCCESS;
  2661. }
  2662. ALIAS (no_rip_timers,
  2663. no_rip_timers_val_cmd,
  2664. "no timers basic <0-65535> <0-65535> <0-65535>",
  2665. NO_STR
  2666. "Adjust routing timers\n"
  2667. "Basic routing protocol update timers\n"
  2668. "Routing table update timer value in second. Default is 30.\n"
  2669. "Routing information timeout timer. Default is 180.\n"
  2670. "Garbage collection timer. Default is 120.\n")
  2671. struct route_table *rip_distance_table;
  2672. struct rip_distance
  2673. {
  2674. /* Distance value for the IP source prefix. */
  2675. u_char distance;
  2676. /* Name of the access-list to be matched. */
  2677. char *access_list;
  2678. };
  2679. static struct rip_distance *
  2680. rip_distance_new ()
  2681. {
  2682. struct rip_distance *new;
  2683. new = XMALLOC (MTYPE_RIP_DISTANCE, sizeof (struct rip_distance));
  2684. memset (new, 0, sizeof (struct rip_distance));
  2685. return new;
  2686. }
  2687. static void
  2688. rip_distance_free (struct rip_distance *rdistance)
  2689. {
  2690. XFREE (MTYPE_RIP_DISTANCE, rdistance);
  2691. }
  2692. static int
  2693. rip_distance_set (struct vty *vty, const char *distance_str, const char *ip_str,
  2694. const char *access_list_str)
  2695. {
  2696. int ret;
  2697. struct prefix_ipv4 p;
  2698. u_char distance;
  2699. struct route_node *rn;
  2700. struct rip_distance *rdistance;
  2701. ret = str2prefix_ipv4 (ip_str, &p);
  2702. if (ret == 0)
  2703. {
  2704. vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
  2705. return CMD_WARNING;
  2706. }
  2707. distance = atoi (distance_str);
  2708. /* Get RIP distance node. */
  2709. rn = route_node_get (rip_distance_table, (struct prefix *) &p);
  2710. if (rn->info)
  2711. {
  2712. rdistance = rn->info;
  2713. route_unlock_node (rn);
  2714. }
  2715. else
  2716. {
  2717. rdistance = rip_distance_new ();
  2718. rn->info = rdistance;
  2719. }
  2720. /* Set distance value. */
  2721. rdistance->distance = distance;
  2722. /* Reset access-list configuration. */
  2723. if (rdistance->access_list)
  2724. {
  2725. free (rdistance->access_list);
  2726. rdistance->access_list = NULL;
  2727. }
  2728. if (access_list_str)
  2729. rdistance->access_list = strdup (access_list_str);
  2730. return CMD_SUCCESS;
  2731. }
  2732. static int
  2733. rip_distance_unset (struct vty *vty, const char *distance_str,
  2734. const char *ip_str, const char *access_list_str)
  2735. {
  2736. int ret;
  2737. struct prefix_ipv4 p;
  2738. u_char distance;
  2739. struct route_node *rn;
  2740. struct rip_distance *rdistance;
  2741. ret = str2prefix_ipv4 (ip_str, &p);
  2742. if (ret == 0)
  2743. {
  2744. vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
  2745. return CMD_WARNING;
  2746. }
  2747. distance = atoi (distance_str);
  2748. rn = route_node_lookup (rip_distance_table, (struct prefix *)&p);
  2749. if (! rn)
  2750. {
  2751. vty_out (vty, "Can't find specified prefix%s", VTY_NEWLINE);
  2752. return CMD_WARNING;
  2753. }
  2754. rdistance = rn->info;
  2755. if (rdistance->access_list)
  2756. free (rdistance->access_list);
  2757. rip_distance_free (rdistance);
  2758. rn->info = NULL;
  2759. route_unlock_node (rn);
  2760. route_unlock_node (rn);
  2761. return CMD_SUCCESS;
  2762. }
  2763. static void
  2764. rip_distance_reset ()
  2765. {
  2766. struct route_node *rn;
  2767. struct rip_distance *rdistance;
  2768. for (rn = route_top (rip_distance_table); rn; rn = route_next (rn))
  2769. if ((rdistance = rn->info) != NULL)
  2770. {
  2771. if (rdistance->access_list)
  2772. free (rdistance->access_list);
  2773. rip_distance_free (rdistance);
  2774. rn->info = NULL;
  2775. route_unlock_node (rn);
  2776. }
  2777. }
  2778. /* Apply RIP information to distance method. */
  2779. u_char
  2780. rip_distance_apply (struct rip_info *rinfo)
  2781. {
  2782. struct route_node *rn;
  2783. struct prefix_ipv4 p;
  2784. struct rip_distance *rdistance;
  2785. struct access_list *alist;
  2786. if (! rip)
  2787. return 0;
  2788. memset (&p, 0, sizeof (struct prefix_ipv4));
  2789. p.family = AF_INET;
  2790. p.prefix = rinfo->from;
  2791. p.prefixlen = IPV4_MAX_BITLEN;
  2792. /* Check source address. */
  2793. rn = route_node_match (rip_distance_table, (struct prefix *) &p);
  2794. if (rn)
  2795. {
  2796. rdistance = rn->info;
  2797. route_unlock_node (rn);
  2798. if (rdistance->access_list)
  2799. {
  2800. alist = access_list_lookup (AFI_IP, rdistance->access_list);
  2801. if (alist == NULL)
  2802. return 0;
  2803. if (access_list_apply (alist, &rinfo->rp->p) == FILTER_DENY)
  2804. return 0;
  2805. return rdistance->distance;
  2806. }
  2807. else
  2808. return rdistance->distance;
  2809. }
  2810. if (rip->distance)
  2811. return rip->distance;
  2812. return 0;
  2813. }
  2814. static void
  2815. rip_distance_show (struct vty *vty)
  2816. {
  2817. struct route_node *rn;
  2818. struct rip_distance *rdistance;
  2819. int header = 1;
  2820. char buf[BUFSIZ];
  2821. vty_out (vty, " Distance: (default is %d)%s",
  2822. rip->distance ? rip->distance :ZEBRA_RIP_DISTANCE_DEFAULT,
  2823. VTY_NEWLINE);
  2824. for (rn = route_top (rip_distance_table); rn; rn = route_next (rn))
  2825. if ((rdistance = rn->info) != NULL)
  2826. {
  2827. if (header)
  2828. {
  2829. vty_out (vty, " Address Distance List%s",
  2830. VTY_NEWLINE);
  2831. header = 0;
  2832. }
  2833. sprintf (buf, "%s/%d", inet_ntoa (rn->p.u.prefix4), rn->p.prefixlen);
  2834. vty_out (vty, " %-20s %4d %s%s",
  2835. buf, rdistance->distance,
  2836. rdistance->access_list ? rdistance->access_list : "",
  2837. VTY_NEWLINE);
  2838. }
  2839. }
  2840. DEFUN (rip_distance,
  2841. rip_distance_cmd,
  2842. "distance <1-255>",
  2843. "Administrative distance\n"
  2844. "Distance value\n")
  2845. {
  2846. rip->distance = atoi (argv[0]);
  2847. return CMD_SUCCESS;
  2848. }
  2849. DEFUN (no_rip_distance,
  2850. no_rip_distance_cmd,
  2851. "no distance <1-255>",
  2852. NO_STR
  2853. "Administrative distance\n"
  2854. "Distance value\n")
  2855. {
  2856. rip->distance = 0;
  2857. return CMD_SUCCESS;
  2858. }
  2859. DEFUN (rip_distance_source,
  2860. rip_distance_source_cmd,
  2861. "distance <1-255> A.B.C.D/M",
  2862. "Administrative distance\n"
  2863. "Distance value\n"
  2864. "IP source prefix\n")
  2865. {
  2866. rip_distance_set (vty, argv[0], argv[1], NULL);
  2867. return CMD_SUCCESS;
  2868. }
  2869. DEFUN (no_rip_distance_source,
  2870. no_rip_distance_source_cmd,
  2871. "no distance <1-255> A.B.C.D/M",
  2872. NO_STR
  2873. "Administrative distance\n"
  2874. "Distance value\n"
  2875. "IP source prefix\n")
  2876. {
  2877. rip_distance_unset (vty, argv[0], argv[1], NULL);
  2878. return CMD_SUCCESS;
  2879. }
  2880. DEFUN (rip_distance_source_access_list,
  2881. rip_distance_source_access_list_cmd,
  2882. "distance <1-255> A.B.C.D/M WORD",
  2883. "Administrative distance\n"
  2884. "Distance value\n"
  2885. "IP source prefix\n"
  2886. "Access list name\n")
  2887. {
  2888. rip_distance_set (vty, argv[0], argv[1], argv[2]);
  2889. return CMD_SUCCESS;
  2890. }
  2891. DEFUN (no_rip_distance_source_access_list,
  2892. no_rip_distance_source_access_list_cmd,
  2893. "no distance <1-255> A.B.C.D/M WORD",
  2894. NO_STR
  2895. "Administrative distance\n"
  2896. "Distance value\n"
  2897. "IP source prefix\n"
  2898. "Access list name\n")
  2899. {
  2900. rip_distance_unset (vty, argv[0], argv[1], argv[2]);
  2901. return CMD_SUCCESS;
  2902. }
  2903. /* Print out routes update time. */
  2904. static void
  2905. rip_vty_out_uptime (struct vty *vty, struct rip_info *rinfo)
  2906. {
  2907. struct timeval timer_now;
  2908. time_t clock;
  2909. struct tm *tm;
  2910. #define TIME_BUF 25
  2911. char timebuf [TIME_BUF];
  2912. struct thread *thread;
  2913. gettimeofday (&timer_now, NULL);
  2914. if ((thread = rinfo->t_timeout) != NULL)
  2915. {
  2916. clock = thread->u.sands.tv_sec - timer_now.tv_sec;
  2917. tm = gmtime (&clock);
  2918. strftime (timebuf, TIME_BUF, "%M:%S", tm);
  2919. vty_out (vty, "%5s", timebuf);
  2920. }
  2921. else if ((thread = rinfo->t_garbage_collect) != NULL)
  2922. {
  2923. clock = thread->u.sands.tv_sec - timer_now.tv_sec;
  2924. tm = gmtime (&clock);
  2925. strftime (timebuf, TIME_BUF, "%M:%S", tm);
  2926. vty_out (vty, "%5s", timebuf);
  2927. }
  2928. }
  2929. static const char *
  2930. rip_route_type_print (int sub_type)
  2931. {
  2932. switch (sub_type)
  2933. {
  2934. case RIP_ROUTE_RTE:
  2935. return "n";
  2936. case RIP_ROUTE_STATIC:
  2937. return "s";
  2938. case RIP_ROUTE_DEFAULT:
  2939. return "d";
  2940. case RIP_ROUTE_REDISTRIBUTE:
  2941. return "r";
  2942. case RIP_ROUTE_INTERFACE:
  2943. return "i";
  2944. default:
  2945. return "?";
  2946. }
  2947. }
  2948. DEFUN (show_ip_rip,
  2949. show_ip_rip_cmd,
  2950. "show ip rip",
  2951. SHOW_STR
  2952. IP_STR
  2953. "Show RIP routes\n")
  2954. {
  2955. struct route_node *np;
  2956. struct rip_info *rinfo;
  2957. if (! rip)
  2958. return CMD_SUCCESS;
  2959. vty_out (vty, "Codes: R - RIP, C - connected, S - Static, O - OSPF, B - BGP%s"
  2960. "Sub-codes:%s"
  2961. " (n) - normal, (s) - static, (d) - default, (r) - redistribute,%s"
  2962. " (i) - interface%s%s"
  2963. " Network Next Hop Metric From Tag Time%s",
  2964. VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
  2965. for (np = route_top (rip->table); np; np = route_next (np))
  2966. if ((rinfo = np->info) != NULL)
  2967. {
  2968. int len;
  2969. len = vty_out (vty, "%c(%s) %s/%d",
  2970. /* np->lock, For debugging. */
  2971. zebra_route_char(rinfo->type),
  2972. rip_route_type_print (rinfo->sub_type),
  2973. inet_ntoa (np->p.u.prefix4), np->p.prefixlen);
  2974. len = 24 - len;
  2975. if (len > 0)
  2976. vty_out (vty, "%*s", len, " ");
  2977. if (rinfo->nexthop.s_addr)
  2978. vty_out (vty, "%-20s %2d ", inet_ntoa (rinfo->nexthop),
  2979. rinfo->metric);
  2980. else
  2981. vty_out (vty, "0.0.0.0 %2d ", rinfo->metric);
  2982. /* Route which exist in kernel routing table. */
  2983. if ((rinfo->type == ZEBRA_ROUTE_RIP) &&
  2984. (rinfo->sub_type == RIP_ROUTE_RTE))
  2985. {
  2986. vty_out (vty, "%-15s ", inet_ntoa (rinfo->from));
  2987. vty_out (vty, "%3d ", rinfo->tag);
  2988. rip_vty_out_uptime (vty, rinfo);
  2989. }
  2990. else if (rinfo->metric == RIP_METRIC_INFINITY)
  2991. {
  2992. vty_out (vty, "self ");
  2993. vty_out (vty, "%3d ", rinfo->tag);
  2994. rip_vty_out_uptime (vty, rinfo);
  2995. }
  2996. else
  2997. {
  2998. if (rinfo->external_metric)
  2999. {
  3000. len = vty_out (vty, "self (%s:%d)",
  3001. zebra_route_string(rinfo->type),
  3002. rinfo->external_metric);
  3003. len = 16 - len;
  3004. if (len > 0)
  3005. vty_out (vty, "%*s", len, " ");
  3006. }
  3007. else
  3008. vty_out (vty, "self ");
  3009. vty_out (vty, "%3d", rinfo->tag);
  3010. }
  3011. vty_out (vty, "%s", VTY_NEWLINE);
  3012. }
  3013. return CMD_SUCCESS;
  3014. }
  3015. /* Return next event time. */
  3016. static int
  3017. rip_next_thread_timer (struct thread *thread)
  3018. {
  3019. struct timeval timer_now;
  3020. gettimeofday (&timer_now, NULL);
  3021. return thread->u.sands.tv_sec - timer_now.tv_sec;
  3022. }
  3023. /* Vincent: formerly, it was show_ip_protocols_rip: "show ip protocols" */
  3024. DEFUN (show_ip_rip_status,
  3025. show_ip_rip_status_cmd,
  3026. "show ip rip status",
  3027. SHOW_STR
  3028. IP_STR
  3029. "Show RIP routes\n"
  3030. "IP routing protocol process parameters and statistics\n")
  3031. {
  3032. struct listnode *node;
  3033. struct interface *ifp;
  3034. struct rip_interface *ri;
  3035. extern struct message ri_version_msg[];
  3036. const char *send_version;
  3037. const char *receive_version;
  3038. if (! rip)
  3039. return CMD_SUCCESS;
  3040. vty_out (vty, "Routing Protocol is \"rip\"%s", VTY_NEWLINE);
  3041. vty_out (vty, " Sending updates every %ld seconds with +/-50%%,",
  3042. rip->update_time);
  3043. vty_out (vty, " next due in %d seconds%s",
  3044. rip_next_thread_timer (rip->t_update),
  3045. VTY_NEWLINE);
  3046. vty_out (vty, " Timeout after %ld seconds,", rip->timeout_time);
  3047. vty_out (vty, " garbage collect after %ld seconds%s", rip->garbage_time,
  3048. VTY_NEWLINE);
  3049. /* Filtering status show. */
  3050. config_show_distribute (vty);
  3051. /* Default metric information. */
  3052. vty_out (vty, " Default redistribution metric is %d%s",
  3053. rip->default_metric, VTY_NEWLINE);
  3054. /* Redistribute information. */
  3055. vty_out (vty, " Redistributing:");
  3056. config_write_rip_redistribute (vty, 0);
  3057. vty_out (vty, "%s", VTY_NEWLINE);
  3058. vty_out (vty, " Default version control: send version %s,",
  3059. lookup(ri_version_msg,rip->version_send));
  3060. if (rip->version_recv == RI_RIP_VERSION_1_AND_2)
  3061. vty_out (vty, " receive any version %s", VTY_NEWLINE);
  3062. else
  3063. vty_out (vty, " receive version %s %s",
  3064. lookup(ri_version_msg,rip->version_recv), VTY_NEWLINE);
  3065. vty_out (vty, " Interface Send Recv Key-chain%s", VTY_NEWLINE);
  3066. for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
  3067. {
  3068. ri = ifp->info;
  3069. if (ri->enable_network || ri->enable_interface)
  3070. {
  3071. if (ri->ri_send == RI_RIP_UNSPEC)
  3072. send_version = lookup (ri_version_msg, rip->version_send);
  3073. else
  3074. send_version = lookup (ri_version_msg, ri->ri_send);
  3075. if (ri->ri_receive == RI_RIP_UNSPEC)
  3076. receive_version = lookup (ri_version_msg, rip->version_recv);
  3077. else
  3078. receive_version = lookup (ri_version_msg, ri->ri_receive);
  3079. vty_out (vty, " %-17s%-3s %-3s %s%s", ifp->name,
  3080. send_version,
  3081. receive_version,
  3082. ri->key_chain ? ri->key_chain : "",
  3083. VTY_NEWLINE);
  3084. }
  3085. }
  3086. vty_out (vty, " Routing for Networks:%s", VTY_NEWLINE);
  3087. config_write_rip_network (vty, 0);
  3088. {
  3089. int found_passive = 0;
  3090. for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
  3091. {
  3092. ri = ifp->info;
  3093. if ((ri->enable_network || ri->enable_interface) && ri->passive)
  3094. {
  3095. if (!found_passive)
  3096. {
  3097. vty_out (vty, " Passive Interface(s):%s", VTY_NEWLINE);
  3098. found_passive = 1;
  3099. }
  3100. vty_out (vty, " %s%s", ifp->name, VTY_NEWLINE);
  3101. }
  3102. }
  3103. }
  3104. vty_out (vty, " Routing Information Sources:%s", VTY_NEWLINE);
  3105. vty_out (vty, " Gateway BadPackets BadRoutes Distance Last Update%s", VTY_NEWLINE);
  3106. rip_peer_display (vty);
  3107. rip_distance_show (vty);
  3108. return CMD_SUCCESS;
  3109. }
  3110. /* RIP configuration write function. */
  3111. static int
  3112. config_write_rip (struct vty *vty)
  3113. {
  3114. int write = 0;
  3115. struct route_node *rn;
  3116. struct rip_distance *rdistance;
  3117. if (rip)
  3118. {
  3119. /* Router RIP statement. */
  3120. vty_out (vty, "router rip%s", VTY_NEWLINE);
  3121. write++;
  3122. /* RIP version statement. Default is RIP version 2. */
  3123. if (rip->version_send != RI_RIP_VERSION_2
  3124. || rip->version_recv != RI_RIP_VERSION_1_AND_2)
  3125. vty_out (vty, " version %d%s", rip->version_send,
  3126. VTY_NEWLINE);
  3127. /* RIP timer configuration. */
  3128. if (rip->update_time != RIP_UPDATE_TIMER_DEFAULT
  3129. || rip->timeout_time != RIP_TIMEOUT_TIMER_DEFAULT
  3130. || rip->garbage_time != RIP_GARBAGE_TIMER_DEFAULT)
  3131. vty_out (vty, " timers basic %lu %lu %lu%s",
  3132. rip->update_time,
  3133. rip->timeout_time,
  3134. rip->garbage_time,
  3135. VTY_NEWLINE);
  3136. /* Default information configuration. */
  3137. if (rip->default_information)
  3138. {
  3139. if (rip->default_information_route_map)
  3140. vty_out (vty, " default-information originate route-map %s%s",
  3141. rip->default_information_route_map, VTY_NEWLINE);
  3142. else
  3143. vty_out (vty, " default-information originate%s",
  3144. VTY_NEWLINE);
  3145. }
  3146. /* Redistribute configuration. */
  3147. config_write_rip_redistribute (vty, 1);
  3148. /* RIP offset-list configuration. */
  3149. config_write_rip_offset_list (vty);
  3150. /* RIP enabled network and interface configuration. */
  3151. config_write_rip_network (vty, 1);
  3152. /* RIP default metric configuration */
  3153. if (rip->default_metric != RIP_DEFAULT_METRIC_DEFAULT)
  3154. vty_out (vty, " default-metric %d%s",
  3155. rip->default_metric, VTY_NEWLINE);
  3156. /* Distribute configuration. */
  3157. write += config_write_distribute (vty);
  3158. /* Interface routemap configuration */
  3159. write += config_write_if_rmap (vty);
  3160. /* Distance configuration. */
  3161. if (rip->distance)
  3162. vty_out (vty, " distance %d%s", rip->distance, VTY_NEWLINE);
  3163. /* RIP source IP prefix distance configuration. */
  3164. for (rn = route_top (rip_distance_table); rn; rn = route_next (rn))
  3165. if ((rdistance = rn->info) != NULL)
  3166. vty_out (vty, " distance %d %s/%d %s%s", rdistance->distance,
  3167. inet_ntoa (rn->p.u.prefix4), rn->p.prefixlen,
  3168. rdistance->access_list ? rdistance->access_list : "",
  3169. VTY_NEWLINE);
  3170. /* RIP static route configuration. */
  3171. for (rn = route_top (rip->route); rn; rn = route_next (rn))
  3172. if (rn->info)
  3173. vty_out (vty, " route %s/%d%s",
  3174. inet_ntoa (rn->p.u.prefix4),
  3175. rn->p.prefixlen,
  3176. VTY_NEWLINE);
  3177. }
  3178. return write;
  3179. }
  3180. /* RIP node structure. */
  3181. struct cmd_node rip_node =
  3182. {
  3183. RIP_NODE,
  3184. "%s(config-router)# ",
  3185. 1
  3186. };
  3187. /* Distribute-list update functions. */
  3188. static void
  3189. rip_distribute_update (struct distribute *dist)
  3190. {
  3191. struct interface *ifp;
  3192. struct rip_interface *ri;
  3193. struct access_list *alist;
  3194. struct prefix_list *plist;
  3195. if (! dist->ifname)
  3196. return;
  3197. ifp = if_lookup_by_name (dist->ifname);
  3198. if (ifp == NULL)
  3199. return;
  3200. ri = ifp->info;
  3201. if (dist->list[DISTRIBUTE_IN])
  3202. {
  3203. alist = access_list_lookup (AFI_IP, dist->list[DISTRIBUTE_IN]);
  3204. if (alist)
  3205. ri->list[RIP_FILTER_IN] = alist;
  3206. else
  3207. ri->list[RIP_FILTER_IN] = NULL;
  3208. }
  3209. else
  3210. ri->list[RIP_FILTER_IN] = NULL;
  3211. if (dist->list[DISTRIBUTE_OUT])
  3212. {
  3213. alist = access_list_lookup (AFI_IP, dist->list[DISTRIBUTE_OUT]);
  3214. if (alist)
  3215. ri->list[RIP_FILTER_OUT] = alist;
  3216. else
  3217. ri->list[RIP_FILTER_OUT] = NULL;
  3218. }
  3219. else
  3220. ri->list[RIP_FILTER_OUT] = NULL;
  3221. if (dist->prefix[DISTRIBUTE_IN])
  3222. {
  3223. plist = prefix_list_lookup (AFI_IP, dist->prefix[DISTRIBUTE_IN]);
  3224. if (plist)
  3225. ri->prefix[RIP_FILTER_IN] = plist;
  3226. else
  3227. ri->prefix[RIP_FILTER_IN] = NULL;
  3228. }
  3229. else
  3230. ri->prefix[RIP_FILTER_IN] = NULL;
  3231. if (dist->prefix[DISTRIBUTE_OUT])
  3232. {
  3233. plist = prefix_list_lookup (AFI_IP, dist->prefix[DISTRIBUTE_OUT]);
  3234. if (plist)
  3235. ri->prefix[RIP_FILTER_OUT] = plist;
  3236. else
  3237. ri->prefix[RIP_FILTER_OUT] = NULL;
  3238. }
  3239. else
  3240. ri->prefix[RIP_FILTER_OUT] = NULL;
  3241. }
  3242. void
  3243. rip_distribute_update_interface (struct interface *ifp)
  3244. {
  3245. struct distribute *dist;
  3246. dist = distribute_lookup (ifp->name);
  3247. if (dist)
  3248. rip_distribute_update (dist);
  3249. }
  3250. /* Update all interface's distribute list. */
  3251. /* ARGSUSED */
  3252. static void
  3253. rip_distribute_update_all (struct prefix_list *notused)
  3254. {
  3255. struct interface *ifp;
  3256. struct listnode *node, *nnode;
  3257. for (ALL_LIST_ELEMENTS (iflist, node, nnode, ifp))
  3258. rip_distribute_update_interface (ifp);
  3259. }
  3260. /* ARGSUSED */
  3261. static void
  3262. rip_distribute_update_all_wrapper(struct access_list *notused)
  3263. {
  3264. rip_distribute_update_all(NULL);
  3265. }
  3266. /* Delete all added rip route. */
  3267. void
  3268. rip_clean ()
  3269. {
  3270. int i;
  3271. struct route_node *rp;
  3272. struct rip_info *rinfo;
  3273. if (rip)
  3274. {
  3275. /* Clear RIP routes */
  3276. for (rp = route_top (rip->table); rp; rp = route_next (rp))
  3277. if ((rinfo = rp->info) != NULL)
  3278. {
  3279. if (rinfo->type == ZEBRA_ROUTE_RIP &&
  3280. rinfo->sub_type == RIP_ROUTE_RTE)
  3281. rip_zebra_ipv4_delete ((struct prefix_ipv4 *)&rp->p,
  3282. &rinfo->nexthop, rinfo->metric);
  3283. RIP_TIMER_OFF (rinfo->t_timeout);
  3284. RIP_TIMER_OFF (rinfo->t_garbage_collect);
  3285. rp->info = NULL;
  3286. route_unlock_node (rp);
  3287. rip_info_free (rinfo);
  3288. }
  3289. /* Cancel RIP related timers. */
  3290. RIP_TIMER_OFF (rip->t_update);
  3291. RIP_TIMER_OFF (rip->t_triggered_update);
  3292. RIP_TIMER_OFF (rip->t_triggered_interval);
  3293. /* Cancel read thread. */
  3294. if (rip->t_read)
  3295. {
  3296. thread_cancel (rip->t_read);
  3297. rip->t_read = NULL;
  3298. }
  3299. /* Close RIP socket. */
  3300. if (rip->sock >= 0)
  3301. {
  3302. close (rip->sock);
  3303. rip->sock = -1;
  3304. }
  3305. /* Static RIP route configuration. */
  3306. for (rp = route_top (rip->route); rp; rp = route_next (rp))
  3307. if (rp->info)
  3308. {
  3309. rp->info = NULL;
  3310. route_unlock_node (rp);
  3311. }
  3312. /* RIP neighbor configuration. */
  3313. for (rp = route_top (rip->neighbor); rp; rp = route_next (rp))
  3314. if (rp->info)
  3315. {
  3316. rp->info = NULL;
  3317. route_unlock_node (rp);
  3318. }
  3319. /* Redistribute related clear. */
  3320. if (rip->default_information_route_map)
  3321. free (rip->default_information_route_map);
  3322. for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
  3323. if (rip->route_map[i].name)
  3324. free (rip->route_map[i].name);
  3325. XFREE (MTYPE_ROUTE_TABLE, rip->table);
  3326. XFREE (MTYPE_ROUTE_TABLE, rip->route);
  3327. XFREE (MTYPE_ROUTE_TABLE, rip->neighbor);
  3328. XFREE (MTYPE_RIP, rip);
  3329. rip = NULL;
  3330. }
  3331. rip_clean_network ();
  3332. rip_passive_nondefault_clean ();
  3333. rip_offset_clean ();
  3334. rip_interface_clean ();
  3335. rip_distance_reset ();
  3336. rip_redistribute_clean ();
  3337. }
  3338. /* Reset all values to the default settings. */
  3339. void
  3340. rip_reset ()
  3341. {
  3342. /* Reset global counters. */
  3343. rip_global_route_changes = 0;
  3344. rip_global_queries = 0;
  3345. /* Call ripd related reset functions. */
  3346. rip_debug_reset ();
  3347. rip_route_map_reset ();
  3348. /* Call library reset functions. */
  3349. vty_reset ();
  3350. access_list_reset ();
  3351. prefix_list_reset ();
  3352. distribute_list_reset ();
  3353. rip_interface_reset ();
  3354. rip_distance_reset ();
  3355. rip_zclient_reset ();
  3356. }
  3357. static void
  3358. rip_if_rmap_update (struct if_rmap *if_rmap)
  3359. {
  3360. struct interface *ifp;
  3361. struct rip_interface *ri;
  3362. struct route_map *rmap;
  3363. ifp = if_lookup_by_name (if_rmap->ifname);
  3364. if (ifp == NULL)
  3365. return;
  3366. ri = ifp->info;
  3367. if (if_rmap->routemap[IF_RMAP_IN])
  3368. {
  3369. rmap = route_map_lookup_by_name (if_rmap->routemap[IF_RMAP_IN]);
  3370. if (rmap)
  3371. ri->routemap[IF_RMAP_IN] = rmap;
  3372. else
  3373. ri->routemap[IF_RMAP_IN] = NULL;
  3374. }
  3375. else
  3376. ri->routemap[RIP_FILTER_IN] = NULL;
  3377. if (if_rmap->routemap[IF_RMAP_OUT])
  3378. {
  3379. rmap = route_map_lookup_by_name (if_rmap->routemap[IF_RMAP_OUT]);
  3380. if (rmap)
  3381. ri->routemap[IF_RMAP_OUT] = rmap;
  3382. else
  3383. ri->routemap[IF_RMAP_OUT] = NULL;
  3384. }
  3385. else
  3386. ri->routemap[RIP_FILTER_OUT] = NULL;
  3387. }
  3388. void
  3389. rip_if_rmap_update_interface (struct interface *ifp)
  3390. {
  3391. struct if_rmap *if_rmap;
  3392. if_rmap = if_rmap_lookup (ifp->name);
  3393. if (if_rmap)
  3394. rip_if_rmap_update (if_rmap);
  3395. }
  3396. static void
  3397. rip_routemap_update_redistribute (void)
  3398. {
  3399. int i;
  3400. if (rip)
  3401. {
  3402. for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
  3403. {
  3404. if (rip->route_map[i].name)
  3405. rip->route_map[i].map =
  3406. route_map_lookup_by_name (rip->route_map[i].name);
  3407. }
  3408. }
  3409. }
  3410. /* ARGSUSED */
  3411. static void
  3412. rip_routemap_update (const char *notused)
  3413. {
  3414. struct interface *ifp;
  3415. struct listnode *node, *nnode;
  3416. for (ALL_LIST_ELEMENTS (iflist, node, nnode, ifp))
  3417. rip_if_rmap_update_interface (ifp);
  3418. rip_routemap_update_redistribute ();
  3419. }
  3420. /* Allocate new rip structure and set default value. */
  3421. void
  3422. rip_init (void)
  3423. {
  3424. /* Randomize for triggered update random(). */
  3425. srand (time (NULL));
  3426. /* Install top nodes. */
  3427. install_node (&rip_node, config_write_rip);
  3428. /* Install rip commands. */
  3429. install_element (VIEW_NODE, &show_ip_rip_cmd);
  3430. install_element (VIEW_NODE, &show_ip_rip_status_cmd);
  3431. install_element (ENABLE_NODE, &show_ip_rip_cmd);
  3432. install_element (ENABLE_NODE, &show_ip_rip_status_cmd);
  3433. install_element (CONFIG_NODE, &router_rip_cmd);
  3434. install_element (CONFIG_NODE, &no_router_rip_cmd);
  3435. install_default (RIP_NODE);
  3436. install_element (RIP_NODE, &rip_version_cmd);
  3437. install_element (RIP_NODE, &no_rip_version_cmd);
  3438. install_element (RIP_NODE, &no_rip_version_val_cmd);
  3439. install_element (RIP_NODE, &rip_default_metric_cmd);
  3440. install_element (RIP_NODE, &no_rip_default_metric_cmd);
  3441. install_element (RIP_NODE, &no_rip_default_metric_val_cmd);
  3442. install_element (RIP_NODE, &rip_timers_cmd);
  3443. install_element (RIP_NODE, &no_rip_timers_cmd);
  3444. install_element (RIP_NODE, &no_rip_timers_val_cmd);
  3445. install_element (RIP_NODE, &rip_route_cmd);
  3446. install_element (RIP_NODE, &no_rip_route_cmd);
  3447. install_element (RIP_NODE, &rip_distance_cmd);
  3448. install_element (RIP_NODE, &no_rip_distance_cmd);
  3449. install_element (RIP_NODE, &rip_distance_source_cmd);
  3450. install_element (RIP_NODE, &no_rip_distance_source_cmd);
  3451. install_element (RIP_NODE, &rip_distance_source_access_list_cmd);
  3452. install_element (RIP_NODE, &no_rip_distance_source_access_list_cmd);
  3453. /* Debug related init. */
  3454. rip_debug_init ();
  3455. /* SNMP init. */
  3456. #ifdef HAVE_SNMP
  3457. rip_snmp_init ();
  3458. #endif /* HAVE_SNMP */
  3459. /* Access list install. */
  3460. access_list_init ();
  3461. access_list_add_hook (rip_distribute_update_all_wrapper);
  3462. access_list_delete_hook (rip_distribute_update_all_wrapper);
  3463. /* Prefix list initialize.*/
  3464. prefix_list_init ();
  3465. prefix_list_add_hook (rip_distribute_update_all);
  3466. prefix_list_delete_hook (rip_distribute_update_all);
  3467. /* Distribute list install. */
  3468. distribute_list_init (RIP_NODE);
  3469. distribute_list_add_hook (rip_distribute_update);
  3470. distribute_list_delete_hook (rip_distribute_update);
  3471. /* Route-map */
  3472. rip_route_map_init ();
  3473. rip_offset_init ();
  3474. route_map_add_hook (rip_routemap_update);
  3475. route_map_delete_hook (rip_routemap_update);
  3476. if_rmap_init (RIP_NODE);
  3477. if_rmap_hook_add (rip_if_rmap_update);
  3478. if_rmap_hook_delete (rip_if_rmap_update);
  3479. /* Distance control. */
  3480. rip_distance_table = route_table_init ();
  3481. }