addrconf.c 111 KB

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