ripd.c 106 KB

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