addrconf.c 124 KB

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