ripd.c 109 KB

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