addrconf.c 154 KB

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