addrconf.c 157 KB

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