addrconf.c 123 KB

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