addrconf.c 115 KB

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