addrconf.c 136 KB

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