addrconf.c 111 KB

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