addrconf.c 151 KB

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