nexthop.c 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Generic nexthop implementation
  3. *
  4. * Copyright (c) 2017-19 Cumulus Networks
  5. * Copyright (c) 2017-19 David Ahern <dsa@cumulusnetworks.com>
  6. */
  7. #include <linux/nexthop.h>
  8. #include <linux/rtnetlink.h>
  9. #include <linux/slab.h>
  10. #include <linux/vmalloc.h>
  11. #include <net/arp.h>
  12. #include <net/ipv6_stubs.h>
  13. #include <net/lwtunnel.h>
  14. #include <net/ndisc.h>
  15. #include <net/nexthop.h>
  16. #include <net/route.h>
  17. #include <net/sock.h>
  18. #define NH_RES_DEFAULT_IDLE_TIMER (120 * HZ)
  19. #define NH_RES_DEFAULT_UNBALANCED_TIMER 0 /* No forced rebalancing. */
  20. static void remove_nexthop(struct net *net, struct nexthop *nh,
  21. struct nl_info *nlinfo);
  22. #define NH_DEV_HASHBITS 8
  23. #define NH_DEV_HASHSIZE (1U << NH_DEV_HASHBITS)
  24. #define NHA_OP_FLAGS_DUMP_ALL (NHA_OP_FLAG_DUMP_STATS | \
  25. NHA_OP_FLAG_DUMP_HW_STATS)
  26. static const struct nla_policy rtm_nh_policy_new[] = {
  27. [NHA_ID] = { .type = NLA_U32 },
  28. [NHA_GROUP] = { .type = NLA_BINARY },
  29. [NHA_GROUP_TYPE] = { .type = NLA_U16 },
  30. [NHA_BLACKHOLE] = { .type = NLA_FLAG },
  31. [NHA_OIF] = { .type = NLA_U32 },
  32. [NHA_GATEWAY] = { .type = NLA_BINARY },
  33. [NHA_ENCAP_TYPE] = { .type = NLA_U16 },
  34. [NHA_ENCAP] = { .type = NLA_NESTED },
  35. [NHA_FDB] = { .type = NLA_FLAG },
  36. [NHA_RES_GROUP] = { .type = NLA_NESTED },
  37. [NHA_HW_STATS_ENABLE] = NLA_POLICY_MAX(NLA_U32, true),
  38. };
  39. static const struct nla_policy rtm_nh_policy_get[] = {
  40. [NHA_ID] = { .type = NLA_U32 },
  41. [NHA_OP_FLAGS] = NLA_POLICY_MASK(NLA_U32,
  42. NHA_OP_FLAGS_DUMP_ALL),
  43. };
  44. static const struct nla_policy rtm_nh_policy_del[] = {
  45. [NHA_ID] = { .type = NLA_U32 },
  46. };
  47. static const struct nla_policy rtm_nh_policy_dump[] = {
  48. [NHA_OIF] = { .type = NLA_U32 },
  49. [NHA_GROUPS] = { .type = NLA_FLAG },
  50. [NHA_MASTER] = { .type = NLA_U32 },
  51. [NHA_FDB] = { .type = NLA_FLAG },
  52. [NHA_OP_FLAGS] = NLA_POLICY_MASK(NLA_U32,
  53. NHA_OP_FLAGS_DUMP_ALL),
  54. };
  55. static const struct nla_policy rtm_nh_res_policy_new[] = {
  56. [NHA_RES_GROUP_BUCKETS] = { .type = NLA_U16 },
  57. [NHA_RES_GROUP_IDLE_TIMER] = { .type = NLA_U32 },
  58. [NHA_RES_GROUP_UNBALANCED_TIMER] = { .type = NLA_U32 },
  59. };
  60. static const struct nla_policy rtm_nh_policy_dump_bucket[] = {
  61. [NHA_ID] = { .type = NLA_U32 },
  62. [NHA_OIF] = { .type = NLA_U32 },
  63. [NHA_MASTER] = { .type = NLA_U32 },
  64. [NHA_RES_BUCKET] = { .type = NLA_NESTED },
  65. };
  66. static const struct nla_policy rtm_nh_res_bucket_policy_dump[] = {
  67. [NHA_RES_BUCKET_NH_ID] = { .type = NLA_U32 },
  68. };
  69. static const struct nla_policy rtm_nh_policy_get_bucket[] = {
  70. [NHA_ID] = { .type = NLA_U32 },
  71. [NHA_RES_BUCKET] = { .type = NLA_NESTED },
  72. };
  73. static const struct nla_policy rtm_nh_res_bucket_policy_get[] = {
  74. [NHA_RES_BUCKET_INDEX] = { .type = NLA_U16 },
  75. };
  76. static bool nexthop_notifiers_is_empty(struct net *net)
  77. {
  78. return !net->nexthop.notifier_chain.head;
  79. }
  80. static void
  81. __nh_notifier_single_info_init(struct nh_notifier_single_info *nh_info,
  82. const struct nh_info *nhi)
  83. {
  84. nh_info->dev = nhi->fib_nhc.nhc_dev;
  85. nh_info->gw_family = nhi->fib_nhc.nhc_gw_family;
  86. if (nh_info->gw_family == AF_INET)
  87. nh_info->ipv4 = nhi->fib_nhc.nhc_gw.ipv4;
  88. else if (nh_info->gw_family == AF_INET6)
  89. nh_info->ipv6 = nhi->fib_nhc.nhc_gw.ipv6;
  90. nh_info->id = nhi->nh_parent->id;
  91. nh_info->is_reject = nhi->reject_nh;
  92. nh_info->is_fdb = nhi->fdb_nh;
  93. nh_info->has_encap = !!nhi->fib_nhc.nhc_lwtstate;
  94. }
  95. static int nh_notifier_single_info_init(struct nh_notifier_info *info,
  96. const struct nexthop *nh)
  97. {
  98. struct nh_info *nhi = rtnl_dereference(nh->nh_info);
  99. info->type = NH_NOTIFIER_INFO_TYPE_SINGLE;
  100. info->nh = kzalloc(sizeof(*info->nh), GFP_KERNEL);
  101. if (!info->nh)
  102. return -ENOMEM;
  103. __nh_notifier_single_info_init(info->nh, nhi);
  104. return 0;
  105. }
  106. static void nh_notifier_single_info_fini(struct nh_notifier_info *info)
  107. {
  108. kfree(info->nh);
  109. }
  110. static int nh_notifier_mpath_info_init(struct nh_notifier_info *info,
  111. struct nh_group *nhg)
  112. {
  113. u16 num_nh = nhg->num_nh;
  114. int i;
  115. info->type = NH_NOTIFIER_INFO_TYPE_GRP;
  116. info->nh_grp = kzalloc(struct_size(info->nh_grp, nh_entries, num_nh),
  117. GFP_KERNEL);
  118. if (!info->nh_grp)
  119. return -ENOMEM;
  120. info->nh_grp->num_nh = num_nh;
  121. info->nh_grp->is_fdb = nhg->fdb_nh;
  122. info->nh_grp->hw_stats = nhg->hw_stats;
  123. for (i = 0; i < num_nh; i++) {
  124. struct nh_grp_entry *nhge = &nhg->nh_entries[i];
  125. struct nh_info *nhi;
  126. nhi = rtnl_dereference(nhge->nh->nh_info);
  127. info->nh_grp->nh_entries[i].weight = nhge->weight;
  128. __nh_notifier_single_info_init(&info->nh_grp->nh_entries[i].nh,
  129. nhi);
  130. }
  131. return 0;
  132. }
  133. static int nh_notifier_res_table_info_init(struct nh_notifier_info *info,
  134. struct nh_group *nhg)
  135. {
  136. struct nh_res_table *res_table = rtnl_dereference(nhg->res_table);
  137. u16 num_nh_buckets = res_table->num_nh_buckets;
  138. unsigned long size;
  139. u16 i;
  140. info->type = NH_NOTIFIER_INFO_TYPE_RES_TABLE;
  141. size = struct_size(info->nh_res_table, nhs, num_nh_buckets);
  142. info->nh_res_table = __vmalloc(size, GFP_KERNEL | __GFP_ZERO |
  143. __GFP_NOWARN);
  144. if (!info->nh_res_table)
  145. return -ENOMEM;
  146. info->nh_res_table->num_nh_buckets = num_nh_buckets;
  147. info->nh_res_table->hw_stats = nhg->hw_stats;
  148. for (i = 0; i < num_nh_buckets; i++) {
  149. struct nh_res_bucket *bucket = &res_table->nh_buckets[i];
  150. struct nh_grp_entry *nhge;
  151. struct nh_info *nhi;
  152. nhge = rtnl_dereference(bucket->nh_entry);
  153. nhi = rtnl_dereference(nhge->nh->nh_info);
  154. __nh_notifier_single_info_init(&info->nh_res_table->nhs[i],
  155. nhi);
  156. }
  157. return 0;
  158. }
  159. static int nh_notifier_grp_info_init(struct nh_notifier_info *info,
  160. const struct nexthop *nh)
  161. {
  162. struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
  163. if (nhg->hash_threshold)
  164. return nh_notifier_mpath_info_init(info, nhg);
  165. else if (nhg->resilient)
  166. return nh_notifier_res_table_info_init(info, nhg);
  167. return -EINVAL;
  168. }
  169. static void nh_notifier_grp_info_fini(struct nh_notifier_info *info,
  170. const struct nexthop *nh)
  171. {
  172. struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
  173. if (nhg->hash_threshold)
  174. kfree(info->nh_grp);
  175. else if (nhg->resilient)
  176. vfree(info->nh_res_table);
  177. }
  178. static int nh_notifier_info_init(struct nh_notifier_info *info,
  179. const struct nexthop *nh)
  180. {
  181. info->id = nh->id;
  182. if (nh->is_group)
  183. return nh_notifier_grp_info_init(info, nh);
  184. else
  185. return nh_notifier_single_info_init(info, nh);
  186. }
  187. static void nh_notifier_info_fini(struct nh_notifier_info *info,
  188. const struct nexthop *nh)
  189. {
  190. if (nh->is_group)
  191. nh_notifier_grp_info_fini(info, nh);
  192. else
  193. nh_notifier_single_info_fini(info);
  194. }
  195. static int call_nexthop_notifiers(struct net *net,
  196. enum nexthop_event_type event_type,
  197. struct nexthop *nh,
  198. struct netlink_ext_ack *extack)
  199. {
  200. struct nh_notifier_info info = {
  201. .net = net,
  202. .extack = extack,
  203. };
  204. int err;
  205. ASSERT_RTNL();
  206. if (nexthop_notifiers_is_empty(net))
  207. return 0;
  208. err = nh_notifier_info_init(&info, nh);
  209. if (err) {
  210. NL_SET_ERR_MSG(extack, "Failed to initialize nexthop notifier info");
  211. return err;
  212. }
  213. err = blocking_notifier_call_chain(&net->nexthop.notifier_chain,
  214. event_type, &info);
  215. nh_notifier_info_fini(&info, nh);
  216. return notifier_to_errno(err);
  217. }
  218. static int
  219. nh_notifier_res_bucket_idle_timer_get(const struct nh_notifier_info *info,
  220. bool force, unsigned int *p_idle_timer_ms)
  221. {
  222. struct nh_res_table *res_table;
  223. struct nh_group *nhg;
  224. struct nexthop *nh;
  225. int err = 0;
  226. /* When 'force' is false, nexthop bucket replacement is performed
  227. * because the bucket was deemed to be idle. In this case, capable
  228. * listeners can choose to perform an atomic replacement: The bucket is
  229. * only replaced if it is inactive. However, if the idle timer interval
  230. * is smaller than the interval in which a listener is querying
  231. * buckets' activity from the device, then atomic replacement should
  232. * not be tried. Pass the idle timer value to listeners, so that they
  233. * could determine which type of replacement to perform.
  234. */
  235. if (force) {
  236. *p_idle_timer_ms = 0;
  237. return 0;
  238. }
  239. rcu_read_lock();
  240. nh = nexthop_find_by_id(info->net, info->id);
  241. if (!nh) {
  242. err = -EINVAL;
  243. goto out;
  244. }
  245. nhg = rcu_dereference(nh->nh_grp);
  246. res_table = rcu_dereference(nhg->res_table);
  247. *p_idle_timer_ms = jiffies_to_msecs(res_table->idle_timer);
  248. out:
  249. rcu_read_unlock();
  250. return err;
  251. }
  252. static int nh_notifier_res_bucket_info_init(struct nh_notifier_info *info,
  253. u16 bucket_index, bool force,
  254. struct nh_info *oldi,
  255. struct nh_info *newi)
  256. {
  257. unsigned int idle_timer_ms;
  258. int err;
  259. err = nh_notifier_res_bucket_idle_timer_get(info, force,
  260. &idle_timer_ms);
  261. if (err)
  262. return err;
  263. info->type = NH_NOTIFIER_INFO_TYPE_RES_BUCKET;
  264. info->nh_res_bucket = kzalloc(sizeof(*info->nh_res_bucket),
  265. GFP_KERNEL);
  266. if (!info->nh_res_bucket)
  267. return -ENOMEM;
  268. info->nh_res_bucket->bucket_index = bucket_index;
  269. info->nh_res_bucket->idle_timer_ms = idle_timer_ms;
  270. info->nh_res_bucket->force = force;
  271. __nh_notifier_single_info_init(&info->nh_res_bucket->old_nh, oldi);
  272. __nh_notifier_single_info_init(&info->nh_res_bucket->new_nh, newi);
  273. return 0;
  274. }
  275. static void nh_notifier_res_bucket_info_fini(struct nh_notifier_info *info)
  276. {
  277. kfree(info->nh_res_bucket);
  278. }
  279. static int __call_nexthop_res_bucket_notifiers(struct net *net, u32 nhg_id,
  280. u16 bucket_index, bool force,
  281. struct nh_info *oldi,
  282. struct nh_info *newi,
  283. struct netlink_ext_ack *extack)
  284. {
  285. struct nh_notifier_info info = {
  286. .net = net,
  287. .extack = extack,
  288. .id = nhg_id,
  289. };
  290. int err;
  291. if (nexthop_notifiers_is_empty(net))
  292. return 0;
  293. err = nh_notifier_res_bucket_info_init(&info, bucket_index, force,
  294. oldi, newi);
  295. if (err)
  296. return err;
  297. err = blocking_notifier_call_chain(&net->nexthop.notifier_chain,
  298. NEXTHOP_EVENT_BUCKET_REPLACE, &info);
  299. nh_notifier_res_bucket_info_fini(&info);
  300. return notifier_to_errno(err);
  301. }
  302. /* There are three users of RES_TABLE, and NHs etc. referenced from there:
  303. *
  304. * 1) a collection of callbacks for NH maintenance. This operates under
  305. * RTNL,
  306. * 2) the delayed work that gradually balances the resilient table,
  307. * 3) and nexthop_select_path(), operating under RCU.
  308. *
  309. * Both the delayed work and the RTNL block are writers, and need to
  310. * maintain mutual exclusion. Since there are only two and well-known
  311. * writers for each table, the RTNL code can make sure it has exclusive
  312. * access thus:
  313. *
  314. * - Have the DW operate without locking;
  315. * - synchronously cancel the DW;
  316. * - do the writing;
  317. * - if the write was not actually a delete, call upkeep, which schedules
  318. * DW again if necessary.
  319. *
  320. * The functions that are always called from the RTNL context use
  321. * rtnl_dereference(). The functions that can also be called from the DW do
  322. * a raw dereference and rely on the above mutual exclusion scheme.
  323. */
  324. #define nh_res_dereference(p) (rcu_dereference_raw(p))
  325. static int call_nexthop_res_bucket_notifiers(struct net *net, u32 nhg_id,
  326. u16 bucket_index, bool force,
  327. struct nexthop *old_nh,
  328. struct nexthop *new_nh,
  329. struct netlink_ext_ack *extack)
  330. {
  331. struct nh_info *oldi = nh_res_dereference(old_nh->nh_info);
  332. struct nh_info *newi = nh_res_dereference(new_nh->nh_info);
  333. return __call_nexthop_res_bucket_notifiers(net, nhg_id, bucket_index,
  334. force, oldi, newi, extack);
  335. }
  336. static int call_nexthop_res_table_notifiers(struct net *net, struct nexthop *nh,
  337. struct netlink_ext_ack *extack)
  338. {
  339. struct nh_notifier_info info = {
  340. .net = net,
  341. .extack = extack,
  342. .id = nh->id,
  343. };
  344. struct nh_group *nhg;
  345. int err;
  346. ASSERT_RTNL();
  347. if (nexthop_notifiers_is_empty(net))
  348. return 0;
  349. /* At this point, the nexthop buckets are still not populated. Only
  350. * emit a notification with the logical nexthops, so that a listener
  351. * could potentially veto it in case of unsupported configuration.
  352. */
  353. nhg = rtnl_dereference(nh->nh_grp);
  354. err = nh_notifier_mpath_info_init(&info, nhg);
  355. if (err) {
  356. NL_SET_ERR_MSG(extack, "Failed to initialize nexthop notifier info");
  357. return err;
  358. }
  359. err = blocking_notifier_call_chain(&net->nexthop.notifier_chain,
  360. NEXTHOP_EVENT_RES_TABLE_PRE_REPLACE,
  361. &info);
  362. kfree(info.nh_grp);
  363. return notifier_to_errno(err);
  364. }
  365. static int call_nexthop_notifier(struct notifier_block *nb, struct net *net,
  366. enum nexthop_event_type event_type,
  367. struct nexthop *nh,
  368. struct netlink_ext_ack *extack)
  369. {
  370. struct nh_notifier_info info = {
  371. .net = net,
  372. .extack = extack,
  373. };
  374. int err;
  375. err = nh_notifier_info_init(&info, nh);
  376. if (err)
  377. return err;
  378. err = nb->notifier_call(nb, event_type, &info);
  379. nh_notifier_info_fini(&info, nh);
  380. return notifier_to_errno(err);
  381. }
  382. static unsigned int nh_dev_hashfn(unsigned int val)
  383. {
  384. unsigned int mask = NH_DEV_HASHSIZE - 1;
  385. return (val ^
  386. (val >> NH_DEV_HASHBITS) ^
  387. (val >> (NH_DEV_HASHBITS * 2))) & mask;
  388. }
  389. static void nexthop_devhash_add(struct net *net, struct nh_info *nhi)
  390. {
  391. struct net_device *dev = nhi->fib_nhc.nhc_dev;
  392. struct hlist_head *head;
  393. unsigned int hash;
  394. WARN_ON(!dev);
  395. hash = nh_dev_hashfn(dev->ifindex);
  396. head = &net->nexthop.devhash[hash];
  397. hlist_add_head(&nhi->dev_hash, head);
  398. }
  399. static void nexthop_free_group(struct nexthop *nh)
  400. {
  401. struct nh_group *nhg;
  402. int i;
  403. nhg = rcu_dereference_raw(nh->nh_grp);
  404. for (i = 0; i < nhg->num_nh; ++i) {
  405. struct nh_grp_entry *nhge = &nhg->nh_entries[i];
  406. WARN_ON(!list_empty(&nhge->nh_list));
  407. free_percpu(nhge->stats);
  408. nexthop_put(nhge->nh);
  409. }
  410. WARN_ON(nhg->spare == nhg);
  411. if (nhg->resilient)
  412. vfree(rcu_dereference_raw(nhg->res_table));
  413. kfree(nhg->spare);
  414. kfree(nhg);
  415. }
  416. static void nexthop_free_single(struct nexthop *nh)
  417. {
  418. struct nh_info *nhi;
  419. nhi = rcu_dereference_raw(nh->nh_info);
  420. switch (nhi->family) {
  421. case AF_INET:
  422. fib_nh_release(nh->net, &nhi->fib_nh);
  423. break;
  424. case AF_INET6:
  425. ipv6_stub->fib6_nh_release(&nhi->fib6_nh);
  426. break;
  427. }
  428. kfree(nhi);
  429. }
  430. void nexthop_free_rcu(struct rcu_head *head)
  431. {
  432. struct nexthop *nh = container_of(head, struct nexthop, rcu);
  433. if (nh->is_group)
  434. nexthop_free_group(nh);
  435. else
  436. nexthop_free_single(nh);
  437. kfree(nh);
  438. }
  439. EXPORT_SYMBOL_GPL(nexthop_free_rcu);
  440. static struct nexthop *nexthop_alloc(void)
  441. {
  442. struct nexthop *nh;
  443. nh = kzalloc(sizeof(struct nexthop), GFP_KERNEL);
  444. if (nh) {
  445. INIT_LIST_HEAD(&nh->fi_list);
  446. INIT_LIST_HEAD(&nh->f6i_list);
  447. INIT_LIST_HEAD(&nh->grp_list);
  448. INIT_LIST_HEAD(&nh->fdb_list);
  449. }
  450. return nh;
  451. }
  452. static struct nh_group *nexthop_grp_alloc(u16 num_nh)
  453. {
  454. struct nh_group *nhg;
  455. nhg = kzalloc(struct_size(nhg, nh_entries, num_nh), GFP_KERNEL);
  456. if (nhg)
  457. nhg->num_nh = num_nh;
  458. return nhg;
  459. }
  460. static void nh_res_table_upkeep_dw(struct work_struct *work);
  461. static struct nh_res_table *
  462. nexthop_res_table_alloc(struct net *net, u32 nhg_id, struct nh_config *cfg)
  463. {
  464. const u16 num_nh_buckets = cfg->nh_grp_res_num_buckets;
  465. struct nh_res_table *res_table;
  466. unsigned long size;
  467. size = struct_size(res_table, nh_buckets, num_nh_buckets);
  468. res_table = __vmalloc(size, GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN);
  469. if (!res_table)
  470. return NULL;
  471. res_table->net = net;
  472. res_table->nhg_id = nhg_id;
  473. INIT_DELAYED_WORK(&res_table->upkeep_dw, &nh_res_table_upkeep_dw);
  474. INIT_LIST_HEAD(&res_table->uw_nh_entries);
  475. res_table->idle_timer = cfg->nh_grp_res_idle_timer;
  476. res_table->unbalanced_timer = cfg->nh_grp_res_unbalanced_timer;
  477. res_table->num_nh_buckets = num_nh_buckets;
  478. return res_table;
  479. }
  480. static void nh_base_seq_inc(struct net *net)
  481. {
  482. while (++net->nexthop.seq == 0)
  483. ;
  484. }
  485. /* no reference taken; rcu lock or rtnl must be held */
  486. struct nexthop *nexthop_find_by_id(struct net *net, u32 id)
  487. {
  488. struct rb_node **pp, *parent = NULL, *next;
  489. pp = &net->nexthop.rb_root.rb_node;
  490. while (1) {
  491. struct nexthop *nh;
  492. next = rcu_dereference_raw(*pp);
  493. if (!next)
  494. break;
  495. parent = next;
  496. nh = rb_entry(parent, struct nexthop, rb_node);
  497. if (id < nh->id)
  498. pp = &next->rb_left;
  499. else if (id > nh->id)
  500. pp = &next->rb_right;
  501. else
  502. return nh;
  503. }
  504. return NULL;
  505. }
  506. EXPORT_SYMBOL_GPL(nexthop_find_by_id);
  507. /* used for auto id allocation; called with rtnl held */
  508. static u32 nh_find_unused_id(struct net *net)
  509. {
  510. u32 id_start = net->nexthop.last_id_allocated;
  511. while (1) {
  512. net->nexthop.last_id_allocated++;
  513. if (net->nexthop.last_id_allocated == id_start)
  514. break;
  515. if (!nexthop_find_by_id(net, net->nexthop.last_id_allocated))
  516. return net->nexthop.last_id_allocated;
  517. }
  518. return 0;
  519. }
  520. static void nh_res_time_set_deadline(unsigned long next_time,
  521. unsigned long *deadline)
  522. {
  523. if (time_before(next_time, *deadline))
  524. *deadline = next_time;
  525. }
  526. static clock_t nh_res_table_unbalanced_time(struct nh_res_table *res_table)
  527. {
  528. if (list_empty(&res_table->uw_nh_entries))
  529. return 0;
  530. return jiffies_delta_to_clock_t(jiffies - res_table->unbalanced_since);
  531. }
  532. static int nla_put_nh_group_res(struct sk_buff *skb, struct nh_group *nhg)
  533. {
  534. struct nh_res_table *res_table = rtnl_dereference(nhg->res_table);
  535. struct nlattr *nest;
  536. nest = nla_nest_start(skb, NHA_RES_GROUP);
  537. if (!nest)
  538. return -EMSGSIZE;
  539. if (nla_put_u16(skb, NHA_RES_GROUP_BUCKETS,
  540. res_table->num_nh_buckets) ||
  541. nla_put_u32(skb, NHA_RES_GROUP_IDLE_TIMER,
  542. jiffies_to_clock_t(res_table->idle_timer)) ||
  543. nla_put_u32(skb, NHA_RES_GROUP_UNBALANCED_TIMER,
  544. jiffies_to_clock_t(res_table->unbalanced_timer)) ||
  545. nla_put_u64_64bit(skb, NHA_RES_GROUP_UNBALANCED_TIME,
  546. nh_res_table_unbalanced_time(res_table),
  547. NHA_RES_GROUP_PAD))
  548. goto nla_put_failure;
  549. nla_nest_end(skb, nest);
  550. return 0;
  551. nla_put_failure:
  552. nla_nest_cancel(skb, nest);
  553. return -EMSGSIZE;
  554. }
  555. static void nh_grp_entry_stats_inc(struct nh_grp_entry *nhge)
  556. {
  557. struct nh_grp_entry_stats *cpu_stats;
  558. cpu_stats = get_cpu_ptr(nhge->stats);
  559. u64_stats_update_begin(&cpu_stats->syncp);
  560. u64_stats_inc(&cpu_stats->packets);
  561. u64_stats_update_end(&cpu_stats->syncp);
  562. put_cpu_ptr(cpu_stats);
  563. }
  564. static void nh_grp_entry_stats_read(struct nh_grp_entry *nhge,
  565. u64 *ret_packets)
  566. {
  567. int i;
  568. *ret_packets = 0;
  569. for_each_possible_cpu(i) {
  570. struct nh_grp_entry_stats *cpu_stats;
  571. unsigned int start;
  572. u64 packets;
  573. cpu_stats = per_cpu_ptr(nhge->stats, i);
  574. do {
  575. start = u64_stats_fetch_begin(&cpu_stats->syncp);
  576. packets = u64_stats_read(&cpu_stats->packets);
  577. } while (u64_stats_fetch_retry(&cpu_stats->syncp, start));
  578. *ret_packets += packets;
  579. }
  580. }
  581. static int nh_notifier_grp_hw_stats_init(struct nh_notifier_info *info,
  582. const struct nexthop *nh)
  583. {
  584. struct nh_group *nhg;
  585. int i;
  586. ASSERT_RTNL();
  587. nhg = rtnl_dereference(nh->nh_grp);
  588. info->id = nh->id;
  589. info->type = NH_NOTIFIER_INFO_TYPE_GRP_HW_STATS;
  590. info->nh_grp_hw_stats = kzalloc(struct_size(info->nh_grp_hw_stats,
  591. stats, nhg->num_nh),
  592. GFP_KERNEL);
  593. if (!info->nh_grp_hw_stats)
  594. return -ENOMEM;
  595. info->nh_grp_hw_stats->num_nh = nhg->num_nh;
  596. for (i = 0; i < nhg->num_nh; i++) {
  597. struct nh_grp_entry *nhge = &nhg->nh_entries[i];
  598. info->nh_grp_hw_stats->stats[i].id = nhge->nh->id;
  599. }
  600. return 0;
  601. }
  602. static void nh_notifier_grp_hw_stats_fini(struct nh_notifier_info *info)
  603. {
  604. kfree(info->nh_grp_hw_stats);
  605. }
  606. void nh_grp_hw_stats_report_delta(struct nh_notifier_grp_hw_stats_info *info,
  607. unsigned int nh_idx,
  608. u64 delta_packets)
  609. {
  610. info->hw_stats_used = true;
  611. info->stats[nh_idx].packets += delta_packets;
  612. }
  613. EXPORT_SYMBOL(nh_grp_hw_stats_report_delta);
  614. static void nh_grp_hw_stats_apply_update(struct nexthop *nh,
  615. struct nh_notifier_info *info)
  616. {
  617. struct nh_group *nhg;
  618. int i;
  619. ASSERT_RTNL();
  620. nhg = rtnl_dereference(nh->nh_grp);
  621. for (i = 0; i < nhg->num_nh; i++) {
  622. struct nh_grp_entry *nhge = &nhg->nh_entries[i];
  623. nhge->packets_hw += info->nh_grp_hw_stats->stats[i].packets;
  624. }
  625. }
  626. static int nh_grp_hw_stats_update(struct nexthop *nh, bool *hw_stats_used)
  627. {
  628. struct nh_notifier_info info = {
  629. .net = nh->net,
  630. };
  631. struct net *net = nh->net;
  632. int err;
  633. if (nexthop_notifiers_is_empty(net)) {
  634. *hw_stats_used = false;
  635. return 0;
  636. }
  637. err = nh_notifier_grp_hw_stats_init(&info, nh);
  638. if (err)
  639. return err;
  640. err = blocking_notifier_call_chain(&net->nexthop.notifier_chain,
  641. NEXTHOP_EVENT_HW_STATS_REPORT_DELTA,
  642. &info);
  643. /* Cache whatever we got, even if there was an error, otherwise the
  644. * successful stats retrievals would get lost.
  645. */
  646. nh_grp_hw_stats_apply_update(nh, &info);
  647. *hw_stats_used = info.nh_grp_hw_stats->hw_stats_used;
  648. nh_notifier_grp_hw_stats_fini(&info);
  649. return notifier_to_errno(err);
  650. }
  651. static int nla_put_nh_group_stats_entry(struct sk_buff *skb,
  652. struct nh_grp_entry *nhge,
  653. u32 op_flags)
  654. {
  655. struct nlattr *nest;
  656. u64 packets;
  657. nh_grp_entry_stats_read(nhge, &packets);
  658. nest = nla_nest_start(skb, NHA_GROUP_STATS_ENTRY);
  659. if (!nest)
  660. return -EMSGSIZE;
  661. if (nla_put_u32(skb, NHA_GROUP_STATS_ENTRY_ID, nhge->nh->id) ||
  662. nla_put_uint(skb, NHA_GROUP_STATS_ENTRY_PACKETS,
  663. packets + nhge->packets_hw))
  664. goto nla_put_failure;
  665. if (op_flags & NHA_OP_FLAG_DUMP_HW_STATS &&
  666. nla_put_uint(skb, NHA_GROUP_STATS_ENTRY_PACKETS_HW,
  667. nhge->packets_hw))
  668. goto nla_put_failure;
  669. nla_nest_end(skb, nest);
  670. return 0;
  671. nla_put_failure:
  672. nla_nest_cancel(skb, nest);
  673. return -EMSGSIZE;
  674. }
  675. static int nla_put_nh_group_stats(struct sk_buff *skb, struct nexthop *nh,
  676. u32 op_flags)
  677. {
  678. struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
  679. struct nlattr *nest;
  680. bool hw_stats_used;
  681. int err;
  682. int i;
  683. if (nla_put_u32(skb, NHA_HW_STATS_ENABLE, nhg->hw_stats))
  684. goto err_out;
  685. if (op_flags & NHA_OP_FLAG_DUMP_HW_STATS &&
  686. nhg->hw_stats) {
  687. err = nh_grp_hw_stats_update(nh, &hw_stats_used);
  688. if (err)
  689. goto out;
  690. if (nla_put_u32(skb, NHA_HW_STATS_USED, hw_stats_used))
  691. goto err_out;
  692. }
  693. nest = nla_nest_start(skb, NHA_GROUP_STATS);
  694. if (!nest)
  695. goto err_out;
  696. for (i = 0; i < nhg->num_nh; i++)
  697. if (nla_put_nh_group_stats_entry(skb, &nhg->nh_entries[i],
  698. op_flags))
  699. goto cancel_out;
  700. nla_nest_end(skb, nest);
  701. return 0;
  702. cancel_out:
  703. nla_nest_cancel(skb, nest);
  704. err_out:
  705. err = -EMSGSIZE;
  706. out:
  707. return err;
  708. }
  709. static int nla_put_nh_group(struct sk_buff *skb, struct nexthop *nh,
  710. u32 op_flags, u32 *resp_op_flags)
  711. {
  712. struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
  713. struct nexthop_grp *p;
  714. size_t len = nhg->num_nh * sizeof(*p);
  715. struct nlattr *nla;
  716. u16 group_type = 0;
  717. u16 weight;
  718. int i;
  719. *resp_op_flags |= NHA_OP_FLAG_RESP_GRP_RESVD_0;
  720. if (nhg->hash_threshold)
  721. group_type = NEXTHOP_GRP_TYPE_MPATH;
  722. else if (nhg->resilient)
  723. group_type = NEXTHOP_GRP_TYPE_RES;
  724. if (nla_put_u16(skb, NHA_GROUP_TYPE, group_type))
  725. goto nla_put_failure;
  726. nla = nla_reserve(skb, NHA_GROUP, len);
  727. if (!nla)
  728. goto nla_put_failure;
  729. p = nla_data(nla);
  730. for (i = 0; i < nhg->num_nh; ++i) {
  731. weight = nhg->nh_entries[i].weight - 1;
  732. *p++ = (struct nexthop_grp) {
  733. .id = nhg->nh_entries[i].nh->id,
  734. .weight = weight,
  735. .weight_high = weight >> 8,
  736. };
  737. }
  738. if (nhg->resilient && nla_put_nh_group_res(skb, nhg))
  739. goto nla_put_failure;
  740. if (op_flags & NHA_OP_FLAG_DUMP_STATS &&
  741. (nla_put_u32(skb, NHA_HW_STATS_ENABLE, nhg->hw_stats) ||
  742. nla_put_nh_group_stats(skb, nh, op_flags)))
  743. goto nla_put_failure;
  744. return 0;
  745. nla_put_failure:
  746. return -EMSGSIZE;
  747. }
  748. static int nh_fill_node(struct sk_buff *skb, struct nexthop *nh,
  749. int event, u32 portid, u32 seq, unsigned int nlflags,
  750. u32 op_flags)
  751. {
  752. struct fib6_nh *fib6_nh;
  753. struct fib_nh *fib_nh;
  754. struct nlmsghdr *nlh;
  755. struct nh_info *nhi;
  756. struct nhmsg *nhm;
  757. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nhm), nlflags);
  758. if (!nlh)
  759. return -EMSGSIZE;
  760. nhm = nlmsg_data(nlh);
  761. nhm->nh_family = AF_UNSPEC;
  762. nhm->nh_flags = nh->nh_flags;
  763. nhm->nh_protocol = nh->protocol;
  764. nhm->nh_scope = 0;
  765. nhm->resvd = 0;
  766. if (nla_put_u32(skb, NHA_ID, nh->id))
  767. goto nla_put_failure;
  768. if (nh->is_group) {
  769. struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
  770. u32 resp_op_flags = 0;
  771. if (nhg->fdb_nh && nla_put_flag(skb, NHA_FDB))
  772. goto nla_put_failure;
  773. if (nla_put_nh_group(skb, nh, op_flags, &resp_op_flags) ||
  774. nla_put_u32(skb, NHA_OP_FLAGS, resp_op_flags))
  775. goto nla_put_failure;
  776. goto out;
  777. }
  778. nhi = rtnl_dereference(nh->nh_info);
  779. nhm->nh_family = nhi->family;
  780. if (nhi->reject_nh) {
  781. if (nla_put_flag(skb, NHA_BLACKHOLE))
  782. goto nla_put_failure;
  783. goto out;
  784. } else if (nhi->fdb_nh) {
  785. if (nla_put_flag(skb, NHA_FDB))
  786. goto nla_put_failure;
  787. } else {
  788. const struct net_device *dev;
  789. dev = nhi->fib_nhc.nhc_dev;
  790. if (dev && nla_put_u32(skb, NHA_OIF, dev->ifindex))
  791. goto nla_put_failure;
  792. }
  793. nhm->nh_scope = nhi->fib_nhc.nhc_scope;
  794. switch (nhi->family) {
  795. case AF_INET:
  796. fib_nh = &nhi->fib_nh;
  797. if (fib_nh->fib_nh_gw_family &&
  798. nla_put_be32(skb, NHA_GATEWAY, fib_nh->fib_nh_gw4))
  799. goto nla_put_failure;
  800. break;
  801. case AF_INET6:
  802. fib6_nh = &nhi->fib6_nh;
  803. if (fib6_nh->fib_nh_gw_family &&
  804. nla_put_in6_addr(skb, NHA_GATEWAY, &fib6_nh->fib_nh_gw6))
  805. goto nla_put_failure;
  806. break;
  807. }
  808. if (nhi->fib_nhc.nhc_lwtstate &&
  809. lwtunnel_fill_encap(skb, nhi->fib_nhc.nhc_lwtstate,
  810. NHA_ENCAP, NHA_ENCAP_TYPE) < 0)
  811. goto nla_put_failure;
  812. out:
  813. nlmsg_end(skb, nlh);
  814. return 0;
  815. nla_put_failure:
  816. nlmsg_cancel(skb, nlh);
  817. return -EMSGSIZE;
  818. }
  819. static size_t nh_nlmsg_size_grp_res(struct nh_group *nhg)
  820. {
  821. return nla_total_size(0) + /* NHA_RES_GROUP */
  822. nla_total_size(2) + /* NHA_RES_GROUP_BUCKETS */
  823. nla_total_size(4) + /* NHA_RES_GROUP_IDLE_TIMER */
  824. nla_total_size(4) + /* NHA_RES_GROUP_UNBALANCED_TIMER */
  825. nla_total_size_64bit(8);/* NHA_RES_GROUP_UNBALANCED_TIME */
  826. }
  827. static size_t nh_nlmsg_size_grp(struct nexthop *nh)
  828. {
  829. struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
  830. size_t sz = sizeof(struct nexthop_grp) * nhg->num_nh;
  831. size_t tot = nla_total_size(sz) +
  832. nla_total_size(2); /* NHA_GROUP_TYPE */
  833. if (nhg->resilient)
  834. tot += nh_nlmsg_size_grp_res(nhg);
  835. return tot;
  836. }
  837. static size_t nh_nlmsg_size_single(struct nexthop *nh)
  838. {
  839. struct nh_info *nhi = rtnl_dereference(nh->nh_info);
  840. size_t sz;
  841. /* covers NHA_BLACKHOLE since NHA_OIF and BLACKHOLE
  842. * are mutually exclusive
  843. */
  844. sz = nla_total_size(4); /* NHA_OIF */
  845. switch (nhi->family) {
  846. case AF_INET:
  847. if (nhi->fib_nh.fib_nh_gw_family)
  848. sz += nla_total_size(4); /* NHA_GATEWAY */
  849. break;
  850. case AF_INET6:
  851. /* NHA_GATEWAY */
  852. if (nhi->fib6_nh.fib_nh_gw_family)
  853. sz += nla_total_size(sizeof(const struct in6_addr));
  854. break;
  855. }
  856. if (nhi->fib_nhc.nhc_lwtstate) {
  857. sz += lwtunnel_get_encap_size(nhi->fib_nhc.nhc_lwtstate);
  858. sz += nla_total_size(2); /* NHA_ENCAP_TYPE */
  859. }
  860. return sz;
  861. }
  862. static size_t nh_nlmsg_size(struct nexthop *nh)
  863. {
  864. size_t sz = NLMSG_ALIGN(sizeof(struct nhmsg));
  865. sz += nla_total_size(4); /* NHA_ID */
  866. if (nh->is_group)
  867. sz += nh_nlmsg_size_grp(nh) +
  868. nla_total_size(4) + /* NHA_OP_FLAGS */
  869. 0;
  870. else
  871. sz += nh_nlmsg_size_single(nh);
  872. return sz;
  873. }
  874. static void nexthop_notify(int event, struct nexthop *nh, struct nl_info *info)
  875. {
  876. unsigned int nlflags = info->nlh ? info->nlh->nlmsg_flags : 0;
  877. u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
  878. struct sk_buff *skb;
  879. int err = -ENOBUFS;
  880. skb = nlmsg_new(nh_nlmsg_size(nh), gfp_any());
  881. if (!skb)
  882. goto errout;
  883. err = nh_fill_node(skb, nh, event, info->portid, seq, nlflags, 0);
  884. if (err < 0) {
  885. /* -EMSGSIZE implies BUG in nh_nlmsg_size() */
  886. WARN_ON(err == -EMSGSIZE);
  887. kfree_skb(skb);
  888. goto errout;
  889. }
  890. rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_NEXTHOP,
  891. info->nlh, gfp_any());
  892. return;
  893. errout:
  894. rtnl_set_sk_err(info->nl_net, RTNLGRP_NEXTHOP, err);
  895. }
  896. static unsigned long nh_res_bucket_used_time(const struct nh_res_bucket *bucket)
  897. {
  898. return (unsigned long)atomic_long_read(&bucket->used_time);
  899. }
  900. static unsigned long
  901. nh_res_bucket_idle_point(const struct nh_res_table *res_table,
  902. const struct nh_res_bucket *bucket,
  903. unsigned long now)
  904. {
  905. unsigned long time = nh_res_bucket_used_time(bucket);
  906. /* Bucket was not used since it was migrated. The idle time is now. */
  907. if (time == bucket->migrated_time)
  908. return now;
  909. return time + res_table->idle_timer;
  910. }
  911. static unsigned long
  912. nh_res_table_unb_point(const struct nh_res_table *res_table)
  913. {
  914. return res_table->unbalanced_since + res_table->unbalanced_timer;
  915. }
  916. static void nh_res_bucket_set_idle(const struct nh_res_table *res_table,
  917. struct nh_res_bucket *bucket)
  918. {
  919. unsigned long now = jiffies;
  920. atomic_long_set(&bucket->used_time, (long)now);
  921. bucket->migrated_time = now;
  922. }
  923. static void nh_res_bucket_set_busy(struct nh_res_bucket *bucket)
  924. {
  925. atomic_long_set(&bucket->used_time, (long)jiffies);
  926. }
  927. static clock_t nh_res_bucket_idle_time(const struct nh_res_bucket *bucket)
  928. {
  929. unsigned long used_time = nh_res_bucket_used_time(bucket);
  930. return jiffies_delta_to_clock_t(jiffies - used_time);
  931. }
  932. static int nh_fill_res_bucket(struct sk_buff *skb, struct nexthop *nh,
  933. struct nh_res_bucket *bucket, u16 bucket_index,
  934. int event, u32 portid, u32 seq,
  935. unsigned int nlflags,
  936. struct netlink_ext_ack *extack)
  937. {
  938. struct nh_grp_entry *nhge = nh_res_dereference(bucket->nh_entry);
  939. struct nlmsghdr *nlh;
  940. struct nlattr *nest;
  941. struct nhmsg *nhm;
  942. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nhm), nlflags);
  943. if (!nlh)
  944. return -EMSGSIZE;
  945. nhm = nlmsg_data(nlh);
  946. nhm->nh_family = AF_UNSPEC;
  947. nhm->nh_flags = bucket->nh_flags;
  948. nhm->nh_protocol = nh->protocol;
  949. nhm->nh_scope = 0;
  950. nhm->resvd = 0;
  951. if (nla_put_u32(skb, NHA_ID, nh->id))
  952. goto nla_put_failure;
  953. nest = nla_nest_start(skb, NHA_RES_BUCKET);
  954. if (!nest)
  955. goto nla_put_failure;
  956. if (nla_put_u16(skb, NHA_RES_BUCKET_INDEX, bucket_index) ||
  957. nla_put_u32(skb, NHA_RES_BUCKET_NH_ID, nhge->nh->id) ||
  958. nla_put_u64_64bit(skb, NHA_RES_BUCKET_IDLE_TIME,
  959. nh_res_bucket_idle_time(bucket),
  960. NHA_RES_BUCKET_PAD))
  961. goto nla_put_failure_nest;
  962. nla_nest_end(skb, nest);
  963. nlmsg_end(skb, nlh);
  964. return 0;
  965. nla_put_failure_nest:
  966. nla_nest_cancel(skb, nest);
  967. nla_put_failure:
  968. nlmsg_cancel(skb, nlh);
  969. return -EMSGSIZE;
  970. }
  971. static void nexthop_bucket_notify(struct nh_res_table *res_table,
  972. u16 bucket_index)
  973. {
  974. struct nh_res_bucket *bucket = &res_table->nh_buckets[bucket_index];
  975. struct nh_grp_entry *nhge = nh_res_dereference(bucket->nh_entry);
  976. struct nexthop *nh = nhge->nh_parent;
  977. struct sk_buff *skb;
  978. int err = -ENOBUFS;
  979. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  980. if (!skb)
  981. goto errout;
  982. err = nh_fill_res_bucket(skb, nh, bucket, bucket_index,
  983. RTM_NEWNEXTHOPBUCKET, 0, 0, NLM_F_REPLACE,
  984. NULL);
  985. if (err < 0) {
  986. kfree_skb(skb);
  987. goto errout;
  988. }
  989. rtnl_notify(skb, nh->net, 0, RTNLGRP_NEXTHOP, NULL, GFP_KERNEL);
  990. return;
  991. errout:
  992. rtnl_set_sk_err(nh->net, RTNLGRP_NEXTHOP, err);
  993. }
  994. static bool valid_group_nh(struct nexthop *nh, unsigned int npaths,
  995. bool *is_fdb, struct netlink_ext_ack *extack)
  996. {
  997. if (nh->is_group) {
  998. struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
  999. /* Nesting groups within groups is not supported. */
  1000. if (nhg->hash_threshold) {
  1001. NL_SET_ERR_MSG(extack,
  1002. "Hash-threshold group can not be a nexthop within a group");
  1003. return false;
  1004. }
  1005. if (nhg->resilient) {
  1006. NL_SET_ERR_MSG(extack,
  1007. "Resilient group can not be a nexthop within a group");
  1008. return false;
  1009. }
  1010. *is_fdb = nhg->fdb_nh;
  1011. } else {
  1012. struct nh_info *nhi = rtnl_dereference(nh->nh_info);
  1013. if (nhi->reject_nh && npaths > 1) {
  1014. NL_SET_ERR_MSG(extack,
  1015. "Blackhole nexthop can not be used in a group with more than 1 path");
  1016. return false;
  1017. }
  1018. *is_fdb = nhi->fdb_nh;
  1019. }
  1020. return true;
  1021. }
  1022. static int nh_check_attr_fdb_group(struct nexthop *nh, u8 *nh_family,
  1023. struct netlink_ext_ack *extack)
  1024. {
  1025. struct nh_info *nhi;
  1026. nhi = rtnl_dereference(nh->nh_info);
  1027. if (!nhi->fdb_nh) {
  1028. NL_SET_ERR_MSG(extack, "FDB nexthop group can only have fdb nexthops");
  1029. return -EINVAL;
  1030. }
  1031. if (*nh_family == AF_UNSPEC) {
  1032. *nh_family = nhi->family;
  1033. } else if (*nh_family != nhi->family) {
  1034. NL_SET_ERR_MSG(extack, "FDB nexthop group cannot have mixed family nexthops");
  1035. return -EINVAL;
  1036. }
  1037. return 0;
  1038. }
  1039. static int nh_check_attr_group(struct net *net,
  1040. struct nlattr *tb[], size_t tb_size,
  1041. u16 nh_grp_type, struct netlink_ext_ack *extack)
  1042. {
  1043. unsigned int len = nla_len(tb[NHA_GROUP]);
  1044. u8 nh_family = AF_UNSPEC;
  1045. struct nexthop_grp *nhg;
  1046. unsigned int i, j;
  1047. u8 nhg_fdb = 0;
  1048. if (!len || len & (sizeof(struct nexthop_grp) - 1)) {
  1049. NL_SET_ERR_MSG(extack,
  1050. "Invalid length for nexthop group attribute");
  1051. return -EINVAL;
  1052. }
  1053. /* convert len to number of nexthop ids */
  1054. len /= sizeof(*nhg);
  1055. nhg = nla_data(tb[NHA_GROUP]);
  1056. for (i = 0; i < len; ++i) {
  1057. if (nhg[i].resvd2) {
  1058. NL_SET_ERR_MSG(extack, "Reserved field in nexthop_grp must be 0");
  1059. return -EINVAL;
  1060. }
  1061. if (nexthop_grp_weight(&nhg[i]) == 0) {
  1062. /* 0xffff got passed in, representing weight of 0x10000,
  1063. * which is too heavy.
  1064. */
  1065. NL_SET_ERR_MSG(extack, "Invalid value for weight");
  1066. return -EINVAL;
  1067. }
  1068. for (j = i + 1; j < len; ++j) {
  1069. if (nhg[i].id == nhg[j].id) {
  1070. NL_SET_ERR_MSG(extack, "Nexthop id can not be used twice in a group");
  1071. return -EINVAL;
  1072. }
  1073. }
  1074. }
  1075. if (tb[NHA_FDB])
  1076. nhg_fdb = 1;
  1077. nhg = nla_data(tb[NHA_GROUP]);
  1078. for (i = 0; i < len; ++i) {
  1079. struct nexthop *nh;
  1080. bool is_fdb_nh;
  1081. nh = nexthop_find_by_id(net, nhg[i].id);
  1082. if (!nh) {
  1083. NL_SET_ERR_MSG(extack, "Invalid nexthop id");
  1084. return -EINVAL;
  1085. }
  1086. if (!valid_group_nh(nh, len, &is_fdb_nh, extack))
  1087. return -EINVAL;
  1088. if (nhg_fdb && nh_check_attr_fdb_group(nh, &nh_family, extack))
  1089. return -EINVAL;
  1090. if (!nhg_fdb && is_fdb_nh) {
  1091. NL_SET_ERR_MSG(extack, "Non FDB nexthop group cannot have fdb nexthops");
  1092. return -EINVAL;
  1093. }
  1094. }
  1095. for (i = NHA_GROUP_TYPE + 1; i < tb_size; ++i) {
  1096. if (!tb[i])
  1097. continue;
  1098. switch (i) {
  1099. case NHA_HW_STATS_ENABLE:
  1100. case NHA_FDB:
  1101. continue;
  1102. case NHA_RES_GROUP:
  1103. if (nh_grp_type == NEXTHOP_GRP_TYPE_RES)
  1104. continue;
  1105. break;
  1106. }
  1107. NL_SET_ERR_MSG(extack,
  1108. "No other attributes can be set in nexthop groups");
  1109. return -EINVAL;
  1110. }
  1111. return 0;
  1112. }
  1113. static bool ipv6_good_nh(const struct fib6_nh *nh)
  1114. {
  1115. int state = NUD_REACHABLE;
  1116. struct neighbour *n;
  1117. rcu_read_lock();
  1118. n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev, &nh->fib_nh_gw6);
  1119. if (n)
  1120. state = READ_ONCE(n->nud_state);
  1121. rcu_read_unlock();
  1122. return !!(state & NUD_VALID);
  1123. }
  1124. static bool ipv4_good_nh(const struct fib_nh *nh)
  1125. {
  1126. int state = NUD_REACHABLE;
  1127. struct neighbour *n;
  1128. rcu_read_lock();
  1129. n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev,
  1130. (__force u32)nh->fib_nh_gw4);
  1131. if (n)
  1132. state = READ_ONCE(n->nud_state);
  1133. rcu_read_unlock();
  1134. return !!(state & NUD_VALID);
  1135. }
  1136. static bool nexthop_is_good_nh(const struct nexthop *nh)
  1137. {
  1138. struct nh_info *nhi = rcu_dereference(nh->nh_info);
  1139. switch (nhi->family) {
  1140. case AF_INET:
  1141. return ipv4_good_nh(&nhi->fib_nh);
  1142. case AF_INET6:
  1143. return ipv6_good_nh(&nhi->fib6_nh);
  1144. }
  1145. return false;
  1146. }
  1147. static struct nexthop *nexthop_select_path_fdb(struct nh_group *nhg, int hash)
  1148. {
  1149. int i;
  1150. for (i = 0; i < nhg->num_nh; i++) {
  1151. struct nh_grp_entry *nhge = &nhg->nh_entries[i];
  1152. if (hash > atomic_read(&nhge->hthr.upper_bound))
  1153. continue;
  1154. nh_grp_entry_stats_inc(nhge);
  1155. return nhge->nh;
  1156. }
  1157. WARN_ON_ONCE(1);
  1158. return NULL;
  1159. }
  1160. static struct nexthop *nexthop_select_path_hthr(struct nh_group *nhg, int hash)
  1161. {
  1162. struct nh_grp_entry *nhge0 = NULL;
  1163. int i;
  1164. if (nhg->fdb_nh)
  1165. return nexthop_select_path_fdb(nhg, hash);
  1166. for (i = 0; i < nhg->num_nh; ++i) {
  1167. struct nh_grp_entry *nhge = &nhg->nh_entries[i];
  1168. /* nexthops always check if it is good and does
  1169. * not rely on a sysctl for this behavior
  1170. */
  1171. if (!nexthop_is_good_nh(nhge->nh))
  1172. continue;
  1173. if (!nhge0)
  1174. nhge0 = nhge;
  1175. if (hash > atomic_read(&nhge->hthr.upper_bound))
  1176. continue;
  1177. nh_grp_entry_stats_inc(nhge);
  1178. return nhge->nh;
  1179. }
  1180. if (!nhge0)
  1181. nhge0 = &nhg->nh_entries[0];
  1182. nh_grp_entry_stats_inc(nhge0);
  1183. return nhge0->nh;
  1184. }
  1185. static struct nexthop *nexthop_select_path_res(struct nh_group *nhg, int hash)
  1186. {
  1187. struct nh_res_table *res_table = rcu_dereference(nhg->res_table);
  1188. u16 bucket_index = hash % res_table->num_nh_buckets;
  1189. struct nh_res_bucket *bucket;
  1190. struct nh_grp_entry *nhge;
  1191. /* nexthop_select_path() is expected to return a non-NULL value, so
  1192. * skip protocol validation and just hand out whatever there is.
  1193. */
  1194. bucket = &res_table->nh_buckets[bucket_index];
  1195. nh_res_bucket_set_busy(bucket);
  1196. nhge = rcu_dereference(bucket->nh_entry);
  1197. nh_grp_entry_stats_inc(nhge);
  1198. return nhge->nh;
  1199. }
  1200. struct nexthop *nexthop_select_path(struct nexthop *nh, int hash)
  1201. {
  1202. struct nh_group *nhg;
  1203. if (!nh->is_group)
  1204. return nh;
  1205. nhg = rcu_dereference(nh->nh_grp);
  1206. if (nhg->hash_threshold)
  1207. return nexthop_select_path_hthr(nhg, hash);
  1208. else if (nhg->resilient)
  1209. return nexthop_select_path_res(nhg, hash);
  1210. /* Unreachable. */
  1211. return NULL;
  1212. }
  1213. EXPORT_SYMBOL_GPL(nexthop_select_path);
  1214. int nexthop_for_each_fib6_nh(struct nexthop *nh,
  1215. int (*cb)(struct fib6_nh *nh, void *arg),
  1216. void *arg)
  1217. {
  1218. struct nh_info *nhi;
  1219. int err;
  1220. if (nh->is_group) {
  1221. struct nh_group *nhg;
  1222. int i;
  1223. nhg = rcu_dereference_rtnl(nh->nh_grp);
  1224. for (i = 0; i < nhg->num_nh; i++) {
  1225. struct nh_grp_entry *nhge = &nhg->nh_entries[i];
  1226. nhi = rcu_dereference_rtnl(nhge->nh->nh_info);
  1227. err = cb(&nhi->fib6_nh, arg);
  1228. if (err)
  1229. return err;
  1230. }
  1231. } else {
  1232. nhi = rcu_dereference_rtnl(nh->nh_info);
  1233. err = cb(&nhi->fib6_nh, arg);
  1234. if (err)
  1235. return err;
  1236. }
  1237. return 0;
  1238. }
  1239. EXPORT_SYMBOL_GPL(nexthop_for_each_fib6_nh);
  1240. static int check_src_addr(const struct in6_addr *saddr,
  1241. struct netlink_ext_ack *extack)
  1242. {
  1243. if (!ipv6_addr_any(saddr)) {
  1244. NL_SET_ERR_MSG(extack, "IPv6 routes using source address can not use nexthop objects");
  1245. return -EINVAL;
  1246. }
  1247. return 0;
  1248. }
  1249. int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg,
  1250. struct netlink_ext_ack *extack)
  1251. {
  1252. struct nh_info *nhi;
  1253. bool is_fdb_nh;
  1254. /* fib6_src is unique to a fib6_info and limits the ability to cache
  1255. * routes in fib6_nh within a nexthop that is potentially shared
  1256. * across multiple fib entries. If the config wants to use source
  1257. * routing it can not use nexthop objects. mlxsw also does not allow
  1258. * fib6_src on routes.
  1259. */
  1260. if (cfg && check_src_addr(&cfg->fc_src, extack) < 0)
  1261. return -EINVAL;
  1262. if (nh->is_group) {
  1263. struct nh_group *nhg;
  1264. nhg = rtnl_dereference(nh->nh_grp);
  1265. if (nhg->has_v4)
  1266. goto no_v4_nh;
  1267. is_fdb_nh = nhg->fdb_nh;
  1268. } else {
  1269. nhi = rtnl_dereference(nh->nh_info);
  1270. if (nhi->family == AF_INET)
  1271. goto no_v4_nh;
  1272. is_fdb_nh = nhi->fdb_nh;
  1273. }
  1274. if (is_fdb_nh) {
  1275. NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop");
  1276. return -EINVAL;
  1277. }
  1278. return 0;
  1279. no_v4_nh:
  1280. NL_SET_ERR_MSG(extack, "IPv6 routes can not use an IPv4 nexthop");
  1281. return -EINVAL;
  1282. }
  1283. EXPORT_SYMBOL_GPL(fib6_check_nexthop);
  1284. /* if existing nexthop has ipv6 routes linked to it, need
  1285. * to verify this new spec works with ipv6
  1286. */
  1287. static int fib6_check_nh_list(struct nexthop *old, struct nexthop *new,
  1288. struct netlink_ext_ack *extack)
  1289. {
  1290. struct fib6_info *f6i;
  1291. if (list_empty(&old->f6i_list))
  1292. return 0;
  1293. list_for_each_entry(f6i, &old->f6i_list, nh_list) {
  1294. if (check_src_addr(&f6i->fib6_src.addr, extack) < 0)
  1295. return -EINVAL;
  1296. }
  1297. return fib6_check_nexthop(new, NULL, extack);
  1298. }
  1299. static int nexthop_check_scope(struct nh_info *nhi, u8 scope,
  1300. struct netlink_ext_ack *extack)
  1301. {
  1302. if (scope == RT_SCOPE_HOST && nhi->fib_nhc.nhc_gw_family) {
  1303. NL_SET_ERR_MSG(extack,
  1304. "Route with host scope can not have a gateway");
  1305. return -EINVAL;
  1306. }
  1307. if (nhi->fib_nhc.nhc_flags & RTNH_F_ONLINK && scope >= RT_SCOPE_LINK) {
  1308. NL_SET_ERR_MSG(extack, "Scope mismatch with nexthop");
  1309. return -EINVAL;
  1310. }
  1311. return 0;
  1312. }
  1313. /* Invoked by fib add code to verify nexthop by id is ok with
  1314. * config for prefix; parts of fib_check_nh not done when nexthop
  1315. * object is used.
  1316. */
  1317. int fib_check_nexthop(struct nexthop *nh, u8 scope,
  1318. struct netlink_ext_ack *extack)
  1319. {
  1320. struct nh_info *nhi;
  1321. int err = 0;
  1322. if (nh->is_group) {
  1323. struct nh_group *nhg;
  1324. nhg = rtnl_dereference(nh->nh_grp);
  1325. if (nhg->fdb_nh) {
  1326. NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop");
  1327. err = -EINVAL;
  1328. goto out;
  1329. }
  1330. if (scope == RT_SCOPE_HOST) {
  1331. NL_SET_ERR_MSG(extack, "Route with host scope can not have multiple nexthops");
  1332. err = -EINVAL;
  1333. goto out;
  1334. }
  1335. /* all nexthops in a group have the same scope */
  1336. nhi = rtnl_dereference(nhg->nh_entries[0].nh->nh_info);
  1337. err = nexthop_check_scope(nhi, scope, extack);
  1338. } else {
  1339. nhi = rtnl_dereference(nh->nh_info);
  1340. if (nhi->fdb_nh) {
  1341. NL_SET_ERR_MSG(extack, "Route cannot point to a fdb nexthop");
  1342. err = -EINVAL;
  1343. goto out;
  1344. }
  1345. err = nexthop_check_scope(nhi, scope, extack);
  1346. }
  1347. out:
  1348. return err;
  1349. }
  1350. static int fib_check_nh_list(struct nexthop *old, struct nexthop *new,
  1351. struct netlink_ext_ack *extack)
  1352. {
  1353. struct fib_info *fi;
  1354. list_for_each_entry(fi, &old->fi_list, nh_list) {
  1355. int err;
  1356. err = fib_check_nexthop(new, fi->fib_scope, extack);
  1357. if (err)
  1358. return err;
  1359. }
  1360. return 0;
  1361. }
  1362. static bool nh_res_nhge_is_balanced(const struct nh_grp_entry *nhge)
  1363. {
  1364. return nhge->res.count_buckets == nhge->res.wants_buckets;
  1365. }
  1366. static bool nh_res_nhge_is_ow(const struct nh_grp_entry *nhge)
  1367. {
  1368. return nhge->res.count_buckets > nhge->res.wants_buckets;
  1369. }
  1370. static bool nh_res_nhge_is_uw(const struct nh_grp_entry *nhge)
  1371. {
  1372. return nhge->res.count_buckets < nhge->res.wants_buckets;
  1373. }
  1374. static bool nh_res_table_is_balanced(const struct nh_res_table *res_table)
  1375. {
  1376. return list_empty(&res_table->uw_nh_entries);
  1377. }
  1378. static void nh_res_bucket_unset_nh(struct nh_res_bucket *bucket)
  1379. {
  1380. struct nh_grp_entry *nhge;
  1381. if (bucket->occupied) {
  1382. nhge = nh_res_dereference(bucket->nh_entry);
  1383. nhge->res.count_buckets--;
  1384. bucket->occupied = false;
  1385. }
  1386. }
  1387. static void nh_res_bucket_set_nh(struct nh_res_bucket *bucket,
  1388. struct nh_grp_entry *nhge)
  1389. {
  1390. nh_res_bucket_unset_nh(bucket);
  1391. bucket->occupied = true;
  1392. rcu_assign_pointer(bucket->nh_entry, nhge);
  1393. nhge->res.count_buckets++;
  1394. }
  1395. static bool nh_res_bucket_should_migrate(struct nh_res_table *res_table,
  1396. struct nh_res_bucket *bucket,
  1397. unsigned long *deadline, bool *force)
  1398. {
  1399. unsigned long now = jiffies;
  1400. struct nh_grp_entry *nhge;
  1401. unsigned long idle_point;
  1402. if (!bucket->occupied) {
  1403. /* The bucket is not occupied, its NHGE pointer is either
  1404. * NULL or obsolete. We _have to_ migrate: set force.
  1405. */
  1406. *force = true;
  1407. return true;
  1408. }
  1409. nhge = nh_res_dereference(bucket->nh_entry);
  1410. /* If the bucket is populated by an underweight or balanced
  1411. * nexthop, do not migrate.
  1412. */
  1413. if (!nh_res_nhge_is_ow(nhge))
  1414. return false;
  1415. /* At this point we know that the bucket is populated with an
  1416. * overweight nexthop. It needs to be migrated to a new nexthop if
  1417. * the idle timer of unbalanced timer expired.
  1418. */
  1419. idle_point = nh_res_bucket_idle_point(res_table, bucket, now);
  1420. if (time_after_eq(now, idle_point)) {
  1421. /* The bucket is idle. We _can_ migrate: unset force. */
  1422. *force = false;
  1423. return true;
  1424. }
  1425. /* Unbalanced timer of 0 means "never force". */
  1426. if (res_table->unbalanced_timer) {
  1427. unsigned long unb_point;
  1428. unb_point = nh_res_table_unb_point(res_table);
  1429. if (time_after(now, unb_point)) {
  1430. /* The bucket is not idle, but the unbalanced timer
  1431. * expired. We _can_ migrate, but set force anyway,
  1432. * so that drivers know to ignore activity reports
  1433. * from the HW.
  1434. */
  1435. *force = true;
  1436. return true;
  1437. }
  1438. nh_res_time_set_deadline(unb_point, deadline);
  1439. }
  1440. nh_res_time_set_deadline(idle_point, deadline);
  1441. return false;
  1442. }
  1443. static bool nh_res_bucket_migrate(struct nh_res_table *res_table,
  1444. u16 bucket_index, bool notify,
  1445. bool notify_nl, bool force)
  1446. {
  1447. struct nh_res_bucket *bucket = &res_table->nh_buckets[bucket_index];
  1448. struct nh_grp_entry *new_nhge;
  1449. struct netlink_ext_ack extack;
  1450. int err;
  1451. new_nhge = list_first_entry_or_null(&res_table->uw_nh_entries,
  1452. struct nh_grp_entry,
  1453. res.uw_nh_entry);
  1454. if (WARN_ON_ONCE(!new_nhge))
  1455. /* If this function is called, "bucket" is either not
  1456. * occupied, or it belongs to a next hop that is
  1457. * overweight. In either case, there ought to be a
  1458. * corresponding underweight next hop.
  1459. */
  1460. return false;
  1461. if (notify) {
  1462. struct nh_grp_entry *old_nhge;
  1463. old_nhge = nh_res_dereference(bucket->nh_entry);
  1464. err = call_nexthop_res_bucket_notifiers(res_table->net,
  1465. res_table->nhg_id,
  1466. bucket_index, force,
  1467. old_nhge->nh,
  1468. new_nhge->nh, &extack);
  1469. if (err) {
  1470. pr_err_ratelimited("%s\n", extack._msg);
  1471. if (!force)
  1472. return false;
  1473. /* It is not possible to veto a forced replacement, so
  1474. * just clear the hardware flags from the nexthop
  1475. * bucket to indicate to user space that this bucket is
  1476. * not correctly populated in hardware.
  1477. */
  1478. bucket->nh_flags &= ~(RTNH_F_OFFLOAD | RTNH_F_TRAP);
  1479. }
  1480. }
  1481. nh_res_bucket_set_nh(bucket, new_nhge);
  1482. nh_res_bucket_set_idle(res_table, bucket);
  1483. if (notify_nl)
  1484. nexthop_bucket_notify(res_table, bucket_index);
  1485. if (nh_res_nhge_is_balanced(new_nhge))
  1486. list_del(&new_nhge->res.uw_nh_entry);
  1487. return true;
  1488. }
  1489. #define NH_RES_UPKEEP_DW_MINIMUM_INTERVAL (HZ / 2)
  1490. static void nh_res_table_upkeep(struct nh_res_table *res_table,
  1491. bool notify, bool notify_nl)
  1492. {
  1493. unsigned long now = jiffies;
  1494. unsigned long deadline;
  1495. u16 i;
  1496. /* Deadline is the next time that upkeep should be run. It is the
  1497. * earliest time at which one of the buckets might be migrated.
  1498. * Start at the most pessimistic estimate: either unbalanced_timer
  1499. * from now, or if there is none, idle_timer from now. For each
  1500. * encountered time point, call nh_res_time_set_deadline() to
  1501. * refine the estimate.
  1502. */
  1503. if (res_table->unbalanced_timer)
  1504. deadline = now + res_table->unbalanced_timer;
  1505. else
  1506. deadline = now + res_table->idle_timer;
  1507. for (i = 0; i < res_table->num_nh_buckets; i++) {
  1508. struct nh_res_bucket *bucket = &res_table->nh_buckets[i];
  1509. bool force;
  1510. if (nh_res_bucket_should_migrate(res_table, bucket,
  1511. &deadline, &force)) {
  1512. if (!nh_res_bucket_migrate(res_table, i, notify,
  1513. notify_nl, force)) {
  1514. unsigned long idle_point;
  1515. /* A driver can override the migration
  1516. * decision if the HW reports that the
  1517. * bucket is actually not idle. Therefore
  1518. * remark the bucket as busy again and
  1519. * update the deadline.
  1520. */
  1521. nh_res_bucket_set_busy(bucket);
  1522. idle_point = nh_res_bucket_idle_point(res_table,
  1523. bucket,
  1524. now);
  1525. nh_res_time_set_deadline(idle_point, &deadline);
  1526. }
  1527. }
  1528. }
  1529. /* If the group is still unbalanced, schedule the next upkeep to
  1530. * either the deadline computed above, or the minimum deadline,
  1531. * whichever comes later.
  1532. */
  1533. if (!nh_res_table_is_balanced(res_table)) {
  1534. unsigned long now = jiffies;
  1535. unsigned long min_deadline;
  1536. min_deadline = now + NH_RES_UPKEEP_DW_MINIMUM_INTERVAL;
  1537. if (time_before(deadline, min_deadline))
  1538. deadline = min_deadline;
  1539. queue_delayed_work(system_power_efficient_wq,
  1540. &res_table->upkeep_dw, deadline - now);
  1541. }
  1542. }
  1543. static void nh_res_table_upkeep_dw(struct work_struct *work)
  1544. {
  1545. struct delayed_work *dw = to_delayed_work(work);
  1546. struct nh_res_table *res_table;
  1547. res_table = container_of(dw, struct nh_res_table, upkeep_dw);
  1548. nh_res_table_upkeep(res_table, true, true);
  1549. }
  1550. static void nh_res_table_cancel_upkeep(struct nh_res_table *res_table)
  1551. {
  1552. cancel_delayed_work_sync(&res_table->upkeep_dw);
  1553. }
  1554. static void nh_res_group_rebalance(struct nh_group *nhg,
  1555. struct nh_res_table *res_table)
  1556. {
  1557. u16 prev_upper_bound = 0;
  1558. u32 total = 0;
  1559. u32 w = 0;
  1560. int i;
  1561. INIT_LIST_HEAD(&res_table->uw_nh_entries);
  1562. for (i = 0; i < nhg->num_nh; ++i)
  1563. total += nhg->nh_entries[i].weight;
  1564. for (i = 0; i < nhg->num_nh; ++i) {
  1565. struct nh_grp_entry *nhge = &nhg->nh_entries[i];
  1566. u16 upper_bound;
  1567. u64 btw;
  1568. w += nhge->weight;
  1569. btw = ((u64)res_table->num_nh_buckets) * w;
  1570. upper_bound = DIV_ROUND_CLOSEST_ULL(btw, total);
  1571. nhge->res.wants_buckets = upper_bound - prev_upper_bound;
  1572. prev_upper_bound = upper_bound;
  1573. if (nh_res_nhge_is_uw(nhge)) {
  1574. if (list_empty(&res_table->uw_nh_entries))
  1575. res_table->unbalanced_since = jiffies;
  1576. list_add(&nhge->res.uw_nh_entry,
  1577. &res_table->uw_nh_entries);
  1578. }
  1579. }
  1580. }
  1581. /* Migrate buckets in res_table so that they reference NHGE's from NHG with
  1582. * the right NH ID. Set those buckets that do not have a corresponding NHGE
  1583. * entry in NHG as not occupied.
  1584. */
  1585. static void nh_res_table_migrate_buckets(struct nh_res_table *res_table,
  1586. struct nh_group *nhg)
  1587. {
  1588. u16 i;
  1589. for (i = 0; i < res_table->num_nh_buckets; i++) {
  1590. struct nh_res_bucket *bucket = &res_table->nh_buckets[i];
  1591. u32 id = rtnl_dereference(bucket->nh_entry)->nh->id;
  1592. bool found = false;
  1593. int j;
  1594. for (j = 0; j < nhg->num_nh; j++) {
  1595. struct nh_grp_entry *nhge = &nhg->nh_entries[j];
  1596. if (nhge->nh->id == id) {
  1597. nh_res_bucket_set_nh(bucket, nhge);
  1598. found = true;
  1599. break;
  1600. }
  1601. }
  1602. if (!found)
  1603. nh_res_bucket_unset_nh(bucket);
  1604. }
  1605. }
  1606. static void replace_nexthop_grp_res(struct nh_group *oldg,
  1607. struct nh_group *newg)
  1608. {
  1609. /* For NH group replacement, the new NHG might only have a stub
  1610. * hash table with 0 buckets, because the number of buckets was not
  1611. * specified. For NH removal, oldg and newg both reference the same
  1612. * res_table. So in any case, in the following, we want to work
  1613. * with oldg->res_table.
  1614. */
  1615. struct nh_res_table *old_res_table = rtnl_dereference(oldg->res_table);
  1616. unsigned long prev_unbalanced_since = old_res_table->unbalanced_since;
  1617. bool prev_has_uw = !list_empty(&old_res_table->uw_nh_entries);
  1618. nh_res_table_cancel_upkeep(old_res_table);
  1619. nh_res_table_migrate_buckets(old_res_table, newg);
  1620. nh_res_group_rebalance(newg, old_res_table);
  1621. if (prev_has_uw && !list_empty(&old_res_table->uw_nh_entries))
  1622. old_res_table->unbalanced_since = prev_unbalanced_since;
  1623. nh_res_table_upkeep(old_res_table, true, false);
  1624. }
  1625. static void nh_hthr_group_rebalance(struct nh_group *nhg)
  1626. {
  1627. u32 total = 0;
  1628. u32 w = 0;
  1629. int i;
  1630. for (i = 0; i < nhg->num_nh; ++i)
  1631. total += nhg->nh_entries[i].weight;
  1632. for (i = 0; i < nhg->num_nh; ++i) {
  1633. struct nh_grp_entry *nhge = &nhg->nh_entries[i];
  1634. u32 upper_bound;
  1635. w += nhge->weight;
  1636. upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31, total) - 1;
  1637. atomic_set(&nhge->hthr.upper_bound, upper_bound);
  1638. }
  1639. }
  1640. static void remove_nh_grp_entry(struct net *net, struct nh_grp_entry *nhge,
  1641. struct nl_info *nlinfo)
  1642. {
  1643. struct nh_grp_entry *nhges, *new_nhges;
  1644. struct nexthop *nhp = nhge->nh_parent;
  1645. struct netlink_ext_ack extack;
  1646. struct nexthop *nh = nhge->nh;
  1647. struct nh_group *nhg, *newg;
  1648. int i, j, err;
  1649. WARN_ON(!nh);
  1650. nhg = rtnl_dereference(nhp->nh_grp);
  1651. newg = nhg->spare;
  1652. /* last entry, keep it visible and remove the parent */
  1653. if (nhg->num_nh == 1) {
  1654. remove_nexthop(net, nhp, nlinfo);
  1655. return;
  1656. }
  1657. newg->has_v4 = false;
  1658. newg->is_multipath = nhg->is_multipath;
  1659. newg->hash_threshold = nhg->hash_threshold;
  1660. newg->resilient = nhg->resilient;
  1661. newg->fdb_nh = nhg->fdb_nh;
  1662. newg->num_nh = nhg->num_nh;
  1663. /* copy old entries to new except the one getting removed */
  1664. nhges = nhg->nh_entries;
  1665. new_nhges = newg->nh_entries;
  1666. for (i = 0, j = 0; i < nhg->num_nh; ++i) {
  1667. struct nh_info *nhi;
  1668. /* current nexthop getting removed */
  1669. if (nhg->nh_entries[i].nh == nh) {
  1670. newg->num_nh--;
  1671. continue;
  1672. }
  1673. nhi = rtnl_dereference(nhges[i].nh->nh_info);
  1674. if (nhi->family == AF_INET)
  1675. newg->has_v4 = true;
  1676. list_del(&nhges[i].nh_list);
  1677. new_nhges[j].stats = nhges[i].stats;
  1678. new_nhges[j].nh_parent = nhges[i].nh_parent;
  1679. new_nhges[j].nh = nhges[i].nh;
  1680. new_nhges[j].weight = nhges[i].weight;
  1681. list_add(&new_nhges[j].nh_list, &new_nhges[j].nh->grp_list);
  1682. j++;
  1683. }
  1684. if (newg->hash_threshold)
  1685. nh_hthr_group_rebalance(newg);
  1686. else if (newg->resilient)
  1687. replace_nexthop_grp_res(nhg, newg);
  1688. rcu_assign_pointer(nhp->nh_grp, newg);
  1689. list_del(&nhge->nh_list);
  1690. free_percpu(nhge->stats);
  1691. nexthop_put(nhge->nh);
  1692. /* Removal of a NH from a resilient group is notified through
  1693. * bucket notifications.
  1694. */
  1695. if (newg->hash_threshold) {
  1696. err = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, nhp,
  1697. &extack);
  1698. if (err)
  1699. pr_err("%s\n", extack._msg);
  1700. }
  1701. if (nlinfo)
  1702. nexthop_notify(RTM_NEWNEXTHOP, nhp, nlinfo);
  1703. }
  1704. static void remove_nexthop_from_groups(struct net *net, struct nexthop *nh,
  1705. struct nl_info *nlinfo)
  1706. {
  1707. struct nh_grp_entry *nhge, *tmp;
  1708. list_for_each_entry_safe(nhge, tmp, &nh->grp_list, nh_list)
  1709. remove_nh_grp_entry(net, nhge, nlinfo);
  1710. /* make sure all see the newly published array before releasing rtnl */
  1711. synchronize_net();
  1712. }
  1713. static void remove_nexthop_group(struct nexthop *nh, struct nl_info *nlinfo)
  1714. {
  1715. struct nh_group *nhg = rcu_dereference_rtnl(nh->nh_grp);
  1716. struct nh_res_table *res_table;
  1717. int i, num_nh = nhg->num_nh;
  1718. for (i = 0; i < num_nh; ++i) {
  1719. struct nh_grp_entry *nhge = &nhg->nh_entries[i];
  1720. if (WARN_ON(!nhge->nh))
  1721. continue;
  1722. list_del_init(&nhge->nh_list);
  1723. }
  1724. if (nhg->resilient) {
  1725. res_table = rtnl_dereference(nhg->res_table);
  1726. nh_res_table_cancel_upkeep(res_table);
  1727. }
  1728. }
  1729. /* not called for nexthop replace */
  1730. static void __remove_nexthop_fib(struct net *net, struct nexthop *nh)
  1731. {
  1732. struct fib6_info *f6i, *tmp;
  1733. bool do_flush = false;
  1734. struct fib_info *fi;
  1735. list_for_each_entry(fi, &nh->fi_list, nh_list) {
  1736. fi->fib_flags |= RTNH_F_DEAD;
  1737. do_flush = true;
  1738. }
  1739. if (do_flush)
  1740. fib_flush(net);
  1741. /* ip6_del_rt removes the entry from this list hence the _safe */
  1742. list_for_each_entry_safe(f6i, tmp, &nh->f6i_list, nh_list) {
  1743. /* __ip6_del_rt does a release, so do a hold here */
  1744. fib6_info_hold(f6i);
  1745. ipv6_stub->ip6_del_rt(net, f6i,
  1746. !READ_ONCE(net->ipv4.sysctl_nexthop_compat_mode));
  1747. }
  1748. }
  1749. static void __remove_nexthop(struct net *net, struct nexthop *nh,
  1750. struct nl_info *nlinfo)
  1751. {
  1752. __remove_nexthop_fib(net, nh);
  1753. if (nh->is_group) {
  1754. remove_nexthop_group(nh, nlinfo);
  1755. } else {
  1756. struct nh_info *nhi;
  1757. nhi = rtnl_dereference(nh->nh_info);
  1758. if (nhi->fib_nhc.nhc_dev)
  1759. hlist_del(&nhi->dev_hash);
  1760. remove_nexthop_from_groups(net, nh, nlinfo);
  1761. }
  1762. }
  1763. static void remove_nexthop(struct net *net, struct nexthop *nh,
  1764. struct nl_info *nlinfo)
  1765. {
  1766. call_nexthop_notifiers(net, NEXTHOP_EVENT_DEL, nh, NULL);
  1767. /* remove from the tree */
  1768. rb_erase(&nh->rb_node, &net->nexthop.rb_root);
  1769. if (nlinfo)
  1770. nexthop_notify(RTM_DELNEXTHOP, nh, nlinfo);
  1771. __remove_nexthop(net, nh, nlinfo);
  1772. nh_base_seq_inc(net);
  1773. nexthop_put(nh);
  1774. }
  1775. /* if any FIB entries reference this nexthop, any dst entries
  1776. * need to be regenerated
  1777. */
  1778. static void nh_rt_cache_flush(struct net *net, struct nexthop *nh,
  1779. struct nexthop *replaced_nh)
  1780. {
  1781. struct fib6_info *f6i;
  1782. struct nh_group *nhg;
  1783. int i;
  1784. if (!list_empty(&nh->fi_list))
  1785. rt_cache_flush(net);
  1786. list_for_each_entry(f6i, &nh->f6i_list, nh_list)
  1787. ipv6_stub->fib6_update_sernum(net, f6i);
  1788. /* if an IPv6 group was replaced, we have to release all old
  1789. * dsts to make sure all refcounts are released
  1790. */
  1791. if (!replaced_nh->is_group)
  1792. return;
  1793. nhg = rtnl_dereference(replaced_nh->nh_grp);
  1794. for (i = 0; i < nhg->num_nh; i++) {
  1795. struct nh_grp_entry *nhge = &nhg->nh_entries[i];
  1796. struct nh_info *nhi = rtnl_dereference(nhge->nh->nh_info);
  1797. if (nhi->family == AF_INET6)
  1798. ipv6_stub->fib6_nh_release_dsts(&nhi->fib6_nh);
  1799. }
  1800. }
  1801. static int replace_nexthop_grp(struct net *net, struct nexthop *old,
  1802. struct nexthop *new, const struct nh_config *cfg,
  1803. struct netlink_ext_ack *extack)
  1804. {
  1805. struct nh_res_table *tmp_table = NULL;
  1806. struct nh_res_table *new_res_table;
  1807. struct nh_res_table *old_res_table;
  1808. struct nh_group *oldg, *newg;
  1809. int i, err;
  1810. if (!new->is_group) {
  1811. NL_SET_ERR_MSG(extack, "Can not replace a nexthop group with a nexthop.");
  1812. return -EINVAL;
  1813. }
  1814. oldg = rtnl_dereference(old->nh_grp);
  1815. newg = rtnl_dereference(new->nh_grp);
  1816. if (newg->hash_threshold != oldg->hash_threshold) {
  1817. NL_SET_ERR_MSG(extack, "Can not replace a nexthop group with one of a different type.");
  1818. return -EINVAL;
  1819. }
  1820. if (newg->hash_threshold) {
  1821. err = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, new,
  1822. extack);
  1823. if (err)
  1824. return err;
  1825. } else if (newg->resilient) {
  1826. new_res_table = rtnl_dereference(newg->res_table);
  1827. old_res_table = rtnl_dereference(oldg->res_table);
  1828. /* Accept if num_nh_buckets was not given, but if it was
  1829. * given, demand that the value be correct.
  1830. */
  1831. if (cfg->nh_grp_res_has_num_buckets &&
  1832. cfg->nh_grp_res_num_buckets !=
  1833. old_res_table->num_nh_buckets) {
  1834. NL_SET_ERR_MSG(extack, "Can not change number of buckets of a resilient nexthop group.");
  1835. return -EINVAL;
  1836. }
  1837. /* Emit a pre-replace notification so that listeners could veto
  1838. * a potentially unsupported configuration. Otherwise,
  1839. * individual bucket replacement notifications would need to be
  1840. * vetoed, which is something that should only happen if the
  1841. * bucket is currently active.
  1842. */
  1843. err = call_nexthop_res_table_notifiers(net, new, extack);
  1844. if (err)
  1845. return err;
  1846. if (cfg->nh_grp_res_has_idle_timer)
  1847. old_res_table->idle_timer = cfg->nh_grp_res_idle_timer;
  1848. if (cfg->nh_grp_res_has_unbalanced_timer)
  1849. old_res_table->unbalanced_timer =
  1850. cfg->nh_grp_res_unbalanced_timer;
  1851. replace_nexthop_grp_res(oldg, newg);
  1852. tmp_table = new_res_table;
  1853. rcu_assign_pointer(newg->res_table, old_res_table);
  1854. rcu_assign_pointer(newg->spare->res_table, old_res_table);
  1855. }
  1856. /* update parents - used by nexthop code for cleanup */
  1857. for (i = 0; i < newg->num_nh; i++)
  1858. newg->nh_entries[i].nh_parent = old;
  1859. rcu_assign_pointer(old->nh_grp, newg);
  1860. /* Make sure concurrent readers are not using 'oldg' anymore. */
  1861. synchronize_net();
  1862. if (newg->resilient) {
  1863. rcu_assign_pointer(oldg->res_table, tmp_table);
  1864. rcu_assign_pointer(oldg->spare->res_table, tmp_table);
  1865. }
  1866. for (i = 0; i < oldg->num_nh; i++)
  1867. oldg->nh_entries[i].nh_parent = new;
  1868. rcu_assign_pointer(new->nh_grp, oldg);
  1869. return 0;
  1870. }
  1871. static void nh_group_v4_update(struct nh_group *nhg)
  1872. {
  1873. struct nh_grp_entry *nhges;
  1874. bool has_v4 = false;
  1875. int i;
  1876. nhges = nhg->nh_entries;
  1877. for (i = 0; i < nhg->num_nh; i++) {
  1878. struct nh_info *nhi;
  1879. nhi = rtnl_dereference(nhges[i].nh->nh_info);
  1880. if (nhi->family == AF_INET)
  1881. has_v4 = true;
  1882. }
  1883. nhg->has_v4 = has_v4;
  1884. }
  1885. static int replace_nexthop_single_notify_res(struct net *net,
  1886. struct nh_res_table *res_table,
  1887. struct nexthop *old,
  1888. struct nh_info *oldi,
  1889. struct nh_info *newi,
  1890. struct netlink_ext_ack *extack)
  1891. {
  1892. u32 nhg_id = res_table->nhg_id;
  1893. int err;
  1894. u16 i;
  1895. for (i = 0; i < res_table->num_nh_buckets; i++) {
  1896. struct nh_res_bucket *bucket = &res_table->nh_buckets[i];
  1897. struct nh_grp_entry *nhge;
  1898. nhge = rtnl_dereference(bucket->nh_entry);
  1899. if (nhge->nh == old) {
  1900. err = __call_nexthop_res_bucket_notifiers(net, nhg_id,
  1901. i, true,
  1902. oldi, newi,
  1903. extack);
  1904. if (err)
  1905. goto err_notify;
  1906. }
  1907. }
  1908. return 0;
  1909. err_notify:
  1910. while (i-- > 0) {
  1911. struct nh_res_bucket *bucket = &res_table->nh_buckets[i];
  1912. struct nh_grp_entry *nhge;
  1913. nhge = rtnl_dereference(bucket->nh_entry);
  1914. if (nhge->nh == old)
  1915. __call_nexthop_res_bucket_notifiers(net, nhg_id, i,
  1916. true, newi, oldi,
  1917. extack);
  1918. }
  1919. return err;
  1920. }
  1921. static int replace_nexthop_single_notify(struct net *net,
  1922. struct nexthop *group_nh,
  1923. struct nexthop *old,
  1924. struct nh_info *oldi,
  1925. struct nh_info *newi,
  1926. struct netlink_ext_ack *extack)
  1927. {
  1928. struct nh_group *nhg = rtnl_dereference(group_nh->nh_grp);
  1929. struct nh_res_table *res_table;
  1930. if (nhg->hash_threshold) {
  1931. return call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE,
  1932. group_nh, extack);
  1933. } else if (nhg->resilient) {
  1934. res_table = rtnl_dereference(nhg->res_table);
  1935. return replace_nexthop_single_notify_res(net, res_table,
  1936. old, oldi, newi,
  1937. extack);
  1938. }
  1939. return -EINVAL;
  1940. }
  1941. static int replace_nexthop_single(struct net *net, struct nexthop *old,
  1942. struct nexthop *new,
  1943. struct netlink_ext_ack *extack)
  1944. {
  1945. u8 old_protocol, old_nh_flags;
  1946. struct nh_info *oldi, *newi;
  1947. struct nh_grp_entry *nhge;
  1948. int err;
  1949. if (new->is_group) {
  1950. NL_SET_ERR_MSG(extack, "Can not replace a nexthop with a nexthop group.");
  1951. return -EINVAL;
  1952. }
  1953. if (!list_empty(&old->grp_list) &&
  1954. rtnl_dereference(new->nh_info)->fdb_nh !=
  1955. rtnl_dereference(old->nh_info)->fdb_nh) {
  1956. NL_SET_ERR_MSG(extack, "Cannot change nexthop FDB status while in a group");
  1957. return -EINVAL;
  1958. }
  1959. err = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, new, extack);
  1960. if (err)
  1961. return err;
  1962. /* Hardware flags were set on 'old' as 'new' is not in the red-black
  1963. * tree. Therefore, inherit the flags from 'old' to 'new'.
  1964. */
  1965. new->nh_flags |= old->nh_flags & (RTNH_F_OFFLOAD | RTNH_F_TRAP);
  1966. oldi = rtnl_dereference(old->nh_info);
  1967. newi = rtnl_dereference(new->nh_info);
  1968. newi->nh_parent = old;
  1969. oldi->nh_parent = new;
  1970. old_protocol = old->protocol;
  1971. old_nh_flags = old->nh_flags;
  1972. old->protocol = new->protocol;
  1973. old->nh_flags = new->nh_flags;
  1974. rcu_assign_pointer(old->nh_info, newi);
  1975. rcu_assign_pointer(new->nh_info, oldi);
  1976. /* Send a replace notification for all the groups using the nexthop. */
  1977. list_for_each_entry(nhge, &old->grp_list, nh_list) {
  1978. struct nexthop *nhp = nhge->nh_parent;
  1979. err = replace_nexthop_single_notify(net, nhp, old, oldi, newi,
  1980. extack);
  1981. if (err)
  1982. goto err_notify;
  1983. }
  1984. /* When replacing an IPv4 nexthop with an IPv6 nexthop, potentially
  1985. * update IPv4 indication in all the groups using the nexthop.
  1986. */
  1987. if (oldi->family == AF_INET && newi->family == AF_INET6) {
  1988. list_for_each_entry(nhge, &old->grp_list, nh_list) {
  1989. struct nexthop *nhp = nhge->nh_parent;
  1990. struct nh_group *nhg;
  1991. nhg = rtnl_dereference(nhp->nh_grp);
  1992. nh_group_v4_update(nhg);
  1993. }
  1994. }
  1995. return 0;
  1996. err_notify:
  1997. rcu_assign_pointer(new->nh_info, newi);
  1998. rcu_assign_pointer(old->nh_info, oldi);
  1999. old->nh_flags = old_nh_flags;
  2000. old->protocol = old_protocol;
  2001. oldi->nh_parent = old;
  2002. newi->nh_parent = new;
  2003. list_for_each_entry_continue_reverse(nhge, &old->grp_list, nh_list) {
  2004. struct nexthop *nhp = nhge->nh_parent;
  2005. replace_nexthop_single_notify(net, nhp, old, newi, oldi, NULL);
  2006. }
  2007. call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, old, extack);
  2008. return err;
  2009. }
  2010. static void __nexthop_replace_notify(struct net *net, struct nexthop *nh,
  2011. struct nl_info *info)
  2012. {
  2013. struct fib6_info *f6i;
  2014. if (!list_empty(&nh->fi_list)) {
  2015. struct fib_info *fi;
  2016. /* expectation is a few fib_info per nexthop and then
  2017. * a lot of routes per fib_info. So mark the fib_info
  2018. * and then walk the fib tables once
  2019. */
  2020. list_for_each_entry(fi, &nh->fi_list, nh_list)
  2021. fi->nh_updated = true;
  2022. fib_info_notify_update(net, info);
  2023. list_for_each_entry(fi, &nh->fi_list, nh_list)
  2024. fi->nh_updated = false;
  2025. }
  2026. list_for_each_entry(f6i, &nh->f6i_list, nh_list)
  2027. ipv6_stub->fib6_rt_update(net, f6i, info);
  2028. }
  2029. /* send RTM_NEWROUTE with REPLACE flag set for all FIB entries
  2030. * linked to this nexthop and for all groups that the nexthop
  2031. * is a member of
  2032. */
  2033. static void nexthop_replace_notify(struct net *net, struct nexthop *nh,
  2034. struct nl_info *info)
  2035. {
  2036. struct nh_grp_entry *nhge;
  2037. __nexthop_replace_notify(net, nh, info);
  2038. list_for_each_entry(nhge, &nh->grp_list, nh_list)
  2039. __nexthop_replace_notify(net, nhge->nh_parent, info);
  2040. }
  2041. static int replace_nexthop(struct net *net, struct nexthop *old,
  2042. struct nexthop *new, const struct nh_config *cfg,
  2043. struct netlink_ext_ack *extack)
  2044. {
  2045. bool new_is_reject = false;
  2046. struct nh_grp_entry *nhge;
  2047. int err;
  2048. /* check that existing FIB entries are ok with the
  2049. * new nexthop definition
  2050. */
  2051. err = fib_check_nh_list(old, new, extack);
  2052. if (err)
  2053. return err;
  2054. err = fib6_check_nh_list(old, new, extack);
  2055. if (err)
  2056. return err;
  2057. if (!new->is_group) {
  2058. struct nh_info *nhi = rtnl_dereference(new->nh_info);
  2059. new_is_reject = nhi->reject_nh;
  2060. }
  2061. list_for_each_entry(nhge, &old->grp_list, nh_list) {
  2062. /* if new nexthop is a blackhole, any groups using this
  2063. * nexthop cannot have more than 1 path
  2064. */
  2065. if (new_is_reject &&
  2066. nexthop_num_path(nhge->nh_parent) > 1) {
  2067. NL_SET_ERR_MSG(extack, "Blackhole nexthop can not be a member of a group with more than one path");
  2068. return -EINVAL;
  2069. }
  2070. err = fib_check_nh_list(nhge->nh_parent, new, extack);
  2071. if (err)
  2072. return err;
  2073. err = fib6_check_nh_list(nhge->nh_parent, new, extack);
  2074. if (err)
  2075. return err;
  2076. }
  2077. if (old->is_group)
  2078. err = replace_nexthop_grp(net, old, new, cfg, extack);
  2079. else
  2080. err = replace_nexthop_single(net, old, new, extack);
  2081. if (!err) {
  2082. nh_rt_cache_flush(net, old, new);
  2083. __remove_nexthop(net, new, NULL);
  2084. nexthop_put(new);
  2085. }
  2086. return err;
  2087. }
  2088. /* called with rtnl_lock held */
  2089. static int insert_nexthop(struct net *net, struct nexthop *new_nh,
  2090. struct nh_config *cfg, struct netlink_ext_ack *extack)
  2091. {
  2092. struct rb_node **pp, *parent = NULL, *next;
  2093. struct rb_root *root = &net->nexthop.rb_root;
  2094. bool replace = !!(cfg->nlflags & NLM_F_REPLACE);
  2095. bool create = !!(cfg->nlflags & NLM_F_CREATE);
  2096. u32 new_id = new_nh->id;
  2097. int replace_notify = 0;
  2098. int rc = -EEXIST;
  2099. pp = &root->rb_node;
  2100. while (1) {
  2101. struct nexthop *nh;
  2102. next = *pp;
  2103. if (!next)
  2104. break;
  2105. parent = next;
  2106. nh = rb_entry(parent, struct nexthop, rb_node);
  2107. if (new_id < nh->id) {
  2108. pp = &next->rb_left;
  2109. } else if (new_id > nh->id) {
  2110. pp = &next->rb_right;
  2111. } else if (replace) {
  2112. rc = replace_nexthop(net, nh, new_nh, cfg, extack);
  2113. if (!rc) {
  2114. new_nh = nh; /* send notification with old nh */
  2115. replace_notify = 1;
  2116. }
  2117. goto out;
  2118. } else {
  2119. /* id already exists and not a replace */
  2120. goto out;
  2121. }
  2122. }
  2123. if (replace && !create) {
  2124. NL_SET_ERR_MSG(extack, "Replace specified without create and no entry exists");
  2125. rc = -ENOENT;
  2126. goto out;
  2127. }
  2128. if (new_nh->is_group) {
  2129. struct nh_group *nhg = rtnl_dereference(new_nh->nh_grp);
  2130. struct nh_res_table *res_table;
  2131. if (nhg->resilient) {
  2132. res_table = rtnl_dereference(nhg->res_table);
  2133. /* Not passing the number of buckets is OK when
  2134. * replacing, but not when creating a new group.
  2135. */
  2136. if (!cfg->nh_grp_res_has_num_buckets) {
  2137. NL_SET_ERR_MSG(extack, "Number of buckets not specified for nexthop group insertion");
  2138. rc = -EINVAL;
  2139. goto out;
  2140. }
  2141. nh_res_group_rebalance(nhg, res_table);
  2142. /* Do not send bucket notifications, we do full
  2143. * notification below.
  2144. */
  2145. nh_res_table_upkeep(res_table, false, false);
  2146. }
  2147. }
  2148. rb_link_node_rcu(&new_nh->rb_node, parent, pp);
  2149. rb_insert_color(&new_nh->rb_node, root);
  2150. /* The initial insertion is a full notification for hash-threshold as
  2151. * well as resilient groups.
  2152. */
  2153. rc = call_nexthop_notifiers(net, NEXTHOP_EVENT_REPLACE, new_nh, extack);
  2154. if (rc)
  2155. rb_erase(&new_nh->rb_node, &net->nexthop.rb_root);
  2156. out:
  2157. if (!rc) {
  2158. nh_base_seq_inc(net);
  2159. nexthop_notify(RTM_NEWNEXTHOP, new_nh, &cfg->nlinfo);
  2160. if (replace_notify &&
  2161. READ_ONCE(net->ipv4.sysctl_nexthop_compat_mode))
  2162. nexthop_replace_notify(net, new_nh, &cfg->nlinfo);
  2163. }
  2164. return rc;
  2165. }
  2166. /* rtnl */
  2167. /* remove all nexthops tied to a device being deleted */
  2168. static void nexthop_flush_dev(struct net_device *dev, unsigned long event)
  2169. {
  2170. unsigned int hash = nh_dev_hashfn(dev->ifindex);
  2171. struct net *net = dev_net(dev);
  2172. struct hlist_head *head = &net->nexthop.devhash[hash];
  2173. struct hlist_node *n;
  2174. struct nh_info *nhi;
  2175. hlist_for_each_entry_safe(nhi, n, head, dev_hash) {
  2176. if (nhi->fib_nhc.nhc_dev != dev)
  2177. continue;
  2178. if (nhi->reject_nh &&
  2179. (event == NETDEV_DOWN || event == NETDEV_CHANGE))
  2180. continue;
  2181. remove_nexthop(net, nhi->nh_parent, NULL);
  2182. }
  2183. }
  2184. /* rtnl; called when net namespace is deleted */
  2185. static void flush_all_nexthops(struct net *net)
  2186. {
  2187. struct rb_root *root = &net->nexthop.rb_root;
  2188. struct rb_node *node;
  2189. struct nexthop *nh;
  2190. while ((node = rb_first(root))) {
  2191. nh = rb_entry(node, struct nexthop, rb_node);
  2192. remove_nexthop(net, nh, NULL);
  2193. cond_resched();
  2194. }
  2195. }
  2196. static struct nexthop *nexthop_create_group(struct net *net,
  2197. struct nh_config *cfg)
  2198. {
  2199. struct nlattr *grps_attr = cfg->nh_grp;
  2200. struct nexthop_grp *entry = nla_data(grps_attr);
  2201. u16 num_nh = nla_len(grps_attr) / sizeof(*entry);
  2202. struct nh_group *nhg;
  2203. struct nexthop *nh;
  2204. int err;
  2205. int i;
  2206. if (WARN_ON(!num_nh))
  2207. return ERR_PTR(-EINVAL);
  2208. nh = nexthop_alloc();
  2209. if (!nh)
  2210. return ERR_PTR(-ENOMEM);
  2211. nh->is_group = 1;
  2212. nhg = nexthop_grp_alloc(num_nh);
  2213. if (!nhg) {
  2214. kfree(nh);
  2215. return ERR_PTR(-ENOMEM);
  2216. }
  2217. /* spare group used for removals */
  2218. nhg->spare = nexthop_grp_alloc(num_nh);
  2219. if (!nhg->spare) {
  2220. kfree(nhg);
  2221. kfree(nh);
  2222. return ERR_PTR(-ENOMEM);
  2223. }
  2224. nhg->spare->spare = nhg;
  2225. for (i = 0; i < nhg->num_nh; ++i) {
  2226. struct nexthop *nhe;
  2227. struct nh_info *nhi;
  2228. nhe = nexthop_find_by_id(net, entry[i].id);
  2229. if (!nexthop_get(nhe)) {
  2230. err = -ENOENT;
  2231. goto out_no_nh;
  2232. }
  2233. nhi = rtnl_dereference(nhe->nh_info);
  2234. if (nhi->family == AF_INET)
  2235. nhg->has_v4 = true;
  2236. nhg->nh_entries[i].stats =
  2237. netdev_alloc_pcpu_stats(struct nh_grp_entry_stats);
  2238. if (!nhg->nh_entries[i].stats) {
  2239. err = -ENOMEM;
  2240. nexthop_put(nhe);
  2241. goto out_no_nh;
  2242. }
  2243. nhg->nh_entries[i].nh = nhe;
  2244. nhg->nh_entries[i].weight = nexthop_grp_weight(&entry[i]);
  2245. list_add(&nhg->nh_entries[i].nh_list, &nhe->grp_list);
  2246. nhg->nh_entries[i].nh_parent = nh;
  2247. }
  2248. if (cfg->nh_grp_type == NEXTHOP_GRP_TYPE_MPATH) {
  2249. nhg->hash_threshold = 1;
  2250. nhg->is_multipath = true;
  2251. } else if (cfg->nh_grp_type == NEXTHOP_GRP_TYPE_RES) {
  2252. struct nh_res_table *res_table;
  2253. res_table = nexthop_res_table_alloc(net, cfg->nh_id, cfg);
  2254. if (!res_table) {
  2255. err = -ENOMEM;
  2256. goto out_no_nh;
  2257. }
  2258. rcu_assign_pointer(nhg->spare->res_table, res_table);
  2259. rcu_assign_pointer(nhg->res_table, res_table);
  2260. nhg->resilient = true;
  2261. nhg->is_multipath = true;
  2262. }
  2263. WARN_ON_ONCE(nhg->hash_threshold + nhg->resilient != 1);
  2264. if (nhg->hash_threshold)
  2265. nh_hthr_group_rebalance(nhg);
  2266. if (cfg->nh_fdb)
  2267. nhg->fdb_nh = 1;
  2268. if (cfg->nh_hw_stats)
  2269. nhg->hw_stats = true;
  2270. rcu_assign_pointer(nh->nh_grp, nhg);
  2271. return nh;
  2272. out_no_nh:
  2273. for (i--; i >= 0; --i) {
  2274. list_del(&nhg->nh_entries[i].nh_list);
  2275. free_percpu(nhg->nh_entries[i].stats);
  2276. nexthop_put(nhg->nh_entries[i].nh);
  2277. }
  2278. kfree(nhg->spare);
  2279. kfree(nhg);
  2280. kfree(nh);
  2281. return ERR_PTR(err);
  2282. }
  2283. static int nh_create_ipv4(struct net *net, struct nexthop *nh,
  2284. struct nh_info *nhi, struct nh_config *cfg,
  2285. struct netlink_ext_ack *extack)
  2286. {
  2287. struct fib_nh *fib_nh = &nhi->fib_nh;
  2288. struct fib_config fib_cfg = {
  2289. .fc_oif = cfg->nh_ifindex,
  2290. .fc_gw4 = cfg->gw.ipv4,
  2291. .fc_gw_family = cfg->gw.ipv4 ? AF_INET : 0,
  2292. .fc_flags = cfg->nh_flags,
  2293. .fc_nlinfo = cfg->nlinfo,
  2294. .fc_encap = cfg->nh_encap,
  2295. .fc_encap_type = cfg->nh_encap_type,
  2296. };
  2297. u32 tb_id = (cfg->dev ? l3mdev_fib_table(cfg->dev) : RT_TABLE_MAIN);
  2298. int err;
  2299. err = fib_nh_init(net, fib_nh, &fib_cfg, 1, extack);
  2300. if (err) {
  2301. fib_nh_release(net, fib_nh);
  2302. goto out;
  2303. }
  2304. if (nhi->fdb_nh)
  2305. goto out;
  2306. /* sets nh_dev if successful */
  2307. err = fib_check_nh(net, fib_nh, tb_id, 0, extack);
  2308. if (!err) {
  2309. nh->nh_flags = fib_nh->fib_nh_flags;
  2310. fib_info_update_nhc_saddr(net, &fib_nh->nh_common,
  2311. !fib_nh->fib_nh_scope ? 0 : fib_nh->fib_nh_scope - 1);
  2312. } else {
  2313. fib_nh_release(net, fib_nh);
  2314. }
  2315. out:
  2316. return err;
  2317. }
  2318. static int nh_create_ipv6(struct net *net, struct nexthop *nh,
  2319. struct nh_info *nhi, struct nh_config *cfg,
  2320. struct netlink_ext_ack *extack)
  2321. {
  2322. struct fib6_nh *fib6_nh = &nhi->fib6_nh;
  2323. struct fib6_config fib6_cfg = {
  2324. .fc_table = l3mdev_fib_table(cfg->dev),
  2325. .fc_ifindex = cfg->nh_ifindex,
  2326. .fc_gateway = cfg->gw.ipv6,
  2327. .fc_flags = cfg->nh_flags,
  2328. .fc_nlinfo = cfg->nlinfo,
  2329. .fc_encap = cfg->nh_encap,
  2330. .fc_encap_type = cfg->nh_encap_type,
  2331. .fc_is_fdb = cfg->nh_fdb,
  2332. };
  2333. int err;
  2334. if (!ipv6_addr_any(&cfg->gw.ipv6))
  2335. fib6_cfg.fc_flags |= RTF_GATEWAY;
  2336. /* sets nh_dev if successful */
  2337. err = ipv6_stub->fib6_nh_init(net, fib6_nh, &fib6_cfg, GFP_KERNEL,
  2338. extack);
  2339. if (err) {
  2340. /* IPv6 is not enabled, don't call fib6_nh_release */
  2341. if (err == -EAFNOSUPPORT)
  2342. goto out;
  2343. ipv6_stub->fib6_nh_release(fib6_nh);
  2344. } else {
  2345. nh->nh_flags = fib6_nh->fib_nh_flags;
  2346. }
  2347. out:
  2348. return err;
  2349. }
  2350. static struct nexthop *nexthop_create(struct net *net, struct nh_config *cfg,
  2351. struct netlink_ext_ack *extack)
  2352. {
  2353. struct nh_info *nhi;
  2354. struct nexthop *nh;
  2355. int err = 0;
  2356. nh = nexthop_alloc();
  2357. if (!nh)
  2358. return ERR_PTR(-ENOMEM);
  2359. nhi = kzalloc(sizeof(*nhi), GFP_KERNEL);
  2360. if (!nhi) {
  2361. kfree(nh);
  2362. return ERR_PTR(-ENOMEM);
  2363. }
  2364. nh->nh_flags = cfg->nh_flags;
  2365. nh->net = net;
  2366. nhi->nh_parent = nh;
  2367. nhi->family = cfg->nh_family;
  2368. nhi->fib_nhc.nhc_scope = RT_SCOPE_LINK;
  2369. if (cfg->nh_fdb)
  2370. nhi->fdb_nh = 1;
  2371. if (cfg->nh_blackhole) {
  2372. nhi->reject_nh = 1;
  2373. cfg->nh_ifindex = net->loopback_dev->ifindex;
  2374. }
  2375. switch (cfg->nh_family) {
  2376. case AF_INET:
  2377. err = nh_create_ipv4(net, nh, nhi, cfg, extack);
  2378. break;
  2379. case AF_INET6:
  2380. err = nh_create_ipv6(net, nh, nhi, cfg, extack);
  2381. break;
  2382. }
  2383. if (err) {
  2384. kfree(nhi);
  2385. kfree(nh);
  2386. return ERR_PTR(err);
  2387. }
  2388. /* add the entry to the device based hash */
  2389. if (!nhi->fdb_nh)
  2390. nexthop_devhash_add(net, nhi);
  2391. rcu_assign_pointer(nh->nh_info, nhi);
  2392. return nh;
  2393. }
  2394. /* called with rtnl lock held */
  2395. static struct nexthop *nexthop_add(struct net *net, struct nh_config *cfg,
  2396. struct netlink_ext_ack *extack)
  2397. {
  2398. struct nexthop *nh;
  2399. int err;
  2400. if (cfg->nlflags & NLM_F_REPLACE && !cfg->nh_id) {
  2401. NL_SET_ERR_MSG(extack, "Replace requires nexthop id");
  2402. return ERR_PTR(-EINVAL);
  2403. }
  2404. if (!cfg->nh_id) {
  2405. cfg->nh_id = nh_find_unused_id(net);
  2406. if (!cfg->nh_id) {
  2407. NL_SET_ERR_MSG(extack, "No unused id");
  2408. return ERR_PTR(-EINVAL);
  2409. }
  2410. }
  2411. if (cfg->nh_grp)
  2412. nh = nexthop_create_group(net, cfg);
  2413. else
  2414. nh = nexthop_create(net, cfg, extack);
  2415. if (IS_ERR(nh))
  2416. return nh;
  2417. refcount_set(&nh->refcnt, 1);
  2418. nh->id = cfg->nh_id;
  2419. nh->protocol = cfg->nh_protocol;
  2420. nh->net = net;
  2421. err = insert_nexthop(net, nh, cfg, extack);
  2422. if (err) {
  2423. __remove_nexthop(net, nh, NULL);
  2424. nexthop_put(nh);
  2425. nh = ERR_PTR(err);
  2426. }
  2427. return nh;
  2428. }
  2429. static int rtm_nh_get_timer(struct nlattr *attr, unsigned long fallback,
  2430. unsigned long *timer_p, bool *has_p,
  2431. struct netlink_ext_ack *extack)
  2432. {
  2433. unsigned long timer;
  2434. u32 value;
  2435. if (!attr) {
  2436. *timer_p = fallback;
  2437. *has_p = false;
  2438. return 0;
  2439. }
  2440. value = nla_get_u32(attr);
  2441. timer = clock_t_to_jiffies(value);
  2442. if (timer == ~0UL) {
  2443. NL_SET_ERR_MSG(extack, "Timer value too large");
  2444. return -EINVAL;
  2445. }
  2446. *timer_p = timer;
  2447. *has_p = true;
  2448. return 0;
  2449. }
  2450. static int rtm_to_nh_config_grp_res(struct nlattr *res, struct nh_config *cfg,
  2451. struct netlink_ext_ack *extack)
  2452. {
  2453. struct nlattr *tb[ARRAY_SIZE(rtm_nh_res_policy_new)] = {};
  2454. int err;
  2455. if (res) {
  2456. err = nla_parse_nested(tb,
  2457. ARRAY_SIZE(rtm_nh_res_policy_new) - 1,
  2458. res, rtm_nh_res_policy_new, extack);
  2459. if (err < 0)
  2460. return err;
  2461. }
  2462. if (tb[NHA_RES_GROUP_BUCKETS]) {
  2463. cfg->nh_grp_res_num_buckets =
  2464. nla_get_u16(tb[NHA_RES_GROUP_BUCKETS]);
  2465. cfg->nh_grp_res_has_num_buckets = true;
  2466. if (!cfg->nh_grp_res_num_buckets) {
  2467. NL_SET_ERR_MSG(extack, "Number of buckets needs to be non-0");
  2468. return -EINVAL;
  2469. }
  2470. }
  2471. err = rtm_nh_get_timer(tb[NHA_RES_GROUP_IDLE_TIMER],
  2472. NH_RES_DEFAULT_IDLE_TIMER,
  2473. &cfg->nh_grp_res_idle_timer,
  2474. &cfg->nh_grp_res_has_idle_timer,
  2475. extack);
  2476. if (err)
  2477. return err;
  2478. return rtm_nh_get_timer(tb[NHA_RES_GROUP_UNBALANCED_TIMER],
  2479. NH_RES_DEFAULT_UNBALANCED_TIMER,
  2480. &cfg->nh_grp_res_unbalanced_timer,
  2481. &cfg->nh_grp_res_has_unbalanced_timer,
  2482. extack);
  2483. }
  2484. static int rtm_to_nh_config(struct net *net, struct sk_buff *skb,
  2485. struct nlmsghdr *nlh, struct nh_config *cfg,
  2486. struct netlink_ext_ack *extack)
  2487. {
  2488. struct nhmsg *nhm = nlmsg_data(nlh);
  2489. struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_new)];
  2490. int err;
  2491. err = nlmsg_parse(nlh, sizeof(*nhm), tb,
  2492. ARRAY_SIZE(rtm_nh_policy_new) - 1,
  2493. rtm_nh_policy_new, extack);
  2494. if (err < 0)
  2495. return err;
  2496. err = -EINVAL;
  2497. if (nhm->resvd || nhm->nh_scope) {
  2498. NL_SET_ERR_MSG(extack, "Invalid values in ancillary header");
  2499. goto out;
  2500. }
  2501. if (nhm->nh_flags & ~NEXTHOP_VALID_USER_FLAGS) {
  2502. NL_SET_ERR_MSG(extack, "Invalid nexthop flags in ancillary header");
  2503. goto out;
  2504. }
  2505. switch (nhm->nh_family) {
  2506. case AF_INET:
  2507. case AF_INET6:
  2508. break;
  2509. case AF_UNSPEC:
  2510. if (tb[NHA_GROUP])
  2511. break;
  2512. fallthrough;
  2513. default:
  2514. NL_SET_ERR_MSG(extack, "Invalid address family");
  2515. goto out;
  2516. }
  2517. memset(cfg, 0, sizeof(*cfg));
  2518. cfg->nlflags = nlh->nlmsg_flags;
  2519. cfg->nlinfo.portid = NETLINK_CB(skb).portid;
  2520. cfg->nlinfo.nlh = nlh;
  2521. cfg->nlinfo.nl_net = net;
  2522. cfg->nh_family = nhm->nh_family;
  2523. cfg->nh_protocol = nhm->nh_protocol;
  2524. cfg->nh_flags = nhm->nh_flags;
  2525. if (tb[NHA_ID])
  2526. cfg->nh_id = nla_get_u32(tb[NHA_ID]);
  2527. if (tb[NHA_FDB]) {
  2528. if (tb[NHA_OIF] || tb[NHA_BLACKHOLE] ||
  2529. tb[NHA_ENCAP] || tb[NHA_ENCAP_TYPE]) {
  2530. NL_SET_ERR_MSG(extack, "Fdb attribute can not be used with encap, oif or blackhole");
  2531. goto out;
  2532. }
  2533. if (nhm->nh_flags) {
  2534. NL_SET_ERR_MSG(extack, "Unsupported nexthop flags in ancillary header");
  2535. goto out;
  2536. }
  2537. cfg->nh_fdb = nla_get_flag(tb[NHA_FDB]);
  2538. }
  2539. if (tb[NHA_GROUP]) {
  2540. if (nhm->nh_family != AF_UNSPEC) {
  2541. NL_SET_ERR_MSG(extack, "Invalid family for group");
  2542. goto out;
  2543. }
  2544. cfg->nh_grp = tb[NHA_GROUP];
  2545. cfg->nh_grp_type = NEXTHOP_GRP_TYPE_MPATH;
  2546. if (tb[NHA_GROUP_TYPE])
  2547. cfg->nh_grp_type = nla_get_u16(tb[NHA_GROUP_TYPE]);
  2548. if (cfg->nh_grp_type > NEXTHOP_GRP_TYPE_MAX) {
  2549. NL_SET_ERR_MSG(extack, "Invalid group type");
  2550. goto out;
  2551. }
  2552. err = nh_check_attr_group(net, tb, ARRAY_SIZE(tb),
  2553. cfg->nh_grp_type, extack);
  2554. if (err)
  2555. goto out;
  2556. if (cfg->nh_grp_type == NEXTHOP_GRP_TYPE_RES)
  2557. err = rtm_to_nh_config_grp_res(tb[NHA_RES_GROUP],
  2558. cfg, extack);
  2559. if (tb[NHA_HW_STATS_ENABLE])
  2560. cfg->nh_hw_stats = nla_get_u32(tb[NHA_HW_STATS_ENABLE]);
  2561. /* no other attributes should be set */
  2562. goto out;
  2563. }
  2564. if (tb[NHA_BLACKHOLE]) {
  2565. if (tb[NHA_GATEWAY] || tb[NHA_OIF] ||
  2566. tb[NHA_ENCAP] || tb[NHA_ENCAP_TYPE] || tb[NHA_FDB]) {
  2567. NL_SET_ERR_MSG(extack, "Blackhole attribute can not be used with gateway, oif, encap or fdb");
  2568. goto out;
  2569. }
  2570. cfg->nh_blackhole = 1;
  2571. err = 0;
  2572. goto out;
  2573. }
  2574. if (!cfg->nh_fdb && !tb[NHA_OIF]) {
  2575. NL_SET_ERR_MSG(extack, "Device attribute required for non-blackhole and non-fdb nexthops");
  2576. goto out;
  2577. }
  2578. if (!cfg->nh_fdb && tb[NHA_OIF]) {
  2579. cfg->nh_ifindex = nla_get_u32(tb[NHA_OIF]);
  2580. if (cfg->nh_ifindex)
  2581. cfg->dev = __dev_get_by_index(net, cfg->nh_ifindex);
  2582. if (!cfg->dev) {
  2583. NL_SET_ERR_MSG(extack, "Invalid device index");
  2584. goto out;
  2585. } else if (!(cfg->dev->flags & IFF_UP)) {
  2586. NL_SET_ERR_MSG(extack, "Nexthop device is not up");
  2587. err = -ENETDOWN;
  2588. goto out;
  2589. } else if (!netif_carrier_ok(cfg->dev)) {
  2590. NL_SET_ERR_MSG(extack, "Carrier for nexthop device is down");
  2591. err = -ENETDOWN;
  2592. goto out;
  2593. }
  2594. }
  2595. err = -EINVAL;
  2596. if (tb[NHA_GATEWAY]) {
  2597. struct nlattr *gwa = tb[NHA_GATEWAY];
  2598. switch (cfg->nh_family) {
  2599. case AF_INET:
  2600. if (nla_len(gwa) != sizeof(u32)) {
  2601. NL_SET_ERR_MSG(extack, "Invalid gateway");
  2602. goto out;
  2603. }
  2604. cfg->gw.ipv4 = nla_get_be32(gwa);
  2605. break;
  2606. case AF_INET6:
  2607. if (nla_len(gwa) != sizeof(struct in6_addr)) {
  2608. NL_SET_ERR_MSG(extack, "Invalid gateway");
  2609. goto out;
  2610. }
  2611. cfg->gw.ipv6 = nla_get_in6_addr(gwa);
  2612. break;
  2613. default:
  2614. NL_SET_ERR_MSG(extack,
  2615. "Unknown address family for gateway");
  2616. goto out;
  2617. }
  2618. } else {
  2619. /* device only nexthop (no gateway) */
  2620. if (cfg->nh_flags & RTNH_F_ONLINK) {
  2621. NL_SET_ERR_MSG(extack,
  2622. "ONLINK flag can not be set for nexthop without a gateway");
  2623. goto out;
  2624. }
  2625. }
  2626. if (tb[NHA_ENCAP]) {
  2627. cfg->nh_encap = tb[NHA_ENCAP];
  2628. if (!tb[NHA_ENCAP_TYPE]) {
  2629. NL_SET_ERR_MSG(extack, "LWT encapsulation type is missing");
  2630. goto out;
  2631. }
  2632. cfg->nh_encap_type = nla_get_u16(tb[NHA_ENCAP_TYPE]);
  2633. err = lwtunnel_valid_encap_type(cfg->nh_encap_type, extack);
  2634. if (err < 0)
  2635. goto out;
  2636. } else if (tb[NHA_ENCAP_TYPE]) {
  2637. NL_SET_ERR_MSG(extack, "LWT encapsulation attribute is missing");
  2638. goto out;
  2639. }
  2640. if (tb[NHA_HW_STATS_ENABLE]) {
  2641. NL_SET_ERR_MSG(extack, "Cannot enable nexthop hardware statistics for non-group nexthops");
  2642. goto out;
  2643. }
  2644. err = 0;
  2645. out:
  2646. return err;
  2647. }
  2648. /* rtnl */
  2649. static int rtm_new_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh,
  2650. struct netlink_ext_ack *extack)
  2651. {
  2652. struct net *net = sock_net(skb->sk);
  2653. struct nh_config cfg;
  2654. struct nexthop *nh;
  2655. int err;
  2656. err = rtm_to_nh_config(net, skb, nlh, &cfg, extack);
  2657. if (!err) {
  2658. nh = nexthop_add(net, &cfg, extack);
  2659. if (IS_ERR(nh))
  2660. err = PTR_ERR(nh);
  2661. }
  2662. return err;
  2663. }
  2664. static int nh_valid_get_del_req(const struct nlmsghdr *nlh,
  2665. struct nlattr **tb, u32 *id, u32 *op_flags,
  2666. struct netlink_ext_ack *extack)
  2667. {
  2668. struct nhmsg *nhm = nlmsg_data(nlh);
  2669. if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) {
  2670. NL_SET_ERR_MSG(extack, "Invalid values in header");
  2671. return -EINVAL;
  2672. }
  2673. if (!tb[NHA_ID]) {
  2674. NL_SET_ERR_MSG(extack, "Nexthop id is missing");
  2675. return -EINVAL;
  2676. }
  2677. *id = nla_get_u32(tb[NHA_ID]);
  2678. if (!(*id)) {
  2679. NL_SET_ERR_MSG(extack, "Invalid nexthop id");
  2680. return -EINVAL;
  2681. }
  2682. if (op_flags) {
  2683. if (tb[NHA_OP_FLAGS])
  2684. *op_flags = nla_get_u32(tb[NHA_OP_FLAGS]);
  2685. else
  2686. *op_flags = 0;
  2687. }
  2688. return 0;
  2689. }
  2690. /* rtnl */
  2691. static int rtm_del_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh,
  2692. struct netlink_ext_ack *extack)
  2693. {
  2694. struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_del)];
  2695. struct net *net = sock_net(skb->sk);
  2696. struct nl_info nlinfo = {
  2697. .nlh = nlh,
  2698. .nl_net = net,
  2699. .portid = NETLINK_CB(skb).portid,
  2700. };
  2701. struct nexthop *nh;
  2702. int err;
  2703. u32 id;
  2704. err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb,
  2705. ARRAY_SIZE(rtm_nh_policy_del) - 1, rtm_nh_policy_del,
  2706. extack);
  2707. if (err < 0)
  2708. return err;
  2709. err = nh_valid_get_del_req(nlh, tb, &id, NULL, extack);
  2710. if (err)
  2711. return err;
  2712. nh = nexthop_find_by_id(net, id);
  2713. if (!nh)
  2714. return -ENOENT;
  2715. remove_nexthop(net, nh, &nlinfo);
  2716. return 0;
  2717. }
  2718. /* rtnl */
  2719. static int rtm_get_nexthop(struct sk_buff *in_skb, struct nlmsghdr *nlh,
  2720. struct netlink_ext_ack *extack)
  2721. {
  2722. struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_get)];
  2723. struct net *net = sock_net(in_skb->sk);
  2724. struct sk_buff *skb = NULL;
  2725. struct nexthop *nh;
  2726. u32 op_flags;
  2727. int err;
  2728. u32 id;
  2729. err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb,
  2730. ARRAY_SIZE(rtm_nh_policy_get) - 1, rtm_nh_policy_get,
  2731. extack);
  2732. if (err < 0)
  2733. return err;
  2734. err = nh_valid_get_del_req(nlh, tb, &id, &op_flags, extack);
  2735. if (err)
  2736. return err;
  2737. err = -ENOBUFS;
  2738. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  2739. if (!skb)
  2740. goto out;
  2741. err = -ENOENT;
  2742. nh = nexthop_find_by_id(net, id);
  2743. if (!nh)
  2744. goto errout_free;
  2745. err = nh_fill_node(skb, nh, RTM_NEWNEXTHOP, NETLINK_CB(in_skb).portid,
  2746. nlh->nlmsg_seq, 0, op_flags);
  2747. if (err < 0) {
  2748. WARN_ON(err == -EMSGSIZE);
  2749. goto errout_free;
  2750. }
  2751. err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
  2752. out:
  2753. return err;
  2754. errout_free:
  2755. kfree_skb(skb);
  2756. goto out;
  2757. }
  2758. struct nh_dump_filter {
  2759. u32 nh_id;
  2760. int dev_idx;
  2761. int master_idx;
  2762. bool group_filter;
  2763. bool fdb_filter;
  2764. u32 res_bucket_nh_id;
  2765. u32 op_flags;
  2766. };
  2767. static bool nh_dump_filtered(struct nexthop *nh,
  2768. struct nh_dump_filter *filter, u8 family)
  2769. {
  2770. const struct net_device *dev;
  2771. const struct nh_info *nhi;
  2772. if (filter->group_filter && !nh->is_group)
  2773. return true;
  2774. if (!filter->dev_idx && !filter->master_idx && !family)
  2775. return false;
  2776. if (nh->is_group)
  2777. return true;
  2778. nhi = rtnl_dereference(nh->nh_info);
  2779. if (family && nhi->family != family)
  2780. return true;
  2781. dev = nhi->fib_nhc.nhc_dev;
  2782. if (filter->dev_idx && (!dev || dev->ifindex != filter->dev_idx))
  2783. return true;
  2784. if (filter->master_idx) {
  2785. struct net_device *master;
  2786. if (!dev)
  2787. return true;
  2788. master = netdev_master_upper_dev_get((struct net_device *)dev);
  2789. if (!master || master->ifindex != filter->master_idx)
  2790. return true;
  2791. }
  2792. return false;
  2793. }
  2794. static int __nh_valid_dump_req(const struct nlmsghdr *nlh, struct nlattr **tb,
  2795. struct nh_dump_filter *filter,
  2796. struct netlink_ext_ack *extack)
  2797. {
  2798. struct nhmsg *nhm;
  2799. u32 idx;
  2800. if (tb[NHA_OIF]) {
  2801. idx = nla_get_u32(tb[NHA_OIF]);
  2802. if (idx > INT_MAX) {
  2803. NL_SET_ERR_MSG(extack, "Invalid device index");
  2804. return -EINVAL;
  2805. }
  2806. filter->dev_idx = idx;
  2807. }
  2808. if (tb[NHA_MASTER]) {
  2809. idx = nla_get_u32(tb[NHA_MASTER]);
  2810. if (idx > INT_MAX) {
  2811. NL_SET_ERR_MSG(extack, "Invalid master device index");
  2812. return -EINVAL;
  2813. }
  2814. filter->master_idx = idx;
  2815. }
  2816. filter->group_filter = nla_get_flag(tb[NHA_GROUPS]);
  2817. filter->fdb_filter = nla_get_flag(tb[NHA_FDB]);
  2818. nhm = nlmsg_data(nlh);
  2819. if (nhm->nh_protocol || nhm->resvd || nhm->nh_scope || nhm->nh_flags) {
  2820. NL_SET_ERR_MSG(extack, "Invalid values in header for nexthop dump request");
  2821. return -EINVAL;
  2822. }
  2823. return 0;
  2824. }
  2825. static int nh_valid_dump_req(const struct nlmsghdr *nlh,
  2826. struct nh_dump_filter *filter,
  2827. struct netlink_callback *cb)
  2828. {
  2829. struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_dump)];
  2830. int err;
  2831. err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb,
  2832. ARRAY_SIZE(rtm_nh_policy_dump) - 1,
  2833. rtm_nh_policy_dump, cb->extack);
  2834. if (err < 0)
  2835. return err;
  2836. if (tb[NHA_OP_FLAGS])
  2837. filter->op_flags = nla_get_u32(tb[NHA_OP_FLAGS]);
  2838. else
  2839. filter->op_flags = 0;
  2840. return __nh_valid_dump_req(nlh, tb, filter, cb->extack);
  2841. }
  2842. struct rtm_dump_nh_ctx {
  2843. u32 idx;
  2844. };
  2845. static struct rtm_dump_nh_ctx *
  2846. rtm_dump_nh_ctx(struct netlink_callback *cb)
  2847. {
  2848. struct rtm_dump_nh_ctx *ctx = (void *)cb->ctx;
  2849. BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
  2850. return ctx;
  2851. }
  2852. static int rtm_dump_walk_nexthops(struct sk_buff *skb,
  2853. struct netlink_callback *cb,
  2854. struct rb_root *root,
  2855. struct rtm_dump_nh_ctx *ctx,
  2856. int (*nh_cb)(struct sk_buff *skb,
  2857. struct netlink_callback *cb,
  2858. struct nexthop *nh, void *data),
  2859. void *data)
  2860. {
  2861. struct rb_node *node;
  2862. int s_idx;
  2863. int err;
  2864. s_idx = ctx->idx;
  2865. for (node = rb_first(root); node; node = rb_next(node)) {
  2866. struct nexthop *nh;
  2867. nh = rb_entry(node, struct nexthop, rb_node);
  2868. if (nh->id < s_idx)
  2869. continue;
  2870. ctx->idx = nh->id;
  2871. err = nh_cb(skb, cb, nh, data);
  2872. if (err)
  2873. return err;
  2874. }
  2875. return 0;
  2876. }
  2877. static int rtm_dump_nexthop_cb(struct sk_buff *skb, struct netlink_callback *cb,
  2878. struct nexthop *nh, void *data)
  2879. {
  2880. struct nhmsg *nhm = nlmsg_data(cb->nlh);
  2881. struct nh_dump_filter *filter = data;
  2882. if (nh_dump_filtered(nh, filter, nhm->nh_family))
  2883. return 0;
  2884. return nh_fill_node(skb, nh, RTM_NEWNEXTHOP,
  2885. NETLINK_CB(cb->skb).portid,
  2886. cb->nlh->nlmsg_seq, NLM_F_MULTI, filter->op_flags);
  2887. }
  2888. /* rtnl */
  2889. static int rtm_dump_nexthop(struct sk_buff *skb, struct netlink_callback *cb)
  2890. {
  2891. struct rtm_dump_nh_ctx *ctx = rtm_dump_nh_ctx(cb);
  2892. struct net *net = sock_net(skb->sk);
  2893. struct rb_root *root = &net->nexthop.rb_root;
  2894. struct nh_dump_filter filter = {};
  2895. int err;
  2896. err = nh_valid_dump_req(cb->nlh, &filter, cb);
  2897. if (err < 0)
  2898. return err;
  2899. err = rtm_dump_walk_nexthops(skb, cb, root, ctx,
  2900. &rtm_dump_nexthop_cb, &filter);
  2901. cb->seq = net->nexthop.seq;
  2902. nl_dump_check_consistent(cb, nlmsg_hdr(skb));
  2903. return err;
  2904. }
  2905. static struct nexthop *
  2906. nexthop_find_group_resilient(struct net *net, u32 id,
  2907. struct netlink_ext_ack *extack)
  2908. {
  2909. struct nh_group *nhg;
  2910. struct nexthop *nh;
  2911. nh = nexthop_find_by_id(net, id);
  2912. if (!nh)
  2913. return ERR_PTR(-ENOENT);
  2914. if (!nh->is_group) {
  2915. NL_SET_ERR_MSG(extack, "Not a nexthop group");
  2916. return ERR_PTR(-EINVAL);
  2917. }
  2918. nhg = rtnl_dereference(nh->nh_grp);
  2919. if (!nhg->resilient) {
  2920. NL_SET_ERR_MSG(extack, "Nexthop group not of type resilient");
  2921. return ERR_PTR(-EINVAL);
  2922. }
  2923. return nh;
  2924. }
  2925. static int nh_valid_dump_nhid(struct nlattr *attr, u32 *nh_id_p,
  2926. struct netlink_ext_ack *extack)
  2927. {
  2928. u32 idx;
  2929. if (attr) {
  2930. idx = nla_get_u32(attr);
  2931. if (!idx) {
  2932. NL_SET_ERR_MSG(extack, "Invalid nexthop id");
  2933. return -EINVAL;
  2934. }
  2935. *nh_id_p = idx;
  2936. } else {
  2937. *nh_id_p = 0;
  2938. }
  2939. return 0;
  2940. }
  2941. static int nh_valid_dump_bucket_req(const struct nlmsghdr *nlh,
  2942. struct nh_dump_filter *filter,
  2943. struct netlink_callback *cb)
  2944. {
  2945. struct nlattr *res_tb[ARRAY_SIZE(rtm_nh_res_bucket_policy_dump)];
  2946. struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_dump_bucket)];
  2947. int err;
  2948. err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb,
  2949. ARRAY_SIZE(rtm_nh_policy_dump_bucket) - 1,
  2950. rtm_nh_policy_dump_bucket, NULL);
  2951. if (err < 0)
  2952. return err;
  2953. err = nh_valid_dump_nhid(tb[NHA_ID], &filter->nh_id, cb->extack);
  2954. if (err)
  2955. return err;
  2956. if (tb[NHA_RES_BUCKET]) {
  2957. size_t max = ARRAY_SIZE(rtm_nh_res_bucket_policy_dump) - 1;
  2958. err = nla_parse_nested(res_tb, max,
  2959. tb[NHA_RES_BUCKET],
  2960. rtm_nh_res_bucket_policy_dump,
  2961. cb->extack);
  2962. if (err < 0)
  2963. return err;
  2964. err = nh_valid_dump_nhid(res_tb[NHA_RES_BUCKET_NH_ID],
  2965. &filter->res_bucket_nh_id,
  2966. cb->extack);
  2967. if (err)
  2968. return err;
  2969. }
  2970. return __nh_valid_dump_req(nlh, tb, filter, cb->extack);
  2971. }
  2972. struct rtm_dump_res_bucket_ctx {
  2973. struct rtm_dump_nh_ctx nh;
  2974. u16 bucket_index;
  2975. };
  2976. static struct rtm_dump_res_bucket_ctx *
  2977. rtm_dump_res_bucket_ctx(struct netlink_callback *cb)
  2978. {
  2979. struct rtm_dump_res_bucket_ctx *ctx = (void *)cb->ctx;
  2980. BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
  2981. return ctx;
  2982. }
  2983. struct rtm_dump_nexthop_bucket_data {
  2984. struct rtm_dump_res_bucket_ctx *ctx;
  2985. struct nh_dump_filter filter;
  2986. };
  2987. static int rtm_dump_nexthop_bucket_nh(struct sk_buff *skb,
  2988. struct netlink_callback *cb,
  2989. struct nexthop *nh,
  2990. struct rtm_dump_nexthop_bucket_data *dd)
  2991. {
  2992. u32 portid = NETLINK_CB(cb->skb).portid;
  2993. struct nhmsg *nhm = nlmsg_data(cb->nlh);
  2994. struct nh_res_table *res_table;
  2995. struct nh_group *nhg;
  2996. u16 bucket_index;
  2997. int err;
  2998. nhg = rtnl_dereference(nh->nh_grp);
  2999. res_table = rtnl_dereference(nhg->res_table);
  3000. for (bucket_index = dd->ctx->bucket_index;
  3001. bucket_index < res_table->num_nh_buckets;
  3002. bucket_index++) {
  3003. struct nh_res_bucket *bucket;
  3004. struct nh_grp_entry *nhge;
  3005. bucket = &res_table->nh_buckets[bucket_index];
  3006. nhge = rtnl_dereference(bucket->nh_entry);
  3007. if (nh_dump_filtered(nhge->nh, &dd->filter, nhm->nh_family))
  3008. continue;
  3009. if (dd->filter.res_bucket_nh_id &&
  3010. dd->filter.res_bucket_nh_id != nhge->nh->id)
  3011. continue;
  3012. dd->ctx->bucket_index = bucket_index;
  3013. err = nh_fill_res_bucket(skb, nh, bucket, bucket_index,
  3014. RTM_NEWNEXTHOPBUCKET, portid,
  3015. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  3016. cb->extack);
  3017. if (err)
  3018. return err;
  3019. }
  3020. dd->ctx->bucket_index = 0;
  3021. return 0;
  3022. }
  3023. static int rtm_dump_nexthop_bucket_cb(struct sk_buff *skb,
  3024. struct netlink_callback *cb,
  3025. struct nexthop *nh, void *data)
  3026. {
  3027. struct rtm_dump_nexthop_bucket_data *dd = data;
  3028. struct nh_group *nhg;
  3029. if (!nh->is_group)
  3030. return 0;
  3031. nhg = rtnl_dereference(nh->nh_grp);
  3032. if (!nhg->resilient)
  3033. return 0;
  3034. return rtm_dump_nexthop_bucket_nh(skb, cb, nh, dd);
  3035. }
  3036. /* rtnl */
  3037. static int rtm_dump_nexthop_bucket(struct sk_buff *skb,
  3038. struct netlink_callback *cb)
  3039. {
  3040. struct rtm_dump_res_bucket_ctx *ctx = rtm_dump_res_bucket_ctx(cb);
  3041. struct rtm_dump_nexthop_bucket_data dd = { .ctx = ctx };
  3042. struct net *net = sock_net(skb->sk);
  3043. struct nexthop *nh;
  3044. int err;
  3045. err = nh_valid_dump_bucket_req(cb->nlh, &dd.filter, cb);
  3046. if (err)
  3047. return err;
  3048. if (dd.filter.nh_id) {
  3049. nh = nexthop_find_group_resilient(net, dd.filter.nh_id,
  3050. cb->extack);
  3051. if (IS_ERR(nh))
  3052. return PTR_ERR(nh);
  3053. err = rtm_dump_nexthop_bucket_nh(skb, cb, nh, &dd);
  3054. } else {
  3055. struct rb_root *root = &net->nexthop.rb_root;
  3056. err = rtm_dump_walk_nexthops(skb, cb, root, &ctx->nh,
  3057. &rtm_dump_nexthop_bucket_cb, &dd);
  3058. }
  3059. cb->seq = net->nexthop.seq;
  3060. nl_dump_check_consistent(cb, nlmsg_hdr(skb));
  3061. return err;
  3062. }
  3063. static int nh_valid_get_bucket_req_res_bucket(struct nlattr *res,
  3064. u16 *bucket_index,
  3065. struct netlink_ext_ack *extack)
  3066. {
  3067. struct nlattr *tb[ARRAY_SIZE(rtm_nh_res_bucket_policy_get)];
  3068. int err;
  3069. err = nla_parse_nested(tb, ARRAY_SIZE(rtm_nh_res_bucket_policy_get) - 1,
  3070. res, rtm_nh_res_bucket_policy_get, extack);
  3071. if (err < 0)
  3072. return err;
  3073. if (!tb[NHA_RES_BUCKET_INDEX]) {
  3074. NL_SET_ERR_MSG(extack, "Bucket index is missing");
  3075. return -EINVAL;
  3076. }
  3077. *bucket_index = nla_get_u16(tb[NHA_RES_BUCKET_INDEX]);
  3078. return 0;
  3079. }
  3080. static int nh_valid_get_bucket_req(const struct nlmsghdr *nlh,
  3081. u32 *id, u16 *bucket_index,
  3082. struct netlink_ext_ack *extack)
  3083. {
  3084. struct nlattr *tb[ARRAY_SIZE(rtm_nh_policy_get_bucket)];
  3085. int err;
  3086. err = nlmsg_parse(nlh, sizeof(struct nhmsg), tb,
  3087. ARRAY_SIZE(rtm_nh_policy_get_bucket) - 1,
  3088. rtm_nh_policy_get_bucket, extack);
  3089. if (err < 0)
  3090. return err;
  3091. err = nh_valid_get_del_req(nlh, tb, id, NULL, extack);
  3092. if (err)
  3093. return err;
  3094. if (!tb[NHA_RES_BUCKET]) {
  3095. NL_SET_ERR_MSG(extack, "Bucket information is missing");
  3096. return -EINVAL;
  3097. }
  3098. err = nh_valid_get_bucket_req_res_bucket(tb[NHA_RES_BUCKET],
  3099. bucket_index, extack);
  3100. if (err)
  3101. return err;
  3102. return 0;
  3103. }
  3104. /* rtnl */
  3105. static int rtm_get_nexthop_bucket(struct sk_buff *in_skb, struct nlmsghdr *nlh,
  3106. struct netlink_ext_ack *extack)
  3107. {
  3108. struct net *net = sock_net(in_skb->sk);
  3109. struct nh_res_table *res_table;
  3110. struct sk_buff *skb = NULL;
  3111. struct nh_group *nhg;
  3112. struct nexthop *nh;
  3113. u16 bucket_index;
  3114. int err;
  3115. u32 id;
  3116. err = nh_valid_get_bucket_req(nlh, &id, &bucket_index, extack);
  3117. if (err)
  3118. return err;
  3119. nh = nexthop_find_group_resilient(net, id, extack);
  3120. if (IS_ERR(nh))
  3121. return PTR_ERR(nh);
  3122. nhg = rtnl_dereference(nh->nh_grp);
  3123. res_table = rtnl_dereference(nhg->res_table);
  3124. if (bucket_index >= res_table->num_nh_buckets) {
  3125. NL_SET_ERR_MSG(extack, "Bucket index out of bounds");
  3126. return -ENOENT;
  3127. }
  3128. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  3129. if (!skb)
  3130. return -ENOBUFS;
  3131. err = nh_fill_res_bucket(skb, nh, &res_table->nh_buckets[bucket_index],
  3132. bucket_index, RTM_NEWNEXTHOPBUCKET,
  3133. NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
  3134. 0, extack);
  3135. if (err < 0) {
  3136. WARN_ON(err == -EMSGSIZE);
  3137. goto errout_free;
  3138. }
  3139. return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
  3140. errout_free:
  3141. kfree_skb(skb);
  3142. return err;
  3143. }
  3144. static void nexthop_sync_mtu(struct net_device *dev, u32 orig_mtu)
  3145. {
  3146. unsigned int hash = nh_dev_hashfn(dev->ifindex);
  3147. struct net *net = dev_net(dev);
  3148. struct hlist_head *head = &net->nexthop.devhash[hash];
  3149. struct hlist_node *n;
  3150. struct nh_info *nhi;
  3151. hlist_for_each_entry_safe(nhi, n, head, dev_hash) {
  3152. if (nhi->fib_nhc.nhc_dev == dev) {
  3153. if (nhi->family == AF_INET)
  3154. fib_nhc_update_mtu(&nhi->fib_nhc, dev->mtu,
  3155. orig_mtu);
  3156. }
  3157. }
  3158. }
  3159. /* rtnl */
  3160. static int nh_netdev_event(struct notifier_block *this,
  3161. unsigned long event, void *ptr)
  3162. {
  3163. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  3164. struct netdev_notifier_info_ext *info_ext;
  3165. switch (event) {
  3166. case NETDEV_DOWN:
  3167. case NETDEV_UNREGISTER:
  3168. nexthop_flush_dev(dev, event);
  3169. break;
  3170. case NETDEV_CHANGE:
  3171. if (!(dev_get_flags(dev) & (IFF_RUNNING | IFF_LOWER_UP)))
  3172. nexthop_flush_dev(dev, event);
  3173. break;
  3174. case NETDEV_CHANGEMTU:
  3175. info_ext = ptr;
  3176. nexthop_sync_mtu(dev, info_ext->ext.mtu);
  3177. rt_cache_flush(dev_net(dev));
  3178. break;
  3179. }
  3180. return NOTIFY_DONE;
  3181. }
  3182. static struct notifier_block nh_netdev_notifier = {
  3183. .notifier_call = nh_netdev_event,
  3184. };
  3185. static int nexthops_dump(struct net *net, struct notifier_block *nb,
  3186. enum nexthop_event_type event_type,
  3187. struct netlink_ext_ack *extack)
  3188. {
  3189. struct rb_root *root = &net->nexthop.rb_root;
  3190. struct rb_node *node;
  3191. int err = 0;
  3192. for (node = rb_first(root); node; node = rb_next(node)) {
  3193. struct nexthop *nh;
  3194. nh = rb_entry(node, struct nexthop, rb_node);
  3195. err = call_nexthop_notifier(nb, net, event_type, nh, extack);
  3196. if (err)
  3197. break;
  3198. }
  3199. return err;
  3200. }
  3201. int register_nexthop_notifier(struct net *net, struct notifier_block *nb,
  3202. struct netlink_ext_ack *extack)
  3203. {
  3204. int err;
  3205. rtnl_lock();
  3206. err = nexthops_dump(net, nb, NEXTHOP_EVENT_REPLACE, extack);
  3207. if (err)
  3208. goto unlock;
  3209. err = blocking_notifier_chain_register(&net->nexthop.notifier_chain,
  3210. nb);
  3211. unlock:
  3212. rtnl_unlock();
  3213. return err;
  3214. }
  3215. EXPORT_SYMBOL(register_nexthop_notifier);
  3216. int __unregister_nexthop_notifier(struct net *net, struct notifier_block *nb)
  3217. {
  3218. int err;
  3219. err = blocking_notifier_chain_unregister(&net->nexthop.notifier_chain,
  3220. nb);
  3221. if (!err)
  3222. nexthops_dump(net, nb, NEXTHOP_EVENT_DEL, NULL);
  3223. return err;
  3224. }
  3225. EXPORT_SYMBOL(__unregister_nexthop_notifier);
  3226. int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb)
  3227. {
  3228. int err;
  3229. rtnl_lock();
  3230. err = __unregister_nexthop_notifier(net, nb);
  3231. rtnl_unlock();
  3232. return err;
  3233. }
  3234. EXPORT_SYMBOL(unregister_nexthop_notifier);
  3235. void nexthop_set_hw_flags(struct net *net, u32 id, bool offload, bool trap)
  3236. {
  3237. struct nexthop *nexthop;
  3238. rcu_read_lock();
  3239. nexthop = nexthop_find_by_id(net, id);
  3240. if (!nexthop)
  3241. goto out;
  3242. nexthop->nh_flags &= ~(RTNH_F_OFFLOAD | RTNH_F_TRAP);
  3243. if (offload)
  3244. nexthop->nh_flags |= RTNH_F_OFFLOAD;
  3245. if (trap)
  3246. nexthop->nh_flags |= RTNH_F_TRAP;
  3247. out:
  3248. rcu_read_unlock();
  3249. }
  3250. EXPORT_SYMBOL(nexthop_set_hw_flags);
  3251. void nexthop_bucket_set_hw_flags(struct net *net, u32 id, u16 bucket_index,
  3252. bool offload, bool trap)
  3253. {
  3254. struct nh_res_table *res_table;
  3255. struct nh_res_bucket *bucket;
  3256. struct nexthop *nexthop;
  3257. struct nh_group *nhg;
  3258. rcu_read_lock();
  3259. nexthop = nexthop_find_by_id(net, id);
  3260. if (!nexthop || !nexthop->is_group)
  3261. goto out;
  3262. nhg = rcu_dereference(nexthop->nh_grp);
  3263. if (!nhg->resilient)
  3264. goto out;
  3265. if (bucket_index >= nhg->res_table->num_nh_buckets)
  3266. goto out;
  3267. res_table = rcu_dereference(nhg->res_table);
  3268. bucket = &res_table->nh_buckets[bucket_index];
  3269. bucket->nh_flags &= ~(RTNH_F_OFFLOAD | RTNH_F_TRAP);
  3270. if (offload)
  3271. bucket->nh_flags |= RTNH_F_OFFLOAD;
  3272. if (trap)
  3273. bucket->nh_flags |= RTNH_F_TRAP;
  3274. out:
  3275. rcu_read_unlock();
  3276. }
  3277. EXPORT_SYMBOL(nexthop_bucket_set_hw_flags);
  3278. void nexthop_res_grp_activity_update(struct net *net, u32 id, u16 num_buckets,
  3279. unsigned long *activity)
  3280. {
  3281. struct nh_res_table *res_table;
  3282. struct nexthop *nexthop;
  3283. struct nh_group *nhg;
  3284. u16 i;
  3285. rcu_read_lock();
  3286. nexthop = nexthop_find_by_id(net, id);
  3287. if (!nexthop || !nexthop->is_group)
  3288. goto out;
  3289. nhg = rcu_dereference(nexthop->nh_grp);
  3290. if (!nhg->resilient)
  3291. goto out;
  3292. /* Instead of silently ignoring some buckets, demand that the sizes
  3293. * be the same.
  3294. */
  3295. res_table = rcu_dereference(nhg->res_table);
  3296. if (num_buckets != res_table->num_nh_buckets)
  3297. goto out;
  3298. for (i = 0; i < num_buckets; i++) {
  3299. if (test_bit(i, activity))
  3300. nh_res_bucket_set_busy(&res_table->nh_buckets[i]);
  3301. }
  3302. out:
  3303. rcu_read_unlock();
  3304. }
  3305. EXPORT_SYMBOL(nexthop_res_grp_activity_update);
  3306. static void __net_exit nexthop_net_exit_batch_rtnl(struct list_head *net_list,
  3307. struct list_head *dev_to_kill)
  3308. {
  3309. struct net *net;
  3310. ASSERT_RTNL();
  3311. list_for_each_entry(net, net_list, exit_list)
  3312. flush_all_nexthops(net);
  3313. }
  3314. static void __net_exit nexthop_net_exit(struct net *net)
  3315. {
  3316. kfree(net->nexthop.devhash);
  3317. net->nexthop.devhash = NULL;
  3318. }
  3319. static int __net_init nexthop_net_init(struct net *net)
  3320. {
  3321. size_t sz = sizeof(struct hlist_head) * NH_DEV_HASHSIZE;
  3322. net->nexthop.rb_root = RB_ROOT;
  3323. net->nexthop.devhash = kzalloc(sz, GFP_KERNEL);
  3324. if (!net->nexthop.devhash)
  3325. return -ENOMEM;
  3326. BLOCKING_INIT_NOTIFIER_HEAD(&net->nexthop.notifier_chain);
  3327. return 0;
  3328. }
  3329. static struct pernet_operations nexthop_net_ops = {
  3330. .init = nexthop_net_init,
  3331. .exit = nexthop_net_exit,
  3332. .exit_batch_rtnl = nexthop_net_exit_batch_rtnl,
  3333. };
  3334. static int __init nexthop_init(void)
  3335. {
  3336. register_pernet_subsys(&nexthop_net_ops);
  3337. register_netdevice_notifier(&nh_netdev_notifier);
  3338. rtnl_register(PF_UNSPEC, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0);
  3339. rtnl_register(PF_UNSPEC, RTM_DELNEXTHOP, rtm_del_nexthop, NULL, 0);
  3340. rtnl_register(PF_UNSPEC, RTM_GETNEXTHOP, rtm_get_nexthop,
  3341. rtm_dump_nexthop, 0);
  3342. rtnl_register(PF_INET, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0);
  3343. rtnl_register(PF_INET, RTM_GETNEXTHOP, NULL, rtm_dump_nexthop, 0);
  3344. rtnl_register(PF_INET6, RTM_NEWNEXTHOP, rtm_new_nexthop, NULL, 0);
  3345. rtnl_register(PF_INET6, RTM_GETNEXTHOP, NULL, rtm_dump_nexthop, 0);
  3346. rtnl_register(PF_UNSPEC, RTM_GETNEXTHOPBUCKET, rtm_get_nexthop_bucket,
  3347. rtm_dump_nexthop_bucket, 0);
  3348. return 0;
  3349. }
  3350. subsys_initcall(nexthop_init);