ripd.c 111 KB

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