addrconf.c 144 KB

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