bond_main.c 179 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661
  1. // SPDX-License-Identifier: GPL-1.0+
  2. /*
  3. * originally based on the dummy device.
  4. *
  5. * Copyright 1999, Thomas Davis, tadavis@lbl.gov.
  6. * Based on dummy.c, and eql.c devices.
  7. *
  8. * bonding.c: an Ethernet Bonding driver
  9. *
  10. * This is useful to talk to a Cisco EtherChannel compatible equipment:
  11. * Cisco 5500
  12. * Sun Trunking (Solaris)
  13. * Alteon AceDirector Trunks
  14. * Linux Bonding
  15. * and probably many L2 switches ...
  16. *
  17. * How it works:
  18. * ifconfig bond0 ipaddress netmask up
  19. * will setup a network device, with an ip address. No mac address
  20. * will be assigned at this time. The hw mac address will come from
  21. * the first slave bonded to the channel. All slaves will then use
  22. * this hw mac address.
  23. *
  24. * ifconfig bond0 down
  25. * will release all slaves, marking them as down.
  26. *
  27. * ifenslave bond0 eth0
  28. * will attach eth0 to bond0 as a slave. eth0 hw mac address will either
  29. * a: be used as initial mac address
  30. * b: if a hw mac address already is there, eth0's hw mac address
  31. * will then be set from bond0.
  32. *
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/types.h>
  37. #include <linux/fcntl.h>
  38. #include <linux/filter.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/ptrace.h>
  41. #include <linux/ioport.h>
  42. #include <linux/in.h>
  43. #include <net/ip.h>
  44. #include <linux/ip.h>
  45. #include <linux/icmp.h>
  46. #include <linux/icmpv6.h>
  47. #include <linux/tcp.h>
  48. #include <linux/udp.h>
  49. #include <linux/slab.h>
  50. #include <linux/string.h>
  51. #include <linux/init.h>
  52. #include <linux/timer.h>
  53. #include <linux/socket.h>
  54. #include <linux/ctype.h>
  55. #include <linux/inet.h>
  56. #include <linux/bitops.h>
  57. #include <linux/io.h>
  58. #include <asm/dma.h>
  59. #include <linux/uaccess.h>
  60. #include <linux/errno.h>
  61. #include <linux/netdevice.h>
  62. #include <linux/inetdevice.h>
  63. #include <linux/igmp.h>
  64. #include <linux/etherdevice.h>
  65. #include <linux/skbuff.h>
  66. #include <net/sock.h>
  67. #include <linux/rtnetlink.h>
  68. #include <linux/smp.h>
  69. #include <linux/if_ether.h>
  70. #include <net/arp.h>
  71. #include <linux/mii.h>
  72. #include <linux/ethtool.h>
  73. #include <linux/if_vlan.h>
  74. #include <linux/if_bonding.h>
  75. #include <linux/phy.h>
  76. #include <linux/jiffies.h>
  77. #include <linux/preempt.h>
  78. #include <net/route.h>
  79. #include <net/net_namespace.h>
  80. #include <net/netns/generic.h>
  81. #include <net/pkt_sched.h>
  82. #include <linux/rculist.h>
  83. #include <net/flow_dissector.h>
  84. #include <net/xfrm.h>
  85. #include <net/bonding.h>
  86. #include <net/bond_3ad.h>
  87. #include <net/bond_alb.h>
  88. #if IS_ENABLED(CONFIG_TLS_DEVICE)
  89. #include <net/tls.h>
  90. #endif
  91. #include <net/ip6_route.h>
  92. #include <net/netdev_lock.h>
  93. #include <net/xdp.h>
  94. #include "bonding_priv.h"
  95. /*---------------------------- Module parameters ----------------------------*/
  96. /* monitor all links that often (in milliseconds). <=0 disables monitoring */
  97. static int max_bonds = BOND_DEFAULT_MAX_BONDS;
  98. static int tx_queues = BOND_DEFAULT_TX_QUEUES;
  99. static int num_peer_notif = 1;
  100. static int miimon;
  101. static int updelay;
  102. static int downdelay;
  103. static int use_carrier = 1;
  104. static char *mode;
  105. static char *primary;
  106. static char *primary_reselect;
  107. static char *lacp_rate;
  108. static int min_links;
  109. static char *ad_select;
  110. static char *xmit_hash_policy;
  111. static int arp_interval;
  112. static char *arp_ip_target[BOND_MAX_ARP_TARGETS];
  113. static char *arp_validate;
  114. static char *arp_all_targets;
  115. static char *fail_over_mac;
  116. static int all_slaves_active;
  117. static struct bond_params bonding_defaults;
  118. static int resend_igmp = BOND_DEFAULT_RESEND_IGMP;
  119. static int packets_per_slave = 1;
  120. static int lp_interval = BOND_ALB_DEFAULT_LP_INTERVAL;
  121. module_param(max_bonds, int, 0);
  122. MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
  123. module_param(tx_queues, int, 0);
  124. MODULE_PARM_DESC(tx_queues, "Max number of transmit queues (default = 16)");
  125. module_param_named(num_grat_arp, num_peer_notif, int, 0644);
  126. MODULE_PARM_DESC(num_grat_arp, "Number of peer notifications to send on "
  127. "failover event (alias of num_unsol_na)");
  128. module_param_named(num_unsol_na, num_peer_notif, int, 0644);
  129. MODULE_PARM_DESC(num_unsol_na, "Number of peer notifications to send on "
  130. "failover event (alias of num_grat_arp)");
  131. module_param(miimon, int, 0);
  132. MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
  133. module_param(updelay, int, 0);
  134. MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
  135. module_param(downdelay, int, 0);
  136. MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
  137. "in milliseconds");
  138. module_param(use_carrier, int, 0);
  139. MODULE_PARM_DESC(use_carrier, "option obsolete, use_carrier cannot be disabled");
  140. module_param(mode, charp, 0);
  141. MODULE_PARM_DESC(mode, "Mode of operation; 0 for balance-rr, "
  142. "1 for active-backup, 2 for balance-xor, "
  143. "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
  144. "6 for balance-alb");
  145. module_param(primary, charp, 0);
  146. MODULE_PARM_DESC(primary, "Primary network device to use");
  147. module_param(primary_reselect, charp, 0);
  148. MODULE_PARM_DESC(primary_reselect, "Reselect primary slave "
  149. "once it comes up; "
  150. "0 for always (default), "
  151. "1 for only if speed of primary is "
  152. "better, "
  153. "2 for only on active slave "
  154. "failure");
  155. module_param(lacp_rate, charp, 0);
  156. MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner; "
  157. "0 for slow, 1 for fast");
  158. module_param(ad_select, charp, 0);
  159. MODULE_PARM_DESC(ad_select, "802.3ad aggregation selection logic; "
  160. "0 for stable (default), 1 for bandwidth, "
  161. "2 for count");
  162. module_param(min_links, int, 0);
  163. MODULE_PARM_DESC(min_links, "Minimum number of available links before turning on carrier");
  164. module_param(xmit_hash_policy, charp, 0);
  165. MODULE_PARM_DESC(xmit_hash_policy, "balance-alb, balance-tlb, balance-xor, 802.3ad hashing method; "
  166. "0 for layer 2 (default), 1 for layer 3+4, "
  167. "2 for layer 2+3, 3 for encap layer 2+3, "
  168. "4 for encap layer 3+4, 5 for vlan+srcmac");
  169. module_param(arp_interval, int, 0);
  170. MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
  171. module_param_array(arp_ip_target, charp, NULL, 0);
  172. MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
  173. module_param(arp_validate, charp, 0);
  174. MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes; "
  175. "0 for none (default), 1 for active, "
  176. "2 for backup, 3 for all");
  177. module_param(arp_all_targets, charp, 0);
  178. MODULE_PARM_DESC(arp_all_targets, "fail on any/all arp targets timeout; 0 for any (default), 1 for all");
  179. module_param(fail_over_mac, charp, 0);
  180. MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to "
  181. "the same MAC; 0 for none (default), "
  182. "1 for active, 2 for follow");
  183. module_param(all_slaves_active, int, 0);
  184. MODULE_PARM_DESC(all_slaves_active, "Keep all frames received on an interface "
  185. "by setting active flag for all slaves; "
  186. "0 for never (default), 1 for always.");
  187. module_param(resend_igmp, int, 0);
  188. MODULE_PARM_DESC(resend_igmp, "Number of IGMP membership reports to send on "
  189. "link failure");
  190. module_param(packets_per_slave, int, 0);
  191. MODULE_PARM_DESC(packets_per_slave, "Packets to send per slave in balance-rr "
  192. "mode; 0 for a random slave, 1 packet per "
  193. "slave (default), >1 packets per slave.");
  194. module_param(lp_interval, uint, 0);
  195. MODULE_PARM_DESC(lp_interval, "The number of seconds between instances where "
  196. "the bonding driver sends learning packets to "
  197. "each slaves peer switch. The default is 1.");
  198. /*----------------------------- Global variables ----------------------------*/
  199. #ifdef CONFIG_NET_POLL_CONTROLLER
  200. atomic_t netpoll_block_tx = ATOMIC_INIT(0);
  201. #endif
  202. unsigned int bond_net_id __read_mostly;
  203. DEFINE_STATIC_KEY_FALSE(bond_bcast_neigh_enabled);
  204. static const struct flow_dissector_key flow_keys_bonding_keys[] = {
  205. {
  206. .key_id = FLOW_DISSECTOR_KEY_CONTROL,
  207. .offset = offsetof(struct flow_keys, control),
  208. },
  209. {
  210. .key_id = FLOW_DISSECTOR_KEY_BASIC,
  211. .offset = offsetof(struct flow_keys, basic),
  212. },
  213. {
  214. .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
  215. .offset = offsetof(struct flow_keys, addrs.v4addrs),
  216. },
  217. {
  218. .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
  219. .offset = offsetof(struct flow_keys, addrs.v6addrs),
  220. },
  221. {
  222. .key_id = FLOW_DISSECTOR_KEY_TIPC,
  223. .offset = offsetof(struct flow_keys, addrs.tipckey),
  224. },
  225. {
  226. .key_id = FLOW_DISSECTOR_KEY_PORTS,
  227. .offset = offsetof(struct flow_keys, ports),
  228. },
  229. {
  230. .key_id = FLOW_DISSECTOR_KEY_ICMP,
  231. .offset = offsetof(struct flow_keys, icmp),
  232. },
  233. {
  234. .key_id = FLOW_DISSECTOR_KEY_VLAN,
  235. .offset = offsetof(struct flow_keys, vlan),
  236. },
  237. {
  238. .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
  239. .offset = offsetof(struct flow_keys, tags),
  240. },
  241. {
  242. .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
  243. .offset = offsetof(struct flow_keys, keyid),
  244. },
  245. };
  246. static struct flow_dissector flow_keys_bonding __read_mostly;
  247. /*-------------------------- Forward declarations ---------------------------*/
  248. static int bond_init(struct net_device *bond_dev);
  249. static void bond_uninit(struct net_device *bond_dev);
  250. static void bond_get_stats(struct net_device *bond_dev,
  251. struct rtnl_link_stats64 *stats);
  252. static void bond_slave_arr_handler(struct work_struct *work);
  253. static bool bond_time_in_interval(struct bonding *bond, unsigned long last_act,
  254. int mod);
  255. static void bond_netdev_notify_work(struct work_struct *work);
  256. /*---------------------------- General routines -----------------------------*/
  257. const char *bond_mode_name(int mode)
  258. {
  259. static const char *names[] = {
  260. [BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)",
  261. [BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)",
  262. [BOND_MODE_XOR] = "load balancing (xor)",
  263. [BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)",
  264. [BOND_MODE_8023AD] = "IEEE 802.3ad Dynamic link aggregation",
  265. [BOND_MODE_TLB] = "transmit load balancing",
  266. [BOND_MODE_ALB] = "adaptive load balancing",
  267. };
  268. if (mode < BOND_MODE_ROUNDROBIN || mode > BOND_MODE_ALB)
  269. return "unknown";
  270. return names[mode];
  271. }
  272. /**
  273. * bond_dev_queue_xmit - Prepare skb for xmit.
  274. *
  275. * @bond: bond device that got this skb for tx.
  276. * @skb: hw accel VLAN tagged skb to transmit
  277. * @slave_dev: slave that is supposed to xmit this skbuff
  278. */
  279. netdev_tx_t bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb,
  280. struct net_device *slave_dev)
  281. {
  282. skb->dev = slave_dev;
  283. BUILD_BUG_ON(sizeof(skb->queue_mapping) !=
  284. sizeof(qdisc_skb_cb(skb)->slave_dev_queue_mapping));
  285. skb_set_queue_mapping(skb, qdisc_skb_cb(skb)->slave_dev_queue_mapping);
  286. if (unlikely(netpoll_tx_running(bond->dev)))
  287. return bond_netpoll_send_skb(bond_get_slave_by_dev(bond, slave_dev), skb);
  288. return dev_queue_xmit(skb);
  289. }
  290. static bool bond_sk_check(struct bonding *bond)
  291. {
  292. switch (BOND_MODE(bond)) {
  293. case BOND_MODE_8023AD:
  294. case BOND_MODE_XOR:
  295. if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34)
  296. return true;
  297. fallthrough;
  298. default:
  299. return false;
  300. }
  301. }
  302. bool __bond_xdp_check(int mode, int xmit_policy)
  303. {
  304. switch (mode) {
  305. case BOND_MODE_ROUNDROBIN:
  306. case BOND_MODE_ACTIVEBACKUP:
  307. return true;
  308. case BOND_MODE_8023AD:
  309. case BOND_MODE_XOR:
  310. /* vlan+srcmac is not supported with XDP as in most cases the 802.1q
  311. * payload is not in the packet due to hardware offload.
  312. */
  313. if (xmit_policy != BOND_XMIT_POLICY_VLAN_SRCMAC)
  314. return true;
  315. fallthrough;
  316. default:
  317. return false;
  318. }
  319. }
  320. bool bond_xdp_check(struct bonding *bond, int mode)
  321. {
  322. return __bond_xdp_check(mode, bond->params.xmit_policy);
  323. }
  324. /*---------------------------------- VLAN -----------------------------------*/
  325. /* In the following 2 functions, bond_vlan_rx_add_vid and bond_vlan_rx_kill_vid,
  326. * We don't protect the slave list iteration with a lock because:
  327. * a. This operation is performed in IOCTL context,
  328. * b. The operation is protected by the RTNL semaphore in the 8021q code,
  329. * c. Holding a lock with BH disabled while directly calling a base driver
  330. * entry point is generally a BAD idea.
  331. *
  332. * The design of synchronization/protection for this operation in the 8021q
  333. * module is good for one or more VLAN devices over a single physical device
  334. * and cannot be extended for a teaming solution like bonding, so there is a
  335. * potential race condition here where a net device from the vlan group might
  336. * be referenced (either by a base driver or the 8021q code) while it is being
  337. * removed from the system. However, it turns out we're not making matters
  338. * worse, and if it works for regular VLAN usage it will work here too.
  339. */
  340. /**
  341. * bond_vlan_rx_add_vid - Propagates adding an id to slaves
  342. * @bond_dev: bonding net device that got called
  343. * @proto: network protocol ID
  344. * @vid: vlan id being added
  345. */
  346. static int bond_vlan_rx_add_vid(struct net_device *bond_dev,
  347. __be16 proto, u16 vid)
  348. {
  349. struct bonding *bond = netdev_priv(bond_dev);
  350. struct slave *slave, *rollback_slave;
  351. struct list_head *iter;
  352. int res;
  353. bond_for_each_slave(bond, slave, iter) {
  354. res = vlan_vid_add(slave->dev, proto, vid);
  355. if (res)
  356. goto unwind;
  357. }
  358. return 0;
  359. unwind:
  360. /* unwind to the slave that failed */
  361. bond_for_each_slave(bond, rollback_slave, iter) {
  362. if (rollback_slave == slave)
  363. break;
  364. vlan_vid_del(rollback_slave->dev, proto, vid);
  365. }
  366. return res;
  367. }
  368. /**
  369. * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
  370. * @bond_dev: bonding net device that got called
  371. * @proto: network protocol ID
  372. * @vid: vlan id being removed
  373. */
  374. static int bond_vlan_rx_kill_vid(struct net_device *bond_dev,
  375. __be16 proto, u16 vid)
  376. {
  377. struct bonding *bond = netdev_priv(bond_dev);
  378. struct list_head *iter;
  379. struct slave *slave;
  380. bond_for_each_slave(bond, slave, iter)
  381. vlan_vid_del(slave->dev, proto, vid);
  382. if (bond_is_lb(bond))
  383. bond_alb_clear_vlan(bond, vid);
  384. return 0;
  385. }
  386. /*---------------------------------- XFRM -----------------------------------*/
  387. #ifdef CONFIG_XFRM_OFFLOAD
  388. /**
  389. * bond_ipsec_dev - Get active device for IPsec offload
  390. * @xs: pointer to transformer state struct
  391. *
  392. * Context: caller must hold rcu_read_lock.
  393. *
  394. * Return: the device for ipsec offload, or NULL if not exist.
  395. **/
  396. static struct net_device *bond_ipsec_dev(struct xfrm_state *xs)
  397. {
  398. struct net_device *bond_dev = xs->xso.dev;
  399. struct bonding *bond;
  400. struct slave *slave;
  401. bond = netdev_priv(bond_dev);
  402. if (BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP)
  403. return NULL;
  404. slave = rcu_dereference(bond->curr_active_slave);
  405. if (!slave)
  406. return NULL;
  407. if (!xs->xso.real_dev)
  408. return NULL;
  409. if (xs->xso.real_dev != slave->dev)
  410. pr_warn_ratelimited("%s: (slave %s): not same with IPsec offload real dev %s\n",
  411. bond_dev->name, slave->dev->name, xs->xso.real_dev->name);
  412. return slave->dev;
  413. }
  414. /**
  415. * bond_ipsec_add_sa - program device with a security association
  416. * @bond_dev: pointer to the bond net device
  417. * @xs: pointer to transformer state struct
  418. * @extack: extack point to fill failure reason
  419. **/
  420. static int bond_ipsec_add_sa(struct net_device *bond_dev,
  421. struct xfrm_state *xs,
  422. struct netlink_ext_ack *extack)
  423. {
  424. struct net_device *real_dev;
  425. netdevice_tracker tracker;
  426. struct bond_ipsec *ipsec;
  427. struct bonding *bond;
  428. struct slave *slave;
  429. int err;
  430. if (!bond_dev)
  431. return -EINVAL;
  432. rcu_read_lock();
  433. bond = netdev_priv(bond_dev);
  434. slave = rcu_dereference(bond->curr_active_slave);
  435. real_dev = slave ? slave->dev : NULL;
  436. netdev_hold(real_dev, &tracker, GFP_ATOMIC);
  437. rcu_read_unlock();
  438. if (!real_dev) {
  439. err = -ENODEV;
  440. goto out;
  441. }
  442. if (!real_dev->xfrmdev_ops ||
  443. !real_dev->xfrmdev_ops->xdo_dev_state_add ||
  444. netif_is_bond_master(real_dev)) {
  445. NL_SET_ERR_MSG_MOD(extack, "Slave does not support ipsec offload");
  446. err = -EINVAL;
  447. goto out;
  448. }
  449. ipsec = kmalloc_obj(*ipsec);
  450. if (!ipsec) {
  451. err = -ENOMEM;
  452. goto out;
  453. }
  454. err = real_dev->xfrmdev_ops->xdo_dev_state_add(real_dev, xs, extack);
  455. if (!err) {
  456. xs->xso.real_dev = real_dev;
  457. ipsec->xs = xs;
  458. INIT_LIST_HEAD(&ipsec->list);
  459. mutex_lock(&bond->ipsec_lock);
  460. list_add(&ipsec->list, &bond->ipsec_list);
  461. mutex_unlock(&bond->ipsec_lock);
  462. } else {
  463. kfree(ipsec);
  464. }
  465. out:
  466. netdev_put(real_dev, &tracker);
  467. return err;
  468. }
  469. static void bond_ipsec_add_sa_all(struct bonding *bond)
  470. {
  471. struct net_device *bond_dev = bond->dev;
  472. struct net_device *real_dev;
  473. struct bond_ipsec *ipsec;
  474. struct slave *slave;
  475. slave = rtnl_dereference(bond->curr_active_slave);
  476. real_dev = slave ? slave->dev : NULL;
  477. if (!real_dev)
  478. return;
  479. mutex_lock(&bond->ipsec_lock);
  480. if (!real_dev->xfrmdev_ops ||
  481. !real_dev->xfrmdev_ops->xdo_dev_state_add ||
  482. netif_is_bond_master(real_dev)) {
  483. if (!list_empty(&bond->ipsec_list))
  484. slave_warn(bond_dev, real_dev,
  485. "%s: no slave xdo_dev_state_add\n",
  486. __func__);
  487. goto out;
  488. }
  489. list_for_each_entry(ipsec, &bond->ipsec_list, list) {
  490. /* If new state is added before ipsec_lock acquired */
  491. if (ipsec->xs->xso.real_dev == real_dev)
  492. continue;
  493. if (real_dev->xfrmdev_ops->xdo_dev_state_add(real_dev,
  494. ipsec->xs, NULL)) {
  495. slave_warn(bond_dev, real_dev, "%s: failed to add SA\n", __func__);
  496. continue;
  497. }
  498. spin_lock_bh(&ipsec->xs->lock);
  499. /* xs might have been killed by the user during the migration
  500. * to the new dev, but bond_ipsec_del_sa() should have done
  501. * nothing, as xso.real_dev is NULL.
  502. * Delete it from the device we just added it to. The pending
  503. * bond_ipsec_free_sa() call will do the rest of the cleanup.
  504. */
  505. if (ipsec->xs->km.state == XFRM_STATE_DEAD &&
  506. real_dev->xfrmdev_ops->xdo_dev_state_delete)
  507. real_dev->xfrmdev_ops->xdo_dev_state_delete(real_dev,
  508. ipsec->xs);
  509. ipsec->xs->xso.real_dev = real_dev;
  510. spin_unlock_bh(&ipsec->xs->lock);
  511. }
  512. out:
  513. mutex_unlock(&bond->ipsec_lock);
  514. }
  515. /**
  516. * bond_ipsec_del_sa - clear out this specific SA
  517. * @bond_dev: pointer to the bond net device
  518. * @xs: pointer to transformer state struct
  519. **/
  520. static void bond_ipsec_del_sa(struct net_device *bond_dev,
  521. struct xfrm_state *xs)
  522. {
  523. struct net_device *real_dev;
  524. if (!bond_dev || !xs->xso.real_dev)
  525. return;
  526. real_dev = xs->xso.real_dev;
  527. if (!real_dev->xfrmdev_ops ||
  528. !real_dev->xfrmdev_ops->xdo_dev_state_delete ||
  529. netif_is_bond_master(real_dev)) {
  530. slave_warn(bond_dev, real_dev, "%s: no slave xdo_dev_state_delete\n", __func__);
  531. return;
  532. }
  533. real_dev->xfrmdev_ops->xdo_dev_state_delete(real_dev, xs);
  534. }
  535. static void bond_ipsec_del_sa_all(struct bonding *bond)
  536. {
  537. struct net_device *bond_dev = bond->dev;
  538. struct net_device *real_dev;
  539. struct bond_ipsec *ipsec;
  540. struct slave *slave;
  541. slave = rtnl_dereference(bond->curr_active_slave);
  542. real_dev = slave ? slave->dev : NULL;
  543. if (!real_dev)
  544. return;
  545. mutex_lock(&bond->ipsec_lock);
  546. list_for_each_entry(ipsec, &bond->ipsec_list, list) {
  547. if (!ipsec->xs->xso.real_dev)
  548. continue;
  549. if (!real_dev->xfrmdev_ops ||
  550. !real_dev->xfrmdev_ops->xdo_dev_state_delete ||
  551. netif_is_bond_master(real_dev)) {
  552. slave_warn(bond_dev, real_dev,
  553. "%s: no slave xdo_dev_state_delete\n",
  554. __func__);
  555. continue;
  556. }
  557. spin_lock_bh(&ipsec->xs->lock);
  558. ipsec->xs->xso.real_dev = NULL;
  559. /* Don't double delete states killed by the user. */
  560. if (ipsec->xs->km.state != XFRM_STATE_DEAD)
  561. real_dev->xfrmdev_ops->xdo_dev_state_delete(real_dev,
  562. ipsec->xs);
  563. spin_unlock_bh(&ipsec->xs->lock);
  564. if (real_dev->xfrmdev_ops->xdo_dev_state_free)
  565. real_dev->xfrmdev_ops->xdo_dev_state_free(real_dev,
  566. ipsec->xs);
  567. }
  568. mutex_unlock(&bond->ipsec_lock);
  569. }
  570. static void bond_ipsec_free_sa(struct net_device *bond_dev,
  571. struct xfrm_state *xs)
  572. {
  573. struct net_device *real_dev;
  574. struct bond_ipsec *ipsec;
  575. struct bonding *bond;
  576. if (!bond_dev)
  577. return;
  578. bond = netdev_priv(bond_dev);
  579. mutex_lock(&bond->ipsec_lock);
  580. if (!xs->xso.real_dev)
  581. goto out;
  582. real_dev = xs->xso.real_dev;
  583. xs->xso.real_dev = NULL;
  584. if (real_dev->xfrmdev_ops &&
  585. real_dev->xfrmdev_ops->xdo_dev_state_free)
  586. real_dev->xfrmdev_ops->xdo_dev_state_free(real_dev, xs);
  587. out:
  588. list_for_each_entry(ipsec, &bond->ipsec_list, list) {
  589. if (ipsec->xs == xs) {
  590. list_del(&ipsec->list);
  591. kfree(ipsec);
  592. break;
  593. }
  594. }
  595. mutex_unlock(&bond->ipsec_lock);
  596. }
  597. /**
  598. * bond_ipsec_offload_ok - can this packet use the xfrm hw offload
  599. * @skb: current data packet
  600. * @xs: pointer to transformer state struct
  601. **/
  602. static bool bond_ipsec_offload_ok(struct sk_buff *skb, struct xfrm_state *xs)
  603. {
  604. struct net_device *real_dev;
  605. rcu_read_lock();
  606. real_dev = bond_ipsec_dev(xs);
  607. if (!real_dev || netif_is_bond_master(real_dev)) {
  608. rcu_read_unlock();
  609. return false;
  610. }
  611. rcu_read_unlock();
  612. return true;
  613. }
  614. /**
  615. * bond_advance_esn_state - ESN support for IPSec HW offload
  616. * @xs: pointer to transformer state struct
  617. **/
  618. static void bond_advance_esn_state(struct xfrm_state *xs)
  619. {
  620. struct net_device *real_dev;
  621. rcu_read_lock();
  622. real_dev = bond_ipsec_dev(xs);
  623. if (!real_dev)
  624. goto out;
  625. if (!real_dev->xfrmdev_ops ||
  626. !real_dev->xfrmdev_ops->xdo_dev_state_advance_esn) {
  627. pr_warn_ratelimited("%s: %s doesn't support xdo_dev_state_advance_esn\n", __func__, real_dev->name);
  628. goto out;
  629. }
  630. real_dev->xfrmdev_ops->xdo_dev_state_advance_esn(xs);
  631. out:
  632. rcu_read_unlock();
  633. }
  634. /**
  635. * bond_xfrm_update_stats - Update xfrm state
  636. * @xs: pointer to transformer state struct
  637. **/
  638. static void bond_xfrm_update_stats(struct xfrm_state *xs)
  639. {
  640. struct net_device *real_dev;
  641. rcu_read_lock();
  642. real_dev = bond_ipsec_dev(xs);
  643. if (!real_dev)
  644. goto out;
  645. if (!real_dev->xfrmdev_ops ||
  646. !real_dev->xfrmdev_ops->xdo_dev_state_update_stats) {
  647. pr_warn_ratelimited("%s: %s doesn't support xdo_dev_state_update_stats\n", __func__, real_dev->name);
  648. goto out;
  649. }
  650. real_dev->xfrmdev_ops->xdo_dev_state_update_stats(xs);
  651. out:
  652. rcu_read_unlock();
  653. }
  654. static const struct xfrmdev_ops bond_xfrmdev_ops = {
  655. .xdo_dev_state_add = bond_ipsec_add_sa,
  656. .xdo_dev_state_delete = bond_ipsec_del_sa,
  657. .xdo_dev_state_free = bond_ipsec_free_sa,
  658. .xdo_dev_offload_ok = bond_ipsec_offload_ok,
  659. .xdo_dev_state_advance_esn = bond_advance_esn_state,
  660. .xdo_dev_state_update_stats = bond_xfrm_update_stats,
  661. };
  662. #endif /* CONFIG_XFRM_OFFLOAD */
  663. /*------------------------------- Link status -------------------------------*/
  664. /* Set the carrier state for the master according to the state of its
  665. * slaves. If any slaves are up, the master is up. In 802.3ad mode,
  666. * do special 802.3ad magic.
  667. *
  668. * Returns zero if carrier state does not change, nonzero if it does.
  669. */
  670. int bond_set_carrier(struct bonding *bond)
  671. {
  672. struct list_head *iter;
  673. struct slave *slave;
  674. if (!bond_has_slaves(bond))
  675. goto down;
  676. if (BOND_MODE(bond) == BOND_MODE_8023AD)
  677. return bond_3ad_set_carrier(bond);
  678. bond_for_each_slave(bond, slave, iter) {
  679. if (slave->link == BOND_LINK_UP) {
  680. if (!netif_carrier_ok(bond->dev)) {
  681. netif_carrier_on(bond->dev);
  682. return 1;
  683. }
  684. return 0;
  685. }
  686. }
  687. down:
  688. if (netif_carrier_ok(bond->dev)) {
  689. netif_carrier_off(bond->dev);
  690. return 1;
  691. }
  692. return 0;
  693. }
  694. /* Get link speed and duplex from the slave's base driver
  695. * using ethtool. If for some reason the call fails or the
  696. * values are invalid, set speed and duplex to -1,
  697. * and return. Return 1 if speed or duplex settings are
  698. * UNKNOWN; 0 otherwise.
  699. */
  700. static int bond_update_speed_duplex(struct slave *slave)
  701. {
  702. struct net_device *slave_dev = slave->dev;
  703. struct ethtool_link_ksettings ecmd;
  704. int res;
  705. res = __ethtool_get_link_ksettings(slave_dev, &ecmd);
  706. if (res < 0)
  707. goto speed_duplex_unknown;
  708. if (ecmd.base.speed == 0 || ecmd.base.speed == ((__u32)-1))
  709. goto speed_duplex_unknown;
  710. switch (ecmd.base.duplex) {
  711. case DUPLEX_FULL:
  712. case DUPLEX_HALF:
  713. break;
  714. default:
  715. goto speed_duplex_unknown;
  716. }
  717. slave->speed = ecmd.base.speed;
  718. slave->duplex = ecmd.base.duplex;
  719. return 0;
  720. speed_duplex_unknown:
  721. slave->speed = SPEED_UNKNOWN;
  722. slave->duplex = DUPLEX_UNKNOWN;
  723. return 1;
  724. }
  725. const char *bond_slave_link_status(s8 link)
  726. {
  727. switch (link) {
  728. case BOND_LINK_UP:
  729. return "up";
  730. case BOND_LINK_FAIL:
  731. return "going down";
  732. case BOND_LINK_DOWN:
  733. return "down";
  734. case BOND_LINK_BACK:
  735. return "going back";
  736. default:
  737. return "unknown";
  738. }
  739. }
  740. /*----------------------------- Multicast list ------------------------------*/
  741. /* Push the promiscuity flag down to appropriate slaves */
  742. static int bond_set_promiscuity(struct bonding *bond, int inc)
  743. {
  744. struct list_head *iter;
  745. int err = 0;
  746. if (bond_uses_primary(bond)) {
  747. struct slave *curr_active = rtnl_dereference(bond->curr_active_slave);
  748. if (curr_active)
  749. err = dev_set_promiscuity(curr_active->dev, inc);
  750. } else {
  751. struct slave *slave;
  752. bond_for_each_slave(bond, slave, iter) {
  753. err = dev_set_promiscuity(slave->dev, inc);
  754. if (err)
  755. return err;
  756. }
  757. }
  758. return err;
  759. }
  760. /* Push the allmulti flag down to all slaves */
  761. static int bond_set_allmulti(struct bonding *bond, int inc)
  762. {
  763. struct list_head *iter;
  764. int err = 0;
  765. if (bond_uses_primary(bond)) {
  766. struct slave *curr_active = rtnl_dereference(bond->curr_active_slave);
  767. if (curr_active)
  768. err = dev_set_allmulti(curr_active->dev, inc);
  769. } else {
  770. struct slave *slave;
  771. bond_for_each_slave(bond, slave, iter) {
  772. err = dev_set_allmulti(slave->dev, inc);
  773. if (err)
  774. return err;
  775. }
  776. }
  777. return err;
  778. }
  779. /* Retrieve the list of registered multicast addresses for the bonding
  780. * device and retransmit an IGMP JOIN request to the current active
  781. * slave.
  782. */
  783. static void bond_resend_igmp_join_requests_delayed(struct work_struct *work)
  784. {
  785. struct bonding *bond = container_of(work, struct bonding,
  786. mcast_work.work);
  787. if (!rtnl_trylock()) {
  788. queue_delayed_work(bond->wq, &bond->mcast_work, 1);
  789. return;
  790. }
  791. call_netdevice_notifiers(NETDEV_RESEND_IGMP, bond->dev);
  792. if (bond->igmp_retrans > 1) {
  793. bond->igmp_retrans--;
  794. queue_delayed_work(bond->wq, &bond->mcast_work, HZ/5);
  795. }
  796. rtnl_unlock();
  797. }
  798. /* Flush bond's hardware addresses from slave */
  799. static void bond_hw_addr_flush(struct net_device *bond_dev,
  800. struct net_device *slave_dev)
  801. {
  802. struct bonding *bond = netdev_priv(bond_dev);
  803. dev_uc_unsync(slave_dev, bond_dev);
  804. dev_mc_unsync(slave_dev, bond_dev);
  805. if (BOND_MODE(bond) == BOND_MODE_8023AD)
  806. dev_mc_del(slave_dev, lacpdu_mcast_addr);
  807. }
  808. /*--------------------------- Active slave change ---------------------------*/
  809. /* Update the hardware address list and promisc/allmulti for the new and
  810. * old active slaves (if any). Modes that are not using primary keep all
  811. * slaves up date at all times; only the modes that use primary need to call
  812. * this function to swap these settings during a failover.
  813. */
  814. static void bond_hw_addr_swap(struct bonding *bond, struct slave *new_active,
  815. struct slave *old_active)
  816. {
  817. if (old_active) {
  818. if (bond->dev->flags & IFF_PROMISC)
  819. dev_set_promiscuity(old_active->dev, -1);
  820. if (bond->dev->flags & IFF_ALLMULTI)
  821. dev_set_allmulti(old_active->dev, -1);
  822. if (bond->dev->flags & IFF_UP)
  823. bond_hw_addr_flush(bond->dev, old_active->dev);
  824. bond_slave_ns_maddrs_add(bond, old_active);
  825. }
  826. if (new_active) {
  827. /* FIXME: Signal errors upstream. */
  828. if (bond->dev->flags & IFF_PROMISC)
  829. dev_set_promiscuity(new_active->dev, 1);
  830. if (bond->dev->flags & IFF_ALLMULTI)
  831. dev_set_allmulti(new_active->dev, 1);
  832. if (bond->dev->flags & IFF_UP) {
  833. netif_addr_lock_bh(bond->dev);
  834. dev_uc_sync(new_active->dev, bond->dev);
  835. dev_mc_sync(new_active->dev, bond->dev);
  836. netif_addr_unlock_bh(bond->dev);
  837. }
  838. bond_slave_ns_maddrs_del(bond, new_active);
  839. }
  840. }
  841. /**
  842. * bond_set_dev_addr - clone slave's address to bond
  843. * @bond_dev: bond net device
  844. * @slave_dev: slave net device
  845. *
  846. * Should be called with RTNL held.
  847. */
  848. static int bond_set_dev_addr(struct net_device *bond_dev,
  849. struct net_device *slave_dev)
  850. {
  851. int err;
  852. slave_dbg(bond_dev, slave_dev, "bond_dev=%p slave_dev=%p slave_dev->addr_len=%d\n",
  853. bond_dev, slave_dev, slave_dev->addr_len);
  854. err = netif_pre_changeaddr_notify(bond_dev, slave_dev->dev_addr, NULL);
  855. if (err)
  856. return err;
  857. __dev_addr_set(bond_dev, slave_dev->dev_addr, slave_dev->addr_len);
  858. bond_dev->addr_assign_type = NET_ADDR_STOLEN;
  859. call_netdevice_notifiers(NETDEV_CHANGEADDR, bond_dev);
  860. return 0;
  861. }
  862. static struct slave *bond_get_old_active(struct bonding *bond,
  863. struct slave *new_active)
  864. {
  865. struct slave *slave;
  866. struct list_head *iter;
  867. bond_for_each_slave(bond, slave, iter) {
  868. if (slave == new_active)
  869. continue;
  870. if (ether_addr_equal(bond->dev->dev_addr, slave->dev->dev_addr))
  871. return slave;
  872. }
  873. return NULL;
  874. }
  875. /* bond_do_fail_over_mac
  876. *
  877. * Perform special MAC address swapping for fail_over_mac settings
  878. *
  879. * Called with RTNL
  880. */
  881. static void bond_do_fail_over_mac(struct bonding *bond,
  882. struct slave *new_active,
  883. struct slave *old_active)
  884. {
  885. u8 tmp_mac[MAX_ADDR_LEN];
  886. struct sockaddr_storage ss;
  887. int rv;
  888. switch (bond->params.fail_over_mac) {
  889. case BOND_FOM_ACTIVE:
  890. if (new_active) {
  891. rv = bond_set_dev_addr(bond->dev, new_active->dev);
  892. if (rv)
  893. slave_err(bond->dev, new_active->dev, "Error %d setting bond MAC from slave\n",
  894. -rv);
  895. }
  896. break;
  897. case BOND_FOM_FOLLOW:
  898. /* if new_active && old_active, swap them
  899. * if just old_active, do nothing (going to no active slave)
  900. * if just new_active, set new_active to bond's MAC
  901. */
  902. if (!new_active)
  903. return;
  904. if (!old_active)
  905. old_active = bond_get_old_active(bond, new_active);
  906. if (old_active) {
  907. bond_hw_addr_copy(tmp_mac, new_active->dev->dev_addr,
  908. new_active->dev->addr_len);
  909. bond_hw_addr_copy(ss.__data,
  910. old_active->dev->dev_addr,
  911. old_active->dev->addr_len);
  912. ss.ss_family = new_active->dev->type;
  913. } else {
  914. bond_hw_addr_copy(ss.__data, bond->dev->dev_addr,
  915. bond->dev->addr_len);
  916. ss.ss_family = bond->dev->type;
  917. }
  918. rv = dev_set_mac_address(new_active->dev, &ss, NULL);
  919. if (rv) {
  920. slave_err(bond->dev, new_active->dev, "Error %d setting MAC of new active slave\n",
  921. -rv);
  922. goto out;
  923. }
  924. if (!old_active)
  925. goto out;
  926. bond_hw_addr_copy(ss.__data, tmp_mac,
  927. new_active->dev->addr_len);
  928. ss.ss_family = old_active->dev->type;
  929. rv = dev_set_mac_address(old_active->dev, &ss, NULL);
  930. if (rv)
  931. slave_err(bond->dev, old_active->dev, "Error %d setting MAC of old active slave\n",
  932. -rv);
  933. out:
  934. break;
  935. default:
  936. netdev_err(bond->dev, "bond_do_fail_over_mac impossible: bad policy %d\n",
  937. bond->params.fail_over_mac);
  938. break;
  939. }
  940. }
  941. /**
  942. * bond_choose_primary_or_current - select the primary or high priority slave
  943. * @bond: our bonding struct
  944. *
  945. * - Check if there is a primary link. If the primary link was set and is up,
  946. * go on and do link reselection.
  947. *
  948. * - If primary link is not set or down, find the highest priority link.
  949. * If the highest priority link is not current slave, set it as primary
  950. * link and do link reselection.
  951. */
  952. static struct slave *bond_choose_primary_or_current(struct bonding *bond)
  953. {
  954. struct slave *prim = rtnl_dereference(bond->primary_slave);
  955. struct slave *curr = rtnl_dereference(bond->curr_active_slave);
  956. struct slave *slave, *hprio = NULL;
  957. struct list_head *iter;
  958. if (!prim || prim->link != BOND_LINK_UP) {
  959. bond_for_each_slave(bond, slave, iter) {
  960. if (slave->link == BOND_LINK_UP) {
  961. hprio = hprio ?: slave;
  962. if (slave->prio > hprio->prio)
  963. hprio = slave;
  964. }
  965. }
  966. if (hprio && hprio != curr) {
  967. prim = hprio;
  968. goto link_reselect;
  969. }
  970. if (!curr || curr->link != BOND_LINK_UP)
  971. return NULL;
  972. return curr;
  973. }
  974. if (bond->force_primary) {
  975. bond->force_primary = false;
  976. return prim;
  977. }
  978. link_reselect:
  979. if (!curr || curr->link != BOND_LINK_UP)
  980. return prim;
  981. /* At this point, prim and curr are both up */
  982. switch (bond->params.primary_reselect) {
  983. case BOND_PRI_RESELECT_ALWAYS:
  984. return prim;
  985. case BOND_PRI_RESELECT_BETTER:
  986. if (prim->speed < curr->speed)
  987. return curr;
  988. if (prim->speed == curr->speed && prim->duplex <= curr->duplex)
  989. return curr;
  990. return prim;
  991. case BOND_PRI_RESELECT_FAILURE:
  992. return curr;
  993. default:
  994. netdev_err(bond->dev, "impossible primary_reselect %d\n",
  995. bond->params.primary_reselect);
  996. return curr;
  997. }
  998. }
  999. /**
  1000. * bond_find_best_slave - select the best available slave to be the active one
  1001. * @bond: our bonding struct
  1002. */
  1003. static struct slave *bond_find_best_slave(struct bonding *bond)
  1004. {
  1005. struct slave *slave, *bestslave = NULL;
  1006. struct list_head *iter;
  1007. int mintime = bond->params.updelay;
  1008. slave = bond_choose_primary_or_current(bond);
  1009. if (slave)
  1010. return slave;
  1011. bond_for_each_slave(bond, slave, iter) {
  1012. if (slave->link == BOND_LINK_UP)
  1013. return slave;
  1014. if (slave->link == BOND_LINK_BACK && bond_slave_is_up(slave) &&
  1015. slave->delay < mintime) {
  1016. mintime = slave->delay;
  1017. bestslave = slave;
  1018. }
  1019. }
  1020. return bestslave;
  1021. }
  1022. /* must be called in RCU critical section or with RTNL held */
  1023. static bool bond_should_notify_peers(struct bonding *bond)
  1024. {
  1025. struct bond_up_slave *usable;
  1026. struct slave *slave = NULL;
  1027. if (!bond->send_peer_notif ||
  1028. bond->send_peer_notif %
  1029. max(1, bond->params.peer_notif_delay) != 0 ||
  1030. !netif_carrier_ok(bond->dev))
  1031. return false;
  1032. /* The send_peer_notif is set by active-backup or 8023ad
  1033. * mode, and cleared in bond_close() when changing mode.
  1034. * It is safe to only check bond mode here.
  1035. */
  1036. if (BOND_MODE(bond) == BOND_MODE_8023AD) {
  1037. usable = rcu_dereference_rtnl(bond->usable_slaves);
  1038. if (!usable || !READ_ONCE(usable->count))
  1039. return false;
  1040. } else {
  1041. slave = rcu_dereference_rtnl(bond->curr_active_slave);
  1042. if (!slave || test_bit(__LINK_STATE_LINKWATCH_PENDING,
  1043. &slave->dev->state))
  1044. return false;
  1045. }
  1046. netdev_dbg(bond->dev, "bond_should_notify_peers: slave %s\n",
  1047. slave ? slave->dev->name : "all");
  1048. return true;
  1049. }
  1050. /* Use this to update send_peer_notif when RTNL may be held in other places. */
  1051. void bond_peer_notify_work_rearm(struct bonding *bond, unsigned long delay)
  1052. {
  1053. queue_delayed_work(bond->wq, &bond->peer_notify_work, delay);
  1054. }
  1055. /* Peer notify update handler. Holds only RTNL */
  1056. static void bond_peer_notify_reset(struct bonding *bond)
  1057. {
  1058. WRITE_ONCE(bond->send_peer_notif,
  1059. bond->params.num_peer_notif *
  1060. max(1, bond->params.peer_notif_delay));
  1061. }
  1062. static void bond_peer_notify_handler(struct work_struct *work)
  1063. {
  1064. struct bonding *bond = container_of(work, struct bonding,
  1065. peer_notify_work.work);
  1066. if (!rtnl_trylock()) {
  1067. bond_peer_notify_work_rearm(bond, 1);
  1068. return;
  1069. }
  1070. bond_peer_notify_reset(bond);
  1071. rtnl_unlock();
  1072. }
  1073. /* Peer notify events post. Holds only RTNL */
  1074. static void bond_peer_notify_may_events(struct bonding *bond, bool force)
  1075. {
  1076. bool notified = false;
  1077. if (bond_should_notify_peers(bond)) {
  1078. notified = true;
  1079. call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, bond->dev);
  1080. }
  1081. if (notified || force)
  1082. bond->send_peer_notif--;
  1083. }
  1084. /**
  1085. * bond_change_active_slave - change the active slave into the specified one
  1086. * @bond: our bonding struct
  1087. * @new_active: the new slave to make the active one
  1088. *
  1089. * Set the new slave to the bond's settings and unset them on the old
  1090. * curr_active_slave.
  1091. * Setting include flags, mc-list, promiscuity, allmulti, etc.
  1092. *
  1093. * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
  1094. * because it is apparently the best available slave we have, even though its
  1095. * updelay hasn't timed out yet.
  1096. *
  1097. * Caller must hold RTNL.
  1098. */
  1099. void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
  1100. {
  1101. struct slave *old_active;
  1102. ASSERT_RTNL();
  1103. old_active = rtnl_dereference(bond->curr_active_slave);
  1104. if (old_active == new_active)
  1105. return;
  1106. #ifdef CONFIG_XFRM_OFFLOAD
  1107. bond_ipsec_del_sa_all(bond);
  1108. #endif /* CONFIG_XFRM_OFFLOAD */
  1109. if (new_active) {
  1110. new_active->last_link_up = jiffies;
  1111. if (new_active->link == BOND_LINK_BACK) {
  1112. if (bond_uses_primary(bond)) {
  1113. slave_info(bond->dev, new_active->dev, "making interface the new active one %d ms earlier\n",
  1114. (bond->params.updelay - new_active->delay) * bond->params.miimon);
  1115. }
  1116. new_active->delay = 0;
  1117. bond_set_slave_link_state(new_active, BOND_LINK_UP,
  1118. BOND_SLAVE_NOTIFY_NOW);
  1119. if (BOND_MODE(bond) == BOND_MODE_8023AD)
  1120. bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
  1121. if (bond_is_lb(bond))
  1122. bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
  1123. } else {
  1124. if (bond_uses_primary(bond))
  1125. slave_info(bond->dev, new_active->dev, "making interface the new active one\n");
  1126. }
  1127. }
  1128. if (bond_uses_primary(bond))
  1129. bond_hw_addr_swap(bond, new_active, old_active);
  1130. if (bond_is_lb(bond)) {
  1131. bond_alb_handle_active_change(bond, new_active);
  1132. if (old_active)
  1133. bond_set_slave_inactive_flags(old_active,
  1134. BOND_SLAVE_NOTIFY_NOW);
  1135. if (new_active)
  1136. bond_set_slave_active_flags(new_active,
  1137. BOND_SLAVE_NOTIFY_NOW);
  1138. } else {
  1139. rcu_assign_pointer(bond->curr_active_slave, new_active);
  1140. }
  1141. if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP) {
  1142. if (old_active)
  1143. bond_set_slave_inactive_flags(old_active,
  1144. BOND_SLAVE_NOTIFY_NOW);
  1145. if (new_active) {
  1146. bond_set_slave_active_flags(new_active,
  1147. BOND_SLAVE_NOTIFY_NOW);
  1148. if (bond->params.fail_over_mac)
  1149. bond_do_fail_over_mac(bond, new_active,
  1150. old_active);
  1151. call_netdevice_notifiers(NETDEV_BONDING_FAILOVER, bond->dev);
  1152. if (netif_running(bond->dev)) {
  1153. bond_peer_notify_reset(bond);
  1154. bond_peer_notify_may_events(bond, false);
  1155. }
  1156. }
  1157. }
  1158. #ifdef CONFIG_XFRM_OFFLOAD
  1159. bond_ipsec_add_sa_all(bond);
  1160. #endif /* CONFIG_XFRM_OFFLOAD */
  1161. /* resend IGMP joins since active slave has changed or
  1162. * all were sent on curr_active_slave.
  1163. * resend only if bond is brought up with the affected
  1164. * bonding modes and the retransmission is enabled
  1165. */
  1166. if (netif_running(bond->dev) && (bond->params.resend_igmp > 0) &&
  1167. ((bond_uses_primary(bond) && new_active) ||
  1168. BOND_MODE(bond) == BOND_MODE_ROUNDROBIN)) {
  1169. bond->igmp_retrans = bond->params.resend_igmp;
  1170. queue_delayed_work(bond->wq, &bond->mcast_work, 1);
  1171. }
  1172. }
  1173. /**
  1174. * bond_select_active_slave - select a new active slave, if needed
  1175. * @bond: our bonding struct
  1176. *
  1177. * This functions should be called when one of the following occurs:
  1178. * - The old curr_active_slave has been released or lost its link.
  1179. * - The primary_slave has got its link back.
  1180. * - A slave has got its link back and there's no old curr_active_slave.
  1181. *
  1182. * Caller must hold RTNL.
  1183. */
  1184. void bond_select_active_slave(struct bonding *bond)
  1185. {
  1186. struct slave *best_slave;
  1187. int rv;
  1188. ASSERT_RTNL();
  1189. best_slave = bond_find_best_slave(bond);
  1190. if (best_slave != rtnl_dereference(bond->curr_active_slave)) {
  1191. bond_change_active_slave(bond, best_slave);
  1192. rv = bond_set_carrier(bond);
  1193. if (!rv)
  1194. return;
  1195. if (netif_carrier_ok(bond->dev))
  1196. netdev_info(bond->dev, "active interface up!\n");
  1197. else
  1198. netdev_info(bond->dev, "now running without any active interface!\n");
  1199. }
  1200. }
  1201. #ifdef CONFIG_NET_POLL_CONTROLLER
  1202. static inline int slave_enable_netpoll(struct slave *slave)
  1203. {
  1204. struct netpoll *np;
  1205. int err = 0;
  1206. np = kzalloc_obj(*np);
  1207. err = -ENOMEM;
  1208. if (!np)
  1209. goto out;
  1210. err = __netpoll_setup(np, slave->dev);
  1211. if (err) {
  1212. kfree(np);
  1213. goto out;
  1214. }
  1215. slave->np = np;
  1216. out:
  1217. return err;
  1218. }
  1219. static inline void slave_disable_netpoll(struct slave *slave)
  1220. {
  1221. struct netpoll *np = slave->np;
  1222. if (!np)
  1223. return;
  1224. slave->np = NULL;
  1225. __netpoll_free(np);
  1226. }
  1227. static void bond_poll_controller(struct net_device *bond_dev)
  1228. {
  1229. struct bonding *bond = netdev_priv(bond_dev);
  1230. struct slave *slave = NULL;
  1231. struct list_head *iter;
  1232. struct ad_info ad_info;
  1233. if (BOND_MODE(bond) == BOND_MODE_8023AD)
  1234. if (bond_3ad_get_active_agg_info(bond, &ad_info))
  1235. return;
  1236. bond_for_each_slave_rcu(bond, slave, iter) {
  1237. if (!bond_slave_is_up(slave))
  1238. continue;
  1239. if (BOND_MODE(bond) == BOND_MODE_8023AD) {
  1240. struct aggregator *agg =
  1241. SLAVE_AD_INFO(slave)->port.aggregator;
  1242. if (agg &&
  1243. agg->aggregator_identifier != ad_info.aggregator_id)
  1244. continue;
  1245. }
  1246. netpoll_poll_dev(slave->dev);
  1247. }
  1248. }
  1249. static void bond_netpoll_cleanup(struct net_device *bond_dev)
  1250. {
  1251. struct bonding *bond = netdev_priv(bond_dev);
  1252. struct list_head *iter;
  1253. struct slave *slave;
  1254. bond_for_each_slave(bond, slave, iter)
  1255. if (bond_slave_is_up(slave))
  1256. slave_disable_netpoll(slave);
  1257. }
  1258. static int bond_netpoll_setup(struct net_device *dev)
  1259. {
  1260. struct bonding *bond = netdev_priv(dev);
  1261. struct list_head *iter;
  1262. struct slave *slave;
  1263. int err = 0;
  1264. bond_for_each_slave(bond, slave, iter) {
  1265. err = slave_enable_netpoll(slave);
  1266. if (err) {
  1267. bond_netpoll_cleanup(dev);
  1268. break;
  1269. }
  1270. }
  1271. return err;
  1272. }
  1273. #else
  1274. static inline int slave_enable_netpoll(struct slave *slave)
  1275. {
  1276. return 0;
  1277. }
  1278. static inline void slave_disable_netpoll(struct slave *slave)
  1279. {
  1280. }
  1281. static void bond_netpoll_cleanup(struct net_device *bond_dev)
  1282. {
  1283. }
  1284. #endif
  1285. /*---------------------------------- IOCTL ----------------------------------*/
  1286. static netdev_features_t bond_fix_features(struct net_device *dev,
  1287. netdev_features_t features)
  1288. {
  1289. struct bonding *bond = netdev_priv(dev);
  1290. struct list_head *iter;
  1291. netdev_features_t mask;
  1292. struct slave *slave;
  1293. mask = features;
  1294. features = netdev_base_features(features);
  1295. bond_for_each_slave(bond, slave, iter) {
  1296. features = netdev_increment_features(features,
  1297. slave->dev->features,
  1298. mask);
  1299. }
  1300. features = netdev_add_tso_features(features, mask);
  1301. return features;
  1302. }
  1303. static int bond_header_create(struct sk_buff *skb, struct net_device *bond_dev,
  1304. unsigned short type, const void *daddr,
  1305. const void *saddr, unsigned int len)
  1306. {
  1307. struct bonding *bond = netdev_priv(bond_dev);
  1308. const struct header_ops *slave_ops;
  1309. struct slave *slave;
  1310. int ret = 0;
  1311. rcu_read_lock();
  1312. slave = rcu_dereference(bond->curr_active_slave);
  1313. if (slave) {
  1314. slave_ops = READ_ONCE(slave->dev->header_ops);
  1315. if (slave_ops && slave_ops->create)
  1316. ret = slave_ops->create(skb, slave->dev,
  1317. type, daddr, saddr, len);
  1318. }
  1319. rcu_read_unlock();
  1320. return ret;
  1321. }
  1322. static int bond_header_parse(const struct sk_buff *skb,
  1323. const struct net_device *dev,
  1324. unsigned char *haddr)
  1325. {
  1326. struct bonding *bond = netdev_priv(dev);
  1327. const struct header_ops *slave_ops;
  1328. struct slave *slave;
  1329. int ret = 0;
  1330. rcu_read_lock();
  1331. slave = rcu_dereference(bond->curr_active_slave);
  1332. if (slave) {
  1333. slave_ops = READ_ONCE(slave->dev->header_ops);
  1334. if (slave_ops && slave_ops->parse)
  1335. ret = slave_ops->parse(skb, slave->dev, haddr);
  1336. }
  1337. rcu_read_unlock();
  1338. return ret;
  1339. }
  1340. static const struct header_ops bond_header_ops = {
  1341. .create = bond_header_create,
  1342. .parse = bond_header_parse,
  1343. };
  1344. static void bond_setup_by_slave(struct net_device *bond_dev,
  1345. struct net_device *slave_dev)
  1346. {
  1347. bool was_up = !!(bond_dev->flags & IFF_UP);
  1348. dev_close(bond_dev);
  1349. bond_dev->header_ops = slave_dev->header_ops ?
  1350. &bond_header_ops : NULL;
  1351. bond_dev->type = slave_dev->type;
  1352. bond_dev->hard_header_len = slave_dev->hard_header_len;
  1353. bond_dev->needed_headroom = slave_dev->needed_headroom;
  1354. bond_dev->addr_len = slave_dev->addr_len;
  1355. memcpy(bond_dev->broadcast, slave_dev->broadcast,
  1356. slave_dev->addr_len);
  1357. if (slave_dev->flags & IFF_POINTOPOINT) {
  1358. bond_dev->flags &= ~(IFF_BROADCAST | IFF_MULTICAST);
  1359. bond_dev->flags |= (IFF_POINTOPOINT | IFF_NOARP);
  1360. }
  1361. if (was_up)
  1362. dev_open(bond_dev, NULL);
  1363. }
  1364. /* On bonding slaves other than the currently active slave, suppress
  1365. * duplicates except for alb non-mcast/bcast.
  1366. */
  1367. static bool bond_should_deliver_exact_match(struct sk_buff *skb,
  1368. struct slave *slave,
  1369. struct bonding *bond)
  1370. {
  1371. if (bond_is_slave_inactive(slave)) {
  1372. if (BOND_MODE(bond) == BOND_MODE_ALB &&
  1373. skb->pkt_type != PACKET_BROADCAST &&
  1374. skb->pkt_type != PACKET_MULTICAST)
  1375. return false;
  1376. return true;
  1377. }
  1378. return false;
  1379. }
  1380. static rx_handler_result_t bond_handle_frame(struct sk_buff **pskb)
  1381. {
  1382. struct sk_buff *skb = *pskb;
  1383. struct slave *slave;
  1384. struct bonding *bond;
  1385. int (*recv_probe)(const struct sk_buff *, struct bonding *,
  1386. struct slave *);
  1387. int ret = RX_HANDLER_ANOTHER;
  1388. skb = skb_share_check(skb, GFP_ATOMIC);
  1389. if (unlikely(!skb))
  1390. return RX_HANDLER_CONSUMED;
  1391. *pskb = skb;
  1392. slave = bond_slave_get_rcu(skb->dev);
  1393. bond = slave->bond;
  1394. recv_probe = READ_ONCE(bond->recv_probe);
  1395. if (recv_probe) {
  1396. ret = recv_probe(skb, bond, slave);
  1397. if (ret == RX_HANDLER_CONSUMED) {
  1398. consume_skb(skb);
  1399. return ret;
  1400. }
  1401. }
  1402. /*
  1403. * For packets determined by bond_should_deliver_exact_match() call to
  1404. * be suppressed we want to make an exception for link-local packets.
  1405. * This is necessary for e.g. LLDP daemons to be able to monitor
  1406. * inactive slave links without being forced to bind to them
  1407. * explicitly.
  1408. *
  1409. * At the same time, packets that are passed to the bonding master
  1410. * (including link-local ones) can have their originating interface
  1411. * determined via PACKET_ORIGDEV socket option.
  1412. */
  1413. if (bond_should_deliver_exact_match(skb, slave, bond)) {
  1414. if (is_link_local_ether_addr(eth_hdr(skb)->h_dest))
  1415. return RX_HANDLER_PASS;
  1416. return RX_HANDLER_EXACT;
  1417. }
  1418. skb->dev = bond->dev;
  1419. if (BOND_MODE(bond) == BOND_MODE_ALB &&
  1420. netif_is_bridge_port(bond->dev) &&
  1421. skb->pkt_type == PACKET_HOST) {
  1422. if (unlikely(skb_cow_head(skb,
  1423. skb->data - skb_mac_header(skb)))) {
  1424. kfree_skb(skb);
  1425. return RX_HANDLER_CONSUMED;
  1426. }
  1427. bond_hw_addr_copy(eth_hdr(skb)->h_dest, bond->dev->dev_addr,
  1428. bond->dev->addr_len);
  1429. }
  1430. return ret;
  1431. }
  1432. static enum netdev_lag_tx_type bond_lag_tx_type(struct bonding *bond)
  1433. {
  1434. switch (BOND_MODE(bond)) {
  1435. case BOND_MODE_ROUNDROBIN:
  1436. return NETDEV_LAG_TX_TYPE_ROUNDROBIN;
  1437. case BOND_MODE_ACTIVEBACKUP:
  1438. return NETDEV_LAG_TX_TYPE_ACTIVEBACKUP;
  1439. case BOND_MODE_BROADCAST:
  1440. return NETDEV_LAG_TX_TYPE_BROADCAST;
  1441. case BOND_MODE_XOR:
  1442. case BOND_MODE_8023AD:
  1443. return NETDEV_LAG_TX_TYPE_HASH;
  1444. default:
  1445. return NETDEV_LAG_TX_TYPE_UNKNOWN;
  1446. }
  1447. }
  1448. static enum netdev_lag_hash bond_lag_hash_type(struct bonding *bond,
  1449. enum netdev_lag_tx_type type)
  1450. {
  1451. if (type != NETDEV_LAG_TX_TYPE_HASH)
  1452. return NETDEV_LAG_HASH_NONE;
  1453. switch (bond->params.xmit_policy) {
  1454. case BOND_XMIT_POLICY_LAYER2:
  1455. return NETDEV_LAG_HASH_L2;
  1456. case BOND_XMIT_POLICY_LAYER34:
  1457. return NETDEV_LAG_HASH_L34;
  1458. case BOND_XMIT_POLICY_LAYER23:
  1459. return NETDEV_LAG_HASH_L23;
  1460. case BOND_XMIT_POLICY_ENCAP23:
  1461. return NETDEV_LAG_HASH_E23;
  1462. case BOND_XMIT_POLICY_ENCAP34:
  1463. return NETDEV_LAG_HASH_E34;
  1464. case BOND_XMIT_POLICY_VLAN_SRCMAC:
  1465. return NETDEV_LAG_HASH_VLAN_SRCMAC;
  1466. default:
  1467. return NETDEV_LAG_HASH_UNKNOWN;
  1468. }
  1469. }
  1470. static int bond_master_upper_dev_link(struct bonding *bond, struct slave *slave,
  1471. struct netlink_ext_ack *extack)
  1472. {
  1473. struct netdev_lag_upper_info lag_upper_info;
  1474. enum netdev_lag_tx_type type;
  1475. int err;
  1476. type = bond_lag_tx_type(bond);
  1477. lag_upper_info.tx_type = type;
  1478. lag_upper_info.hash_type = bond_lag_hash_type(bond, type);
  1479. err = netdev_master_upper_dev_link(slave->dev, bond->dev, slave,
  1480. &lag_upper_info, extack);
  1481. if (err)
  1482. return err;
  1483. slave->dev->flags |= IFF_SLAVE;
  1484. return 0;
  1485. }
  1486. static void bond_upper_dev_unlink(struct bonding *bond, struct slave *slave)
  1487. {
  1488. netdev_upper_dev_unlink(slave->dev, bond->dev);
  1489. slave->dev->flags &= ~IFF_SLAVE;
  1490. }
  1491. static void slave_kobj_release(struct kobject *kobj)
  1492. {
  1493. struct slave *slave = to_slave(kobj);
  1494. struct bonding *bond = bond_get_bond_by_slave(slave);
  1495. cancel_delayed_work_sync(&slave->notify_work);
  1496. if (BOND_MODE(bond) == BOND_MODE_8023AD)
  1497. kfree(SLAVE_AD_INFO(slave));
  1498. kfree(slave);
  1499. }
  1500. static struct kobj_type slave_ktype = {
  1501. .release = slave_kobj_release,
  1502. #ifdef CONFIG_SYSFS
  1503. .sysfs_ops = &slave_sysfs_ops,
  1504. #endif
  1505. };
  1506. static int bond_kobj_init(struct slave *slave)
  1507. {
  1508. int err;
  1509. err = kobject_init_and_add(&slave->kobj, &slave_ktype,
  1510. &(slave->dev->dev.kobj), "bonding_slave");
  1511. if (err)
  1512. kobject_put(&slave->kobj);
  1513. return err;
  1514. }
  1515. static struct slave *bond_alloc_slave(struct bonding *bond,
  1516. struct net_device *slave_dev)
  1517. {
  1518. struct slave *slave = NULL;
  1519. slave = kzalloc_obj(*slave);
  1520. if (!slave)
  1521. return NULL;
  1522. slave->bond = bond;
  1523. slave->dev = slave_dev;
  1524. INIT_DELAYED_WORK(&slave->notify_work, bond_netdev_notify_work);
  1525. if (bond_kobj_init(slave))
  1526. return NULL;
  1527. if (BOND_MODE(bond) == BOND_MODE_8023AD) {
  1528. SLAVE_AD_INFO(slave) = kzalloc_obj(struct ad_slave_info);
  1529. if (!SLAVE_AD_INFO(slave)) {
  1530. kobject_put(&slave->kobj);
  1531. return NULL;
  1532. }
  1533. }
  1534. return slave;
  1535. }
  1536. static void bond_fill_ifbond(struct bonding *bond, struct ifbond *info)
  1537. {
  1538. info->bond_mode = BOND_MODE(bond);
  1539. info->miimon = bond->params.miimon;
  1540. info->num_slaves = bond->slave_cnt;
  1541. }
  1542. static void bond_fill_ifslave(struct slave *slave, struct ifslave *info)
  1543. {
  1544. strcpy(info->slave_name, slave->dev->name);
  1545. info->link = slave->link;
  1546. info->state = bond_slave_state(slave);
  1547. info->link_failure_count = slave->link_failure_count;
  1548. }
  1549. static void bond_netdev_notify_work(struct work_struct *_work)
  1550. {
  1551. struct slave *slave = container_of(_work, struct slave,
  1552. notify_work.work);
  1553. if (rtnl_trylock()) {
  1554. struct netdev_bonding_info binfo;
  1555. bond_fill_ifslave(slave, &binfo.slave);
  1556. bond_fill_ifbond(slave->bond, &binfo.master);
  1557. netdev_bonding_info_change(slave->dev, &binfo);
  1558. rtnl_unlock();
  1559. } else {
  1560. queue_delayed_work(slave->bond->wq, &slave->notify_work, 1);
  1561. }
  1562. }
  1563. void bond_queue_slave_event(struct slave *slave)
  1564. {
  1565. queue_delayed_work(slave->bond->wq, &slave->notify_work, 0);
  1566. }
  1567. void bond_lower_state_changed(struct slave *slave)
  1568. {
  1569. struct netdev_lag_lower_state_info info;
  1570. info.link_up = slave->link == BOND_LINK_UP ||
  1571. slave->link == BOND_LINK_FAIL;
  1572. info.tx_enabled = bond_is_active_slave(slave);
  1573. netdev_lower_state_changed(slave->dev, &info);
  1574. }
  1575. #define BOND_NL_ERR(bond_dev, extack, errmsg) do { \
  1576. if (extack) \
  1577. NL_SET_ERR_MSG(extack, errmsg); \
  1578. else \
  1579. netdev_err(bond_dev, "Error: %s\n", errmsg); \
  1580. } while (0)
  1581. #define SLAVE_NL_ERR(bond_dev, slave_dev, extack, errmsg) do { \
  1582. if (extack) \
  1583. NL_SET_ERR_MSG(extack, errmsg); \
  1584. else \
  1585. slave_err(bond_dev, slave_dev, "Error: %s\n", errmsg); \
  1586. } while (0)
  1587. /* The bonding driver uses ether_setup() to convert a master bond device
  1588. * to ARPHRD_ETHER, that resets the target netdevice's flags so we always
  1589. * have to restore the IFF_MASTER flag, and only restore IFF_SLAVE and IFF_UP
  1590. * if they were set
  1591. */
  1592. static void bond_ether_setup(struct net_device *bond_dev)
  1593. {
  1594. unsigned int flags = bond_dev->flags & (IFF_SLAVE | IFF_UP);
  1595. ether_setup(bond_dev);
  1596. bond_dev->flags |= IFF_MASTER | flags;
  1597. bond_dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  1598. }
  1599. void bond_xdp_set_features(struct net_device *bond_dev)
  1600. {
  1601. struct bonding *bond = netdev_priv(bond_dev);
  1602. xdp_features_t val = NETDEV_XDP_ACT_MASK;
  1603. struct list_head *iter;
  1604. struct slave *slave;
  1605. ASSERT_RTNL();
  1606. if (!bond_xdp_check(bond, BOND_MODE(bond)) || !bond_has_slaves(bond)) {
  1607. xdp_clear_features_flag(bond_dev);
  1608. return;
  1609. }
  1610. bond_for_each_slave(bond, slave, iter)
  1611. val &= slave->dev->xdp_features;
  1612. val &= ~NETDEV_XDP_ACT_XSK_ZEROCOPY;
  1613. xdp_set_features_flag(bond_dev, val);
  1614. }
  1615. /* enslave device <slave> to bond device <master> */
  1616. int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev,
  1617. struct netlink_ext_ack *extack)
  1618. {
  1619. struct bonding *bond = netdev_priv(bond_dev);
  1620. const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
  1621. struct slave *new_slave = NULL, *prev_slave;
  1622. struct sockaddr_storage ss;
  1623. int res = 0, i;
  1624. if (slave_dev->flags & IFF_MASTER &&
  1625. !netif_is_bond_master(slave_dev)) {
  1626. BOND_NL_ERR(bond_dev, extack,
  1627. "Device type (master device) cannot be enslaved");
  1628. return -EPERM;
  1629. }
  1630. /* already in-use? */
  1631. if (netdev_is_rx_handler_busy(slave_dev)) {
  1632. SLAVE_NL_ERR(bond_dev, slave_dev, extack,
  1633. "Device is in use and cannot be enslaved");
  1634. return -EBUSY;
  1635. }
  1636. if (bond_dev == slave_dev) {
  1637. BOND_NL_ERR(bond_dev, extack, "Cannot enslave bond to itself.");
  1638. return -EPERM;
  1639. }
  1640. /* vlan challenged mutual exclusion */
  1641. /* no need to lock since we're protected by rtnl_lock */
  1642. if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
  1643. slave_dbg(bond_dev, slave_dev, "is NETIF_F_VLAN_CHALLENGED\n");
  1644. if (vlan_uses_dev(bond_dev)) {
  1645. SLAVE_NL_ERR(bond_dev, slave_dev, extack,
  1646. "Can not enslave VLAN challenged device to VLAN enabled bond");
  1647. return -EPERM;
  1648. } else {
  1649. slave_warn(bond_dev, slave_dev, "enslaved VLAN challenged slave. Adding VLANs will be blocked as long as it is part of bond.\n");
  1650. }
  1651. } else {
  1652. slave_dbg(bond_dev, slave_dev, "is !NETIF_F_VLAN_CHALLENGED\n");
  1653. }
  1654. if (slave_dev->features & NETIF_F_HW_ESP)
  1655. slave_dbg(bond_dev, slave_dev, "is esp-hw-offload capable\n");
  1656. /* Old ifenslave binaries are no longer supported. These can
  1657. * be identified with moderate accuracy by the state of the slave:
  1658. * the current ifenslave will set the interface down prior to
  1659. * enslaving it; the old ifenslave will not.
  1660. */
  1661. if (slave_dev->flags & IFF_UP) {
  1662. SLAVE_NL_ERR(bond_dev, slave_dev, extack,
  1663. "Device can not be enslaved while up");
  1664. return -EPERM;
  1665. }
  1666. /* set bonding device ether type by slave - bonding netdevices are
  1667. * created with ether_setup, so when the slave type is not ARPHRD_ETHER
  1668. * there is a need to override some of the type dependent attribs/funcs.
  1669. *
  1670. * bond ether type mutual exclusion - don't allow slaves of dissimilar
  1671. * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
  1672. */
  1673. if (!bond_has_slaves(bond)) {
  1674. if (bond_dev->type != slave_dev->type) {
  1675. if (slave_dev->type != ARPHRD_ETHER &&
  1676. BOND_MODE(bond) == BOND_MODE_8023AD) {
  1677. SLAVE_NL_ERR(bond_dev, slave_dev, extack,
  1678. "8023AD mode requires Ethernet devices");
  1679. return -EINVAL;
  1680. }
  1681. slave_dbg(bond_dev, slave_dev, "change device type from %d to %d\n",
  1682. bond_dev->type, slave_dev->type);
  1683. res = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE,
  1684. bond_dev);
  1685. res = notifier_to_errno(res);
  1686. if (res) {
  1687. slave_err(bond_dev, slave_dev, "refused to change device type\n");
  1688. return -EBUSY;
  1689. }
  1690. /* Flush unicast and multicast addresses */
  1691. dev_uc_flush(bond_dev);
  1692. dev_mc_flush(bond_dev);
  1693. if (slave_dev->type != ARPHRD_ETHER)
  1694. bond_setup_by_slave(bond_dev, slave_dev);
  1695. else
  1696. bond_ether_setup(bond_dev);
  1697. call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE,
  1698. bond_dev);
  1699. }
  1700. } else if (bond_dev->type != slave_dev->type) {
  1701. SLAVE_NL_ERR(bond_dev, slave_dev, extack,
  1702. "Device type is different from other slaves");
  1703. return -EINVAL;
  1704. }
  1705. if (slave_dev->type == ARPHRD_INFINIBAND &&
  1706. BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
  1707. SLAVE_NL_ERR(bond_dev, slave_dev, extack,
  1708. "Only active-backup mode is supported for infiniband slaves");
  1709. res = -EOPNOTSUPP;
  1710. goto err_undo_flags;
  1711. }
  1712. if (!slave_ops->ndo_set_mac_address ||
  1713. slave_dev->type == ARPHRD_INFINIBAND) {
  1714. slave_warn(bond_dev, slave_dev, "The slave device specified does not support setting the MAC address\n");
  1715. if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP &&
  1716. bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
  1717. if (!bond_has_slaves(bond)) {
  1718. bond->params.fail_over_mac = BOND_FOM_ACTIVE;
  1719. slave_warn(bond_dev, slave_dev, "Setting fail_over_mac to active for active-backup mode\n");
  1720. } else {
  1721. SLAVE_NL_ERR(bond_dev, slave_dev, extack,
  1722. "Slave device does not support setting the MAC address, but fail_over_mac is not set to active");
  1723. res = -EOPNOTSUPP;
  1724. goto err_undo_flags;
  1725. }
  1726. }
  1727. }
  1728. call_netdevice_notifiers(NETDEV_JOIN, slave_dev);
  1729. /* If this is the first slave, then we need to set the master's hardware
  1730. * address to be the same as the slave's.
  1731. */
  1732. if (!bond_has_slaves(bond) &&
  1733. bond->dev->addr_assign_type == NET_ADDR_RANDOM) {
  1734. res = bond_set_dev_addr(bond->dev, slave_dev);
  1735. if (res)
  1736. goto err_undo_flags;
  1737. }
  1738. new_slave = bond_alloc_slave(bond, slave_dev);
  1739. if (!new_slave) {
  1740. res = -ENOMEM;
  1741. goto err_undo_flags;
  1742. }
  1743. /* Set the new_slave's queue_id to be zero. Queue ID mapping
  1744. * is set via sysfs or module option if desired.
  1745. */
  1746. new_slave->queue_id = 0;
  1747. /* Save slave's original mtu and then set it to match the bond */
  1748. new_slave->original_mtu = slave_dev->mtu;
  1749. res = dev_set_mtu(slave_dev, bond->dev->mtu);
  1750. if (res) {
  1751. slave_err(bond_dev, slave_dev, "Error %d calling dev_set_mtu\n", res);
  1752. goto err_free;
  1753. }
  1754. /* Save slave's original ("permanent") mac address for modes
  1755. * that need it, and for restoring it upon release, and then
  1756. * set it to the master's address
  1757. */
  1758. bond_hw_addr_copy(new_slave->perm_hwaddr, slave_dev->dev_addr,
  1759. slave_dev->addr_len);
  1760. if (!bond->params.fail_over_mac ||
  1761. BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
  1762. /* Set slave to master's mac address. The application already
  1763. * set the master's mac address to that of the first slave
  1764. */
  1765. memcpy(ss.__data, bond_dev->dev_addr, bond_dev->addr_len);
  1766. } else if (bond->params.fail_over_mac == BOND_FOM_FOLLOW &&
  1767. BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP &&
  1768. bond_has_slaves(bond) &&
  1769. memcmp(slave_dev->dev_addr, bond_dev->dev_addr, bond_dev->addr_len) == 0) {
  1770. /* Set slave to random address to avoid duplicate mac
  1771. * address in later fail over.
  1772. */
  1773. eth_random_addr(ss.__data);
  1774. } else {
  1775. goto skip_mac_set;
  1776. }
  1777. ss.ss_family = slave_dev->type;
  1778. res = dev_set_mac_address(slave_dev, &ss, extack);
  1779. if (res) {
  1780. slave_err(bond_dev, slave_dev, "Error %d calling set_mac_address\n", res);
  1781. goto err_restore_mtu;
  1782. }
  1783. skip_mac_set:
  1784. /* set no_addrconf flag before open to prevent IPv6 addrconf */
  1785. slave_dev->priv_flags |= IFF_NO_ADDRCONF;
  1786. /* open the slave since the application closed it */
  1787. res = dev_open(slave_dev, extack);
  1788. if (res) {
  1789. slave_err(bond_dev, slave_dev, "Opening slave failed\n");
  1790. goto err_restore_mac;
  1791. }
  1792. slave_dev->priv_flags |= IFF_BONDING;
  1793. /* initialize slave stats */
  1794. dev_get_stats(new_slave->dev, &new_slave->slave_stats);
  1795. if (bond_is_lb(bond)) {
  1796. /* bond_alb_init_slave() must be called before all other stages since
  1797. * it might fail and we do not want to have to undo everything
  1798. */
  1799. res = bond_alb_init_slave(bond, new_slave);
  1800. if (res)
  1801. goto err_close;
  1802. }
  1803. res = vlan_vids_add_by_dev(slave_dev, bond_dev);
  1804. if (res) {
  1805. slave_err(bond_dev, slave_dev, "Couldn't add bond vlan ids\n");
  1806. goto err_close;
  1807. }
  1808. prev_slave = bond_last_slave(bond);
  1809. new_slave->delay = 0;
  1810. new_slave->link_failure_count = 0;
  1811. if (bond_update_speed_duplex(new_slave) &&
  1812. bond_needs_speed_duplex(bond))
  1813. new_slave->link = BOND_LINK_DOWN;
  1814. new_slave->last_rx = jiffies -
  1815. (msecs_to_jiffies(bond->params.arp_interval) + 1);
  1816. for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
  1817. new_slave->target_last_arp_rx[i] = new_slave->last_rx;
  1818. new_slave->last_tx = new_slave->last_rx;
  1819. /* check for initial state */
  1820. new_slave->link = BOND_LINK_NOCHANGE;
  1821. if (bond->params.miimon) {
  1822. if (netif_running(slave_dev) && netif_carrier_ok(slave_dev)) {
  1823. if (bond->params.updelay) {
  1824. bond_set_slave_link_state(new_slave,
  1825. BOND_LINK_BACK,
  1826. BOND_SLAVE_NOTIFY_NOW);
  1827. new_slave->delay = bond->params.updelay;
  1828. } else {
  1829. bond_set_slave_link_state(new_slave,
  1830. BOND_LINK_UP,
  1831. BOND_SLAVE_NOTIFY_NOW);
  1832. }
  1833. } else {
  1834. bond_set_slave_link_state(new_slave, BOND_LINK_DOWN,
  1835. BOND_SLAVE_NOTIFY_NOW);
  1836. }
  1837. } else if (bond->params.arp_interval) {
  1838. bond_set_slave_link_state(new_slave,
  1839. (netif_carrier_ok(slave_dev) ?
  1840. BOND_LINK_UP : BOND_LINK_DOWN),
  1841. BOND_SLAVE_NOTIFY_NOW);
  1842. } else {
  1843. bond_set_slave_link_state(new_slave, BOND_LINK_UP,
  1844. BOND_SLAVE_NOTIFY_NOW);
  1845. }
  1846. if (new_slave->link != BOND_LINK_DOWN)
  1847. new_slave->last_link_up = jiffies;
  1848. slave_dbg(bond_dev, slave_dev, "Initial state of slave is BOND_LINK_%s\n",
  1849. new_slave->link == BOND_LINK_DOWN ? "DOWN" :
  1850. (new_slave->link == BOND_LINK_UP ? "UP" : "BACK"));
  1851. if (bond_uses_primary(bond) && bond->params.primary[0]) {
  1852. /* if there is a primary slave, remember it */
  1853. if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
  1854. rcu_assign_pointer(bond->primary_slave, new_slave);
  1855. bond->force_primary = true;
  1856. }
  1857. }
  1858. switch (BOND_MODE(bond)) {
  1859. case BOND_MODE_ACTIVEBACKUP:
  1860. bond_set_slave_inactive_flags(new_slave,
  1861. BOND_SLAVE_NOTIFY_NOW);
  1862. break;
  1863. case BOND_MODE_8023AD:
  1864. /* in 802.3ad mode, the internal mechanism
  1865. * will activate the slaves in the selected
  1866. * aggregator
  1867. */
  1868. bond_set_slave_inactive_flags(new_slave, BOND_SLAVE_NOTIFY_NOW);
  1869. /* if this is the first slave */
  1870. if (!prev_slave) {
  1871. SLAVE_AD_INFO(new_slave)->id = 1;
  1872. /* Initialize AD with the number of times that the AD timer is called in 1 second
  1873. * can be called only after the mac address of the bond is set
  1874. */
  1875. bond_3ad_initialize(bond);
  1876. } else {
  1877. SLAVE_AD_INFO(new_slave)->id =
  1878. SLAVE_AD_INFO(prev_slave)->id + 1;
  1879. }
  1880. bond_3ad_bind_slave(new_slave);
  1881. break;
  1882. case BOND_MODE_TLB:
  1883. case BOND_MODE_ALB:
  1884. bond_set_active_slave(new_slave);
  1885. bond_set_slave_inactive_flags(new_slave, BOND_SLAVE_NOTIFY_NOW);
  1886. break;
  1887. default:
  1888. slave_dbg(bond_dev, slave_dev, "This slave is always active in trunk mode\n");
  1889. /* always active in trunk mode */
  1890. bond_set_active_slave(new_slave);
  1891. /* In trunking mode there is little meaning to curr_active_slave
  1892. * anyway (it holds no special properties of the bond device),
  1893. * so we can change it without calling change_active_interface()
  1894. */
  1895. if (!rcu_access_pointer(bond->curr_active_slave) &&
  1896. new_slave->link == BOND_LINK_UP)
  1897. rcu_assign_pointer(bond->curr_active_slave, new_slave);
  1898. break;
  1899. } /* switch(bond_mode) */
  1900. #ifdef CONFIG_NET_POLL_CONTROLLER
  1901. if (bond->dev->npinfo) {
  1902. if (slave_enable_netpoll(new_slave)) {
  1903. slave_info(bond_dev, slave_dev, "master_dev is using netpoll, but new slave device does not support netpoll\n");
  1904. res = -EBUSY;
  1905. goto err_detach;
  1906. }
  1907. }
  1908. #endif
  1909. if (!(bond_dev->features & NETIF_F_LRO))
  1910. dev_disable_lro(slave_dev);
  1911. res = netdev_rx_handler_register(slave_dev, bond_handle_frame,
  1912. new_slave);
  1913. if (res) {
  1914. slave_dbg(bond_dev, slave_dev, "Error %d calling netdev_rx_handler_register\n", res);
  1915. goto err_detach;
  1916. }
  1917. res = bond_master_upper_dev_link(bond, new_slave, extack);
  1918. if (res) {
  1919. slave_dbg(bond_dev, slave_dev, "Error %d calling bond_master_upper_dev_link\n", res);
  1920. goto err_unregister;
  1921. }
  1922. bond_lower_state_changed(new_slave);
  1923. res = bond_sysfs_slave_add(new_slave);
  1924. if (res) {
  1925. slave_dbg(bond_dev, slave_dev, "Error %d calling bond_sysfs_slave_add\n", res);
  1926. goto err_upper_unlink;
  1927. }
  1928. /* If the mode uses primary, then the following is handled by
  1929. * bond_change_active_slave().
  1930. */
  1931. if (!bond_uses_primary(bond)) {
  1932. /* set promiscuity level to new slave */
  1933. if (bond_dev->flags & IFF_PROMISC) {
  1934. res = dev_set_promiscuity(slave_dev, 1);
  1935. if (res)
  1936. goto err_sysfs_del;
  1937. }
  1938. /* set allmulti level to new slave */
  1939. if (bond_dev->flags & IFF_ALLMULTI) {
  1940. res = dev_set_allmulti(slave_dev, 1);
  1941. if (res) {
  1942. if (bond_dev->flags & IFF_PROMISC)
  1943. dev_set_promiscuity(slave_dev, -1);
  1944. goto err_sysfs_del;
  1945. }
  1946. }
  1947. if (bond_dev->flags & IFF_UP) {
  1948. netif_addr_lock_bh(bond_dev);
  1949. dev_mc_sync_multiple(slave_dev, bond_dev);
  1950. dev_uc_sync_multiple(slave_dev, bond_dev);
  1951. netif_addr_unlock_bh(bond_dev);
  1952. if (BOND_MODE(bond) == BOND_MODE_8023AD)
  1953. dev_mc_add(slave_dev, lacpdu_mcast_addr);
  1954. }
  1955. }
  1956. bond->slave_cnt++;
  1957. netdev_compute_master_upper_features(bond->dev, true);
  1958. bond_set_carrier(bond);
  1959. /* Needs to be called before bond_select_active_slave(), which will
  1960. * remove the maddrs if the slave is selected as active slave.
  1961. */
  1962. bond_slave_ns_maddrs_add(bond, new_slave);
  1963. if (bond_uses_primary(bond)) {
  1964. block_netpoll_tx();
  1965. bond_select_active_slave(bond);
  1966. unblock_netpoll_tx();
  1967. }
  1968. if (!slave_dev->netdev_ops->ndo_bpf ||
  1969. !slave_dev->netdev_ops->ndo_xdp_xmit) {
  1970. if (bond->xdp_prog) {
  1971. SLAVE_NL_ERR(bond_dev, slave_dev, extack,
  1972. "Slave does not support XDP");
  1973. res = -EOPNOTSUPP;
  1974. goto err_sysfs_del;
  1975. }
  1976. } else if (bond->xdp_prog) {
  1977. struct netdev_bpf xdp = {
  1978. .command = XDP_SETUP_PROG,
  1979. .flags = 0,
  1980. .prog = bond->xdp_prog,
  1981. .extack = extack,
  1982. };
  1983. if (dev_xdp_prog_count(slave_dev) > 0) {
  1984. SLAVE_NL_ERR(bond_dev, slave_dev, extack,
  1985. "Slave has XDP program loaded, please unload before enslaving");
  1986. res = -EOPNOTSUPP;
  1987. goto err_sysfs_del;
  1988. }
  1989. res = dev_xdp_propagate(slave_dev, &xdp);
  1990. if (res < 0) {
  1991. /* ndo_bpf() sets extack error message */
  1992. slave_dbg(bond_dev, slave_dev, "Error %d calling ndo_bpf\n", res);
  1993. goto err_sysfs_del;
  1994. }
  1995. if (bond->xdp_prog)
  1996. bpf_prog_inc(bond->xdp_prog);
  1997. }
  1998. /* broadcast mode uses the all_slaves to loop through slaves. */
  1999. if (bond_mode_can_use_xmit_hash(bond) ||
  2000. BOND_MODE(bond) == BOND_MODE_BROADCAST)
  2001. bond_update_slave_arr(bond, NULL);
  2002. bond_xdp_set_features(bond_dev);
  2003. slave_info(bond_dev, slave_dev, "Enslaving as %s interface with %s link\n",
  2004. bond_is_active_slave(new_slave) ? "an active" : "a backup",
  2005. new_slave->link != BOND_LINK_DOWN ? "an up" : "a down");
  2006. /* enslave is successful */
  2007. bond_queue_slave_event(new_slave);
  2008. return 0;
  2009. /* Undo stages on error */
  2010. err_sysfs_del:
  2011. bond_sysfs_slave_del(new_slave);
  2012. err_upper_unlink:
  2013. bond_upper_dev_unlink(bond, new_slave);
  2014. err_unregister:
  2015. netdev_rx_handler_unregister(slave_dev);
  2016. err_detach:
  2017. vlan_vids_del_by_dev(slave_dev, bond_dev);
  2018. if (rcu_access_pointer(bond->primary_slave) == new_slave)
  2019. RCU_INIT_POINTER(bond->primary_slave, NULL);
  2020. if (rcu_access_pointer(bond->curr_active_slave) == new_slave) {
  2021. block_netpoll_tx();
  2022. bond_change_active_slave(bond, NULL);
  2023. bond_select_active_slave(bond);
  2024. unblock_netpoll_tx();
  2025. }
  2026. /* either primary_slave or curr_active_slave might've changed */
  2027. synchronize_rcu();
  2028. slave_disable_netpoll(new_slave);
  2029. err_close:
  2030. if (!netif_is_bond_master(slave_dev))
  2031. slave_dev->priv_flags &= ~IFF_BONDING;
  2032. dev_close(slave_dev);
  2033. err_restore_mac:
  2034. slave_dev->priv_flags &= ~IFF_NO_ADDRCONF;
  2035. if (!bond->params.fail_over_mac ||
  2036. BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
  2037. /* XXX TODO - fom follow mode needs to change master's
  2038. * MAC if this slave's MAC is in use by the bond, or at
  2039. * least print a warning.
  2040. */
  2041. bond_hw_addr_copy(ss.__data, new_slave->perm_hwaddr,
  2042. new_slave->dev->addr_len);
  2043. ss.ss_family = slave_dev->type;
  2044. dev_set_mac_address(slave_dev, &ss, NULL);
  2045. }
  2046. err_restore_mtu:
  2047. dev_set_mtu(slave_dev, new_slave->original_mtu);
  2048. err_free:
  2049. kobject_put(&new_slave->kobj);
  2050. err_undo_flags:
  2051. /* Enslave of first slave has failed and we need to fix master's mac */
  2052. if (!bond_has_slaves(bond)) {
  2053. if (ether_addr_equal_64bits(bond_dev->dev_addr,
  2054. slave_dev->dev_addr))
  2055. eth_hw_addr_random(bond_dev);
  2056. if (bond_dev->type != ARPHRD_ETHER) {
  2057. dev_close(bond_dev);
  2058. bond_ether_setup(bond_dev);
  2059. }
  2060. }
  2061. return res;
  2062. }
  2063. /* Try to release the slave device <slave> from the bond device <master>
  2064. * It is legal to access curr_active_slave without a lock because all the function
  2065. * is RTNL-locked. If "all" is true it means that the function is being called
  2066. * while destroying a bond interface and all slaves are being released.
  2067. *
  2068. * The rules for slave state should be:
  2069. * for Active/Backup:
  2070. * Active stays on all backups go down
  2071. * for Bonded connections:
  2072. * The first up interface should be left on and all others downed.
  2073. */
  2074. static int __bond_release_one(struct net_device *bond_dev,
  2075. struct net_device *slave_dev,
  2076. bool all, bool unregister)
  2077. {
  2078. struct bonding *bond = netdev_priv(bond_dev);
  2079. struct slave *slave, *oldcurrent;
  2080. struct sockaddr_storage ss;
  2081. int old_flags = bond_dev->flags;
  2082. netdev_features_t old_features = bond_dev->features;
  2083. /* slave is not a slave or master is not master of this slave */
  2084. if (!(slave_dev->flags & IFF_SLAVE) ||
  2085. !netdev_has_upper_dev(slave_dev, bond_dev)) {
  2086. slave_dbg(bond_dev, slave_dev, "cannot release slave\n");
  2087. return -EINVAL;
  2088. }
  2089. block_netpoll_tx();
  2090. slave = bond_get_slave_by_dev(bond, slave_dev);
  2091. if (!slave) {
  2092. /* not a slave of this bond */
  2093. slave_info(bond_dev, slave_dev, "interface not enslaved\n");
  2094. unblock_netpoll_tx();
  2095. return -EINVAL;
  2096. }
  2097. bond_set_slave_inactive_flags(slave, BOND_SLAVE_NOTIFY_NOW);
  2098. bond_sysfs_slave_del(slave);
  2099. /* recompute stats just before removing the slave */
  2100. bond_get_stats(bond->dev, &bond->bond_stats);
  2101. if (bond->xdp_prog) {
  2102. struct netdev_bpf xdp = {
  2103. .command = XDP_SETUP_PROG,
  2104. .flags = 0,
  2105. .prog = NULL,
  2106. .extack = NULL,
  2107. };
  2108. if (dev_xdp_propagate(slave_dev, &xdp))
  2109. slave_warn(bond_dev, slave_dev, "failed to unload XDP program\n");
  2110. }
  2111. /* unregister rx_handler early so bond_handle_frame wouldn't be called
  2112. * for this slave anymore.
  2113. */
  2114. netdev_rx_handler_unregister(slave_dev);
  2115. if (BOND_MODE(bond) == BOND_MODE_8023AD)
  2116. bond_3ad_unbind_slave(slave);
  2117. bond_upper_dev_unlink(bond, slave);
  2118. if (bond_mode_can_use_xmit_hash(bond) ||
  2119. BOND_MODE(bond) == BOND_MODE_BROADCAST)
  2120. bond_update_slave_arr(bond, slave);
  2121. slave_info(bond_dev, slave_dev, "Releasing %s interface\n",
  2122. bond_is_active_slave(slave) ? "active" : "backup");
  2123. oldcurrent = rcu_access_pointer(bond->curr_active_slave);
  2124. RCU_INIT_POINTER(bond->current_arp_slave, NULL);
  2125. if (!all && (bond->params.fail_over_mac != BOND_FOM_ACTIVE ||
  2126. BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP)) {
  2127. if (ether_addr_equal_64bits(bond_dev->dev_addr, slave->perm_hwaddr) &&
  2128. bond_has_slaves(bond))
  2129. slave_warn(bond_dev, slave_dev, "the permanent HWaddr of slave - %pM - is still in use by bond - set the HWaddr of slave to a different address to avoid conflicts\n",
  2130. slave->perm_hwaddr);
  2131. }
  2132. if (rtnl_dereference(bond->primary_slave) == slave)
  2133. RCU_INIT_POINTER(bond->primary_slave, NULL);
  2134. if (oldcurrent == slave)
  2135. bond_change_active_slave(bond, NULL);
  2136. /* Must be called after bond_change_active_slave () as the slave
  2137. * might change from an active slave to a backup slave. Then it is
  2138. * necessary to clear the maddrs on the backup slave.
  2139. */
  2140. bond_slave_ns_maddrs_del(bond, slave);
  2141. if (bond_is_lb(bond)) {
  2142. /* Must be called only after the slave has been
  2143. * detached from the list and the curr_active_slave
  2144. * has been cleared (if our_slave == old_current),
  2145. * but before a new active slave is selected.
  2146. */
  2147. bond_alb_deinit_slave(bond, slave);
  2148. }
  2149. if (all) {
  2150. RCU_INIT_POINTER(bond->curr_active_slave, NULL);
  2151. } else if (oldcurrent == slave) {
  2152. /* Note that we hold RTNL over this sequence, so there
  2153. * is no concern that another slave add/remove event
  2154. * will interfere.
  2155. */
  2156. bond_select_active_slave(bond);
  2157. }
  2158. bond_set_carrier(bond);
  2159. if (!bond_has_slaves(bond))
  2160. eth_hw_addr_random(bond_dev);
  2161. unblock_netpoll_tx();
  2162. synchronize_rcu();
  2163. bond->slave_cnt--;
  2164. if (!bond_has_slaves(bond)) {
  2165. call_netdevice_notifiers(NETDEV_CHANGEADDR, bond->dev);
  2166. call_netdevice_notifiers(NETDEV_RELEASE, bond->dev);
  2167. }
  2168. netdev_compute_master_upper_features(bond->dev, true);
  2169. if (!(bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
  2170. (old_features & NETIF_F_VLAN_CHALLENGED))
  2171. slave_info(bond_dev, slave_dev, "last VLAN challenged slave left bond - VLAN blocking is removed\n");
  2172. vlan_vids_del_by_dev(slave_dev, bond_dev);
  2173. /* If the mode uses primary, then this case was handled above by
  2174. * bond_change_active_slave(..., NULL)
  2175. */
  2176. if (!bond_uses_primary(bond)) {
  2177. /* unset promiscuity level from slave
  2178. * NOTE: The NETDEV_CHANGEADDR call above may change the value
  2179. * of the IFF_PROMISC flag in the bond_dev, but we need the
  2180. * value of that flag before that change, as that was the value
  2181. * when this slave was attached, so we cache at the start of the
  2182. * function and use it here. Same goes for ALLMULTI below
  2183. */
  2184. if (old_flags & IFF_PROMISC)
  2185. dev_set_promiscuity(slave_dev, -1);
  2186. /* unset allmulti level from slave */
  2187. if (old_flags & IFF_ALLMULTI)
  2188. dev_set_allmulti(slave_dev, -1);
  2189. if (old_flags & IFF_UP)
  2190. bond_hw_addr_flush(bond_dev, slave_dev);
  2191. }
  2192. slave_disable_netpoll(slave);
  2193. /* close slave before restoring its mac address */
  2194. dev_close(slave_dev);
  2195. slave_dev->priv_flags &= ~IFF_NO_ADDRCONF;
  2196. if (bond->params.fail_over_mac != BOND_FOM_ACTIVE ||
  2197. BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
  2198. /* restore original ("permanent") mac address */
  2199. bond_hw_addr_copy(ss.__data, slave->perm_hwaddr,
  2200. slave->dev->addr_len);
  2201. ss.ss_family = slave_dev->type;
  2202. dev_set_mac_address(slave_dev, &ss, NULL);
  2203. }
  2204. if (unregister) {
  2205. netdev_lock_ops(slave_dev);
  2206. __netif_set_mtu(slave_dev, slave->original_mtu);
  2207. netdev_unlock_ops(slave_dev);
  2208. } else {
  2209. dev_set_mtu(slave_dev, slave->original_mtu);
  2210. }
  2211. if (!netif_is_bond_master(slave_dev))
  2212. slave_dev->priv_flags &= ~IFF_BONDING;
  2213. bond_xdp_set_features(bond_dev);
  2214. kobject_put(&slave->kobj);
  2215. return 0;
  2216. }
  2217. /* A wrapper used because of ndo_del_link */
  2218. int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
  2219. {
  2220. return __bond_release_one(bond_dev, slave_dev, false, false);
  2221. }
  2222. /* First release a slave and then destroy the bond if no more slaves are left.
  2223. * Must be under rtnl_lock when this function is called.
  2224. */
  2225. static int bond_release_and_destroy(struct net_device *bond_dev,
  2226. struct net_device *slave_dev)
  2227. {
  2228. struct bonding *bond = netdev_priv(bond_dev);
  2229. int ret;
  2230. ret = __bond_release_one(bond_dev, slave_dev, false, true);
  2231. if (ret == 0 && !bond_has_slaves(bond) &&
  2232. bond_dev->reg_state != NETREG_UNREGISTERING) {
  2233. bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
  2234. netdev_info(bond_dev, "Destroying bond\n");
  2235. bond_remove_proc_entry(bond);
  2236. unregister_netdevice(bond_dev);
  2237. }
  2238. return ret;
  2239. }
  2240. static void bond_info_query(struct net_device *bond_dev, struct ifbond *info)
  2241. {
  2242. struct bonding *bond = netdev_priv(bond_dev);
  2243. bond_fill_ifbond(bond, info);
  2244. }
  2245. static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
  2246. {
  2247. struct bonding *bond = netdev_priv(bond_dev);
  2248. struct list_head *iter;
  2249. int i = 0, res = -ENODEV;
  2250. struct slave *slave;
  2251. bond_for_each_slave(bond, slave, iter) {
  2252. if (i++ == (int)info->slave_id) {
  2253. res = 0;
  2254. bond_fill_ifslave(slave, info);
  2255. break;
  2256. }
  2257. }
  2258. return res;
  2259. }
  2260. /*-------------------------------- Monitoring -------------------------------*/
  2261. /* called with rcu_read_lock() */
  2262. static int bond_miimon_inspect(struct bonding *bond)
  2263. {
  2264. bool ignore_updelay = false;
  2265. int link_state, commit = 0;
  2266. struct list_head *iter;
  2267. struct slave *slave;
  2268. if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP) {
  2269. ignore_updelay = !rcu_dereference(bond->curr_active_slave);
  2270. } else {
  2271. struct bond_up_slave *usable_slaves;
  2272. usable_slaves = rcu_dereference(bond->usable_slaves);
  2273. if (usable_slaves && usable_slaves->count == 0)
  2274. ignore_updelay = true;
  2275. }
  2276. bond_for_each_slave_rcu(bond, slave, iter) {
  2277. bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
  2278. link_state = netif_running(slave->dev) &&
  2279. netif_carrier_ok(slave->dev);
  2280. switch (slave->link) {
  2281. case BOND_LINK_UP:
  2282. if (link_state)
  2283. continue;
  2284. bond_propose_link_state(slave, BOND_LINK_FAIL);
  2285. commit++;
  2286. slave->delay = bond->params.downdelay;
  2287. if (slave->delay && net_ratelimit()) {
  2288. slave_info(bond->dev, slave->dev, "link status down for %sinterface, disabling it in %d ms\n",
  2289. (BOND_MODE(bond) ==
  2290. BOND_MODE_ACTIVEBACKUP) ?
  2291. (bond_is_active_slave(slave) ?
  2292. "active " : "backup ") : "",
  2293. bond->params.downdelay * bond->params.miimon);
  2294. }
  2295. fallthrough;
  2296. case BOND_LINK_FAIL:
  2297. if (link_state) {
  2298. /* recovered before downdelay expired */
  2299. bond_propose_link_state(slave, BOND_LINK_UP);
  2300. slave->last_link_up = jiffies;
  2301. if (net_ratelimit())
  2302. slave_info(bond->dev, slave->dev, "link status up again after %d ms\n",
  2303. (bond->params.downdelay - slave->delay) *
  2304. bond->params.miimon);
  2305. commit++;
  2306. continue;
  2307. }
  2308. if (slave->delay <= 0) {
  2309. bond_propose_link_state(slave, BOND_LINK_DOWN);
  2310. commit++;
  2311. continue;
  2312. }
  2313. slave->delay--;
  2314. break;
  2315. case BOND_LINK_DOWN:
  2316. if (!link_state)
  2317. continue;
  2318. bond_propose_link_state(slave, BOND_LINK_BACK);
  2319. commit++;
  2320. slave->delay = bond->params.updelay;
  2321. if (slave->delay && net_ratelimit()) {
  2322. slave_info(bond->dev, slave->dev, "link status up, enabling it in %d ms\n",
  2323. ignore_updelay ? 0 :
  2324. bond->params.updelay *
  2325. bond->params.miimon);
  2326. }
  2327. fallthrough;
  2328. case BOND_LINK_BACK:
  2329. if (!link_state) {
  2330. bond_propose_link_state(slave, BOND_LINK_DOWN);
  2331. if (net_ratelimit())
  2332. slave_info(bond->dev, slave->dev, "link status down again after %d ms\n",
  2333. (bond->params.updelay - slave->delay) *
  2334. bond->params.miimon);
  2335. commit++;
  2336. continue;
  2337. }
  2338. if (ignore_updelay)
  2339. slave->delay = 0;
  2340. if (slave->delay <= 0) {
  2341. bond_propose_link_state(slave, BOND_LINK_UP);
  2342. commit++;
  2343. ignore_updelay = false;
  2344. continue;
  2345. }
  2346. slave->delay--;
  2347. break;
  2348. }
  2349. }
  2350. return commit;
  2351. }
  2352. static void bond_miimon_link_change(struct bonding *bond,
  2353. struct slave *slave,
  2354. char link)
  2355. {
  2356. switch (BOND_MODE(bond)) {
  2357. case BOND_MODE_8023AD:
  2358. bond_3ad_handle_link_change(slave, link);
  2359. break;
  2360. case BOND_MODE_TLB:
  2361. case BOND_MODE_ALB:
  2362. bond_alb_handle_link_change(bond, slave, link);
  2363. break;
  2364. case BOND_MODE_XOR:
  2365. bond_update_slave_arr(bond, NULL);
  2366. break;
  2367. }
  2368. }
  2369. static void bond_miimon_commit(struct bonding *bond)
  2370. {
  2371. struct slave *slave, *primary, *active;
  2372. bool do_failover = false;
  2373. struct list_head *iter;
  2374. ASSERT_RTNL();
  2375. bond_for_each_slave(bond, slave, iter) {
  2376. switch (slave->link_new_state) {
  2377. case BOND_LINK_NOCHANGE:
  2378. /* For 802.3ad mode, check current slave speed and
  2379. * duplex again in case its port was disabled after
  2380. * invalid speed/duplex reporting but recovered before
  2381. * link monitoring could make a decision on the actual
  2382. * link status
  2383. */
  2384. if (BOND_MODE(bond) == BOND_MODE_8023AD &&
  2385. slave->link == BOND_LINK_UP)
  2386. bond_3ad_adapter_speed_duplex_changed(slave);
  2387. continue;
  2388. case BOND_LINK_UP:
  2389. if (bond_update_speed_duplex(slave) &&
  2390. bond_needs_speed_duplex(bond)) {
  2391. slave->link = BOND_LINK_DOWN;
  2392. if (net_ratelimit())
  2393. slave_warn(bond->dev, slave->dev,
  2394. "failed to get link speed/duplex\n");
  2395. continue;
  2396. }
  2397. bond_set_slave_link_state(slave, BOND_LINK_UP,
  2398. BOND_SLAVE_NOTIFY_NOW);
  2399. slave->last_link_up = jiffies;
  2400. primary = rtnl_dereference(bond->primary_slave);
  2401. if (BOND_MODE(bond) == BOND_MODE_8023AD) {
  2402. /* prevent it from being the active one */
  2403. bond_set_backup_slave(slave);
  2404. } else if (BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
  2405. /* make it immediately active */
  2406. bond_set_active_slave(slave);
  2407. }
  2408. slave_info(bond->dev, slave->dev, "link status definitely up, %u Mbps %s duplex\n",
  2409. slave->speed == SPEED_UNKNOWN ? 0 : slave->speed,
  2410. slave->duplex ? "full" : "half");
  2411. bond_miimon_link_change(bond, slave, BOND_LINK_UP);
  2412. active = rtnl_dereference(bond->curr_active_slave);
  2413. if (!active || slave == primary || slave->prio > active->prio)
  2414. do_failover = true;
  2415. continue;
  2416. case BOND_LINK_DOWN:
  2417. if (slave->link_failure_count < UINT_MAX)
  2418. slave->link_failure_count++;
  2419. bond_set_slave_link_state(slave, BOND_LINK_DOWN,
  2420. BOND_SLAVE_NOTIFY_NOW);
  2421. if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP ||
  2422. BOND_MODE(bond) == BOND_MODE_8023AD)
  2423. bond_set_slave_inactive_flags(slave,
  2424. BOND_SLAVE_NOTIFY_NOW);
  2425. slave_info(bond->dev, slave->dev, "link status definitely down, disabling slave\n");
  2426. bond_miimon_link_change(bond, slave, BOND_LINK_DOWN);
  2427. if (slave == rcu_access_pointer(bond->curr_active_slave))
  2428. do_failover = true;
  2429. continue;
  2430. case BOND_LINK_FAIL:
  2431. case BOND_LINK_BACK:
  2432. slave_dbg(bond->dev, slave->dev, "link_new_state %d on slave\n",
  2433. slave->link_new_state);
  2434. continue;
  2435. default:
  2436. slave_err(bond->dev, slave->dev, "invalid link_new_state %d on slave\n",
  2437. slave->link_new_state);
  2438. bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
  2439. continue;
  2440. }
  2441. }
  2442. if (do_failover) {
  2443. block_netpoll_tx();
  2444. bond_select_active_slave(bond);
  2445. unblock_netpoll_tx();
  2446. }
  2447. bond_set_carrier(bond);
  2448. }
  2449. /* bond_mii_monitor
  2450. *
  2451. * Really a wrapper that splits the mii monitor into two phases: an
  2452. * inspection, then (if inspection indicates something needs to be done)
  2453. * an acquisition of appropriate locks followed by a commit phase to
  2454. * implement whatever link state changes are indicated.
  2455. */
  2456. static void bond_mii_monitor(struct work_struct *work)
  2457. {
  2458. struct bonding *bond = container_of(work, struct bonding,
  2459. mii_work.work);
  2460. struct list_head *iter;
  2461. struct slave *slave;
  2462. unsigned long delay;
  2463. bool commit;
  2464. delay = msecs_to_jiffies(bond->params.miimon);
  2465. if (!bond_has_slaves(bond))
  2466. goto re_arm;
  2467. rcu_read_lock();
  2468. commit = !!bond_miimon_inspect(bond);
  2469. rcu_read_unlock();
  2470. if (commit || READ_ONCE(bond->send_peer_notif)) {
  2471. /* Race avoidance with bond_close cancel of workqueue */
  2472. if (!rtnl_trylock()) {
  2473. delay = 1;
  2474. goto re_arm;
  2475. }
  2476. if (commit) {
  2477. bond_for_each_slave(bond, slave, iter) {
  2478. bond_commit_link_state(slave,
  2479. BOND_SLAVE_NOTIFY_LATER);
  2480. }
  2481. bond_miimon_commit(bond);
  2482. }
  2483. if (bond->send_peer_notif)
  2484. bond_peer_notify_may_events(bond, true);
  2485. rtnl_unlock(); /* might sleep, hold no other locks */
  2486. }
  2487. re_arm:
  2488. if (bond->params.miimon)
  2489. queue_delayed_work(bond->wq, &bond->mii_work, delay);
  2490. }
  2491. static int bond_upper_dev_walk(struct net_device *upper,
  2492. struct netdev_nested_priv *priv)
  2493. {
  2494. __be32 ip = *(__be32 *)priv->data;
  2495. return ip == bond_confirm_addr(upper, 0, ip);
  2496. }
  2497. static bool bond_has_this_ip(struct bonding *bond, __be32 ip)
  2498. {
  2499. struct netdev_nested_priv priv = {
  2500. .data = (void *)&ip,
  2501. };
  2502. bool ret = false;
  2503. if (ip == bond_confirm_addr(bond->dev, 0, ip))
  2504. return true;
  2505. rcu_read_lock();
  2506. if (netdev_walk_all_upper_dev_rcu(bond->dev, bond_upper_dev_walk, &priv))
  2507. ret = true;
  2508. rcu_read_unlock();
  2509. return ret;
  2510. }
  2511. #define BOND_VLAN_PROTO_NONE cpu_to_be16(0xffff)
  2512. static bool bond_handle_vlan(struct slave *slave, struct bond_vlan_tag *tags,
  2513. struct sk_buff *skb)
  2514. {
  2515. struct net_device *bond_dev = slave->bond->dev;
  2516. struct net_device *slave_dev = slave->dev;
  2517. struct bond_vlan_tag *outer_tag = tags;
  2518. if (!tags || tags->vlan_proto == BOND_VLAN_PROTO_NONE)
  2519. return true;
  2520. tags++;
  2521. /* Go through all the tags backwards and add them to the packet */
  2522. while (tags->vlan_proto != BOND_VLAN_PROTO_NONE) {
  2523. if (!tags->vlan_id) {
  2524. tags++;
  2525. continue;
  2526. }
  2527. slave_dbg(bond_dev, slave_dev, "inner tag: proto %X vid %X\n",
  2528. ntohs(outer_tag->vlan_proto), tags->vlan_id);
  2529. skb = vlan_insert_tag_set_proto(skb, tags->vlan_proto,
  2530. tags->vlan_id);
  2531. if (!skb) {
  2532. net_err_ratelimited("failed to insert inner VLAN tag\n");
  2533. return false;
  2534. }
  2535. tags++;
  2536. }
  2537. /* Set the outer tag */
  2538. if (outer_tag->vlan_id) {
  2539. slave_dbg(bond_dev, slave_dev, "outer tag: proto %X vid %X\n",
  2540. ntohs(outer_tag->vlan_proto), outer_tag->vlan_id);
  2541. __vlan_hwaccel_put_tag(skb, outer_tag->vlan_proto,
  2542. outer_tag->vlan_id);
  2543. }
  2544. return true;
  2545. }
  2546. /* We go to the (large) trouble of VLAN tagging ARP frames because
  2547. * switches in VLAN mode (especially if ports are configured as
  2548. * "native" to a VLAN) might not pass non-tagged frames.
  2549. */
  2550. static void bond_arp_send(struct slave *slave, int arp_op, __be32 dest_ip,
  2551. __be32 src_ip, struct bond_vlan_tag *tags)
  2552. {
  2553. struct net_device *bond_dev = slave->bond->dev;
  2554. struct net_device *slave_dev = slave->dev;
  2555. struct sk_buff *skb;
  2556. slave_dbg(bond_dev, slave_dev, "arp %d on slave: dst %pI4 src %pI4\n",
  2557. arp_op, &dest_ip, &src_ip);
  2558. skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
  2559. NULL, slave_dev->dev_addr, NULL);
  2560. if (!skb) {
  2561. net_err_ratelimited("ARP packet allocation failed\n");
  2562. return;
  2563. }
  2564. if (bond_handle_vlan(slave, tags, skb)) {
  2565. slave_update_last_tx(slave);
  2566. arp_xmit(skb);
  2567. }
  2568. return;
  2569. }
  2570. /* Validate the device path between the @start_dev and the @end_dev.
  2571. * The path is valid if the @end_dev is reachable through device
  2572. * stacking.
  2573. * When the path is validated, collect any vlan information in the
  2574. * path.
  2575. */
  2576. struct bond_vlan_tag *bond_verify_device_path(struct net_device *start_dev,
  2577. struct net_device *end_dev,
  2578. int level)
  2579. {
  2580. struct bond_vlan_tag *tags;
  2581. struct net_device *upper;
  2582. struct list_head *iter;
  2583. if (start_dev == end_dev) {
  2584. tags = kzalloc_objs(*tags, level + 1, GFP_ATOMIC);
  2585. if (!tags)
  2586. return ERR_PTR(-ENOMEM);
  2587. tags[level].vlan_proto = BOND_VLAN_PROTO_NONE;
  2588. return tags;
  2589. }
  2590. netdev_for_each_upper_dev_rcu(start_dev, upper, iter) {
  2591. tags = bond_verify_device_path(upper, end_dev, level + 1);
  2592. if (IS_ERR_OR_NULL(tags)) {
  2593. if (IS_ERR(tags))
  2594. return tags;
  2595. continue;
  2596. }
  2597. if (is_vlan_dev(upper)) {
  2598. tags[level].vlan_proto = vlan_dev_vlan_proto(upper);
  2599. tags[level].vlan_id = vlan_dev_vlan_id(upper);
  2600. }
  2601. return tags;
  2602. }
  2603. return NULL;
  2604. }
  2605. static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
  2606. {
  2607. struct rtable *rt;
  2608. struct bond_vlan_tag *tags;
  2609. __be32 *targets = bond->params.arp_targets, addr;
  2610. int i;
  2611. for (i = 0; i < BOND_MAX_ARP_TARGETS && targets[i]; i++) {
  2612. slave_dbg(bond->dev, slave->dev, "%s: target %pI4\n",
  2613. __func__, &targets[i]);
  2614. tags = NULL;
  2615. /* Find out through which dev should the packet go */
  2616. rt = ip_route_output(dev_net(bond->dev), targets[i], 0, 0, 0,
  2617. RT_SCOPE_LINK);
  2618. if (IS_ERR(rt)) {
  2619. /* there's no route to target - try to send arp
  2620. * probe to generate any traffic (arp_validate=0)
  2621. */
  2622. if (bond->params.arp_validate)
  2623. pr_warn_once("%s: no route to arp_ip_target %pI4 and arp_validate is set\n",
  2624. bond->dev->name,
  2625. &targets[i]);
  2626. bond_arp_send(slave, ARPOP_REQUEST, targets[i],
  2627. 0, tags);
  2628. continue;
  2629. }
  2630. /* bond device itself */
  2631. if (rt->dst.dev == bond->dev)
  2632. goto found;
  2633. rcu_read_lock();
  2634. tags = bond_verify_device_path(bond->dev, rt->dst.dev, 0);
  2635. rcu_read_unlock();
  2636. if (!IS_ERR_OR_NULL(tags))
  2637. goto found;
  2638. /* Not our device - skip */
  2639. slave_dbg(bond->dev, slave->dev, "no path to arp_ip_target %pI4 via rt.dev %s\n",
  2640. &targets[i], rt->dst.dev ? rt->dst.dev->name : "NULL");
  2641. ip_rt_put(rt);
  2642. continue;
  2643. found:
  2644. addr = bond_confirm_addr(rt->dst.dev, targets[i], 0);
  2645. ip_rt_put(rt);
  2646. bond_arp_send(slave, ARPOP_REQUEST, targets[i], addr, tags);
  2647. kfree(tags);
  2648. }
  2649. }
  2650. static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
  2651. {
  2652. int i;
  2653. if (!sip || !bond_has_this_ip(bond, tip)) {
  2654. slave_dbg(bond->dev, slave->dev, "%s: sip %pI4 tip %pI4 not found\n",
  2655. __func__, &sip, &tip);
  2656. return;
  2657. }
  2658. i = bond_get_targets_ip(bond->params.arp_targets, sip);
  2659. if (i == -1) {
  2660. slave_dbg(bond->dev, slave->dev, "%s: sip %pI4 not found in targets\n",
  2661. __func__, &sip);
  2662. return;
  2663. }
  2664. WRITE_ONCE(slave->last_rx, jiffies);
  2665. WRITE_ONCE(slave->target_last_arp_rx[i], jiffies);
  2666. }
  2667. static int bond_arp_rcv(const struct sk_buff *skb, struct bonding *bond,
  2668. struct slave *slave)
  2669. {
  2670. struct arphdr *arp = (struct arphdr *)skb->data;
  2671. struct slave *curr_active_slave, *curr_arp_slave;
  2672. unsigned char *arp_ptr;
  2673. __be32 sip, tip;
  2674. unsigned int alen;
  2675. alen = arp_hdr_len(bond->dev);
  2676. if (alen > skb_headlen(skb)) {
  2677. arp = kmalloc(alen, GFP_ATOMIC);
  2678. if (!arp)
  2679. goto out_unlock;
  2680. if (skb_copy_bits(skb, 0, arp, alen) < 0)
  2681. goto out_unlock;
  2682. }
  2683. if (arp->ar_hln != bond->dev->addr_len ||
  2684. skb->pkt_type == PACKET_OTHERHOST ||
  2685. skb->pkt_type == PACKET_LOOPBACK ||
  2686. arp->ar_hrd != htons(ARPHRD_ETHER) ||
  2687. arp->ar_pro != htons(ETH_P_IP) ||
  2688. arp->ar_pln != 4)
  2689. goto out_unlock;
  2690. arp_ptr = (unsigned char *)(arp + 1);
  2691. arp_ptr += bond->dev->addr_len;
  2692. memcpy(&sip, arp_ptr, 4);
  2693. arp_ptr += 4 + bond->dev->addr_len;
  2694. memcpy(&tip, arp_ptr, 4);
  2695. slave_dbg(bond->dev, slave->dev, "%s: %s/%d av %d sv %d sip %pI4 tip %pI4\n",
  2696. __func__, slave->dev->name, bond_slave_state(slave),
  2697. bond->params.arp_validate, slave_do_arp_validate(bond, slave),
  2698. &sip, &tip);
  2699. curr_active_slave = rcu_dereference(bond->curr_active_slave);
  2700. curr_arp_slave = rcu_dereference(bond->current_arp_slave);
  2701. /* We 'trust' the received ARP enough to validate it if:
  2702. *
  2703. * (a) the slave receiving the ARP is active (which includes the
  2704. * current ARP slave, if any), or
  2705. *
  2706. * (b) the receiving slave isn't active, but there is a currently
  2707. * active slave and it received valid arp reply(s) after it became
  2708. * the currently active slave, or
  2709. *
  2710. * (c) there is an ARP slave that sent an ARP during the prior ARP
  2711. * interval, and we receive an ARP reply on any slave. We accept
  2712. * these because switch FDB update delays may deliver the ARP
  2713. * reply to a slave other than the sender of the ARP request.
  2714. *
  2715. * Note: for (b), backup slaves are receiving the broadcast ARP
  2716. * request, not a reply. This request passes from the sending
  2717. * slave through the L2 switch(es) to the receiving slave. Since
  2718. * this is checking the request, sip/tip are swapped for
  2719. * validation.
  2720. *
  2721. * This is done to avoid endless looping when we can't reach the
  2722. * arp_ip_target and fool ourselves with our own arp requests.
  2723. */
  2724. if (bond_is_active_slave(slave))
  2725. bond_validate_arp(bond, slave, sip, tip);
  2726. else if (curr_active_slave &&
  2727. time_after(slave_last_rx(bond, curr_active_slave),
  2728. curr_active_slave->last_link_up))
  2729. bond_validate_arp(bond, slave, tip, sip);
  2730. else if (curr_arp_slave && (arp->ar_op == htons(ARPOP_REPLY)) &&
  2731. bond_time_in_interval(bond, slave_last_tx(curr_arp_slave), 1))
  2732. bond_validate_arp(bond, slave, sip, tip);
  2733. out_unlock:
  2734. if (arp != (struct arphdr *)skb->data)
  2735. kfree(arp);
  2736. return RX_HANDLER_ANOTHER;
  2737. }
  2738. #if IS_ENABLED(CONFIG_IPV6)
  2739. static void bond_ns_send(struct slave *slave, const struct in6_addr *daddr,
  2740. const struct in6_addr *saddr, struct bond_vlan_tag *tags)
  2741. {
  2742. struct net_device *bond_dev = slave->bond->dev;
  2743. struct net_device *slave_dev = slave->dev;
  2744. struct in6_addr mcaddr;
  2745. struct sk_buff *skb;
  2746. slave_dbg(bond_dev, slave_dev, "NS on slave: dst %pI6c src %pI6c\n",
  2747. daddr, saddr);
  2748. skb = ndisc_ns_create(slave_dev, daddr, saddr, 0);
  2749. if (!skb) {
  2750. net_err_ratelimited("NS packet allocation failed\n");
  2751. return;
  2752. }
  2753. addrconf_addr_solict_mult(daddr, &mcaddr);
  2754. if (bond_handle_vlan(slave, tags, skb)) {
  2755. slave_update_last_tx(slave);
  2756. ndisc_send_skb(skb, &mcaddr, saddr);
  2757. }
  2758. }
  2759. static void bond_ns_send_all(struct bonding *bond, struct slave *slave)
  2760. {
  2761. struct in6_addr *targets = bond->params.ns_targets;
  2762. struct bond_vlan_tag *tags;
  2763. struct dst_entry *dst;
  2764. struct in6_addr saddr;
  2765. struct flowi6 fl6;
  2766. int i;
  2767. for (i = 0; i < BOND_MAX_NS_TARGETS && !ipv6_addr_any(&targets[i]); i++) {
  2768. slave_dbg(bond->dev, slave->dev, "%s: target %pI6c\n",
  2769. __func__, &targets[i]);
  2770. tags = NULL;
  2771. /* Find out through which dev should the packet go */
  2772. memset(&fl6, 0, sizeof(struct flowi6));
  2773. fl6.daddr = targets[i];
  2774. dst = ip6_route_output(dev_net(bond->dev), NULL, &fl6);
  2775. if (dst->error) {
  2776. dst_release(dst);
  2777. /* there's no route to target - try to send arp
  2778. * probe to generate any traffic (arp_validate=0)
  2779. */
  2780. if (bond->params.arp_validate)
  2781. pr_warn_once("%s: no route to ns_ip6_target %pI6c and arp_validate is set\n",
  2782. bond->dev->name,
  2783. &targets[i]);
  2784. bond_ns_send(slave, &targets[i], &in6addr_any, tags);
  2785. continue;
  2786. }
  2787. /* bond device itself */
  2788. if (dst->dev == bond->dev)
  2789. goto found;
  2790. rcu_read_lock();
  2791. tags = bond_verify_device_path(bond->dev, dst->dev, 0);
  2792. rcu_read_unlock();
  2793. if (!IS_ERR_OR_NULL(tags))
  2794. goto found;
  2795. /* Not our device - skip */
  2796. slave_dbg(bond->dev, slave->dev, "no path to ns_ip6_target %pI6c via dst->dev %s\n",
  2797. &targets[i], dst->dev ? dst->dev->name : "NULL");
  2798. dst_release(dst);
  2799. continue;
  2800. found:
  2801. if (!ipv6_dev_get_saddr(dev_net(dst->dev), dst->dev, &targets[i], 0, &saddr))
  2802. bond_ns_send(slave, &targets[i], &saddr, tags);
  2803. else
  2804. bond_ns_send(slave, &targets[i], &in6addr_any, tags);
  2805. dst_release(dst);
  2806. kfree(tags);
  2807. }
  2808. }
  2809. static int bond_confirm_addr6(struct net_device *dev,
  2810. struct netdev_nested_priv *priv)
  2811. {
  2812. struct in6_addr *addr = (struct in6_addr *)priv->data;
  2813. return ipv6_chk_addr(dev_net(dev), addr, dev, 0);
  2814. }
  2815. static bool bond_has_this_ip6(struct bonding *bond, struct in6_addr *addr)
  2816. {
  2817. struct netdev_nested_priv priv = {
  2818. .data = addr,
  2819. };
  2820. int ret = false;
  2821. if (bond_confirm_addr6(bond->dev, &priv))
  2822. return true;
  2823. rcu_read_lock();
  2824. if (netdev_walk_all_upper_dev_rcu(bond->dev, bond_confirm_addr6, &priv))
  2825. ret = true;
  2826. rcu_read_unlock();
  2827. return ret;
  2828. }
  2829. static void bond_validate_na(struct bonding *bond, struct slave *slave,
  2830. struct in6_addr *saddr, struct in6_addr *daddr)
  2831. {
  2832. int i;
  2833. /* Ignore NAs that:
  2834. * 1. Source address is unspecified address.
  2835. * 2. Dest address is neither all-nodes multicast address nor
  2836. * exist on bond interface.
  2837. */
  2838. if (ipv6_addr_any(saddr) ||
  2839. (!ipv6_addr_equal(daddr, &in6addr_linklocal_allnodes) &&
  2840. !bond_has_this_ip6(bond, daddr))) {
  2841. slave_dbg(bond->dev, slave->dev, "%s: sip %pI6c tip %pI6c not found\n",
  2842. __func__, saddr, daddr);
  2843. return;
  2844. }
  2845. i = bond_get_targets_ip6(bond->params.ns_targets, saddr);
  2846. if (i == -1) {
  2847. slave_dbg(bond->dev, slave->dev, "%s: sip %pI6c not found in targets\n",
  2848. __func__, saddr);
  2849. return;
  2850. }
  2851. WRITE_ONCE(slave->last_rx, jiffies);
  2852. WRITE_ONCE(slave->target_last_arp_rx[i], jiffies);
  2853. }
  2854. static int bond_na_rcv(const struct sk_buff *skb, struct bonding *bond,
  2855. struct slave *slave)
  2856. {
  2857. struct slave *curr_active_slave, *curr_arp_slave;
  2858. struct in6_addr *saddr, *daddr;
  2859. struct {
  2860. struct ipv6hdr ip6;
  2861. struct icmp6hdr icmp6;
  2862. } *combined, _combined;
  2863. if (skb->pkt_type == PACKET_OTHERHOST ||
  2864. skb->pkt_type == PACKET_LOOPBACK)
  2865. goto out;
  2866. combined = skb_header_pointer(skb, 0, sizeof(_combined), &_combined);
  2867. if (!combined || combined->ip6.nexthdr != NEXTHDR_ICMP ||
  2868. (combined->icmp6.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION &&
  2869. combined->icmp6.icmp6_type != NDISC_NEIGHBOUR_ADVERTISEMENT))
  2870. goto out;
  2871. saddr = &combined->ip6.saddr;
  2872. daddr = &combined->ip6.daddr;
  2873. slave_dbg(bond->dev, slave->dev, "%s: %s/%d av %d sv %d sip %pI6c tip %pI6c\n",
  2874. __func__, slave->dev->name, bond_slave_state(slave),
  2875. bond->params.arp_validate, slave_do_arp_validate(bond, slave),
  2876. saddr, daddr);
  2877. curr_active_slave = rcu_dereference(bond->curr_active_slave);
  2878. curr_arp_slave = rcu_dereference(bond->current_arp_slave);
  2879. /* We 'trust' the received ARP enough to validate it if:
  2880. * see bond_arp_rcv().
  2881. */
  2882. if (bond_is_active_slave(slave))
  2883. bond_validate_na(bond, slave, saddr, daddr);
  2884. else if (curr_active_slave &&
  2885. time_after(slave_last_rx(bond, curr_active_slave),
  2886. curr_active_slave->last_link_up))
  2887. bond_validate_na(bond, slave, daddr, saddr);
  2888. else if (curr_arp_slave &&
  2889. bond_time_in_interval(bond, slave_last_tx(curr_arp_slave), 1))
  2890. bond_validate_na(bond, slave, saddr, daddr);
  2891. out:
  2892. return RX_HANDLER_ANOTHER;
  2893. }
  2894. #endif
  2895. int bond_rcv_validate(const struct sk_buff *skb, struct bonding *bond,
  2896. struct slave *slave)
  2897. {
  2898. #if IS_ENABLED(CONFIG_IPV6)
  2899. bool is_ipv6 = skb->protocol == __cpu_to_be16(ETH_P_IPV6);
  2900. #endif
  2901. bool is_arp = skb->protocol == __cpu_to_be16(ETH_P_ARP);
  2902. slave_dbg(bond->dev, slave->dev, "%s: skb->dev %s\n",
  2903. __func__, skb->dev->name);
  2904. /* Use arp validate logic for both ARP and NS */
  2905. if (!slave_do_arp_validate(bond, slave)) {
  2906. if ((slave_do_arp_validate_only(bond) && is_arp) ||
  2907. #if IS_ENABLED(CONFIG_IPV6)
  2908. (slave_do_arp_validate_only(bond) && is_ipv6) ||
  2909. #endif
  2910. !slave_do_arp_validate_only(bond))
  2911. WRITE_ONCE(slave->last_rx, jiffies);
  2912. return RX_HANDLER_ANOTHER;
  2913. } else if (is_arp) {
  2914. return bond_arp_rcv(skb, bond, slave);
  2915. #if IS_ENABLED(CONFIG_IPV6)
  2916. } else if (is_ipv6 && likely(ipv6_mod_enabled())) {
  2917. return bond_na_rcv(skb, bond, slave);
  2918. #endif
  2919. } else {
  2920. return RX_HANDLER_ANOTHER;
  2921. }
  2922. }
  2923. static void bond_send_validate(struct bonding *bond, struct slave *slave)
  2924. {
  2925. bond_arp_send_all(bond, slave);
  2926. #if IS_ENABLED(CONFIG_IPV6)
  2927. bond_ns_send_all(bond, slave);
  2928. #endif
  2929. }
  2930. /* function to verify if we're in the arp_interval timeslice, returns true if
  2931. * (last_act - arp_interval) <= jiffies <= (last_act + mod * arp_interval +
  2932. * arp_interval/2) . the arp_interval/2 is needed for really fast networks.
  2933. */
  2934. static bool bond_time_in_interval(struct bonding *bond, unsigned long last_act,
  2935. int mod)
  2936. {
  2937. int delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
  2938. return time_in_range(jiffies,
  2939. last_act - delta_in_ticks,
  2940. last_act + mod * delta_in_ticks + delta_in_ticks/2);
  2941. }
  2942. /* This function is called regularly to monitor each slave's link
  2943. * ensuring that traffic is being sent and received when arp monitoring
  2944. * is used in load-balancing mode. if the adapter has been dormant, then an
  2945. * arp is transmitted to generate traffic. see activebackup_arp_monitor for
  2946. * arp monitoring in active backup mode.
  2947. */
  2948. static void bond_loadbalance_arp_mon(struct bonding *bond)
  2949. {
  2950. struct slave *slave, *oldcurrent;
  2951. struct list_head *iter;
  2952. int do_failover = 0, slave_state_changed = 0;
  2953. if (!bond_has_slaves(bond))
  2954. goto re_arm;
  2955. rcu_read_lock();
  2956. oldcurrent = rcu_dereference(bond->curr_active_slave);
  2957. /* see if any of the previous devices are up now (i.e. they have
  2958. * xmt and rcv traffic). the curr_active_slave does not come into
  2959. * the picture unless it is null. also, slave->last_link_up is not
  2960. * needed here because we send an arp on each slave and give a slave
  2961. * as long as it needs to get the tx/rx within the delta.
  2962. * TODO: what about up/down delay in arp mode? it wasn't here before
  2963. * so it can wait
  2964. */
  2965. bond_for_each_slave_rcu(bond, slave, iter) {
  2966. unsigned long last_tx = slave_last_tx(slave);
  2967. bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
  2968. if (slave->link != BOND_LINK_UP) {
  2969. if (bond_time_in_interval(bond, last_tx, 1) &&
  2970. bond_time_in_interval(bond, READ_ONCE(slave->last_rx), 1)) {
  2971. bond_propose_link_state(slave, BOND_LINK_UP);
  2972. slave_state_changed = 1;
  2973. /* primary_slave has no meaning in round-robin
  2974. * mode. the window of a slave being up and
  2975. * curr_active_slave being null after enslaving
  2976. * is closed.
  2977. */
  2978. if (!oldcurrent) {
  2979. slave_info(bond->dev, slave->dev, "link status definitely up\n");
  2980. do_failover = 1;
  2981. } else {
  2982. slave_info(bond->dev, slave->dev, "interface is now up\n");
  2983. }
  2984. }
  2985. } else {
  2986. /* slave->link == BOND_LINK_UP */
  2987. /* not all switches will respond to an arp request
  2988. * when the source ip is 0, so don't take the link down
  2989. * if we don't know our ip yet
  2990. */
  2991. if (!bond_time_in_interval(bond, last_tx,
  2992. bond->params.missed_max) ||
  2993. !bond_time_in_interval(bond, READ_ONCE(slave->last_rx),
  2994. bond->params.missed_max)) {
  2995. bond_propose_link_state(slave, BOND_LINK_DOWN);
  2996. slave_state_changed = 1;
  2997. if (slave->link_failure_count < UINT_MAX)
  2998. slave->link_failure_count++;
  2999. slave_info(bond->dev, slave->dev, "interface is now down\n");
  3000. if (slave == oldcurrent)
  3001. do_failover = 1;
  3002. }
  3003. }
  3004. /* note: if switch is in round-robin mode, all links
  3005. * must tx arp to ensure all links rx an arp - otherwise
  3006. * links may oscillate or not come up at all; if switch is
  3007. * in something like xor mode, there is nothing we can
  3008. * do - all replies will be rx'ed on same link causing slaves
  3009. * to be unstable during low/no traffic periods
  3010. */
  3011. if (bond_slave_is_up(slave))
  3012. bond_send_validate(bond, slave);
  3013. }
  3014. rcu_read_unlock();
  3015. if (do_failover || slave_state_changed) {
  3016. if (!rtnl_trylock())
  3017. goto re_arm;
  3018. bond_for_each_slave(bond, slave, iter) {
  3019. if (slave->link_new_state != BOND_LINK_NOCHANGE)
  3020. slave->link = slave->link_new_state;
  3021. }
  3022. if (slave_state_changed) {
  3023. bond_slave_state_change(bond);
  3024. if (BOND_MODE(bond) == BOND_MODE_XOR)
  3025. bond_update_slave_arr(bond, NULL);
  3026. }
  3027. if (do_failover) {
  3028. block_netpoll_tx();
  3029. bond_select_active_slave(bond);
  3030. unblock_netpoll_tx();
  3031. }
  3032. rtnl_unlock();
  3033. }
  3034. re_arm:
  3035. if (bond->params.arp_interval)
  3036. queue_delayed_work(bond->wq, &bond->arp_work,
  3037. msecs_to_jiffies(bond->params.arp_interval));
  3038. }
  3039. /* Called to inspect slaves for active-backup mode ARP monitor link state
  3040. * changes. Sets proposed link state in slaves to specify what action
  3041. * should take place for the slave. Returns 0 if no changes are found, >0
  3042. * if changes to link states must be committed.
  3043. *
  3044. * Called with rcu_read_lock held.
  3045. */
  3046. static int bond_ab_arp_inspect(struct bonding *bond)
  3047. {
  3048. unsigned long last_tx, last_rx;
  3049. struct list_head *iter;
  3050. struct slave *slave;
  3051. int commit = 0;
  3052. bond_for_each_slave_rcu(bond, slave, iter) {
  3053. bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
  3054. last_rx = slave_last_rx(bond, slave);
  3055. if (slave->link != BOND_LINK_UP) {
  3056. if (bond_time_in_interval(bond, last_rx, 1)) {
  3057. bond_propose_link_state(slave, BOND_LINK_UP);
  3058. commit++;
  3059. } else if (slave->link == BOND_LINK_BACK) {
  3060. bond_propose_link_state(slave, BOND_LINK_FAIL);
  3061. commit++;
  3062. }
  3063. continue;
  3064. }
  3065. /* Give slaves 2*delta after being enslaved or made
  3066. * active. This avoids bouncing, as the last receive
  3067. * times need a full ARP monitor cycle to be updated.
  3068. */
  3069. if (bond_time_in_interval(bond, slave->last_link_up, 2))
  3070. continue;
  3071. /* Backup slave is down if:
  3072. * - No current_arp_slave AND
  3073. * - more than (missed_max+1)*delta since last receive AND
  3074. * - the bond has an IP address
  3075. *
  3076. * Note: a non-null current_arp_slave indicates
  3077. * the curr_active_slave went down and we are
  3078. * searching for a new one; under this condition
  3079. * we only take the curr_active_slave down - this
  3080. * gives each slave a chance to tx/rx traffic
  3081. * before being taken out
  3082. */
  3083. if (!bond_is_active_slave(slave) &&
  3084. !rcu_access_pointer(bond->current_arp_slave) &&
  3085. !bond_time_in_interval(bond, last_rx, bond->params.missed_max + 1)) {
  3086. bond_propose_link_state(slave, BOND_LINK_DOWN);
  3087. commit++;
  3088. }
  3089. /* Active slave is down if:
  3090. * - more than missed_max*delta since transmitting OR
  3091. * - (more than missed_max*delta since receive AND
  3092. * the bond has an IP address)
  3093. */
  3094. last_tx = slave_last_tx(slave);
  3095. if (bond_is_active_slave(slave) &&
  3096. (!bond_time_in_interval(bond, last_tx, bond->params.missed_max) ||
  3097. !bond_time_in_interval(bond, last_rx, bond->params.missed_max))) {
  3098. bond_propose_link_state(slave, BOND_LINK_DOWN);
  3099. commit++;
  3100. }
  3101. }
  3102. return commit;
  3103. }
  3104. /* Called to commit link state changes noted by inspection step of
  3105. * active-backup mode ARP monitor.
  3106. *
  3107. * Called with RTNL hold.
  3108. */
  3109. static void bond_ab_arp_commit(struct bonding *bond)
  3110. {
  3111. bool do_failover = false;
  3112. struct list_head *iter;
  3113. unsigned long last_tx;
  3114. struct slave *slave;
  3115. bond_for_each_slave(bond, slave, iter) {
  3116. switch (slave->link_new_state) {
  3117. case BOND_LINK_NOCHANGE:
  3118. continue;
  3119. case BOND_LINK_UP:
  3120. last_tx = slave_last_tx(slave);
  3121. if (rtnl_dereference(bond->curr_active_slave) != slave ||
  3122. (!rtnl_dereference(bond->curr_active_slave) &&
  3123. bond_time_in_interval(bond, last_tx, 1))) {
  3124. struct slave *current_arp_slave;
  3125. current_arp_slave = rtnl_dereference(bond->current_arp_slave);
  3126. bond_set_slave_link_state(slave, BOND_LINK_UP,
  3127. BOND_SLAVE_NOTIFY_NOW);
  3128. if (current_arp_slave) {
  3129. bond_set_slave_inactive_flags(
  3130. current_arp_slave,
  3131. BOND_SLAVE_NOTIFY_NOW);
  3132. RCU_INIT_POINTER(bond->current_arp_slave, NULL);
  3133. }
  3134. slave_info(bond->dev, slave->dev, "link status definitely up\n");
  3135. if (!rtnl_dereference(bond->curr_active_slave) ||
  3136. slave == rtnl_dereference(bond->primary_slave) ||
  3137. slave->prio > rtnl_dereference(bond->curr_active_slave)->prio)
  3138. do_failover = true;
  3139. }
  3140. continue;
  3141. case BOND_LINK_DOWN:
  3142. if (slave->link_failure_count < UINT_MAX)
  3143. slave->link_failure_count++;
  3144. bond_set_slave_link_state(slave, BOND_LINK_DOWN,
  3145. BOND_SLAVE_NOTIFY_NOW);
  3146. bond_set_slave_inactive_flags(slave,
  3147. BOND_SLAVE_NOTIFY_NOW);
  3148. slave_info(bond->dev, slave->dev, "link status definitely down, disabling slave\n");
  3149. if (slave == rtnl_dereference(bond->curr_active_slave)) {
  3150. RCU_INIT_POINTER(bond->current_arp_slave, NULL);
  3151. do_failover = true;
  3152. }
  3153. continue;
  3154. case BOND_LINK_FAIL:
  3155. bond_set_slave_link_state(slave, BOND_LINK_FAIL,
  3156. BOND_SLAVE_NOTIFY_NOW);
  3157. bond_set_slave_inactive_flags(slave,
  3158. BOND_SLAVE_NOTIFY_NOW);
  3159. /* A slave has just been enslaved and has become
  3160. * the current active slave.
  3161. */
  3162. if (rtnl_dereference(bond->curr_active_slave))
  3163. RCU_INIT_POINTER(bond->current_arp_slave, NULL);
  3164. continue;
  3165. default:
  3166. slave_err(bond->dev, slave->dev,
  3167. "impossible: link_new_state %d on slave\n",
  3168. slave->link_new_state);
  3169. continue;
  3170. }
  3171. }
  3172. if (do_failover) {
  3173. block_netpoll_tx();
  3174. bond_select_active_slave(bond);
  3175. unblock_netpoll_tx();
  3176. }
  3177. bond_set_carrier(bond);
  3178. }
  3179. /* Send ARP probes for active-backup mode ARP monitor.
  3180. *
  3181. * Called with rcu_read_lock held.
  3182. */
  3183. static bool bond_ab_arp_probe(struct bonding *bond)
  3184. {
  3185. struct slave *slave, *before = NULL, *new_slave = NULL,
  3186. *curr_arp_slave = rcu_dereference(bond->current_arp_slave),
  3187. *curr_active_slave = rcu_dereference(bond->curr_active_slave);
  3188. struct list_head *iter;
  3189. bool found = false;
  3190. bool should_notify_rtnl = BOND_SLAVE_NOTIFY_LATER;
  3191. if (curr_arp_slave && curr_active_slave)
  3192. netdev_info(bond->dev, "PROBE: c_arp %s && cas %s BAD\n",
  3193. curr_arp_slave->dev->name,
  3194. curr_active_slave->dev->name);
  3195. if (curr_active_slave) {
  3196. bond_send_validate(bond, curr_active_slave);
  3197. return should_notify_rtnl;
  3198. }
  3199. /* if we don't have a curr_active_slave, search for the next available
  3200. * backup slave from the current_arp_slave and make it the candidate
  3201. * for becoming the curr_active_slave
  3202. */
  3203. if (!curr_arp_slave) {
  3204. curr_arp_slave = bond_first_slave_rcu(bond);
  3205. if (!curr_arp_slave)
  3206. return should_notify_rtnl;
  3207. }
  3208. bond_for_each_slave_rcu(bond, slave, iter) {
  3209. if (!found && !before && bond_slave_is_up(slave))
  3210. before = slave;
  3211. if (found && !new_slave && bond_slave_is_up(slave))
  3212. new_slave = slave;
  3213. /* if the link state is up at this point, we
  3214. * mark it down - this can happen if we have
  3215. * simultaneous link failures and
  3216. * reselect_active_interface doesn't make this
  3217. * one the current slave so it is still marked
  3218. * up when it is actually down
  3219. */
  3220. if (!bond_slave_is_up(slave) && slave->link == BOND_LINK_UP) {
  3221. bond_set_slave_link_state(slave, BOND_LINK_DOWN,
  3222. BOND_SLAVE_NOTIFY_LATER);
  3223. if (slave->link_failure_count < UINT_MAX)
  3224. slave->link_failure_count++;
  3225. bond_set_slave_inactive_flags(slave,
  3226. BOND_SLAVE_NOTIFY_LATER);
  3227. slave_info(bond->dev, slave->dev, "backup interface is now down\n");
  3228. }
  3229. if (slave == curr_arp_slave)
  3230. found = true;
  3231. }
  3232. if (!new_slave && before)
  3233. new_slave = before;
  3234. if (!new_slave)
  3235. goto check_state;
  3236. bond_set_slave_link_state(new_slave, BOND_LINK_BACK,
  3237. BOND_SLAVE_NOTIFY_LATER);
  3238. bond_set_slave_active_flags(new_slave, BOND_SLAVE_NOTIFY_LATER);
  3239. bond_send_validate(bond, new_slave);
  3240. new_slave->last_link_up = jiffies;
  3241. rcu_assign_pointer(bond->current_arp_slave, new_slave);
  3242. check_state:
  3243. bond_for_each_slave_rcu(bond, slave, iter) {
  3244. if (slave->should_notify || slave->should_notify_link) {
  3245. should_notify_rtnl = BOND_SLAVE_NOTIFY_NOW;
  3246. break;
  3247. }
  3248. }
  3249. return should_notify_rtnl;
  3250. }
  3251. static void bond_activebackup_arp_mon(struct bonding *bond)
  3252. {
  3253. bool should_notify_rtnl;
  3254. int delta_in_ticks;
  3255. delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
  3256. if (!bond_has_slaves(bond))
  3257. goto re_arm;
  3258. rcu_read_lock();
  3259. if (bond_ab_arp_inspect(bond)) {
  3260. rcu_read_unlock();
  3261. /* Race avoidance with bond_close flush of workqueue */
  3262. if (!rtnl_trylock()) {
  3263. delta_in_ticks = 1;
  3264. goto re_arm;
  3265. }
  3266. bond_ab_arp_commit(bond);
  3267. rtnl_unlock();
  3268. rcu_read_lock();
  3269. }
  3270. should_notify_rtnl = bond_ab_arp_probe(bond);
  3271. rcu_read_unlock();
  3272. if (READ_ONCE(bond->send_peer_notif) || should_notify_rtnl) {
  3273. if (!rtnl_trylock()) {
  3274. delta_in_ticks = 1;
  3275. goto re_arm;
  3276. }
  3277. if (bond->send_peer_notif)
  3278. bond_peer_notify_may_events(bond, true);
  3279. if (should_notify_rtnl) {
  3280. bond_slave_state_notify(bond);
  3281. bond_slave_link_notify(bond);
  3282. }
  3283. rtnl_unlock();
  3284. }
  3285. re_arm:
  3286. if (bond->params.arp_interval)
  3287. queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
  3288. }
  3289. static void bond_arp_monitor(struct work_struct *work)
  3290. {
  3291. struct bonding *bond = container_of(work, struct bonding,
  3292. arp_work.work);
  3293. if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP)
  3294. bond_activebackup_arp_mon(bond);
  3295. else
  3296. bond_loadbalance_arp_mon(bond);
  3297. }
  3298. /*-------------------------- netdev event handling --------------------------*/
  3299. /* Change device name */
  3300. static int bond_event_changename(struct bonding *bond)
  3301. {
  3302. bond_remove_proc_entry(bond);
  3303. bond_create_proc_entry(bond);
  3304. bond_debug_reregister(bond);
  3305. return NOTIFY_DONE;
  3306. }
  3307. static int bond_master_netdev_event(unsigned long event,
  3308. struct net_device *bond_dev)
  3309. {
  3310. struct bonding *event_bond = netdev_priv(bond_dev);
  3311. netdev_dbg(bond_dev, "%s called\n", __func__);
  3312. switch (event) {
  3313. case NETDEV_CHANGENAME:
  3314. return bond_event_changename(event_bond);
  3315. case NETDEV_UNREGISTER:
  3316. bond_remove_proc_entry(event_bond);
  3317. #ifdef CONFIG_XFRM_OFFLOAD
  3318. xfrm_dev_state_flush(dev_net(bond_dev), bond_dev, true);
  3319. #endif /* CONFIG_XFRM_OFFLOAD */
  3320. break;
  3321. case NETDEV_REGISTER:
  3322. bond_create_proc_entry(event_bond);
  3323. break;
  3324. default:
  3325. break;
  3326. }
  3327. return NOTIFY_DONE;
  3328. }
  3329. static int bond_slave_netdev_event(unsigned long event,
  3330. struct net_device *slave_dev)
  3331. {
  3332. struct slave *slave = bond_slave_get_rtnl(slave_dev), *primary;
  3333. struct bonding *bond;
  3334. struct net_device *bond_dev;
  3335. /* A netdev event can be generated while enslaving a device
  3336. * before netdev_rx_handler_register is called in which case
  3337. * slave will be NULL
  3338. */
  3339. if (!slave) {
  3340. netdev_dbg(slave_dev, "%s called on NULL slave\n", __func__);
  3341. return NOTIFY_DONE;
  3342. }
  3343. bond_dev = slave->bond->dev;
  3344. bond = slave->bond;
  3345. primary = rtnl_dereference(bond->primary_slave);
  3346. slave_dbg(bond_dev, slave_dev, "%s called\n", __func__);
  3347. switch (event) {
  3348. case NETDEV_UNREGISTER:
  3349. if (bond_dev->type != ARPHRD_ETHER)
  3350. bond_release_and_destroy(bond_dev, slave_dev);
  3351. else
  3352. __bond_release_one(bond_dev, slave_dev, false, true);
  3353. break;
  3354. case NETDEV_UP:
  3355. case NETDEV_CHANGE:
  3356. /* For 802.3ad mode only:
  3357. * Getting invalid Speed/Duplex values here will put slave
  3358. * in weird state. Mark it as link-fail if the link was
  3359. * previously up or link-down if it hasn't yet come up, and
  3360. * let link-monitoring (miimon) set it right when correct
  3361. * speeds/duplex are available.
  3362. */
  3363. if (bond_update_speed_duplex(slave) &&
  3364. BOND_MODE(bond) == BOND_MODE_8023AD) {
  3365. if (slave->last_link_up)
  3366. slave->link = BOND_LINK_FAIL;
  3367. else
  3368. slave->link = BOND_LINK_DOWN;
  3369. }
  3370. if (BOND_MODE(bond) == BOND_MODE_8023AD)
  3371. bond_3ad_adapter_speed_duplex_changed(slave);
  3372. fallthrough;
  3373. case NETDEV_DOWN:
  3374. /* Refresh slave-array if applicable!
  3375. * If the setup does not use miimon or arpmon (mode-specific!),
  3376. * then these events will not cause the slave-array to be
  3377. * refreshed. This will cause xmit to use a slave that is not
  3378. * usable. Avoid such situation by refeshing the array at these
  3379. * events. If these (miimon/arpmon) parameters are configured
  3380. * then array gets refreshed twice and that should be fine!
  3381. */
  3382. if (bond_mode_can_use_xmit_hash(bond))
  3383. bond_update_slave_arr(bond, NULL);
  3384. break;
  3385. case NETDEV_CHANGEMTU:
  3386. /* TODO: Should slaves be allowed to
  3387. * independently alter their MTU? For
  3388. * an active-backup bond, slaves need
  3389. * not be the same type of device, so
  3390. * MTUs may vary. For other modes,
  3391. * slaves arguably should have the
  3392. * same MTUs. To do this, we'd need to
  3393. * take over the slave's change_mtu
  3394. * function for the duration of their
  3395. * servitude.
  3396. */
  3397. break;
  3398. case NETDEV_CHANGENAME:
  3399. /* we don't care if we don't have primary set */
  3400. if (!bond_uses_primary(bond) ||
  3401. !bond->params.primary[0])
  3402. break;
  3403. if (slave == primary) {
  3404. /* slave's name changed - he's no longer primary */
  3405. RCU_INIT_POINTER(bond->primary_slave, NULL);
  3406. } else if (!strcmp(slave_dev->name, bond->params.primary)) {
  3407. /* we have a new primary slave */
  3408. rcu_assign_pointer(bond->primary_slave, slave);
  3409. } else { /* we didn't change primary - exit */
  3410. break;
  3411. }
  3412. netdev_info(bond->dev, "Primary slave changed to %s, reselecting active slave\n",
  3413. primary ? slave_dev->name : "none");
  3414. block_netpoll_tx();
  3415. bond_select_active_slave(bond);
  3416. unblock_netpoll_tx();
  3417. break;
  3418. case NETDEV_FEAT_CHANGE:
  3419. if (!bond->notifier_ctx) {
  3420. bond->notifier_ctx = true;
  3421. netdev_compute_master_upper_features(bond->dev, true);
  3422. bond->notifier_ctx = false;
  3423. }
  3424. break;
  3425. case NETDEV_RESEND_IGMP:
  3426. /* Propagate to master device */
  3427. call_netdevice_notifiers(event, slave->bond->dev);
  3428. break;
  3429. case NETDEV_XDP_FEAT_CHANGE:
  3430. bond_xdp_set_features(bond_dev);
  3431. break;
  3432. default:
  3433. break;
  3434. }
  3435. return NOTIFY_DONE;
  3436. }
  3437. /* bond_netdev_event: handle netdev notifier chain events.
  3438. *
  3439. * This function receives events for the netdev chain. The caller (an
  3440. * ioctl handler calling blocking_notifier_call_chain) holds the necessary
  3441. * locks for us to safely manipulate the slave devices (RTNL lock,
  3442. * dev_probe_lock).
  3443. */
  3444. static int bond_netdev_event(struct notifier_block *this,
  3445. unsigned long event, void *ptr)
  3446. {
  3447. struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
  3448. netdev_dbg(event_dev, "%s received %s\n",
  3449. __func__, netdev_cmd_to_name(event));
  3450. if (!(event_dev->priv_flags & IFF_BONDING))
  3451. return NOTIFY_DONE;
  3452. if (event_dev->flags & IFF_MASTER) {
  3453. int ret;
  3454. ret = bond_master_netdev_event(event, event_dev);
  3455. if (ret != NOTIFY_DONE)
  3456. return ret;
  3457. }
  3458. if (event_dev->flags & IFF_SLAVE)
  3459. return bond_slave_netdev_event(event, event_dev);
  3460. return NOTIFY_DONE;
  3461. }
  3462. static struct notifier_block bond_netdev_notifier = {
  3463. .notifier_call = bond_netdev_event,
  3464. };
  3465. /*---------------------------- Hashing Policies -----------------------------*/
  3466. /* Helper to access data in a packet, with or without a backing skb.
  3467. * If skb is given the data is linearized if necessary via pskb_may_pull.
  3468. */
  3469. static inline const void *bond_pull_data(struct sk_buff *skb,
  3470. const void *data, int hlen, int n)
  3471. {
  3472. if (likely(n <= hlen))
  3473. return data;
  3474. else if (skb && likely(pskb_may_pull(skb, n)))
  3475. return skb->data;
  3476. return NULL;
  3477. }
  3478. /* L2 hash helper */
  3479. static inline u32 bond_eth_hash(struct sk_buff *skb, const void *data, int mhoff, int hlen)
  3480. {
  3481. struct ethhdr *ep;
  3482. data = bond_pull_data(skb, data, hlen, mhoff + sizeof(struct ethhdr));
  3483. if (!data)
  3484. return 0;
  3485. ep = (struct ethhdr *)(data + mhoff);
  3486. return ep->h_dest[5] ^ ep->h_source[5] ^ be16_to_cpu(ep->h_proto);
  3487. }
  3488. static bool bond_flow_ip(struct sk_buff *skb, struct flow_keys *fk, const void *data,
  3489. int hlen, __be16 l2_proto, int *nhoff, int *ip_proto, bool l34)
  3490. {
  3491. const struct ipv6hdr *iph6;
  3492. const struct iphdr *iph;
  3493. if (l2_proto == htons(ETH_P_IP)) {
  3494. data = bond_pull_data(skb, data, hlen, *nhoff + sizeof(*iph));
  3495. if (!data)
  3496. return false;
  3497. iph = (const struct iphdr *)(data + *nhoff);
  3498. iph_to_flow_copy_v4addrs(fk, iph);
  3499. *nhoff += iph->ihl << 2;
  3500. if (!ip_is_fragment(iph))
  3501. *ip_proto = iph->protocol;
  3502. } else if (l2_proto == htons(ETH_P_IPV6)) {
  3503. data = bond_pull_data(skb, data, hlen, *nhoff + sizeof(*iph6));
  3504. if (!data)
  3505. return false;
  3506. iph6 = (const struct ipv6hdr *)(data + *nhoff);
  3507. iph_to_flow_copy_v6addrs(fk, iph6);
  3508. *nhoff += sizeof(*iph6);
  3509. *ip_proto = iph6->nexthdr;
  3510. } else {
  3511. return false;
  3512. }
  3513. if (l34 && *ip_proto >= 0)
  3514. fk->ports.ports = skb_flow_get_ports(skb, *nhoff, *ip_proto, data, hlen);
  3515. return true;
  3516. }
  3517. static u32 bond_vlan_srcmac_hash(struct sk_buff *skb, const void *data, int mhoff, int hlen)
  3518. {
  3519. u32 srcmac_vendor = 0, srcmac_dev = 0;
  3520. struct ethhdr *mac_hdr;
  3521. u16 vlan = 0;
  3522. int i;
  3523. data = bond_pull_data(skb, data, hlen, mhoff + sizeof(struct ethhdr));
  3524. if (!data)
  3525. return 0;
  3526. mac_hdr = (struct ethhdr *)(data + mhoff);
  3527. for (i = 0; i < 3; i++)
  3528. srcmac_vendor = (srcmac_vendor << 8) | mac_hdr->h_source[i];
  3529. for (i = 3; i < ETH_ALEN; i++)
  3530. srcmac_dev = (srcmac_dev << 8) | mac_hdr->h_source[i];
  3531. if (skb && skb_vlan_tag_present(skb))
  3532. vlan = skb_vlan_tag_get(skb);
  3533. return vlan ^ srcmac_vendor ^ srcmac_dev;
  3534. }
  3535. /* Extract the appropriate headers based on bond's xmit policy */
  3536. static bool bond_flow_dissect(struct bonding *bond, struct sk_buff *skb, const void *data,
  3537. __be16 l2_proto, int nhoff, int hlen, struct flow_keys *fk)
  3538. {
  3539. bool l34 = bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34;
  3540. int ip_proto = -1;
  3541. switch (bond->params.xmit_policy) {
  3542. case BOND_XMIT_POLICY_ENCAP23:
  3543. case BOND_XMIT_POLICY_ENCAP34:
  3544. memset(fk, 0, sizeof(*fk));
  3545. return __skb_flow_dissect(dev_net(bond->dev), skb,
  3546. &flow_keys_bonding, fk, data,
  3547. l2_proto, nhoff, hlen, 0);
  3548. default:
  3549. break;
  3550. }
  3551. fk->ports.ports = 0;
  3552. memset(&fk->icmp, 0, sizeof(fk->icmp));
  3553. if (!bond_flow_ip(skb, fk, data, hlen, l2_proto, &nhoff, &ip_proto, l34))
  3554. return false;
  3555. /* ICMP error packets contains at least 8 bytes of the header
  3556. * of the packet which generated the error. Use this information
  3557. * to correlate ICMP error packets within the same flow which
  3558. * generated the error.
  3559. */
  3560. if (ip_proto == IPPROTO_ICMP || ip_proto == IPPROTO_ICMPV6) {
  3561. skb_flow_get_icmp_tci(skb, &fk->icmp, data, nhoff, hlen);
  3562. if (ip_proto == IPPROTO_ICMP) {
  3563. if (!icmp_is_err(fk->icmp.type))
  3564. return true;
  3565. nhoff += sizeof(struct icmphdr);
  3566. } else if (ip_proto == IPPROTO_ICMPV6) {
  3567. if (!icmpv6_is_err(fk->icmp.type))
  3568. return true;
  3569. nhoff += sizeof(struct icmp6hdr);
  3570. }
  3571. return bond_flow_ip(skb, fk, data, hlen, l2_proto, &nhoff, &ip_proto, l34);
  3572. }
  3573. return true;
  3574. }
  3575. static u32 bond_ip_hash(u32 hash, struct flow_keys *flow, int xmit_policy)
  3576. {
  3577. hash ^= (__force u32)flow_get_u32_dst(flow) ^
  3578. (__force u32)flow_get_u32_src(flow);
  3579. hash ^= (hash >> 16);
  3580. hash ^= (hash >> 8);
  3581. /* discard lowest hash bit to deal with the common even ports pattern */
  3582. if (xmit_policy == BOND_XMIT_POLICY_LAYER34 ||
  3583. xmit_policy == BOND_XMIT_POLICY_ENCAP34)
  3584. return hash >> 1;
  3585. return hash;
  3586. }
  3587. /* Generate hash based on xmit policy. If @skb is given it is used to linearize
  3588. * the data as required, but this function can be used without it if the data is
  3589. * known to be linear (e.g. with xdp_buff).
  3590. */
  3591. static u32 __bond_xmit_hash(struct bonding *bond, struct sk_buff *skb, const void *data,
  3592. __be16 l2_proto, int mhoff, int nhoff, int hlen)
  3593. {
  3594. struct flow_keys flow;
  3595. u32 hash;
  3596. if (bond->params.xmit_policy == BOND_XMIT_POLICY_VLAN_SRCMAC)
  3597. return bond_vlan_srcmac_hash(skb, data, mhoff, hlen);
  3598. if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER2 ||
  3599. !bond_flow_dissect(bond, skb, data, l2_proto, nhoff, hlen, &flow))
  3600. return bond_eth_hash(skb, data, mhoff, hlen);
  3601. if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER23 ||
  3602. bond->params.xmit_policy == BOND_XMIT_POLICY_ENCAP23) {
  3603. hash = bond_eth_hash(skb, data, mhoff, hlen);
  3604. } else {
  3605. if (flow.icmp.id)
  3606. memcpy(&hash, &flow.icmp, sizeof(hash));
  3607. else
  3608. memcpy(&hash, &flow.ports.ports, sizeof(hash));
  3609. }
  3610. return bond_ip_hash(hash, &flow, bond->params.xmit_policy);
  3611. }
  3612. /**
  3613. * bond_xmit_hash - generate a hash value based on the xmit policy
  3614. * @bond: bonding device
  3615. * @skb: buffer to use for headers
  3616. *
  3617. * This function will extract the necessary headers from the skb buffer and use
  3618. * them to generate a hash based on the xmit_policy set in the bonding device
  3619. */
  3620. u32 bond_xmit_hash(struct bonding *bond, struct sk_buff *skb)
  3621. {
  3622. if (bond->params.xmit_policy == BOND_XMIT_POLICY_ENCAP34 &&
  3623. skb->l4_hash)
  3624. return skb->hash;
  3625. return __bond_xmit_hash(bond, skb, skb->data, skb->protocol,
  3626. 0, skb_network_offset(skb),
  3627. skb_headlen(skb));
  3628. }
  3629. /**
  3630. * bond_xmit_hash_xdp - generate a hash value based on the xmit policy
  3631. * @bond: bonding device
  3632. * @xdp: buffer to use for headers
  3633. *
  3634. * The XDP variant of bond_xmit_hash.
  3635. */
  3636. static u32 bond_xmit_hash_xdp(struct bonding *bond, struct xdp_buff *xdp)
  3637. {
  3638. struct ethhdr *eth;
  3639. if (xdp->data + sizeof(struct ethhdr) > xdp->data_end)
  3640. return 0;
  3641. eth = (struct ethhdr *)xdp->data;
  3642. return __bond_xmit_hash(bond, NULL, xdp->data, eth->h_proto, 0,
  3643. sizeof(struct ethhdr), xdp->data_end - xdp->data);
  3644. }
  3645. /*-------------------------- Device entry points ----------------------------*/
  3646. void bond_work_init_all(struct bonding *bond)
  3647. {
  3648. /* ndo_stop, bond_close() will try to flush the work under
  3649. * the rtnl lock. The workqueue must not block on rtnl lock
  3650. * to avoid deadlock.
  3651. */
  3652. INIT_DELAYED_WORK(&bond->mcast_work,
  3653. bond_resend_igmp_join_requests_delayed);
  3654. INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
  3655. INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
  3656. INIT_DELAYED_WORK(&bond->arp_work, bond_arp_monitor);
  3657. INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
  3658. INIT_DELAYED_WORK(&bond->slave_arr_work, bond_slave_arr_handler);
  3659. INIT_DELAYED_WORK(&bond->peer_notify_work, bond_peer_notify_handler);
  3660. }
  3661. void bond_work_cancel_all(struct bonding *bond)
  3662. {
  3663. cancel_delayed_work_sync(&bond->mii_work);
  3664. cancel_delayed_work_sync(&bond->arp_work);
  3665. cancel_delayed_work_sync(&bond->alb_work);
  3666. cancel_delayed_work_sync(&bond->ad_work);
  3667. cancel_delayed_work_sync(&bond->mcast_work);
  3668. cancel_delayed_work_sync(&bond->slave_arr_work);
  3669. cancel_delayed_work_sync(&bond->peer_notify_work);
  3670. }
  3671. static int bond_open(struct net_device *bond_dev)
  3672. {
  3673. struct bonding *bond = netdev_priv(bond_dev);
  3674. struct list_head *iter;
  3675. struct slave *slave;
  3676. if (BOND_MODE(bond) == BOND_MODE_ROUNDROBIN && !bond->rr_tx_counter) {
  3677. bond->rr_tx_counter = alloc_percpu(u32);
  3678. if (!bond->rr_tx_counter)
  3679. return -ENOMEM;
  3680. }
  3681. /* reset slave->backup and slave->inactive */
  3682. if (bond_has_slaves(bond)) {
  3683. bond_for_each_slave(bond, slave, iter) {
  3684. if (bond_uses_primary(bond) &&
  3685. slave != rcu_access_pointer(bond->curr_active_slave)) {
  3686. bond_set_slave_inactive_flags(slave,
  3687. BOND_SLAVE_NOTIFY_NOW);
  3688. } else if (BOND_MODE(bond) != BOND_MODE_8023AD) {
  3689. bond_set_slave_active_flags(slave,
  3690. BOND_SLAVE_NOTIFY_NOW);
  3691. }
  3692. }
  3693. }
  3694. if (bond_is_lb(bond)) {
  3695. /* bond_alb_initialize must be called before the timer
  3696. * is started.
  3697. */
  3698. if (bond_alb_initialize(bond, (BOND_MODE(bond) == BOND_MODE_ALB)))
  3699. return -ENOMEM;
  3700. if (bond->params.tlb_dynamic_lb || BOND_MODE(bond) == BOND_MODE_ALB)
  3701. queue_delayed_work(bond->wq, &bond->alb_work, 0);
  3702. }
  3703. if (bond->params.miimon) /* link check interval, in milliseconds. */
  3704. queue_delayed_work(bond->wq, &bond->mii_work, 0);
  3705. if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
  3706. queue_delayed_work(bond->wq, &bond->arp_work, 0);
  3707. bond->recv_probe = bond_rcv_validate;
  3708. }
  3709. if (BOND_MODE(bond) == BOND_MODE_8023AD) {
  3710. queue_delayed_work(bond->wq, &bond->ad_work, 0);
  3711. /* register to receive LACPDUs */
  3712. bond->recv_probe = bond_3ad_lacpdu_recv;
  3713. bond_3ad_initiate_agg_selection(bond, 1);
  3714. bond_for_each_slave(bond, slave, iter)
  3715. dev_mc_add(slave->dev, lacpdu_mcast_addr);
  3716. if (bond->params.broadcast_neighbor)
  3717. static_branch_inc(&bond_bcast_neigh_enabled);
  3718. }
  3719. if (bond_mode_can_use_xmit_hash(bond))
  3720. bond_update_slave_arr(bond, NULL);
  3721. return 0;
  3722. }
  3723. static int bond_close(struct net_device *bond_dev)
  3724. {
  3725. struct bonding *bond = netdev_priv(bond_dev);
  3726. struct slave *slave;
  3727. bond_work_cancel_all(bond);
  3728. bond->send_peer_notif = 0;
  3729. WRITE_ONCE(bond->recv_probe, NULL);
  3730. /* Wait for any in-flight RX handlers */
  3731. synchronize_net();
  3732. if (bond_is_lb(bond))
  3733. bond_alb_deinitialize(bond);
  3734. if (BOND_MODE(bond) == BOND_MODE_8023AD &&
  3735. bond->params.broadcast_neighbor)
  3736. static_branch_dec(&bond_bcast_neigh_enabled);
  3737. if (bond_uses_primary(bond)) {
  3738. rcu_read_lock();
  3739. slave = rcu_dereference(bond->curr_active_slave);
  3740. if (slave)
  3741. bond_hw_addr_flush(bond_dev, slave->dev);
  3742. rcu_read_unlock();
  3743. } else {
  3744. struct list_head *iter;
  3745. bond_for_each_slave(bond, slave, iter)
  3746. bond_hw_addr_flush(bond_dev, slave->dev);
  3747. }
  3748. return 0;
  3749. }
  3750. /* fold stats, assuming all rtnl_link_stats64 fields are u64, but
  3751. * that some drivers can provide 32bit values only.
  3752. */
  3753. static void bond_fold_stats(struct rtnl_link_stats64 *_res,
  3754. const struct rtnl_link_stats64 *_new,
  3755. const struct rtnl_link_stats64 *_old)
  3756. {
  3757. const u64 *new = (const u64 *)_new;
  3758. const u64 *old = (const u64 *)_old;
  3759. u64 *res = (u64 *)_res;
  3760. int i;
  3761. for (i = 0; i < sizeof(*_res) / sizeof(u64); i++) {
  3762. u64 nv = new[i];
  3763. u64 ov = old[i];
  3764. s64 delta = nv - ov;
  3765. /* detects if this particular field is 32bit only */
  3766. if (((nv | ov) >> 32) == 0)
  3767. delta = (s64)(s32)((u32)nv - (u32)ov);
  3768. /* filter anomalies, some drivers reset their stats
  3769. * at down/up events.
  3770. */
  3771. if (delta > 0)
  3772. res[i] += delta;
  3773. }
  3774. }
  3775. #ifdef CONFIG_LOCKDEP
  3776. static int bond_get_lowest_level_rcu(struct net_device *dev)
  3777. {
  3778. struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
  3779. struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
  3780. int cur = 0, max = 0;
  3781. now = dev;
  3782. iter = &dev->adj_list.lower;
  3783. while (1) {
  3784. next = NULL;
  3785. while (1) {
  3786. ldev = netdev_next_lower_dev_rcu(now, &iter);
  3787. if (!ldev)
  3788. break;
  3789. next = ldev;
  3790. niter = &ldev->adj_list.lower;
  3791. dev_stack[cur] = now;
  3792. iter_stack[cur++] = iter;
  3793. if (max <= cur)
  3794. max = cur;
  3795. break;
  3796. }
  3797. if (!next) {
  3798. if (!cur)
  3799. return max;
  3800. next = dev_stack[--cur];
  3801. niter = iter_stack[cur];
  3802. }
  3803. now = next;
  3804. iter = niter;
  3805. }
  3806. return max;
  3807. }
  3808. #endif
  3809. static void bond_get_stats(struct net_device *bond_dev,
  3810. struct rtnl_link_stats64 *stats)
  3811. {
  3812. struct bonding *bond = netdev_priv(bond_dev);
  3813. struct rtnl_link_stats64 temp;
  3814. struct list_head *iter;
  3815. struct slave *slave;
  3816. int nest_level = 0;
  3817. rcu_read_lock();
  3818. #ifdef CONFIG_LOCKDEP
  3819. nest_level = bond_get_lowest_level_rcu(bond_dev);
  3820. #endif
  3821. spin_lock_nested(&bond->stats_lock, nest_level);
  3822. memcpy(stats, &bond->bond_stats, sizeof(*stats));
  3823. bond_for_each_slave_rcu(bond, slave, iter) {
  3824. const struct rtnl_link_stats64 *new =
  3825. dev_get_stats(slave->dev, &temp);
  3826. bond_fold_stats(stats, new, &slave->slave_stats);
  3827. /* save off the slave stats for the next run */
  3828. memcpy(&slave->slave_stats, new, sizeof(*new));
  3829. }
  3830. memcpy(&bond->bond_stats, stats, sizeof(*stats));
  3831. spin_unlock(&bond->stats_lock);
  3832. rcu_read_unlock();
  3833. }
  3834. static int bond_eth_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
  3835. {
  3836. struct bonding *bond = netdev_priv(bond_dev);
  3837. struct mii_ioctl_data *mii = NULL;
  3838. netdev_dbg(bond_dev, "bond_eth_ioctl: cmd=%d\n", cmd);
  3839. switch (cmd) {
  3840. case SIOCGMIIPHY:
  3841. mii = if_mii(ifr);
  3842. if (!mii)
  3843. return -EINVAL;
  3844. mii->phy_id = 0;
  3845. fallthrough;
  3846. case SIOCGMIIREG:
  3847. /* We do this again just in case we were called by SIOCGMIIREG
  3848. * instead of SIOCGMIIPHY.
  3849. */
  3850. mii = if_mii(ifr);
  3851. if (!mii)
  3852. return -EINVAL;
  3853. if (mii->reg_num == 1) {
  3854. mii->val_out = 0;
  3855. if (netif_carrier_ok(bond->dev))
  3856. mii->val_out = BMSR_LSTATUS;
  3857. }
  3858. break;
  3859. default:
  3860. return -EOPNOTSUPP;
  3861. }
  3862. return 0;
  3863. }
  3864. static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
  3865. {
  3866. struct bonding *bond = netdev_priv(bond_dev);
  3867. struct net_device *slave_dev = NULL;
  3868. struct ifbond k_binfo;
  3869. struct ifbond __user *u_binfo = NULL;
  3870. struct ifslave k_sinfo;
  3871. struct ifslave __user *u_sinfo = NULL;
  3872. struct bond_opt_value newval;
  3873. struct net *net;
  3874. int res = 0;
  3875. netdev_dbg(bond_dev, "bond_ioctl: cmd=%d\n", cmd);
  3876. switch (cmd) {
  3877. case SIOCBONDINFOQUERY:
  3878. u_binfo = (struct ifbond __user *)ifr->ifr_data;
  3879. if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
  3880. return -EFAULT;
  3881. bond_info_query(bond_dev, &k_binfo);
  3882. if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
  3883. return -EFAULT;
  3884. return 0;
  3885. case SIOCBONDSLAVEINFOQUERY:
  3886. u_sinfo = (struct ifslave __user *)ifr->ifr_data;
  3887. if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
  3888. return -EFAULT;
  3889. res = bond_slave_info_query(bond_dev, &k_sinfo);
  3890. if (res == 0 &&
  3891. copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
  3892. return -EFAULT;
  3893. return res;
  3894. default:
  3895. break;
  3896. }
  3897. net = dev_net(bond_dev);
  3898. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  3899. return -EPERM;
  3900. slave_dev = __dev_get_by_name(net, ifr->ifr_slave);
  3901. slave_dbg(bond_dev, slave_dev, "slave_dev=%p:\n", slave_dev);
  3902. if (!slave_dev)
  3903. return -ENODEV;
  3904. switch (cmd) {
  3905. case SIOCBONDENSLAVE:
  3906. res = bond_enslave(bond_dev, slave_dev, NULL);
  3907. break;
  3908. case SIOCBONDRELEASE:
  3909. res = bond_release(bond_dev, slave_dev);
  3910. break;
  3911. case SIOCBONDSETHWADDR:
  3912. res = bond_set_dev_addr(bond_dev, slave_dev);
  3913. break;
  3914. case SIOCBONDCHANGEACTIVE:
  3915. bond_opt_initstr(&newval, slave_dev->name);
  3916. res = __bond_opt_set_notify(bond, BOND_OPT_ACTIVE_SLAVE,
  3917. &newval);
  3918. break;
  3919. default:
  3920. res = -EOPNOTSUPP;
  3921. }
  3922. return res;
  3923. }
  3924. static int bond_siocdevprivate(struct net_device *bond_dev, struct ifreq *ifr,
  3925. void __user *data, int cmd)
  3926. {
  3927. struct ifreq ifrdata = { .ifr_data = data };
  3928. switch (cmd) {
  3929. case BOND_INFO_QUERY_OLD:
  3930. return bond_do_ioctl(bond_dev, &ifrdata, SIOCBONDINFOQUERY);
  3931. case BOND_SLAVE_INFO_QUERY_OLD:
  3932. return bond_do_ioctl(bond_dev, &ifrdata, SIOCBONDSLAVEINFOQUERY);
  3933. case BOND_ENSLAVE_OLD:
  3934. return bond_do_ioctl(bond_dev, ifr, SIOCBONDENSLAVE);
  3935. case BOND_RELEASE_OLD:
  3936. return bond_do_ioctl(bond_dev, ifr, SIOCBONDRELEASE);
  3937. case BOND_SETHWADDR_OLD:
  3938. return bond_do_ioctl(bond_dev, ifr, SIOCBONDSETHWADDR);
  3939. case BOND_CHANGE_ACTIVE_OLD:
  3940. return bond_do_ioctl(bond_dev, ifr, SIOCBONDCHANGEACTIVE);
  3941. }
  3942. return -EOPNOTSUPP;
  3943. }
  3944. static void bond_change_rx_flags(struct net_device *bond_dev, int change)
  3945. {
  3946. struct bonding *bond = netdev_priv(bond_dev);
  3947. if (change & IFF_PROMISC)
  3948. bond_set_promiscuity(bond,
  3949. bond_dev->flags & IFF_PROMISC ? 1 : -1);
  3950. if (change & IFF_ALLMULTI)
  3951. bond_set_allmulti(bond,
  3952. bond_dev->flags & IFF_ALLMULTI ? 1 : -1);
  3953. }
  3954. static void bond_set_rx_mode(struct net_device *bond_dev)
  3955. {
  3956. struct bonding *bond = netdev_priv(bond_dev);
  3957. struct list_head *iter;
  3958. struct slave *slave;
  3959. rcu_read_lock();
  3960. if (bond_uses_primary(bond)) {
  3961. slave = rcu_dereference(bond->curr_active_slave);
  3962. if (slave) {
  3963. dev_uc_sync(slave->dev, bond_dev);
  3964. dev_mc_sync(slave->dev, bond_dev);
  3965. }
  3966. } else {
  3967. bond_for_each_slave_rcu(bond, slave, iter) {
  3968. dev_uc_sync_multiple(slave->dev, bond_dev);
  3969. dev_mc_sync_multiple(slave->dev, bond_dev);
  3970. }
  3971. }
  3972. rcu_read_unlock();
  3973. }
  3974. static int bond_neigh_init(struct neighbour *n)
  3975. {
  3976. struct bonding *bond = netdev_priv(n->dev);
  3977. const struct net_device_ops *slave_ops;
  3978. struct neigh_parms parms;
  3979. struct slave *slave;
  3980. int ret = 0;
  3981. rcu_read_lock();
  3982. slave = bond_first_slave_rcu(bond);
  3983. if (!slave)
  3984. goto out;
  3985. slave_ops = slave->dev->netdev_ops;
  3986. if (!slave_ops->ndo_neigh_setup)
  3987. goto out;
  3988. /* TODO: find another way [1] to implement this.
  3989. * Passing a zeroed structure is fragile,
  3990. * but at least we do not pass garbage.
  3991. *
  3992. * [1] One way would be that ndo_neigh_setup() never touch
  3993. * struct neigh_parms, but propagate the new neigh_setup()
  3994. * back to ___neigh_create() / neigh_parms_alloc()
  3995. */
  3996. memset(&parms, 0, sizeof(parms));
  3997. ret = slave_ops->ndo_neigh_setup(slave->dev, &parms);
  3998. if (ret)
  3999. goto out;
  4000. if (parms.neigh_setup)
  4001. ret = parms.neigh_setup(n);
  4002. out:
  4003. rcu_read_unlock();
  4004. return ret;
  4005. }
  4006. /* The bonding ndo_neigh_setup is called at init time beofre any
  4007. * slave exists. So we must declare proxy setup function which will
  4008. * be used at run time to resolve the actual slave neigh param setup.
  4009. *
  4010. * It's also called by master devices (such as vlans) to setup their
  4011. * underlying devices. In that case - do nothing, we're already set up from
  4012. * our init.
  4013. */
  4014. static int bond_neigh_setup(struct net_device *dev,
  4015. struct neigh_parms *parms)
  4016. {
  4017. /* modify only our neigh_parms */
  4018. if (parms->dev == dev)
  4019. parms->neigh_setup = bond_neigh_init;
  4020. return 0;
  4021. }
  4022. /* Change the MTU of all of a master's slaves to match the master */
  4023. static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
  4024. {
  4025. struct bonding *bond = netdev_priv(bond_dev);
  4026. struct slave *slave, *rollback_slave;
  4027. struct list_head *iter;
  4028. int res = 0;
  4029. netdev_dbg(bond_dev, "bond=%p, new_mtu=%d\n", bond, new_mtu);
  4030. bond_for_each_slave(bond, slave, iter) {
  4031. slave_dbg(bond_dev, slave->dev, "s %p c_m %p\n",
  4032. slave, slave->dev->netdev_ops->ndo_change_mtu);
  4033. res = dev_set_mtu(slave->dev, new_mtu);
  4034. if (res) {
  4035. /* If we failed to set the slave's mtu to the new value
  4036. * we must abort the operation even in ACTIVE_BACKUP
  4037. * mode, because if we allow the backup slaves to have
  4038. * different mtu values than the active slave we'll
  4039. * need to change their mtu when doing a failover. That
  4040. * means changing their mtu from timer context, which
  4041. * is probably not a good idea.
  4042. */
  4043. slave_dbg(bond_dev, slave->dev, "err %d setting mtu to %d\n",
  4044. res, new_mtu);
  4045. goto unwind;
  4046. }
  4047. }
  4048. WRITE_ONCE(bond_dev->mtu, new_mtu);
  4049. return 0;
  4050. unwind:
  4051. /* unwind from head to the slave that failed */
  4052. bond_for_each_slave(bond, rollback_slave, iter) {
  4053. int tmp_res;
  4054. if (rollback_slave == slave)
  4055. break;
  4056. tmp_res = dev_set_mtu(rollback_slave->dev, bond_dev->mtu);
  4057. if (tmp_res)
  4058. slave_dbg(bond_dev, rollback_slave->dev, "unwind err %d\n",
  4059. tmp_res);
  4060. }
  4061. return res;
  4062. }
  4063. /* Change HW address
  4064. *
  4065. * Note that many devices must be down to change the HW address, and
  4066. * downing the master releases all slaves. We can make bonds full of
  4067. * bonding devices to test this, however.
  4068. */
  4069. static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
  4070. {
  4071. struct bonding *bond = netdev_priv(bond_dev);
  4072. struct slave *slave, *rollback_slave;
  4073. struct sockaddr_storage *ss = addr, tmp_ss;
  4074. struct list_head *iter;
  4075. int res = 0;
  4076. if (BOND_MODE(bond) == BOND_MODE_ALB)
  4077. return bond_alb_set_mac_address(bond_dev, addr);
  4078. netdev_dbg(bond_dev, "%s: bond=%p\n", __func__, bond);
  4079. /* If fail_over_mac is enabled, do nothing and return success.
  4080. * Returning an error causes ifenslave to fail.
  4081. */
  4082. if (bond->params.fail_over_mac &&
  4083. BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP)
  4084. return 0;
  4085. if (!is_valid_ether_addr(ss->__data))
  4086. return -EADDRNOTAVAIL;
  4087. bond_for_each_slave(bond, slave, iter) {
  4088. slave_dbg(bond_dev, slave->dev, "%s: slave=%p\n",
  4089. __func__, slave);
  4090. res = dev_set_mac_address(slave->dev, addr, NULL);
  4091. if (res) {
  4092. /* TODO: consider downing the slave
  4093. * and retry ?
  4094. * User should expect communications
  4095. * breakage anyway until ARP finish
  4096. * updating, so...
  4097. */
  4098. slave_dbg(bond_dev, slave->dev, "%s: err %d\n",
  4099. __func__, res);
  4100. goto unwind;
  4101. }
  4102. }
  4103. /* success */
  4104. dev_addr_set(bond_dev, ss->__data);
  4105. return 0;
  4106. unwind:
  4107. memcpy(tmp_ss.__data, bond_dev->dev_addr, bond_dev->addr_len);
  4108. tmp_ss.ss_family = bond_dev->type;
  4109. /* unwind from head to the slave that failed */
  4110. bond_for_each_slave(bond, rollback_slave, iter) {
  4111. int tmp_res;
  4112. if (rollback_slave == slave)
  4113. break;
  4114. tmp_res = dev_set_mac_address(rollback_slave->dev, &tmp_ss, NULL);
  4115. if (tmp_res) {
  4116. slave_dbg(bond_dev, rollback_slave->dev, "%s: unwind err %d\n",
  4117. __func__, tmp_res);
  4118. }
  4119. }
  4120. return res;
  4121. }
  4122. /**
  4123. * bond_get_slave_by_id - get xmit slave with slave_id
  4124. * @bond: bonding device that is transmitting
  4125. * @slave_id: slave id up to slave_cnt-1 through which to transmit
  4126. *
  4127. * This function tries to get slave with slave_id but in case
  4128. * it fails, it tries to find the first available slave for transmission.
  4129. */
  4130. static struct slave *bond_get_slave_by_id(struct bonding *bond,
  4131. int slave_id)
  4132. {
  4133. struct list_head *iter;
  4134. struct slave *slave;
  4135. int i = slave_id;
  4136. /* Here we start from the slave with slave_id */
  4137. bond_for_each_slave_rcu(bond, slave, iter) {
  4138. if (--i < 0) {
  4139. if (bond_slave_can_tx(slave))
  4140. return slave;
  4141. }
  4142. }
  4143. /* Here we start from the first slave up to slave_id */
  4144. i = slave_id;
  4145. bond_for_each_slave_rcu(bond, slave, iter) {
  4146. if (--i < 0)
  4147. break;
  4148. if (bond_slave_can_tx(slave))
  4149. return slave;
  4150. }
  4151. /* no slave that can tx has been found */
  4152. return NULL;
  4153. }
  4154. /**
  4155. * bond_rr_gen_slave_id - generate slave id based on packets_per_slave
  4156. * @bond: bonding device to use
  4157. *
  4158. * Based on the value of the bonding device's packets_per_slave parameter
  4159. * this function generates a slave id, which is usually used as the next
  4160. * slave to transmit through.
  4161. */
  4162. static u32 bond_rr_gen_slave_id(struct bonding *bond)
  4163. {
  4164. u32 slave_id;
  4165. struct reciprocal_value reciprocal_packets_per_slave;
  4166. int packets_per_slave = bond->params.packets_per_slave;
  4167. switch (packets_per_slave) {
  4168. case 0:
  4169. slave_id = get_random_u32();
  4170. break;
  4171. case 1:
  4172. slave_id = this_cpu_inc_return(*bond->rr_tx_counter);
  4173. break;
  4174. default:
  4175. reciprocal_packets_per_slave =
  4176. bond->params.reciprocal_packets_per_slave;
  4177. slave_id = this_cpu_inc_return(*bond->rr_tx_counter);
  4178. slave_id = reciprocal_divide(slave_id,
  4179. reciprocal_packets_per_slave);
  4180. break;
  4181. }
  4182. return slave_id;
  4183. }
  4184. static struct slave *bond_xmit_roundrobin_slave_get(struct bonding *bond,
  4185. struct sk_buff *skb)
  4186. {
  4187. struct slave *slave;
  4188. int slave_cnt;
  4189. u32 slave_id;
  4190. /* Start with the curr_active_slave that joined the bond as the
  4191. * default for sending IGMP traffic. For failover purposes one
  4192. * needs to maintain some consistency for the interface that will
  4193. * send the join/membership reports. The curr_active_slave found
  4194. * will send all of this type of traffic.
  4195. */
  4196. if (skb->protocol == htons(ETH_P_IP)) {
  4197. int noff = skb_network_offset(skb);
  4198. struct iphdr *iph;
  4199. if (unlikely(!pskb_may_pull(skb, noff + sizeof(*iph))))
  4200. goto non_igmp;
  4201. iph = ip_hdr(skb);
  4202. if (iph->protocol == IPPROTO_IGMP) {
  4203. slave = rcu_dereference(bond->curr_active_slave);
  4204. if (slave)
  4205. return slave;
  4206. return bond_get_slave_by_id(bond, 0);
  4207. }
  4208. }
  4209. non_igmp:
  4210. slave_cnt = READ_ONCE(bond->slave_cnt);
  4211. if (likely(slave_cnt)) {
  4212. slave_id = bond_rr_gen_slave_id(bond) % slave_cnt;
  4213. return bond_get_slave_by_id(bond, slave_id);
  4214. }
  4215. return NULL;
  4216. }
  4217. static struct slave *bond_xdp_xmit_roundrobin_slave_get(struct bonding *bond,
  4218. struct xdp_buff *xdp)
  4219. {
  4220. struct slave *slave;
  4221. int slave_cnt;
  4222. u32 slave_id;
  4223. const struct ethhdr *eth;
  4224. void *data = xdp->data;
  4225. if (data + sizeof(struct ethhdr) > xdp->data_end)
  4226. goto non_igmp;
  4227. eth = (struct ethhdr *)data;
  4228. data += sizeof(struct ethhdr);
  4229. /* See comment on IGMP in bond_xmit_roundrobin_slave_get() */
  4230. if (eth->h_proto == htons(ETH_P_IP)) {
  4231. const struct iphdr *iph;
  4232. if (data + sizeof(struct iphdr) > xdp->data_end)
  4233. goto non_igmp;
  4234. iph = (struct iphdr *)data;
  4235. if (iph->protocol == IPPROTO_IGMP) {
  4236. slave = rcu_dereference(bond->curr_active_slave);
  4237. if (slave)
  4238. return slave;
  4239. return bond_get_slave_by_id(bond, 0);
  4240. }
  4241. }
  4242. non_igmp:
  4243. slave_cnt = READ_ONCE(bond->slave_cnt);
  4244. if (likely(slave_cnt)) {
  4245. slave_id = bond_rr_gen_slave_id(bond) % slave_cnt;
  4246. return bond_get_slave_by_id(bond, slave_id);
  4247. }
  4248. return NULL;
  4249. }
  4250. static netdev_tx_t bond_xmit_roundrobin(struct sk_buff *skb,
  4251. struct net_device *bond_dev)
  4252. {
  4253. struct bonding *bond = netdev_priv(bond_dev);
  4254. struct slave *slave;
  4255. slave = bond_xmit_roundrobin_slave_get(bond, skb);
  4256. if (likely(slave))
  4257. return bond_dev_queue_xmit(bond, skb, slave->dev);
  4258. return bond_tx_drop(bond_dev, skb);
  4259. }
  4260. static struct slave *bond_xmit_activebackup_slave_get(struct bonding *bond)
  4261. {
  4262. return rcu_dereference(bond->curr_active_slave);
  4263. }
  4264. /* In active-backup mode, we know that bond->curr_active_slave is always valid if
  4265. * the bond has a usable interface.
  4266. */
  4267. static netdev_tx_t bond_xmit_activebackup(struct sk_buff *skb,
  4268. struct net_device *bond_dev)
  4269. {
  4270. struct bonding *bond = netdev_priv(bond_dev);
  4271. struct slave *slave;
  4272. slave = bond_xmit_activebackup_slave_get(bond);
  4273. if (slave)
  4274. return bond_dev_queue_xmit(bond, skb, slave->dev);
  4275. return bond_tx_drop(bond_dev, skb);
  4276. }
  4277. /* Use this to update slave_array when (a) it's not appropriate to update
  4278. * slave_array right away (note that update_slave_array() may sleep)
  4279. * and / or (b) RTNL is not held.
  4280. */
  4281. void bond_slave_arr_work_rearm(struct bonding *bond, unsigned long delay)
  4282. {
  4283. queue_delayed_work(bond->wq, &bond->slave_arr_work, delay);
  4284. }
  4285. /* Slave array work handler. Holds only RTNL */
  4286. static void bond_slave_arr_handler(struct work_struct *work)
  4287. {
  4288. struct bonding *bond = container_of(work, struct bonding,
  4289. slave_arr_work.work);
  4290. int ret;
  4291. if (!rtnl_trylock())
  4292. goto err;
  4293. ret = bond_update_slave_arr(bond, NULL);
  4294. rtnl_unlock();
  4295. if (ret) {
  4296. pr_warn_ratelimited("Failed to update slave array from WT\n");
  4297. goto err;
  4298. }
  4299. return;
  4300. err:
  4301. bond_slave_arr_work_rearm(bond, 1);
  4302. }
  4303. static void bond_skip_slave(struct bond_up_slave *slaves,
  4304. struct slave *skipslave)
  4305. {
  4306. int idx;
  4307. /* Rare situation where caller has asked to skip a specific
  4308. * slave but allocation failed (most likely!). BTW this is
  4309. * only possible when the call is initiated from
  4310. * __bond_release_one(). In this situation; overwrite the
  4311. * skipslave entry in the array with the last entry from the
  4312. * array to avoid a situation where the xmit path may choose
  4313. * this to-be-skipped slave to send a packet out.
  4314. */
  4315. for (idx = 0; slaves && idx < slaves->count; idx++) {
  4316. if (skipslave == slaves->arr[idx]) {
  4317. slaves->arr[idx] =
  4318. slaves->arr[slaves->count - 1];
  4319. slaves->count--;
  4320. break;
  4321. }
  4322. }
  4323. }
  4324. static void bond_set_slave_arr(struct bonding *bond,
  4325. struct bond_up_slave *usable_slaves,
  4326. struct bond_up_slave *all_slaves)
  4327. {
  4328. struct bond_up_slave *usable, *all;
  4329. all = rtnl_dereference(bond->all_slaves);
  4330. rcu_assign_pointer(bond->all_slaves, all_slaves);
  4331. kfree_rcu(all, rcu);
  4332. if (BOND_MODE(bond) == BOND_MODE_BROADCAST) {
  4333. kfree_rcu(usable_slaves, rcu);
  4334. return;
  4335. }
  4336. usable = rtnl_dereference(bond->usable_slaves);
  4337. rcu_assign_pointer(bond->usable_slaves, usable_slaves);
  4338. kfree_rcu(usable, rcu);
  4339. }
  4340. static void bond_reset_slave_arr(struct bonding *bond)
  4341. {
  4342. bond_set_slave_arr(bond, NULL, NULL);
  4343. }
  4344. /* Build the usable slaves array in control path for modes that use xmit-hash
  4345. * to determine the slave interface -
  4346. * (a) BOND_MODE_8023AD
  4347. * (b) BOND_MODE_XOR
  4348. * (c) (BOND_MODE_TLB || BOND_MODE_ALB) && tlb_dynamic_lb == 0
  4349. *
  4350. * The caller is expected to hold RTNL only and NO other lock!
  4351. */
  4352. int bond_update_slave_arr(struct bonding *bond, struct slave *skipslave)
  4353. {
  4354. struct bond_up_slave *usable_slaves = NULL, *all_slaves = NULL;
  4355. struct slave *slave;
  4356. struct list_head *iter;
  4357. int agg_id = 0;
  4358. int ret = 0;
  4359. might_sleep();
  4360. usable_slaves = kzalloc_flex(*usable_slaves, arr, bond->slave_cnt);
  4361. all_slaves = kzalloc_flex(*all_slaves, arr, bond->slave_cnt);
  4362. if (!usable_slaves || !all_slaves) {
  4363. ret = -ENOMEM;
  4364. goto out;
  4365. }
  4366. if (BOND_MODE(bond) == BOND_MODE_8023AD) {
  4367. struct ad_info ad_info;
  4368. spin_lock_bh(&bond->mode_lock);
  4369. if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
  4370. spin_unlock_bh(&bond->mode_lock);
  4371. pr_debug("bond_3ad_get_active_agg_info failed\n");
  4372. /* No active aggragator means it's not safe to use
  4373. * the previous array.
  4374. */
  4375. bond_reset_slave_arr(bond);
  4376. goto out;
  4377. }
  4378. spin_unlock_bh(&bond->mode_lock);
  4379. agg_id = ad_info.aggregator_id;
  4380. }
  4381. bond_for_each_slave(bond, slave, iter) {
  4382. if (skipslave == slave)
  4383. continue;
  4384. all_slaves->arr[all_slaves->count++] = slave;
  4385. if (BOND_MODE(bond) == BOND_MODE_8023AD) {
  4386. struct aggregator *agg;
  4387. agg = SLAVE_AD_INFO(slave)->port.aggregator;
  4388. if (!agg || agg->aggregator_identifier != agg_id)
  4389. continue;
  4390. }
  4391. if (!bond_slave_can_tx(slave))
  4392. continue;
  4393. slave_dbg(bond->dev, slave->dev, "Adding slave to tx hash array[%d]\n",
  4394. usable_slaves->count);
  4395. usable_slaves->arr[usable_slaves->count++] = slave;
  4396. }
  4397. bond_set_slave_arr(bond, usable_slaves, all_slaves);
  4398. return ret;
  4399. out:
  4400. if (ret != 0 && skipslave) {
  4401. bond_skip_slave(rtnl_dereference(bond->all_slaves),
  4402. skipslave);
  4403. bond_skip_slave(rtnl_dereference(bond->usable_slaves),
  4404. skipslave);
  4405. }
  4406. kfree_rcu(all_slaves, rcu);
  4407. kfree_rcu(usable_slaves, rcu);
  4408. return ret;
  4409. }
  4410. static struct slave *bond_xmit_3ad_xor_slave_get(struct bonding *bond,
  4411. struct sk_buff *skb,
  4412. struct bond_up_slave *slaves)
  4413. {
  4414. struct slave *slave;
  4415. unsigned int count;
  4416. u32 hash;
  4417. hash = bond_xmit_hash(bond, skb);
  4418. count = slaves ? READ_ONCE(slaves->count) : 0;
  4419. if (unlikely(!count))
  4420. return NULL;
  4421. slave = slaves->arr[hash % count];
  4422. return slave;
  4423. }
  4424. static struct slave *bond_xdp_xmit_3ad_xor_slave_get(struct bonding *bond,
  4425. struct xdp_buff *xdp)
  4426. {
  4427. struct bond_up_slave *slaves;
  4428. unsigned int count;
  4429. u32 hash;
  4430. hash = bond_xmit_hash_xdp(bond, xdp);
  4431. slaves = rcu_dereference(bond->usable_slaves);
  4432. count = slaves ? READ_ONCE(slaves->count) : 0;
  4433. if (unlikely(!count))
  4434. return NULL;
  4435. return slaves->arr[hash % count];
  4436. }
  4437. static bool bond_should_broadcast_neighbor(struct sk_buff *skb,
  4438. struct net_device *dev)
  4439. {
  4440. struct bonding *bond = netdev_priv(dev);
  4441. struct {
  4442. struct ipv6hdr ip6;
  4443. struct icmp6hdr icmp6;
  4444. } *combined, _combined;
  4445. if (!static_branch_unlikely(&bond_bcast_neigh_enabled))
  4446. return false;
  4447. if (!bond->params.broadcast_neighbor)
  4448. return false;
  4449. if (skb->protocol == htons(ETH_P_ARP))
  4450. return true;
  4451. if (skb->protocol == htons(ETH_P_IPV6)) {
  4452. combined = skb_header_pointer(skb, skb_mac_header_len(skb),
  4453. sizeof(_combined),
  4454. &_combined);
  4455. if (combined && combined->ip6.nexthdr == NEXTHDR_ICMP &&
  4456. (combined->icmp6.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
  4457. combined->icmp6.icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT))
  4458. return true;
  4459. }
  4460. return false;
  4461. }
  4462. /* Use this Xmit function for 3AD as well as XOR modes. The current
  4463. * usable slave array is formed in the control path. The xmit function
  4464. * just calculates hash and sends the packet out.
  4465. */
  4466. static netdev_tx_t bond_3ad_xor_xmit(struct sk_buff *skb,
  4467. struct net_device *dev)
  4468. {
  4469. struct bonding *bond = netdev_priv(dev);
  4470. struct bond_up_slave *slaves;
  4471. struct slave *slave;
  4472. slaves = rcu_dereference(bond->usable_slaves);
  4473. slave = bond_xmit_3ad_xor_slave_get(bond, skb, slaves);
  4474. if (likely(slave))
  4475. return bond_dev_queue_xmit(bond, skb, slave->dev);
  4476. return bond_tx_drop(dev, skb);
  4477. }
  4478. /* in broadcast mode, we send everything to all or usable slave interfaces.
  4479. * under rcu_read_lock when this function is called.
  4480. */
  4481. static netdev_tx_t bond_xmit_broadcast(struct sk_buff *skb,
  4482. struct net_device *bond_dev,
  4483. bool all_slaves)
  4484. {
  4485. struct bonding *bond = netdev_priv(bond_dev);
  4486. struct bond_up_slave *slaves;
  4487. bool xmit_suc = false;
  4488. bool skb_used = false;
  4489. int slaves_count, i;
  4490. if (all_slaves)
  4491. slaves = rcu_dereference(bond->all_slaves);
  4492. else
  4493. slaves = rcu_dereference(bond->usable_slaves);
  4494. slaves_count = slaves ? READ_ONCE(slaves->count) : 0;
  4495. for (i = 0; i < slaves_count; i++) {
  4496. struct slave *slave = slaves->arr[i];
  4497. struct sk_buff *skb2;
  4498. if (!(bond_slave_is_up(slave) && slave->link == BOND_LINK_UP))
  4499. continue;
  4500. if (i + 1 == slaves_count) {
  4501. skb2 = skb;
  4502. skb_used = true;
  4503. } else {
  4504. skb2 = skb_clone(skb, GFP_ATOMIC);
  4505. if (!skb2) {
  4506. net_err_ratelimited("%s: Error: %s: skb_clone() failed\n",
  4507. bond_dev->name, __func__);
  4508. continue;
  4509. }
  4510. }
  4511. if (bond_dev_queue_xmit(bond, skb2, slave->dev) == NETDEV_TX_OK)
  4512. xmit_suc = true;
  4513. }
  4514. if (!skb_used)
  4515. dev_kfree_skb_any(skb);
  4516. if (xmit_suc)
  4517. return NETDEV_TX_OK;
  4518. dev_core_stats_tx_dropped_inc(bond_dev);
  4519. return NET_XMIT_DROP;
  4520. }
  4521. /*------------------------- Device initialization ---------------------------*/
  4522. /* Lookup the slave that corresponds to a qid */
  4523. static inline int bond_slave_override(struct bonding *bond,
  4524. struct sk_buff *skb)
  4525. {
  4526. struct slave *slave = NULL;
  4527. struct list_head *iter;
  4528. if (!skb_rx_queue_recorded(skb))
  4529. return 1;
  4530. /* Find out if any slaves have the same mapping as this skb. */
  4531. bond_for_each_slave_rcu(bond, slave, iter) {
  4532. if (READ_ONCE(slave->queue_id) == skb_get_queue_mapping(skb)) {
  4533. if (bond_slave_is_up(slave) &&
  4534. slave->link == BOND_LINK_UP) {
  4535. bond_dev_queue_xmit(bond, skb, slave->dev);
  4536. return 0;
  4537. }
  4538. /* If the slave isn't UP, use default transmit policy. */
  4539. break;
  4540. }
  4541. }
  4542. return 1;
  4543. }
  4544. static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb,
  4545. struct net_device *sb_dev)
  4546. {
  4547. /* This helper function exists to help dev_pick_tx get the correct
  4548. * destination queue. Using a helper function skips a call to
  4549. * skb_tx_hash and will put the skbs in the queue we expect on their
  4550. * way down to the bonding driver.
  4551. */
  4552. u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
  4553. /* Save the original txq to restore before passing to the driver */
  4554. qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb_get_queue_mapping(skb);
  4555. if (unlikely(txq >= dev->real_num_tx_queues)) {
  4556. do {
  4557. txq -= dev->real_num_tx_queues;
  4558. } while (txq >= dev->real_num_tx_queues);
  4559. }
  4560. return txq;
  4561. }
  4562. static struct net_device *bond_xmit_get_slave(struct net_device *master_dev,
  4563. struct sk_buff *skb,
  4564. bool all_slaves)
  4565. {
  4566. struct bonding *bond = netdev_priv(master_dev);
  4567. struct bond_up_slave *slaves;
  4568. struct slave *slave = NULL;
  4569. switch (BOND_MODE(bond)) {
  4570. case BOND_MODE_ROUNDROBIN:
  4571. slave = bond_xmit_roundrobin_slave_get(bond, skb);
  4572. break;
  4573. case BOND_MODE_ACTIVEBACKUP:
  4574. slave = bond_xmit_activebackup_slave_get(bond);
  4575. break;
  4576. case BOND_MODE_8023AD:
  4577. case BOND_MODE_XOR:
  4578. if (all_slaves)
  4579. slaves = rcu_dereference(bond->all_slaves);
  4580. else
  4581. slaves = rcu_dereference(bond->usable_slaves);
  4582. slave = bond_xmit_3ad_xor_slave_get(bond, skb, slaves);
  4583. break;
  4584. case BOND_MODE_BROADCAST:
  4585. break;
  4586. case BOND_MODE_ALB:
  4587. slave = bond_xmit_alb_slave_get(bond, skb);
  4588. break;
  4589. case BOND_MODE_TLB:
  4590. slave = bond_xmit_tlb_slave_get(bond, skb);
  4591. break;
  4592. default:
  4593. /* Should never happen, mode already checked */
  4594. WARN_ONCE(true, "Unknown bonding mode");
  4595. break;
  4596. }
  4597. if (slave)
  4598. return slave->dev;
  4599. return NULL;
  4600. }
  4601. static void bond_sk_to_flow(struct sock *sk, struct flow_keys *flow)
  4602. {
  4603. switch (sk->sk_family) {
  4604. #if IS_ENABLED(CONFIG_IPV6)
  4605. case AF_INET6:
  4606. if (ipv6_only_sock(sk) ||
  4607. ipv6_addr_type(&sk->sk_v6_daddr) != IPV6_ADDR_MAPPED) {
  4608. flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
  4609. flow->addrs.v6addrs.src = inet6_sk(sk)->saddr;
  4610. flow->addrs.v6addrs.dst = sk->sk_v6_daddr;
  4611. break;
  4612. }
  4613. fallthrough;
  4614. #endif
  4615. default: /* AF_INET */
  4616. flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
  4617. flow->addrs.v4addrs.src = inet_sk(sk)->inet_rcv_saddr;
  4618. flow->addrs.v4addrs.dst = inet_sk(sk)->inet_daddr;
  4619. break;
  4620. }
  4621. flow->ports.src = inet_sk(sk)->inet_sport;
  4622. flow->ports.dst = inet_sk(sk)->inet_dport;
  4623. }
  4624. /**
  4625. * bond_sk_hash_l34 - generate a hash value based on the socket's L3 and L4 fields
  4626. * @sk: socket to use for headers
  4627. *
  4628. * This function will extract the necessary field from the socket and use
  4629. * them to generate a hash based on the LAYER34 xmit_policy.
  4630. * Assumes that sk is a TCP or UDP socket.
  4631. */
  4632. static u32 bond_sk_hash_l34(struct sock *sk)
  4633. {
  4634. struct flow_keys flow;
  4635. u32 hash;
  4636. bond_sk_to_flow(sk, &flow);
  4637. /* L4 */
  4638. memcpy(&hash, &flow.ports.ports, sizeof(hash));
  4639. /* L3 */
  4640. return bond_ip_hash(hash, &flow, BOND_XMIT_POLICY_LAYER34);
  4641. }
  4642. static struct net_device *__bond_sk_get_lower_dev(struct bonding *bond,
  4643. struct sock *sk)
  4644. {
  4645. struct bond_up_slave *slaves;
  4646. struct slave *slave;
  4647. unsigned int count;
  4648. u32 hash;
  4649. slaves = rcu_dereference(bond->usable_slaves);
  4650. count = slaves ? READ_ONCE(slaves->count) : 0;
  4651. if (unlikely(!count))
  4652. return NULL;
  4653. hash = bond_sk_hash_l34(sk);
  4654. slave = slaves->arr[hash % count];
  4655. return slave->dev;
  4656. }
  4657. static struct net_device *bond_sk_get_lower_dev(struct net_device *dev,
  4658. struct sock *sk)
  4659. {
  4660. struct bonding *bond = netdev_priv(dev);
  4661. struct net_device *lower = NULL;
  4662. rcu_read_lock();
  4663. if (bond_sk_check(bond))
  4664. lower = __bond_sk_get_lower_dev(bond, sk);
  4665. rcu_read_unlock();
  4666. return lower;
  4667. }
  4668. #if IS_ENABLED(CONFIG_TLS_DEVICE)
  4669. static netdev_tx_t bond_tls_device_xmit(struct bonding *bond, struct sk_buff *skb,
  4670. struct net_device *dev)
  4671. {
  4672. struct net_device *tls_netdev = rcu_dereference(tls_get_ctx(skb->sk)->netdev);
  4673. /* tls_netdev might become NULL, even if tls_is_skb_tx_device_offloaded
  4674. * was true, if tls_device_down is running in parallel, but it's OK,
  4675. * because bond_get_slave_by_dev has a NULL check.
  4676. */
  4677. if (likely(bond_get_slave_by_dev(bond, tls_netdev)))
  4678. return bond_dev_queue_xmit(bond, skb, tls_netdev);
  4679. return bond_tx_drop(dev, skb);
  4680. }
  4681. #endif
  4682. static netdev_tx_t __bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
  4683. {
  4684. struct bonding *bond = netdev_priv(dev);
  4685. if (bond_should_override_tx_queue(bond) &&
  4686. !bond_slave_override(bond, skb))
  4687. return NETDEV_TX_OK;
  4688. #if IS_ENABLED(CONFIG_TLS_DEVICE)
  4689. if (tls_is_skb_tx_device_offloaded(skb))
  4690. return bond_tls_device_xmit(bond, skb, dev);
  4691. #endif
  4692. switch (BOND_MODE(bond)) {
  4693. case BOND_MODE_ROUNDROBIN:
  4694. return bond_xmit_roundrobin(skb, dev);
  4695. case BOND_MODE_ACTIVEBACKUP:
  4696. return bond_xmit_activebackup(skb, dev);
  4697. case BOND_MODE_8023AD:
  4698. if (bond_should_broadcast_neighbor(skb, dev))
  4699. return bond_xmit_broadcast(skb, dev, false);
  4700. fallthrough;
  4701. case BOND_MODE_XOR:
  4702. return bond_3ad_xor_xmit(skb, dev);
  4703. case BOND_MODE_BROADCAST:
  4704. return bond_xmit_broadcast(skb, dev, true);
  4705. case BOND_MODE_ALB:
  4706. return bond_alb_xmit(skb, dev);
  4707. case BOND_MODE_TLB:
  4708. return bond_tlb_xmit(skb, dev);
  4709. default:
  4710. /* Should never happen, mode already checked */
  4711. netdev_err(dev, "Unknown bonding mode %d\n", BOND_MODE(bond));
  4712. WARN_ON_ONCE(1);
  4713. return bond_tx_drop(dev, skb);
  4714. }
  4715. }
  4716. static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
  4717. {
  4718. struct bonding *bond = netdev_priv(dev);
  4719. netdev_tx_t ret = NETDEV_TX_OK;
  4720. /* If we risk deadlock from transmitting this in the
  4721. * netpoll path, tell netpoll to queue the frame for later tx
  4722. */
  4723. if (unlikely(is_netpoll_tx_blocked(dev)))
  4724. return NETDEV_TX_BUSY;
  4725. rcu_read_lock();
  4726. if (bond_has_slaves(bond))
  4727. ret = __bond_start_xmit(skb, dev);
  4728. else
  4729. ret = bond_tx_drop(dev, skb);
  4730. rcu_read_unlock();
  4731. return ret;
  4732. }
  4733. static struct net_device *
  4734. bond_xdp_get_xmit_slave(struct net_device *bond_dev, struct xdp_buff *xdp)
  4735. {
  4736. struct bonding *bond = netdev_priv(bond_dev);
  4737. struct slave *slave;
  4738. /* Caller needs to hold rcu_read_lock() */
  4739. switch (BOND_MODE(bond)) {
  4740. case BOND_MODE_ROUNDROBIN:
  4741. slave = bond_xdp_xmit_roundrobin_slave_get(bond, xdp);
  4742. break;
  4743. case BOND_MODE_ACTIVEBACKUP:
  4744. slave = bond_xmit_activebackup_slave_get(bond);
  4745. break;
  4746. case BOND_MODE_8023AD:
  4747. case BOND_MODE_XOR:
  4748. slave = bond_xdp_xmit_3ad_xor_slave_get(bond, xdp);
  4749. break;
  4750. default:
  4751. if (net_ratelimit())
  4752. netdev_err(bond_dev, "Unknown bonding mode %d for xdp xmit\n",
  4753. BOND_MODE(bond));
  4754. return NULL;
  4755. }
  4756. if (slave)
  4757. return slave->dev;
  4758. return NULL;
  4759. }
  4760. static int bond_xdp_xmit(struct net_device *bond_dev,
  4761. int n, struct xdp_frame **frames, u32 flags)
  4762. {
  4763. int nxmit, err = -ENXIO;
  4764. rcu_read_lock();
  4765. for (nxmit = 0; nxmit < n; nxmit++) {
  4766. struct xdp_frame *frame = frames[nxmit];
  4767. struct xdp_frame *frames1[] = {frame};
  4768. struct net_device *slave_dev;
  4769. struct xdp_buff xdp;
  4770. xdp_convert_frame_to_buff(frame, &xdp);
  4771. slave_dev = bond_xdp_get_xmit_slave(bond_dev, &xdp);
  4772. if (!slave_dev) {
  4773. err = -ENXIO;
  4774. break;
  4775. }
  4776. err = slave_dev->netdev_ops->ndo_xdp_xmit(slave_dev, 1, frames1, flags);
  4777. if (err < 1)
  4778. break;
  4779. }
  4780. rcu_read_unlock();
  4781. /* If error happened on the first frame then we can pass the error up, otherwise
  4782. * report the number of frames that were xmitted.
  4783. */
  4784. if (err < 0)
  4785. return (nxmit == 0 ? err : nxmit);
  4786. return nxmit;
  4787. }
  4788. static int bond_xdp_set(struct net_device *dev, struct bpf_prog *prog,
  4789. struct netlink_ext_ack *extack)
  4790. {
  4791. struct bonding *bond = netdev_priv(dev);
  4792. struct list_head *iter;
  4793. struct slave *slave, *rollback_slave;
  4794. struct bpf_prog *old_prog;
  4795. struct netdev_bpf xdp = {
  4796. .command = XDP_SETUP_PROG,
  4797. .flags = 0,
  4798. .prog = prog,
  4799. .extack = extack,
  4800. };
  4801. int err;
  4802. ASSERT_RTNL();
  4803. if (!bond_xdp_check(bond, BOND_MODE(bond))) {
  4804. BOND_NL_ERR(dev, extack,
  4805. "No native XDP support for the current bonding mode");
  4806. return -EOPNOTSUPP;
  4807. }
  4808. old_prog = bond->xdp_prog;
  4809. bond->xdp_prog = prog;
  4810. bond_for_each_slave(bond, slave, iter) {
  4811. struct net_device *slave_dev = slave->dev;
  4812. if (!slave_dev->netdev_ops->ndo_bpf ||
  4813. !slave_dev->netdev_ops->ndo_xdp_xmit) {
  4814. SLAVE_NL_ERR(dev, slave_dev, extack,
  4815. "Slave device does not support XDP");
  4816. err = -EOPNOTSUPP;
  4817. goto err;
  4818. }
  4819. if (dev_xdp_prog_count(slave_dev) > 0) {
  4820. SLAVE_NL_ERR(dev, slave_dev, extack,
  4821. "Slave has XDP program loaded, please unload before enslaving");
  4822. err = -EOPNOTSUPP;
  4823. goto err;
  4824. }
  4825. err = dev_xdp_propagate(slave_dev, &xdp);
  4826. if (err < 0) {
  4827. /* ndo_bpf() sets extack error message */
  4828. slave_err(dev, slave_dev, "Error %d calling ndo_bpf\n", err);
  4829. goto err;
  4830. }
  4831. if (prog)
  4832. bpf_prog_inc(prog);
  4833. }
  4834. if (prog) {
  4835. static_branch_inc(&bpf_master_redirect_enabled_key);
  4836. } else if (old_prog) {
  4837. bpf_prog_put(old_prog);
  4838. static_branch_dec(&bpf_master_redirect_enabled_key);
  4839. }
  4840. return 0;
  4841. err:
  4842. /* unwind the program changes */
  4843. bond->xdp_prog = old_prog;
  4844. xdp.prog = old_prog;
  4845. xdp.extack = NULL; /* do not overwrite original error */
  4846. bond_for_each_slave(bond, rollback_slave, iter) {
  4847. struct net_device *slave_dev = rollback_slave->dev;
  4848. int err_unwind;
  4849. if (slave == rollback_slave)
  4850. break;
  4851. err_unwind = dev_xdp_propagate(slave_dev, &xdp);
  4852. if (err_unwind < 0)
  4853. slave_err(dev, slave_dev,
  4854. "Error %d when unwinding XDP program change\n", err_unwind);
  4855. else if (xdp.prog)
  4856. bpf_prog_inc(xdp.prog);
  4857. }
  4858. return err;
  4859. }
  4860. static int bond_xdp(struct net_device *dev, struct netdev_bpf *xdp)
  4861. {
  4862. switch (xdp->command) {
  4863. case XDP_SETUP_PROG:
  4864. return bond_xdp_set(dev, xdp->prog, xdp->extack);
  4865. default:
  4866. return -EINVAL;
  4867. }
  4868. }
  4869. static u32 bond_mode_bcast_speed(struct slave *slave, u32 speed)
  4870. {
  4871. if (speed == 0 || speed == SPEED_UNKNOWN)
  4872. speed = slave->speed;
  4873. else
  4874. speed = min(speed, slave->speed);
  4875. return speed;
  4876. }
  4877. /* Set the BOND_PHC_INDEX flag to notify user space */
  4878. static int bond_set_phc_index_flag(struct kernel_hwtstamp_config *kernel_cfg)
  4879. {
  4880. struct ifreq *ifr = kernel_cfg->ifr;
  4881. struct hwtstamp_config cfg;
  4882. if (kernel_cfg->copied_to_user) {
  4883. /* Lower device has a legacy implementation */
  4884. if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
  4885. return -EFAULT;
  4886. cfg.flags |= HWTSTAMP_FLAG_BONDED_PHC_INDEX;
  4887. if (copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)))
  4888. return -EFAULT;
  4889. } else {
  4890. kernel_cfg->flags |= HWTSTAMP_FLAG_BONDED_PHC_INDEX;
  4891. }
  4892. return 0;
  4893. }
  4894. static int bond_hwtstamp_get(struct net_device *dev,
  4895. struct kernel_hwtstamp_config *cfg)
  4896. {
  4897. struct bonding *bond = netdev_priv(dev);
  4898. struct net_device *real_dev;
  4899. int err;
  4900. real_dev = bond_option_active_slave_get_rcu(bond);
  4901. if (!real_dev)
  4902. return -EOPNOTSUPP;
  4903. err = generic_hwtstamp_get_lower(real_dev, cfg);
  4904. if (err)
  4905. return err;
  4906. return bond_set_phc_index_flag(cfg);
  4907. }
  4908. static int bond_hwtstamp_set(struct net_device *dev,
  4909. struct kernel_hwtstamp_config *cfg,
  4910. struct netlink_ext_ack *extack)
  4911. {
  4912. struct bonding *bond = netdev_priv(dev);
  4913. struct net_device *real_dev;
  4914. int err;
  4915. if (!(cfg->flags & HWTSTAMP_FLAG_BONDED_PHC_INDEX))
  4916. return -EOPNOTSUPP;
  4917. real_dev = bond_option_active_slave_get_rcu(bond);
  4918. if (!real_dev)
  4919. return -EOPNOTSUPP;
  4920. err = generic_hwtstamp_set_lower(real_dev, cfg, extack);
  4921. if (err)
  4922. return err;
  4923. return bond_set_phc_index_flag(cfg);
  4924. }
  4925. static int bond_ethtool_get_link_ksettings(struct net_device *bond_dev,
  4926. struct ethtool_link_ksettings *cmd)
  4927. {
  4928. struct bonding *bond = netdev_priv(bond_dev);
  4929. struct list_head *iter;
  4930. struct slave *slave;
  4931. u32 speed = 0;
  4932. cmd->base.duplex = DUPLEX_UNKNOWN;
  4933. cmd->base.port = PORT_OTHER;
  4934. /* Since bond_slave_can_tx returns false for all inactive or down slaves, we
  4935. * do not need to check mode. Though link speed might not represent
  4936. * the true receive or transmit bandwidth (not all modes are symmetric)
  4937. * this is an accurate maximum.
  4938. */
  4939. bond_for_each_slave(bond, slave, iter) {
  4940. if (bond_slave_can_tx(slave)) {
  4941. bond_update_speed_duplex(slave);
  4942. if (slave->speed != SPEED_UNKNOWN) {
  4943. if (BOND_MODE(bond) == BOND_MODE_BROADCAST)
  4944. speed = bond_mode_bcast_speed(slave,
  4945. speed);
  4946. else
  4947. speed += slave->speed;
  4948. }
  4949. if (cmd->base.duplex == DUPLEX_UNKNOWN &&
  4950. slave->duplex != DUPLEX_UNKNOWN)
  4951. cmd->base.duplex = slave->duplex;
  4952. }
  4953. }
  4954. cmd->base.speed = speed ? : SPEED_UNKNOWN;
  4955. return 0;
  4956. }
  4957. static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
  4958. struct ethtool_drvinfo *drvinfo)
  4959. {
  4960. strscpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
  4961. snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "%d",
  4962. BOND_ABI_VERSION);
  4963. }
  4964. static int bond_ethtool_get_ts_info(struct net_device *bond_dev,
  4965. struct kernel_ethtool_ts_info *info)
  4966. {
  4967. struct bonding *bond = netdev_priv(bond_dev);
  4968. struct kernel_ethtool_ts_info ts_info;
  4969. struct net_device *real_dev;
  4970. bool sw_tx_support = false;
  4971. struct list_head *iter;
  4972. struct slave *slave;
  4973. int ret = 0;
  4974. rcu_read_lock();
  4975. real_dev = bond_option_active_slave_get_rcu(bond);
  4976. dev_hold(real_dev);
  4977. rcu_read_unlock();
  4978. if (real_dev) {
  4979. ret = ethtool_get_ts_info_by_layer(real_dev, info);
  4980. } else {
  4981. /* Check if all slaves support software tx timestamping */
  4982. rcu_read_lock();
  4983. bond_for_each_slave_rcu(bond, slave, iter) {
  4984. ret = ethtool_get_ts_info_by_layer(slave->dev, &ts_info);
  4985. if (!ret && (ts_info.so_timestamping & SOF_TIMESTAMPING_TX_SOFTWARE)) {
  4986. sw_tx_support = true;
  4987. continue;
  4988. }
  4989. sw_tx_support = false;
  4990. break;
  4991. }
  4992. rcu_read_unlock();
  4993. }
  4994. if (sw_tx_support)
  4995. info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE;
  4996. dev_put(real_dev);
  4997. return ret;
  4998. }
  4999. static const struct ethtool_ops bond_ethtool_ops = {
  5000. .get_drvinfo = bond_ethtool_get_drvinfo,
  5001. .get_link = ethtool_op_get_link,
  5002. .get_link_ksettings = bond_ethtool_get_link_ksettings,
  5003. .get_ts_info = bond_ethtool_get_ts_info,
  5004. };
  5005. static const struct net_device_ops bond_netdev_ops = {
  5006. .ndo_init = bond_init,
  5007. .ndo_uninit = bond_uninit,
  5008. .ndo_open = bond_open,
  5009. .ndo_stop = bond_close,
  5010. .ndo_start_xmit = bond_start_xmit,
  5011. .ndo_select_queue = bond_select_queue,
  5012. .ndo_get_stats64 = bond_get_stats,
  5013. .ndo_eth_ioctl = bond_eth_ioctl,
  5014. .ndo_siocbond = bond_do_ioctl,
  5015. .ndo_siocdevprivate = bond_siocdevprivate,
  5016. .ndo_change_rx_flags = bond_change_rx_flags,
  5017. .ndo_set_rx_mode = bond_set_rx_mode,
  5018. .ndo_change_mtu = bond_change_mtu,
  5019. .ndo_set_mac_address = bond_set_mac_address,
  5020. .ndo_neigh_setup = bond_neigh_setup,
  5021. .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
  5022. .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
  5023. #ifdef CONFIG_NET_POLL_CONTROLLER
  5024. .ndo_netpoll_setup = bond_netpoll_setup,
  5025. .ndo_netpoll_cleanup = bond_netpoll_cleanup,
  5026. .ndo_poll_controller = bond_poll_controller,
  5027. #endif
  5028. .ndo_add_slave = bond_enslave,
  5029. .ndo_del_slave = bond_release,
  5030. .ndo_fix_features = bond_fix_features,
  5031. .ndo_features_check = passthru_features_check,
  5032. .ndo_get_xmit_slave = bond_xmit_get_slave,
  5033. .ndo_sk_get_lower_dev = bond_sk_get_lower_dev,
  5034. .ndo_bpf = bond_xdp,
  5035. .ndo_xdp_xmit = bond_xdp_xmit,
  5036. .ndo_xdp_get_xmit_slave = bond_xdp_get_xmit_slave,
  5037. .ndo_hwtstamp_get = bond_hwtstamp_get,
  5038. .ndo_hwtstamp_set = bond_hwtstamp_set,
  5039. };
  5040. static const struct device_type bond_type = {
  5041. .name = "bond",
  5042. };
  5043. static void bond_destructor(struct net_device *bond_dev)
  5044. {
  5045. struct bonding *bond = netdev_priv(bond_dev);
  5046. if (bond->wq)
  5047. destroy_workqueue(bond->wq);
  5048. free_percpu(bond->rr_tx_counter);
  5049. }
  5050. void bond_setup(struct net_device *bond_dev)
  5051. {
  5052. struct bonding *bond = netdev_priv(bond_dev);
  5053. spin_lock_init(&bond->mode_lock);
  5054. bond->params = bonding_defaults;
  5055. /* Initialize pointers */
  5056. bond->dev = bond_dev;
  5057. /* Initialize the device entry points */
  5058. ether_setup(bond_dev);
  5059. bond_dev->max_mtu = ETH_MAX_MTU;
  5060. bond_dev->netdev_ops = &bond_netdev_ops;
  5061. bond_dev->ethtool_ops = &bond_ethtool_ops;
  5062. bond_dev->needs_free_netdev = true;
  5063. bond_dev->priv_destructor = bond_destructor;
  5064. SET_NETDEV_DEVTYPE(bond_dev, &bond_type);
  5065. /* Initialize the device options */
  5066. bond_dev->flags |= IFF_MASTER;
  5067. bond_dev->priv_flags |= IFF_BONDING | IFF_UNICAST_FLT | IFF_NO_QUEUE;
  5068. bond_dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
  5069. #ifdef CONFIG_XFRM_OFFLOAD
  5070. /* set up xfrm device ops (only supported in active-backup right now) */
  5071. bond_dev->xfrmdev_ops = &bond_xfrmdev_ops;
  5072. INIT_LIST_HEAD(&bond->ipsec_list);
  5073. mutex_init(&bond->ipsec_lock);
  5074. #endif /* CONFIG_XFRM_OFFLOAD */
  5075. /* don't acquire bond device's netif_tx_lock when transmitting */
  5076. bond_dev->lltx = true;
  5077. /* Don't allow bond devices to change network namespaces. */
  5078. bond_dev->netns_immutable = true;
  5079. /* By default, we declare the bond to be fully
  5080. * VLAN hardware accelerated capable. Special
  5081. * care is taken in the various xmit functions
  5082. * when there are slaves that are not hw accel
  5083. * capable
  5084. */
  5085. bond_dev->hw_features = MASTER_UPPER_DEV_VLAN_FEATURES |
  5086. NETIF_F_HW_VLAN_CTAG_RX |
  5087. NETIF_F_HW_VLAN_CTAG_FILTER |
  5088. NETIF_F_HW_VLAN_STAG_RX |
  5089. NETIF_F_HW_VLAN_STAG_FILTER;
  5090. bond_dev->hw_features |= NETIF_F_GSO_ENCAP_ALL;
  5091. bond_dev->features |= bond_dev->hw_features;
  5092. bond_dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
  5093. bond_dev->features |= NETIF_F_GSO_PARTIAL;
  5094. #ifdef CONFIG_XFRM_OFFLOAD
  5095. bond_dev->hw_features |= BOND_XFRM_FEATURES;
  5096. /* Only enable XFRM features if this is an active-backup config */
  5097. if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP)
  5098. bond_dev->features |= BOND_XFRM_FEATURES;
  5099. #endif /* CONFIG_XFRM_OFFLOAD */
  5100. }
  5101. /* Destroy a bonding device.
  5102. * Must be under rtnl_lock when this function is called.
  5103. */
  5104. static void bond_uninit(struct net_device *bond_dev)
  5105. {
  5106. struct bonding *bond = netdev_priv(bond_dev);
  5107. struct list_head *iter;
  5108. struct slave *slave;
  5109. bond_netpoll_cleanup(bond_dev);
  5110. /* Release the bonded slaves */
  5111. bond_for_each_slave(bond, slave, iter)
  5112. __bond_release_one(bond_dev, slave->dev, true, true);
  5113. netdev_info(bond_dev, "Released all slaves\n");
  5114. #ifdef CONFIG_XFRM_OFFLOAD
  5115. mutex_destroy(&bond->ipsec_lock);
  5116. #endif /* CONFIG_XFRM_OFFLOAD */
  5117. bond_set_slave_arr(bond, NULL, NULL);
  5118. list_del_rcu(&bond->bond_list);
  5119. bond_debug_unregister(bond);
  5120. }
  5121. /*------------------------- Module initialization ---------------------------*/
  5122. static int __init bond_check_params(struct bond_params *params)
  5123. {
  5124. int arp_validate_value, fail_over_mac_value, primary_reselect_value, i;
  5125. struct bond_opt_value newval;
  5126. const struct bond_opt_value *valptr;
  5127. int arp_all_targets_value = 0;
  5128. u16 ad_actor_sys_prio = 0;
  5129. u16 ad_user_port_key = 0;
  5130. __be32 arp_target[BOND_MAX_ARP_TARGETS] = { 0 };
  5131. int arp_ip_count;
  5132. int bond_mode = BOND_MODE_ROUNDROBIN;
  5133. int xmit_hashtype = BOND_XMIT_POLICY_LAYER2;
  5134. int lacp_fast = 0;
  5135. int tlb_dynamic_lb;
  5136. /* Convert string parameters. */
  5137. if (mode) {
  5138. bond_opt_initstr(&newval, mode);
  5139. valptr = bond_opt_parse(bond_opt_get(BOND_OPT_MODE), &newval);
  5140. if (!valptr) {
  5141. pr_err("Error: Invalid bonding mode \"%s\"\n", mode);
  5142. return -EINVAL;
  5143. }
  5144. bond_mode = valptr->value;
  5145. }
  5146. if (xmit_hash_policy) {
  5147. if (bond_mode == BOND_MODE_ROUNDROBIN ||
  5148. bond_mode == BOND_MODE_ACTIVEBACKUP ||
  5149. bond_mode == BOND_MODE_BROADCAST) {
  5150. pr_info("xmit_hash_policy param is irrelevant in mode %s\n",
  5151. bond_mode_name(bond_mode));
  5152. } else {
  5153. bond_opt_initstr(&newval, xmit_hash_policy);
  5154. valptr = bond_opt_parse(bond_opt_get(BOND_OPT_XMIT_HASH),
  5155. &newval);
  5156. if (!valptr) {
  5157. pr_err("Error: Invalid xmit_hash_policy \"%s\"\n",
  5158. xmit_hash_policy);
  5159. return -EINVAL;
  5160. }
  5161. xmit_hashtype = valptr->value;
  5162. }
  5163. }
  5164. if (lacp_rate) {
  5165. if (bond_mode != BOND_MODE_8023AD) {
  5166. pr_info("lacp_rate param is irrelevant in mode %s\n",
  5167. bond_mode_name(bond_mode));
  5168. } else {
  5169. bond_opt_initstr(&newval, lacp_rate);
  5170. valptr = bond_opt_parse(bond_opt_get(BOND_OPT_LACP_RATE),
  5171. &newval);
  5172. if (!valptr) {
  5173. pr_err("Error: Invalid lacp rate \"%s\"\n",
  5174. lacp_rate);
  5175. return -EINVAL;
  5176. }
  5177. lacp_fast = valptr->value;
  5178. }
  5179. }
  5180. if (ad_select) {
  5181. bond_opt_initstr(&newval, ad_select);
  5182. valptr = bond_opt_parse(bond_opt_get(BOND_OPT_AD_SELECT),
  5183. &newval);
  5184. if (!valptr) {
  5185. pr_err("Error: Invalid ad_select \"%s\"\n", ad_select);
  5186. return -EINVAL;
  5187. }
  5188. params->ad_select = valptr->value;
  5189. if (bond_mode != BOND_MODE_8023AD)
  5190. pr_warn("ad_select param only affects 802.3ad mode\n");
  5191. } else {
  5192. params->ad_select = BOND_AD_STABLE;
  5193. }
  5194. if (max_bonds < 0) {
  5195. pr_warn("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
  5196. max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
  5197. max_bonds = BOND_DEFAULT_MAX_BONDS;
  5198. }
  5199. if (miimon < 0) {
  5200. pr_warn("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to 0\n",
  5201. miimon, INT_MAX);
  5202. miimon = 0;
  5203. }
  5204. if (updelay < 0) {
  5205. pr_warn("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
  5206. updelay, INT_MAX);
  5207. updelay = 0;
  5208. }
  5209. if (downdelay < 0) {
  5210. pr_warn("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
  5211. downdelay, INT_MAX);
  5212. downdelay = 0;
  5213. }
  5214. if (use_carrier != 1) {
  5215. pr_err("Error: invalid use_carrier parameter (%d)\n",
  5216. use_carrier);
  5217. return -EINVAL;
  5218. }
  5219. if (num_peer_notif < 0 || num_peer_notif > 255) {
  5220. pr_warn("Warning: num_grat_arp/num_unsol_na (%d) not in range 0-255 so it was reset to 1\n",
  5221. num_peer_notif);
  5222. num_peer_notif = 1;
  5223. }
  5224. /* reset values for 802.3ad/TLB/ALB */
  5225. if (!bond_mode_uses_arp(bond_mode)) {
  5226. if (!miimon) {
  5227. pr_warn("Warning: miimon must be specified, otherwise bonding will not detect link failure, speed and duplex which are essential for 802.3ad operation\n");
  5228. pr_warn("Forcing miimon to 100msec\n");
  5229. miimon = BOND_DEFAULT_MIIMON;
  5230. }
  5231. }
  5232. if (tx_queues < 1 || tx_queues > 255) {
  5233. pr_warn("Warning: tx_queues (%d) should be between 1 and 255, resetting to %d\n",
  5234. tx_queues, BOND_DEFAULT_TX_QUEUES);
  5235. tx_queues = BOND_DEFAULT_TX_QUEUES;
  5236. }
  5237. if ((all_slaves_active != 0) && (all_slaves_active != 1)) {
  5238. pr_warn("Warning: all_slaves_active module parameter (%d), not of valid value (0/1), so it was set to 0\n",
  5239. all_slaves_active);
  5240. all_slaves_active = 0;
  5241. }
  5242. if (resend_igmp < 0 || resend_igmp > 255) {
  5243. pr_warn("Warning: resend_igmp (%d) should be between 0 and 255, resetting to %d\n",
  5244. resend_igmp, BOND_DEFAULT_RESEND_IGMP);
  5245. resend_igmp = BOND_DEFAULT_RESEND_IGMP;
  5246. }
  5247. bond_opt_initval(&newval, packets_per_slave);
  5248. if (!bond_opt_parse(bond_opt_get(BOND_OPT_PACKETS_PER_SLAVE), &newval)) {
  5249. pr_warn("Warning: packets_per_slave (%d) should be between 0 and %u resetting to 1\n",
  5250. packets_per_slave, USHRT_MAX);
  5251. packets_per_slave = 1;
  5252. }
  5253. if (bond_mode == BOND_MODE_ALB) {
  5254. pr_notice("In ALB mode you might experience client disconnections upon reconnection of a link if the bonding module updelay parameter (%d msec) is incompatible with the forwarding delay time of the switch\n",
  5255. updelay);
  5256. }
  5257. if (!miimon) {
  5258. if (updelay || downdelay) {
  5259. /* just warn the user the up/down delay will have
  5260. * no effect since miimon is zero...
  5261. */
  5262. pr_warn("Warning: miimon module parameter not set and updelay (%d) or downdelay (%d) module parameter is set; updelay and downdelay have no effect unless miimon is set\n",
  5263. updelay, downdelay);
  5264. }
  5265. } else {
  5266. /* don't allow arp monitoring */
  5267. if (arp_interval) {
  5268. pr_warn("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n",
  5269. miimon, arp_interval);
  5270. arp_interval = 0;
  5271. }
  5272. if ((updelay % miimon) != 0) {
  5273. pr_warn("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
  5274. updelay, miimon, (updelay / miimon) * miimon);
  5275. }
  5276. updelay /= miimon;
  5277. if ((downdelay % miimon) != 0) {
  5278. pr_warn("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n",
  5279. downdelay, miimon,
  5280. (downdelay / miimon) * miimon);
  5281. }
  5282. downdelay /= miimon;
  5283. }
  5284. if (arp_interval < 0) {
  5285. pr_warn("Warning: arp_interval module parameter (%d), not in range 0-%d, so it was reset to 0\n",
  5286. arp_interval, INT_MAX);
  5287. arp_interval = 0;
  5288. }
  5289. for (arp_ip_count = 0, i = 0;
  5290. (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[i]; i++) {
  5291. __be32 ip;
  5292. /* not a complete check, but good enough to catch mistakes */
  5293. if (!in4_pton(arp_ip_target[i], -1, (u8 *)&ip, -1, NULL) ||
  5294. !bond_is_ip_target_ok(ip)) {
  5295. pr_warn("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n",
  5296. arp_ip_target[i]);
  5297. arp_interval = 0;
  5298. } else {
  5299. if (bond_get_targets_ip(arp_target, ip) == -1)
  5300. arp_target[arp_ip_count++] = ip;
  5301. else
  5302. pr_warn("Warning: duplicate address %pI4 in arp_ip_target, skipping\n",
  5303. &ip);
  5304. }
  5305. }
  5306. if (arp_interval && !arp_ip_count) {
  5307. /* don't allow arping if no arp_ip_target given... */
  5308. pr_warn("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n",
  5309. arp_interval);
  5310. arp_interval = 0;
  5311. }
  5312. if (arp_validate) {
  5313. if (!arp_interval) {
  5314. pr_err("arp_validate requires arp_interval\n");
  5315. return -EINVAL;
  5316. }
  5317. bond_opt_initstr(&newval, arp_validate);
  5318. valptr = bond_opt_parse(bond_opt_get(BOND_OPT_ARP_VALIDATE),
  5319. &newval);
  5320. if (!valptr) {
  5321. pr_err("Error: invalid arp_validate \"%s\"\n",
  5322. arp_validate);
  5323. return -EINVAL;
  5324. }
  5325. arp_validate_value = valptr->value;
  5326. } else {
  5327. arp_validate_value = 0;
  5328. }
  5329. if (arp_all_targets) {
  5330. bond_opt_initstr(&newval, arp_all_targets);
  5331. valptr = bond_opt_parse(bond_opt_get(BOND_OPT_ARP_ALL_TARGETS),
  5332. &newval);
  5333. if (!valptr) {
  5334. pr_err("Error: invalid arp_all_targets_value \"%s\"\n",
  5335. arp_all_targets);
  5336. arp_all_targets_value = 0;
  5337. } else {
  5338. arp_all_targets_value = valptr->value;
  5339. }
  5340. }
  5341. if (miimon) {
  5342. pr_info("MII link monitoring set to %d ms\n", miimon);
  5343. } else if (arp_interval) {
  5344. valptr = bond_opt_get_val(BOND_OPT_ARP_VALIDATE,
  5345. arp_validate_value);
  5346. pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):",
  5347. arp_interval, valptr->string, arp_ip_count);
  5348. for (i = 0; i < arp_ip_count; i++)
  5349. pr_cont(" %s", arp_ip_target[i]);
  5350. pr_cont("\n");
  5351. } else if (max_bonds) {
  5352. /* miimon and arp_interval not set, we need one so things
  5353. * work as expected, see bonding.txt for details
  5354. */
  5355. pr_debug("Warning: either miimon or arp_interval and arp_ip_target module parameters must be specified, otherwise bonding will not detect link failures! see bonding.txt for details\n");
  5356. }
  5357. if (primary && !bond_mode_uses_primary(bond_mode)) {
  5358. /* currently, using a primary only makes sense
  5359. * in active backup, TLB or ALB modes
  5360. */
  5361. pr_warn("Warning: %s primary device specified but has no effect in %s mode\n",
  5362. primary, bond_mode_name(bond_mode));
  5363. primary = NULL;
  5364. }
  5365. if (primary && primary_reselect) {
  5366. bond_opt_initstr(&newval, primary_reselect);
  5367. valptr = bond_opt_parse(bond_opt_get(BOND_OPT_PRIMARY_RESELECT),
  5368. &newval);
  5369. if (!valptr) {
  5370. pr_err("Error: Invalid primary_reselect \"%s\"\n",
  5371. primary_reselect);
  5372. return -EINVAL;
  5373. }
  5374. primary_reselect_value = valptr->value;
  5375. } else {
  5376. primary_reselect_value = BOND_PRI_RESELECT_ALWAYS;
  5377. }
  5378. if (fail_over_mac) {
  5379. bond_opt_initstr(&newval, fail_over_mac);
  5380. valptr = bond_opt_parse(bond_opt_get(BOND_OPT_FAIL_OVER_MAC),
  5381. &newval);
  5382. if (!valptr) {
  5383. pr_err("Error: invalid fail_over_mac \"%s\"\n",
  5384. fail_over_mac);
  5385. return -EINVAL;
  5386. }
  5387. fail_over_mac_value = valptr->value;
  5388. if (bond_mode != BOND_MODE_ACTIVEBACKUP)
  5389. pr_warn("Warning: fail_over_mac only affects active-backup mode\n");
  5390. } else {
  5391. fail_over_mac_value = BOND_FOM_NONE;
  5392. }
  5393. bond_opt_initstr(&newval, "default");
  5394. valptr = bond_opt_parse(
  5395. bond_opt_get(BOND_OPT_AD_ACTOR_SYS_PRIO),
  5396. &newval);
  5397. if (!valptr) {
  5398. pr_err("Error: No ad_actor_sys_prio default value");
  5399. return -EINVAL;
  5400. }
  5401. ad_actor_sys_prio = valptr->value;
  5402. valptr = bond_opt_parse(bond_opt_get(BOND_OPT_AD_USER_PORT_KEY),
  5403. &newval);
  5404. if (!valptr) {
  5405. pr_err("Error: No ad_user_port_key default value");
  5406. return -EINVAL;
  5407. }
  5408. ad_user_port_key = valptr->value;
  5409. bond_opt_initstr(&newval, "default");
  5410. valptr = bond_opt_parse(bond_opt_get(BOND_OPT_TLB_DYNAMIC_LB), &newval);
  5411. if (!valptr) {
  5412. pr_err("Error: No tlb_dynamic_lb default value");
  5413. return -EINVAL;
  5414. }
  5415. tlb_dynamic_lb = valptr->value;
  5416. if (lp_interval == 0) {
  5417. pr_warn("Warning: ip_interval must be between 1 and %d, so it was reset to %d\n",
  5418. INT_MAX, BOND_ALB_DEFAULT_LP_INTERVAL);
  5419. lp_interval = BOND_ALB_DEFAULT_LP_INTERVAL;
  5420. }
  5421. /* fill params struct with the proper values */
  5422. params->mode = bond_mode;
  5423. params->xmit_policy = xmit_hashtype;
  5424. params->miimon = miimon;
  5425. params->num_peer_notif = num_peer_notif;
  5426. params->arp_interval = arp_interval;
  5427. params->arp_validate = arp_validate_value;
  5428. params->arp_all_targets = arp_all_targets_value;
  5429. params->missed_max = 2;
  5430. params->updelay = updelay;
  5431. params->downdelay = downdelay;
  5432. params->peer_notif_delay = 0;
  5433. params->lacp_active = 1;
  5434. params->lacp_fast = lacp_fast;
  5435. params->primary[0] = 0;
  5436. params->primary_reselect = primary_reselect_value;
  5437. params->fail_over_mac = fail_over_mac_value;
  5438. params->tx_queues = tx_queues;
  5439. params->all_slaves_active = all_slaves_active;
  5440. params->resend_igmp = resend_igmp;
  5441. params->min_links = min_links;
  5442. params->lp_interval = lp_interval;
  5443. params->packets_per_slave = packets_per_slave;
  5444. params->tlb_dynamic_lb = tlb_dynamic_lb;
  5445. params->ad_actor_sys_prio = ad_actor_sys_prio;
  5446. eth_zero_addr(params->ad_actor_system);
  5447. params->ad_user_port_key = ad_user_port_key;
  5448. params->coupled_control = 1;
  5449. params->broadcast_neighbor = 0;
  5450. if (packets_per_slave > 0) {
  5451. params->reciprocal_packets_per_slave =
  5452. reciprocal_value(packets_per_slave);
  5453. } else {
  5454. /* reciprocal_packets_per_slave is unused if
  5455. * packets_per_slave is 0 or 1, just initialize it
  5456. */
  5457. params->reciprocal_packets_per_slave =
  5458. (struct reciprocal_value) { 0 };
  5459. }
  5460. if (primary)
  5461. strscpy_pad(params->primary, primary, sizeof(params->primary));
  5462. memcpy(params->arp_targets, arp_target, sizeof(arp_target));
  5463. #if IS_ENABLED(CONFIG_IPV6)
  5464. memset(params->ns_targets, 0, sizeof(struct in6_addr) * BOND_MAX_NS_TARGETS);
  5465. #endif
  5466. return 0;
  5467. }
  5468. /* Called from registration process */
  5469. static int bond_init(struct net_device *bond_dev)
  5470. {
  5471. struct bonding *bond = netdev_priv(bond_dev);
  5472. struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
  5473. netdev_dbg(bond_dev, "Begin bond_init\n");
  5474. bond->wq = alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM,
  5475. bond_dev->name);
  5476. if (!bond->wq)
  5477. return -ENOMEM;
  5478. bond->notifier_ctx = false;
  5479. spin_lock_init(&bond->stats_lock);
  5480. netdev_lockdep_set_classes(bond_dev);
  5481. list_add_tail_rcu(&bond->bond_list, &bn->dev_list);
  5482. bond_prepare_sysfs_group(bond);
  5483. bond_debug_register(bond);
  5484. /* Ensure valid dev_addr */
  5485. if (is_zero_ether_addr(bond_dev->dev_addr) &&
  5486. bond_dev->addr_assign_type == NET_ADDR_PERM)
  5487. eth_hw_addr_random(bond_dev);
  5488. return 0;
  5489. }
  5490. unsigned int bond_get_num_tx_queues(void)
  5491. {
  5492. return tx_queues;
  5493. }
  5494. /* Create a new bond based on the specified name and bonding parameters.
  5495. * If name is NULL, obtain a suitable "bond%d" name for us.
  5496. * Caller must NOT hold rtnl_lock; we need to release it here before we
  5497. * set up our sysfs entries.
  5498. */
  5499. int bond_create(struct net *net, const char *name)
  5500. {
  5501. struct net_device *bond_dev;
  5502. struct bonding *bond;
  5503. int res = -ENOMEM;
  5504. rtnl_lock();
  5505. bond_dev = alloc_netdev_mq(sizeof(struct bonding),
  5506. name ? name : "bond%d", NET_NAME_UNKNOWN,
  5507. bond_setup, tx_queues);
  5508. if (!bond_dev)
  5509. goto out;
  5510. bond = netdev_priv(bond_dev);
  5511. dev_net_set(bond_dev, net);
  5512. bond_dev->rtnl_link_ops = &bond_link_ops;
  5513. res = register_netdevice(bond_dev);
  5514. if (res < 0) {
  5515. free_netdev(bond_dev);
  5516. goto out;
  5517. }
  5518. netif_carrier_off(bond_dev);
  5519. bond_work_init_all(bond);
  5520. out:
  5521. rtnl_unlock();
  5522. return res;
  5523. }
  5524. static int __net_init bond_net_init(struct net *net)
  5525. {
  5526. struct bond_net *bn = net_generic(net, bond_net_id);
  5527. bn->net = net;
  5528. INIT_LIST_HEAD(&bn->dev_list);
  5529. bond_create_proc_dir(bn);
  5530. bond_create_sysfs(bn);
  5531. return 0;
  5532. }
  5533. /* According to commit 69b0216ac255 ("bonding: fix bonding_masters
  5534. * race condition in bond unloading") we need to remove sysfs files
  5535. * before we remove our devices (done later in bond_net_exit_rtnl())
  5536. */
  5537. static void __net_exit bond_net_pre_exit(struct net *net)
  5538. {
  5539. struct bond_net *bn = net_generic(net, bond_net_id);
  5540. bond_destroy_sysfs(bn);
  5541. }
  5542. static void __net_exit bond_net_exit_rtnl(struct net *net,
  5543. struct list_head *dev_kill_list)
  5544. {
  5545. struct bond_net *bn = net_generic(net, bond_net_id);
  5546. struct bonding *bond, *tmp_bond;
  5547. /* Kill off any bonds created after unregistering bond rtnl ops */
  5548. list_for_each_entry_safe(bond, tmp_bond, &bn->dev_list, bond_list)
  5549. unregister_netdevice_queue(bond->dev, dev_kill_list);
  5550. }
  5551. /* According to commit 23fa5c2caae0 ("bonding: destroy proc directory
  5552. * only after all bonds are gone") bond_destroy_proc_dir() is called
  5553. * after bond_net_exit_rtnl() has completed.
  5554. */
  5555. static void __net_exit bond_net_exit_batch(struct list_head *net_list)
  5556. {
  5557. struct bond_net *bn;
  5558. struct net *net;
  5559. list_for_each_entry(net, net_list, exit_list) {
  5560. bn = net_generic(net, bond_net_id);
  5561. bond_destroy_proc_dir(bn);
  5562. }
  5563. }
  5564. static struct pernet_operations bond_net_ops = {
  5565. .init = bond_net_init,
  5566. .pre_exit = bond_net_pre_exit,
  5567. .exit_rtnl = bond_net_exit_rtnl,
  5568. .exit_batch = bond_net_exit_batch,
  5569. .id = &bond_net_id,
  5570. .size = sizeof(struct bond_net),
  5571. };
  5572. static int __init bonding_init(void)
  5573. {
  5574. int i;
  5575. int res;
  5576. res = bond_check_params(&bonding_defaults);
  5577. if (res)
  5578. goto out;
  5579. bond_create_debugfs();
  5580. res = register_pernet_subsys(&bond_net_ops);
  5581. if (res)
  5582. goto err_net_ops;
  5583. res = bond_netlink_init();
  5584. if (res)
  5585. goto err_link;
  5586. for (i = 0; i < max_bonds; i++) {
  5587. res = bond_create(&init_net, NULL);
  5588. if (res)
  5589. goto err;
  5590. }
  5591. skb_flow_dissector_init(&flow_keys_bonding,
  5592. flow_keys_bonding_keys,
  5593. ARRAY_SIZE(flow_keys_bonding_keys));
  5594. register_netdevice_notifier(&bond_netdev_notifier);
  5595. out:
  5596. return res;
  5597. err:
  5598. bond_netlink_fini();
  5599. err_link:
  5600. unregister_pernet_subsys(&bond_net_ops);
  5601. err_net_ops:
  5602. bond_destroy_debugfs();
  5603. goto out;
  5604. }
  5605. static void __exit bonding_exit(void)
  5606. {
  5607. unregister_netdevice_notifier(&bond_netdev_notifier);
  5608. bond_netlink_fini();
  5609. unregister_pernet_subsys(&bond_net_ops);
  5610. bond_destroy_debugfs();
  5611. #ifdef CONFIG_NET_POLL_CONTROLLER
  5612. /* Make sure we don't have an imbalance on our netpoll blocking */
  5613. WARN_ON(atomic_read(&netpoll_block_tx));
  5614. #endif
  5615. }
  5616. module_init(bonding_init);
  5617. module_exit(bonding_exit);
  5618. MODULE_LICENSE("GPL");
  5619. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  5620. MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
  5621. MODULE_IMPORT_NS("NETDEV_INTERNAL");