addrconf.c 115 KB

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