addrconf.c 134 KB

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