addrconf.c 130 KB

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