addrconf.c 181 KB

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