ripd.c 109 KB

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