addrconf.c 140 KB

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