addrconf.c 163 KB

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