addrconf.c 125 KB

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