addrconf.c 164 KB

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