addrconf.c 125 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235
  1. /*
  2. * IPv6 Address [auto]configuration
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. */
  14. /*
  15. * Changes:
  16. *
  17. * Janos Farkas : delete timer on ifdown
  18. * <chexum@bankinf.banki.hu>
  19. * Andi Kleen : kill double kfree on module
  20. * unload.
  21. * Maciej W. Rozycki : FDDI support
  22. * sekiya@USAGI : Don't send too many RS
  23. * packets.
  24. * yoshfuji@USAGI : Fixed interval between DAD
  25. * packets.
  26. * YOSHIFUJI Hideaki @USAGI : improved accuracy of
  27. * address validation timer.
  28. * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
  29. * support.
  30. * Yuji SEKIYA @USAGI : Don't assign a same IPv6
  31. * address on a same interface.
  32. * YOSHIFUJI Hideaki @USAGI : ARCnet support
  33. * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
  34. * seq_file.
  35. * YOSHIFUJI Hideaki @USAGI : improved source address
  36. * selection; consider scope,
  37. * status etc.
  38. */
  39. #define pr_fmt(fmt) "IPv6: " fmt
  40. #include <linux/errno.h>
  41. #include <linux/types.h>
  42. #include <linux/kernel.h>
  43. #include <linux/socket.h>
  44. #include <linux/sockios.h>
  45. #include <linux/net.h>
  46. #include <linux/in6.h>
  47. #include <linux/netdevice.h>
  48. #include <linux/if_addr.h>
  49. #include <linux/if_arp.h>
  50. #include <linux/if_arcnet.h>
  51. #include <linux/if_infiniband.h>
  52. #include <linux/route.h>
  53. #include <linux/inetdevice.h>
  54. #include <linux/init.h>
  55. #include <linux/slab.h>
  56. #ifdef CONFIG_SYSCTL
  57. #include <linux/sysctl.h>
  58. #endif
  59. #include <linux/capability.h>
  60. #include <linux/delay.h>
  61. #include <linux/notifier.h>
  62. #include <linux/string.h>
  63. #include <linux/hash.h>
  64. #include <net/net_namespace.h>
  65. #include <net/sock.h>
  66. #include <net/snmp.h>
  67. #include <net/af_ieee802154.h>
  68. #include <net/firewire.h>
  69. #include <net/ipv6.h>
  70. #include <net/protocol.h>
  71. #include <net/ndisc.h>
  72. #include <net/ip6_route.h>
  73. #include <net/addrconf.h>
  74. #include <net/tcp.h>
  75. #include <net/ip.h>
  76. #include <net/netlink.h>
  77. #include <net/pkt_sched.h>
  78. #include <linux/if_tunnel.h>
  79. #include <linux/rtnetlink.h>
  80. #include <linux/netconf.h>
  81. #ifdef CONFIG_IPV6_PRIVACY
  82. #include <linux/random.h>
  83. #endif
  84. #include <linux/uaccess.h>
  85. #include <asm/unaligned.h>
  86. #include <linux/proc_fs.h>
  87. #include <linux/seq_file.h>
  88. #include <linux/export.h>
  89. /* Set to 3 to get tracing... */
  90. #define ACONF_DEBUG 2
  91. #if ACONF_DEBUG >= 3
  92. #define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
  93. #else
  94. #define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0)
  95. #endif
  96. #define INFINITY_LIFE_TIME 0xFFFFFFFF
  97. static inline u32 cstamp_delta(unsigned long cstamp)
  98. {
  99. return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
  100. }
  101. #ifdef CONFIG_SYSCTL
  102. static void addrconf_sysctl_register(struct inet6_dev *idev);
  103. static void addrconf_sysctl_unregister(struct inet6_dev *idev);
  104. #else
  105. static inline void addrconf_sysctl_register(struct inet6_dev *idev)
  106. {
  107. }
  108. static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
  109. {
  110. }
  111. #endif
  112. #ifdef CONFIG_IPV6_PRIVACY
  113. static void __ipv6_regen_rndid(struct inet6_dev *idev);
  114. static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
  115. static void ipv6_regen_rndid(unsigned long data);
  116. #endif
  117. static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
  118. static int ipv6_count_addresses(struct inet6_dev *idev);
  119. /*
  120. * Configured unicast address hash table
  121. */
  122. static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
  123. static DEFINE_SPINLOCK(addrconf_hash_lock);
  124. static void addrconf_verify(unsigned long);
  125. static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
  126. static DEFINE_SPINLOCK(addrconf_verify_lock);
  127. static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
  128. static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
  129. static void addrconf_type_change(struct net_device *dev,
  130. unsigned long event);
  131. static int addrconf_ifdown(struct net_device *dev, int how);
  132. static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
  133. int plen,
  134. const struct net_device *dev,
  135. u32 flags, u32 noflags);
  136. static void addrconf_dad_start(struct inet6_ifaddr *ifp);
  137. static void addrconf_dad_timer(unsigned long data);
  138. static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
  139. static void addrconf_dad_run(struct inet6_dev *idev);
  140. static void addrconf_rs_timer(unsigned long data);
  141. static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
  142. static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
  143. static void inet6_prefix_notify(int event, struct inet6_dev *idev,
  144. struct prefix_info *pinfo);
  145. static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
  146. struct net_device *dev);
  147. static struct ipv6_devconf ipv6_devconf __read_mostly = {
  148. .forwarding = 0,
  149. .hop_limit = IPV6_DEFAULT_HOPLIMIT,
  150. .mtu6 = IPV6_MIN_MTU,
  151. .accept_ra = 1,
  152. .accept_redirects = 1,
  153. .autoconf = 1,
  154. .force_mld_version = 0,
  155. .mldv1_unsolicited_report_interval = 10 * HZ,
  156. .mldv2_unsolicited_report_interval = HZ,
  157. .dad_transmits = 1,
  158. .rtr_solicits = MAX_RTR_SOLICITATIONS,
  159. .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
  160. .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
  161. #ifdef CONFIG_IPV6_PRIVACY
  162. .use_tempaddr = 0,
  163. .temp_valid_lft = TEMP_VALID_LIFETIME,
  164. .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
  165. .regen_max_retry = REGEN_MAX_RETRY,
  166. .max_desync_factor = MAX_DESYNC_FACTOR,
  167. #endif
  168. .max_addresses = IPV6_MAX_ADDRESSES,
  169. .accept_ra_defrtr = 1,
  170. .accept_ra_pinfo = 1,
  171. #ifdef CONFIG_IPV6_ROUTER_PREF
  172. .accept_ra_rtr_pref = 1,
  173. .rtr_probe_interval = 60 * HZ,
  174. #ifdef CONFIG_IPV6_ROUTE_INFO
  175. .accept_ra_rt_info_max_plen = 0,
  176. #endif
  177. #endif
  178. .proxy_ndp = 0,
  179. .accept_source_route = 0, /* we do not accept RH0 by default. */
  180. .disable_ipv6 = 0,
  181. .accept_dad = 1,
  182. .suppress_frag_ndisc = 1,
  183. };
  184. static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
  185. .forwarding = 0,
  186. .hop_limit = IPV6_DEFAULT_HOPLIMIT,
  187. .mtu6 = IPV6_MIN_MTU,
  188. .accept_ra = 1,
  189. .accept_redirects = 1,
  190. .autoconf = 1,
  191. .force_mld_version = 0,
  192. .mldv1_unsolicited_report_interval = 10 * HZ,
  193. .mldv2_unsolicited_report_interval = HZ,
  194. .dad_transmits = 1,
  195. .rtr_solicits = MAX_RTR_SOLICITATIONS,
  196. .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
  197. .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
  198. #ifdef CONFIG_IPV6_PRIVACY
  199. .use_tempaddr = 0,
  200. .temp_valid_lft = TEMP_VALID_LIFETIME,
  201. .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
  202. .regen_max_retry = REGEN_MAX_RETRY,
  203. .max_desync_factor = MAX_DESYNC_FACTOR,
  204. #endif
  205. .max_addresses = IPV6_MAX_ADDRESSES,
  206. .accept_ra_defrtr = 1,
  207. .accept_ra_pinfo = 1,
  208. #ifdef CONFIG_IPV6_ROUTER_PREF
  209. .accept_ra_rtr_pref = 1,
  210. .rtr_probe_interval = 60 * HZ,
  211. #ifdef CONFIG_IPV6_ROUTE_INFO
  212. .accept_ra_rt_info_max_plen = 0,
  213. #endif
  214. #endif
  215. .proxy_ndp = 0,
  216. .accept_source_route = 0, /* we do not accept RH0 by default. */
  217. .disable_ipv6 = 0,
  218. .accept_dad = 1,
  219. .suppress_frag_ndisc = 1,
  220. };
  221. /* Check if a valid qdisc is available */
  222. static inline bool addrconf_qdisc_ok(const struct net_device *dev)
  223. {
  224. return !qdisc_tx_is_noop(dev);
  225. }
  226. static void addrconf_del_rs_timer(struct inet6_dev *idev)
  227. {
  228. if (del_timer(&idev->rs_timer))
  229. __in6_dev_put(idev);
  230. }
  231. static void addrconf_del_dad_timer(struct inet6_ifaddr *ifp)
  232. {
  233. if (del_timer(&ifp->dad_timer))
  234. __in6_ifa_put(ifp);
  235. }
  236. static void addrconf_mod_rs_timer(struct inet6_dev *idev,
  237. unsigned long when)
  238. {
  239. if (!timer_pending(&idev->rs_timer))
  240. in6_dev_hold(idev);
  241. mod_timer(&idev->rs_timer, jiffies + when);
  242. }
  243. static void addrconf_mod_dad_timer(struct inet6_ifaddr *ifp,
  244. unsigned long when)
  245. {
  246. if (!timer_pending(&ifp->dad_timer))
  247. in6_ifa_hold(ifp);
  248. mod_timer(&ifp->dad_timer, jiffies + when);
  249. }
  250. static int snmp6_alloc_dev(struct inet6_dev *idev)
  251. {
  252. if (snmp_mib_init((void __percpu **)idev->stats.ipv6,
  253. sizeof(struct ipstats_mib),
  254. __alignof__(struct ipstats_mib)) < 0)
  255. goto err_ip;
  256. idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
  257. GFP_KERNEL);
  258. if (!idev->stats.icmpv6dev)
  259. goto err_icmp;
  260. idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
  261. GFP_KERNEL);
  262. if (!idev->stats.icmpv6msgdev)
  263. goto err_icmpmsg;
  264. return 0;
  265. err_icmpmsg:
  266. kfree(idev->stats.icmpv6dev);
  267. err_icmp:
  268. snmp_mib_free((void __percpu **)idev->stats.ipv6);
  269. err_ip:
  270. return -ENOMEM;
  271. }
  272. static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
  273. {
  274. struct inet6_dev *ndev;
  275. ASSERT_RTNL();
  276. if (dev->mtu < IPV6_MIN_MTU)
  277. return NULL;
  278. ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
  279. if (ndev == NULL)
  280. return NULL;
  281. rwlock_init(&ndev->lock);
  282. ndev->dev = dev;
  283. INIT_LIST_HEAD(&ndev->addr_list);
  284. setup_timer(&ndev->rs_timer, addrconf_rs_timer,
  285. (unsigned long)ndev);
  286. memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
  287. ndev->cnf.mtu6 = dev->mtu;
  288. ndev->cnf.sysctl = NULL;
  289. ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
  290. if (ndev->nd_parms == NULL) {
  291. kfree(ndev);
  292. return NULL;
  293. }
  294. if (ndev->cnf.forwarding)
  295. dev_disable_lro(dev);
  296. /* We refer to the device */
  297. dev_hold(dev);
  298. if (snmp6_alloc_dev(ndev) < 0) {
  299. ADBG(KERN_WARNING
  300. "%s: cannot allocate memory for statistics; dev=%s.\n",
  301. __func__, dev->name);
  302. neigh_parms_release(&nd_tbl, ndev->nd_parms);
  303. dev_put(dev);
  304. kfree(ndev);
  305. return NULL;
  306. }
  307. if (snmp6_register_dev(ndev) < 0) {
  308. ADBG(KERN_WARNING
  309. "%s: cannot create /proc/net/dev_snmp6/%s\n",
  310. __func__, dev->name);
  311. neigh_parms_release(&nd_tbl, ndev->nd_parms);
  312. ndev->dead = 1;
  313. in6_dev_finish_destroy(ndev);
  314. return NULL;
  315. }
  316. /* One reference from device. We must do this before
  317. * we invoke __ipv6_regen_rndid().
  318. */
  319. in6_dev_hold(ndev);
  320. if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
  321. ndev->cnf.accept_dad = -1;
  322. #if IS_ENABLED(CONFIG_IPV6_SIT)
  323. if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
  324. pr_info("%s: Disabled Multicast RS\n", dev->name);
  325. ndev->cnf.rtr_solicits = 0;
  326. }
  327. #endif
  328. #ifdef CONFIG_IPV6_PRIVACY
  329. INIT_LIST_HEAD(&ndev->tempaddr_list);
  330. setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
  331. if ((dev->flags&IFF_LOOPBACK) ||
  332. dev->type == ARPHRD_TUNNEL ||
  333. dev->type == ARPHRD_TUNNEL6 ||
  334. dev->type == ARPHRD_SIT ||
  335. dev->type == ARPHRD_NONE) {
  336. ndev->cnf.use_tempaddr = -1;
  337. } else {
  338. in6_dev_hold(ndev);
  339. ipv6_regen_rndid((unsigned long) ndev);
  340. }
  341. #endif
  342. ndev->token = in6addr_any;
  343. if (netif_running(dev) && addrconf_qdisc_ok(dev))
  344. ndev->if_flags |= IF_READY;
  345. ipv6_mc_init_dev(ndev);
  346. ndev->tstamp = jiffies;
  347. addrconf_sysctl_register(ndev);
  348. /* protected by rtnl_lock */
  349. rcu_assign_pointer(dev->ip6_ptr, ndev);
  350. /* Join interface-local all-node multicast group */
  351. ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
  352. /* Join all-node multicast group */
  353. ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
  354. /* Join all-router multicast group if forwarding is set */
  355. if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
  356. ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
  357. return ndev;
  358. }
  359. static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
  360. {
  361. struct inet6_dev *idev;
  362. ASSERT_RTNL();
  363. idev = __in6_dev_get(dev);
  364. if (!idev) {
  365. idev = ipv6_add_dev(dev);
  366. if (!idev)
  367. return NULL;
  368. }
  369. if (dev->flags&IFF_UP)
  370. ipv6_mc_up(idev);
  371. return idev;
  372. }
  373. static int inet6_netconf_msgsize_devconf(int type)
  374. {
  375. int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
  376. + nla_total_size(4); /* NETCONFA_IFINDEX */
  377. /* type -1 is used for ALL */
  378. if (type == -1 || type == NETCONFA_FORWARDING)
  379. size += nla_total_size(4);
  380. #ifdef CONFIG_IPV6_MROUTE
  381. if (type == -1 || type == NETCONFA_MC_FORWARDING)
  382. size += nla_total_size(4);
  383. #endif
  384. return size;
  385. }
  386. static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
  387. struct ipv6_devconf *devconf, u32 portid,
  388. u32 seq, int event, unsigned int flags,
  389. int type)
  390. {
  391. struct nlmsghdr *nlh;
  392. struct netconfmsg *ncm;
  393. nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
  394. flags);
  395. if (nlh == NULL)
  396. return -EMSGSIZE;
  397. ncm = nlmsg_data(nlh);
  398. ncm->ncm_family = AF_INET6;
  399. if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
  400. goto nla_put_failure;
  401. /* type -1 is used for ALL */
  402. if ((type == -1 || type == NETCONFA_FORWARDING) &&
  403. nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
  404. goto nla_put_failure;
  405. #ifdef CONFIG_IPV6_MROUTE
  406. if ((type == -1 || type == NETCONFA_MC_FORWARDING) &&
  407. nla_put_s32(skb, NETCONFA_MC_FORWARDING,
  408. devconf->mc_forwarding) < 0)
  409. goto nla_put_failure;
  410. #endif
  411. return nlmsg_end(skb, nlh);
  412. nla_put_failure:
  413. nlmsg_cancel(skb, nlh);
  414. return -EMSGSIZE;
  415. }
  416. void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
  417. struct ipv6_devconf *devconf)
  418. {
  419. struct sk_buff *skb;
  420. int err = -ENOBUFS;
  421. skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_ATOMIC);
  422. if (skb == NULL)
  423. goto errout;
  424. err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
  425. RTM_NEWNETCONF, 0, type);
  426. if (err < 0) {
  427. /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
  428. WARN_ON(err == -EMSGSIZE);
  429. kfree_skb(skb);
  430. goto errout;
  431. }
  432. rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_ATOMIC);
  433. return;
  434. errout:
  435. rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
  436. }
  437. static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
  438. [NETCONFA_IFINDEX] = { .len = sizeof(int) },
  439. [NETCONFA_FORWARDING] = { .len = sizeof(int) },
  440. };
  441. static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
  442. struct nlmsghdr *nlh)
  443. {
  444. struct net *net = sock_net(in_skb->sk);
  445. struct nlattr *tb[NETCONFA_MAX+1];
  446. struct netconfmsg *ncm;
  447. struct sk_buff *skb;
  448. struct ipv6_devconf *devconf;
  449. struct inet6_dev *in6_dev;
  450. struct net_device *dev;
  451. int ifindex;
  452. int err;
  453. err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
  454. devconf_ipv6_policy);
  455. if (err < 0)
  456. goto errout;
  457. err = EINVAL;
  458. if (!tb[NETCONFA_IFINDEX])
  459. goto errout;
  460. ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
  461. switch (ifindex) {
  462. case NETCONFA_IFINDEX_ALL:
  463. devconf = net->ipv6.devconf_all;
  464. break;
  465. case NETCONFA_IFINDEX_DEFAULT:
  466. devconf = net->ipv6.devconf_dflt;
  467. break;
  468. default:
  469. dev = __dev_get_by_index(net, ifindex);
  470. if (dev == NULL)
  471. goto errout;
  472. in6_dev = __in6_dev_get(dev);
  473. if (in6_dev == NULL)
  474. goto errout;
  475. devconf = &in6_dev->cnf;
  476. break;
  477. }
  478. err = -ENOBUFS;
  479. skb = nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC);
  480. if (skb == NULL)
  481. goto errout;
  482. err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
  483. NETLINK_CB(in_skb).portid,
  484. nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
  485. -1);
  486. if (err < 0) {
  487. /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
  488. WARN_ON(err == -EMSGSIZE);
  489. kfree_skb(skb);
  490. goto errout;
  491. }
  492. err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
  493. errout:
  494. return err;
  495. }
  496. static int inet6_netconf_dump_devconf(struct sk_buff *skb,
  497. struct netlink_callback *cb)
  498. {
  499. struct net *net = sock_net(skb->sk);
  500. int h, s_h;
  501. int idx, s_idx;
  502. struct net_device *dev;
  503. struct inet6_dev *idev;
  504. struct hlist_head *head;
  505. s_h = cb->args[0];
  506. s_idx = idx = cb->args[1];
  507. for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
  508. idx = 0;
  509. head = &net->dev_index_head[h];
  510. rcu_read_lock();
  511. cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
  512. net->dev_base_seq;
  513. hlist_for_each_entry_rcu(dev, head, index_hlist) {
  514. if (idx < s_idx)
  515. goto cont;
  516. idev = __in6_dev_get(dev);
  517. if (!idev)
  518. goto cont;
  519. if (inet6_netconf_fill_devconf(skb, dev->ifindex,
  520. &idev->cnf,
  521. NETLINK_CB(cb->skb).portid,
  522. cb->nlh->nlmsg_seq,
  523. RTM_NEWNETCONF,
  524. NLM_F_MULTI,
  525. -1) <= 0) {
  526. rcu_read_unlock();
  527. goto done;
  528. }
  529. nl_dump_check_consistent(cb, nlmsg_hdr(skb));
  530. cont:
  531. idx++;
  532. }
  533. rcu_read_unlock();
  534. }
  535. if (h == NETDEV_HASHENTRIES) {
  536. if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
  537. net->ipv6.devconf_all,
  538. NETLINK_CB(cb->skb).portid,
  539. cb->nlh->nlmsg_seq,
  540. RTM_NEWNETCONF, NLM_F_MULTI,
  541. -1) <= 0)
  542. goto done;
  543. else
  544. h++;
  545. }
  546. if (h == NETDEV_HASHENTRIES + 1) {
  547. if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
  548. net->ipv6.devconf_dflt,
  549. NETLINK_CB(cb->skb).portid,
  550. cb->nlh->nlmsg_seq,
  551. RTM_NEWNETCONF, NLM_F_MULTI,
  552. -1) <= 0)
  553. goto done;
  554. else
  555. h++;
  556. }
  557. done:
  558. cb->args[0] = h;
  559. cb->args[1] = idx;
  560. return skb->len;
  561. }
  562. #ifdef CONFIG_SYSCTL
  563. static void dev_forward_change(struct inet6_dev *idev)
  564. {
  565. struct net_device *dev;
  566. struct inet6_ifaddr *ifa;
  567. if (!idev)
  568. return;
  569. dev = idev->dev;
  570. if (idev->cnf.forwarding)
  571. dev_disable_lro(dev);
  572. if (dev->flags & IFF_MULTICAST) {
  573. if (idev->cnf.forwarding) {
  574. ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
  575. ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
  576. ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
  577. } else {
  578. ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
  579. ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
  580. ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
  581. }
  582. }
  583. list_for_each_entry(ifa, &idev->addr_list, if_list) {
  584. if (ifa->flags&IFA_F_TENTATIVE)
  585. continue;
  586. if (idev->cnf.forwarding)
  587. addrconf_join_anycast(ifa);
  588. else
  589. addrconf_leave_anycast(ifa);
  590. }
  591. inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
  592. dev->ifindex, &idev->cnf);
  593. }
  594. static void addrconf_forward_change(struct net *net, __s32 newf)
  595. {
  596. struct net_device *dev;
  597. struct inet6_dev *idev;
  598. for_each_netdev(net, dev) {
  599. idev = __in6_dev_get(dev);
  600. if (idev) {
  601. int changed = (!idev->cnf.forwarding) ^ (!newf);
  602. idev->cnf.forwarding = newf;
  603. if (changed)
  604. dev_forward_change(idev);
  605. }
  606. }
  607. }
  608. static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
  609. {
  610. struct net *net;
  611. int old;
  612. if (!rtnl_trylock())
  613. return restart_syscall();
  614. net = (struct net *)table->extra2;
  615. old = *p;
  616. *p = newf;
  617. if (p == &net->ipv6.devconf_dflt->forwarding) {
  618. if ((!newf) ^ (!old))
  619. inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
  620. NETCONFA_IFINDEX_DEFAULT,
  621. net->ipv6.devconf_dflt);
  622. rtnl_unlock();
  623. return 0;
  624. }
  625. if (p == &net->ipv6.devconf_all->forwarding) {
  626. net->ipv6.devconf_dflt->forwarding = newf;
  627. addrconf_forward_change(net, newf);
  628. if ((!newf) ^ (!old))
  629. inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
  630. NETCONFA_IFINDEX_ALL,
  631. net->ipv6.devconf_all);
  632. } else if ((!newf) ^ (!old))
  633. dev_forward_change((struct inet6_dev *)table->extra1);
  634. rtnl_unlock();
  635. if (newf)
  636. rt6_purge_dflt_routers(net);
  637. return 1;
  638. }
  639. #endif
  640. /* Nobody refers to this ifaddr, destroy it */
  641. void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
  642. {
  643. WARN_ON(!hlist_unhashed(&ifp->addr_lst));
  644. #ifdef NET_REFCNT_DEBUG
  645. pr_debug("%s\n", __func__);
  646. #endif
  647. in6_dev_put(ifp->idev);
  648. if (del_timer(&ifp->dad_timer))
  649. pr_notice("Timer is still running, when freeing ifa=%p\n", ifp);
  650. if (ifp->state != INET6_IFADDR_STATE_DEAD) {
  651. pr_warn("Freeing alive inet6 address %p\n", ifp);
  652. return;
  653. }
  654. ip6_rt_put(ifp->rt);
  655. kfree_rcu(ifp, rcu);
  656. }
  657. static void
  658. ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
  659. {
  660. struct list_head *p;
  661. int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
  662. /*
  663. * Each device address list is sorted in order of scope -
  664. * global before linklocal.
  665. */
  666. list_for_each(p, &idev->addr_list) {
  667. struct inet6_ifaddr *ifa
  668. = list_entry(p, struct inet6_ifaddr, if_list);
  669. if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
  670. break;
  671. }
  672. list_add_tail(&ifp->if_list, p);
  673. }
  674. static u32 inet6_addr_hash(const struct in6_addr *addr)
  675. {
  676. return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
  677. }
  678. /* On success it returns ifp with increased reference count */
  679. static struct inet6_ifaddr *
  680. ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
  681. const struct in6_addr *peer_addr, int pfxlen,
  682. int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
  683. {
  684. struct inet6_ifaddr *ifa = NULL;
  685. struct rt6_info *rt;
  686. unsigned int hash;
  687. int err = 0;
  688. int addr_type = ipv6_addr_type(addr);
  689. if (addr_type == IPV6_ADDR_ANY ||
  690. addr_type & IPV6_ADDR_MULTICAST ||
  691. (!(idev->dev->flags & IFF_LOOPBACK) &&
  692. addr_type & IPV6_ADDR_LOOPBACK))
  693. return ERR_PTR(-EADDRNOTAVAIL);
  694. rcu_read_lock_bh();
  695. if (idev->dead) {
  696. err = -ENODEV; /*XXX*/
  697. goto out2;
  698. }
  699. if (idev->cnf.disable_ipv6) {
  700. err = -EACCES;
  701. goto out2;
  702. }
  703. spin_lock(&addrconf_hash_lock);
  704. /* Ignore adding duplicate addresses on an interface */
  705. if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
  706. ADBG("ipv6_add_addr: already assigned\n");
  707. err = -EEXIST;
  708. goto out;
  709. }
  710. ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
  711. if (ifa == NULL) {
  712. ADBG("ipv6_add_addr: malloc failed\n");
  713. err = -ENOBUFS;
  714. goto out;
  715. }
  716. rt = addrconf_dst_alloc(idev, addr, false);
  717. if (IS_ERR(rt)) {
  718. err = PTR_ERR(rt);
  719. goto out;
  720. }
  721. ifa->addr = *addr;
  722. if (peer_addr)
  723. ifa->peer_addr = *peer_addr;
  724. spin_lock_init(&ifa->lock);
  725. spin_lock_init(&ifa->state_lock);
  726. setup_timer(&ifa->dad_timer, addrconf_dad_timer,
  727. (unsigned long)ifa);
  728. INIT_HLIST_NODE(&ifa->addr_lst);
  729. ifa->scope = scope;
  730. ifa->prefix_len = pfxlen;
  731. ifa->flags = flags | IFA_F_TENTATIVE;
  732. ifa->valid_lft = valid_lft;
  733. ifa->prefered_lft = prefered_lft;
  734. ifa->cstamp = ifa->tstamp = jiffies;
  735. ifa->tokenized = false;
  736. ifa->rt = rt;
  737. ifa->idev = idev;
  738. in6_dev_hold(idev);
  739. /* For caller */
  740. in6_ifa_hold(ifa);
  741. /* Add to big hash table */
  742. hash = inet6_addr_hash(addr);
  743. hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
  744. spin_unlock(&addrconf_hash_lock);
  745. write_lock(&idev->lock);
  746. /* Add to inet6_dev unicast addr list. */
  747. ipv6_link_dev_addr(idev, ifa);
  748. #ifdef CONFIG_IPV6_PRIVACY
  749. if (ifa->flags&IFA_F_TEMPORARY) {
  750. list_add(&ifa->tmp_list, &idev->tempaddr_list);
  751. in6_ifa_hold(ifa);
  752. }
  753. #endif
  754. in6_ifa_hold(ifa);
  755. write_unlock(&idev->lock);
  756. out2:
  757. rcu_read_unlock_bh();
  758. if (likely(err == 0))
  759. inet6addr_notifier_call_chain(NETDEV_UP, ifa);
  760. else {
  761. kfree(ifa);
  762. ifa = ERR_PTR(err);
  763. }
  764. return ifa;
  765. out:
  766. spin_unlock(&addrconf_hash_lock);
  767. goto out2;
  768. }
  769. /* This function wants to get referenced ifp and releases it before return */
  770. static void ipv6_del_addr(struct inet6_ifaddr *ifp)
  771. {
  772. struct inet6_ifaddr *ifa, *ifn;
  773. struct inet6_dev *idev = ifp->idev;
  774. int state;
  775. int deleted = 0, onlink = 0;
  776. unsigned long expires = jiffies;
  777. spin_lock_bh(&ifp->state_lock);
  778. state = ifp->state;
  779. ifp->state = INET6_IFADDR_STATE_DEAD;
  780. spin_unlock_bh(&ifp->state_lock);
  781. if (state == INET6_IFADDR_STATE_DEAD)
  782. goto out;
  783. spin_lock_bh(&addrconf_hash_lock);
  784. hlist_del_init_rcu(&ifp->addr_lst);
  785. spin_unlock_bh(&addrconf_hash_lock);
  786. write_lock_bh(&idev->lock);
  787. #ifdef CONFIG_IPV6_PRIVACY
  788. if (ifp->flags&IFA_F_TEMPORARY) {
  789. list_del(&ifp->tmp_list);
  790. if (ifp->ifpub) {
  791. in6_ifa_put(ifp->ifpub);
  792. ifp->ifpub = NULL;
  793. }
  794. __in6_ifa_put(ifp);
  795. }
  796. #endif
  797. list_for_each_entry_safe(ifa, ifn, &idev->addr_list, if_list) {
  798. if (ifa == ifp) {
  799. list_del_init(&ifp->if_list);
  800. __in6_ifa_put(ifp);
  801. if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
  802. break;
  803. deleted = 1;
  804. continue;
  805. } else if (ifp->flags & IFA_F_PERMANENT) {
  806. if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
  807. ifp->prefix_len)) {
  808. if (ifa->flags & IFA_F_PERMANENT) {
  809. onlink = 1;
  810. if (deleted)
  811. break;
  812. } else {
  813. unsigned long lifetime;
  814. if (!onlink)
  815. onlink = -1;
  816. spin_lock(&ifa->lock);
  817. lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
  818. /*
  819. * Note: Because this address is
  820. * not permanent, lifetime <
  821. * LONG_MAX / HZ here.
  822. */
  823. if (time_before(expires,
  824. ifa->tstamp + lifetime * HZ))
  825. expires = ifa->tstamp + lifetime * HZ;
  826. spin_unlock(&ifa->lock);
  827. }
  828. }
  829. }
  830. }
  831. write_unlock_bh(&idev->lock);
  832. addrconf_del_dad_timer(ifp);
  833. ipv6_ifa_notify(RTM_DELADDR, ifp);
  834. inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
  835. /*
  836. * Purge or update corresponding prefix
  837. *
  838. * 1) we don't purge prefix here if address was not permanent.
  839. * prefix is managed by its own lifetime.
  840. * 2) if there're no addresses, delete prefix.
  841. * 3) if there're still other permanent address(es),
  842. * corresponding prefix is still permanent.
  843. * 4) otherwise, update prefix lifetime to the
  844. * longest valid lifetime among the corresponding
  845. * addresses on the device.
  846. * Note: subsequent RA will update lifetime.
  847. *
  848. * --yoshfuji
  849. */
  850. if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
  851. struct in6_addr prefix;
  852. struct rt6_info *rt;
  853. ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
  854. rt = addrconf_get_prefix_route(&prefix,
  855. ifp->prefix_len,
  856. ifp->idev->dev,
  857. 0, RTF_GATEWAY | RTF_DEFAULT);
  858. if (rt) {
  859. if (onlink == 0) {
  860. ip6_del_rt(rt);
  861. rt = NULL;
  862. } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
  863. rt6_set_expires(rt, expires);
  864. }
  865. }
  866. ip6_rt_put(rt);
  867. }
  868. /* clean up prefsrc entries */
  869. rt6_remove_prefsrc(ifp);
  870. out:
  871. in6_ifa_put(ifp);
  872. }
  873. #ifdef CONFIG_IPV6_PRIVACY
  874. static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
  875. {
  876. struct inet6_dev *idev = ifp->idev;
  877. struct in6_addr addr, *tmpaddr;
  878. unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
  879. unsigned long regen_advance;
  880. int tmp_plen;
  881. int ret = 0;
  882. u32 addr_flags;
  883. unsigned long now = jiffies;
  884. write_lock(&idev->lock);
  885. if (ift) {
  886. spin_lock_bh(&ift->lock);
  887. memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
  888. spin_unlock_bh(&ift->lock);
  889. tmpaddr = &addr;
  890. } else {
  891. tmpaddr = NULL;
  892. }
  893. retry:
  894. in6_dev_hold(idev);
  895. if (idev->cnf.use_tempaddr <= 0) {
  896. write_unlock(&idev->lock);
  897. pr_info("%s: use_tempaddr is disabled\n", __func__);
  898. in6_dev_put(idev);
  899. ret = -1;
  900. goto out;
  901. }
  902. spin_lock_bh(&ifp->lock);
  903. if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
  904. idev->cnf.use_tempaddr = -1; /*XXX*/
  905. spin_unlock_bh(&ifp->lock);
  906. write_unlock(&idev->lock);
  907. pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
  908. __func__);
  909. in6_dev_put(idev);
  910. ret = -1;
  911. goto out;
  912. }
  913. in6_ifa_hold(ifp);
  914. memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
  915. __ipv6_try_regen_rndid(idev, tmpaddr);
  916. memcpy(&addr.s6_addr[8], idev->rndid, 8);
  917. age = (now - ifp->tstamp) / HZ;
  918. tmp_valid_lft = min_t(__u32,
  919. ifp->valid_lft,
  920. idev->cnf.temp_valid_lft + age);
  921. tmp_prefered_lft = min_t(__u32,
  922. ifp->prefered_lft,
  923. idev->cnf.temp_prefered_lft + age -
  924. idev->cnf.max_desync_factor);
  925. tmp_plen = ifp->prefix_len;
  926. tmp_tstamp = ifp->tstamp;
  927. spin_unlock_bh(&ifp->lock);
  928. regen_advance = idev->cnf.regen_max_retry *
  929. idev->cnf.dad_transmits *
  930. idev->nd_parms->retrans_time / HZ;
  931. write_unlock(&idev->lock);
  932. /* A temporary address is created only if this calculated Preferred
  933. * Lifetime is greater than REGEN_ADVANCE time units. In particular,
  934. * an implementation must not create a temporary address with a zero
  935. * Preferred Lifetime.
  936. */
  937. if (tmp_prefered_lft <= regen_advance) {
  938. in6_ifa_put(ifp);
  939. in6_dev_put(idev);
  940. ret = -1;
  941. goto out;
  942. }
  943. addr_flags = IFA_F_TEMPORARY;
  944. /* set in addrconf_prefix_rcv() */
  945. if (ifp->flags & IFA_F_OPTIMISTIC)
  946. addr_flags |= IFA_F_OPTIMISTIC;
  947. ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
  948. ipv6_addr_scope(&addr), addr_flags,
  949. tmp_valid_lft, tmp_prefered_lft);
  950. if (IS_ERR(ift)) {
  951. in6_ifa_put(ifp);
  952. in6_dev_put(idev);
  953. pr_info("%s: retry temporary address regeneration\n", __func__);
  954. tmpaddr = &addr;
  955. write_lock(&idev->lock);
  956. goto retry;
  957. }
  958. spin_lock_bh(&ift->lock);
  959. ift->ifpub = ifp;
  960. ift->cstamp = now;
  961. ift->tstamp = tmp_tstamp;
  962. spin_unlock_bh(&ift->lock);
  963. addrconf_dad_start(ift);
  964. in6_ifa_put(ift);
  965. in6_dev_put(idev);
  966. out:
  967. return ret;
  968. }
  969. #endif
  970. /*
  971. * Choose an appropriate source address (RFC3484)
  972. */
  973. enum {
  974. IPV6_SADDR_RULE_INIT = 0,
  975. IPV6_SADDR_RULE_LOCAL,
  976. IPV6_SADDR_RULE_SCOPE,
  977. IPV6_SADDR_RULE_PREFERRED,
  978. #ifdef CONFIG_IPV6_MIP6
  979. IPV6_SADDR_RULE_HOA,
  980. #endif
  981. IPV6_SADDR_RULE_OIF,
  982. IPV6_SADDR_RULE_LABEL,
  983. #ifdef CONFIG_IPV6_PRIVACY
  984. IPV6_SADDR_RULE_PRIVACY,
  985. #endif
  986. IPV6_SADDR_RULE_ORCHID,
  987. IPV6_SADDR_RULE_PREFIX,
  988. IPV6_SADDR_RULE_MAX
  989. };
  990. struct ipv6_saddr_score {
  991. int rule;
  992. int addr_type;
  993. struct inet6_ifaddr *ifa;
  994. DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
  995. int scopedist;
  996. int matchlen;
  997. };
  998. struct ipv6_saddr_dst {
  999. const struct in6_addr *addr;
  1000. int ifindex;
  1001. int scope;
  1002. int label;
  1003. unsigned int prefs;
  1004. };
  1005. static inline int ipv6_saddr_preferred(int type)
  1006. {
  1007. if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
  1008. return 1;
  1009. return 0;
  1010. }
  1011. static int ipv6_get_saddr_eval(struct net *net,
  1012. struct ipv6_saddr_score *score,
  1013. struct ipv6_saddr_dst *dst,
  1014. int i)
  1015. {
  1016. int ret;
  1017. if (i <= score->rule) {
  1018. switch (i) {
  1019. case IPV6_SADDR_RULE_SCOPE:
  1020. ret = score->scopedist;
  1021. break;
  1022. case IPV6_SADDR_RULE_PREFIX:
  1023. ret = score->matchlen;
  1024. break;
  1025. default:
  1026. ret = !!test_bit(i, score->scorebits);
  1027. }
  1028. goto out;
  1029. }
  1030. switch (i) {
  1031. case IPV6_SADDR_RULE_INIT:
  1032. /* Rule 0: remember if hiscore is not ready yet */
  1033. ret = !!score->ifa;
  1034. break;
  1035. case IPV6_SADDR_RULE_LOCAL:
  1036. /* Rule 1: Prefer same address */
  1037. ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
  1038. break;
  1039. case IPV6_SADDR_RULE_SCOPE:
  1040. /* Rule 2: Prefer appropriate scope
  1041. *
  1042. * ret
  1043. * ^
  1044. * -1 | d 15
  1045. * ---+--+-+---> scope
  1046. * |
  1047. * | d is scope of the destination.
  1048. * B-d | \
  1049. * | \ <- smaller scope is better if
  1050. * B-15 | \ if scope is enough for destinaion.
  1051. * | ret = B - scope (-1 <= scope >= d <= 15).
  1052. * d-C-1 | /
  1053. * |/ <- greater is better
  1054. * -C / if scope is not enough for destination.
  1055. * /| ret = scope - C (-1 <= d < scope <= 15).
  1056. *
  1057. * d - C - 1 < B -15 (for all -1 <= d <= 15).
  1058. * C > d + 14 - B >= 15 + 14 - B = 29 - B.
  1059. * Assume B = 0 and we get C > 29.
  1060. */
  1061. ret = __ipv6_addr_src_scope(score->addr_type);
  1062. if (ret >= dst->scope)
  1063. ret = -ret;
  1064. else
  1065. ret -= 128; /* 30 is enough */
  1066. score->scopedist = ret;
  1067. break;
  1068. case IPV6_SADDR_RULE_PREFERRED:
  1069. /* Rule 3: Avoid deprecated and optimistic addresses */
  1070. ret = ipv6_saddr_preferred(score->addr_type) ||
  1071. !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC));
  1072. break;
  1073. #ifdef CONFIG_IPV6_MIP6
  1074. case IPV6_SADDR_RULE_HOA:
  1075. {
  1076. /* Rule 4: Prefer home address */
  1077. int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
  1078. ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
  1079. break;
  1080. }
  1081. #endif
  1082. case IPV6_SADDR_RULE_OIF:
  1083. /* Rule 5: Prefer outgoing interface */
  1084. ret = (!dst->ifindex ||
  1085. dst->ifindex == score->ifa->idev->dev->ifindex);
  1086. break;
  1087. case IPV6_SADDR_RULE_LABEL:
  1088. /* Rule 6: Prefer matching label */
  1089. ret = ipv6_addr_label(net,
  1090. &score->ifa->addr, score->addr_type,
  1091. score->ifa->idev->dev->ifindex) == dst->label;
  1092. break;
  1093. #ifdef CONFIG_IPV6_PRIVACY
  1094. case IPV6_SADDR_RULE_PRIVACY:
  1095. {
  1096. /* Rule 7: Prefer public address
  1097. * Note: prefer temporary address if use_tempaddr >= 2
  1098. */
  1099. int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
  1100. !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
  1101. score->ifa->idev->cnf.use_tempaddr >= 2;
  1102. ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
  1103. break;
  1104. }
  1105. #endif
  1106. case IPV6_SADDR_RULE_ORCHID:
  1107. /* Rule 8-: Prefer ORCHID vs ORCHID or
  1108. * non-ORCHID vs non-ORCHID
  1109. */
  1110. ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
  1111. ipv6_addr_orchid(dst->addr));
  1112. break;
  1113. case IPV6_SADDR_RULE_PREFIX:
  1114. /* Rule 8: Use longest matching prefix */
  1115. ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
  1116. if (ret > score->ifa->prefix_len)
  1117. ret = score->ifa->prefix_len;
  1118. score->matchlen = ret;
  1119. break;
  1120. default:
  1121. ret = 0;
  1122. }
  1123. if (ret)
  1124. __set_bit(i, score->scorebits);
  1125. score->rule = i;
  1126. out:
  1127. return ret;
  1128. }
  1129. int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
  1130. const struct in6_addr *daddr, unsigned int prefs,
  1131. struct in6_addr *saddr)
  1132. {
  1133. struct ipv6_saddr_score scores[2],
  1134. *score = &scores[0], *hiscore = &scores[1];
  1135. struct ipv6_saddr_dst dst;
  1136. struct net_device *dev;
  1137. int dst_type;
  1138. dst_type = __ipv6_addr_type(daddr);
  1139. dst.addr = daddr;
  1140. dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
  1141. dst.scope = __ipv6_addr_src_scope(dst_type);
  1142. dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
  1143. dst.prefs = prefs;
  1144. hiscore->rule = -1;
  1145. hiscore->ifa = NULL;
  1146. rcu_read_lock();
  1147. for_each_netdev_rcu(net, dev) {
  1148. struct inet6_dev *idev;
  1149. /* Candidate Source Address (section 4)
  1150. * - multicast and link-local destination address,
  1151. * the set of candidate source address MUST only
  1152. * include addresses assigned to interfaces
  1153. * belonging to the same link as the outgoing
  1154. * interface.
  1155. * (- For site-local destination addresses, the
  1156. * set of candidate source addresses MUST only
  1157. * include addresses assigned to interfaces
  1158. * belonging to the same site as the outgoing
  1159. * interface.)
  1160. */
  1161. if (((dst_type & IPV6_ADDR_MULTICAST) ||
  1162. dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
  1163. dst.ifindex && dev->ifindex != dst.ifindex)
  1164. continue;
  1165. idev = __in6_dev_get(dev);
  1166. if (!idev)
  1167. continue;
  1168. read_lock_bh(&idev->lock);
  1169. list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
  1170. int i;
  1171. /*
  1172. * - Tentative Address (RFC2462 section 5.4)
  1173. * - A tentative address is not considered
  1174. * "assigned to an interface" in the traditional
  1175. * sense, unless it is also flagged as optimistic.
  1176. * - Candidate Source Address (section 4)
  1177. * - In any case, anycast addresses, multicast
  1178. * addresses, and the unspecified address MUST
  1179. * NOT be included in a candidate set.
  1180. */
  1181. if ((score->ifa->flags & IFA_F_TENTATIVE) &&
  1182. (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
  1183. continue;
  1184. score->addr_type = __ipv6_addr_type(&score->ifa->addr);
  1185. if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
  1186. score->addr_type & IPV6_ADDR_MULTICAST)) {
  1187. LIMIT_NETDEBUG(KERN_DEBUG
  1188. "ADDRCONF: unspecified / multicast address "
  1189. "assigned as unicast address on %s",
  1190. dev->name);
  1191. continue;
  1192. }
  1193. score->rule = -1;
  1194. bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
  1195. for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
  1196. int minihiscore, miniscore;
  1197. minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i);
  1198. miniscore = ipv6_get_saddr_eval(net, score, &dst, i);
  1199. if (minihiscore > miniscore) {
  1200. if (i == IPV6_SADDR_RULE_SCOPE &&
  1201. score->scopedist > 0) {
  1202. /*
  1203. * special case:
  1204. * each remaining entry
  1205. * has too small (not enough)
  1206. * scope, because ifa entries
  1207. * are sorted by their scope
  1208. * values.
  1209. */
  1210. goto try_nextdev;
  1211. }
  1212. break;
  1213. } else if (minihiscore < miniscore) {
  1214. if (hiscore->ifa)
  1215. in6_ifa_put(hiscore->ifa);
  1216. in6_ifa_hold(score->ifa);
  1217. swap(hiscore, score);
  1218. /* restore our iterator */
  1219. score->ifa = hiscore->ifa;
  1220. break;
  1221. }
  1222. }
  1223. }
  1224. try_nextdev:
  1225. read_unlock_bh(&idev->lock);
  1226. }
  1227. rcu_read_unlock();
  1228. if (!hiscore->ifa)
  1229. return -EADDRNOTAVAIL;
  1230. *saddr = hiscore->ifa->addr;
  1231. in6_ifa_put(hiscore->ifa);
  1232. return 0;
  1233. }
  1234. EXPORT_SYMBOL(ipv6_dev_get_saddr);
  1235. int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
  1236. unsigned char banned_flags)
  1237. {
  1238. struct inet6_ifaddr *ifp;
  1239. int err = -EADDRNOTAVAIL;
  1240. list_for_each_entry(ifp, &idev->addr_list, if_list) {
  1241. if (ifp->scope == IFA_LINK &&
  1242. !(ifp->flags & banned_flags)) {
  1243. *addr = ifp->addr;
  1244. err = 0;
  1245. break;
  1246. }
  1247. }
  1248. return err;
  1249. }
  1250. int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
  1251. unsigned char banned_flags)
  1252. {
  1253. struct inet6_dev *idev;
  1254. int err = -EADDRNOTAVAIL;
  1255. rcu_read_lock();
  1256. idev = __in6_dev_get(dev);
  1257. if (idev) {
  1258. read_lock_bh(&idev->lock);
  1259. err = __ipv6_get_lladdr(idev, addr, banned_flags);
  1260. read_unlock_bh(&idev->lock);
  1261. }
  1262. rcu_read_unlock();
  1263. return err;
  1264. }
  1265. static int ipv6_count_addresses(struct inet6_dev *idev)
  1266. {
  1267. int cnt = 0;
  1268. struct inet6_ifaddr *ifp;
  1269. read_lock_bh(&idev->lock);
  1270. list_for_each_entry(ifp, &idev->addr_list, if_list)
  1271. cnt++;
  1272. read_unlock_bh(&idev->lock);
  1273. return cnt;
  1274. }
  1275. int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
  1276. const struct net_device *dev, int strict)
  1277. {
  1278. struct inet6_ifaddr *ifp;
  1279. unsigned int hash = inet6_addr_hash(addr);
  1280. rcu_read_lock_bh();
  1281. hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
  1282. if (!net_eq(dev_net(ifp->idev->dev), net))
  1283. continue;
  1284. if (ipv6_addr_equal(&ifp->addr, addr) &&
  1285. !(ifp->flags&IFA_F_TENTATIVE) &&
  1286. (dev == NULL || ifp->idev->dev == dev ||
  1287. !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
  1288. rcu_read_unlock_bh();
  1289. return 1;
  1290. }
  1291. }
  1292. rcu_read_unlock_bh();
  1293. return 0;
  1294. }
  1295. EXPORT_SYMBOL(ipv6_chk_addr);
  1296. static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
  1297. struct net_device *dev)
  1298. {
  1299. unsigned int hash = inet6_addr_hash(addr);
  1300. struct inet6_ifaddr *ifp;
  1301. hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
  1302. if (!net_eq(dev_net(ifp->idev->dev), net))
  1303. continue;
  1304. if (ipv6_addr_equal(&ifp->addr, addr)) {
  1305. if (dev == NULL || ifp->idev->dev == dev)
  1306. return true;
  1307. }
  1308. }
  1309. return false;
  1310. }
  1311. /* Compares an address/prefix_len with addresses on device @dev.
  1312. * If one is found it returns true.
  1313. */
  1314. bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
  1315. const unsigned int prefix_len, struct net_device *dev)
  1316. {
  1317. struct inet6_dev *idev;
  1318. struct inet6_ifaddr *ifa;
  1319. bool ret = false;
  1320. rcu_read_lock();
  1321. idev = __in6_dev_get(dev);
  1322. if (idev) {
  1323. read_lock_bh(&idev->lock);
  1324. list_for_each_entry(ifa, &idev->addr_list, if_list) {
  1325. ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
  1326. if (ret)
  1327. break;
  1328. }
  1329. read_unlock_bh(&idev->lock);
  1330. }
  1331. rcu_read_unlock();
  1332. return ret;
  1333. }
  1334. EXPORT_SYMBOL(ipv6_chk_custom_prefix);
  1335. int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
  1336. {
  1337. struct inet6_dev *idev;
  1338. struct inet6_ifaddr *ifa;
  1339. int onlink;
  1340. onlink = 0;
  1341. rcu_read_lock();
  1342. idev = __in6_dev_get(dev);
  1343. if (idev) {
  1344. read_lock_bh(&idev->lock);
  1345. list_for_each_entry(ifa, &idev->addr_list, if_list) {
  1346. onlink = ipv6_prefix_equal(addr, &ifa->addr,
  1347. ifa->prefix_len);
  1348. if (onlink)
  1349. break;
  1350. }
  1351. read_unlock_bh(&idev->lock);
  1352. }
  1353. rcu_read_unlock();
  1354. return onlink;
  1355. }
  1356. EXPORT_SYMBOL(ipv6_chk_prefix);
  1357. struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
  1358. struct net_device *dev, int strict)
  1359. {
  1360. struct inet6_ifaddr *ifp, *result = NULL;
  1361. unsigned int hash = inet6_addr_hash(addr);
  1362. rcu_read_lock_bh();
  1363. hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
  1364. if (!net_eq(dev_net(ifp->idev->dev), net))
  1365. continue;
  1366. if (ipv6_addr_equal(&ifp->addr, addr)) {
  1367. if (dev == NULL || ifp->idev->dev == dev ||
  1368. !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
  1369. result = ifp;
  1370. in6_ifa_hold(ifp);
  1371. break;
  1372. }
  1373. }
  1374. }
  1375. rcu_read_unlock_bh();
  1376. return result;
  1377. }
  1378. /* Gets referenced address, destroys ifaddr */
  1379. static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
  1380. {
  1381. if (ifp->flags&IFA_F_PERMANENT) {
  1382. spin_lock_bh(&ifp->lock);
  1383. addrconf_del_dad_timer(ifp);
  1384. ifp->flags |= IFA_F_TENTATIVE;
  1385. if (dad_failed)
  1386. ifp->flags |= IFA_F_DADFAILED;
  1387. spin_unlock_bh(&ifp->lock);
  1388. if (dad_failed)
  1389. ipv6_ifa_notify(0, ifp);
  1390. in6_ifa_put(ifp);
  1391. #ifdef CONFIG_IPV6_PRIVACY
  1392. } else if (ifp->flags&IFA_F_TEMPORARY) {
  1393. struct inet6_ifaddr *ifpub;
  1394. spin_lock_bh(&ifp->lock);
  1395. ifpub = ifp->ifpub;
  1396. if (ifpub) {
  1397. in6_ifa_hold(ifpub);
  1398. spin_unlock_bh(&ifp->lock);
  1399. ipv6_create_tempaddr(ifpub, ifp);
  1400. in6_ifa_put(ifpub);
  1401. } else {
  1402. spin_unlock_bh(&ifp->lock);
  1403. }
  1404. ipv6_del_addr(ifp);
  1405. #endif
  1406. } else
  1407. ipv6_del_addr(ifp);
  1408. }
  1409. static int addrconf_dad_end(struct inet6_ifaddr *ifp)
  1410. {
  1411. int err = -ENOENT;
  1412. spin_lock(&ifp->state_lock);
  1413. if (ifp->state == INET6_IFADDR_STATE_DAD) {
  1414. ifp->state = INET6_IFADDR_STATE_POSTDAD;
  1415. err = 0;
  1416. }
  1417. spin_unlock(&ifp->state_lock);
  1418. return err;
  1419. }
  1420. void addrconf_dad_failure(struct inet6_ifaddr *ifp)
  1421. {
  1422. struct inet6_dev *idev = ifp->idev;
  1423. if (addrconf_dad_end(ifp)) {
  1424. in6_ifa_put(ifp);
  1425. return;
  1426. }
  1427. net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
  1428. ifp->idev->dev->name, &ifp->addr);
  1429. if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
  1430. struct in6_addr addr;
  1431. addr.s6_addr32[0] = htonl(0xfe800000);
  1432. addr.s6_addr32[1] = 0;
  1433. if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
  1434. ipv6_addr_equal(&ifp->addr, &addr)) {
  1435. /* DAD failed for link-local based on MAC address */
  1436. idev->cnf.disable_ipv6 = 1;
  1437. pr_info("%s: IPv6 being disabled!\n",
  1438. ifp->idev->dev->name);
  1439. }
  1440. }
  1441. addrconf_dad_stop(ifp, 1);
  1442. }
  1443. /* Join to solicited addr multicast group. */
  1444. void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
  1445. {
  1446. struct in6_addr maddr;
  1447. if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
  1448. return;
  1449. addrconf_addr_solict_mult(addr, &maddr);
  1450. ipv6_dev_mc_inc(dev, &maddr);
  1451. }
  1452. void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
  1453. {
  1454. struct in6_addr maddr;
  1455. if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
  1456. return;
  1457. addrconf_addr_solict_mult(addr, &maddr);
  1458. __ipv6_dev_mc_dec(idev, &maddr);
  1459. }
  1460. static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
  1461. {
  1462. struct in6_addr addr;
  1463. if (ifp->prefix_len == 127) /* RFC 6164 */
  1464. return;
  1465. ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
  1466. if (ipv6_addr_any(&addr))
  1467. return;
  1468. ipv6_dev_ac_inc(ifp->idev->dev, &addr);
  1469. }
  1470. static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
  1471. {
  1472. struct in6_addr addr;
  1473. if (ifp->prefix_len == 127) /* RFC 6164 */
  1474. return;
  1475. ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
  1476. if (ipv6_addr_any(&addr))
  1477. return;
  1478. __ipv6_dev_ac_dec(ifp->idev, &addr);
  1479. }
  1480. static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
  1481. {
  1482. if (dev->addr_len != ETH_ALEN)
  1483. return -1;
  1484. memcpy(eui, dev->dev_addr, 3);
  1485. memcpy(eui + 5, dev->dev_addr + 3, 3);
  1486. /*
  1487. * The zSeries OSA network cards can be shared among various
  1488. * OS instances, but the OSA cards have only one MAC address.
  1489. * This leads to duplicate address conflicts in conjunction
  1490. * with IPv6 if more than one instance uses the same card.
  1491. *
  1492. * The driver for these cards can deliver a unique 16-bit
  1493. * identifier for each instance sharing the same card. It is
  1494. * placed instead of 0xFFFE in the interface identifier. The
  1495. * "u" bit of the interface identifier is not inverted in this
  1496. * case. Hence the resulting interface identifier has local
  1497. * scope according to RFC2373.
  1498. */
  1499. if (dev->dev_id) {
  1500. eui[3] = (dev->dev_id >> 8) & 0xFF;
  1501. eui[4] = dev->dev_id & 0xFF;
  1502. } else {
  1503. eui[3] = 0xFF;
  1504. eui[4] = 0xFE;
  1505. eui[0] ^= 2;
  1506. }
  1507. return 0;
  1508. }
  1509. static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
  1510. {
  1511. if (dev->addr_len != IEEE802154_ADDR_LEN)
  1512. return -1;
  1513. memcpy(eui, dev->dev_addr, 8);
  1514. eui[0] ^= 2;
  1515. return 0;
  1516. }
  1517. static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
  1518. {
  1519. union fwnet_hwaddr *ha;
  1520. if (dev->addr_len != FWNET_ALEN)
  1521. return -1;
  1522. ha = (union fwnet_hwaddr *)dev->dev_addr;
  1523. memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
  1524. eui[0] ^= 2;
  1525. return 0;
  1526. }
  1527. static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
  1528. {
  1529. /* XXX: inherit EUI-64 from other interface -- yoshfuji */
  1530. if (dev->addr_len != ARCNET_ALEN)
  1531. return -1;
  1532. memset(eui, 0, 7);
  1533. eui[7] = *(u8 *)dev->dev_addr;
  1534. return 0;
  1535. }
  1536. static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
  1537. {
  1538. if (dev->addr_len != INFINIBAND_ALEN)
  1539. return -1;
  1540. memcpy(eui, dev->dev_addr + 12, 8);
  1541. eui[0] |= 2;
  1542. return 0;
  1543. }
  1544. static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
  1545. {
  1546. if (addr == 0)
  1547. return -1;
  1548. eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
  1549. ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
  1550. ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
  1551. ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
  1552. ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
  1553. ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
  1554. eui[1] = 0;
  1555. eui[2] = 0x5E;
  1556. eui[3] = 0xFE;
  1557. memcpy(eui + 4, &addr, 4);
  1558. return 0;
  1559. }
  1560. static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
  1561. {
  1562. if (dev->priv_flags & IFF_ISATAP)
  1563. return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
  1564. return -1;
  1565. }
  1566. static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
  1567. {
  1568. return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
  1569. }
  1570. static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
  1571. {
  1572. memcpy(eui, dev->perm_addr, 3);
  1573. memcpy(eui + 5, dev->perm_addr + 3, 3);
  1574. eui[3] = 0xFF;
  1575. eui[4] = 0xFE;
  1576. eui[0] ^= 2;
  1577. return 0;
  1578. }
  1579. static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
  1580. {
  1581. switch (dev->type) {
  1582. case ARPHRD_ETHER:
  1583. case ARPHRD_FDDI:
  1584. return addrconf_ifid_eui48(eui, dev);
  1585. case ARPHRD_ARCNET:
  1586. return addrconf_ifid_arcnet(eui, dev);
  1587. case ARPHRD_INFINIBAND:
  1588. return addrconf_ifid_infiniband(eui, dev);
  1589. case ARPHRD_SIT:
  1590. return addrconf_ifid_sit(eui, dev);
  1591. case ARPHRD_IPGRE:
  1592. return addrconf_ifid_gre(eui, dev);
  1593. case ARPHRD_IEEE802154:
  1594. return addrconf_ifid_eui64(eui, dev);
  1595. case ARPHRD_IEEE1394:
  1596. return addrconf_ifid_ieee1394(eui, dev);
  1597. case ARPHRD_TUNNEL6:
  1598. return addrconf_ifid_ip6tnl(eui, dev);
  1599. }
  1600. return -1;
  1601. }
  1602. static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
  1603. {
  1604. int err = -1;
  1605. struct inet6_ifaddr *ifp;
  1606. read_lock_bh(&idev->lock);
  1607. list_for_each_entry(ifp, &idev->addr_list, if_list) {
  1608. if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
  1609. memcpy(eui, ifp->addr.s6_addr+8, 8);
  1610. err = 0;
  1611. break;
  1612. }
  1613. }
  1614. read_unlock_bh(&idev->lock);
  1615. return err;
  1616. }
  1617. #ifdef CONFIG_IPV6_PRIVACY
  1618. /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
  1619. static void __ipv6_regen_rndid(struct inet6_dev *idev)
  1620. {
  1621. regen:
  1622. get_random_bytes(idev->rndid, sizeof(idev->rndid));
  1623. idev->rndid[0] &= ~0x02;
  1624. /*
  1625. * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
  1626. * check if generated address is not inappropriate
  1627. *
  1628. * - Reserved subnet anycast (RFC 2526)
  1629. * 11111101 11....11 1xxxxxxx
  1630. * - ISATAP (RFC4214) 6.1
  1631. * 00-00-5E-FE-xx-xx-xx-xx
  1632. * - value 0
  1633. * - XXX: already assigned to an address on the device
  1634. */
  1635. if (idev->rndid[0] == 0xfd &&
  1636. (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
  1637. (idev->rndid[7]&0x80))
  1638. goto regen;
  1639. if ((idev->rndid[0]|idev->rndid[1]) == 0) {
  1640. if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
  1641. goto regen;
  1642. if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
  1643. goto regen;
  1644. }
  1645. }
  1646. static void ipv6_regen_rndid(unsigned long data)
  1647. {
  1648. struct inet6_dev *idev = (struct inet6_dev *) data;
  1649. unsigned long expires;
  1650. rcu_read_lock_bh();
  1651. write_lock_bh(&idev->lock);
  1652. if (idev->dead)
  1653. goto out;
  1654. __ipv6_regen_rndid(idev);
  1655. expires = jiffies +
  1656. idev->cnf.temp_prefered_lft * HZ -
  1657. idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time -
  1658. idev->cnf.max_desync_factor * HZ;
  1659. if (time_before(expires, jiffies)) {
  1660. pr_warn("%s: too short regeneration interval; timer disabled for %s\n",
  1661. __func__, idev->dev->name);
  1662. goto out;
  1663. }
  1664. if (!mod_timer(&idev->regen_timer, expires))
  1665. in6_dev_hold(idev);
  1666. out:
  1667. write_unlock_bh(&idev->lock);
  1668. rcu_read_unlock_bh();
  1669. in6_dev_put(idev);
  1670. }
  1671. static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
  1672. {
  1673. if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
  1674. __ipv6_regen_rndid(idev);
  1675. }
  1676. #endif
  1677. /*
  1678. * Add prefix route.
  1679. */
  1680. static void
  1681. addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
  1682. unsigned long expires, u32 flags)
  1683. {
  1684. struct fib6_config cfg = {
  1685. .fc_table = RT6_TABLE_PREFIX,
  1686. .fc_metric = IP6_RT_PRIO_ADDRCONF,
  1687. .fc_ifindex = dev->ifindex,
  1688. .fc_expires = expires,
  1689. .fc_dst_len = plen,
  1690. .fc_flags = RTF_UP | flags,
  1691. .fc_nlinfo.nl_net = dev_net(dev),
  1692. .fc_protocol = RTPROT_KERNEL,
  1693. };
  1694. cfg.fc_dst = *pfx;
  1695. /* Prevent useless cloning on PtP SIT.
  1696. This thing is done here expecting that the whole
  1697. class of non-broadcast devices need not cloning.
  1698. */
  1699. #if IS_ENABLED(CONFIG_IPV6_SIT)
  1700. if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
  1701. cfg.fc_flags |= RTF_NONEXTHOP;
  1702. #endif
  1703. ip6_route_add(&cfg);
  1704. }
  1705. static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
  1706. int plen,
  1707. const struct net_device *dev,
  1708. u32 flags, u32 noflags)
  1709. {
  1710. struct fib6_node *fn;
  1711. struct rt6_info *rt = NULL;
  1712. struct fib6_table *table;
  1713. table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX);
  1714. if (table == NULL)
  1715. return NULL;
  1716. read_lock_bh(&table->tb6_lock);
  1717. fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
  1718. if (!fn)
  1719. goto out;
  1720. for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
  1721. if (rt->dst.dev->ifindex != dev->ifindex)
  1722. continue;
  1723. if ((rt->rt6i_flags & flags) != flags)
  1724. continue;
  1725. if ((rt->rt6i_flags & noflags) != 0)
  1726. continue;
  1727. dst_hold(&rt->dst);
  1728. break;
  1729. }
  1730. out:
  1731. read_unlock_bh(&table->tb6_lock);
  1732. return rt;
  1733. }
  1734. /* Create "default" multicast route to the interface */
  1735. static void addrconf_add_mroute(struct net_device *dev)
  1736. {
  1737. struct fib6_config cfg = {
  1738. .fc_table = RT6_TABLE_LOCAL,
  1739. .fc_metric = IP6_RT_PRIO_ADDRCONF,
  1740. .fc_ifindex = dev->ifindex,
  1741. .fc_dst_len = 8,
  1742. .fc_flags = RTF_UP,
  1743. .fc_nlinfo.nl_net = dev_net(dev),
  1744. };
  1745. ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
  1746. ip6_route_add(&cfg);
  1747. }
  1748. #if IS_ENABLED(CONFIG_IPV6_SIT)
  1749. static void sit_route_add(struct net_device *dev)
  1750. {
  1751. struct fib6_config cfg = {
  1752. .fc_table = RT6_TABLE_MAIN,
  1753. .fc_metric = IP6_RT_PRIO_ADDRCONF,
  1754. .fc_ifindex = dev->ifindex,
  1755. .fc_dst_len = 96,
  1756. .fc_flags = RTF_UP | RTF_NONEXTHOP,
  1757. .fc_nlinfo.nl_net = dev_net(dev),
  1758. };
  1759. /* prefix length - 96 bits "::d.d.d.d" */
  1760. ip6_route_add(&cfg);
  1761. }
  1762. #endif
  1763. static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
  1764. {
  1765. struct inet6_dev *idev;
  1766. ASSERT_RTNL();
  1767. idev = ipv6_find_idev(dev);
  1768. if (!idev)
  1769. return ERR_PTR(-ENOBUFS);
  1770. if (idev->cnf.disable_ipv6)
  1771. return ERR_PTR(-EACCES);
  1772. /* Add default multicast route */
  1773. if (!(dev->flags & IFF_LOOPBACK))
  1774. addrconf_add_mroute(dev);
  1775. return idev;
  1776. }
  1777. void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
  1778. {
  1779. struct prefix_info *pinfo;
  1780. __u32 valid_lft;
  1781. __u32 prefered_lft;
  1782. int addr_type;
  1783. struct inet6_dev *in6_dev;
  1784. struct net *net = dev_net(dev);
  1785. pinfo = (struct prefix_info *) opt;
  1786. if (len < sizeof(struct prefix_info)) {
  1787. ADBG("addrconf: prefix option too short\n");
  1788. return;
  1789. }
  1790. /*
  1791. * Validation checks ([ADDRCONF], page 19)
  1792. */
  1793. addr_type = ipv6_addr_type(&pinfo->prefix);
  1794. if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
  1795. return;
  1796. valid_lft = ntohl(pinfo->valid);
  1797. prefered_lft = ntohl(pinfo->prefered);
  1798. if (prefered_lft > valid_lft) {
  1799. net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
  1800. return;
  1801. }
  1802. in6_dev = in6_dev_get(dev);
  1803. if (in6_dev == NULL) {
  1804. net_dbg_ratelimited("addrconf: device %s not configured\n",
  1805. dev->name);
  1806. return;
  1807. }
  1808. /*
  1809. * Two things going on here:
  1810. * 1) Add routes for on-link prefixes
  1811. * 2) Configure prefixes with the auto flag set
  1812. */
  1813. if (pinfo->onlink) {
  1814. struct rt6_info *rt;
  1815. unsigned long rt_expires;
  1816. /* Avoid arithmetic overflow. Really, we could
  1817. * save rt_expires in seconds, likely valid_lft,
  1818. * but it would require division in fib gc, that it
  1819. * not good.
  1820. */
  1821. if (HZ > USER_HZ)
  1822. rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
  1823. else
  1824. rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
  1825. if (addrconf_finite_timeout(rt_expires))
  1826. rt_expires *= HZ;
  1827. rt = addrconf_get_prefix_route(&pinfo->prefix,
  1828. pinfo->prefix_len,
  1829. dev,
  1830. RTF_ADDRCONF | RTF_PREFIX_RT,
  1831. RTF_GATEWAY | RTF_DEFAULT);
  1832. if (rt) {
  1833. /* Autoconf prefix route */
  1834. if (valid_lft == 0) {
  1835. ip6_del_rt(rt);
  1836. rt = NULL;
  1837. } else if (addrconf_finite_timeout(rt_expires)) {
  1838. /* not infinity */
  1839. rt6_set_expires(rt, jiffies + rt_expires);
  1840. } else {
  1841. rt6_clean_expires(rt);
  1842. }
  1843. } else if (valid_lft) {
  1844. clock_t expires = 0;
  1845. int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
  1846. if (addrconf_finite_timeout(rt_expires)) {
  1847. /* not infinity */
  1848. flags |= RTF_EXPIRES;
  1849. expires = jiffies_to_clock_t(rt_expires);
  1850. }
  1851. addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
  1852. dev, expires, flags);
  1853. }
  1854. ip6_rt_put(rt);
  1855. }
  1856. /* Try to figure out our local address for this prefix */
  1857. if (pinfo->autoconf && in6_dev->cnf.autoconf) {
  1858. struct inet6_ifaddr *ifp;
  1859. struct in6_addr addr;
  1860. int create = 0, update_lft = 0;
  1861. bool tokenized = false;
  1862. if (pinfo->prefix_len == 64) {
  1863. memcpy(&addr, &pinfo->prefix, 8);
  1864. if (!ipv6_addr_any(&in6_dev->token)) {
  1865. read_lock_bh(&in6_dev->lock);
  1866. memcpy(addr.s6_addr + 8,
  1867. in6_dev->token.s6_addr + 8, 8);
  1868. read_unlock_bh(&in6_dev->lock);
  1869. tokenized = true;
  1870. } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
  1871. ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
  1872. in6_dev_put(in6_dev);
  1873. return;
  1874. }
  1875. goto ok;
  1876. }
  1877. net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
  1878. pinfo->prefix_len);
  1879. in6_dev_put(in6_dev);
  1880. return;
  1881. ok:
  1882. ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
  1883. if (ifp == NULL && valid_lft) {
  1884. int max_addresses = in6_dev->cnf.max_addresses;
  1885. u32 addr_flags = 0;
  1886. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  1887. if (in6_dev->cnf.optimistic_dad &&
  1888. !net->ipv6.devconf_all->forwarding && sllao)
  1889. addr_flags = IFA_F_OPTIMISTIC;
  1890. #endif
  1891. /* Do not allow to create too much of autoconfigured
  1892. * addresses; this would be too easy way to crash kernel.
  1893. */
  1894. if (!max_addresses ||
  1895. ipv6_count_addresses(in6_dev) < max_addresses)
  1896. ifp = ipv6_add_addr(in6_dev, &addr, NULL,
  1897. pinfo->prefix_len,
  1898. addr_type&IPV6_ADDR_SCOPE_MASK,
  1899. addr_flags, valid_lft,
  1900. prefered_lft);
  1901. if (IS_ERR_OR_NULL(ifp)) {
  1902. in6_dev_put(in6_dev);
  1903. return;
  1904. }
  1905. update_lft = 0;
  1906. create = 1;
  1907. ifp->cstamp = jiffies;
  1908. ifp->tokenized = tokenized;
  1909. addrconf_dad_start(ifp);
  1910. }
  1911. if (ifp) {
  1912. int flags;
  1913. unsigned long now;
  1914. #ifdef CONFIG_IPV6_PRIVACY
  1915. struct inet6_ifaddr *ift;
  1916. #endif
  1917. u32 stored_lft;
  1918. /* update lifetime (RFC2462 5.5.3 e) */
  1919. spin_lock(&ifp->lock);
  1920. now = jiffies;
  1921. if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
  1922. stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
  1923. else
  1924. stored_lft = 0;
  1925. if (!update_lft && !create && stored_lft) {
  1926. const u32 minimum_lft = min(
  1927. stored_lft, (u32)MIN_VALID_LIFETIME);
  1928. valid_lft = max(valid_lft, minimum_lft);
  1929. /* RFC4862 Section 5.5.3e:
  1930. * "Note that the preferred lifetime of the
  1931. * corresponding address is always reset to
  1932. * the Preferred Lifetime in the received
  1933. * Prefix Information option, regardless of
  1934. * whether the valid lifetime is also reset or
  1935. * ignored."
  1936. *
  1937. * So we should always update prefered_lft here.
  1938. */
  1939. update_lft = 1;
  1940. }
  1941. if (update_lft) {
  1942. ifp->valid_lft = valid_lft;
  1943. ifp->prefered_lft = prefered_lft;
  1944. ifp->tstamp = now;
  1945. flags = ifp->flags;
  1946. ifp->flags &= ~IFA_F_DEPRECATED;
  1947. spin_unlock(&ifp->lock);
  1948. if (!(flags&IFA_F_TENTATIVE))
  1949. ipv6_ifa_notify(0, ifp);
  1950. } else
  1951. spin_unlock(&ifp->lock);
  1952. #ifdef CONFIG_IPV6_PRIVACY
  1953. read_lock_bh(&in6_dev->lock);
  1954. /* update all temporary addresses in the list */
  1955. list_for_each_entry(ift, &in6_dev->tempaddr_list,
  1956. tmp_list) {
  1957. int age, max_valid, max_prefered;
  1958. if (ifp != ift->ifpub)
  1959. continue;
  1960. /*
  1961. * RFC 4941 section 3.3:
  1962. * If a received option will extend the lifetime
  1963. * of a public address, the lifetimes of
  1964. * temporary addresses should be extended,
  1965. * subject to the overall constraint that no
  1966. * temporary addresses should ever remain
  1967. * "valid" or "preferred" for a time longer than
  1968. * (TEMP_VALID_LIFETIME) or
  1969. * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR),
  1970. * respectively.
  1971. */
  1972. age = (now - ift->cstamp) / HZ;
  1973. max_valid = in6_dev->cnf.temp_valid_lft - age;
  1974. if (max_valid < 0)
  1975. max_valid = 0;
  1976. max_prefered = in6_dev->cnf.temp_prefered_lft -
  1977. in6_dev->cnf.max_desync_factor -
  1978. age;
  1979. if (max_prefered < 0)
  1980. max_prefered = 0;
  1981. if (valid_lft > max_valid)
  1982. valid_lft = max_valid;
  1983. if (prefered_lft > max_prefered)
  1984. prefered_lft = max_prefered;
  1985. spin_lock(&ift->lock);
  1986. flags = ift->flags;
  1987. ift->valid_lft = valid_lft;
  1988. ift->prefered_lft = prefered_lft;
  1989. ift->tstamp = now;
  1990. if (prefered_lft > 0)
  1991. ift->flags &= ~IFA_F_DEPRECATED;
  1992. spin_unlock(&ift->lock);
  1993. if (!(flags&IFA_F_TENTATIVE))
  1994. ipv6_ifa_notify(0, ift);
  1995. }
  1996. if ((create || list_empty(&in6_dev->tempaddr_list)) && in6_dev->cnf.use_tempaddr > 0) {
  1997. /*
  1998. * When a new public address is created as
  1999. * described in [ADDRCONF], also create a new
  2000. * temporary address. Also create a temporary
  2001. * address if it's enabled but no temporary
  2002. * address currently exists.
  2003. */
  2004. read_unlock_bh(&in6_dev->lock);
  2005. ipv6_create_tempaddr(ifp, NULL);
  2006. } else {
  2007. read_unlock_bh(&in6_dev->lock);
  2008. }
  2009. #endif
  2010. in6_ifa_put(ifp);
  2011. addrconf_verify(0);
  2012. }
  2013. }
  2014. inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
  2015. in6_dev_put(in6_dev);
  2016. }
  2017. /*
  2018. * Set destination address.
  2019. * Special case for SIT interfaces where we create a new "virtual"
  2020. * device.
  2021. */
  2022. int addrconf_set_dstaddr(struct net *net, void __user *arg)
  2023. {
  2024. struct in6_ifreq ireq;
  2025. struct net_device *dev;
  2026. int err = -EINVAL;
  2027. rtnl_lock();
  2028. err = -EFAULT;
  2029. if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
  2030. goto err_exit;
  2031. dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
  2032. err = -ENODEV;
  2033. if (dev == NULL)
  2034. goto err_exit;
  2035. #if IS_ENABLED(CONFIG_IPV6_SIT)
  2036. if (dev->type == ARPHRD_SIT) {
  2037. const struct net_device_ops *ops = dev->netdev_ops;
  2038. struct ifreq ifr;
  2039. struct ip_tunnel_parm p;
  2040. err = -EADDRNOTAVAIL;
  2041. if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
  2042. goto err_exit;
  2043. memset(&p, 0, sizeof(p));
  2044. p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
  2045. p.iph.saddr = 0;
  2046. p.iph.version = 4;
  2047. p.iph.ihl = 5;
  2048. p.iph.protocol = IPPROTO_IPV6;
  2049. p.iph.ttl = 64;
  2050. ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
  2051. if (ops->ndo_do_ioctl) {
  2052. mm_segment_t oldfs = get_fs();
  2053. set_fs(KERNEL_DS);
  2054. err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
  2055. set_fs(oldfs);
  2056. } else
  2057. err = -EOPNOTSUPP;
  2058. if (err == 0) {
  2059. err = -ENOBUFS;
  2060. dev = __dev_get_by_name(net, p.name);
  2061. if (!dev)
  2062. goto err_exit;
  2063. err = dev_open(dev);
  2064. }
  2065. }
  2066. #endif
  2067. err_exit:
  2068. rtnl_unlock();
  2069. return err;
  2070. }
  2071. /*
  2072. * Manual configuration of address on an interface
  2073. */
  2074. static int inet6_addr_add(struct net *net, int ifindex, const struct in6_addr *pfx,
  2075. const struct in6_addr *peer_pfx,
  2076. unsigned int plen, __u8 ifa_flags, __u32 prefered_lft,
  2077. __u32 valid_lft)
  2078. {
  2079. struct inet6_ifaddr *ifp;
  2080. struct inet6_dev *idev;
  2081. struct net_device *dev;
  2082. int scope;
  2083. u32 flags;
  2084. clock_t expires;
  2085. unsigned long timeout;
  2086. ASSERT_RTNL();
  2087. if (plen > 128)
  2088. return -EINVAL;
  2089. /* check the lifetime */
  2090. if (!valid_lft || prefered_lft > valid_lft)
  2091. return -EINVAL;
  2092. dev = __dev_get_by_index(net, ifindex);
  2093. if (!dev)
  2094. return -ENODEV;
  2095. idev = addrconf_add_dev(dev);
  2096. if (IS_ERR(idev))
  2097. return PTR_ERR(idev);
  2098. scope = ipv6_addr_scope(pfx);
  2099. timeout = addrconf_timeout_fixup(valid_lft, HZ);
  2100. if (addrconf_finite_timeout(timeout)) {
  2101. expires = jiffies_to_clock_t(timeout * HZ);
  2102. valid_lft = timeout;
  2103. flags = RTF_EXPIRES;
  2104. } else {
  2105. expires = 0;
  2106. flags = 0;
  2107. ifa_flags |= IFA_F_PERMANENT;
  2108. }
  2109. timeout = addrconf_timeout_fixup(prefered_lft, HZ);
  2110. if (addrconf_finite_timeout(timeout)) {
  2111. if (timeout == 0)
  2112. ifa_flags |= IFA_F_DEPRECATED;
  2113. prefered_lft = timeout;
  2114. }
  2115. ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
  2116. valid_lft, prefered_lft);
  2117. if (!IS_ERR(ifp)) {
  2118. addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
  2119. expires, flags);
  2120. /*
  2121. * Note that section 3.1 of RFC 4429 indicates
  2122. * that the Optimistic flag should not be set for
  2123. * manually configured addresses
  2124. */
  2125. addrconf_dad_start(ifp);
  2126. in6_ifa_put(ifp);
  2127. addrconf_verify(0);
  2128. return 0;
  2129. }
  2130. return PTR_ERR(ifp);
  2131. }
  2132. static int inet6_addr_del(struct net *net, int ifindex, const struct in6_addr *pfx,
  2133. unsigned int plen)
  2134. {
  2135. struct inet6_ifaddr *ifp;
  2136. struct inet6_dev *idev;
  2137. struct net_device *dev;
  2138. if (plen > 128)
  2139. return -EINVAL;
  2140. dev = __dev_get_by_index(net, ifindex);
  2141. if (!dev)
  2142. return -ENODEV;
  2143. if ((idev = __in6_dev_get(dev)) == NULL)
  2144. return -ENXIO;
  2145. read_lock_bh(&idev->lock);
  2146. list_for_each_entry(ifp, &idev->addr_list, if_list) {
  2147. if (ifp->prefix_len == plen &&
  2148. ipv6_addr_equal(pfx, &ifp->addr)) {
  2149. in6_ifa_hold(ifp);
  2150. read_unlock_bh(&idev->lock);
  2151. ipv6_del_addr(ifp);
  2152. return 0;
  2153. }
  2154. }
  2155. read_unlock_bh(&idev->lock);
  2156. return -EADDRNOTAVAIL;
  2157. }
  2158. int addrconf_add_ifaddr(struct net *net, void __user *arg)
  2159. {
  2160. struct in6_ifreq ireq;
  2161. int err;
  2162. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  2163. return -EPERM;
  2164. if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
  2165. return -EFAULT;
  2166. rtnl_lock();
  2167. err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
  2168. ireq.ifr6_prefixlen, IFA_F_PERMANENT,
  2169. INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
  2170. rtnl_unlock();
  2171. return err;
  2172. }
  2173. int addrconf_del_ifaddr(struct net *net, void __user *arg)
  2174. {
  2175. struct in6_ifreq ireq;
  2176. int err;
  2177. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  2178. return -EPERM;
  2179. if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
  2180. return -EFAULT;
  2181. rtnl_lock();
  2182. err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
  2183. ireq.ifr6_prefixlen);
  2184. rtnl_unlock();
  2185. return err;
  2186. }
  2187. static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
  2188. int plen, int scope)
  2189. {
  2190. struct inet6_ifaddr *ifp;
  2191. ifp = ipv6_add_addr(idev, addr, NULL, plen,
  2192. scope, IFA_F_PERMANENT, 0, 0);
  2193. if (!IS_ERR(ifp)) {
  2194. spin_lock_bh(&ifp->lock);
  2195. ifp->flags &= ~IFA_F_TENTATIVE;
  2196. spin_unlock_bh(&ifp->lock);
  2197. ipv6_ifa_notify(RTM_NEWADDR, ifp);
  2198. in6_ifa_put(ifp);
  2199. }
  2200. }
  2201. #if IS_ENABLED(CONFIG_IPV6_SIT)
  2202. static void sit_add_v4_addrs(struct inet6_dev *idev)
  2203. {
  2204. struct in6_addr addr;
  2205. struct net_device *dev;
  2206. struct net *net = dev_net(idev->dev);
  2207. int scope;
  2208. ASSERT_RTNL();
  2209. memset(&addr, 0, sizeof(struct in6_addr));
  2210. memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
  2211. if (idev->dev->flags&IFF_POINTOPOINT) {
  2212. addr.s6_addr32[0] = htonl(0xfe800000);
  2213. scope = IFA_LINK;
  2214. } else {
  2215. scope = IPV6_ADDR_COMPATv4;
  2216. }
  2217. if (addr.s6_addr32[3]) {
  2218. add_addr(idev, &addr, 128, scope);
  2219. return;
  2220. }
  2221. for_each_netdev(net, dev) {
  2222. struct in_device *in_dev = __in_dev_get_rtnl(dev);
  2223. if (in_dev && (dev->flags & IFF_UP)) {
  2224. struct in_ifaddr *ifa;
  2225. int flag = scope;
  2226. for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
  2227. int plen;
  2228. addr.s6_addr32[3] = ifa->ifa_local;
  2229. if (ifa->ifa_scope == RT_SCOPE_LINK)
  2230. continue;
  2231. if (ifa->ifa_scope >= RT_SCOPE_HOST) {
  2232. if (idev->dev->flags&IFF_POINTOPOINT)
  2233. continue;
  2234. flag |= IFA_HOST;
  2235. }
  2236. if (idev->dev->flags&IFF_POINTOPOINT)
  2237. plen = 64;
  2238. else
  2239. plen = 96;
  2240. add_addr(idev, &addr, plen, flag);
  2241. }
  2242. }
  2243. }
  2244. }
  2245. #endif
  2246. static void init_loopback(struct net_device *dev)
  2247. {
  2248. struct inet6_dev *idev;
  2249. struct net_device *sp_dev;
  2250. struct inet6_ifaddr *sp_ifa;
  2251. struct rt6_info *sp_rt;
  2252. /* ::1 */
  2253. ASSERT_RTNL();
  2254. if ((idev = ipv6_find_idev(dev)) == NULL) {
  2255. pr_debug("%s: add_dev failed\n", __func__);
  2256. return;
  2257. }
  2258. add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
  2259. /* Add routes to other interface's IPv6 addresses */
  2260. for_each_netdev(dev_net(dev), sp_dev) {
  2261. if (!strcmp(sp_dev->name, dev->name))
  2262. continue;
  2263. idev = __in6_dev_get(sp_dev);
  2264. if (!idev)
  2265. continue;
  2266. read_lock_bh(&idev->lock);
  2267. list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
  2268. if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
  2269. continue;
  2270. if (sp_ifa->rt)
  2271. continue;
  2272. sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, 0);
  2273. /* Failure cases are ignored */
  2274. if (!IS_ERR(sp_rt)) {
  2275. sp_ifa->rt = sp_rt;
  2276. ip6_ins_rt(sp_rt);
  2277. }
  2278. }
  2279. read_unlock_bh(&idev->lock);
  2280. }
  2281. }
  2282. static void addrconf_add_linklocal(struct inet6_dev *idev, const struct in6_addr *addr)
  2283. {
  2284. struct inet6_ifaddr *ifp;
  2285. u32 addr_flags = IFA_F_PERMANENT;
  2286. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  2287. if (idev->cnf.optimistic_dad &&
  2288. !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
  2289. addr_flags |= IFA_F_OPTIMISTIC;
  2290. #endif
  2291. ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags, 0, 0);
  2292. if (!IS_ERR(ifp)) {
  2293. addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
  2294. addrconf_dad_start(ifp);
  2295. in6_ifa_put(ifp);
  2296. }
  2297. }
  2298. static void addrconf_dev_config(struct net_device *dev)
  2299. {
  2300. struct in6_addr addr;
  2301. struct inet6_dev *idev;
  2302. ASSERT_RTNL();
  2303. if ((dev->type != ARPHRD_ETHER) &&
  2304. (dev->type != ARPHRD_FDDI) &&
  2305. (dev->type != ARPHRD_ARCNET) &&
  2306. (dev->type != ARPHRD_INFINIBAND) &&
  2307. (dev->type != ARPHRD_IEEE802154) &&
  2308. (dev->type != ARPHRD_IEEE1394) &&
  2309. (dev->type != ARPHRD_TUNNEL6)) {
  2310. /* Alas, we support only Ethernet autoconfiguration. */
  2311. return;
  2312. }
  2313. idev = addrconf_add_dev(dev);
  2314. if (IS_ERR(idev))
  2315. return;
  2316. memset(&addr, 0, sizeof(struct in6_addr));
  2317. addr.s6_addr32[0] = htonl(0xFE800000);
  2318. if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
  2319. addrconf_add_linklocal(idev, &addr);
  2320. }
  2321. #if IS_ENABLED(CONFIG_IPV6_SIT)
  2322. static void addrconf_sit_config(struct net_device *dev)
  2323. {
  2324. struct inet6_dev *idev;
  2325. ASSERT_RTNL();
  2326. /*
  2327. * Configure the tunnel with one of our IPv4
  2328. * addresses... we should configure all of
  2329. * our v4 addrs in the tunnel
  2330. */
  2331. if ((idev = ipv6_find_idev(dev)) == NULL) {
  2332. pr_debug("%s: add_dev failed\n", __func__);
  2333. return;
  2334. }
  2335. if (dev->priv_flags & IFF_ISATAP) {
  2336. struct in6_addr addr;
  2337. ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
  2338. addrconf_prefix_route(&addr, 64, dev, 0, 0);
  2339. if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
  2340. addrconf_add_linklocal(idev, &addr);
  2341. return;
  2342. }
  2343. sit_add_v4_addrs(idev);
  2344. if (dev->flags&IFF_POINTOPOINT)
  2345. addrconf_add_mroute(dev);
  2346. else
  2347. sit_route_add(dev);
  2348. }
  2349. #endif
  2350. #if IS_ENABLED(CONFIG_NET_IPGRE)
  2351. static void addrconf_gre_config(struct net_device *dev)
  2352. {
  2353. struct inet6_dev *idev;
  2354. struct in6_addr addr;
  2355. ASSERT_RTNL();
  2356. if ((idev = ipv6_find_idev(dev)) == NULL) {
  2357. pr_debug("%s: add_dev failed\n", __func__);
  2358. return;
  2359. }
  2360. ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
  2361. addrconf_prefix_route(&addr, 64, dev, 0, 0);
  2362. if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
  2363. addrconf_add_linklocal(idev, &addr);
  2364. }
  2365. #endif
  2366. static inline int
  2367. ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
  2368. {
  2369. struct in6_addr lladdr;
  2370. if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
  2371. addrconf_add_linklocal(idev, &lladdr);
  2372. return 0;
  2373. }
  2374. return -1;
  2375. }
  2376. static int addrconf_notify(struct notifier_block *this, unsigned long event,
  2377. void *ptr)
  2378. {
  2379. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  2380. struct inet6_dev *idev = __in6_dev_get(dev);
  2381. int run_pending = 0;
  2382. int err;
  2383. switch (event) {
  2384. case NETDEV_REGISTER:
  2385. if (!idev && dev->mtu >= IPV6_MIN_MTU) {
  2386. idev = ipv6_add_dev(dev);
  2387. if (!idev)
  2388. return notifier_from_errno(-ENOMEM);
  2389. }
  2390. break;
  2391. case NETDEV_UP:
  2392. case NETDEV_CHANGE:
  2393. if (dev->flags & IFF_SLAVE)
  2394. break;
  2395. if (event == NETDEV_UP) {
  2396. if (!addrconf_qdisc_ok(dev)) {
  2397. /* device is not ready yet. */
  2398. pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
  2399. dev->name);
  2400. break;
  2401. }
  2402. if (!idev && dev->mtu >= IPV6_MIN_MTU)
  2403. idev = ipv6_add_dev(dev);
  2404. if (idev) {
  2405. idev->if_flags |= IF_READY;
  2406. run_pending = 1;
  2407. }
  2408. } else {
  2409. if (!addrconf_qdisc_ok(dev)) {
  2410. /* device is still not ready. */
  2411. break;
  2412. }
  2413. if (idev) {
  2414. if (idev->if_flags & IF_READY)
  2415. /* device is already configured. */
  2416. break;
  2417. idev->if_flags |= IF_READY;
  2418. }
  2419. pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
  2420. dev->name);
  2421. run_pending = 1;
  2422. }
  2423. switch (dev->type) {
  2424. #if IS_ENABLED(CONFIG_IPV6_SIT)
  2425. case ARPHRD_SIT:
  2426. addrconf_sit_config(dev);
  2427. break;
  2428. #endif
  2429. #if IS_ENABLED(CONFIG_NET_IPGRE)
  2430. case ARPHRD_IPGRE:
  2431. addrconf_gre_config(dev);
  2432. break;
  2433. #endif
  2434. case ARPHRD_LOOPBACK:
  2435. init_loopback(dev);
  2436. break;
  2437. default:
  2438. addrconf_dev_config(dev);
  2439. break;
  2440. }
  2441. if (idev) {
  2442. if (run_pending)
  2443. addrconf_dad_run(idev);
  2444. /*
  2445. * If the MTU changed during the interface down,
  2446. * when the interface up, the changed MTU must be
  2447. * reflected in the idev as well as routers.
  2448. */
  2449. if (idev->cnf.mtu6 != dev->mtu &&
  2450. dev->mtu >= IPV6_MIN_MTU) {
  2451. rt6_mtu_change(dev, dev->mtu);
  2452. idev->cnf.mtu6 = dev->mtu;
  2453. }
  2454. idev->tstamp = jiffies;
  2455. inet6_ifinfo_notify(RTM_NEWLINK, idev);
  2456. /*
  2457. * If the changed mtu during down is lower than
  2458. * IPV6_MIN_MTU stop IPv6 on this interface.
  2459. */
  2460. if (dev->mtu < IPV6_MIN_MTU)
  2461. addrconf_ifdown(dev, 1);
  2462. }
  2463. break;
  2464. case NETDEV_CHANGEMTU:
  2465. if (idev && dev->mtu >= IPV6_MIN_MTU) {
  2466. rt6_mtu_change(dev, dev->mtu);
  2467. idev->cnf.mtu6 = dev->mtu;
  2468. break;
  2469. }
  2470. if (!idev && dev->mtu >= IPV6_MIN_MTU) {
  2471. idev = ipv6_add_dev(dev);
  2472. if (idev)
  2473. break;
  2474. }
  2475. /*
  2476. * MTU falled under IPV6_MIN_MTU.
  2477. * Stop IPv6 on this interface.
  2478. */
  2479. case NETDEV_DOWN:
  2480. case NETDEV_UNREGISTER:
  2481. /*
  2482. * Remove all addresses from this interface.
  2483. */
  2484. addrconf_ifdown(dev, event != NETDEV_DOWN);
  2485. break;
  2486. case NETDEV_CHANGENAME:
  2487. if (idev) {
  2488. snmp6_unregister_dev(idev);
  2489. addrconf_sysctl_unregister(idev);
  2490. addrconf_sysctl_register(idev);
  2491. err = snmp6_register_dev(idev);
  2492. if (err)
  2493. return notifier_from_errno(err);
  2494. }
  2495. break;
  2496. case NETDEV_PRE_TYPE_CHANGE:
  2497. case NETDEV_POST_TYPE_CHANGE:
  2498. addrconf_type_change(dev, event);
  2499. break;
  2500. }
  2501. return NOTIFY_OK;
  2502. }
  2503. /*
  2504. * addrconf module should be notified of a device going up
  2505. */
  2506. static struct notifier_block ipv6_dev_notf = {
  2507. .notifier_call = addrconf_notify,
  2508. };
  2509. static void addrconf_type_change(struct net_device *dev, unsigned long event)
  2510. {
  2511. struct inet6_dev *idev;
  2512. ASSERT_RTNL();
  2513. idev = __in6_dev_get(dev);
  2514. if (event == NETDEV_POST_TYPE_CHANGE)
  2515. ipv6_mc_remap(idev);
  2516. else if (event == NETDEV_PRE_TYPE_CHANGE)
  2517. ipv6_mc_unmap(idev);
  2518. }
  2519. static int addrconf_ifdown(struct net_device *dev, int how)
  2520. {
  2521. struct net *net = dev_net(dev);
  2522. struct inet6_dev *idev;
  2523. struct inet6_ifaddr *ifa;
  2524. int state, i;
  2525. ASSERT_RTNL();
  2526. rt6_ifdown(net, dev);
  2527. neigh_ifdown(&nd_tbl, dev);
  2528. idev = __in6_dev_get(dev);
  2529. if (idev == NULL)
  2530. return -ENODEV;
  2531. /*
  2532. * Step 1: remove reference to ipv6 device from parent device.
  2533. * Do not dev_put!
  2534. */
  2535. if (how) {
  2536. idev->dead = 1;
  2537. /* protected by rtnl_lock */
  2538. RCU_INIT_POINTER(dev->ip6_ptr, NULL);
  2539. /* Step 1.5: remove snmp6 entry */
  2540. snmp6_unregister_dev(idev);
  2541. }
  2542. /* Step 2: clear hash table */
  2543. for (i = 0; i < IN6_ADDR_HSIZE; i++) {
  2544. struct hlist_head *h = &inet6_addr_lst[i];
  2545. spin_lock_bh(&addrconf_hash_lock);
  2546. restart:
  2547. hlist_for_each_entry_rcu(ifa, h, addr_lst) {
  2548. if (ifa->idev == idev) {
  2549. hlist_del_init_rcu(&ifa->addr_lst);
  2550. addrconf_del_dad_timer(ifa);
  2551. goto restart;
  2552. }
  2553. }
  2554. spin_unlock_bh(&addrconf_hash_lock);
  2555. }
  2556. write_lock_bh(&idev->lock);
  2557. addrconf_del_rs_timer(idev);
  2558. /* Step 2: clear flags for stateless addrconf */
  2559. if (!how)
  2560. idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
  2561. #ifdef CONFIG_IPV6_PRIVACY
  2562. if (how && del_timer(&idev->regen_timer))
  2563. in6_dev_put(idev);
  2564. /* Step 3: clear tempaddr list */
  2565. while (!list_empty(&idev->tempaddr_list)) {
  2566. ifa = list_first_entry(&idev->tempaddr_list,
  2567. struct inet6_ifaddr, tmp_list);
  2568. list_del(&ifa->tmp_list);
  2569. write_unlock_bh(&idev->lock);
  2570. spin_lock_bh(&ifa->lock);
  2571. if (ifa->ifpub) {
  2572. in6_ifa_put(ifa->ifpub);
  2573. ifa->ifpub = NULL;
  2574. }
  2575. spin_unlock_bh(&ifa->lock);
  2576. in6_ifa_put(ifa);
  2577. write_lock_bh(&idev->lock);
  2578. }
  2579. #endif
  2580. while (!list_empty(&idev->addr_list)) {
  2581. ifa = list_first_entry(&idev->addr_list,
  2582. struct inet6_ifaddr, if_list);
  2583. addrconf_del_dad_timer(ifa);
  2584. list_del(&ifa->if_list);
  2585. write_unlock_bh(&idev->lock);
  2586. spin_lock_bh(&ifa->state_lock);
  2587. state = ifa->state;
  2588. ifa->state = INET6_IFADDR_STATE_DEAD;
  2589. spin_unlock_bh(&ifa->state_lock);
  2590. if (state != INET6_IFADDR_STATE_DEAD) {
  2591. __ipv6_ifa_notify(RTM_DELADDR, ifa);
  2592. inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
  2593. }
  2594. in6_ifa_put(ifa);
  2595. write_lock_bh(&idev->lock);
  2596. }
  2597. write_unlock_bh(&idev->lock);
  2598. /* Step 5: Discard multicast list */
  2599. if (how)
  2600. ipv6_mc_destroy_dev(idev);
  2601. else
  2602. ipv6_mc_down(idev);
  2603. idev->tstamp = jiffies;
  2604. /* Last: Shot the device (if unregistered) */
  2605. if (how) {
  2606. addrconf_sysctl_unregister(idev);
  2607. neigh_parms_release(&nd_tbl, idev->nd_parms);
  2608. neigh_ifdown(&nd_tbl, dev);
  2609. in6_dev_put(idev);
  2610. }
  2611. return 0;
  2612. }
  2613. static void addrconf_rs_timer(unsigned long data)
  2614. {
  2615. struct inet6_dev *idev = (struct inet6_dev *)data;
  2616. struct net_device *dev = idev->dev;
  2617. struct in6_addr lladdr;
  2618. write_lock(&idev->lock);
  2619. if (idev->dead || !(idev->if_flags & IF_READY))
  2620. goto out;
  2621. if (!ipv6_accept_ra(idev))
  2622. goto out;
  2623. /* Announcement received after solicitation was sent */
  2624. if (idev->if_flags & IF_RA_RCVD)
  2625. goto out;
  2626. if (idev->rs_probes++ < idev->cnf.rtr_solicits) {
  2627. write_unlock(&idev->lock);
  2628. if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
  2629. ndisc_send_rs(dev, &lladdr,
  2630. &in6addr_linklocal_allrouters);
  2631. else
  2632. goto put;
  2633. write_lock(&idev->lock);
  2634. /* The wait after the last probe can be shorter */
  2635. addrconf_mod_rs_timer(idev, (idev->rs_probes ==
  2636. idev->cnf.rtr_solicits) ?
  2637. idev->cnf.rtr_solicit_delay :
  2638. idev->cnf.rtr_solicit_interval);
  2639. } else {
  2640. /*
  2641. * Note: we do not support deprecated "all on-link"
  2642. * assumption any longer.
  2643. */
  2644. pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
  2645. }
  2646. out:
  2647. write_unlock(&idev->lock);
  2648. put:
  2649. in6_dev_put(idev);
  2650. }
  2651. /*
  2652. * Duplicate Address Detection
  2653. */
  2654. static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
  2655. {
  2656. unsigned long rand_num;
  2657. struct inet6_dev *idev = ifp->idev;
  2658. if (ifp->flags & IFA_F_OPTIMISTIC)
  2659. rand_num = 0;
  2660. else
  2661. rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
  2662. ifp->dad_probes = idev->cnf.dad_transmits;
  2663. addrconf_mod_dad_timer(ifp, rand_num);
  2664. }
  2665. static void addrconf_dad_start(struct inet6_ifaddr *ifp)
  2666. {
  2667. struct inet6_dev *idev = ifp->idev;
  2668. struct net_device *dev = idev->dev;
  2669. addrconf_join_solict(dev, &ifp->addr);
  2670. net_srandom(ifp->addr.s6_addr32[3]);
  2671. read_lock_bh(&idev->lock);
  2672. spin_lock(&ifp->lock);
  2673. if (ifp->state == INET6_IFADDR_STATE_DEAD)
  2674. goto out;
  2675. if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
  2676. idev->cnf.accept_dad < 1 ||
  2677. !(ifp->flags&IFA_F_TENTATIVE) ||
  2678. ifp->flags & IFA_F_NODAD) {
  2679. ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
  2680. spin_unlock(&ifp->lock);
  2681. read_unlock_bh(&idev->lock);
  2682. addrconf_dad_completed(ifp);
  2683. return;
  2684. }
  2685. if (!(idev->if_flags & IF_READY)) {
  2686. spin_unlock(&ifp->lock);
  2687. read_unlock_bh(&idev->lock);
  2688. /*
  2689. * If the device is not ready:
  2690. * - keep it tentative if it is a permanent address.
  2691. * - otherwise, kill it.
  2692. */
  2693. in6_ifa_hold(ifp);
  2694. addrconf_dad_stop(ifp, 0);
  2695. return;
  2696. }
  2697. /*
  2698. * Optimistic nodes can start receiving
  2699. * Frames right away
  2700. */
  2701. if (ifp->flags & IFA_F_OPTIMISTIC)
  2702. ip6_ins_rt(ifp->rt);
  2703. addrconf_dad_kick(ifp);
  2704. out:
  2705. spin_unlock(&ifp->lock);
  2706. read_unlock_bh(&idev->lock);
  2707. }
  2708. static void addrconf_dad_timer(unsigned long data)
  2709. {
  2710. struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
  2711. struct inet6_dev *idev = ifp->idev;
  2712. struct in6_addr mcaddr;
  2713. if (!ifp->dad_probes && addrconf_dad_end(ifp))
  2714. goto out;
  2715. write_lock(&idev->lock);
  2716. if (idev->dead || !(idev->if_flags & IF_READY)) {
  2717. write_unlock(&idev->lock);
  2718. goto out;
  2719. }
  2720. spin_lock(&ifp->lock);
  2721. if (ifp->state == INET6_IFADDR_STATE_DEAD) {
  2722. spin_unlock(&ifp->lock);
  2723. write_unlock(&idev->lock);
  2724. goto out;
  2725. }
  2726. if (ifp->dad_probes == 0) {
  2727. /*
  2728. * DAD was successful
  2729. */
  2730. ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
  2731. spin_unlock(&ifp->lock);
  2732. write_unlock(&idev->lock);
  2733. addrconf_dad_completed(ifp);
  2734. goto out;
  2735. }
  2736. ifp->dad_probes--;
  2737. addrconf_mod_dad_timer(ifp, ifp->idev->nd_parms->retrans_time);
  2738. spin_unlock(&ifp->lock);
  2739. write_unlock(&idev->lock);
  2740. /* send a neighbour solicitation for our addr */
  2741. addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
  2742. ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
  2743. out:
  2744. in6_ifa_put(ifp);
  2745. }
  2746. static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
  2747. {
  2748. struct net_device *dev = ifp->idev->dev;
  2749. struct in6_addr lladdr;
  2750. bool send_rs, send_mld;
  2751. addrconf_del_dad_timer(ifp);
  2752. /*
  2753. * Configure the address for reception. Now it is valid.
  2754. */
  2755. ipv6_ifa_notify(RTM_NEWADDR, ifp);
  2756. /* If added prefix is link local and we are prepared to process
  2757. router advertisements, start sending router solicitations.
  2758. */
  2759. read_lock_bh(&ifp->idev->lock);
  2760. spin_lock(&ifp->lock);
  2761. send_mld = ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL &&
  2762. ifp->idev->valid_ll_addr_cnt == 1;
  2763. send_rs = send_mld &&
  2764. ipv6_accept_ra(ifp->idev) &&
  2765. ifp->idev->cnf.rtr_solicits > 0 &&
  2766. (dev->flags&IFF_LOOPBACK) == 0;
  2767. spin_unlock(&ifp->lock);
  2768. read_unlock_bh(&ifp->idev->lock);
  2769. /* While dad is in progress mld report's source address is in6_addrany.
  2770. * Resend with proper ll now.
  2771. */
  2772. if (send_mld)
  2773. ipv6_mc_dad_complete(ifp->idev);
  2774. if (send_rs) {
  2775. /*
  2776. * If a host as already performed a random delay
  2777. * [...] as part of DAD [...] there is no need
  2778. * to delay again before sending the first RS
  2779. */
  2780. if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
  2781. return;
  2782. ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
  2783. write_lock_bh(&ifp->idev->lock);
  2784. spin_lock(&ifp->lock);
  2785. ifp->idev->rs_probes = 1;
  2786. ifp->idev->if_flags |= IF_RS_SENT;
  2787. addrconf_mod_rs_timer(ifp->idev,
  2788. ifp->idev->cnf.rtr_solicit_interval);
  2789. spin_unlock(&ifp->lock);
  2790. write_unlock_bh(&ifp->idev->lock);
  2791. }
  2792. }
  2793. static void addrconf_dad_run(struct inet6_dev *idev)
  2794. {
  2795. struct inet6_ifaddr *ifp;
  2796. read_lock_bh(&idev->lock);
  2797. list_for_each_entry(ifp, &idev->addr_list, if_list) {
  2798. spin_lock(&ifp->lock);
  2799. if (ifp->flags & IFA_F_TENTATIVE &&
  2800. ifp->state == INET6_IFADDR_STATE_DAD)
  2801. addrconf_dad_kick(ifp);
  2802. spin_unlock(&ifp->lock);
  2803. }
  2804. read_unlock_bh(&idev->lock);
  2805. }
  2806. #ifdef CONFIG_PROC_FS
  2807. struct if6_iter_state {
  2808. struct seq_net_private p;
  2809. int bucket;
  2810. int offset;
  2811. };
  2812. static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
  2813. {
  2814. struct inet6_ifaddr *ifa = NULL;
  2815. struct if6_iter_state *state = seq->private;
  2816. struct net *net = seq_file_net(seq);
  2817. int p = 0;
  2818. /* initial bucket if pos is 0 */
  2819. if (pos == 0) {
  2820. state->bucket = 0;
  2821. state->offset = 0;
  2822. }
  2823. for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
  2824. hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
  2825. addr_lst) {
  2826. if (!net_eq(dev_net(ifa->idev->dev), net))
  2827. continue;
  2828. /* sync with offset */
  2829. if (p < state->offset) {
  2830. p++;
  2831. continue;
  2832. }
  2833. state->offset++;
  2834. return ifa;
  2835. }
  2836. /* prepare for next bucket */
  2837. state->offset = 0;
  2838. p = 0;
  2839. }
  2840. return NULL;
  2841. }
  2842. static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
  2843. struct inet6_ifaddr *ifa)
  2844. {
  2845. struct if6_iter_state *state = seq->private;
  2846. struct net *net = seq_file_net(seq);
  2847. hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
  2848. if (!net_eq(dev_net(ifa->idev->dev), net))
  2849. continue;
  2850. state->offset++;
  2851. return ifa;
  2852. }
  2853. while (++state->bucket < IN6_ADDR_HSIZE) {
  2854. state->offset = 0;
  2855. hlist_for_each_entry_rcu_bh(ifa,
  2856. &inet6_addr_lst[state->bucket], addr_lst) {
  2857. if (!net_eq(dev_net(ifa->idev->dev), net))
  2858. continue;
  2859. state->offset++;
  2860. return ifa;
  2861. }
  2862. }
  2863. return NULL;
  2864. }
  2865. static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
  2866. __acquires(rcu_bh)
  2867. {
  2868. rcu_read_lock_bh();
  2869. return if6_get_first(seq, *pos);
  2870. }
  2871. static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2872. {
  2873. struct inet6_ifaddr *ifa;
  2874. ifa = if6_get_next(seq, v);
  2875. ++*pos;
  2876. return ifa;
  2877. }
  2878. static void if6_seq_stop(struct seq_file *seq, void *v)
  2879. __releases(rcu_bh)
  2880. {
  2881. rcu_read_unlock_bh();
  2882. }
  2883. static int if6_seq_show(struct seq_file *seq, void *v)
  2884. {
  2885. struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
  2886. seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
  2887. &ifp->addr,
  2888. ifp->idev->dev->ifindex,
  2889. ifp->prefix_len,
  2890. ifp->scope,
  2891. ifp->flags,
  2892. ifp->idev->dev->name);
  2893. return 0;
  2894. }
  2895. static const struct seq_operations if6_seq_ops = {
  2896. .start = if6_seq_start,
  2897. .next = if6_seq_next,
  2898. .show = if6_seq_show,
  2899. .stop = if6_seq_stop,
  2900. };
  2901. static int if6_seq_open(struct inode *inode, struct file *file)
  2902. {
  2903. return seq_open_net(inode, file, &if6_seq_ops,
  2904. sizeof(struct if6_iter_state));
  2905. }
  2906. static const struct file_operations if6_fops = {
  2907. .owner = THIS_MODULE,
  2908. .open = if6_seq_open,
  2909. .read = seq_read,
  2910. .llseek = seq_lseek,
  2911. .release = seq_release_net,
  2912. };
  2913. static int __net_init if6_proc_net_init(struct net *net)
  2914. {
  2915. if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
  2916. return -ENOMEM;
  2917. return 0;
  2918. }
  2919. static void __net_exit if6_proc_net_exit(struct net *net)
  2920. {
  2921. remove_proc_entry("if_inet6", net->proc_net);
  2922. }
  2923. static struct pernet_operations if6_proc_net_ops = {
  2924. .init = if6_proc_net_init,
  2925. .exit = if6_proc_net_exit,
  2926. };
  2927. int __init if6_proc_init(void)
  2928. {
  2929. return register_pernet_subsys(&if6_proc_net_ops);
  2930. }
  2931. void if6_proc_exit(void)
  2932. {
  2933. unregister_pernet_subsys(&if6_proc_net_ops);
  2934. }
  2935. #endif /* CONFIG_PROC_FS */
  2936. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  2937. /* Check if address is a home address configured on any interface. */
  2938. int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
  2939. {
  2940. int ret = 0;
  2941. struct inet6_ifaddr *ifp = NULL;
  2942. unsigned int hash = inet6_addr_hash(addr);
  2943. rcu_read_lock_bh();
  2944. hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
  2945. if (!net_eq(dev_net(ifp->idev->dev), net))
  2946. continue;
  2947. if (ipv6_addr_equal(&ifp->addr, addr) &&
  2948. (ifp->flags & IFA_F_HOMEADDRESS)) {
  2949. ret = 1;
  2950. break;
  2951. }
  2952. }
  2953. rcu_read_unlock_bh();
  2954. return ret;
  2955. }
  2956. #endif
  2957. /*
  2958. * Periodic address status verification
  2959. */
  2960. static void addrconf_verify(unsigned long foo)
  2961. {
  2962. unsigned long now, next, next_sec, next_sched;
  2963. struct inet6_ifaddr *ifp;
  2964. int i;
  2965. rcu_read_lock_bh();
  2966. spin_lock(&addrconf_verify_lock);
  2967. now = jiffies;
  2968. next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
  2969. del_timer(&addr_chk_timer);
  2970. for (i = 0; i < IN6_ADDR_HSIZE; i++) {
  2971. restart:
  2972. hlist_for_each_entry_rcu_bh(ifp,
  2973. &inet6_addr_lst[i], addr_lst) {
  2974. unsigned long age;
  2975. if (ifp->flags & IFA_F_PERMANENT)
  2976. continue;
  2977. spin_lock(&ifp->lock);
  2978. /* We try to batch several events at once. */
  2979. age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
  2980. if (ifp->valid_lft != INFINITY_LIFE_TIME &&
  2981. age >= ifp->valid_lft) {
  2982. spin_unlock(&ifp->lock);
  2983. in6_ifa_hold(ifp);
  2984. ipv6_del_addr(ifp);
  2985. goto restart;
  2986. } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
  2987. spin_unlock(&ifp->lock);
  2988. continue;
  2989. } else if (age >= ifp->prefered_lft) {
  2990. /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
  2991. int deprecate = 0;
  2992. if (!(ifp->flags&IFA_F_DEPRECATED)) {
  2993. deprecate = 1;
  2994. ifp->flags |= IFA_F_DEPRECATED;
  2995. }
  2996. if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
  2997. next = ifp->tstamp + ifp->valid_lft * HZ;
  2998. spin_unlock(&ifp->lock);
  2999. if (deprecate) {
  3000. in6_ifa_hold(ifp);
  3001. ipv6_ifa_notify(0, ifp);
  3002. in6_ifa_put(ifp);
  3003. goto restart;
  3004. }
  3005. #ifdef CONFIG_IPV6_PRIVACY
  3006. } else if ((ifp->flags&IFA_F_TEMPORARY) &&
  3007. !(ifp->flags&IFA_F_TENTATIVE)) {
  3008. unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
  3009. ifp->idev->cnf.dad_transmits *
  3010. ifp->idev->nd_parms->retrans_time / HZ;
  3011. if (age >= ifp->prefered_lft - regen_advance) {
  3012. struct inet6_ifaddr *ifpub = ifp->ifpub;
  3013. if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
  3014. next = ifp->tstamp + ifp->prefered_lft * HZ;
  3015. if (!ifp->regen_count && ifpub) {
  3016. ifp->regen_count++;
  3017. in6_ifa_hold(ifp);
  3018. in6_ifa_hold(ifpub);
  3019. spin_unlock(&ifp->lock);
  3020. spin_lock(&ifpub->lock);
  3021. ifpub->regen_count = 0;
  3022. spin_unlock(&ifpub->lock);
  3023. ipv6_create_tempaddr(ifpub, ifp);
  3024. in6_ifa_put(ifpub);
  3025. in6_ifa_put(ifp);
  3026. goto restart;
  3027. }
  3028. } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
  3029. next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
  3030. spin_unlock(&ifp->lock);
  3031. #endif
  3032. } else {
  3033. /* ifp->prefered_lft <= ifp->valid_lft */
  3034. if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
  3035. next = ifp->tstamp + ifp->prefered_lft * HZ;
  3036. spin_unlock(&ifp->lock);
  3037. }
  3038. }
  3039. }
  3040. next_sec = round_jiffies_up(next);
  3041. next_sched = next;
  3042. /* If rounded timeout is accurate enough, accept it. */
  3043. if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
  3044. next_sched = next_sec;
  3045. /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
  3046. if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
  3047. next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
  3048. ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
  3049. now, next, next_sec, next_sched);
  3050. addr_chk_timer.expires = next_sched;
  3051. add_timer(&addr_chk_timer);
  3052. spin_unlock(&addrconf_verify_lock);
  3053. rcu_read_unlock_bh();
  3054. }
  3055. static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
  3056. struct in6_addr **peer_pfx)
  3057. {
  3058. struct in6_addr *pfx = NULL;
  3059. *peer_pfx = NULL;
  3060. if (addr)
  3061. pfx = nla_data(addr);
  3062. if (local) {
  3063. if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
  3064. *peer_pfx = pfx;
  3065. pfx = nla_data(local);
  3066. }
  3067. return pfx;
  3068. }
  3069. static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
  3070. [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
  3071. [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
  3072. [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
  3073. };
  3074. static int
  3075. inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
  3076. {
  3077. struct net *net = sock_net(skb->sk);
  3078. struct ifaddrmsg *ifm;
  3079. struct nlattr *tb[IFA_MAX+1];
  3080. struct in6_addr *pfx, *peer_pfx;
  3081. int err;
  3082. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
  3083. if (err < 0)
  3084. return err;
  3085. ifm = nlmsg_data(nlh);
  3086. pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
  3087. if (pfx == NULL)
  3088. return -EINVAL;
  3089. return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen);
  3090. }
  3091. static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
  3092. u32 prefered_lft, u32 valid_lft)
  3093. {
  3094. u32 flags;
  3095. clock_t expires;
  3096. unsigned long timeout;
  3097. if (!valid_lft || (prefered_lft > valid_lft))
  3098. return -EINVAL;
  3099. timeout = addrconf_timeout_fixup(valid_lft, HZ);
  3100. if (addrconf_finite_timeout(timeout)) {
  3101. expires = jiffies_to_clock_t(timeout * HZ);
  3102. valid_lft = timeout;
  3103. flags = RTF_EXPIRES;
  3104. } else {
  3105. expires = 0;
  3106. flags = 0;
  3107. ifa_flags |= IFA_F_PERMANENT;
  3108. }
  3109. timeout = addrconf_timeout_fixup(prefered_lft, HZ);
  3110. if (addrconf_finite_timeout(timeout)) {
  3111. if (timeout == 0)
  3112. ifa_flags |= IFA_F_DEPRECATED;
  3113. prefered_lft = timeout;
  3114. }
  3115. spin_lock_bh(&ifp->lock);
  3116. ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags;
  3117. ifp->tstamp = jiffies;
  3118. ifp->valid_lft = valid_lft;
  3119. ifp->prefered_lft = prefered_lft;
  3120. spin_unlock_bh(&ifp->lock);
  3121. if (!(ifp->flags&IFA_F_TENTATIVE))
  3122. ipv6_ifa_notify(0, ifp);
  3123. addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
  3124. expires, flags);
  3125. addrconf_verify(0);
  3126. return 0;
  3127. }
  3128. static int
  3129. inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
  3130. {
  3131. struct net *net = sock_net(skb->sk);
  3132. struct ifaddrmsg *ifm;
  3133. struct nlattr *tb[IFA_MAX+1];
  3134. struct in6_addr *pfx, *peer_pfx;
  3135. struct inet6_ifaddr *ifa;
  3136. struct net_device *dev;
  3137. u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
  3138. u8 ifa_flags;
  3139. int err;
  3140. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
  3141. if (err < 0)
  3142. return err;
  3143. ifm = nlmsg_data(nlh);
  3144. pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
  3145. if (pfx == NULL)
  3146. return -EINVAL;
  3147. if (tb[IFA_CACHEINFO]) {
  3148. struct ifa_cacheinfo *ci;
  3149. ci = nla_data(tb[IFA_CACHEINFO]);
  3150. valid_lft = ci->ifa_valid;
  3151. preferred_lft = ci->ifa_prefered;
  3152. } else {
  3153. preferred_lft = INFINITY_LIFE_TIME;
  3154. valid_lft = INFINITY_LIFE_TIME;
  3155. }
  3156. dev = __dev_get_by_index(net, ifm->ifa_index);
  3157. if (dev == NULL)
  3158. return -ENODEV;
  3159. /* We ignore other flags so far. */
  3160. ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
  3161. ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
  3162. if (ifa == NULL) {
  3163. /*
  3164. * It would be best to check for !NLM_F_CREATE here but
  3165. * userspace alreay relies on not having to provide this.
  3166. */
  3167. return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
  3168. ifm->ifa_prefixlen, ifa_flags,
  3169. preferred_lft, valid_lft);
  3170. }
  3171. if (nlh->nlmsg_flags & NLM_F_EXCL ||
  3172. !(nlh->nlmsg_flags & NLM_F_REPLACE))
  3173. err = -EEXIST;
  3174. else
  3175. err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
  3176. in6_ifa_put(ifa);
  3177. return err;
  3178. }
  3179. static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
  3180. u8 scope, int ifindex)
  3181. {
  3182. struct ifaddrmsg *ifm;
  3183. ifm = nlmsg_data(nlh);
  3184. ifm->ifa_family = AF_INET6;
  3185. ifm->ifa_prefixlen = prefixlen;
  3186. ifm->ifa_flags = flags;
  3187. ifm->ifa_scope = scope;
  3188. ifm->ifa_index = ifindex;
  3189. }
  3190. static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
  3191. unsigned long tstamp, u32 preferred, u32 valid)
  3192. {
  3193. struct ifa_cacheinfo ci;
  3194. ci.cstamp = cstamp_delta(cstamp);
  3195. ci.tstamp = cstamp_delta(tstamp);
  3196. ci.ifa_prefered = preferred;
  3197. ci.ifa_valid = valid;
  3198. return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
  3199. }
  3200. static inline int rt_scope(int ifa_scope)
  3201. {
  3202. if (ifa_scope & IFA_HOST)
  3203. return RT_SCOPE_HOST;
  3204. else if (ifa_scope & IFA_LINK)
  3205. return RT_SCOPE_LINK;
  3206. else if (ifa_scope & IFA_SITE)
  3207. return RT_SCOPE_SITE;
  3208. else
  3209. return RT_SCOPE_UNIVERSE;
  3210. }
  3211. static inline int inet6_ifaddr_msgsize(void)
  3212. {
  3213. return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
  3214. + nla_total_size(16) /* IFA_LOCAL */
  3215. + nla_total_size(16) /* IFA_ADDRESS */
  3216. + nla_total_size(sizeof(struct ifa_cacheinfo));
  3217. }
  3218. static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
  3219. u32 portid, u32 seq, int event, unsigned int flags)
  3220. {
  3221. struct nlmsghdr *nlh;
  3222. u32 preferred, valid;
  3223. nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
  3224. if (nlh == NULL)
  3225. return -EMSGSIZE;
  3226. put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
  3227. ifa->idev->dev->ifindex);
  3228. if (!(ifa->flags&IFA_F_PERMANENT)) {
  3229. preferred = ifa->prefered_lft;
  3230. valid = ifa->valid_lft;
  3231. if (preferred != INFINITY_LIFE_TIME) {
  3232. long tval = (jiffies - ifa->tstamp)/HZ;
  3233. if (preferred > tval)
  3234. preferred -= tval;
  3235. else
  3236. preferred = 0;
  3237. if (valid != INFINITY_LIFE_TIME) {
  3238. if (valid > tval)
  3239. valid -= tval;
  3240. else
  3241. valid = 0;
  3242. }
  3243. }
  3244. } else {
  3245. preferred = INFINITY_LIFE_TIME;
  3246. valid = INFINITY_LIFE_TIME;
  3247. }
  3248. if (!ipv6_addr_any(&ifa->peer_addr)) {
  3249. if (nla_put(skb, IFA_LOCAL, 16, &ifa->addr) < 0 ||
  3250. nla_put(skb, IFA_ADDRESS, 16, &ifa->peer_addr) < 0)
  3251. goto error;
  3252. } else
  3253. if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0)
  3254. goto error;
  3255. if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
  3256. goto error;
  3257. return nlmsg_end(skb, nlh);
  3258. error:
  3259. nlmsg_cancel(skb, nlh);
  3260. return -EMSGSIZE;
  3261. }
  3262. static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
  3263. u32 portid, u32 seq, int event, u16 flags)
  3264. {
  3265. struct nlmsghdr *nlh;
  3266. u8 scope = RT_SCOPE_UNIVERSE;
  3267. int ifindex = ifmca->idev->dev->ifindex;
  3268. if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
  3269. scope = RT_SCOPE_SITE;
  3270. nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
  3271. if (nlh == NULL)
  3272. return -EMSGSIZE;
  3273. put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
  3274. if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
  3275. put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
  3276. INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
  3277. nlmsg_cancel(skb, nlh);
  3278. return -EMSGSIZE;
  3279. }
  3280. return nlmsg_end(skb, nlh);
  3281. }
  3282. static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
  3283. u32 portid, u32 seq, int event, unsigned int flags)
  3284. {
  3285. struct nlmsghdr *nlh;
  3286. u8 scope = RT_SCOPE_UNIVERSE;
  3287. int ifindex = ifaca->aca_idev->dev->ifindex;
  3288. if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
  3289. scope = RT_SCOPE_SITE;
  3290. nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
  3291. if (nlh == NULL)
  3292. return -EMSGSIZE;
  3293. put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
  3294. if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
  3295. put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
  3296. INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
  3297. nlmsg_cancel(skb, nlh);
  3298. return -EMSGSIZE;
  3299. }
  3300. return nlmsg_end(skb, nlh);
  3301. }
  3302. enum addr_type_t {
  3303. UNICAST_ADDR,
  3304. MULTICAST_ADDR,
  3305. ANYCAST_ADDR,
  3306. };
  3307. /* called with rcu_read_lock() */
  3308. static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
  3309. struct netlink_callback *cb, enum addr_type_t type,
  3310. int s_ip_idx, int *p_ip_idx)
  3311. {
  3312. struct ifmcaddr6 *ifmca;
  3313. struct ifacaddr6 *ifaca;
  3314. int err = 1;
  3315. int ip_idx = *p_ip_idx;
  3316. read_lock_bh(&idev->lock);
  3317. switch (type) {
  3318. case UNICAST_ADDR: {
  3319. struct inet6_ifaddr *ifa;
  3320. /* unicast address incl. temp addr */
  3321. list_for_each_entry(ifa, &idev->addr_list, if_list) {
  3322. if (++ip_idx < s_ip_idx)
  3323. continue;
  3324. err = inet6_fill_ifaddr(skb, ifa,
  3325. NETLINK_CB(cb->skb).portid,
  3326. cb->nlh->nlmsg_seq,
  3327. RTM_NEWADDR,
  3328. NLM_F_MULTI);
  3329. if (err <= 0)
  3330. break;
  3331. nl_dump_check_consistent(cb, nlmsg_hdr(skb));
  3332. }
  3333. break;
  3334. }
  3335. case MULTICAST_ADDR:
  3336. /* multicast address */
  3337. for (ifmca = idev->mc_list; ifmca;
  3338. ifmca = ifmca->next, ip_idx++) {
  3339. if (ip_idx < s_ip_idx)
  3340. continue;
  3341. err = inet6_fill_ifmcaddr(skb, ifmca,
  3342. NETLINK_CB(cb->skb).portid,
  3343. cb->nlh->nlmsg_seq,
  3344. RTM_GETMULTICAST,
  3345. NLM_F_MULTI);
  3346. if (err <= 0)
  3347. break;
  3348. }
  3349. break;
  3350. case ANYCAST_ADDR:
  3351. /* anycast address */
  3352. for (ifaca = idev->ac_list; ifaca;
  3353. ifaca = ifaca->aca_next, ip_idx++) {
  3354. if (ip_idx < s_ip_idx)
  3355. continue;
  3356. err = inet6_fill_ifacaddr(skb, ifaca,
  3357. NETLINK_CB(cb->skb).portid,
  3358. cb->nlh->nlmsg_seq,
  3359. RTM_GETANYCAST,
  3360. NLM_F_MULTI);
  3361. if (err <= 0)
  3362. break;
  3363. }
  3364. break;
  3365. default:
  3366. break;
  3367. }
  3368. read_unlock_bh(&idev->lock);
  3369. *p_ip_idx = ip_idx;
  3370. return err;
  3371. }
  3372. static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
  3373. enum addr_type_t type)
  3374. {
  3375. struct net *net = sock_net(skb->sk);
  3376. int h, s_h;
  3377. int idx, ip_idx;
  3378. int s_idx, s_ip_idx;
  3379. struct net_device *dev;
  3380. struct inet6_dev *idev;
  3381. struct hlist_head *head;
  3382. s_h = cb->args[0];
  3383. s_idx = idx = cb->args[1];
  3384. s_ip_idx = ip_idx = cb->args[2];
  3385. rcu_read_lock();
  3386. cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
  3387. for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
  3388. idx = 0;
  3389. head = &net->dev_index_head[h];
  3390. hlist_for_each_entry_rcu(dev, head, index_hlist) {
  3391. if (idx < s_idx)
  3392. goto cont;
  3393. if (h > s_h || idx > s_idx)
  3394. s_ip_idx = 0;
  3395. ip_idx = 0;
  3396. idev = __in6_dev_get(dev);
  3397. if (!idev)
  3398. goto cont;
  3399. if (in6_dump_addrs(idev, skb, cb, type,
  3400. s_ip_idx, &ip_idx) <= 0)
  3401. goto done;
  3402. cont:
  3403. idx++;
  3404. }
  3405. }
  3406. done:
  3407. rcu_read_unlock();
  3408. cb->args[0] = h;
  3409. cb->args[1] = idx;
  3410. cb->args[2] = ip_idx;
  3411. return skb->len;
  3412. }
  3413. static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
  3414. {
  3415. enum addr_type_t type = UNICAST_ADDR;
  3416. return inet6_dump_addr(skb, cb, type);
  3417. }
  3418. static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
  3419. {
  3420. enum addr_type_t type = MULTICAST_ADDR;
  3421. return inet6_dump_addr(skb, cb, type);
  3422. }
  3423. static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
  3424. {
  3425. enum addr_type_t type = ANYCAST_ADDR;
  3426. return inet6_dump_addr(skb, cb, type);
  3427. }
  3428. static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
  3429. {
  3430. struct net *net = sock_net(in_skb->sk);
  3431. struct ifaddrmsg *ifm;
  3432. struct nlattr *tb[IFA_MAX+1];
  3433. struct in6_addr *addr = NULL, *peer;
  3434. struct net_device *dev = NULL;
  3435. struct inet6_ifaddr *ifa;
  3436. struct sk_buff *skb;
  3437. int err;
  3438. err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
  3439. if (err < 0)
  3440. goto errout;
  3441. addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
  3442. if (addr == NULL) {
  3443. err = -EINVAL;
  3444. goto errout;
  3445. }
  3446. ifm = nlmsg_data(nlh);
  3447. if (ifm->ifa_index)
  3448. dev = __dev_get_by_index(net, ifm->ifa_index);
  3449. ifa = ipv6_get_ifaddr(net, addr, dev, 1);
  3450. if (!ifa) {
  3451. err = -EADDRNOTAVAIL;
  3452. goto errout;
  3453. }
  3454. skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
  3455. if (!skb) {
  3456. err = -ENOBUFS;
  3457. goto errout_ifa;
  3458. }
  3459. err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
  3460. nlh->nlmsg_seq, RTM_NEWADDR, 0);
  3461. if (err < 0) {
  3462. /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
  3463. WARN_ON(err == -EMSGSIZE);
  3464. kfree_skb(skb);
  3465. goto errout_ifa;
  3466. }
  3467. err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
  3468. errout_ifa:
  3469. in6_ifa_put(ifa);
  3470. errout:
  3471. return err;
  3472. }
  3473. static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
  3474. {
  3475. struct sk_buff *skb;
  3476. struct net *net = dev_net(ifa->idev->dev);
  3477. int err = -ENOBUFS;
  3478. skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
  3479. if (skb == NULL)
  3480. goto errout;
  3481. err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
  3482. if (err < 0) {
  3483. /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
  3484. WARN_ON(err == -EMSGSIZE);
  3485. kfree_skb(skb);
  3486. goto errout;
  3487. }
  3488. rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
  3489. return;
  3490. errout:
  3491. if (err < 0)
  3492. rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
  3493. }
  3494. static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
  3495. __s32 *array, int bytes)
  3496. {
  3497. BUG_ON(bytes < (DEVCONF_MAX * 4));
  3498. memset(array, 0, bytes);
  3499. array[DEVCONF_FORWARDING] = cnf->forwarding;
  3500. array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
  3501. array[DEVCONF_MTU6] = cnf->mtu6;
  3502. array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
  3503. array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
  3504. array[DEVCONF_AUTOCONF] = cnf->autoconf;
  3505. array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
  3506. array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
  3507. array[DEVCONF_RTR_SOLICIT_INTERVAL] =
  3508. jiffies_to_msecs(cnf->rtr_solicit_interval);
  3509. array[DEVCONF_RTR_SOLICIT_DELAY] =
  3510. jiffies_to_msecs(cnf->rtr_solicit_delay);
  3511. array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
  3512. array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
  3513. jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
  3514. array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
  3515. jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
  3516. #ifdef CONFIG_IPV6_PRIVACY
  3517. array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
  3518. array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
  3519. array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
  3520. array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
  3521. array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
  3522. #endif
  3523. array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
  3524. array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
  3525. array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
  3526. #ifdef CONFIG_IPV6_ROUTER_PREF
  3527. array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
  3528. array[DEVCONF_RTR_PROBE_INTERVAL] =
  3529. jiffies_to_msecs(cnf->rtr_probe_interval);
  3530. #ifdef CONFIG_IPV6_ROUTE_INFO
  3531. array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
  3532. #endif
  3533. #endif
  3534. array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
  3535. array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
  3536. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  3537. array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
  3538. #endif
  3539. #ifdef CONFIG_IPV6_MROUTE
  3540. array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
  3541. #endif
  3542. array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
  3543. array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
  3544. array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
  3545. array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
  3546. array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
  3547. }
  3548. static inline size_t inet6_ifla6_size(void)
  3549. {
  3550. return nla_total_size(4) /* IFLA_INET6_FLAGS */
  3551. + nla_total_size(sizeof(struct ifla_cacheinfo))
  3552. + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
  3553. + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
  3554. + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
  3555. + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
  3556. }
  3557. static inline size_t inet6_if_nlmsg_size(void)
  3558. {
  3559. return NLMSG_ALIGN(sizeof(struct ifinfomsg))
  3560. + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
  3561. + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
  3562. + nla_total_size(4) /* IFLA_MTU */
  3563. + nla_total_size(4) /* IFLA_LINK */
  3564. + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
  3565. }
  3566. static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
  3567. int items, int bytes)
  3568. {
  3569. int i;
  3570. int pad = bytes - sizeof(u64) * items;
  3571. BUG_ON(pad < 0);
  3572. /* Use put_unaligned() because stats may not be aligned for u64. */
  3573. put_unaligned(items, &stats[0]);
  3574. for (i = 1; i < items; i++)
  3575. put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
  3576. memset(&stats[items], 0, pad);
  3577. }
  3578. static inline void __snmp6_fill_stats64(u64 *stats, void __percpu **mib,
  3579. int items, int bytes, size_t syncpoff)
  3580. {
  3581. int i;
  3582. int pad = bytes - sizeof(u64) * items;
  3583. BUG_ON(pad < 0);
  3584. /* Use put_unaligned() because stats may not be aligned for u64. */
  3585. put_unaligned(items, &stats[0]);
  3586. for (i = 1; i < items; i++)
  3587. put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]);
  3588. memset(&stats[items], 0, pad);
  3589. }
  3590. static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
  3591. int bytes)
  3592. {
  3593. switch (attrtype) {
  3594. case IFLA_INET6_STATS:
  3595. __snmp6_fill_stats64(stats, (void __percpu **)idev->stats.ipv6,
  3596. IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp));
  3597. break;
  3598. case IFLA_INET6_ICMP6STATS:
  3599. __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes);
  3600. break;
  3601. }
  3602. }
  3603. static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev)
  3604. {
  3605. struct nlattr *nla;
  3606. struct ifla_cacheinfo ci;
  3607. if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
  3608. goto nla_put_failure;
  3609. ci.max_reasm_len = IPV6_MAXPLEN;
  3610. ci.tstamp = cstamp_delta(idev->tstamp);
  3611. ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
  3612. ci.retrans_time = jiffies_to_msecs(idev->nd_parms->retrans_time);
  3613. if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
  3614. goto nla_put_failure;
  3615. nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
  3616. if (nla == NULL)
  3617. goto nla_put_failure;
  3618. ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
  3619. /* XXX - MC not implemented */
  3620. nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
  3621. if (nla == NULL)
  3622. goto nla_put_failure;
  3623. snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
  3624. nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
  3625. if (nla == NULL)
  3626. goto nla_put_failure;
  3627. snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
  3628. nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
  3629. if (nla == NULL)
  3630. goto nla_put_failure;
  3631. read_lock_bh(&idev->lock);
  3632. memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
  3633. read_unlock_bh(&idev->lock);
  3634. return 0;
  3635. nla_put_failure:
  3636. return -EMSGSIZE;
  3637. }
  3638. static size_t inet6_get_link_af_size(const struct net_device *dev)
  3639. {
  3640. if (!__in6_dev_get(dev))
  3641. return 0;
  3642. return inet6_ifla6_size();
  3643. }
  3644. static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
  3645. {
  3646. struct inet6_dev *idev = __in6_dev_get(dev);
  3647. if (!idev)
  3648. return -ENODATA;
  3649. if (inet6_fill_ifla6_attrs(skb, idev) < 0)
  3650. return -EMSGSIZE;
  3651. return 0;
  3652. }
  3653. static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
  3654. {
  3655. struct inet6_ifaddr *ifp;
  3656. struct net_device *dev = idev->dev;
  3657. bool update_rs = false;
  3658. struct in6_addr ll_addr;
  3659. if (token == NULL)
  3660. return -EINVAL;
  3661. if (ipv6_addr_any(token))
  3662. return -EINVAL;
  3663. if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
  3664. return -EINVAL;
  3665. if (!ipv6_accept_ra(idev))
  3666. return -EINVAL;
  3667. if (idev->cnf.rtr_solicits <= 0)
  3668. return -EINVAL;
  3669. write_lock_bh(&idev->lock);
  3670. BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
  3671. memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
  3672. write_unlock_bh(&idev->lock);
  3673. if (!idev->dead && (idev->if_flags & IF_READY) &&
  3674. !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
  3675. IFA_F_OPTIMISTIC)) {
  3676. /* If we're not ready, then normal ifup will take care
  3677. * of this. Otherwise, we need to request our rs here.
  3678. */
  3679. ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
  3680. update_rs = true;
  3681. }
  3682. write_lock_bh(&idev->lock);
  3683. if (update_rs) {
  3684. idev->if_flags |= IF_RS_SENT;
  3685. idev->rs_probes = 1;
  3686. addrconf_mod_rs_timer(idev, idev->cnf.rtr_solicit_interval);
  3687. }
  3688. /* Well, that's kinda nasty ... */
  3689. list_for_each_entry(ifp, &idev->addr_list, if_list) {
  3690. spin_lock(&ifp->lock);
  3691. if (ifp->tokenized) {
  3692. ifp->valid_lft = 0;
  3693. ifp->prefered_lft = 0;
  3694. }
  3695. spin_unlock(&ifp->lock);
  3696. }
  3697. write_unlock_bh(&idev->lock);
  3698. addrconf_verify(0);
  3699. return 0;
  3700. }
  3701. static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
  3702. {
  3703. int err = -EINVAL;
  3704. struct inet6_dev *idev = __in6_dev_get(dev);
  3705. struct nlattr *tb[IFLA_INET6_MAX + 1];
  3706. if (!idev)
  3707. return -EAFNOSUPPORT;
  3708. if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
  3709. BUG();
  3710. if (tb[IFLA_INET6_TOKEN])
  3711. err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
  3712. return err;
  3713. }
  3714. static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
  3715. u32 portid, u32 seq, int event, unsigned int flags)
  3716. {
  3717. struct net_device *dev = idev->dev;
  3718. struct ifinfomsg *hdr;
  3719. struct nlmsghdr *nlh;
  3720. void *protoinfo;
  3721. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
  3722. if (nlh == NULL)
  3723. return -EMSGSIZE;
  3724. hdr = nlmsg_data(nlh);
  3725. hdr->ifi_family = AF_INET6;
  3726. hdr->__ifi_pad = 0;
  3727. hdr->ifi_type = dev->type;
  3728. hdr->ifi_index = dev->ifindex;
  3729. hdr->ifi_flags = dev_get_flags(dev);
  3730. hdr->ifi_change = 0;
  3731. if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
  3732. (dev->addr_len &&
  3733. nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
  3734. nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
  3735. (dev->ifindex != dev->iflink &&
  3736. nla_put_u32(skb, IFLA_LINK, dev->iflink)))
  3737. goto nla_put_failure;
  3738. protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
  3739. if (protoinfo == NULL)
  3740. goto nla_put_failure;
  3741. if (inet6_fill_ifla6_attrs(skb, idev) < 0)
  3742. goto nla_put_failure;
  3743. nla_nest_end(skb, protoinfo);
  3744. return nlmsg_end(skb, nlh);
  3745. nla_put_failure:
  3746. nlmsg_cancel(skb, nlh);
  3747. return -EMSGSIZE;
  3748. }
  3749. static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
  3750. {
  3751. struct net *net = sock_net(skb->sk);
  3752. int h, s_h;
  3753. int idx = 0, s_idx;
  3754. struct net_device *dev;
  3755. struct inet6_dev *idev;
  3756. struct hlist_head *head;
  3757. s_h = cb->args[0];
  3758. s_idx = cb->args[1];
  3759. rcu_read_lock();
  3760. for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
  3761. idx = 0;
  3762. head = &net->dev_index_head[h];
  3763. hlist_for_each_entry_rcu(dev, head, index_hlist) {
  3764. if (idx < s_idx)
  3765. goto cont;
  3766. idev = __in6_dev_get(dev);
  3767. if (!idev)
  3768. goto cont;
  3769. if (inet6_fill_ifinfo(skb, idev,
  3770. NETLINK_CB(cb->skb).portid,
  3771. cb->nlh->nlmsg_seq,
  3772. RTM_NEWLINK, NLM_F_MULTI) <= 0)
  3773. goto out;
  3774. cont:
  3775. idx++;
  3776. }
  3777. }
  3778. out:
  3779. rcu_read_unlock();
  3780. cb->args[1] = idx;
  3781. cb->args[0] = h;
  3782. return skb->len;
  3783. }
  3784. void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
  3785. {
  3786. struct sk_buff *skb;
  3787. struct net *net = dev_net(idev->dev);
  3788. int err = -ENOBUFS;
  3789. skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
  3790. if (skb == NULL)
  3791. goto errout;
  3792. err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
  3793. if (err < 0) {
  3794. /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
  3795. WARN_ON(err == -EMSGSIZE);
  3796. kfree_skb(skb);
  3797. goto errout;
  3798. }
  3799. rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
  3800. return;
  3801. errout:
  3802. if (err < 0)
  3803. rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
  3804. }
  3805. static inline size_t inet6_prefix_nlmsg_size(void)
  3806. {
  3807. return NLMSG_ALIGN(sizeof(struct prefixmsg))
  3808. + nla_total_size(sizeof(struct in6_addr))
  3809. + nla_total_size(sizeof(struct prefix_cacheinfo));
  3810. }
  3811. static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
  3812. struct prefix_info *pinfo, u32 portid, u32 seq,
  3813. int event, unsigned int flags)
  3814. {
  3815. struct prefixmsg *pmsg;
  3816. struct nlmsghdr *nlh;
  3817. struct prefix_cacheinfo ci;
  3818. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
  3819. if (nlh == NULL)
  3820. return -EMSGSIZE;
  3821. pmsg = nlmsg_data(nlh);
  3822. pmsg->prefix_family = AF_INET6;
  3823. pmsg->prefix_pad1 = 0;
  3824. pmsg->prefix_pad2 = 0;
  3825. pmsg->prefix_ifindex = idev->dev->ifindex;
  3826. pmsg->prefix_len = pinfo->prefix_len;
  3827. pmsg->prefix_type = pinfo->type;
  3828. pmsg->prefix_pad3 = 0;
  3829. pmsg->prefix_flags = 0;
  3830. if (pinfo->onlink)
  3831. pmsg->prefix_flags |= IF_PREFIX_ONLINK;
  3832. if (pinfo->autoconf)
  3833. pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
  3834. if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
  3835. goto nla_put_failure;
  3836. ci.preferred_time = ntohl(pinfo->prefered);
  3837. ci.valid_time = ntohl(pinfo->valid);
  3838. if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
  3839. goto nla_put_failure;
  3840. return nlmsg_end(skb, nlh);
  3841. nla_put_failure:
  3842. nlmsg_cancel(skb, nlh);
  3843. return -EMSGSIZE;
  3844. }
  3845. static void inet6_prefix_notify(int event, struct inet6_dev *idev,
  3846. struct prefix_info *pinfo)
  3847. {
  3848. struct sk_buff *skb;
  3849. struct net *net = dev_net(idev->dev);
  3850. int err = -ENOBUFS;
  3851. skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
  3852. if (skb == NULL)
  3853. goto errout;
  3854. err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
  3855. if (err < 0) {
  3856. /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
  3857. WARN_ON(err == -EMSGSIZE);
  3858. kfree_skb(skb);
  3859. goto errout;
  3860. }
  3861. rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
  3862. return;
  3863. errout:
  3864. if (err < 0)
  3865. rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
  3866. }
  3867. static void update_valid_ll_addr_cnt(struct inet6_ifaddr *ifp, int count)
  3868. {
  3869. write_lock_bh(&ifp->idev->lock);
  3870. spin_lock(&ifp->lock);
  3871. if (((ifp->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|
  3872. IFA_F_DADFAILED)) == IFA_F_PERMANENT) &&
  3873. (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL))
  3874. ifp->idev->valid_ll_addr_cnt += count;
  3875. WARN_ON(ifp->idev->valid_ll_addr_cnt < 0);
  3876. spin_unlock(&ifp->lock);
  3877. write_unlock_bh(&ifp->idev->lock);
  3878. }
  3879. static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
  3880. {
  3881. struct net *net = dev_net(ifp->idev->dev);
  3882. inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
  3883. switch (event) {
  3884. case RTM_NEWADDR:
  3885. update_valid_ll_addr_cnt(ifp, 1);
  3886. /*
  3887. * If the address was optimistic
  3888. * we inserted the route at the start of
  3889. * our DAD process, so we don't need
  3890. * to do it again
  3891. */
  3892. if (!(ifp->rt->rt6i_node))
  3893. ip6_ins_rt(ifp->rt);
  3894. if (ifp->idev->cnf.forwarding)
  3895. addrconf_join_anycast(ifp);
  3896. if (!ipv6_addr_any(&ifp->peer_addr))
  3897. addrconf_prefix_route(&ifp->peer_addr, 128,
  3898. ifp->idev->dev, 0, 0);
  3899. break;
  3900. case RTM_DELADDR:
  3901. update_valid_ll_addr_cnt(ifp, -1);
  3902. if (ifp->idev->cnf.forwarding)
  3903. addrconf_leave_anycast(ifp);
  3904. addrconf_leave_solict(ifp->idev, &ifp->addr);
  3905. if (!ipv6_addr_any(&ifp->peer_addr)) {
  3906. struct rt6_info *rt;
  3907. struct net_device *dev = ifp->idev->dev;
  3908. rt = rt6_lookup(dev_net(dev), &ifp->peer_addr, NULL,
  3909. dev->ifindex, 1);
  3910. if (rt) {
  3911. dst_hold(&rt->dst);
  3912. if (ip6_del_rt(rt))
  3913. dst_free(&rt->dst);
  3914. }
  3915. }
  3916. dst_hold(&ifp->rt->dst);
  3917. if (ip6_del_rt(ifp->rt))
  3918. dst_free(&ifp->rt->dst);
  3919. break;
  3920. }
  3921. atomic_inc(&net->ipv6.dev_addr_genid);
  3922. rt_genid_bump_ipv6(net);
  3923. }
  3924. static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
  3925. {
  3926. rcu_read_lock_bh();
  3927. if (likely(ifp->idev->dead == 0))
  3928. __ipv6_ifa_notify(event, ifp);
  3929. rcu_read_unlock_bh();
  3930. }
  3931. #ifdef CONFIG_SYSCTL
  3932. static
  3933. int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
  3934. void __user *buffer, size_t *lenp, loff_t *ppos)
  3935. {
  3936. int *valp = ctl->data;
  3937. int val = *valp;
  3938. loff_t pos = *ppos;
  3939. struct ctl_table lctl;
  3940. int ret;
  3941. /*
  3942. * ctl->data points to idev->cnf.forwarding, we should
  3943. * not modify it until we get the rtnl lock.
  3944. */
  3945. lctl = *ctl;
  3946. lctl.data = &val;
  3947. ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
  3948. if (write)
  3949. ret = addrconf_fixup_forwarding(ctl, valp, val);
  3950. if (ret)
  3951. *ppos = pos;
  3952. return ret;
  3953. }
  3954. static void dev_disable_change(struct inet6_dev *idev)
  3955. {
  3956. struct netdev_notifier_info info;
  3957. if (!idev || !idev->dev)
  3958. return;
  3959. netdev_notifier_info_init(&info, idev->dev);
  3960. if (idev->cnf.disable_ipv6)
  3961. addrconf_notify(NULL, NETDEV_DOWN, &info);
  3962. else
  3963. addrconf_notify(NULL, NETDEV_UP, &info);
  3964. }
  3965. static void addrconf_disable_change(struct net *net, __s32 newf)
  3966. {
  3967. struct net_device *dev;
  3968. struct inet6_dev *idev;
  3969. rcu_read_lock();
  3970. for_each_netdev_rcu(net, dev) {
  3971. idev = __in6_dev_get(dev);
  3972. if (idev) {
  3973. int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
  3974. idev->cnf.disable_ipv6 = newf;
  3975. if (changed)
  3976. dev_disable_change(idev);
  3977. }
  3978. }
  3979. rcu_read_unlock();
  3980. }
  3981. static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
  3982. {
  3983. struct net *net;
  3984. int old;
  3985. if (!rtnl_trylock())
  3986. return restart_syscall();
  3987. net = (struct net *)table->extra2;
  3988. old = *p;
  3989. *p = newf;
  3990. if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
  3991. rtnl_unlock();
  3992. return 0;
  3993. }
  3994. if (p == &net->ipv6.devconf_all->disable_ipv6) {
  3995. net->ipv6.devconf_dflt->disable_ipv6 = newf;
  3996. addrconf_disable_change(net, newf);
  3997. } else if ((!newf) ^ (!old))
  3998. dev_disable_change((struct inet6_dev *)table->extra1);
  3999. rtnl_unlock();
  4000. return 0;
  4001. }
  4002. static
  4003. int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
  4004. void __user *buffer, size_t *lenp, loff_t *ppos)
  4005. {
  4006. int *valp = ctl->data;
  4007. int val = *valp;
  4008. loff_t pos = *ppos;
  4009. struct ctl_table lctl;
  4010. int ret;
  4011. /*
  4012. * ctl->data points to idev->cnf.disable_ipv6, we should
  4013. * not modify it until we get the rtnl lock.
  4014. */
  4015. lctl = *ctl;
  4016. lctl.data = &val;
  4017. ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
  4018. if (write)
  4019. ret = addrconf_disable_ipv6(ctl, valp, val);
  4020. if (ret)
  4021. *ppos = pos;
  4022. return ret;
  4023. }
  4024. static struct addrconf_sysctl_table
  4025. {
  4026. struct ctl_table_header *sysctl_header;
  4027. struct ctl_table addrconf_vars[DEVCONF_MAX+1];
  4028. } addrconf_sysctl __read_mostly = {
  4029. .sysctl_header = NULL,
  4030. .addrconf_vars = {
  4031. {
  4032. .procname = "forwarding",
  4033. .data = &ipv6_devconf.forwarding,
  4034. .maxlen = sizeof(int),
  4035. .mode = 0644,
  4036. .proc_handler = addrconf_sysctl_forward,
  4037. },
  4038. {
  4039. .procname = "hop_limit",
  4040. .data = &ipv6_devconf.hop_limit,
  4041. .maxlen = sizeof(int),
  4042. .mode = 0644,
  4043. .proc_handler = proc_dointvec,
  4044. },
  4045. {
  4046. .procname = "mtu",
  4047. .data = &ipv6_devconf.mtu6,
  4048. .maxlen = sizeof(int),
  4049. .mode = 0644,
  4050. .proc_handler = proc_dointvec,
  4051. },
  4052. {
  4053. .procname = "accept_ra",
  4054. .data = &ipv6_devconf.accept_ra,
  4055. .maxlen = sizeof(int),
  4056. .mode = 0644,
  4057. .proc_handler = proc_dointvec,
  4058. },
  4059. {
  4060. .procname = "accept_redirects",
  4061. .data = &ipv6_devconf.accept_redirects,
  4062. .maxlen = sizeof(int),
  4063. .mode = 0644,
  4064. .proc_handler = proc_dointvec,
  4065. },
  4066. {
  4067. .procname = "autoconf",
  4068. .data = &ipv6_devconf.autoconf,
  4069. .maxlen = sizeof(int),
  4070. .mode = 0644,
  4071. .proc_handler = proc_dointvec,
  4072. },
  4073. {
  4074. .procname = "dad_transmits",
  4075. .data = &ipv6_devconf.dad_transmits,
  4076. .maxlen = sizeof(int),
  4077. .mode = 0644,
  4078. .proc_handler = proc_dointvec,
  4079. },
  4080. {
  4081. .procname = "router_solicitations",
  4082. .data = &ipv6_devconf.rtr_solicits,
  4083. .maxlen = sizeof(int),
  4084. .mode = 0644,
  4085. .proc_handler = proc_dointvec,
  4086. },
  4087. {
  4088. .procname = "router_solicitation_interval",
  4089. .data = &ipv6_devconf.rtr_solicit_interval,
  4090. .maxlen = sizeof(int),
  4091. .mode = 0644,
  4092. .proc_handler = proc_dointvec_jiffies,
  4093. },
  4094. {
  4095. .procname = "router_solicitation_delay",
  4096. .data = &ipv6_devconf.rtr_solicit_delay,
  4097. .maxlen = sizeof(int),
  4098. .mode = 0644,
  4099. .proc_handler = proc_dointvec_jiffies,
  4100. },
  4101. {
  4102. .procname = "force_mld_version",
  4103. .data = &ipv6_devconf.force_mld_version,
  4104. .maxlen = sizeof(int),
  4105. .mode = 0644,
  4106. .proc_handler = proc_dointvec,
  4107. },
  4108. {
  4109. .procname = "mldv1_unsolicited_report_interval",
  4110. .data =
  4111. &ipv6_devconf.mldv1_unsolicited_report_interval,
  4112. .maxlen = sizeof(int),
  4113. .mode = 0644,
  4114. .proc_handler = proc_dointvec_ms_jiffies,
  4115. },
  4116. {
  4117. .procname = "mldv2_unsolicited_report_interval",
  4118. .data =
  4119. &ipv6_devconf.mldv2_unsolicited_report_interval,
  4120. .maxlen = sizeof(int),
  4121. .mode = 0644,
  4122. .proc_handler = proc_dointvec_ms_jiffies,
  4123. },
  4124. #ifdef CONFIG_IPV6_PRIVACY
  4125. {
  4126. .procname = "use_tempaddr",
  4127. .data = &ipv6_devconf.use_tempaddr,
  4128. .maxlen = sizeof(int),
  4129. .mode = 0644,
  4130. .proc_handler = proc_dointvec,
  4131. },
  4132. {
  4133. .procname = "temp_valid_lft",
  4134. .data = &ipv6_devconf.temp_valid_lft,
  4135. .maxlen = sizeof(int),
  4136. .mode = 0644,
  4137. .proc_handler = proc_dointvec,
  4138. },
  4139. {
  4140. .procname = "temp_prefered_lft",
  4141. .data = &ipv6_devconf.temp_prefered_lft,
  4142. .maxlen = sizeof(int),
  4143. .mode = 0644,
  4144. .proc_handler = proc_dointvec,
  4145. },
  4146. {
  4147. .procname = "regen_max_retry",
  4148. .data = &ipv6_devconf.regen_max_retry,
  4149. .maxlen = sizeof(int),
  4150. .mode = 0644,
  4151. .proc_handler = proc_dointvec,
  4152. },
  4153. {
  4154. .procname = "max_desync_factor",
  4155. .data = &ipv6_devconf.max_desync_factor,
  4156. .maxlen = sizeof(int),
  4157. .mode = 0644,
  4158. .proc_handler = proc_dointvec,
  4159. },
  4160. #endif
  4161. {
  4162. .procname = "max_addresses",
  4163. .data = &ipv6_devconf.max_addresses,
  4164. .maxlen = sizeof(int),
  4165. .mode = 0644,
  4166. .proc_handler = proc_dointvec,
  4167. },
  4168. {
  4169. .procname = "accept_ra_defrtr",
  4170. .data = &ipv6_devconf.accept_ra_defrtr,
  4171. .maxlen = sizeof(int),
  4172. .mode = 0644,
  4173. .proc_handler = proc_dointvec,
  4174. },
  4175. {
  4176. .procname = "accept_ra_pinfo",
  4177. .data = &ipv6_devconf.accept_ra_pinfo,
  4178. .maxlen = sizeof(int),
  4179. .mode = 0644,
  4180. .proc_handler = proc_dointvec,
  4181. },
  4182. #ifdef CONFIG_IPV6_ROUTER_PREF
  4183. {
  4184. .procname = "accept_ra_rtr_pref",
  4185. .data = &ipv6_devconf.accept_ra_rtr_pref,
  4186. .maxlen = sizeof(int),
  4187. .mode = 0644,
  4188. .proc_handler = proc_dointvec,
  4189. },
  4190. {
  4191. .procname = "router_probe_interval",
  4192. .data = &ipv6_devconf.rtr_probe_interval,
  4193. .maxlen = sizeof(int),
  4194. .mode = 0644,
  4195. .proc_handler = proc_dointvec_jiffies,
  4196. },
  4197. #ifdef CONFIG_IPV6_ROUTE_INFO
  4198. {
  4199. .procname = "accept_ra_rt_info_max_plen",
  4200. .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
  4201. .maxlen = sizeof(int),
  4202. .mode = 0644,
  4203. .proc_handler = proc_dointvec,
  4204. },
  4205. #endif
  4206. #endif
  4207. {
  4208. .procname = "proxy_ndp",
  4209. .data = &ipv6_devconf.proxy_ndp,
  4210. .maxlen = sizeof(int),
  4211. .mode = 0644,
  4212. .proc_handler = proc_dointvec,
  4213. },
  4214. {
  4215. .procname = "accept_source_route",
  4216. .data = &ipv6_devconf.accept_source_route,
  4217. .maxlen = sizeof(int),
  4218. .mode = 0644,
  4219. .proc_handler = proc_dointvec,
  4220. },
  4221. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  4222. {
  4223. .procname = "optimistic_dad",
  4224. .data = &ipv6_devconf.optimistic_dad,
  4225. .maxlen = sizeof(int),
  4226. .mode = 0644,
  4227. .proc_handler = proc_dointvec,
  4228. },
  4229. #endif
  4230. #ifdef CONFIG_IPV6_MROUTE
  4231. {
  4232. .procname = "mc_forwarding",
  4233. .data = &ipv6_devconf.mc_forwarding,
  4234. .maxlen = sizeof(int),
  4235. .mode = 0444,
  4236. .proc_handler = proc_dointvec,
  4237. },
  4238. #endif
  4239. {
  4240. .procname = "disable_ipv6",
  4241. .data = &ipv6_devconf.disable_ipv6,
  4242. .maxlen = sizeof(int),
  4243. .mode = 0644,
  4244. .proc_handler = addrconf_sysctl_disable,
  4245. },
  4246. {
  4247. .procname = "accept_dad",
  4248. .data = &ipv6_devconf.accept_dad,
  4249. .maxlen = sizeof(int),
  4250. .mode = 0644,
  4251. .proc_handler = proc_dointvec,
  4252. },
  4253. {
  4254. .procname = "force_tllao",
  4255. .data = &ipv6_devconf.force_tllao,
  4256. .maxlen = sizeof(int),
  4257. .mode = 0644,
  4258. .proc_handler = proc_dointvec
  4259. },
  4260. {
  4261. .procname = "ndisc_notify",
  4262. .data = &ipv6_devconf.ndisc_notify,
  4263. .maxlen = sizeof(int),
  4264. .mode = 0644,
  4265. .proc_handler = proc_dointvec
  4266. },
  4267. {
  4268. .procname = "suppress_frag_ndisc",
  4269. .data = &ipv6_devconf.suppress_frag_ndisc,
  4270. .maxlen = sizeof(int),
  4271. .mode = 0644,
  4272. .proc_handler = proc_dointvec
  4273. },
  4274. {
  4275. /* sentinel */
  4276. }
  4277. },
  4278. };
  4279. static int __addrconf_sysctl_register(struct net *net, char *dev_name,
  4280. struct inet6_dev *idev, struct ipv6_devconf *p)
  4281. {
  4282. int i;
  4283. struct addrconf_sysctl_table *t;
  4284. char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
  4285. t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
  4286. if (t == NULL)
  4287. goto out;
  4288. for (i = 0; t->addrconf_vars[i].data; i++) {
  4289. t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf;
  4290. t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
  4291. t->addrconf_vars[i].extra2 = net;
  4292. }
  4293. snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
  4294. t->sysctl_header = register_net_sysctl(net, path, t->addrconf_vars);
  4295. if (t->sysctl_header == NULL)
  4296. goto free;
  4297. p->sysctl = t;
  4298. return 0;
  4299. free:
  4300. kfree(t);
  4301. out:
  4302. return -ENOBUFS;
  4303. }
  4304. static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
  4305. {
  4306. struct addrconf_sysctl_table *t;
  4307. if (p->sysctl == NULL)
  4308. return;
  4309. t = p->sysctl;
  4310. p->sysctl = NULL;
  4311. unregister_net_sysctl_table(t->sysctl_header);
  4312. kfree(t);
  4313. }
  4314. static void addrconf_sysctl_register(struct inet6_dev *idev)
  4315. {
  4316. neigh_sysctl_register(idev->dev, idev->nd_parms, "ipv6",
  4317. &ndisc_ifinfo_sysctl_change);
  4318. __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
  4319. idev, &idev->cnf);
  4320. }
  4321. static void addrconf_sysctl_unregister(struct inet6_dev *idev)
  4322. {
  4323. __addrconf_sysctl_unregister(&idev->cnf);
  4324. neigh_sysctl_unregister(idev->nd_parms);
  4325. }
  4326. #endif
  4327. static int __net_init addrconf_init_net(struct net *net)
  4328. {
  4329. int err = -ENOMEM;
  4330. struct ipv6_devconf *all, *dflt;
  4331. all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
  4332. if (all == NULL)
  4333. goto err_alloc_all;
  4334. dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
  4335. if (dflt == NULL)
  4336. goto err_alloc_dflt;
  4337. /* these will be inherited by all namespaces */
  4338. dflt->autoconf = ipv6_defaults.autoconf;
  4339. dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
  4340. net->ipv6.devconf_all = all;
  4341. net->ipv6.devconf_dflt = dflt;
  4342. #ifdef CONFIG_SYSCTL
  4343. err = __addrconf_sysctl_register(net, "all", NULL, all);
  4344. if (err < 0)
  4345. goto err_reg_all;
  4346. err = __addrconf_sysctl_register(net, "default", NULL, dflt);
  4347. if (err < 0)
  4348. goto err_reg_dflt;
  4349. #endif
  4350. return 0;
  4351. #ifdef CONFIG_SYSCTL
  4352. err_reg_dflt:
  4353. __addrconf_sysctl_unregister(all);
  4354. err_reg_all:
  4355. kfree(dflt);
  4356. #endif
  4357. err_alloc_dflt:
  4358. kfree(all);
  4359. err_alloc_all:
  4360. return err;
  4361. }
  4362. static void __net_exit addrconf_exit_net(struct net *net)
  4363. {
  4364. #ifdef CONFIG_SYSCTL
  4365. __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
  4366. __addrconf_sysctl_unregister(net->ipv6.devconf_all);
  4367. #endif
  4368. if (!net_eq(net, &init_net)) {
  4369. kfree(net->ipv6.devconf_dflt);
  4370. kfree(net->ipv6.devconf_all);
  4371. }
  4372. }
  4373. static struct pernet_operations addrconf_ops = {
  4374. .init = addrconf_init_net,
  4375. .exit = addrconf_exit_net,
  4376. };
  4377. static struct rtnl_af_ops inet6_ops = {
  4378. .family = AF_INET6,
  4379. .fill_link_af = inet6_fill_link_af,
  4380. .get_link_af_size = inet6_get_link_af_size,
  4381. .set_link_af = inet6_set_link_af,
  4382. };
  4383. /*
  4384. * Init / cleanup code
  4385. */
  4386. int __init addrconf_init(void)
  4387. {
  4388. int i, err;
  4389. err = ipv6_addr_label_init();
  4390. if (err < 0) {
  4391. pr_crit("%s: cannot initialize default policy table: %d\n",
  4392. __func__, err);
  4393. goto out;
  4394. }
  4395. err = register_pernet_subsys(&addrconf_ops);
  4396. if (err < 0)
  4397. goto out_addrlabel;
  4398. /* The addrconf netdev notifier requires that loopback_dev
  4399. * has it's ipv6 private information allocated and setup
  4400. * before it can bring up and give link-local addresses
  4401. * to other devices which are up.
  4402. *
  4403. * Unfortunately, loopback_dev is not necessarily the first
  4404. * entry in the global dev_base list of net devices. In fact,
  4405. * it is likely to be the very last entry on that list.
  4406. * So this causes the notifier registry below to try and
  4407. * give link-local addresses to all devices besides loopback_dev
  4408. * first, then loopback_dev, which cases all the non-loopback_dev
  4409. * devices to fail to get a link-local address.
  4410. *
  4411. * So, as a temporary fix, allocate the ipv6 structure for
  4412. * loopback_dev first by hand.
  4413. * Longer term, all of the dependencies ipv6 has upon the loopback
  4414. * device and it being up should be removed.
  4415. */
  4416. rtnl_lock();
  4417. if (!ipv6_add_dev(init_net.loopback_dev))
  4418. err = -ENOMEM;
  4419. rtnl_unlock();
  4420. if (err)
  4421. goto errlo;
  4422. for (i = 0; i < IN6_ADDR_HSIZE; i++)
  4423. INIT_HLIST_HEAD(&inet6_addr_lst[i]);
  4424. register_netdevice_notifier(&ipv6_dev_notf);
  4425. addrconf_verify(0);
  4426. err = rtnl_af_register(&inet6_ops);
  4427. if (err < 0)
  4428. goto errout_af;
  4429. err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
  4430. NULL);
  4431. if (err < 0)
  4432. goto errout;
  4433. /* Only the first call to __rtnl_register can fail */
  4434. __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
  4435. __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
  4436. __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
  4437. inet6_dump_ifaddr, NULL);
  4438. __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
  4439. inet6_dump_ifmcaddr, NULL);
  4440. __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
  4441. inet6_dump_ifacaddr, NULL);
  4442. __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
  4443. inet6_netconf_dump_devconf, NULL);
  4444. ipv6_addr_label_rtnl_register();
  4445. return 0;
  4446. errout:
  4447. rtnl_af_unregister(&inet6_ops);
  4448. errout_af:
  4449. unregister_netdevice_notifier(&ipv6_dev_notf);
  4450. errlo:
  4451. unregister_pernet_subsys(&addrconf_ops);
  4452. out_addrlabel:
  4453. ipv6_addr_label_cleanup();
  4454. out:
  4455. return err;
  4456. }
  4457. void addrconf_cleanup(void)
  4458. {
  4459. struct net_device *dev;
  4460. int i;
  4461. unregister_netdevice_notifier(&ipv6_dev_notf);
  4462. unregister_pernet_subsys(&addrconf_ops);
  4463. ipv6_addr_label_cleanup();
  4464. rtnl_lock();
  4465. __rtnl_af_unregister(&inet6_ops);
  4466. /* clean dev list */
  4467. for_each_netdev(&init_net, dev) {
  4468. if (__in6_dev_get(dev) == NULL)
  4469. continue;
  4470. addrconf_ifdown(dev, 1);
  4471. }
  4472. addrconf_ifdown(init_net.loopback_dev, 2);
  4473. /*
  4474. * Check hash table.
  4475. */
  4476. spin_lock_bh(&addrconf_hash_lock);
  4477. for (i = 0; i < IN6_ADDR_HSIZE; i++)
  4478. WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
  4479. spin_unlock_bh(&addrconf_hash_lock);
  4480. del_timer(&addr_chk_timer);
  4481. rtnl_unlock();
  4482. }