route.c 118 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839
  1. /*
  2. * Linux INET6 implementation
  3. * FIB front-end.
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. /* Changes:
  14. *
  15. * YOSHIFUJI Hideaki @USAGI
  16. * reworked default router selection.
  17. * - respect outgoing interface
  18. * - select from (probably) reachable routers (i.e.
  19. * routers in REACHABLE, STALE, DELAY or PROBE states).
  20. * - always select the same router if it is (probably)
  21. * reachable. otherwise, round-robin the list.
  22. * Ville Nuorvala
  23. * Fixed routing subtrees.
  24. */
  25. #define pr_fmt(fmt) "IPv6: " fmt
  26. #include <linux/capability.h>
  27. #include <linux/errno.h>
  28. #include <linux/export.h>
  29. #include <linux/types.h>
  30. #include <linux/times.h>
  31. #include <linux/socket.h>
  32. #include <linux/sockios.h>
  33. #include <linux/net.h>
  34. #include <linux/route.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/in6.h>
  37. #include <linux/mroute6.h>
  38. #include <linux/init.h>
  39. #include <linux/if_arp.h>
  40. #include <linux/proc_fs.h>
  41. #include <linux/seq_file.h>
  42. #include <linux/nsproxy.h>
  43. #include <linux/slab.h>
  44. #include <linux/jhash.h>
  45. #include <net/net_namespace.h>
  46. #include <net/snmp.h>
  47. #include <net/ipv6.h>
  48. #include <net/ip6_fib.h>
  49. #include <net/ip6_route.h>
  50. #include <net/ndisc.h>
  51. #include <net/addrconf.h>
  52. #include <net/tcp.h>
  53. #include <linux/rtnetlink.h>
  54. #include <net/dst.h>
  55. #include <net/dst_metadata.h>
  56. #include <net/xfrm.h>
  57. #include <net/netevent.h>
  58. #include <net/netlink.h>
  59. #include <net/nexthop.h>
  60. #include <net/lwtunnel.h>
  61. #include <net/ip_tunnels.h>
  62. #include <net/l3mdev.h>
  63. #include <trace/events/fib6.h>
  64. #include <linux/uaccess.h>
  65. #ifdef CONFIG_SYSCTL
  66. #include <linux/sysctl.h>
  67. #endif
  68. enum rt6_nud_state {
  69. RT6_NUD_FAIL_HARD = -3,
  70. RT6_NUD_FAIL_PROBE = -2,
  71. RT6_NUD_FAIL_DO_RR = -1,
  72. RT6_NUD_SUCCEED = 1
  73. };
  74. static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort);
  75. static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
  76. static unsigned int ip6_default_advmss(const struct dst_entry *dst);
  77. static unsigned int ip6_mtu(const struct dst_entry *dst);
  78. static struct dst_entry *ip6_negative_advice(struct dst_entry *);
  79. static void ip6_dst_destroy(struct dst_entry *);
  80. static void ip6_dst_ifdown(struct dst_entry *,
  81. struct net_device *dev, int how);
  82. static int ip6_dst_gc(struct dst_ops *ops);
  83. static int ip6_pkt_discard(struct sk_buff *skb);
  84. static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
  85. static int ip6_pkt_prohibit(struct sk_buff *skb);
  86. static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
  87. static void ip6_link_failure(struct sk_buff *skb);
  88. static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
  89. struct sk_buff *skb, u32 mtu);
  90. static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
  91. struct sk_buff *skb);
  92. static void rt6_dst_from_metrics_check(struct rt6_info *rt);
  93. static int rt6_score_route(struct rt6_info *rt, int oif, int strict);
  94. static size_t rt6_nlmsg_size(struct rt6_info *rt);
  95. static int rt6_fill_node(struct net *net,
  96. struct sk_buff *skb, struct rt6_info *rt,
  97. struct in6_addr *dst, struct in6_addr *src,
  98. int iif, int type, u32 portid, u32 seq,
  99. unsigned int flags);
  100. static struct rt6_info *rt6_find_cached_rt(struct rt6_info *rt,
  101. struct in6_addr *daddr,
  102. struct in6_addr *saddr);
  103. #ifdef CONFIG_IPV6_ROUTE_INFO
  104. static struct rt6_info *rt6_add_route_info(struct net *net,
  105. const struct in6_addr *prefix, int prefixlen,
  106. const struct in6_addr *gwaddr,
  107. struct net_device *dev,
  108. unsigned int pref);
  109. static struct rt6_info *rt6_get_route_info(struct net *net,
  110. const struct in6_addr *prefix, int prefixlen,
  111. const struct in6_addr *gwaddr,
  112. struct net_device *dev);
  113. #endif
  114. struct uncached_list {
  115. spinlock_t lock;
  116. struct list_head head;
  117. };
  118. static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
  119. static void rt6_uncached_list_add(struct rt6_info *rt)
  120. {
  121. struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
  122. rt->rt6i_uncached_list = ul;
  123. spin_lock_bh(&ul->lock);
  124. list_add_tail(&rt->rt6i_uncached, &ul->head);
  125. spin_unlock_bh(&ul->lock);
  126. }
  127. static void rt6_uncached_list_del(struct rt6_info *rt)
  128. {
  129. if (!list_empty(&rt->rt6i_uncached)) {
  130. struct uncached_list *ul = rt->rt6i_uncached_list;
  131. struct net *net = dev_net(rt->dst.dev);
  132. spin_lock_bh(&ul->lock);
  133. list_del(&rt->rt6i_uncached);
  134. atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
  135. spin_unlock_bh(&ul->lock);
  136. }
  137. }
  138. static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
  139. {
  140. struct net_device *loopback_dev = net->loopback_dev;
  141. int cpu;
  142. if (dev == loopback_dev)
  143. return;
  144. for_each_possible_cpu(cpu) {
  145. struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
  146. struct rt6_info *rt;
  147. spin_lock_bh(&ul->lock);
  148. list_for_each_entry(rt, &ul->head, rt6i_uncached) {
  149. struct inet6_dev *rt_idev = rt->rt6i_idev;
  150. struct net_device *rt_dev = rt->dst.dev;
  151. if (rt_idev->dev == dev) {
  152. rt->rt6i_idev = in6_dev_get(loopback_dev);
  153. in6_dev_put(rt_idev);
  154. }
  155. if (rt_dev == dev) {
  156. rt->dst.dev = loopback_dev;
  157. dev_hold(rt->dst.dev);
  158. dev_put(rt_dev);
  159. }
  160. }
  161. spin_unlock_bh(&ul->lock);
  162. }
  163. }
  164. static u32 *rt6_pcpu_cow_metrics(struct rt6_info *rt)
  165. {
  166. return dst_metrics_write_ptr(&rt->from->dst);
  167. }
  168. static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
  169. {
  170. struct rt6_info *rt = (struct rt6_info *)dst;
  171. if (rt->rt6i_flags & RTF_PCPU)
  172. return rt6_pcpu_cow_metrics(rt);
  173. else if (rt->rt6i_flags & RTF_CACHE)
  174. return NULL;
  175. else
  176. return dst_cow_metrics_generic(dst, old);
  177. }
  178. static inline const void *choose_neigh_daddr(struct rt6_info *rt,
  179. struct sk_buff *skb,
  180. const void *daddr)
  181. {
  182. struct in6_addr *p = &rt->rt6i_gateway;
  183. if (!ipv6_addr_any(p))
  184. return (const void *) p;
  185. else if (skb)
  186. return &ipv6_hdr(skb)->daddr;
  187. return daddr;
  188. }
  189. static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst,
  190. struct sk_buff *skb,
  191. const void *daddr)
  192. {
  193. struct rt6_info *rt = (struct rt6_info *) dst;
  194. struct neighbour *n;
  195. daddr = choose_neigh_daddr(rt, skb, daddr);
  196. n = __ipv6_neigh_lookup(dst->dev, daddr);
  197. if (n)
  198. return n;
  199. return neigh_create(&nd_tbl, daddr, dst->dev);
  200. }
  201. static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
  202. {
  203. struct net_device *dev = dst->dev;
  204. struct rt6_info *rt = (struct rt6_info *)dst;
  205. daddr = choose_neigh_daddr(rt, NULL, daddr);
  206. if (!daddr)
  207. return;
  208. if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
  209. return;
  210. if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
  211. return;
  212. __ipv6_confirm_neigh(dev, daddr);
  213. }
  214. static struct dst_ops ip6_dst_ops_template = {
  215. .family = AF_INET6,
  216. .gc = ip6_dst_gc,
  217. .gc_thresh = 1024,
  218. .check = ip6_dst_check,
  219. .default_advmss = ip6_default_advmss,
  220. .mtu = ip6_mtu,
  221. .cow_metrics = ipv6_cow_metrics,
  222. .destroy = ip6_dst_destroy,
  223. .ifdown = ip6_dst_ifdown,
  224. .negative_advice = ip6_negative_advice,
  225. .link_failure = ip6_link_failure,
  226. .update_pmtu = ip6_rt_update_pmtu,
  227. .redirect = rt6_do_redirect,
  228. .local_out = __ip6_local_out,
  229. .neigh_lookup = ip6_neigh_lookup,
  230. .confirm_neigh = ip6_confirm_neigh,
  231. };
  232. static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
  233. {
  234. unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
  235. return mtu ? : dst->dev->mtu;
  236. }
  237. static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
  238. struct sk_buff *skb, u32 mtu)
  239. {
  240. }
  241. static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
  242. struct sk_buff *skb)
  243. {
  244. }
  245. static struct dst_ops ip6_dst_blackhole_ops = {
  246. .family = AF_INET6,
  247. .destroy = ip6_dst_destroy,
  248. .check = ip6_dst_check,
  249. .mtu = ip6_blackhole_mtu,
  250. .default_advmss = ip6_default_advmss,
  251. .update_pmtu = ip6_rt_blackhole_update_pmtu,
  252. .redirect = ip6_rt_blackhole_redirect,
  253. .cow_metrics = dst_cow_metrics_generic,
  254. .neigh_lookup = ip6_neigh_lookup,
  255. };
  256. static const u32 ip6_template_metrics[RTAX_MAX] = {
  257. [RTAX_HOPLIMIT - 1] = 0,
  258. };
  259. static const struct rt6_info ip6_null_entry_template = {
  260. .dst = {
  261. .__refcnt = ATOMIC_INIT(1),
  262. .__use = 1,
  263. .obsolete = DST_OBSOLETE_FORCE_CHK,
  264. .error = -ENETUNREACH,
  265. .input = ip6_pkt_discard,
  266. .output = ip6_pkt_discard_out,
  267. },
  268. .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
  269. .rt6i_protocol = RTPROT_KERNEL,
  270. .rt6i_metric = ~(u32) 0,
  271. .rt6i_ref = ATOMIC_INIT(1),
  272. };
  273. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  274. static const struct rt6_info ip6_prohibit_entry_template = {
  275. .dst = {
  276. .__refcnt = ATOMIC_INIT(1),
  277. .__use = 1,
  278. .obsolete = DST_OBSOLETE_FORCE_CHK,
  279. .error = -EACCES,
  280. .input = ip6_pkt_prohibit,
  281. .output = ip6_pkt_prohibit_out,
  282. },
  283. .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
  284. .rt6i_protocol = RTPROT_KERNEL,
  285. .rt6i_metric = ~(u32) 0,
  286. .rt6i_ref = ATOMIC_INIT(1),
  287. };
  288. static const struct rt6_info ip6_blk_hole_entry_template = {
  289. .dst = {
  290. .__refcnt = ATOMIC_INIT(1),
  291. .__use = 1,
  292. .obsolete = DST_OBSOLETE_FORCE_CHK,
  293. .error = -EINVAL,
  294. .input = dst_discard,
  295. .output = dst_discard_out,
  296. },
  297. .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
  298. .rt6i_protocol = RTPROT_KERNEL,
  299. .rt6i_metric = ~(u32) 0,
  300. .rt6i_ref = ATOMIC_INIT(1),
  301. };
  302. #endif
  303. static void rt6_info_init(struct rt6_info *rt)
  304. {
  305. struct dst_entry *dst = &rt->dst;
  306. memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
  307. INIT_LIST_HEAD(&rt->rt6i_siblings);
  308. INIT_LIST_HEAD(&rt->rt6i_uncached);
  309. }
  310. /* allocate dst with ip6_dst_ops */
  311. static struct rt6_info *__ip6_dst_alloc(struct net *net,
  312. struct net_device *dev,
  313. int flags)
  314. {
  315. struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
  316. 1, DST_OBSOLETE_FORCE_CHK, flags);
  317. if (rt) {
  318. rt6_info_init(rt);
  319. atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
  320. }
  321. return rt;
  322. }
  323. struct rt6_info *ip6_dst_alloc(struct net *net,
  324. struct net_device *dev,
  325. int flags)
  326. {
  327. struct rt6_info *rt = __ip6_dst_alloc(net, dev, flags);
  328. if (rt) {
  329. rt->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, GFP_ATOMIC);
  330. if (!rt->rt6i_pcpu) {
  331. dst_release_immediate(&rt->dst);
  332. return NULL;
  333. }
  334. }
  335. return rt;
  336. }
  337. EXPORT_SYMBOL(ip6_dst_alloc);
  338. static void ip6_dst_destroy(struct dst_entry *dst)
  339. {
  340. struct rt6_info *rt = (struct rt6_info *)dst;
  341. struct rt6_exception_bucket *bucket;
  342. struct rt6_info *from = rt->from;
  343. struct inet6_dev *idev;
  344. dst_destroy_metrics_generic(dst);
  345. free_percpu(rt->rt6i_pcpu);
  346. rt6_uncached_list_del(rt);
  347. idev = rt->rt6i_idev;
  348. if (idev) {
  349. rt->rt6i_idev = NULL;
  350. in6_dev_put(idev);
  351. }
  352. bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1);
  353. if (bucket) {
  354. rt->rt6i_exception_bucket = NULL;
  355. kfree(bucket);
  356. }
  357. rt->from = NULL;
  358. dst_release(&from->dst);
  359. }
  360. static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  361. int how)
  362. {
  363. struct rt6_info *rt = (struct rt6_info *)dst;
  364. struct inet6_dev *idev = rt->rt6i_idev;
  365. struct net_device *loopback_dev =
  366. dev_net(dev)->loopback_dev;
  367. if (idev && idev->dev != loopback_dev) {
  368. struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
  369. if (loopback_idev) {
  370. rt->rt6i_idev = loopback_idev;
  371. in6_dev_put(idev);
  372. }
  373. }
  374. }
  375. static bool __rt6_check_expired(const struct rt6_info *rt)
  376. {
  377. if (rt->rt6i_flags & RTF_EXPIRES)
  378. return time_after(jiffies, rt->dst.expires);
  379. else
  380. return false;
  381. }
  382. static bool rt6_check_expired(const struct rt6_info *rt)
  383. {
  384. if (rt->rt6i_flags & RTF_EXPIRES) {
  385. if (time_after(jiffies, rt->dst.expires))
  386. return true;
  387. } else if (rt->from) {
  388. return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
  389. rt6_check_expired(rt->from);
  390. }
  391. return false;
  392. }
  393. static struct rt6_info *rt6_multipath_select(struct rt6_info *match,
  394. struct flowi6 *fl6, int oif,
  395. int strict)
  396. {
  397. struct rt6_info *sibling, *next_sibling;
  398. int route_choosen;
  399. /* We might have already computed the hash for ICMPv6 errors. In such
  400. * case it will always be non-zero. Otherwise now is the time to do it.
  401. */
  402. if (!fl6->mp_hash)
  403. fl6->mp_hash = rt6_multipath_hash(fl6, NULL);
  404. route_choosen = fl6->mp_hash % (match->rt6i_nsiblings + 1);
  405. /* Don't change the route, if route_choosen == 0
  406. * (siblings does not include ourself)
  407. */
  408. if (route_choosen)
  409. list_for_each_entry_safe(sibling, next_sibling,
  410. &match->rt6i_siblings, rt6i_siblings) {
  411. route_choosen--;
  412. if (route_choosen == 0) {
  413. struct inet6_dev *idev = sibling->rt6i_idev;
  414. if (!netif_carrier_ok(sibling->dst.dev) &&
  415. idev->cnf.ignore_routes_with_linkdown)
  416. break;
  417. if (rt6_score_route(sibling, oif, strict) < 0)
  418. break;
  419. match = sibling;
  420. break;
  421. }
  422. }
  423. return match;
  424. }
  425. /*
  426. * Route lookup. rcu_read_lock() should be held.
  427. */
  428. static inline struct rt6_info *rt6_device_match(struct net *net,
  429. struct rt6_info *rt,
  430. const struct in6_addr *saddr,
  431. int oif,
  432. int flags)
  433. {
  434. struct rt6_info *local = NULL;
  435. struct rt6_info *sprt;
  436. if (!oif && ipv6_addr_any(saddr))
  437. goto out;
  438. for (sprt = rt; sprt; sprt = rcu_dereference(sprt->rt6_next)) {
  439. struct net_device *dev = sprt->dst.dev;
  440. if (oif) {
  441. if (dev->ifindex == oif)
  442. return sprt;
  443. if (dev->flags & IFF_LOOPBACK) {
  444. if (!sprt->rt6i_idev ||
  445. sprt->rt6i_idev->dev->ifindex != oif) {
  446. if (flags & RT6_LOOKUP_F_IFACE)
  447. continue;
  448. if (local &&
  449. local->rt6i_idev->dev->ifindex == oif)
  450. continue;
  451. }
  452. local = sprt;
  453. }
  454. } else {
  455. if (ipv6_chk_addr(net, saddr, dev,
  456. flags & RT6_LOOKUP_F_IFACE))
  457. return sprt;
  458. }
  459. }
  460. if (oif) {
  461. if (local)
  462. return local;
  463. if (flags & RT6_LOOKUP_F_IFACE)
  464. return net->ipv6.ip6_null_entry;
  465. }
  466. out:
  467. return rt;
  468. }
  469. #ifdef CONFIG_IPV6_ROUTER_PREF
  470. struct __rt6_probe_work {
  471. struct work_struct work;
  472. struct in6_addr target;
  473. struct net_device *dev;
  474. };
  475. static void rt6_probe_deferred(struct work_struct *w)
  476. {
  477. struct in6_addr mcaddr;
  478. struct __rt6_probe_work *work =
  479. container_of(w, struct __rt6_probe_work, work);
  480. addrconf_addr_solict_mult(&work->target, &mcaddr);
  481. ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
  482. dev_put(work->dev);
  483. kfree(work);
  484. }
  485. static void rt6_probe(struct rt6_info *rt)
  486. {
  487. struct __rt6_probe_work *work;
  488. struct neighbour *neigh;
  489. /*
  490. * Okay, this does not seem to be appropriate
  491. * for now, however, we need to check if it
  492. * is really so; aka Router Reachability Probing.
  493. *
  494. * Router Reachability Probe MUST be rate-limited
  495. * to no more than one per minute.
  496. */
  497. if (!rt || !(rt->rt6i_flags & RTF_GATEWAY))
  498. return;
  499. rcu_read_lock_bh();
  500. neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
  501. if (neigh) {
  502. if (neigh->nud_state & NUD_VALID)
  503. goto out;
  504. work = NULL;
  505. write_lock(&neigh->lock);
  506. if (!(neigh->nud_state & NUD_VALID) &&
  507. time_after(jiffies,
  508. neigh->updated +
  509. rt->rt6i_idev->cnf.rtr_probe_interval)) {
  510. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  511. if (work)
  512. __neigh_set_probe_once(neigh);
  513. }
  514. write_unlock(&neigh->lock);
  515. } else {
  516. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  517. }
  518. if (work) {
  519. INIT_WORK(&work->work, rt6_probe_deferred);
  520. work->target = rt->rt6i_gateway;
  521. dev_hold(rt->dst.dev);
  522. work->dev = rt->dst.dev;
  523. schedule_work(&work->work);
  524. }
  525. out:
  526. rcu_read_unlock_bh();
  527. }
  528. #else
  529. static inline void rt6_probe(struct rt6_info *rt)
  530. {
  531. }
  532. #endif
  533. /*
  534. * Default Router Selection (RFC 2461 6.3.6)
  535. */
  536. static inline int rt6_check_dev(struct rt6_info *rt, int oif)
  537. {
  538. struct net_device *dev = rt->dst.dev;
  539. if (!oif || dev->ifindex == oif)
  540. return 2;
  541. if ((dev->flags & IFF_LOOPBACK) &&
  542. rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
  543. return 1;
  544. return 0;
  545. }
  546. static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt)
  547. {
  548. struct neighbour *neigh;
  549. enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
  550. if (rt->rt6i_flags & RTF_NONEXTHOP ||
  551. !(rt->rt6i_flags & RTF_GATEWAY))
  552. return RT6_NUD_SUCCEED;
  553. rcu_read_lock_bh();
  554. neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
  555. if (neigh) {
  556. read_lock(&neigh->lock);
  557. if (neigh->nud_state & NUD_VALID)
  558. ret = RT6_NUD_SUCCEED;
  559. #ifdef CONFIG_IPV6_ROUTER_PREF
  560. else if (!(neigh->nud_state & NUD_FAILED))
  561. ret = RT6_NUD_SUCCEED;
  562. else
  563. ret = RT6_NUD_FAIL_PROBE;
  564. #endif
  565. read_unlock(&neigh->lock);
  566. } else {
  567. ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
  568. RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
  569. }
  570. rcu_read_unlock_bh();
  571. return ret;
  572. }
  573. static int rt6_score_route(struct rt6_info *rt, int oif,
  574. int strict)
  575. {
  576. int m;
  577. m = rt6_check_dev(rt, oif);
  578. if (!m && (strict & RT6_LOOKUP_F_IFACE))
  579. return RT6_NUD_FAIL_HARD;
  580. #ifdef CONFIG_IPV6_ROUTER_PREF
  581. m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
  582. #endif
  583. if (strict & RT6_LOOKUP_F_REACHABLE) {
  584. int n = rt6_check_neigh(rt);
  585. if (n < 0)
  586. return n;
  587. }
  588. return m;
  589. }
  590. static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
  591. int *mpri, struct rt6_info *match,
  592. bool *do_rr)
  593. {
  594. int m;
  595. bool match_do_rr = false;
  596. struct inet6_dev *idev = rt->rt6i_idev;
  597. struct net_device *dev = rt->dst.dev;
  598. if (dev && !netif_carrier_ok(dev) &&
  599. idev->cnf.ignore_routes_with_linkdown &&
  600. !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
  601. goto out;
  602. if (rt6_check_expired(rt))
  603. goto out;
  604. m = rt6_score_route(rt, oif, strict);
  605. if (m == RT6_NUD_FAIL_DO_RR) {
  606. match_do_rr = true;
  607. m = 0; /* lowest valid score */
  608. } else if (m == RT6_NUD_FAIL_HARD) {
  609. goto out;
  610. }
  611. if (strict & RT6_LOOKUP_F_REACHABLE)
  612. rt6_probe(rt);
  613. /* note that m can be RT6_NUD_FAIL_PROBE at this point */
  614. if (m > *mpri) {
  615. *do_rr = match_do_rr;
  616. *mpri = m;
  617. match = rt;
  618. }
  619. out:
  620. return match;
  621. }
  622. static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
  623. struct rt6_info *leaf,
  624. struct rt6_info *rr_head,
  625. u32 metric, int oif, int strict,
  626. bool *do_rr)
  627. {
  628. struct rt6_info *rt, *match, *cont;
  629. int mpri = -1;
  630. match = NULL;
  631. cont = NULL;
  632. for (rt = rr_head; rt; rt = rcu_dereference(rt->rt6_next)) {
  633. if (rt->rt6i_metric != metric) {
  634. cont = rt;
  635. break;
  636. }
  637. match = find_match(rt, oif, strict, &mpri, match, do_rr);
  638. }
  639. for (rt = leaf; rt && rt != rr_head;
  640. rt = rcu_dereference(rt->rt6_next)) {
  641. if (rt->rt6i_metric != metric) {
  642. cont = rt;
  643. break;
  644. }
  645. match = find_match(rt, oif, strict, &mpri, match, do_rr);
  646. }
  647. if (match || !cont)
  648. return match;
  649. for (rt = cont; rt; rt = rcu_dereference(rt->rt6_next))
  650. match = find_match(rt, oif, strict, &mpri, match, do_rr);
  651. return match;
  652. }
  653. static struct rt6_info *rt6_select(struct net *net, struct fib6_node *fn,
  654. int oif, int strict)
  655. {
  656. struct rt6_info *leaf = rcu_dereference(fn->leaf);
  657. struct rt6_info *match, *rt0;
  658. bool do_rr = false;
  659. int key_plen;
  660. if (!leaf || leaf == net->ipv6.ip6_null_entry)
  661. return net->ipv6.ip6_null_entry;
  662. rt0 = rcu_dereference(fn->rr_ptr);
  663. if (!rt0)
  664. rt0 = leaf;
  665. /* Double check to make sure fn is not an intermediate node
  666. * and fn->leaf does not points to its child's leaf
  667. * (This might happen if all routes under fn are deleted from
  668. * the tree and fib6_repair_tree() is called on the node.)
  669. */
  670. key_plen = rt0->rt6i_dst.plen;
  671. #ifdef CONFIG_IPV6_SUBTREES
  672. if (rt0->rt6i_src.plen)
  673. key_plen = rt0->rt6i_src.plen;
  674. #endif
  675. if (fn->fn_bit != key_plen)
  676. return net->ipv6.ip6_null_entry;
  677. match = find_rr_leaf(fn, leaf, rt0, rt0->rt6i_metric, oif, strict,
  678. &do_rr);
  679. if (do_rr) {
  680. struct rt6_info *next = rcu_dereference(rt0->rt6_next);
  681. /* no entries matched; do round-robin */
  682. if (!next || next->rt6i_metric != rt0->rt6i_metric)
  683. next = leaf;
  684. if (next != rt0) {
  685. spin_lock_bh(&leaf->rt6i_table->tb6_lock);
  686. /* make sure next is not being deleted from the tree */
  687. if (next->rt6i_node)
  688. rcu_assign_pointer(fn->rr_ptr, next);
  689. spin_unlock_bh(&leaf->rt6i_table->tb6_lock);
  690. }
  691. }
  692. return match ? match : net->ipv6.ip6_null_entry;
  693. }
  694. static bool rt6_is_gw_or_nonexthop(const struct rt6_info *rt)
  695. {
  696. return (rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY));
  697. }
  698. #ifdef CONFIG_IPV6_ROUTE_INFO
  699. int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
  700. const struct in6_addr *gwaddr)
  701. {
  702. struct net *net = dev_net(dev);
  703. struct route_info *rinfo = (struct route_info *) opt;
  704. struct in6_addr prefix_buf, *prefix;
  705. unsigned int pref;
  706. unsigned long lifetime;
  707. struct rt6_info *rt;
  708. if (len < sizeof(struct route_info)) {
  709. return -EINVAL;
  710. }
  711. /* Sanity check for prefix_len and length */
  712. if (rinfo->length > 3) {
  713. return -EINVAL;
  714. } else if (rinfo->prefix_len > 128) {
  715. return -EINVAL;
  716. } else if (rinfo->prefix_len > 64) {
  717. if (rinfo->length < 2) {
  718. return -EINVAL;
  719. }
  720. } else if (rinfo->prefix_len > 0) {
  721. if (rinfo->length < 1) {
  722. return -EINVAL;
  723. }
  724. }
  725. pref = rinfo->route_pref;
  726. if (pref == ICMPV6_ROUTER_PREF_INVALID)
  727. return -EINVAL;
  728. lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
  729. if (rinfo->length == 3)
  730. prefix = (struct in6_addr *)rinfo->prefix;
  731. else {
  732. /* this function is safe */
  733. ipv6_addr_prefix(&prefix_buf,
  734. (struct in6_addr *)rinfo->prefix,
  735. rinfo->prefix_len);
  736. prefix = &prefix_buf;
  737. }
  738. if (rinfo->prefix_len == 0)
  739. rt = rt6_get_dflt_router(gwaddr, dev);
  740. else
  741. rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
  742. gwaddr, dev);
  743. if (rt && !lifetime) {
  744. ip6_del_rt(rt);
  745. rt = NULL;
  746. }
  747. if (!rt && lifetime)
  748. rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
  749. dev, pref);
  750. else if (rt)
  751. rt->rt6i_flags = RTF_ROUTEINFO |
  752. (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
  753. if (rt) {
  754. if (!addrconf_finite_timeout(lifetime))
  755. rt6_clean_expires(rt);
  756. else
  757. rt6_set_expires(rt, jiffies + HZ * lifetime);
  758. ip6_rt_put(rt);
  759. }
  760. return 0;
  761. }
  762. #endif
  763. static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
  764. struct in6_addr *saddr)
  765. {
  766. struct fib6_node *pn, *sn;
  767. while (1) {
  768. if (fn->fn_flags & RTN_TL_ROOT)
  769. return NULL;
  770. pn = rcu_dereference(fn->parent);
  771. sn = FIB6_SUBTREE(pn);
  772. if (sn && sn != fn)
  773. fn = fib6_lookup(sn, NULL, saddr);
  774. else
  775. fn = pn;
  776. if (fn->fn_flags & RTN_RTINFO)
  777. return fn;
  778. }
  779. }
  780. static bool ip6_hold_safe(struct net *net, struct rt6_info **prt,
  781. bool null_fallback)
  782. {
  783. struct rt6_info *rt = *prt;
  784. if (dst_hold_safe(&rt->dst))
  785. return true;
  786. if (null_fallback) {
  787. rt = net->ipv6.ip6_null_entry;
  788. dst_hold(&rt->dst);
  789. } else {
  790. rt = NULL;
  791. }
  792. *prt = rt;
  793. return false;
  794. }
  795. static struct rt6_info *ip6_pol_route_lookup(struct net *net,
  796. struct fib6_table *table,
  797. struct flowi6 *fl6, int flags)
  798. {
  799. struct rt6_info *rt, *rt_cache;
  800. struct fib6_node *fn;
  801. rcu_read_lock();
  802. fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
  803. restart:
  804. rt = rcu_dereference(fn->leaf);
  805. if (!rt) {
  806. rt = net->ipv6.ip6_null_entry;
  807. } else {
  808. rt = rt6_device_match(net, rt, &fl6->saddr,
  809. fl6->flowi6_oif, flags);
  810. if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0)
  811. rt = rt6_multipath_select(rt, fl6,
  812. fl6->flowi6_oif, flags);
  813. }
  814. if (rt == net->ipv6.ip6_null_entry) {
  815. fn = fib6_backtrack(fn, &fl6->saddr);
  816. if (fn)
  817. goto restart;
  818. }
  819. /* Search through exception table */
  820. rt_cache = rt6_find_cached_rt(rt, &fl6->daddr, &fl6->saddr);
  821. if (rt_cache)
  822. rt = rt_cache;
  823. if (ip6_hold_safe(net, &rt, true))
  824. dst_use_noref(&rt->dst, jiffies);
  825. rcu_read_unlock();
  826. trace_fib6_table_lookup(net, rt, table, fl6);
  827. return rt;
  828. }
  829. struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
  830. int flags)
  831. {
  832. return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
  833. }
  834. EXPORT_SYMBOL_GPL(ip6_route_lookup);
  835. struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
  836. const struct in6_addr *saddr, int oif, int strict)
  837. {
  838. struct flowi6 fl6 = {
  839. .flowi6_oif = oif,
  840. .daddr = *daddr,
  841. };
  842. struct dst_entry *dst;
  843. int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
  844. if (saddr) {
  845. memcpy(&fl6.saddr, saddr, sizeof(*saddr));
  846. flags |= RT6_LOOKUP_F_HAS_SADDR;
  847. }
  848. dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
  849. if (dst->error == 0)
  850. return (struct rt6_info *) dst;
  851. dst_release(dst);
  852. return NULL;
  853. }
  854. EXPORT_SYMBOL(rt6_lookup);
  855. /* ip6_ins_rt is called with FREE table->tb6_lock.
  856. * It takes new route entry, the addition fails by any reason the
  857. * route is released.
  858. * Caller must hold dst before calling it.
  859. */
  860. static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info,
  861. struct mx6_config *mxc,
  862. struct netlink_ext_ack *extack)
  863. {
  864. int err;
  865. struct fib6_table *table;
  866. table = rt->rt6i_table;
  867. spin_lock_bh(&table->tb6_lock);
  868. err = fib6_add(&table->tb6_root, rt, info, mxc, extack);
  869. spin_unlock_bh(&table->tb6_lock);
  870. return err;
  871. }
  872. int ip6_ins_rt(struct rt6_info *rt)
  873. {
  874. struct nl_info info = { .nl_net = dev_net(rt->dst.dev), };
  875. struct mx6_config mxc = { .mx = NULL, };
  876. /* Hold dst to account for the reference from the fib6 tree */
  877. dst_hold(&rt->dst);
  878. return __ip6_ins_rt(rt, &info, &mxc, NULL);
  879. }
  880. /* called with rcu_lock held */
  881. static struct net_device *ip6_rt_get_dev_rcu(struct rt6_info *rt)
  882. {
  883. struct net_device *dev = rt->dst.dev;
  884. if (rt->rt6i_flags & (RTF_LOCAL | RTF_ANYCAST)) {
  885. /* for copies of local routes, dst->dev needs to be the
  886. * device if it is a master device, the master device if
  887. * device is enslaved, and the loopback as the default
  888. */
  889. if (netif_is_l3_slave(dev) &&
  890. !rt6_need_strict(&rt->rt6i_dst.addr))
  891. dev = l3mdev_master_dev_rcu(dev);
  892. else if (!netif_is_l3_master(dev))
  893. dev = dev_net(dev)->loopback_dev;
  894. /* last case is netif_is_l3_master(dev) is true in which
  895. * case we want dev returned to be dev
  896. */
  897. }
  898. return dev;
  899. }
  900. static struct rt6_info *ip6_rt_cache_alloc(struct rt6_info *ort,
  901. const struct in6_addr *daddr,
  902. const struct in6_addr *saddr)
  903. {
  904. struct net_device *dev;
  905. struct rt6_info *rt;
  906. /*
  907. * Clone the route.
  908. */
  909. if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU))
  910. ort = ort->from;
  911. rcu_read_lock();
  912. dev = ip6_rt_get_dev_rcu(ort);
  913. rt = __ip6_dst_alloc(dev_net(dev), dev, 0);
  914. rcu_read_unlock();
  915. if (!rt)
  916. return NULL;
  917. ip6_rt_copy_init(rt, ort);
  918. rt->rt6i_flags |= RTF_CACHE;
  919. rt->rt6i_metric = 0;
  920. rt->dst.flags |= DST_HOST;
  921. rt->rt6i_dst.addr = *daddr;
  922. rt->rt6i_dst.plen = 128;
  923. if (!rt6_is_gw_or_nonexthop(ort)) {
  924. if (ort->rt6i_dst.plen != 128 &&
  925. ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
  926. rt->rt6i_flags |= RTF_ANYCAST;
  927. #ifdef CONFIG_IPV6_SUBTREES
  928. if (rt->rt6i_src.plen && saddr) {
  929. rt->rt6i_src.addr = *saddr;
  930. rt->rt6i_src.plen = 128;
  931. }
  932. #endif
  933. }
  934. return rt;
  935. }
  936. static struct rt6_info *ip6_rt_pcpu_alloc(struct rt6_info *rt)
  937. {
  938. struct net_device *dev;
  939. struct rt6_info *pcpu_rt;
  940. rcu_read_lock();
  941. dev = ip6_rt_get_dev_rcu(rt);
  942. pcpu_rt = __ip6_dst_alloc(dev_net(dev), dev, rt->dst.flags);
  943. rcu_read_unlock();
  944. if (!pcpu_rt)
  945. return NULL;
  946. ip6_rt_copy_init(pcpu_rt, rt);
  947. pcpu_rt->rt6i_protocol = rt->rt6i_protocol;
  948. pcpu_rt->rt6i_flags |= RTF_PCPU;
  949. return pcpu_rt;
  950. }
  951. /* It should be called with rcu_read_lock() acquired */
  952. static struct rt6_info *rt6_get_pcpu_route(struct rt6_info *rt)
  953. {
  954. struct rt6_info *pcpu_rt, **p;
  955. p = this_cpu_ptr(rt->rt6i_pcpu);
  956. pcpu_rt = *p;
  957. if (pcpu_rt && ip6_hold_safe(NULL, &pcpu_rt, false))
  958. rt6_dst_from_metrics_check(pcpu_rt);
  959. return pcpu_rt;
  960. }
  961. static struct rt6_info *rt6_make_pcpu_route(struct rt6_info *rt)
  962. {
  963. struct rt6_info *pcpu_rt, *prev, **p;
  964. pcpu_rt = ip6_rt_pcpu_alloc(rt);
  965. if (!pcpu_rt) {
  966. struct net *net = dev_net(rt->dst.dev);
  967. dst_hold(&net->ipv6.ip6_null_entry->dst);
  968. return net->ipv6.ip6_null_entry;
  969. }
  970. dst_hold(&pcpu_rt->dst);
  971. p = this_cpu_ptr(rt->rt6i_pcpu);
  972. prev = cmpxchg(p, NULL, pcpu_rt);
  973. BUG_ON(prev);
  974. rt6_dst_from_metrics_check(pcpu_rt);
  975. return pcpu_rt;
  976. }
  977. /* exception hash table implementation
  978. */
  979. static DEFINE_SPINLOCK(rt6_exception_lock);
  980. /* Remove rt6_ex from hash table and free the memory
  981. * Caller must hold rt6_exception_lock
  982. */
  983. static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
  984. struct rt6_exception *rt6_ex)
  985. {
  986. struct net *net;
  987. if (!bucket || !rt6_ex)
  988. return;
  989. net = dev_net(rt6_ex->rt6i->dst.dev);
  990. rt6_ex->rt6i->rt6i_node = NULL;
  991. hlist_del_rcu(&rt6_ex->hlist);
  992. rt6_release(rt6_ex->rt6i);
  993. kfree_rcu(rt6_ex, rcu);
  994. WARN_ON_ONCE(!bucket->depth);
  995. bucket->depth--;
  996. net->ipv6.rt6_stats->fib_rt_cache--;
  997. }
  998. /* Remove oldest rt6_ex in bucket and free the memory
  999. * Caller must hold rt6_exception_lock
  1000. */
  1001. static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
  1002. {
  1003. struct rt6_exception *rt6_ex, *oldest = NULL;
  1004. if (!bucket)
  1005. return;
  1006. hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
  1007. if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
  1008. oldest = rt6_ex;
  1009. }
  1010. rt6_remove_exception(bucket, oldest);
  1011. }
  1012. static u32 rt6_exception_hash(const struct in6_addr *dst,
  1013. const struct in6_addr *src)
  1014. {
  1015. static u32 seed __read_mostly;
  1016. u32 val;
  1017. net_get_random_once(&seed, sizeof(seed));
  1018. val = jhash(dst, sizeof(*dst), seed);
  1019. #ifdef CONFIG_IPV6_SUBTREES
  1020. if (src)
  1021. val = jhash(src, sizeof(*src), val);
  1022. #endif
  1023. return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
  1024. }
  1025. /* Helper function to find the cached rt in the hash table
  1026. * and update bucket pointer to point to the bucket for this
  1027. * (daddr, saddr) pair
  1028. * Caller must hold rt6_exception_lock
  1029. */
  1030. static struct rt6_exception *
  1031. __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
  1032. const struct in6_addr *daddr,
  1033. const struct in6_addr *saddr)
  1034. {
  1035. struct rt6_exception *rt6_ex;
  1036. u32 hval;
  1037. if (!(*bucket) || !daddr)
  1038. return NULL;
  1039. hval = rt6_exception_hash(daddr, saddr);
  1040. *bucket += hval;
  1041. hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
  1042. struct rt6_info *rt6 = rt6_ex->rt6i;
  1043. bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
  1044. #ifdef CONFIG_IPV6_SUBTREES
  1045. if (matched && saddr)
  1046. matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
  1047. #endif
  1048. if (matched)
  1049. return rt6_ex;
  1050. }
  1051. return NULL;
  1052. }
  1053. /* Helper function to find the cached rt in the hash table
  1054. * and update bucket pointer to point to the bucket for this
  1055. * (daddr, saddr) pair
  1056. * Caller must hold rcu_read_lock()
  1057. */
  1058. static struct rt6_exception *
  1059. __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
  1060. const struct in6_addr *daddr,
  1061. const struct in6_addr *saddr)
  1062. {
  1063. struct rt6_exception *rt6_ex;
  1064. u32 hval;
  1065. WARN_ON_ONCE(!rcu_read_lock_held());
  1066. if (!(*bucket) || !daddr)
  1067. return NULL;
  1068. hval = rt6_exception_hash(daddr, saddr);
  1069. *bucket += hval;
  1070. hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
  1071. struct rt6_info *rt6 = rt6_ex->rt6i;
  1072. bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
  1073. #ifdef CONFIG_IPV6_SUBTREES
  1074. if (matched && saddr)
  1075. matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
  1076. #endif
  1077. if (matched)
  1078. return rt6_ex;
  1079. }
  1080. return NULL;
  1081. }
  1082. static int rt6_insert_exception(struct rt6_info *nrt,
  1083. struct rt6_info *ort)
  1084. {
  1085. struct net *net = dev_net(ort->dst.dev);
  1086. struct rt6_exception_bucket *bucket;
  1087. struct in6_addr *src_key = NULL;
  1088. struct rt6_exception *rt6_ex;
  1089. int err = 0;
  1090. /* ort can't be a cache or pcpu route */
  1091. if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU))
  1092. ort = ort->from;
  1093. WARN_ON_ONCE(ort->rt6i_flags & (RTF_CACHE | RTF_PCPU));
  1094. spin_lock_bh(&rt6_exception_lock);
  1095. if (ort->exception_bucket_flushed) {
  1096. err = -EINVAL;
  1097. goto out;
  1098. }
  1099. bucket = rcu_dereference_protected(ort->rt6i_exception_bucket,
  1100. lockdep_is_held(&rt6_exception_lock));
  1101. if (!bucket) {
  1102. bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
  1103. GFP_ATOMIC);
  1104. if (!bucket) {
  1105. err = -ENOMEM;
  1106. goto out;
  1107. }
  1108. rcu_assign_pointer(ort->rt6i_exception_bucket, bucket);
  1109. }
  1110. #ifdef CONFIG_IPV6_SUBTREES
  1111. /* rt6i_src.plen != 0 indicates ort is in subtree
  1112. * and exception table is indexed by a hash of
  1113. * both rt6i_dst and rt6i_src.
  1114. * Otherwise, the exception table is indexed by
  1115. * a hash of only rt6i_dst.
  1116. */
  1117. if (ort->rt6i_src.plen)
  1118. src_key = &nrt->rt6i_src.addr;
  1119. #endif
  1120. /* Update rt6i_prefsrc as it could be changed
  1121. * in rt6_remove_prefsrc()
  1122. */
  1123. nrt->rt6i_prefsrc = ort->rt6i_prefsrc;
  1124. /* rt6_mtu_change() might lower mtu on ort.
  1125. * Only insert this exception route if its mtu
  1126. * is less than ort's mtu value.
  1127. */
  1128. if (nrt->rt6i_pmtu >= dst_mtu(&ort->dst)) {
  1129. err = -EINVAL;
  1130. goto out;
  1131. }
  1132. rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
  1133. src_key);
  1134. if (rt6_ex)
  1135. rt6_remove_exception(bucket, rt6_ex);
  1136. rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
  1137. if (!rt6_ex) {
  1138. err = -ENOMEM;
  1139. goto out;
  1140. }
  1141. rt6_ex->rt6i = nrt;
  1142. rt6_ex->stamp = jiffies;
  1143. atomic_inc(&nrt->rt6i_ref);
  1144. nrt->rt6i_node = ort->rt6i_node;
  1145. hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
  1146. bucket->depth++;
  1147. net->ipv6.rt6_stats->fib_rt_cache++;
  1148. if (bucket->depth > FIB6_MAX_DEPTH)
  1149. rt6_exception_remove_oldest(bucket);
  1150. out:
  1151. spin_unlock_bh(&rt6_exception_lock);
  1152. /* Update fn->fn_sernum to invalidate all cached dst */
  1153. if (!err) {
  1154. fib6_update_sernum(ort);
  1155. fib6_force_start_gc(net);
  1156. }
  1157. return err;
  1158. }
  1159. void rt6_flush_exceptions(struct rt6_info *rt)
  1160. {
  1161. struct rt6_exception_bucket *bucket;
  1162. struct rt6_exception *rt6_ex;
  1163. struct hlist_node *tmp;
  1164. int i;
  1165. spin_lock_bh(&rt6_exception_lock);
  1166. /* Prevent rt6_insert_exception() to recreate the bucket list */
  1167. rt->exception_bucket_flushed = 1;
  1168. bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
  1169. lockdep_is_held(&rt6_exception_lock));
  1170. if (!bucket)
  1171. goto out;
  1172. for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
  1173. hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist)
  1174. rt6_remove_exception(bucket, rt6_ex);
  1175. WARN_ON_ONCE(bucket->depth);
  1176. bucket++;
  1177. }
  1178. out:
  1179. spin_unlock_bh(&rt6_exception_lock);
  1180. }
  1181. /* Find cached rt in the hash table inside passed in rt
  1182. * Caller has to hold rcu_read_lock()
  1183. */
  1184. static struct rt6_info *rt6_find_cached_rt(struct rt6_info *rt,
  1185. struct in6_addr *daddr,
  1186. struct in6_addr *saddr)
  1187. {
  1188. struct rt6_exception_bucket *bucket;
  1189. struct in6_addr *src_key = NULL;
  1190. struct rt6_exception *rt6_ex;
  1191. struct rt6_info *res = NULL;
  1192. bucket = rcu_dereference(rt->rt6i_exception_bucket);
  1193. #ifdef CONFIG_IPV6_SUBTREES
  1194. /* rt6i_src.plen != 0 indicates rt is in subtree
  1195. * and exception table is indexed by a hash of
  1196. * both rt6i_dst and rt6i_src.
  1197. * Otherwise, the exception table is indexed by
  1198. * a hash of only rt6i_dst.
  1199. */
  1200. if (rt->rt6i_src.plen)
  1201. src_key = saddr;
  1202. #endif
  1203. rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
  1204. if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
  1205. res = rt6_ex->rt6i;
  1206. return res;
  1207. }
  1208. /* Remove the passed in cached rt from the hash table that contains it */
  1209. int rt6_remove_exception_rt(struct rt6_info *rt)
  1210. {
  1211. struct rt6_exception_bucket *bucket;
  1212. struct rt6_info *from = rt->from;
  1213. struct in6_addr *src_key = NULL;
  1214. struct rt6_exception *rt6_ex;
  1215. int err;
  1216. if (!from ||
  1217. !(rt->rt6i_flags & RTF_CACHE))
  1218. return -EINVAL;
  1219. if (!rcu_access_pointer(from->rt6i_exception_bucket))
  1220. return -ENOENT;
  1221. spin_lock_bh(&rt6_exception_lock);
  1222. bucket = rcu_dereference_protected(from->rt6i_exception_bucket,
  1223. lockdep_is_held(&rt6_exception_lock));
  1224. #ifdef CONFIG_IPV6_SUBTREES
  1225. /* rt6i_src.plen != 0 indicates 'from' is in subtree
  1226. * and exception table is indexed by a hash of
  1227. * both rt6i_dst and rt6i_src.
  1228. * Otherwise, the exception table is indexed by
  1229. * a hash of only rt6i_dst.
  1230. */
  1231. if (from->rt6i_src.plen)
  1232. src_key = &rt->rt6i_src.addr;
  1233. #endif
  1234. rt6_ex = __rt6_find_exception_spinlock(&bucket,
  1235. &rt->rt6i_dst.addr,
  1236. src_key);
  1237. if (rt6_ex) {
  1238. rt6_remove_exception(bucket, rt6_ex);
  1239. err = 0;
  1240. } else {
  1241. err = -ENOENT;
  1242. }
  1243. spin_unlock_bh(&rt6_exception_lock);
  1244. return err;
  1245. }
  1246. /* Find rt6_ex which contains the passed in rt cache and
  1247. * refresh its stamp
  1248. */
  1249. static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
  1250. {
  1251. struct rt6_exception_bucket *bucket;
  1252. struct rt6_info *from = rt->from;
  1253. struct in6_addr *src_key = NULL;
  1254. struct rt6_exception *rt6_ex;
  1255. if (!from ||
  1256. !(rt->rt6i_flags & RTF_CACHE))
  1257. return;
  1258. rcu_read_lock();
  1259. bucket = rcu_dereference(from->rt6i_exception_bucket);
  1260. #ifdef CONFIG_IPV6_SUBTREES
  1261. /* rt6i_src.plen != 0 indicates 'from' is in subtree
  1262. * and exception table is indexed by a hash of
  1263. * both rt6i_dst and rt6i_src.
  1264. * Otherwise, the exception table is indexed by
  1265. * a hash of only rt6i_dst.
  1266. */
  1267. if (from->rt6i_src.plen)
  1268. src_key = &rt->rt6i_src.addr;
  1269. #endif
  1270. rt6_ex = __rt6_find_exception_rcu(&bucket,
  1271. &rt->rt6i_dst.addr,
  1272. src_key);
  1273. if (rt6_ex)
  1274. rt6_ex->stamp = jiffies;
  1275. rcu_read_unlock();
  1276. }
  1277. static void rt6_exceptions_remove_prefsrc(struct rt6_info *rt)
  1278. {
  1279. struct rt6_exception_bucket *bucket;
  1280. struct rt6_exception *rt6_ex;
  1281. int i;
  1282. bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
  1283. lockdep_is_held(&rt6_exception_lock));
  1284. if (bucket) {
  1285. for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
  1286. hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
  1287. rt6_ex->rt6i->rt6i_prefsrc.plen = 0;
  1288. }
  1289. bucket++;
  1290. }
  1291. }
  1292. }
  1293. static void rt6_exceptions_update_pmtu(struct rt6_info *rt, int mtu)
  1294. {
  1295. struct rt6_exception_bucket *bucket;
  1296. struct rt6_exception *rt6_ex;
  1297. int i;
  1298. bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
  1299. lockdep_is_held(&rt6_exception_lock));
  1300. if (bucket) {
  1301. for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
  1302. hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
  1303. struct rt6_info *entry = rt6_ex->rt6i;
  1304. /* For RTF_CACHE with rt6i_pmtu == 0
  1305. * (i.e. a redirected route),
  1306. * the metrics of its rt->dst.from has already
  1307. * been updated.
  1308. */
  1309. if (entry->rt6i_pmtu && entry->rt6i_pmtu > mtu)
  1310. entry->rt6i_pmtu = mtu;
  1311. }
  1312. bucket++;
  1313. }
  1314. }
  1315. }
  1316. #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE)
  1317. static void rt6_exceptions_clean_tohost(struct rt6_info *rt,
  1318. struct in6_addr *gateway)
  1319. {
  1320. struct rt6_exception_bucket *bucket;
  1321. struct rt6_exception *rt6_ex;
  1322. struct hlist_node *tmp;
  1323. int i;
  1324. if (!rcu_access_pointer(rt->rt6i_exception_bucket))
  1325. return;
  1326. spin_lock_bh(&rt6_exception_lock);
  1327. bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
  1328. lockdep_is_held(&rt6_exception_lock));
  1329. if (bucket) {
  1330. for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
  1331. hlist_for_each_entry_safe(rt6_ex, tmp,
  1332. &bucket->chain, hlist) {
  1333. struct rt6_info *entry = rt6_ex->rt6i;
  1334. if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
  1335. RTF_CACHE_GATEWAY &&
  1336. ipv6_addr_equal(gateway,
  1337. &entry->rt6i_gateway)) {
  1338. rt6_remove_exception(bucket, rt6_ex);
  1339. }
  1340. }
  1341. bucket++;
  1342. }
  1343. }
  1344. spin_unlock_bh(&rt6_exception_lock);
  1345. }
  1346. static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
  1347. struct rt6_exception *rt6_ex,
  1348. struct fib6_gc_args *gc_args,
  1349. unsigned long now)
  1350. {
  1351. struct rt6_info *rt = rt6_ex->rt6i;
  1352. /* we are pruning and obsoleting aged-out and non gateway exceptions
  1353. * even if others have still references to them, so that on next
  1354. * dst_check() such references can be dropped.
  1355. * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
  1356. * expired, independently from their aging, as per RFC 8201 section 4
  1357. */
  1358. if (!(rt->rt6i_flags & RTF_EXPIRES) &&
  1359. time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
  1360. RT6_TRACE("aging clone %p\n", rt);
  1361. rt6_remove_exception(bucket, rt6_ex);
  1362. return;
  1363. } else if (rt->rt6i_flags & RTF_GATEWAY) {
  1364. struct neighbour *neigh;
  1365. __u8 neigh_flags = 0;
  1366. neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
  1367. if (neigh) {
  1368. neigh_flags = neigh->flags;
  1369. neigh_release(neigh);
  1370. }
  1371. if (!(neigh_flags & NTF_ROUTER)) {
  1372. RT6_TRACE("purging route %p via non-router but gateway\n",
  1373. rt);
  1374. rt6_remove_exception(bucket, rt6_ex);
  1375. return;
  1376. }
  1377. } else if (__rt6_check_expired(rt)) {
  1378. RT6_TRACE("purging expired route %p\n", rt);
  1379. rt6_remove_exception(bucket, rt6_ex);
  1380. return;
  1381. }
  1382. gc_args->more++;
  1383. }
  1384. void rt6_age_exceptions(struct rt6_info *rt,
  1385. struct fib6_gc_args *gc_args,
  1386. unsigned long now)
  1387. {
  1388. struct rt6_exception_bucket *bucket;
  1389. struct rt6_exception *rt6_ex;
  1390. struct hlist_node *tmp;
  1391. int i;
  1392. if (!rcu_access_pointer(rt->rt6i_exception_bucket))
  1393. return;
  1394. spin_lock_bh(&rt6_exception_lock);
  1395. bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
  1396. lockdep_is_held(&rt6_exception_lock));
  1397. if (bucket) {
  1398. for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
  1399. hlist_for_each_entry_safe(rt6_ex, tmp,
  1400. &bucket->chain, hlist) {
  1401. rt6_age_examine_exception(bucket, rt6_ex,
  1402. gc_args, now);
  1403. }
  1404. bucket++;
  1405. }
  1406. }
  1407. spin_unlock_bh(&rt6_exception_lock);
  1408. }
  1409. struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
  1410. int oif, struct flowi6 *fl6, int flags)
  1411. {
  1412. struct fib6_node *fn, *saved_fn;
  1413. struct rt6_info *rt, *rt_cache;
  1414. int strict = 0;
  1415. strict |= flags & RT6_LOOKUP_F_IFACE;
  1416. strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
  1417. if (net->ipv6.devconf_all->forwarding == 0)
  1418. strict |= RT6_LOOKUP_F_REACHABLE;
  1419. rcu_read_lock();
  1420. fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
  1421. saved_fn = fn;
  1422. if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
  1423. oif = 0;
  1424. redo_rt6_select:
  1425. rt = rt6_select(net, fn, oif, strict);
  1426. if (rt->rt6i_nsiblings)
  1427. rt = rt6_multipath_select(rt, fl6, oif, strict);
  1428. if (rt == net->ipv6.ip6_null_entry) {
  1429. fn = fib6_backtrack(fn, &fl6->saddr);
  1430. if (fn)
  1431. goto redo_rt6_select;
  1432. else if (strict & RT6_LOOKUP_F_REACHABLE) {
  1433. /* also consider unreachable route */
  1434. strict &= ~RT6_LOOKUP_F_REACHABLE;
  1435. fn = saved_fn;
  1436. goto redo_rt6_select;
  1437. }
  1438. }
  1439. /*Search through exception table */
  1440. rt_cache = rt6_find_cached_rt(rt, &fl6->daddr, &fl6->saddr);
  1441. if (rt_cache)
  1442. rt = rt_cache;
  1443. if (rt == net->ipv6.ip6_null_entry) {
  1444. rcu_read_unlock();
  1445. dst_hold(&rt->dst);
  1446. trace_fib6_table_lookup(net, rt, table, fl6);
  1447. return rt;
  1448. } else if (rt->rt6i_flags & RTF_CACHE) {
  1449. if (ip6_hold_safe(net, &rt, true)) {
  1450. dst_use_noref(&rt->dst, jiffies);
  1451. rt6_dst_from_metrics_check(rt);
  1452. }
  1453. rcu_read_unlock();
  1454. trace_fib6_table_lookup(net, rt, table, fl6);
  1455. return rt;
  1456. } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
  1457. !(rt->rt6i_flags & RTF_GATEWAY))) {
  1458. /* Create a RTF_CACHE clone which will not be
  1459. * owned by the fib6 tree. It is for the special case where
  1460. * the daddr in the skb during the neighbor look-up is different
  1461. * from the fl6->daddr used to look-up route here.
  1462. */
  1463. struct rt6_info *uncached_rt;
  1464. if (ip6_hold_safe(net, &rt, true)) {
  1465. dst_use_noref(&rt->dst, jiffies);
  1466. } else {
  1467. rcu_read_unlock();
  1468. uncached_rt = rt;
  1469. goto uncached_rt_out;
  1470. }
  1471. rcu_read_unlock();
  1472. uncached_rt = ip6_rt_cache_alloc(rt, &fl6->daddr, NULL);
  1473. dst_release(&rt->dst);
  1474. if (uncached_rt) {
  1475. /* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc()
  1476. * No need for another dst_hold()
  1477. */
  1478. rt6_uncached_list_add(uncached_rt);
  1479. atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
  1480. } else {
  1481. uncached_rt = net->ipv6.ip6_null_entry;
  1482. dst_hold(&uncached_rt->dst);
  1483. }
  1484. uncached_rt_out:
  1485. trace_fib6_table_lookup(net, uncached_rt, table, fl6);
  1486. return uncached_rt;
  1487. } else {
  1488. /* Get a percpu copy */
  1489. struct rt6_info *pcpu_rt;
  1490. dst_use_noref(&rt->dst, jiffies);
  1491. local_bh_disable();
  1492. pcpu_rt = rt6_get_pcpu_route(rt);
  1493. if (!pcpu_rt) {
  1494. /* atomic_inc_not_zero() is needed when using rcu */
  1495. if (atomic_inc_not_zero(&rt->rt6i_ref)) {
  1496. /* No dst_hold() on rt is needed because grabbing
  1497. * rt->rt6i_ref makes sure rt can't be released.
  1498. */
  1499. pcpu_rt = rt6_make_pcpu_route(rt);
  1500. rt6_release(rt);
  1501. } else {
  1502. /* rt is already removed from tree */
  1503. pcpu_rt = net->ipv6.ip6_null_entry;
  1504. dst_hold(&pcpu_rt->dst);
  1505. }
  1506. }
  1507. local_bh_enable();
  1508. rcu_read_unlock();
  1509. trace_fib6_table_lookup(net, pcpu_rt, table, fl6);
  1510. return pcpu_rt;
  1511. }
  1512. }
  1513. EXPORT_SYMBOL_GPL(ip6_pol_route);
  1514. static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
  1515. struct flowi6 *fl6, int flags)
  1516. {
  1517. return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
  1518. }
  1519. struct dst_entry *ip6_route_input_lookup(struct net *net,
  1520. struct net_device *dev,
  1521. struct flowi6 *fl6, int flags)
  1522. {
  1523. if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
  1524. flags |= RT6_LOOKUP_F_IFACE;
  1525. return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
  1526. }
  1527. EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
  1528. static void ip6_multipath_l3_keys(const struct sk_buff *skb,
  1529. struct flow_keys *keys)
  1530. {
  1531. const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
  1532. const struct ipv6hdr *key_iph = outer_iph;
  1533. const struct ipv6hdr *inner_iph;
  1534. const struct icmp6hdr *icmph;
  1535. struct ipv6hdr _inner_iph;
  1536. if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
  1537. goto out;
  1538. icmph = icmp6_hdr(skb);
  1539. if (icmph->icmp6_type != ICMPV6_DEST_UNREACH &&
  1540. icmph->icmp6_type != ICMPV6_PKT_TOOBIG &&
  1541. icmph->icmp6_type != ICMPV6_TIME_EXCEED &&
  1542. icmph->icmp6_type != ICMPV6_PARAMPROB)
  1543. goto out;
  1544. inner_iph = skb_header_pointer(skb,
  1545. skb_transport_offset(skb) + sizeof(*icmph),
  1546. sizeof(_inner_iph), &_inner_iph);
  1547. if (!inner_iph)
  1548. goto out;
  1549. key_iph = inner_iph;
  1550. out:
  1551. memset(keys, 0, sizeof(*keys));
  1552. keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
  1553. keys->addrs.v6addrs.src = key_iph->saddr;
  1554. keys->addrs.v6addrs.dst = key_iph->daddr;
  1555. keys->tags.flow_label = ip6_flowinfo(key_iph);
  1556. keys->basic.ip_proto = key_iph->nexthdr;
  1557. }
  1558. /* if skb is set it will be used and fl6 can be NULL */
  1559. u32 rt6_multipath_hash(const struct flowi6 *fl6, const struct sk_buff *skb)
  1560. {
  1561. struct flow_keys hash_keys;
  1562. if (skb) {
  1563. ip6_multipath_l3_keys(skb, &hash_keys);
  1564. return flow_hash_from_keys(&hash_keys);
  1565. }
  1566. return get_hash_from_flowi6(fl6);
  1567. }
  1568. void ip6_route_input(struct sk_buff *skb)
  1569. {
  1570. const struct ipv6hdr *iph = ipv6_hdr(skb);
  1571. struct net *net = dev_net(skb->dev);
  1572. int flags = RT6_LOOKUP_F_HAS_SADDR;
  1573. struct ip_tunnel_info *tun_info;
  1574. struct flowi6 fl6 = {
  1575. .flowi6_iif = skb->dev->ifindex,
  1576. .daddr = iph->daddr,
  1577. .saddr = iph->saddr,
  1578. .flowlabel = ip6_flowinfo(iph),
  1579. .flowi6_mark = skb->mark,
  1580. .flowi6_proto = iph->nexthdr,
  1581. };
  1582. tun_info = skb_tunnel_info(skb);
  1583. if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
  1584. fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
  1585. if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
  1586. fl6.mp_hash = rt6_multipath_hash(&fl6, skb);
  1587. skb_dst_drop(skb);
  1588. skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
  1589. }
  1590. static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
  1591. struct flowi6 *fl6, int flags)
  1592. {
  1593. return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
  1594. }
  1595. struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk,
  1596. struct flowi6 *fl6, int flags)
  1597. {
  1598. bool any_src;
  1599. if (rt6_need_strict(&fl6->daddr)) {
  1600. struct dst_entry *dst;
  1601. dst = l3mdev_link_scope_lookup(net, fl6);
  1602. if (dst)
  1603. return dst;
  1604. }
  1605. fl6->flowi6_iif = LOOPBACK_IFINDEX;
  1606. any_src = ipv6_addr_any(&fl6->saddr);
  1607. if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
  1608. (fl6->flowi6_oif && any_src))
  1609. flags |= RT6_LOOKUP_F_IFACE;
  1610. if (!any_src)
  1611. flags |= RT6_LOOKUP_F_HAS_SADDR;
  1612. else if (sk)
  1613. flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
  1614. return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
  1615. }
  1616. EXPORT_SYMBOL_GPL(ip6_route_output_flags);
  1617. struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
  1618. {
  1619. struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
  1620. struct net_device *loopback_dev = net->loopback_dev;
  1621. struct dst_entry *new = NULL;
  1622. rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
  1623. DST_OBSOLETE_DEAD, 0);
  1624. if (rt) {
  1625. rt6_info_init(rt);
  1626. atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
  1627. new = &rt->dst;
  1628. new->__use = 1;
  1629. new->input = dst_discard;
  1630. new->output = dst_discard_out;
  1631. dst_copy_metrics(new, &ort->dst);
  1632. rt->rt6i_idev = in6_dev_get(loopback_dev);
  1633. rt->rt6i_gateway = ort->rt6i_gateway;
  1634. rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
  1635. rt->rt6i_metric = 0;
  1636. memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
  1637. #ifdef CONFIG_IPV6_SUBTREES
  1638. memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
  1639. #endif
  1640. }
  1641. dst_release(dst_orig);
  1642. return new ? new : ERR_PTR(-ENOMEM);
  1643. }
  1644. /*
  1645. * Destination cache support functions
  1646. */
  1647. static void rt6_dst_from_metrics_check(struct rt6_info *rt)
  1648. {
  1649. if (rt->from &&
  1650. dst_metrics_ptr(&rt->dst) != dst_metrics_ptr(&rt->from->dst))
  1651. dst_init_metrics(&rt->dst, dst_metrics_ptr(&rt->from->dst), true);
  1652. }
  1653. static struct dst_entry *rt6_check(struct rt6_info *rt, u32 cookie)
  1654. {
  1655. u32 rt_cookie = 0;
  1656. if (!rt6_get_cookie_safe(rt, &rt_cookie) || rt_cookie != cookie)
  1657. return NULL;
  1658. if (rt6_check_expired(rt))
  1659. return NULL;
  1660. return &rt->dst;
  1661. }
  1662. static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, u32 cookie)
  1663. {
  1664. if (!__rt6_check_expired(rt) &&
  1665. rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
  1666. rt6_check(rt->from, cookie))
  1667. return &rt->dst;
  1668. else
  1669. return NULL;
  1670. }
  1671. static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
  1672. {
  1673. struct rt6_info *rt;
  1674. rt = (struct rt6_info *) dst;
  1675. /* All IPV6 dsts are created with ->obsolete set to the value
  1676. * DST_OBSOLETE_FORCE_CHK which forces validation calls down
  1677. * into this function always.
  1678. */
  1679. rt6_dst_from_metrics_check(rt);
  1680. if (rt->rt6i_flags & RTF_PCPU ||
  1681. (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->from))
  1682. return rt6_dst_from_check(rt, cookie);
  1683. else
  1684. return rt6_check(rt, cookie);
  1685. }
  1686. static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
  1687. {
  1688. struct rt6_info *rt = (struct rt6_info *) dst;
  1689. if (rt) {
  1690. if (rt->rt6i_flags & RTF_CACHE) {
  1691. if (rt6_check_expired(rt)) {
  1692. ip6_del_rt(rt);
  1693. dst = NULL;
  1694. }
  1695. } else {
  1696. dst_release(dst);
  1697. dst = NULL;
  1698. }
  1699. }
  1700. return dst;
  1701. }
  1702. static void ip6_link_failure(struct sk_buff *skb)
  1703. {
  1704. struct rt6_info *rt;
  1705. icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
  1706. rt = (struct rt6_info *) skb_dst(skb);
  1707. if (rt) {
  1708. if (rt->rt6i_flags & RTF_CACHE) {
  1709. if (dst_hold_safe(&rt->dst))
  1710. ip6_del_rt(rt);
  1711. } else {
  1712. struct fib6_node *fn;
  1713. rcu_read_lock();
  1714. fn = rcu_dereference(rt->rt6i_node);
  1715. if (fn && (rt->rt6i_flags & RTF_DEFAULT))
  1716. fn->fn_sernum = -1;
  1717. rcu_read_unlock();
  1718. }
  1719. }
  1720. }
  1721. static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
  1722. {
  1723. struct net *net = dev_net(rt->dst.dev);
  1724. rt->rt6i_flags |= RTF_MODIFIED;
  1725. rt->rt6i_pmtu = mtu;
  1726. rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
  1727. }
  1728. static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
  1729. {
  1730. return !(rt->rt6i_flags & RTF_CACHE) &&
  1731. (rt->rt6i_flags & RTF_PCPU ||
  1732. rcu_access_pointer(rt->rt6i_node));
  1733. }
  1734. static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
  1735. const struct ipv6hdr *iph, u32 mtu)
  1736. {
  1737. const struct in6_addr *daddr, *saddr;
  1738. struct rt6_info *rt6 = (struct rt6_info *)dst;
  1739. if (rt6->rt6i_flags & RTF_LOCAL)
  1740. return;
  1741. if (dst_metric_locked(dst, RTAX_MTU))
  1742. return;
  1743. if (iph) {
  1744. daddr = &iph->daddr;
  1745. saddr = &iph->saddr;
  1746. } else if (sk) {
  1747. daddr = &sk->sk_v6_daddr;
  1748. saddr = &inet6_sk(sk)->saddr;
  1749. } else {
  1750. daddr = NULL;
  1751. saddr = NULL;
  1752. }
  1753. dst_confirm_neigh(dst, daddr);
  1754. mtu = max_t(u32, mtu, IPV6_MIN_MTU);
  1755. if (mtu >= dst_mtu(dst))
  1756. return;
  1757. if (!rt6_cache_allowed_for_pmtu(rt6)) {
  1758. rt6_do_update_pmtu(rt6, mtu);
  1759. /* update rt6_ex->stamp for cache */
  1760. if (rt6->rt6i_flags & RTF_CACHE)
  1761. rt6_update_exception_stamp_rt(rt6);
  1762. } else if (daddr) {
  1763. struct rt6_info *nrt6;
  1764. nrt6 = ip6_rt_cache_alloc(rt6, daddr, saddr);
  1765. if (nrt6) {
  1766. rt6_do_update_pmtu(nrt6, mtu);
  1767. if (rt6_insert_exception(nrt6, rt6))
  1768. dst_release_immediate(&nrt6->dst);
  1769. }
  1770. }
  1771. }
  1772. static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
  1773. struct sk_buff *skb, u32 mtu)
  1774. {
  1775. __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
  1776. }
  1777. void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
  1778. int oif, u32 mark, kuid_t uid)
  1779. {
  1780. const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
  1781. struct dst_entry *dst;
  1782. struct flowi6 fl6;
  1783. memset(&fl6, 0, sizeof(fl6));
  1784. fl6.flowi6_oif = oif;
  1785. fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark);
  1786. fl6.daddr = iph->daddr;
  1787. fl6.saddr = iph->saddr;
  1788. fl6.flowlabel = ip6_flowinfo(iph);
  1789. fl6.flowi6_uid = uid;
  1790. dst = ip6_route_output(net, NULL, &fl6);
  1791. if (!dst->error)
  1792. __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
  1793. dst_release(dst);
  1794. }
  1795. EXPORT_SYMBOL_GPL(ip6_update_pmtu);
  1796. void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
  1797. {
  1798. struct dst_entry *dst;
  1799. ip6_update_pmtu(skb, sock_net(sk), mtu,
  1800. sk->sk_bound_dev_if, sk->sk_mark, sk->sk_uid);
  1801. dst = __sk_dst_get(sk);
  1802. if (!dst || !dst->obsolete ||
  1803. dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
  1804. return;
  1805. bh_lock_sock(sk);
  1806. if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
  1807. ip6_datagram_dst_update(sk, false);
  1808. bh_unlock_sock(sk);
  1809. }
  1810. EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
  1811. /* Handle redirects */
  1812. struct ip6rd_flowi {
  1813. struct flowi6 fl6;
  1814. struct in6_addr gateway;
  1815. };
  1816. static struct rt6_info *__ip6_route_redirect(struct net *net,
  1817. struct fib6_table *table,
  1818. struct flowi6 *fl6,
  1819. int flags)
  1820. {
  1821. struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
  1822. struct rt6_info *rt, *rt_cache;
  1823. struct fib6_node *fn;
  1824. /* Get the "current" route for this destination and
  1825. * check if the redirect has come from appropriate router.
  1826. *
  1827. * RFC 4861 specifies that redirects should only be
  1828. * accepted if they come from the nexthop to the target.
  1829. * Due to the way the routes are chosen, this notion
  1830. * is a bit fuzzy and one might need to check all possible
  1831. * routes.
  1832. */
  1833. rcu_read_lock();
  1834. fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
  1835. restart:
  1836. for_each_fib6_node_rt_rcu(fn) {
  1837. if (rt6_check_expired(rt))
  1838. continue;
  1839. if (rt->dst.error)
  1840. break;
  1841. if (!(rt->rt6i_flags & RTF_GATEWAY))
  1842. continue;
  1843. if (fl6->flowi6_oif != rt->dst.dev->ifindex)
  1844. continue;
  1845. /* rt_cache's gateway might be different from its 'parent'
  1846. * in the case of an ip redirect.
  1847. * So we keep searching in the exception table if the gateway
  1848. * is different.
  1849. */
  1850. if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway)) {
  1851. rt_cache = rt6_find_cached_rt(rt,
  1852. &fl6->daddr,
  1853. &fl6->saddr);
  1854. if (rt_cache &&
  1855. ipv6_addr_equal(&rdfl->gateway,
  1856. &rt_cache->rt6i_gateway)) {
  1857. rt = rt_cache;
  1858. break;
  1859. }
  1860. continue;
  1861. }
  1862. break;
  1863. }
  1864. if (!rt)
  1865. rt = net->ipv6.ip6_null_entry;
  1866. else if (rt->dst.error) {
  1867. rt = net->ipv6.ip6_null_entry;
  1868. goto out;
  1869. }
  1870. if (rt == net->ipv6.ip6_null_entry) {
  1871. fn = fib6_backtrack(fn, &fl6->saddr);
  1872. if (fn)
  1873. goto restart;
  1874. }
  1875. out:
  1876. ip6_hold_safe(net, &rt, true);
  1877. rcu_read_unlock();
  1878. trace_fib6_table_lookup(net, rt, table, fl6);
  1879. return rt;
  1880. };
  1881. static struct dst_entry *ip6_route_redirect(struct net *net,
  1882. const struct flowi6 *fl6,
  1883. const struct in6_addr *gateway)
  1884. {
  1885. int flags = RT6_LOOKUP_F_HAS_SADDR;
  1886. struct ip6rd_flowi rdfl;
  1887. rdfl.fl6 = *fl6;
  1888. rdfl.gateway = *gateway;
  1889. return fib6_rule_lookup(net, &rdfl.fl6,
  1890. flags, __ip6_route_redirect);
  1891. }
  1892. void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
  1893. kuid_t uid)
  1894. {
  1895. const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
  1896. struct dst_entry *dst;
  1897. struct flowi6 fl6;
  1898. memset(&fl6, 0, sizeof(fl6));
  1899. fl6.flowi6_iif = LOOPBACK_IFINDEX;
  1900. fl6.flowi6_oif = oif;
  1901. fl6.flowi6_mark = mark;
  1902. fl6.daddr = iph->daddr;
  1903. fl6.saddr = iph->saddr;
  1904. fl6.flowlabel = ip6_flowinfo(iph);
  1905. fl6.flowi6_uid = uid;
  1906. dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr);
  1907. rt6_do_redirect(dst, NULL, skb);
  1908. dst_release(dst);
  1909. }
  1910. EXPORT_SYMBOL_GPL(ip6_redirect);
  1911. void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif,
  1912. u32 mark)
  1913. {
  1914. const struct ipv6hdr *iph = ipv6_hdr(skb);
  1915. const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
  1916. struct dst_entry *dst;
  1917. struct flowi6 fl6;
  1918. memset(&fl6, 0, sizeof(fl6));
  1919. fl6.flowi6_iif = LOOPBACK_IFINDEX;
  1920. fl6.flowi6_oif = oif;
  1921. fl6.flowi6_mark = mark;
  1922. fl6.daddr = msg->dest;
  1923. fl6.saddr = iph->daddr;
  1924. fl6.flowi6_uid = sock_net_uid(net, NULL);
  1925. dst = ip6_route_redirect(net, &fl6, &iph->saddr);
  1926. rt6_do_redirect(dst, NULL, skb);
  1927. dst_release(dst);
  1928. }
  1929. void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
  1930. {
  1931. ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
  1932. sk->sk_uid);
  1933. }
  1934. EXPORT_SYMBOL_GPL(ip6_sk_redirect);
  1935. static unsigned int ip6_default_advmss(const struct dst_entry *dst)
  1936. {
  1937. struct net_device *dev = dst->dev;
  1938. unsigned int mtu = dst_mtu(dst);
  1939. struct net *net = dev_net(dev);
  1940. mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
  1941. if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
  1942. mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
  1943. /*
  1944. * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
  1945. * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
  1946. * IPV6_MAXPLEN is also valid and means: "any MSS,
  1947. * rely only on pmtu discovery"
  1948. */
  1949. if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
  1950. mtu = IPV6_MAXPLEN;
  1951. return mtu;
  1952. }
  1953. static unsigned int ip6_mtu(const struct dst_entry *dst)
  1954. {
  1955. const struct rt6_info *rt = (const struct rt6_info *)dst;
  1956. unsigned int mtu = rt->rt6i_pmtu;
  1957. struct inet6_dev *idev;
  1958. if (mtu)
  1959. goto out;
  1960. mtu = dst_metric_raw(dst, RTAX_MTU);
  1961. if (mtu)
  1962. goto out;
  1963. mtu = IPV6_MIN_MTU;
  1964. rcu_read_lock();
  1965. idev = __in6_dev_get(dst->dev);
  1966. if (idev)
  1967. mtu = idev->cnf.mtu6;
  1968. rcu_read_unlock();
  1969. out:
  1970. mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
  1971. return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
  1972. }
  1973. struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
  1974. struct flowi6 *fl6)
  1975. {
  1976. struct dst_entry *dst;
  1977. struct rt6_info *rt;
  1978. struct inet6_dev *idev = in6_dev_get(dev);
  1979. struct net *net = dev_net(dev);
  1980. if (unlikely(!idev))
  1981. return ERR_PTR(-ENODEV);
  1982. rt = ip6_dst_alloc(net, dev, 0);
  1983. if (unlikely(!rt)) {
  1984. in6_dev_put(idev);
  1985. dst = ERR_PTR(-ENOMEM);
  1986. goto out;
  1987. }
  1988. rt->dst.flags |= DST_HOST;
  1989. rt->dst.input = ip6_input;
  1990. rt->dst.output = ip6_output;
  1991. rt->rt6i_gateway = fl6->daddr;
  1992. rt->rt6i_dst.addr = fl6->daddr;
  1993. rt->rt6i_dst.plen = 128;
  1994. rt->rt6i_idev = idev;
  1995. dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
  1996. /* Add this dst into uncached_list so that rt6_ifdown() can
  1997. * do proper release of the net_device
  1998. */
  1999. rt6_uncached_list_add(rt);
  2000. atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
  2001. dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
  2002. out:
  2003. return dst;
  2004. }
  2005. static int ip6_dst_gc(struct dst_ops *ops)
  2006. {
  2007. struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
  2008. int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
  2009. int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
  2010. int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
  2011. int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
  2012. unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
  2013. int entries;
  2014. entries = dst_entries_get_fast(ops);
  2015. if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
  2016. entries <= rt_max_size)
  2017. goto out;
  2018. net->ipv6.ip6_rt_gc_expire++;
  2019. fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
  2020. entries = dst_entries_get_slow(ops);
  2021. if (entries < ops->gc_thresh)
  2022. net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
  2023. out:
  2024. net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
  2025. return entries > rt_max_size;
  2026. }
  2027. static int ip6_convert_metrics(struct mx6_config *mxc,
  2028. const struct fib6_config *cfg)
  2029. {
  2030. struct net *net = cfg->fc_nlinfo.nl_net;
  2031. bool ecn_ca = false;
  2032. struct nlattr *nla;
  2033. int remaining;
  2034. u32 *mp;
  2035. if (!cfg->fc_mx)
  2036. return 0;
  2037. mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
  2038. if (unlikely(!mp))
  2039. return -ENOMEM;
  2040. nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
  2041. int type = nla_type(nla);
  2042. u32 val;
  2043. if (!type)
  2044. continue;
  2045. if (unlikely(type > RTAX_MAX))
  2046. goto err;
  2047. if (type == RTAX_CC_ALGO) {
  2048. char tmp[TCP_CA_NAME_MAX];
  2049. nla_strlcpy(tmp, nla, sizeof(tmp));
  2050. val = tcp_ca_get_key_by_name(net, tmp, &ecn_ca);
  2051. if (val == TCP_CA_UNSPEC)
  2052. goto err;
  2053. } else {
  2054. val = nla_get_u32(nla);
  2055. }
  2056. if (type == RTAX_HOPLIMIT && val > 255)
  2057. val = 255;
  2058. if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK))
  2059. goto err;
  2060. mp[type - 1] = val;
  2061. __set_bit(type - 1, mxc->mx_valid);
  2062. }
  2063. if (ecn_ca) {
  2064. __set_bit(RTAX_FEATURES - 1, mxc->mx_valid);
  2065. mp[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA;
  2066. }
  2067. mxc->mx = mp;
  2068. return 0;
  2069. err:
  2070. kfree(mp);
  2071. return -EINVAL;
  2072. }
  2073. static struct rt6_info *ip6_nh_lookup_table(struct net *net,
  2074. struct fib6_config *cfg,
  2075. const struct in6_addr *gw_addr)
  2076. {
  2077. struct flowi6 fl6 = {
  2078. .flowi6_oif = cfg->fc_ifindex,
  2079. .daddr = *gw_addr,
  2080. .saddr = cfg->fc_prefsrc,
  2081. };
  2082. struct fib6_table *table;
  2083. struct rt6_info *rt;
  2084. int flags = RT6_LOOKUP_F_IFACE | RT6_LOOKUP_F_IGNORE_LINKSTATE;
  2085. table = fib6_get_table(net, cfg->fc_table);
  2086. if (!table)
  2087. return NULL;
  2088. if (!ipv6_addr_any(&cfg->fc_prefsrc))
  2089. flags |= RT6_LOOKUP_F_HAS_SADDR;
  2090. rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, flags);
  2091. /* if table lookup failed, fall back to full lookup */
  2092. if (rt == net->ipv6.ip6_null_entry) {
  2093. ip6_rt_put(rt);
  2094. rt = NULL;
  2095. }
  2096. return rt;
  2097. }
  2098. static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg,
  2099. struct netlink_ext_ack *extack)
  2100. {
  2101. struct net *net = cfg->fc_nlinfo.nl_net;
  2102. struct rt6_info *rt = NULL;
  2103. struct net_device *dev = NULL;
  2104. struct inet6_dev *idev = NULL;
  2105. struct fib6_table *table;
  2106. int addr_type;
  2107. int err = -EINVAL;
  2108. /* RTF_PCPU is an internal flag; can not be set by userspace */
  2109. if (cfg->fc_flags & RTF_PCPU) {
  2110. NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
  2111. goto out;
  2112. }
  2113. /* RTF_CACHE is an internal flag; can not be set by userspace */
  2114. if (cfg->fc_flags & RTF_CACHE) {
  2115. NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
  2116. goto out;
  2117. }
  2118. if (cfg->fc_dst_len > 128) {
  2119. NL_SET_ERR_MSG(extack, "Invalid prefix length");
  2120. goto out;
  2121. }
  2122. if (cfg->fc_src_len > 128) {
  2123. NL_SET_ERR_MSG(extack, "Invalid source address length");
  2124. goto out;
  2125. }
  2126. #ifndef CONFIG_IPV6_SUBTREES
  2127. if (cfg->fc_src_len) {
  2128. NL_SET_ERR_MSG(extack,
  2129. "Specifying source address requires IPV6_SUBTREES to be enabled");
  2130. goto out;
  2131. }
  2132. #endif
  2133. if (cfg->fc_ifindex) {
  2134. err = -ENODEV;
  2135. dev = dev_get_by_index(net, cfg->fc_ifindex);
  2136. if (!dev)
  2137. goto out;
  2138. idev = in6_dev_get(dev);
  2139. if (!idev)
  2140. goto out;
  2141. }
  2142. if (cfg->fc_metric == 0)
  2143. cfg->fc_metric = IP6_RT_PRIO_USER;
  2144. err = -ENOBUFS;
  2145. if (cfg->fc_nlinfo.nlh &&
  2146. !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
  2147. table = fib6_get_table(net, cfg->fc_table);
  2148. if (!table) {
  2149. pr_warn("NLM_F_CREATE should be specified when creating new route\n");
  2150. table = fib6_new_table(net, cfg->fc_table);
  2151. }
  2152. } else {
  2153. table = fib6_new_table(net, cfg->fc_table);
  2154. }
  2155. if (!table)
  2156. goto out;
  2157. rt = ip6_dst_alloc(net, NULL,
  2158. (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT);
  2159. if (!rt) {
  2160. err = -ENOMEM;
  2161. goto out;
  2162. }
  2163. if (cfg->fc_flags & RTF_EXPIRES)
  2164. rt6_set_expires(rt, jiffies +
  2165. clock_t_to_jiffies(cfg->fc_expires));
  2166. else
  2167. rt6_clean_expires(rt);
  2168. if (cfg->fc_protocol == RTPROT_UNSPEC)
  2169. cfg->fc_protocol = RTPROT_BOOT;
  2170. rt->rt6i_protocol = cfg->fc_protocol;
  2171. addr_type = ipv6_addr_type(&cfg->fc_dst);
  2172. if (addr_type & IPV6_ADDR_MULTICAST)
  2173. rt->dst.input = ip6_mc_input;
  2174. else if (cfg->fc_flags & RTF_LOCAL)
  2175. rt->dst.input = ip6_input;
  2176. else
  2177. rt->dst.input = ip6_forward;
  2178. rt->dst.output = ip6_output;
  2179. if (cfg->fc_encap) {
  2180. struct lwtunnel_state *lwtstate;
  2181. err = lwtunnel_build_state(cfg->fc_encap_type,
  2182. cfg->fc_encap, AF_INET6, cfg,
  2183. &lwtstate, extack);
  2184. if (err)
  2185. goto out;
  2186. rt->dst.lwtstate = lwtstate_get(lwtstate);
  2187. if (lwtunnel_output_redirect(rt->dst.lwtstate)) {
  2188. rt->dst.lwtstate->orig_output = rt->dst.output;
  2189. rt->dst.output = lwtunnel_output;
  2190. }
  2191. if (lwtunnel_input_redirect(rt->dst.lwtstate)) {
  2192. rt->dst.lwtstate->orig_input = rt->dst.input;
  2193. rt->dst.input = lwtunnel_input;
  2194. }
  2195. }
  2196. ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
  2197. rt->rt6i_dst.plen = cfg->fc_dst_len;
  2198. if (rt->rt6i_dst.plen == 128)
  2199. rt->dst.flags |= DST_HOST;
  2200. #ifdef CONFIG_IPV6_SUBTREES
  2201. ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
  2202. rt->rt6i_src.plen = cfg->fc_src_len;
  2203. #endif
  2204. rt->rt6i_metric = cfg->fc_metric;
  2205. /* We cannot add true routes via loopback here,
  2206. they would result in kernel looping; promote them to reject routes
  2207. */
  2208. if ((cfg->fc_flags & RTF_REJECT) ||
  2209. (dev && (dev->flags & IFF_LOOPBACK) &&
  2210. !(addr_type & IPV6_ADDR_LOOPBACK) &&
  2211. !(cfg->fc_flags & RTF_LOCAL))) {
  2212. /* hold loopback dev/idev if we haven't done so. */
  2213. if (dev != net->loopback_dev) {
  2214. if (dev) {
  2215. dev_put(dev);
  2216. in6_dev_put(idev);
  2217. }
  2218. dev = net->loopback_dev;
  2219. dev_hold(dev);
  2220. idev = in6_dev_get(dev);
  2221. if (!idev) {
  2222. err = -ENODEV;
  2223. goto out;
  2224. }
  2225. }
  2226. rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
  2227. switch (cfg->fc_type) {
  2228. case RTN_BLACKHOLE:
  2229. rt->dst.error = -EINVAL;
  2230. rt->dst.output = dst_discard_out;
  2231. rt->dst.input = dst_discard;
  2232. break;
  2233. case RTN_PROHIBIT:
  2234. rt->dst.error = -EACCES;
  2235. rt->dst.output = ip6_pkt_prohibit_out;
  2236. rt->dst.input = ip6_pkt_prohibit;
  2237. break;
  2238. case RTN_THROW:
  2239. case RTN_UNREACHABLE:
  2240. default:
  2241. rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN
  2242. : (cfg->fc_type == RTN_UNREACHABLE)
  2243. ? -EHOSTUNREACH : -ENETUNREACH;
  2244. rt->dst.output = ip6_pkt_discard_out;
  2245. rt->dst.input = ip6_pkt_discard;
  2246. break;
  2247. }
  2248. goto install_route;
  2249. }
  2250. if (cfg->fc_flags & RTF_GATEWAY) {
  2251. const struct in6_addr *gw_addr;
  2252. int gwa_type;
  2253. gw_addr = &cfg->fc_gateway;
  2254. gwa_type = ipv6_addr_type(gw_addr);
  2255. /* if gw_addr is local we will fail to detect this in case
  2256. * address is still TENTATIVE (DAD in progress). rt6_lookup()
  2257. * will return already-added prefix route via interface that
  2258. * prefix route was assigned to, which might be non-loopback.
  2259. */
  2260. err = -EINVAL;
  2261. if (ipv6_chk_addr_and_flags(net, gw_addr,
  2262. gwa_type & IPV6_ADDR_LINKLOCAL ?
  2263. dev : NULL, 0, 0)) {
  2264. NL_SET_ERR_MSG(extack, "Invalid gateway address");
  2265. goto out;
  2266. }
  2267. rt->rt6i_gateway = *gw_addr;
  2268. if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
  2269. struct rt6_info *grt = NULL;
  2270. /* IPv6 strictly inhibits using not link-local
  2271. addresses as nexthop address.
  2272. Otherwise, router will not able to send redirects.
  2273. It is very good, but in some (rare!) circumstances
  2274. (SIT, PtP, NBMA NOARP links) it is handy to allow
  2275. some exceptions. --ANK
  2276. We allow IPv4-mapped nexthops to support RFC4798-type
  2277. addressing
  2278. */
  2279. if (!(gwa_type & (IPV6_ADDR_UNICAST |
  2280. IPV6_ADDR_MAPPED))) {
  2281. NL_SET_ERR_MSG(extack,
  2282. "Invalid gateway address");
  2283. goto out;
  2284. }
  2285. if (cfg->fc_table) {
  2286. grt = ip6_nh_lookup_table(net, cfg, gw_addr);
  2287. if (grt) {
  2288. if (grt->rt6i_flags & RTF_GATEWAY ||
  2289. (dev && dev != grt->dst.dev)) {
  2290. ip6_rt_put(grt);
  2291. grt = NULL;
  2292. }
  2293. }
  2294. }
  2295. if (!grt)
  2296. grt = rt6_lookup(net, gw_addr, NULL,
  2297. cfg->fc_ifindex, 1);
  2298. err = -EHOSTUNREACH;
  2299. if (!grt)
  2300. goto out;
  2301. if (dev) {
  2302. if (dev != grt->dst.dev) {
  2303. ip6_rt_put(grt);
  2304. goto out;
  2305. }
  2306. } else {
  2307. dev = grt->dst.dev;
  2308. idev = grt->rt6i_idev;
  2309. dev_hold(dev);
  2310. in6_dev_hold(grt->rt6i_idev);
  2311. }
  2312. if (!(grt->rt6i_flags & RTF_GATEWAY))
  2313. err = 0;
  2314. ip6_rt_put(grt);
  2315. if (err)
  2316. goto out;
  2317. }
  2318. err = -EINVAL;
  2319. if (!dev) {
  2320. NL_SET_ERR_MSG(extack, "Egress device not specified");
  2321. goto out;
  2322. } else if (dev->flags & IFF_LOOPBACK) {
  2323. NL_SET_ERR_MSG(extack,
  2324. "Egress device can not be loopback device for this route");
  2325. goto out;
  2326. }
  2327. }
  2328. err = -ENODEV;
  2329. if (!dev)
  2330. goto out;
  2331. if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
  2332. if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
  2333. NL_SET_ERR_MSG(extack, "Invalid source address");
  2334. err = -EINVAL;
  2335. goto out;
  2336. }
  2337. rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
  2338. rt->rt6i_prefsrc.plen = 128;
  2339. } else
  2340. rt->rt6i_prefsrc.plen = 0;
  2341. rt->rt6i_flags = cfg->fc_flags;
  2342. install_route:
  2343. rt->dst.dev = dev;
  2344. rt->rt6i_idev = idev;
  2345. rt->rt6i_table = table;
  2346. cfg->fc_nlinfo.nl_net = dev_net(dev);
  2347. return rt;
  2348. out:
  2349. if (dev)
  2350. dev_put(dev);
  2351. if (idev)
  2352. in6_dev_put(idev);
  2353. if (rt)
  2354. dst_release_immediate(&rt->dst);
  2355. return ERR_PTR(err);
  2356. }
  2357. int ip6_route_add(struct fib6_config *cfg,
  2358. struct netlink_ext_ack *extack)
  2359. {
  2360. struct mx6_config mxc = { .mx = NULL, };
  2361. struct rt6_info *rt;
  2362. int err;
  2363. rt = ip6_route_info_create(cfg, extack);
  2364. if (IS_ERR(rt)) {
  2365. err = PTR_ERR(rt);
  2366. rt = NULL;
  2367. goto out;
  2368. }
  2369. err = ip6_convert_metrics(&mxc, cfg);
  2370. if (err)
  2371. goto out;
  2372. err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, &mxc, extack);
  2373. kfree(mxc.mx);
  2374. return err;
  2375. out:
  2376. if (rt)
  2377. dst_release_immediate(&rt->dst);
  2378. return err;
  2379. }
  2380. static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
  2381. {
  2382. int err;
  2383. struct fib6_table *table;
  2384. struct net *net = dev_net(rt->dst.dev);
  2385. if (rt == net->ipv6.ip6_null_entry) {
  2386. err = -ENOENT;
  2387. goto out;
  2388. }
  2389. table = rt->rt6i_table;
  2390. spin_lock_bh(&table->tb6_lock);
  2391. err = fib6_del(rt, info);
  2392. spin_unlock_bh(&table->tb6_lock);
  2393. out:
  2394. ip6_rt_put(rt);
  2395. return err;
  2396. }
  2397. int ip6_del_rt(struct rt6_info *rt)
  2398. {
  2399. struct nl_info info = {
  2400. .nl_net = dev_net(rt->dst.dev),
  2401. };
  2402. return __ip6_del_rt(rt, &info);
  2403. }
  2404. static int __ip6_del_rt_siblings(struct rt6_info *rt, struct fib6_config *cfg)
  2405. {
  2406. struct nl_info *info = &cfg->fc_nlinfo;
  2407. struct net *net = info->nl_net;
  2408. struct sk_buff *skb = NULL;
  2409. struct fib6_table *table;
  2410. int err = -ENOENT;
  2411. if (rt == net->ipv6.ip6_null_entry)
  2412. goto out_put;
  2413. table = rt->rt6i_table;
  2414. spin_lock_bh(&table->tb6_lock);
  2415. if (rt->rt6i_nsiblings && cfg->fc_delete_all_nh) {
  2416. struct rt6_info *sibling, *next_sibling;
  2417. /* prefer to send a single notification with all hops */
  2418. skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
  2419. if (skb) {
  2420. u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
  2421. if (rt6_fill_node(net, skb, rt,
  2422. NULL, NULL, 0, RTM_DELROUTE,
  2423. info->portid, seq, 0) < 0) {
  2424. kfree_skb(skb);
  2425. skb = NULL;
  2426. } else
  2427. info->skip_notify = 1;
  2428. }
  2429. list_for_each_entry_safe(sibling, next_sibling,
  2430. &rt->rt6i_siblings,
  2431. rt6i_siblings) {
  2432. err = fib6_del(sibling, info);
  2433. if (err)
  2434. goto out_unlock;
  2435. }
  2436. }
  2437. err = fib6_del(rt, info);
  2438. out_unlock:
  2439. spin_unlock_bh(&table->tb6_lock);
  2440. out_put:
  2441. ip6_rt_put(rt);
  2442. if (skb) {
  2443. rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
  2444. info->nlh, gfp_any());
  2445. }
  2446. return err;
  2447. }
  2448. static int ip6_route_del(struct fib6_config *cfg,
  2449. struct netlink_ext_ack *extack)
  2450. {
  2451. struct rt6_info *rt, *rt_cache;
  2452. struct fib6_table *table;
  2453. struct fib6_node *fn;
  2454. int err = -ESRCH;
  2455. table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
  2456. if (!table) {
  2457. NL_SET_ERR_MSG(extack, "FIB table does not exist");
  2458. return err;
  2459. }
  2460. rcu_read_lock();
  2461. fn = fib6_locate(&table->tb6_root,
  2462. &cfg->fc_dst, cfg->fc_dst_len,
  2463. &cfg->fc_src, cfg->fc_src_len,
  2464. !(cfg->fc_flags & RTF_CACHE));
  2465. if (fn) {
  2466. for_each_fib6_node_rt_rcu(fn) {
  2467. if (cfg->fc_flags & RTF_CACHE) {
  2468. rt_cache = rt6_find_cached_rt(rt, &cfg->fc_dst,
  2469. &cfg->fc_src);
  2470. if (!rt_cache)
  2471. continue;
  2472. rt = rt_cache;
  2473. }
  2474. if (cfg->fc_ifindex &&
  2475. (!rt->dst.dev ||
  2476. rt->dst.dev->ifindex != cfg->fc_ifindex))
  2477. continue;
  2478. if (cfg->fc_flags & RTF_GATEWAY &&
  2479. !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
  2480. continue;
  2481. if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
  2482. continue;
  2483. if (cfg->fc_protocol && cfg->fc_protocol != rt->rt6i_protocol)
  2484. continue;
  2485. if (!dst_hold_safe(&rt->dst))
  2486. break;
  2487. rcu_read_unlock();
  2488. /* if gateway was specified only delete the one hop */
  2489. if (cfg->fc_flags & RTF_GATEWAY)
  2490. return __ip6_del_rt(rt, &cfg->fc_nlinfo);
  2491. return __ip6_del_rt_siblings(rt, cfg);
  2492. }
  2493. }
  2494. rcu_read_unlock();
  2495. return err;
  2496. }
  2497. static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
  2498. {
  2499. struct netevent_redirect netevent;
  2500. struct rt6_info *rt, *nrt = NULL;
  2501. struct ndisc_options ndopts;
  2502. struct inet6_dev *in6_dev;
  2503. struct neighbour *neigh;
  2504. struct rd_msg *msg;
  2505. int optlen, on_link;
  2506. u8 *lladdr;
  2507. optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
  2508. optlen -= sizeof(*msg);
  2509. if (optlen < 0) {
  2510. net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
  2511. return;
  2512. }
  2513. msg = (struct rd_msg *)icmp6_hdr(skb);
  2514. if (ipv6_addr_is_multicast(&msg->dest)) {
  2515. net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
  2516. return;
  2517. }
  2518. on_link = 0;
  2519. if (ipv6_addr_equal(&msg->dest, &msg->target)) {
  2520. on_link = 1;
  2521. } else if (ipv6_addr_type(&msg->target) !=
  2522. (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
  2523. net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
  2524. return;
  2525. }
  2526. in6_dev = __in6_dev_get(skb->dev);
  2527. if (!in6_dev)
  2528. return;
  2529. if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
  2530. return;
  2531. /* RFC2461 8.1:
  2532. * The IP source address of the Redirect MUST be the same as the current
  2533. * first-hop router for the specified ICMP Destination Address.
  2534. */
  2535. if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
  2536. net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
  2537. return;
  2538. }
  2539. lladdr = NULL;
  2540. if (ndopts.nd_opts_tgt_lladdr) {
  2541. lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
  2542. skb->dev);
  2543. if (!lladdr) {
  2544. net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
  2545. return;
  2546. }
  2547. }
  2548. rt = (struct rt6_info *) dst;
  2549. if (rt->rt6i_flags & RTF_REJECT) {
  2550. net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
  2551. return;
  2552. }
  2553. /* Redirect received -> path was valid.
  2554. * Look, redirects are sent only in response to data packets,
  2555. * so that this nexthop apparently is reachable. --ANK
  2556. */
  2557. dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
  2558. neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
  2559. if (!neigh)
  2560. return;
  2561. /*
  2562. * We have finally decided to accept it.
  2563. */
  2564. ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
  2565. NEIGH_UPDATE_F_WEAK_OVERRIDE|
  2566. NEIGH_UPDATE_F_OVERRIDE|
  2567. (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
  2568. NEIGH_UPDATE_F_ISROUTER)),
  2569. NDISC_REDIRECT, &ndopts);
  2570. nrt = ip6_rt_cache_alloc(rt, &msg->dest, NULL);
  2571. if (!nrt)
  2572. goto out;
  2573. nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
  2574. if (on_link)
  2575. nrt->rt6i_flags &= ~RTF_GATEWAY;
  2576. nrt->rt6i_protocol = RTPROT_REDIRECT;
  2577. nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
  2578. /* No need to remove rt from the exception table if rt is
  2579. * a cached route because rt6_insert_exception() will
  2580. * takes care of it
  2581. */
  2582. if (rt6_insert_exception(nrt, rt)) {
  2583. dst_release_immediate(&nrt->dst);
  2584. goto out;
  2585. }
  2586. netevent.old = &rt->dst;
  2587. netevent.new = &nrt->dst;
  2588. netevent.daddr = &msg->dest;
  2589. netevent.neigh = neigh;
  2590. call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
  2591. out:
  2592. neigh_release(neigh);
  2593. }
  2594. /*
  2595. * Misc support functions
  2596. */
  2597. static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from)
  2598. {
  2599. BUG_ON(from->from);
  2600. rt->rt6i_flags &= ~RTF_EXPIRES;
  2601. dst_hold(&from->dst);
  2602. rt->from = from;
  2603. dst_init_metrics(&rt->dst, dst_metrics_ptr(&from->dst), true);
  2604. }
  2605. static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort)
  2606. {
  2607. rt->dst.input = ort->dst.input;
  2608. rt->dst.output = ort->dst.output;
  2609. rt->rt6i_dst = ort->rt6i_dst;
  2610. rt->dst.error = ort->dst.error;
  2611. rt->rt6i_idev = ort->rt6i_idev;
  2612. if (rt->rt6i_idev)
  2613. in6_dev_hold(rt->rt6i_idev);
  2614. rt->dst.lastuse = jiffies;
  2615. rt->rt6i_gateway = ort->rt6i_gateway;
  2616. rt->rt6i_flags = ort->rt6i_flags;
  2617. rt6_set_from(rt, ort);
  2618. rt->rt6i_metric = ort->rt6i_metric;
  2619. #ifdef CONFIG_IPV6_SUBTREES
  2620. rt->rt6i_src = ort->rt6i_src;
  2621. #endif
  2622. rt->rt6i_prefsrc = ort->rt6i_prefsrc;
  2623. rt->rt6i_table = ort->rt6i_table;
  2624. rt->dst.lwtstate = lwtstate_get(ort->dst.lwtstate);
  2625. }
  2626. #ifdef CONFIG_IPV6_ROUTE_INFO
  2627. static struct rt6_info *rt6_get_route_info(struct net *net,
  2628. const struct in6_addr *prefix, int prefixlen,
  2629. const struct in6_addr *gwaddr,
  2630. struct net_device *dev)
  2631. {
  2632. u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
  2633. int ifindex = dev->ifindex;
  2634. struct fib6_node *fn;
  2635. struct rt6_info *rt = NULL;
  2636. struct fib6_table *table;
  2637. table = fib6_get_table(net, tb_id);
  2638. if (!table)
  2639. return NULL;
  2640. rcu_read_lock();
  2641. fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
  2642. if (!fn)
  2643. goto out;
  2644. for_each_fib6_node_rt_rcu(fn) {
  2645. if (rt->dst.dev->ifindex != ifindex)
  2646. continue;
  2647. if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
  2648. continue;
  2649. if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
  2650. continue;
  2651. ip6_hold_safe(NULL, &rt, false);
  2652. break;
  2653. }
  2654. out:
  2655. rcu_read_unlock();
  2656. return rt;
  2657. }
  2658. static struct rt6_info *rt6_add_route_info(struct net *net,
  2659. const struct in6_addr *prefix, int prefixlen,
  2660. const struct in6_addr *gwaddr,
  2661. struct net_device *dev,
  2662. unsigned int pref)
  2663. {
  2664. struct fib6_config cfg = {
  2665. .fc_metric = IP6_RT_PRIO_USER,
  2666. .fc_ifindex = dev->ifindex,
  2667. .fc_dst_len = prefixlen,
  2668. .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
  2669. RTF_UP | RTF_PREF(pref),
  2670. .fc_protocol = RTPROT_RA,
  2671. .fc_nlinfo.portid = 0,
  2672. .fc_nlinfo.nlh = NULL,
  2673. .fc_nlinfo.nl_net = net,
  2674. };
  2675. cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
  2676. cfg.fc_dst = *prefix;
  2677. cfg.fc_gateway = *gwaddr;
  2678. /* We should treat it as a default route if prefix length is 0. */
  2679. if (!prefixlen)
  2680. cfg.fc_flags |= RTF_DEFAULT;
  2681. ip6_route_add(&cfg, NULL);
  2682. return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
  2683. }
  2684. #endif
  2685. struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
  2686. {
  2687. u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
  2688. struct rt6_info *rt;
  2689. struct fib6_table *table;
  2690. table = fib6_get_table(dev_net(dev), tb_id);
  2691. if (!table)
  2692. return NULL;
  2693. rcu_read_lock();
  2694. for_each_fib6_node_rt_rcu(&table->tb6_root) {
  2695. if (dev == rt->dst.dev &&
  2696. ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
  2697. ipv6_addr_equal(&rt->rt6i_gateway, addr))
  2698. break;
  2699. }
  2700. if (rt)
  2701. ip6_hold_safe(NULL, &rt, false);
  2702. rcu_read_unlock();
  2703. return rt;
  2704. }
  2705. struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
  2706. struct net_device *dev,
  2707. unsigned int pref)
  2708. {
  2709. struct fib6_config cfg = {
  2710. .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
  2711. .fc_metric = IP6_RT_PRIO_USER,
  2712. .fc_ifindex = dev->ifindex,
  2713. .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
  2714. RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
  2715. .fc_protocol = RTPROT_RA,
  2716. .fc_nlinfo.portid = 0,
  2717. .fc_nlinfo.nlh = NULL,
  2718. .fc_nlinfo.nl_net = dev_net(dev),
  2719. };
  2720. cfg.fc_gateway = *gwaddr;
  2721. if (!ip6_route_add(&cfg, NULL)) {
  2722. struct fib6_table *table;
  2723. table = fib6_get_table(dev_net(dev), cfg.fc_table);
  2724. if (table)
  2725. table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
  2726. }
  2727. return rt6_get_dflt_router(gwaddr, dev);
  2728. }
  2729. static void __rt6_purge_dflt_routers(struct fib6_table *table)
  2730. {
  2731. struct rt6_info *rt;
  2732. restart:
  2733. rcu_read_lock();
  2734. for_each_fib6_node_rt_rcu(&table->tb6_root) {
  2735. if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
  2736. (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) {
  2737. if (dst_hold_safe(&rt->dst)) {
  2738. rcu_read_unlock();
  2739. ip6_del_rt(rt);
  2740. } else {
  2741. rcu_read_unlock();
  2742. }
  2743. goto restart;
  2744. }
  2745. }
  2746. rcu_read_unlock();
  2747. table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
  2748. }
  2749. void rt6_purge_dflt_routers(struct net *net)
  2750. {
  2751. struct fib6_table *table;
  2752. struct hlist_head *head;
  2753. unsigned int h;
  2754. rcu_read_lock();
  2755. for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
  2756. head = &net->ipv6.fib_table_hash[h];
  2757. hlist_for_each_entry_rcu(table, head, tb6_hlist) {
  2758. if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
  2759. __rt6_purge_dflt_routers(table);
  2760. }
  2761. }
  2762. rcu_read_unlock();
  2763. }
  2764. static void rtmsg_to_fib6_config(struct net *net,
  2765. struct in6_rtmsg *rtmsg,
  2766. struct fib6_config *cfg)
  2767. {
  2768. memset(cfg, 0, sizeof(*cfg));
  2769. cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
  2770. : RT6_TABLE_MAIN;
  2771. cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
  2772. cfg->fc_metric = rtmsg->rtmsg_metric;
  2773. cfg->fc_expires = rtmsg->rtmsg_info;
  2774. cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
  2775. cfg->fc_src_len = rtmsg->rtmsg_src_len;
  2776. cfg->fc_flags = rtmsg->rtmsg_flags;
  2777. cfg->fc_nlinfo.nl_net = net;
  2778. cfg->fc_dst = rtmsg->rtmsg_dst;
  2779. cfg->fc_src = rtmsg->rtmsg_src;
  2780. cfg->fc_gateway = rtmsg->rtmsg_gateway;
  2781. }
  2782. int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
  2783. {
  2784. struct fib6_config cfg;
  2785. struct in6_rtmsg rtmsg;
  2786. int err;
  2787. switch (cmd) {
  2788. case SIOCADDRT: /* Add a route */
  2789. case SIOCDELRT: /* Delete a route */
  2790. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  2791. return -EPERM;
  2792. err = copy_from_user(&rtmsg, arg,
  2793. sizeof(struct in6_rtmsg));
  2794. if (err)
  2795. return -EFAULT;
  2796. rtmsg_to_fib6_config(net, &rtmsg, &cfg);
  2797. rtnl_lock();
  2798. switch (cmd) {
  2799. case SIOCADDRT:
  2800. err = ip6_route_add(&cfg, NULL);
  2801. break;
  2802. case SIOCDELRT:
  2803. err = ip6_route_del(&cfg, NULL);
  2804. break;
  2805. default:
  2806. err = -EINVAL;
  2807. }
  2808. rtnl_unlock();
  2809. return err;
  2810. }
  2811. return -EINVAL;
  2812. }
  2813. /*
  2814. * Drop the packet on the floor
  2815. */
  2816. static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
  2817. {
  2818. int type;
  2819. struct dst_entry *dst = skb_dst(skb);
  2820. switch (ipstats_mib_noroutes) {
  2821. case IPSTATS_MIB_INNOROUTES:
  2822. type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
  2823. if (type == IPV6_ADDR_ANY) {
  2824. IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
  2825. IPSTATS_MIB_INADDRERRORS);
  2826. break;
  2827. }
  2828. /* FALLTHROUGH */
  2829. case IPSTATS_MIB_OUTNOROUTES:
  2830. IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
  2831. ipstats_mib_noroutes);
  2832. break;
  2833. }
  2834. icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
  2835. kfree_skb(skb);
  2836. return 0;
  2837. }
  2838. static int ip6_pkt_discard(struct sk_buff *skb)
  2839. {
  2840. return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
  2841. }
  2842. static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
  2843. {
  2844. skb->dev = skb_dst(skb)->dev;
  2845. return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
  2846. }
  2847. static int ip6_pkt_prohibit(struct sk_buff *skb)
  2848. {
  2849. return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
  2850. }
  2851. static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
  2852. {
  2853. skb->dev = skb_dst(skb)->dev;
  2854. return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
  2855. }
  2856. /*
  2857. * Allocate a dst for local (unicast / anycast) address.
  2858. */
  2859. struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
  2860. const struct in6_addr *addr,
  2861. bool anycast)
  2862. {
  2863. u32 tb_id;
  2864. struct net *net = dev_net(idev->dev);
  2865. struct net_device *dev = idev->dev;
  2866. struct rt6_info *rt;
  2867. rt = ip6_dst_alloc(net, dev, DST_NOCOUNT);
  2868. if (!rt)
  2869. return ERR_PTR(-ENOMEM);
  2870. in6_dev_hold(idev);
  2871. rt->dst.flags |= DST_HOST;
  2872. rt->dst.input = ip6_input;
  2873. rt->dst.output = ip6_output;
  2874. rt->rt6i_idev = idev;
  2875. rt->rt6i_protocol = RTPROT_KERNEL;
  2876. rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
  2877. if (anycast)
  2878. rt->rt6i_flags |= RTF_ANYCAST;
  2879. else
  2880. rt->rt6i_flags |= RTF_LOCAL;
  2881. rt->rt6i_gateway = *addr;
  2882. rt->rt6i_dst.addr = *addr;
  2883. rt->rt6i_dst.plen = 128;
  2884. tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL;
  2885. rt->rt6i_table = fib6_get_table(net, tb_id);
  2886. return rt;
  2887. }
  2888. /* remove deleted ip from prefsrc entries */
  2889. struct arg_dev_net_ip {
  2890. struct net_device *dev;
  2891. struct net *net;
  2892. struct in6_addr *addr;
  2893. };
  2894. static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
  2895. {
  2896. struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
  2897. struct net *net = ((struct arg_dev_net_ip *)arg)->net;
  2898. struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
  2899. if (((void *)rt->dst.dev == dev || !dev) &&
  2900. rt != net->ipv6.ip6_null_entry &&
  2901. ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
  2902. spin_lock_bh(&rt6_exception_lock);
  2903. /* remove prefsrc entry */
  2904. rt->rt6i_prefsrc.plen = 0;
  2905. /* need to update cache as well */
  2906. rt6_exceptions_remove_prefsrc(rt);
  2907. spin_unlock_bh(&rt6_exception_lock);
  2908. }
  2909. return 0;
  2910. }
  2911. void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
  2912. {
  2913. struct net *net = dev_net(ifp->idev->dev);
  2914. struct arg_dev_net_ip adni = {
  2915. .dev = ifp->idev->dev,
  2916. .net = net,
  2917. .addr = &ifp->addr,
  2918. };
  2919. fib6_clean_all(net, fib6_remove_prefsrc, &adni);
  2920. }
  2921. #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY)
  2922. /* Remove routers and update dst entries when gateway turn into host. */
  2923. static int fib6_clean_tohost(struct rt6_info *rt, void *arg)
  2924. {
  2925. struct in6_addr *gateway = (struct in6_addr *)arg;
  2926. if (((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
  2927. ipv6_addr_equal(gateway, &rt->rt6i_gateway)) {
  2928. return -1;
  2929. }
  2930. /* Further clean up cached routes in exception table.
  2931. * This is needed because cached route may have a different
  2932. * gateway than its 'parent' in the case of an ip redirect.
  2933. */
  2934. rt6_exceptions_clean_tohost(rt, gateway);
  2935. return 0;
  2936. }
  2937. void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
  2938. {
  2939. fib6_clean_all(net, fib6_clean_tohost, gateway);
  2940. }
  2941. struct arg_dev_net {
  2942. struct net_device *dev;
  2943. struct net *net;
  2944. };
  2945. /* called with write lock held for table with rt */
  2946. static int fib6_ifdown(struct rt6_info *rt, void *arg)
  2947. {
  2948. const struct arg_dev_net *adn = arg;
  2949. const struct net_device *dev = adn->dev;
  2950. if ((rt->dst.dev == dev || !dev) &&
  2951. rt != adn->net->ipv6.ip6_null_entry &&
  2952. (rt->rt6i_nsiblings == 0 ||
  2953. (dev && netdev_unregistering(dev)) ||
  2954. !rt->rt6i_idev->cnf.ignore_routes_with_linkdown))
  2955. return -1;
  2956. return 0;
  2957. }
  2958. void rt6_ifdown(struct net *net, struct net_device *dev)
  2959. {
  2960. struct arg_dev_net adn = {
  2961. .dev = dev,
  2962. .net = net,
  2963. };
  2964. fib6_clean_all(net, fib6_ifdown, &adn);
  2965. if (dev)
  2966. rt6_uncached_list_flush_dev(net, dev);
  2967. }
  2968. struct rt6_mtu_change_arg {
  2969. struct net_device *dev;
  2970. unsigned int mtu;
  2971. };
  2972. static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
  2973. {
  2974. struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
  2975. struct inet6_dev *idev;
  2976. /* In IPv6 pmtu discovery is not optional,
  2977. so that RTAX_MTU lock cannot disable it.
  2978. We still use this lock to block changes
  2979. caused by addrconf/ndisc.
  2980. */
  2981. idev = __in6_dev_get(arg->dev);
  2982. if (!idev)
  2983. return 0;
  2984. /* For administrative MTU increase, there is no way to discover
  2985. IPv6 PMTU increase, so PMTU increase should be updated here.
  2986. Since RFC 1981 doesn't include administrative MTU increase
  2987. update PMTU increase is a MUST. (i.e. jumbo frame)
  2988. */
  2989. /*
  2990. If new MTU is less than route PMTU, this new MTU will be the
  2991. lowest MTU in the path, update the route PMTU to reflect PMTU
  2992. decreases; if new MTU is greater than route PMTU, and the
  2993. old MTU is the lowest MTU in the path, update the route PMTU
  2994. to reflect the increase. In this case if the other nodes' MTU
  2995. also have the lowest MTU, TOO BIG MESSAGE will be lead to
  2996. PMTU discovery.
  2997. */
  2998. if (rt->dst.dev == arg->dev &&
  2999. dst_metric_raw(&rt->dst, RTAX_MTU) &&
  3000. !dst_metric_locked(&rt->dst, RTAX_MTU)) {
  3001. spin_lock_bh(&rt6_exception_lock);
  3002. if (dst_mtu(&rt->dst) >= arg->mtu ||
  3003. (dst_mtu(&rt->dst) < arg->mtu &&
  3004. dst_mtu(&rt->dst) == idev->cnf.mtu6)) {
  3005. dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
  3006. }
  3007. rt6_exceptions_update_pmtu(rt, arg->mtu);
  3008. spin_unlock_bh(&rt6_exception_lock);
  3009. }
  3010. return 0;
  3011. }
  3012. void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
  3013. {
  3014. struct rt6_mtu_change_arg arg = {
  3015. .dev = dev,
  3016. .mtu = mtu,
  3017. };
  3018. fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
  3019. }
  3020. static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
  3021. [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
  3022. [RTA_OIF] = { .type = NLA_U32 },
  3023. [RTA_IIF] = { .type = NLA_U32 },
  3024. [RTA_PRIORITY] = { .type = NLA_U32 },
  3025. [RTA_METRICS] = { .type = NLA_NESTED },
  3026. [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
  3027. [RTA_PREF] = { .type = NLA_U8 },
  3028. [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
  3029. [RTA_ENCAP] = { .type = NLA_NESTED },
  3030. [RTA_EXPIRES] = { .type = NLA_U32 },
  3031. [RTA_UID] = { .type = NLA_U32 },
  3032. [RTA_MARK] = { .type = NLA_U32 },
  3033. };
  3034. static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
  3035. struct fib6_config *cfg,
  3036. struct netlink_ext_ack *extack)
  3037. {
  3038. struct rtmsg *rtm;
  3039. struct nlattr *tb[RTA_MAX+1];
  3040. unsigned int pref;
  3041. int err;
  3042. err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
  3043. NULL);
  3044. if (err < 0)
  3045. goto errout;
  3046. err = -EINVAL;
  3047. rtm = nlmsg_data(nlh);
  3048. memset(cfg, 0, sizeof(*cfg));
  3049. cfg->fc_table = rtm->rtm_table;
  3050. cfg->fc_dst_len = rtm->rtm_dst_len;
  3051. cfg->fc_src_len = rtm->rtm_src_len;
  3052. cfg->fc_flags = RTF_UP;
  3053. cfg->fc_protocol = rtm->rtm_protocol;
  3054. cfg->fc_type = rtm->rtm_type;
  3055. if (rtm->rtm_type == RTN_UNREACHABLE ||
  3056. rtm->rtm_type == RTN_BLACKHOLE ||
  3057. rtm->rtm_type == RTN_PROHIBIT ||
  3058. rtm->rtm_type == RTN_THROW)
  3059. cfg->fc_flags |= RTF_REJECT;
  3060. if (rtm->rtm_type == RTN_LOCAL)
  3061. cfg->fc_flags |= RTF_LOCAL;
  3062. if (rtm->rtm_flags & RTM_F_CLONED)
  3063. cfg->fc_flags |= RTF_CACHE;
  3064. cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
  3065. cfg->fc_nlinfo.nlh = nlh;
  3066. cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
  3067. if (tb[RTA_GATEWAY]) {
  3068. cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
  3069. cfg->fc_flags |= RTF_GATEWAY;
  3070. }
  3071. if (tb[RTA_DST]) {
  3072. int plen = (rtm->rtm_dst_len + 7) >> 3;
  3073. if (nla_len(tb[RTA_DST]) < plen)
  3074. goto errout;
  3075. nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
  3076. }
  3077. if (tb[RTA_SRC]) {
  3078. int plen = (rtm->rtm_src_len + 7) >> 3;
  3079. if (nla_len(tb[RTA_SRC]) < plen)
  3080. goto errout;
  3081. nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
  3082. }
  3083. if (tb[RTA_PREFSRC])
  3084. cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
  3085. if (tb[RTA_OIF])
  3086. cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
  3087. if (tb[RTA_PRIORITY])
  3088. cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
  3089. if (tb[RTA_METRICS]) {
  3090. cfg->fc_mx = nla_data(tb[RTA_METRICS]);
  3091. cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
  3092. }
  3093. if (tb[RTA_TABLE])
  3094. cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
  3095. if (tb[RTA_MULTIPATH]) {
  3096. cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
  3097. cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
  3098. err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
  3099. cfg->fc_mp_len, extack);
  3100. if (err < 0)
  3101. goto errout;
  3102. }
  3103. if (tb[RTA_PREF]) {
  3104. pref = nla_get_u8(tb[RTA_PREF]);
  3105. if (pref != ICMPV6_ROUTER_PREF_LOW &&
  3106. pref != ICMPV6_ROUTER_PREF_HIGH)
  3107. pref = ICMPV6_ROUTER_PREF_MEDIUM;
  3108. cfg->fc_flags |= RTF_PREF(pref);
  3109. }
  3110. if (tb[RTA_ENCAP])
  3111. cfg->fc_encap = tb[RTA_ENCAP];
  3112. if (tb[RTA_ENCAP_TYPE]) {
  3113. cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
  3114. err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
  3115. if (err < 0)
  3116. goto errout;
  3117. }
  3118. if (tb[RTA_EXPIRES]) {
  3119. unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
  3120. if (addrconf_finite_timeout(timeout)) {
  3121. cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
  3122. cfg->fc_flags |= RTF_EXPIRES;
  3123. }
  3124. }
  3125. err = 0;
  3126. errout:
  3127. return err;
  3128. }
  3129. struct rt6_nh {
  3130. struct rt6_info *rt6_info;
  3131. struct fib6_config r_cfg;
  3132. struct mx6_config mxc;
  3133. struct list_head next;
  3134. };
  3135. static void ip6_print_replace_route_err(struct list_head *rt6_nh_list)
  3136. {
  3137. struct rt6_nh *nh;
  3138. list_for_each_entry(nh, rt6_nh_list, next) {
  3139. pr_warn("IPV6: multipath route replace failed (check consistency of installed routes): %pI6c nexthop %pI6c ifi %d\n",
  3140. &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway,
  3141. nh->r_cfg.fc_ifindex);
  3142. }
  3143. }
  3144. static int ip6_route_info_append(struct list_head *rt6_nh_list,
  3145. struct rt6_info *rt, struct fib6_config *r_cfg)
  3146. {
  3147. struct rt6_nh *nh;
  3148. int err = -EEXIST;
  3149. list_for_each_entry(nh, rt6_nh_list, next) {
  3150. /* check if rt6_info already exists */
  3151. if (rt6_duplicate_nexthop(nh->rt6_info, rt))
  3152. return err;
  3153. }
  3154. nh = kzalloc(sizeof(*nh), GFP_KERNEL);
  3155. if (!nh)
  3156. return -ENOMEM;
  3157. nh->rt6_info = rt;
  3158. err = ip6_convert_metrics(&nh->mxc, r_cfg);
  3159. if (err) {
  3160. kfree(nh);
  3161. return err;
  3162. }
  3163. memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
  3164. list_add_tail(&nh->next, rt6_nh_list);
  3165. return 0;
  3166. }
  3167. static void ip6_route_mpath_notify(struct rt6_info *rt,
  3168. struct rt6_info *rt_last,
  3169. struct nl_info *info,
  3170. __u16 nlflags)
  3171. {
  3172. /* if this is an APPEND route, then rt points to the first route
  3173. * inserted and rt_last points to last route inserted. Userspace
  3174. * wants a consistent dump of the route which starts at the first
  3175. * nexthop. Since sibling routes are always added at the end of
  3176. * the list, find the first sibling of the last route appended
  3177. */
  3178. if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->rt6i_nsiblings) {
  3179. rt = list_first_entry(&rt_last->rt6i_siblings,
  3180. struct rt6_info,
  3181. rt6i_siblings);
  3182. }
  3183. if (rt)
  3184. inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
  3185. }
  3186. static int ip6_route_multipath_add(struct fib6_config *cfg,
  3187. struct netlink_ext_ack *extack)
  3188. {
  3189. struct rt6_info *rt_notif = NULL, *rt_last = NULL;
  3190. struct nl_info *info = &cfg->fc_nlinfo;
  3191. struct fib6_config r_cfg;
  3192. struct rtnexthop *rtnh;
  3193. struct rt6_info *rt;
  3194. struct rt6_nh *err_nh;
  3195. struct rt6_nh *nh, *nh_safe;
  3196. __u16 nlflags;
  3197. int remaining;
  3198. int attrlen;
  3199. int err = 1;
  3200. int nhn = 0;
  3201. int replace = (cfg->fc_nlinfo.nlh &&
  3202. (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
  3203. LIST_HEAD(rt6_nh_list);
  3204. nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
  3205. if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
  3206. nlflags |= NLM_F_APPEND;
  3207. remaining = cfg->fc_mp_len;
  3208. rtnh = (struct rtnexthop *)cfg->fc_mp;
  3209. /* Parse a Multipath Entry and build a list (rt6_nh_list) of
  3210. * rt6_info structs per nexthop
  3211. */
  3212. while (rtnh_ok(rtnh, remaining)) {
  3213. memcpy(&r_cfg, cfg, sizeof(*cfg));
  3214. if (rtnh->rtnh_ifindex)
  3215. r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
  3216. attrlen = rtnh_attrlen(rtnh);
  3217. if (attrlen > 0) {
  3218. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  3219. nla = nla_find(attrs, attrlen, RTA_GATEWAY);
  3220. if (nla) {
  3221. r_cfg.fc_gateway = nla_get_in6_addr(nla);
  3222. r_cfg.fc_flags |= RTF_GATEWAY;
  3223. }
  3224. r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
  3225. nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
  3226. if (nla)
  3227. r_cfg.fc_encap_type = nla_get_u16(nla);
  3228. }
  3229. rt = ip6_route_info_create(&r_cfg, extack);
  3230. if (IS_ERR(rt)) {
  3231. err = PTR_ERR(rt);
  3232. rt = NULL;
  3233. goto cleanup;
  3234. }
  3235. err = ip6_route_info_append(&rt6_nh_list, rt, &r_cfg);
  3236. if (err) {
  3237. dst_release_immediate(&rt->dst);
  3238. goto cleanup;
  3239. }
  3240. rtnh = rtnh_next(rtnh, &remaining);
  3241. }
  3242. /* for add and replace send one notification with all nexthops.
  3243. * Skip the notification in fib6_add_rt2node and send one with
  3244. * the full route when done
  3245. */
  3246. info->skip_notify = 1;
  3247. err_nh = NULL;
  3248. list_for_each_entry(nh, &rt6_nh_list, next) {
  3249. rt_last = nh->rt6_info;
  3250. err = __ip6_ins_rt(nh->rt6_info, info, &nh->mxc, extack);
  3251. /* save reference to first route for notification */
  3252. if (!rt_notif && !err)
  3253. rt_notif = nh->rt6_info;
  3254. /* nh->rt6_info is used or freed at this point, reset to NULL*/
  3255. nh->rt6_info = NULL;
  3256. if (err) {
  3257. if (replace && nhn)
  3258. ip6_print_replace_route_err(&rt6_nh_list);
  3259. err_nh = nh;
  3260. goto add_errout;
  3261. }
  3262. /* Because each route is added like a single route we remove
  3263. * these flags after the first nexthop: if there is a collision,
  3264. * we have already failed to add the first nexthop:
  3265. * fib6_add_rt2node() has rejected it; when replacing, old
  3266. * nexthops have been replaced by first new, the rest should
  3267. * be added to it.
  3268. */
  3269. cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
  3270. NLM_F_REPLACE);
  3271. nhn++;
  3272. }
  3273. /* success ... tell user about new route */
  3274. ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
  3275. goto cleanup;
  3276. add_errout:
  3277. /* send notification for routes that were added so that
  3278. * the delete notifications sent by ip6_route_del are
  3279. * coherent
  3280. */
  3281. if (rt_notif)
  3282. ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
  3283. /* Delete routes that were already added */
  3284. list_for_each_entry(nh, &rt6_nh_list, next) {
  3285. if (err_nh == nh)
  3286. break;
  3287. ip6_route_del(&nh->r_cfg, extack);
  3288. }
  3289. cleanup:
  3290. list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
  3291. if (nh->rt6_info)
  3292. dst_release_immediate(&nh->rt6_info->dst);
  3293. kfree(nh->mxc.mx);
  3294. list_del(&nh->next);
  3295. kfree(nh);
  3296. }
  3297. return err;
  3298. }
  3299. static int ip6_route_multipath_del(struct fib6_config *cfg,
  3300. struct netlink_ext_ack *extack)
  3301. {
  3302. struct fib6_config r_cfg;
  3303. struct rtnexthop *rtnh;
  3304. int remaining;
  3305. int attrlen;
  3306. int err = 1, last_err = 0;
  3307. remaining = cfg->fc_mp_len;
  3308. rtnh = (struct rtnexthop *)cfg->fc_mp;
  3309. /* Parse a Multipath Entry */
  3310. while (rtnh_ok(rtnh, remaining)) {
  3311. memcpy(&r_cfg, cfg, sizeof(*cfg));
  3312. if (rtnh->rtnh_ifindex)
  3313. r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
  3314. attrlen = rtnh_attrlen(rtnh);
  3315. if (attrlen > 0) {
  3316. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  3317. nla = nla_find(attrs, attrlen, RTA_GATEWAY);
  3318. if (nla) {
  3319. nla_memcpy(&r_cfg.fc_gateway, nla, 16);
  3320. r_cfg.fc_flags |= RTF_GATEWAY;
  3321. }
  3322. }
  3323. err = ip6_route_del(&r_cfg, extack);
  3324. if (err)
  3325. last_err = err;
  3326. rtnh = rtnh_next(rtnh, &remaining);
  3327. }
  3328. return last_err;
  3329. }
  3330. static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
  3331. struct netlink_ext_ack *extack)
  3332. {
  3333. struct fib6_config cfg;
  3334. int err;
  3335. err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
  3336. if (err < 0)
  3337. return err;
  3338. if (cfg.fc_mp)
  3339. return ip6_route_multipath_del(&cfg, extack);
  3340. else {
  3341. cfg.fc_delete_all_nh = 1;
  3342. return ip6_route_del(&cfg, extack);
  3343. }
  3344. }
  3345. static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
  3346. struct netlink_ext_ack *extack)
  3347. {
  3348. struct fib6_config cfg;
  3349. int err;
  3350. err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
  3351. if (err < 0)
  3352. return err;
  3353. if (cfg.fc_mp)
  3354. return ip6_route_multipath_add(&cfg, extack);
  3355. else
  3356. return ip6_route_add(&cfg, extack);
  3357. }
  3358. static size_t rt6_nlmsg_size(struct rt6_info *rt)
  3359. {
  3360. int nexthop_len = 0;
  3361. if (rt->rt6i_nsiblings) {
  3362. nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */
  3363. + NLA_ALIGN(sizeof(struct rtnexthop))
  3364. + nla_total_size(16) /* RTA_GATEWAY */
  3365. + lwtunnel_get_encap_size(rt->dst.lwtstate);
  3366. nexthop_len *= rt->rt6i_nsiblings;
  3367. }
  3368. return NLMSG_ALIGN(sizeof(struct rtmsg))
  3369. + nla_total_size(16) /* RTA_SRC */
  3370. + nla_total_size(16) /* RTA_DST */
  3371. + nla_total_size(16) /* RTA_GATEWAY */
  3372. + nla_total_size(16) /* RTA_PREFSRC */
  3373. + nla_total_size(4) /* RTA_TABLE */
  3374. + nla_total_size(4) /* RTA_IIF */
  3375. + nla_total_size(4) /* RTA_OIF */
  3376. + nla_total_size(4) /* RTA_PRIORITY */
  3377. + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
  3378. + nla_total_size(sizeof(struct rta_cacheinfo))
  3379. + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
  3380. + nla_total_size(1) /* RTA_PREF */
  3381. + lwtunnel_get_encap_size(rt->dst.lwtstate)
  3382. + nexthop_len;
  3383. }
  3384. static int rt6_nexthop_info(struct sk_buff *skb, struct rt6_info *rt,
  3385. unsigned int *flags, bool skip_oif)
  3386. {
  3387. if (!netif_running(rt->dst.dev) || !netif_carrier_ok(rt->dst.dev)) {
  3388. *flags |= RTNH_F_LINKDOWN;
  3389. if (rt->rt6i_idev->cnf.ignore_routes_with_linkdown)
  3390. *flags |= RTNH_F_DEAD;
  3391. }
  3392. if (rt->rt6i_flags & RTF_GATEWAY) {
  3393. if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->rt6i_gateway) < 0)
  3394. goto nla_put_failure;
  3395. }
  3396. if (rt->rt6i_nh_flags & RTNH_F_OFFLOAD)
  3397. *flags |= RTNH_F_OFFLOAD;
  3398. /* not needed for multipath encoding b/c it has a rtnexthop struct */
  3399. if (!skip_oif && rt->dst.dev &&
  3400. nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
  3401. goto nla_put_failure;
  3402. if (rt->dst.lwtstate &&
  3403. lwtunnel_fill_encap(skb, rt->dst.lwtstate) < 0)
  3404. goto nla_put_failure;
  3405. return 0;
  3406. nla_put_failure:
  3407. return -EMSGSIZE;
  3408. }
  3409. /* add multipath next hop */
  3410. static int rt6_add_nexthop(struct sk_buff *skb, struct rt6_info *rt)
  3411. {
  3412. struct rtnexthop *rtnh;
  3413. unsigned int flags = 0;
  3414. rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
  3415. if (!rtnh)
  3416. goto nla_put_failure;
  3417. rtnh->rtnh_hops = 0;
  3418. rtnh->rtnh_ifindex = rt->dst.dev ? rt->dst.dev->ifindex : 0;
  3419. if (rt6_nexthop_info(skb, rt, &flags, true) < 0)
  3420. goto nla_put_failure;
  3421. rtnh->rtnh_flags = flags;
  3422. /* length of rtnetlink header + attributes */
  3423. rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
  3424. return 0;
  3425. nla_put_failure:
  3426. return -EMSGSIZE;
  3427. }
  3428. static int rt6_fill_node(struct net *net,
  3429. struct sk_buff *skb, struct rt6_info *rt,
  3430. struct in6_addr *dst, struct in6_addr *src,
  3431. int iif, int type, u32 portid, u32 seq,
  3432. unsigned int flags)
  3433. {
  3434. u32 metrics[RTAX_MAX];
  3435. struct rtmsg *rtm;
  3436. struct nlmsghdr *nlh;
  3437. long expires;
  3438. u32 table;
  3439. nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
  3440. if (!nlh)
  3441. return -EMSGSIZE;
  3442. rtm = nlmsg_data(nlh);
  3443. rtm->rtm_family = AF_INET6;
  3444. rtm->rtm_dst_len = rt->rt6i_dst.plen;
  3445. rtm->rtm_src_len = rt->rt6i_src.plen;
  3446. rtm->rtm_tos = 0;
  3447. if (rt->rt6i_table)
  3448. table = rt->rt6i_table->tb6_id;
  3449. else
  3450. table = RT6_TABLE_UNSPEC;
  3451. rtm->rtm_table = table;
  3452. if (nla_put_u32(skb, RTA_TABLE, table))
  3453. goto nla_put_failure;
  3454. if (rt->rt6i_flags & RTF_REJECT) {
  3455. switch (rt->dst.error) {
  3456. case -EINVAL:
  3457. rtm->rtm_type = RTN_BLACKHOLE;
  3458. break;
  3459. case -EACCES:
  3460. rtm->rtm_type = RTN_PROHIBIT;
  3461. break;
  3462. case -EAGAIN:
  3463. rtm->rtm_type = RTN_THROW;
  3464. break;
  3465. default:
  3466. rtm->rtm_type = RTN_UNREACHABLE;
  3467. break;
  3468. }
  3469. }
  3470. else if (rt->rt6i_flags & RTF_LOCAL)
  3471. rtm->rtm_type = RTN_LOCAL;
  3472. else if (rt->rt6i_flags & RTF_ANYCAST)
  3473. rtm->rtm_type = RTN_ANYCAST;
  3474. else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
  3475. rtm->rtm_type = RTN_LOCAL;
  3476. else
  3477. rtm->rtm_type = RTN_UNICAST;
  3478. rtm->rtm_flags = 0;
  3479. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  3480. rtm->rtm_protocol = rt->rt6i_protocol;
  3481. if (rt->rt6i_flags & RTF_CACHE)
  3482. rtm->rtm_flags |= RTM_F_CLONED;
  3483. if (dst) {
  3484. if (nla_put_in6_addr(skb, RTA_DST, dst))
  3485. goto nla_put_failure;
  3486. rtm->rtm_dst_len = 128;
  3487. } else if (rtm->rtm_dst_len)
  3488. if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr))
  3489. goto nla_put_failure;
  3490. #ifdef CONFIG_IPV6_SUBTREES
  3491. if (src) {
  3492. if (nla_put_in6_addr(skb, RTA_SRC, src))
  3493. goto nla_put_failure;
  3494. rtm->rtm_src_len = 128;
  3495. } else if (rtm->rtm_src_len &&
  3496. nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr))
  3497. goto nla_put_failure;
  3498. #endif
  3499. if (iif) {
  3500. #ifdef CONFIG_IPV6_MROUTE
  3501. if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
  3502. int err = ip6mr_get_route(net, skb, rtm, portid);
  3503. if (err == 0)
  3504. return 0;
  3505. if (err < 0)
  3506. goto nla_put_failure;
  3507. } else
  3508. #endif
  3509. if (nla_put_u32(skb, RTA_IIF, iif))
  3510. goto nla_put_failure;
  3511. } else if (dst) {
  3512. struct in6_addr saddr_buf;
  3513. if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
  3514. nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
  3515. goto nla_put_failure;
  3516. }
  3517. if (rt->rt6i_prefsrc.plen) {
  3518. struct in6_addr saddr_buf;
  3519. saddr_buf = rt->rt6i_prefsrc.addr;
  3520. if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
  3521. goto nla_put_failure;
  3522. }
  3523. memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics));
  3524. if (rt->rt6i_pmtu)
  3525. metrics[RTAX_MTU - 1] = rt->rt6i_pmtu;
  3526. if (rtnetlink_put_metrics(skb, metrics) < 0)
  3527. goto nla_put_failure;
  3528. if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
  3529. goto nla_put_failure;
  3530. /* For multipath routes, walk the siblings list and add
  3531. * each as a nexthop within RTA_MULTIPATH.
  3532. */
  3533. if (rt->rt6i_nsiblings) {
  3534. struct rt6_info *sibling, *next_sibling;
  3535. struct nlattr *mp;
  3536. mp = nla_nest_start(skb, RTA_MULTIPATH);
  3537. if (!mp)
  3538. goto nla_put_failure;
  3539. if (rt6_add_nexthop(skb, rt) < 0)
  3540. goto nla_put_failure;
  3541. list_for_each_entry_safe(sibling, next_sibling,
  3542. &rt->rt6i_siblings, rt6i_siblings) {
  3543. if (rt6_add_nexthop(skb, sibling) < 0)
  3544. goto nla_put_failure;
  3545. }
  3546. nla_nest_end(skb, mp);
  3547. } else {
  3548. if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0)
  3549. goto nla_put_failure;
  3550. }
  3551. expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0;
  3552. if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
  3553. goto nla_put_failure;
  3554. if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags)))
  3555. goto nla_put_failure;
  3556. nlmsg_end(skb, nlh);
  3557. return 0;
  3558. nla_put_failure:
  3559. nlmsg_cancel(skb, nlh);
  3560. return -EMSGSIZE;
  3561. }
  3562. int rt6_dump_route(struct rt6_info *rt, void *p_arg)
  3563. {
  3564. struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
  3565. struct net *net = arg->net;
  3566. if (rt == net->ipv6.ip6_null_entry)
  3567. return 0;
  3568. if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
  3569. struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
  3570. /* user wants prefix routes only */
  3571. if (rtm->rtm_flags & RTM_F_PREFIX &&
  3572. !(rt->rt6i_flags & RTF_PREFIX_RT)) {
  3573. /* success since this is not a prefix route */
  3574. return 1;
  3575. }
  3576. }
  3577. return rt6_fill_node(net,
  3578. arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
  3579. NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq,
  3580. NLM_F_MULTI);
  3581. }
  3582. static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
  3583. struct netlink_ext_ack *extack)
  3584. {
  3585. struct net *net = sock_net(in_skb->sk);
  3586. struct nlattr *tb[RTA_MAX+1];
  3587. int err, iif = 0, oif = 0;
  3588. struct dst_entry *dst;
  3589. struct rt6_info *rt;
  3590. struct sk_buff *skb;
  3591. struct rtmsg *rtm;
  3592. struct flowi6 fl6;
  3593. bool fibmatch;
  3594. err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
  3595. extack);
  3596. if (err < 0)
  3597. goto errout;
  3598. err = -EINVAL;
  3599. memset(&fl6, 0, sizeof(fl6));
  3600. rtm = nlmsg_data(nlh);
  3601. fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
  3602. fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
  3603. if (tb[RTA_SRC]) {
  3604. if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
  3605. goto errout;
  3606. fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
  3607. }
  3608. if (tb[RTA_DST]) {
  3609. if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
  3610. goto errout;
  3611. fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
  3612. }
  3613. if (tb[RTA_IIF])
  3614. iif = nla_get_u32(tb[RTA_IIF]);
  3615. if (tb[RTA_OIF])
  3616. oif = nla_get_u32(tb[RTA_OIF]);
  3617. if (tb[RTA_MARK])
  3618. fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
  3619. if (tb[RTA_UID])
  3620. fl6.flowi6_uid = make_kuid(current_user_ns(),
  3621. nla_get_u32(tb[RTA_UID]));
  3622. else
  3623. fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
  3624. if (iif) {
  3625. struct net_device *dev;
  3626. int flags = 0;
  3627. rcu_read_lock();
  3628. dev = dev_get_by_index_rcu(net, iif);
  3629. if (!dev) {
  3630. rcu_read_unlock();
  3631. err = -ENODEV;
  3632. goto errout;
  3633. }
  3634. fl6.flowi6_iif = iif;
  3635. if (!ipv6_addr_any(&fl6.saddr))
  3636. flags |= RT6_LOOKUP_F_HAS_SADDR;
  3637. dst = ip6_route_input_lookup(net, dev, &fl6, flags);
  3638. rcu_read_unlock();
  3639. } else {
  3640. fl6.flowi6_oif = oif;
  3641. dst = ip6_route_output(net, NULL, &fl6);
  3642. }
  3643. rt = container_of(dst, struct rt6_info, dst);
  3644. if (rt->dst.error) {
  3645. err = rt->dst.error;
  3646. ip6_rt_put(rt);
  3647. goto errout;
  3648. }
  3649. if (rt == net->ipv6.ip6_null_entry) {
  3650. err = rt->dst.error;
  3651. ip6_rt_put(rt);
  3652. goto errout;
  3653. }
  3654. if (fibmatch && rt->from) {
  3655. struct rt6_info *ort = rt->from;
  3656. dst_hold(&ort->dst);
  3657. ip6_rt_put(rt);
  3658. rt = ort;
  3659. }
  3660. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  3661. if (!skb) {
  3662. ip6_rt_put(rt);
  3663. err = -ENOBUFS;
  3664. goto errout;
  3665. }
  3666. skb_dst_set(skb, &rt->dst);
  3667. if (fibmatch)
  3668. err = rt6_fill_node(net, skb, rt, NULL, NULL, iif,
  3669. RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
  3670. nlh->nlmsg_seq, 0);
  3671. else
  3672. err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
  3673. RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
  3674. nlh->nlmsg_seq, 0);
  3675. if (err < 0) {
  3676. kfree_skb(skb);
  3677. goto errout;
  3678. }
  3679. err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
  3680. errout:
  3681. return err;
  3682. }
  3683. void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info,
  3684. unsigned int nlm_flags)
  3685. {
  3686. struct sk_buff *skb;
  3687. struct net *net = info->nl_net;
  3688. u32 seq;
  3689. int err;
  3690. err = -ENOBUFS;
  3691. seq = info->nlh ? info->nlh->nlmsg_seq : 0;
  3692. skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
  3693. if (!skb)
  3694. goto errout;
  3695. err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
  3696. event, info->portid, seq, nlm_flags);
  3697. if (err < 0) {
  3698. /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
  3699. WARN_ON(err == -EMSGSIZE);
  3700. kfree_skb(skb);
  3701. goto errout;
  3702. }
  3703. rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
  3704. info->nlh, gfp_any());
  3705. return;
  3706. errout:
  3707. if (err < 0)
  3708. rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
  3709. }
  3710. static int ip6_route_dev_notify(struct notifier_block *this,
  3711. unsigned long event, void *ptr)
  3712. {
  3713. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  3714. struct net *net = dev_net(dev);
  3715. if (!(dev->flags & IFF_LOOPBACK))
  3716. return NOTIFY_OK;
  3717. if (event == NETDEV_REGISTER) {
  3718. net->ipv6.ip6_null_entry->dst.dev = dev;
  3719. net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
  3720. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  3721. net->ipv6.ip6_prohibit_entry->dst.dev = dev;
  3722. net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
  3723. net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
  3724. net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
  3725. #endif
  3726. } else if (event == NETDEV_UNREGISTER &&
  3727. dev->reg_state != NETREG_UNREGISTERED) {
  3728. /* NETDEV_UNREGISTER could be fired for multiple times by
  3729. * netdev_wait_allrefs(). Make sure we only call this once.
  3730. */
  3731. in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
  3732. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  3733. in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
  3734. in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
  3735. #endif
  3736. }
  3737. return NOTIFY_OK;
  3738. }
  3739. /*
  3740. * /proc
  3741. */
  3742. #ifdef CONFIG_PROC_FS
  3743. static const struct file_operations ipv6_route_proc_fops = {
  3744. .owner = THIS_MODULE,
  3745. .open = ipv6_route_open,
  3746. .read = seq_read,
  3747. .llseek = seq_lseek,
  3748. .release = seq_release_net,
  3749. };
  3750. static int rt6_stats_seq_show(struct seq_file *seq, void *v)
  3751. {
  3752. struct net *net = (struct net *)seq->private;
  3753. seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
  3754. net->ipv6.rt6_stats->fib_nodes,
  3755. net->ipv6.rt6_stats->fib_route_nodes,
  3756. atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
  3757. net->ipv6.rt6_stats->fib_rt_entries,
  3758. net->ipv6.rt6_stats->fib_rt_cache,
  3759. dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
  3760. net->ipv6.rt6_stats->fib_discarded_routes);
  3761. return 0;
  3762. }
  3763. static int rt6_stats_seq_open(struct inode *inode, struct file *file)
  3764. {
  3765. return single_open_net(inode, file, rt6_stats_seq_show);
  3766. }
  3767. static const struct file_operations rt6_stats_seq_fops = {
  3768. .owner = THIS_MODULE,
  3769. .open = rt6_stats_seq_open,
  3770. .read = seq_read,
  3771. .llseek = seq_lseek,
  3772. .release = single_release_net,
  3773. };
  3774. #endif /* CONFIG_PROC_FS */
  3775. #ifdef CONFIG_SYSCTL
  3776. static
  3777. int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
  3778. void __user *buffer, size_t *lenp, loff_t *ppos)
  3779. {
  3780. struct net *net;
  3781. int delay;
  3782. if (!write)
  3783. return -EINVAL;
  3784. net = (struct net *)ctl->extra1;
  3785. delay = net->ipv6.sysctl.flush_delay;
  3786. proc_dointvec(ctl, write, buffer, lenp, ppos);
  3787. fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
  3788. return 0;
  3789. }
  3790. struct ctl_table ipv6_route_table_template[] = {
  3791. {
  3792. .procname = "flush",
  3793. .data = &init_net.ipv6.sysctl.flush_delay,
  3794. .maxlen = sizeof(int),
  3795. .mode = 0200,
  3796. .proc_handler = ipv6_sysctl_rtcache_flush
  3797. },
  3798. {
  3799. .procname = "gc_thresh",
  3800. .data = &ip6_dst_ops_template.gc_thresh,
  3801. .maxlen = sizeof(int),
  3802. .mode = 0644,
  3803. .proc_handler = proc_dointvec,
  3804. },
  3805. {
  3806. .procname = "max_size",
  3807. .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
  3808. .maxlen = sizeof(int),
  3809. .mode = 0644,
  3810. .proc_handler = proc_dointvec,
  3811. },
  3812. {
  3813. .procname = "gc_min_interval",
  3814. .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
  3815. .maxlen = sizeof(int),
  3816. .mode = 0644,
  3817. .proc_handler = proc_dointvec_jiffies,
  3818. },
  3819. {
  3820. .procname = "gc_timeout",
  3821. .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
  3822. .maxlen = sizeof(int),
  3823. .mode = 0644,
  3824. .proc_handler = proc_dointvec_jiffies,
  3825. },
  3826. {
  3827. .procname = "gc_interval",
  3828. .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
  3829. .maxlen = sizeof(int),
  3830. .mode = 0644,
  3831. .proc_handler = proc_dointvec_jiffies,
  3832. },
  3833. {
  3834. .procname = "gc_elasticity",
  3835. .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
  3836. .maxlen = sizeof(int),
  3837. .mode = 0644,
  3838. .proc_handler = proc_dointvec,
  3839. },
  3840. {
  3841. .procname = "mtu_expires",
  3842. .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
  3843. .maxlen = sizeof(int),
  3844. .mode = 0644,
  3845. .proc_handler = proc_dointvec_jiffies,
  3846. },
  3847. {
  3848. .procname = "min_adv_mss",
  3849. .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
  3850. .maxlen = sizeof(int),
  3851. .mode = 0644,
  3852. .proc_handler = proc_dointvec,
  3853. },
  3854. {
  3855. .procname = "gc_min_interval_ms",
  3856. .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
  3857. .maxlen = sizeof(int),
  3858. .mode = 0644,
  3859. .proc_handler = proc_dointvec_ms_jiffies,
  3860. },
  3861. { }
  3862. };
  3863. struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
  3864. {
  3865. struct ctl_table *table;
  3866. table = kmemdup(ipv6_route_table_template,
  3867. sizeof(ipv6_route_table_template),
  3868. GFP_KERNEL);
  3869. if (table) {
  3870. table[0].data = &net->ipv6.sysctl.flush_delay;
  3871. table[0].extra1 = net;
  3872. table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
  3873. table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
  3874. table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
  3875. table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
  3876. table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
  3877. table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
  3878. table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
  3879. table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
  3880. table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
  3881. /* Don't export sysctls to unprivileged users */
  3882. if (net->user_ns != &init_user_ns)
  3883. table[0].procname = NULL;
  3884. }
  3885. return table;
  3886. }
  3887. #endif
  3888. static int __net_init ip6_route_net_init(struct net *net)
  3889. {
  3890. int ret = -ENOMEM;
  3891. memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
  3892. sizeof(net->ipv6.ip6_dst_ops));
  3893. if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
  3894. goto out_ip6_dst_ops;
  3895. net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
  3896. sizeof(*net->ipv6.ip6_null_entry),
  3897. GFP_KERNEL);
  3898. if (!net->ipv6.ip6_null_entry)
  3899. goto out_ip6_dst_entries;
  3900. net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
  3901. dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
  3902. ip6_template_metrics, true);
  3903. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  3904. net->ipv6.fib6_has_custom_rules = false;
  3905. net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
  3906. sizeof(*net->ipv6.ip6_prohibit_entry),
  3907. GFP_KERNEL);
  3908. if (!net->ipv6.ip6_prohibit_entry)
  3909. goto out_ip6_null_entry;
  3910. net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
  3911. dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
  3912. ip6_template_metrics, true);
  3913. net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
  3914. sizeof(*net->ipv6.ip6_blk_hole_entry),
  3915. GFP_KERNEL);
  3916. if (!net->ipv6.ip6_blk_hole_entry)
  3917. goto out_ip6_prohibit_entry;
  3918. net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
  3919. dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
  3920. ip6_template_metrics, true);
  3921. #endif
  3922. net->ipv6.sysctl.flush_delay = 0;
  3923. net->ipv6.sysctl.ip6_rt_max_size = 4096;
  3924. net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
  3925. net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
  3926. net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
  3927. net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
  3928. net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
  3929. net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
  3930. net->ipv6.ip6_rt_gc_expire = 30*HZ;
  3931. ret = 0;
  3932. out:
  3933. return ret;
  3934. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  3935. out_ip6_prohibit_entry:
  3936. kfree(net->ipv6.ip6_prohibit_entry);
  3937. out_ip6_null_entry:
  3938. kfree(net->ipv6.ip6_null_entry);
  3939. #endif
  3940. out_ip6_dst_entries:
  3941. dst_entries_destroy(&net->ipv6.ip6_dst_ops);
  3942. out_ip6_dst_ops:
  3943. goto out;
  3944. }
  3945. static void __net_exit ip6_route_net_exit(struct net *net)
  3946. {
  3947. kfree(net->ipv6.ip6_null_entry);
  3948. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  3949. kfree(net->ipv6.ip6_prohibit_entry);
  3950. kfree(net->ipv6.ip6_blk_hole_entry);
  3951. #endif
  3952. dst_entries_destroy(&net->ipv6.ip6_dst_ops);
  3953. }
  3954. static int __net_init ip6_route_net_init_late(struct net *net)
  3955. {
  3956. #ifdef CONFIG_PROC_FS
  3957. proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops);
  3958. proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops);
  3959. #endif
  3960. return 0;
  3961. }
  3962. static void __net_exit ip6_route_net_exit_late(struct net *net)
  3963. {
  3964. #ifdef CONFIG_PROC_FS
  3965. remove_proc_entry("ipv6_route", net->proc_net);
  3966. remove_proc_entry("rt6_stats", net->proc_net);
  3967. #endif
  3968. }
  3969. static struct pernet_operations ip6_route_net_ops = {
  3970. .init = ip6_route_net_init,
  3971. .exit = ip6_route_net_exit,
  3972. };
  3973. static int __net_init ipv6_inetpeer_init(struct net *net)
  3974. {
  3975. struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
  3976. if (!bp)
  3977. return -ENOMEM;
  3978. inet_peer_base_init(bp);
  3979. net->ipv6.peers = bp;
  3980. return 0;
  3981. }
  3982. static void __net_exit ipv6_inetpeer_exit(struct net *net)
  3983. {
  3984. struct inet_peer_base *bp = net->ipv6.peers;
  3985. net->ipv6.peers = NULL;
  3986. inetpeer_invalidate_tree(bp);
  3987. kfree(bp);
  3988. }
  3989. static struct pernet_operations ipv6_inetpeer_ops = {
  3990. .init = ipv6_inetpeer_init,
  3991. .exit = ipv6_inetpeer_exit,
  3992. };
  3993. static struct pernet_operations ip6_route_net_late_ops = {
  3994. .init = ip6_route_net_init_late,
  3995. .exit = ip6_route_net_exit_late,
  3996. };
  3997. static struct notifier_block ip6_route_dev_notifier = {
  3998. .notifier_call = ip6_route_dev_notify,
  3999. .priority = ADDRCONF_NOTIFY_PRIORITY - 10,
  4000. };
  4001. void __init ip6_route_init_special_entries(void)
  4002. {
  4003. /* Registering of the loopback is done before this portion of code,
  4004. * the loopback reference in rt6_info will not be taken, do it
  4005. * manually for init_net */
  4006. init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
  4007. init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
  4008. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  4009. init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
  4010. init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
  4011. init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
  4012. init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
  4013. #endif
  4014. }
  4015. int __init ip6_route_init(void)
  4016. {
  4017. int ret;
  4018. int cpu;
  4019. ret = -ENOMEM;
  4020. ip6_dst_ops_template.kmem_cachep =
  4021. kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
  4022. SLAB_HWCACHE_ALIGN, NULL);
  4023. if (!ip6_dst_ops_template.kmem_cachep)
  4024. goto out;
  4025. ret = dst_entries_init(&ip6_dst_blackhole_ops);
  4026. if (ret)
  4027. goto out_kmem_cache;
  4028. ret = register_pernet_subsys(&ipv6_inetpeer_ops);
  4029. if (ret)
  4030. goto out_dst_entries;
  4031. ret = register_pernet_subsys(&ip6_route_net_ops);
  4032. if (ret)
  4033. goto out_register_inetpeer;
  4034. ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
  4035. ret = fib6_init();
  4036. if (ret)
  4037. goto out_register_subsys;
  4038. ret = xfrm6_init();
  4039. if (ret)
  4040. goto out_fib6_init;
  4041. ret = fib6_rules_init();
  4042. if (ret)
  4043. goto xfrm6_init;
  4044. ret = register_pernet_subsys(&ip6_route_net_late_ops);
  4045. if (ret)
  4046. goto fib6_rules_init;
  4047. ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
  4048. inet6_rtm_newroute, NULL, 0);
  4049. if (ret < 0)
  4050. goto out_register_late_subsys;
  4051. ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
  4052. inet6_rtm_delroute, NULL, 0);
  4053. if (ret < 0)
  4054. goto out_register_late_subsys;
  4055. ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
  4056. inet6_rtm_getroute, NULL,
  4057. RTNL_FLAG_DOIT_UNLOCKED);
  4058. if (ret < 0)
  4059. goto out_register_late_subsys;
  4060. ret = register_netdevice_notifier(&ip6_route_dev_notifier);
  4061. if (ret)
  4062. goto out_register_late_subsys;
  4063. for_each_possible_cpu(cpu) {
  4064. struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
  4065. INIT_LIST_HEAD(&ul->head);
  4066. spin_lock_init(&ul->lock);
  4067. }
  4068. out:
  4069. return ret;
  4070. out_register_late_subsys:
  4071. rtnl_unregister_all(PF_INET6);
  4072. unregister_pernet_subsys(&ip6_route_net_late_ops);
  4073. fib6_rules_init:
  4074. fib6_rules_cleanup();
  4075. xfrm6_init:
  4076. xfrm6_fini();
  4077. out_fib6_init:
  4078. fib6_gc_cleanup();
  4079. out_register_subsys:
  4080. unregister_pernet_subsys(&ip6_route_net_ops);
  4081. out_register_inetpeer:
  4082. unregister_pernet_subsys(&ipv6_inetpeer_ops);
  4083. out_dst_entries:
  4084. dst_entries_destroy(&ip6_dst_blackhole_ops);
  4085. out_kmem_cache:
  4086. kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
  4087. goto out;
  4088. }
  4089. void ip6_route_cleanup(void)
  4090. {
  4091. unregister_netdevice_notifier(&ip6_route_dev_notifier);
  4092. unregister_pernet_subsys(&ip6_route_net_late_ops);
  4093. fib6_rules_cleanup();
  4094. xfrm6_fini();
  4095. fib6_gc_cleanup();
  4096. unregister_pernet_subsys(&ipv6_inetpeer_ops);
  4097. unregister_pernet_subsys(&ip6_route_net_ops);
  4098. dst_entries_destroy(&ip6_dst_blackhole_ops);
  4099. kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
  4100. }