addrconf.c 115 KB

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