addrconf.c 153 KB

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