bond_main.c 175 KB

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