addrconf.c 132 KB

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