cls_api.c 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/sched/cls_api.c Packet classifier API.
  4. *
  5. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  6. *
  7. * Changes:
  8. *
  9. * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support
  10. */
  11. #include <linux/module.h>
  12. #include <linux/types.h>
  13. #include <linux/kernel.h>
  14. #include <linux/string.h>
  15. #include <linux/errno.h>
  16. #include <linux/err.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/init.h>
  19. #include <linux/kmod.h>
  20. #include <linux/slab.h>
  21. #include <linux/idr.h>
  22. #include <linux/jhash.h>
  23. #include <linux/rculist.h>
  24. #include <linux/rhashtable.h>
  25. #include <net/net_namespace.h>
  26. #include <net/sock.h>
  27. #include <net/netlink.h>
  28. #include <net/pkt_sched.h>
  29. #include <net/pkt_cls.h>
  30. #include <net/tc_act/tc_pedit.h>
  31. #include <net/tc_act/tc_mirred.h>
  32. #include <net/tc_act/tc_vlan.h>
  33. #include <net/tc_act/tc_tunnel_key.h>
  34. #include <net/tc_act/tc_csum.h>
  35. #include <net/tc_act/tc_gact.h>
  36. #include <net/tc_act/tc_police.h>
  37. #include <net/tc_act/tc_sample.h>
  38. #include <net/tc_act/tc_skbedit.h>
  39. #include <net/tc_act/tc_ct.h>
  40. #include <net/tc_act/tc_mpls.h>
  41. #include <net/tc_act/tc_gate.h>
  42. #include <net/flow_offload.h>
  43. #include <net/tc_wrapper.h>
  44. /* The list of all installed classifier types */
  45. static LIST_HEAD(tcf_proto_base);
  46. /* Protects list of registered TC modules. It is pure SMP lock. */
  47. static DEFINE_RWLOCK(cls_mod_lock);
  48. static struct xarray tcf_exts_miss_cookies_xa;
  49. struct tcf_exts_miss_cookie_node {
  50. const struct tcf_chain *chain;
  51. const struct tcf_proto *tp;
  52. const struct tcf_exts *exts;
  53. u32 chain_index;
  54. u32 tp_prio;
  55. u32 handle;
  56. u32 miss_cookie_base;
  57. struct rcu_head rcu;
  58. };
  59. /* Each tc action entry cookie will be comprised of 32bit miss_cookie_base +
  60. * action index in the exts tc actions array.
  61. */
  62. union tcf_exts_miss_cookie {
  63. struct {
  64. u32 miss_cookie_base;
  65. u32 act_index;
  66. };
  67. u64 miss_cookie;
  68. };
  69. #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
  70. static int
  71. tcf_exts_miss_cookie_base_alloc(struct tcf_exts *exts, struct tcf_proto *tp,
  72. u32 handle)
  73. {
  74. struct tcf_exts_miss_cookie_node *n;
  75. static u32 next;
  76. int err;
  77. if (WARN_ON(!handle || !tp->ops->get_exts))
  78. return -EINVAL;
  79. n = kzalloc(sizeof(*n), GFP_KERNEL);
  80. if (!n)
  81. return -ENOMEM;
  82. n->chain_index = tp->chain->index;
  83. n->chain = tp->chain;
  84. n->tp_prio = tp->prio;
  85. n->tp = tp;
  86. n->exts = exts;
  87. n->handle = handle;
  88. err = xa_alloc_cyclic(&tcf_exts_miss_cookies_xa, &n->miss_cookie_base,
  89. n, xa_limit_32b, &next, GFP_KERNEL);
  90. if (err)
  91. goto err_xa_alloc;
  92. exts->miss_cookie_node = n;
  93. return 0;
  94. err_xa_alloc:
  95. kfree(n);
  96. return err;
  97. }
  98. static void tcf_exts_miss_cookie_base_destroy(struct tcf_exts *exts)
  99. {
  100. struct tcf_exts_miss_cookie_node *n;
  101. if (!exts->miss_cookie_node)
  102. return;
  103. n = exts->miss_cookie_node;
  104. xa_erase(&tcf_exts_miss_cookies_xa, n->miss_cookie_base);
  105. kfree_rcu(n, rcu);
  106. }
  107. static struct tcf_exts_miss_cookie_node *
  108. tcf_exts_miss_cookie_lookup(u64 miss_cookie, int *act_index)
  109. {
  110. union tcf_exts_miss_cookie mc = { .miss_cookie = miss_cookie, };
  111. *act_index = mc.act_index;
  112. return xa_load(&tcf_exts_miss_cookies_xa, mc.miss_cookie_base);
  113. }
  114. #else /* IS_ENABLED(CONFIG_NET_TC_SKB_EXT) */
  115. static int
  116. tcf_exts_miss_cookie_base_alloc(struct tcf_exts *exts, struct tcf_proto *tp,
  117. u32 handle)
  118. {
  119. return 0;
  120. }
  121. static void tcf_exts_miss_cookie_base_destroy(struct tcf_exts *exts)
  122. {
  123. }
  124. #endif /* IS_ENABLED(CONFIG_NET_TC_SKB_EXT) */
  125. static u64 tcf_exts_miss_cookie_get(u32 miss_cookie_base, int act_index)
  126. {
  127. union tcf_exts_miss_cookie mc = { .act_index = act_index, };
  128. if (!miss_cookie_base)
  129. return 0;
  130. mc.miss_cookie_base = miss_cookie_base;
  131. return mc.miss_cookie;
  132. }
  133. #ifdef CONFIG_NET_CLS_ACT
  134. DEFINE_STATIC_KEY_FALSE(tc_skb_ext_tc);
  135. EXPORT_SYMBOL(tc_skb_ext_tc);
  136. void tc_skb_ext_tc_enable(void)
  137. {
  138. static_branch_inc(&tc_skb_ext_tc);
  139. }
  140. EXPORT_SYMBOL(tc_skb_ext_tc_enable);
  141. void tc_skb_ext_tc_disable(void)
  142. {
  143. static_branch_dec(&tc_skb_ext_tc);
  144. }
  145. EXPORT_SYMBOL(tc_skb_ext_tc_disable);
  146. #endif
  147. static u32 destroy_obj_hashfn(const struct tcf_proto *tp)
  148. {
  149. return jhash_3words(tp->chain->index, tp->prio,
  150. (__force __u32)tp->protocol, 0);
  151. }
  152. static void tcf_proto_signal_destroying(struct tcf_chain *chain,
  153. struct tcf_proto *tp)
  154. {
  155. struct tcf_block *block = chain->block;
  156. mutex_lock(&block->proto_destroy_lock);
  157. hash_add_rcu(block->proto_destroy_ht, &tp->destroy_ht_node,
  158. destroy_obj_hashfn(tp));
  159. mutex_unlock(&block->proto_destroy_lock);
  160. }
  161. static bool tcf_proto_cmp(const struct tcf_proto *tp1,
  162. const struct tcf_proto *tp2)
  163. {
  164. return tp1->chain->index == tp2->chain->index &&
  165. tp1->prio == tp2->prio &&
  166. tp1->protocol == tp2->protocol;
  167. }
  168. static bool tcf_proto_exists_destroying(struct tcf_chain *chain,
  169. struct tcf_proto *tp)
  170. {
  171. u32 hash = destroy_obj_hashfn(tp);
  172. struct tcf_proto *iter;
  173. bool found = false;
  174. rcu_read_lock();
  175. hash_for_each_possible_rcu(chain->block->proto_destroy_ht, iter,
  176. destroy_ht_node, hash) {
  177. if (tcf_proto_cmp(tp, iter)) {
  178. found = true;
  179. break;
  180. }
  181. }
  182. rcu_read_unlock();
  183. return found;
  184. }
  185. static void
  186. tcf_proto_signal_destroyed(struct tcf_chain *chain, struct tcf_proto *tp)
  187. {
  188. struct tcf_block *block = chain->block;
  189. mutex_lock(&block->proto_destroy_lock);
  190. if (hash_hashed(&tp->destroy_ht_node))
  191. hash_del_rcu(&tp->destroy_ht_node);
  192. mutex_unlock(&block->proto_destroy_lock);
  193. }
  194. /* Find classifier type by string name */
  195. static const struct tcf_proto_ops *__tcf_proto_lookup_ops(const char *kind)
  196. {
  197. const struct tcf_proto_ops *t, *res = NULL;
  198. if (kind) {
  199. read_lock(&cls_mod_lock);
  200. list_for_each_entry(t, &tcf_proto_base, head) {
  201. if (strcmp(kind, t->kind) == 0) {
  202. if (try_module_get(t->owner))
  203. res = t;
  204. break;
  205. }
  206. }
  207. read_unlock(&cls_mod_lock);
  208. }
  209. return res;
  210. }
  211. static const struct tcf_proto_ops *
  212. tcf_proto_lookup_ops(const char *kind, bool rtnl_held,
  213. struct netlink_ext_ack *extack)
  214. {
  215. const struct tcf_proto_ops *ops;
  216. ops = __tcf_proto_lookup_ops(kind);
  217. if (ops)
  218. return ops;
  219. #ifdef CONFIG_MODULES
  220. if (rtnl_held)
  221. rtnl_unlock();
  222. request_module(NET_CLS_ALIAS_PREFIX "%s", kind);
  223. if (rtnl_held)
  224. rtnl_lock();
  225. ops = __tcf_proto_lookup_ops(kind);
  226. /* We dropped the RTNL semaphore in order to perform
  227. * the module load. So, even if we succeeded in loading
  228. * the module we have to replay the request. We indicate
  229. * this using -EAGAIN.
  230. */
  231. if (ops) {
  232. module_put(ops->owner);
  233. return ERR_PTR(-EAGAIN);
  234. }
  235. #endif
  236. NL_SET_ERR_MSG(extack, "TC classifier not found");
  237. return ERR_PTR(-ENOENT);
  238. }
  239. /* Register(unregister) new classifier type */
  240. int register_tcf_proto_ops(struct tcf_proto_ops *ops)
  241. {
  242. struct tcf_proto_ops *t;
  243. int rc = -EEXIST;
  244. write_lock(&cls_mod_lock);
  245. list_for_each_entry(t, &tcf_proto_base, head)
  246. if (!strcmp(ops->kind, t->kind))
  247. goto out;
  248. list_add_tail(&ops->head, &tcf_proto_base);
  249. rc = 0;
  250. out:
  251. write_unlock(&cls_mod_lock);
  252. return rc;
  253. }
  254. EXPORT_SYMBOL(register_tcf_proto_ops);
  255. static struct workqueue_struct *tc_filter_wq;
  256. void unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
  257. {
  258. struct tcf_proto_ops *t;
  259. int rc = -ENOENT;
  260. /* Wait for outstanding call_rcu()s, if any, from a
  261. * tcf_proto_ops's destroy() handler.
  262. */
  263. rcu_barrier();
  264. flush_workqueue(tc_filter_wq);
  265. write_lock(&cls_mod_lock);
  266. list_for_each_entry(t, &tcf_proto_base, head) {
  267. if (t == ops) {
  268. list_del(&t->head);
  269. rc = 0;
  270. break;
  271. }
  272. }
  273. write_unlock(&cls_mod_lock);
  274. WARN(rc, "unregister tc filter kind(%s) failed %d\n", ops->kind, rc);
  275. }
  276. EXPORT_SYMBOL(unregister_tcf_proto_ops);
  277. bool tcf_queue_work(struct rcu_work *rwork, work_func_t func)
  278. {
  279. INIT_RCU_WORK(rwork, func);
  280. return queue_rcu_work(tc_filter_wq, rwork);
  281. }
  282. EXPORT_SYMBOL(tcf_queue_work);
  283. /* Select new prio value from the range, managed by kernel. */
  284. static inline u32 tcf_auto_prio(struct tcf_proto *tp)
  285. {
  286. u32 first = TC_H_MAKE(0xC0000000U, 0U);
  287. if (tp)
  288. first = tp->prio - 1;
  289. return TC_H_MAJ(first);
  290. }
  291. static bool tcf_proto_check_kind(struct nlattr *kind, char *name)
  292. {
  293. if (kind)
  294. return nla_strscpy(name, kind, IFNAMSIZ) < 0;
  295. memset(name, 0, IFNAMSIZ);
  296. return false;
  297. }
  298. static bool tcf_proto_is_unlocked(const char *kind)
  299. {
  300. const struct tcf_proto_ops *ops;
  301. bool ret;
  302. if (strlen(kind) == 0)
  303. return false;
  304. ops = tcf_proto_lookup_ops(kind, false, NULL);
  305. /* On error return false to take rtnl lock. Proto lookup/create
  306. * functions will perform lookup again and properly handle errors.
  307. */
  308. if (IS_ERR(ops))
  309. return false;
  310. ret = !!(ops->flags & TCF_PROTO_OPS_DOIT_UNLOCKED);
  311. module_put(ops->owner);
  312. return ret;
  313. }
  314. static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
  315. u32 prio, struct tcf_chain *chain,
  316. bool rtnl_held,
  317. struct netlink_ext_ack *extack)
  318. {
  319. struct tcf_proto *tp;
  320. int err;
  321. tp = kzalloc(sizeof(*tp), GFP_KERNEL);
  322. if (!tp)
  323. return ERR_PTR(-ENOBUFS);
  324. tp->ops = tcf_proto_lookup_ops(kind, rtnl_held, extack);
  325. if (IS_ERR(tp->ops)) {
  326. err = PTR_ERR(tp->ops);
  327. goto errout;
  328. }
  329. tp->classify = tp->ops->classify;
  330. tp->protocol = protocol;
  331. tp->prio = prio;
  332. tp->chain = chain;
  333. tp->usesw = !tp->ops->reoffload;
  334. spin_lock_init(&tp->lock);
  335. refcount_set(&tp->refcnt, 1);
  336. err = tp->ops->init(tp);
  337. if (err) {
  338. module_put(tp->ops->owner);
  339. goto errout;
  340. }
  341. return tp;
  342. errout:
  343. kfree(tp);
  344. return ERR_PTR(err);
  345. }
  346. static void tcf_proto_get(struct tcf_proto *tp)
  347. {
  348. refcount_inc(&tp->refcnt);
  349. }
  350. static void tcf_proto_count_usesw(struct tcf_proto *tp, bool add)
  351. {
  352. #ifdef CONFIG_NET_CLS_ACT
  353. struct tcf_block *block = tp->chain->block;
  354. bool counted = false;
  355. if (!add) {
  356. if (tp->usesw && tp->counted) {
  357. if (!atomic_dec_return(&block->useswcnt))
  358. static_branch_dec(&tcf_sw_enabled_key);
  359. tp->counted = false;
  360. }
  361. return;
  362. }
  363. spin_lock(&tp->lock);
  364. if (tp->usesw && !tp->counted) {
  365. counted = true;
  366. tp->counted = true;
  367. }
  368. spin_unlock(&tp->lock);
  369. if (counted && atomic_inc_return(&block->useswcnt) == 1)
  370. static_branch_inc(&tcf_sw_enabled_key);
  371. #endif
  372. }
  373. static void tcf_chain_put(struct tcf_chain *chain);
  374. static void tcf_proto_destroy(struct tcf_proto *tp, bool rtnl_held,
  375. bool sig_destroy, struct netlink_ext_ack *extack)
  376. {
  377. tp->ops->destroy(tp, rtnl_held, extack);
  378. tcf_proto_count_usesw(tp, false);
  379. if (sig_destroy)
  380. tcf_proto_signal_destroyed(tp->chain, tp);
  381. tcf_chain_put(tp->chain);
  382. module_put(tp->ops->owner);
  383. kfree_rcu(tp, rcu);
  384. }
  385. static void tcf_proto_put(struct tcf_proto *tp, bool rtnl_held,
  386. struct netlink_ext_ack *extack)
  387. {
  388. if (refcount_dec_and_test(&tp->refcnt))
  389. tcf_proto_destroy(tp, rtnl_held, true, extack);
  390. }
  391. static bool tcf_proto_check_delete(struct tcf_proto *tp)
  392. {
  393. if (tp->ops->delete_empty)
  394. return tp->ops->delete_empty(tp);
  395. tp->deleting = true;
  396. return tp->deleting;
  397. }
  398. static void tcf_proto_mark_delete(struct tcf_proto *tp)
  399. {
  400. spin_lock(&tp->lock);
  401. tp->deleting = true;
  402. spin_unlock(&tp->lock);
  403. }
  404. static bool tcf_proto_is_deleting(struct tcf_proto *tp)
  405. {
  406. bool deleting;
  407. spin_lock(&tp->lock);
  408. deleting = tp->deleting;
  409. spin_unlock(&tp->lock);
  410. return deleting;
  411. }
  412. #define ASSERT_BLOCK_LOCKED(block) \
  413. lockdep_assert_held(&(block)->lock)
  414. struct tcf_filter_chain_list_item {
  415. struct list_head list;
  416. tcf_chain_head_change_t *chain_head_change;
  417. void *chain_head_change_priv;
  418. };
  419. static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
  420. u32 chain_index)
  421. {
  422. struct tcf_chain *chain;
  423. ASSERT_BLOCK_LOCKED(block);
  424. chain = kzalloc(sizeof(*chain), GFP_KERNEL);
  425. if (!chain)
  426. return NULL;
  427. list_add_tail_rcu(&chain->list, &block->chain_list);
  428. mutex_init(&chain->filter_chain_lock);
  429. chain->block = block;
  430. chain->index = chain_index;
  431. chain->refcnt = 1;
  432. if (!chain->index)
  433. block->chain0.chain = chain;
  434. return chain;
  435. }
  436. static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
  437. struct tcf_proto *tp_head)
  438. {
  439. if (item->chain_head_change)
  440. item->chain_head_change(tp_head, item->chain_head_change_priv);
  441. }
  442. static void tcf_chain0_head_change(struct tcf_chain *chain,
  443. struct tcf_proto *tp_head)
  444. {
  445. struct tcf_filter_chain_list_item *item;
  446. struct tcf_block *block = chain->block;
  447. if (chain->index)
  448. return;
  449. mutex_lock(&block->lock);
  450. list_for_each_entry(item, &block->chain0.filter_chain_list, list)
  451. tcf_chain_head_change_item(item, tp_head);
  452. mutex_unlock(&block->lock);
  453. }
  454. /* Returns true if block can be safely freed. */
  455. static bool tcf_chain_detach(struct tcf_chain *chain)
  456. {
  457. struct tcf_block *block = chain->block;
  458. ASSERT_BLOCK_LOCKED(block);
  459. list_del_rcu(&chain->list);
  460. if (!chain->index)
  461. block->chain0.chain = NULL;
  462. if (list_empty(&block->chain_list) &&
  463. refcount_read(&block->refcnt) == 0)
  464. return true;
  465. return false;
  466. }
  467. static void tcf_block_destroy(struct tcf_block *block)
  468. {
  469. mutex_destroy(&block->lock);
  470. mutex_destroy(&block->proto_destroy_lock);
  471. xa_destroy(&block->ports);
  472. kfree_rcu(block, rcu);
  473. }
  474. static void tcf_chain_destroy(struct tcf_chain *chain, bool free_block)
  475. {
  476. struct tcf_block *block = chain->block;
  477. mutex_destroy(&chain->filter_chain_lock);
  478. kfree_rcu(chain, rcu);
  479. if (free_block)
  480. tcf_block_destroy(block);
  481. }
  482. static void tcf_chain_hold(struct tcf_chain *chain)
  483. {
  484. ASSERT_BLOCK_LOCKED(chain->block);
  485. ++chain->refcnt;
  486. }
  487. static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain)
  488. {
  489. ASSERT_BLOCK_LOCKED(chain->block);
  490. /* In case all the references are action references, this
  491. * chain should not be shown to the user.
  492. */
  493. return chain->refcnt == chain->action_refcnt;
  494. }
  495. static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block,
  496. u32 chain_index)
  497. {
  498. struct tcf_chain *chain;
  499. ASSERT_BLOCK_LOCKED(block);
  500. list_for_each_entry(chain, &block->chain_list, list) {
  501. if (chain->index == chain_index)
  502. return chain;
  503. }
  504. return NULL;
  505. }
  506. #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
  507. static struct tcf_chain *tcf_chain_lookup_rcu(const struct tcf_block *block,
  508. u32 chain_index)
  509. {
  510. struct tcf_chain *chain;
  511. list_for_each_entry_rcu(chain, &block->chain_list, list) {
  512. if (chain->index == chain_index)
  513. return chain;
  514. }
  515. return NULL;
  516. }
  517. #endif
  518. static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
  519. u32 seq, u16 flags, int event, bool unicast,
  520. struct netlink_ext_ack *extack);
  521. static struct tcf_chain *__tcf_chain_get(struct tcf_block *block,
  522. u32 chain_index, bool create,
  523. bool by_act)
  524. {
  525. struct tcf_chain *chain = NULL;
  526. bool is_first_reference;
  527. mutex_lock(&block->lock);
  528. chain = tcf_chain_lookup(block, chain_index);
  529. if (chain) {
  530. tcf_chain_hold(chain);
  531. } else {
  532. if (!create)
  533. goto errout;
  534. chain = tcf_chain_create(block, chain_index);
  535. if (!chain)
  536. goto errout;
  537. }
  538. if (by_act)
  539. ++chain->action_refcnt;
  540. is_first_reference = chain->refcnt - chain->action_refcnt == 1;
  541. mutex_unlock(&block->lock);
  542. /* Send notification only in case we got the first
  543. * non-action reference. Until then, the chain acts only as
  544. * a placeholder for actions pointing to it and user ought
  545. * not know about them.
  546. */
  547. if (is_first_reference && !by_act)
  548. tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
  549. RTM_NEWCHAIN, false, NULL);
  550. return chain;
  551. errout:
  552. mutex_unlock(&block->lock);
  553. return chain;
  554. }
  555. static struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
  556. bool create)
  557. {
  558. return __tcf_chain_get(block, chain_index, create, false);
  559. }
  560. struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index)
  561. {
  562. return __tcf_chain_get(block, chain_index, true, true);
  563. }
  564. EXPORT_SYMBOL(tcf_chain_get_by_act);
  565. static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
  566. void *tmplt_priv);
  567. static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
  568. void *tmplt_priv, u32 chain_index,
  569. struct tcf_block *block, struct sk_buff *oskb,
  570. u32 seq, u16 flags);
  571. static void __tcf_chain_put(struct tcf_chain *chain, bool by_act,
  572. bool explicitly_created)
  573. {
  574. struct tcf_block *block = chain->block;
  575. const struct tcf_proto_ops *tmplt_ops;
  576. unsigned int refcnt, non_act_refcnt;
  577. bool free_block = false;
  578. void *tmplt_priv;
  579. mutex_lock(&block->lock);
  580. if (explicitly_created) {
  581. if (!chain->explicitly_created) {
  582. mutex_unlock(&block->lock);
  583. return;
  584. }
  585. chain->explicitly_created = false;
  586. }
  587. if (by_act)
  588. chain->action_refcnt--;
  589. /* tc_chain_notify_delete can't be called while holding block lock.
  590. * However, when block is unlocked chain can be changed concurrently, so
  591. * save these to temporary variables.
  592. */
  593. refcnt = --chain->refcnt;
  594. non_act_refcnt = refcnt - chain->action_refcnt;
  595. tmplt_ops = chain->tmplt_ops;
  596. tmplt_priv = chain->tmplt_priv;
  597. if (non_act_refcnt == chain->explicitly_created && !by_act) {
  598. if (non_act_refcnt == 0)
  599. tc_chain_notify_delete(tmplt_ops, tmplt_priv,
  600. chain->index, block, NULL, 0, 0);
  601. /* Last reference to chain, no need to lock. */
  602. chain->flushing = false;
  603. }
  604. if (refcnt == 0)
  605. free_block = tcf_chain_detach(chain);
  606. mutex_unlock(&block->lock);
  607. if (refcnt == 0) {
  608. tc_chain_tmplt_del(tmplt_ops, tmplt_priv);
  609. tcf_chain_destroy(chain, free_block);
  610. }
  611. }
  612. static void tcf_chain_put(struct tcf_chain *chain)
  613. {
  614. __tcf_chain_put(chain, false, false);
  615. }
  616. void tcf_chain_put_by_act(struct tcf_chain *chain)
  617. {
  618. __tcf_chain_put(chain, true, false);
  619. }
  620. EXPORT_SYMBOL(tcf_chain_put_by_act);
  621. static void tcf_chain_put_explicitly_created(struct tcf_chain *chain)
  622. {
  623. __tcf_chain_put(chain, false, true);
  624. }
  625. static void tcf_chain_flush(struct tcf_chain *chain, bool rtnl_held)
  626. {
  627. struct tcf_proto *tp, *tp_next;
  628. mutex_lock(&chain->filter_chain_lock);
  629. tp = tcf_chain_dereference(chain->filter_chain, chain);
  630. while (tp) {
  631. tp_next = rcu_dereference_protected(tp->next, 1);
  632. tcf_proto_signal_destroying(chain, tp);
  633. tp = tp_next;
  634. }
  635. tp = tcf_chain_dereference(chain->filter_chain, chain);
  636. RCU_INIT_POINTER(chain->filter_chain, NULL);
  637. tcf_chain0_head_change(chain, NULL);
  638. chain->flushing = true;
  639. mutex_unlock(&chain->filter_chain_lock);
  640. while (tp) {
  641. tp_next = rcu_dereference_protected(tp->next, 1);
  642. tcf_proto_put(tp, rtnl_held, NULL);
  643. tp = tp_next;
  644. }
  645. }
  646. static int tcf_block_setup(struct tcf_block *block,
  647. struct flow_block_offload *bo);
  648. static void tcf_block_offload_init(struct flow_block_offload *bo,
  649. struct net_device *dev, struct Qdisc *sch,
  650. enum flow_block_command command,
  651. enum flow_block_binder_type binder_type,
  652. struct flow_block *flow_block,
  653. bool shared, struct netlink_ext_ack *extack)
  654. {
  655. bo->net = dev_net(dev);
  656. bo->command = command;
  657. bo->binder_type = binder_type;
  658. bo->block = flow_block;
  659. bo->block_shared = shared;
  660. bo->extack = extack;
  661. bo->sch = sch;
  662. bo->cb_list_head = &flow_block->cb_list;
  663. INIT_LIST_HEAD(&bo->cb_list);
  664. }
  665. static void tcf_block_unbind(struct tcf_block *block,
  666. struct flow_block_offload *bo);
  667. static void tc_block_indr_cleanup(struct flow_block_cb *block_cb)
  668. {
  669. struct tcf_block *block = block_cb->indr.data;
  670. struct net_device *dev = block_cb->indr.dev;
  671. struct Qdisc *sch = block_cb->indr.sch;
  672. struct netlink_ext_ack extack = {};
  673. struct flow_block_offload bo = {};
  674. tcf_block_offload_init(&bo, dev, sch, FLOW_BLOCK_UNBIND,
  675. block_cb->indr.binder_type,
  676. &block->flow_block, tcf_block_shared(block),
  677. &extack);
  678. rtnl_lock();
  679. down_write(&block->cb_lock);
  680. list_del(&block_cb->driver_list);
  681. list_move(&block_cb->list, &bo.cb_list);
  682. tcf_block_unbind(block, &bo);
  683. up_write(&block->cb_lock);
  684. rtnl_unlock();
  685. }
  686. static bool tcf_block_offload_in_use(struct tcf_block *block)
  687. {
  688. return atomic_read(&block->offloadcnt);
  689. }
  690. static int tcf_block_offload_cmd(struct tcf_block *block,
  691. struct net_device *dev, struct Qdisc *sch,
  692. struct tcf_block_ext_info *ei,
  693. enum flow_block_command command,
  694. struct netlink_ext_ack *extack)
  695. {
  696. struct flow_block_offload bo = {};
  697. tcf_block_offload_init(&bo, dev, sch, command, ei->binder_type,
  698. &block->flow_block, tcf_block_shared(block),
  699. extack);
  700. if (dev->netdev_ops->ndo_setup_tc) {
  701. int err;
  702. err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
  703. if (err < 0) {
  704. if (err != -EOPNOTSUPP)
  705. NL_SET_ERR_MSG(extack, "Driver ndo_setup_tc failed");
  706. return err;
  707. }
  708. return tcf_block_setup(block, &bo);
  709. }
  710. flow_indr_dev_setup_offload(dev, sch, TC_SETUP_BLOCK, block, &bo,
  711. tc_block_indr_cleanup);
  712. tcf_block_setup(block, &bo);
  713. return -EOPNOTSUPP;
  714. }
  715. static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
  716. struct tcf_block_ext_info *ei,
  717. struct netlink_ext_ack *extack)
  718. {
  719. struct net_device *dev = q->dev_queue->dev;
  720. int err;
  721. down_write(&block->cb_lock);
  722. /* If tc offload feature is disabled and the block we try to bind
  723. * to already has some offloaded filters, forbid to bind.
  724. */
  725. if (dev->netdev_ops->ndo_setup_tc &&
  726. !tc_can_offload(dev) &&
  727. tcf_block_offload_in_use(block)) {
  728. NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled");
  729. err = -EOPNOTSUPP;
  730. goto err_unlock;
  731. }
  732. err = tcf_block_offload_cmd(block, dev, q, ei, FLOW_BLOCK_BIND, extack);
  733. if (err == -EOPNOTSUPP)
  734. goto no_offload_dev_inc;
  735. if (err)
  736. goto err_unlock;
  737. up_write(&block->cb_lock);
  738. return 0;
  739. no_offload_dev_inc:
  740. if (tcf_block_offload_in_use(block))
  741. goto err_unlock;
  742. err = 0;
  743. block->nooffloaddevcnt++;
  744. err_unlock:
  745. up_write(&block->cb_lock);
  746. return err;
  747. }
  748. static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
  749. struct tcf_block_ext_info *ei)
  750. {
  751. struct net_device *dev = q->dev_queue->dev;
  752. int err;
  753. down_write(&block->cb_lock);
  754. err = tcf_block_offload_cmd(block, dev, q, ei, FLOW_BLOCK_UNBIND, NULL);
  755. if (err == -EOPNOTSUPP)
  756. goto no_offload_dev_dec;
  757. up_write(&block->cb_lock);
  758. return;
  759. no_offload_dev_dec:
  760. WARN_ON(block->nooffloaddevcnt-- == 0);
  761. up_write(&block->cb_lock);
  762. }
  763. static int
  764. tcf_chain0_head_change_cb_add(struct tcf_block *block,
  765. struct tcf_block_ext_info *ei,
  766. struct netlink_ext_ack *extack)
  767. {
  768. struct tcf_filter_chain_list_item *item;
  769. struct tcf_chain *chain0;
  770. item = kmalloc(sizeof(*item), GFP_KERNEL);
  771. if (!item) {
  772. NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
  773. return -ENOMEM;
  774. }
  775. item->chain_head_change = ei->chain_head_change;
  776. item->chain_head_change_priv = ei->chain_head_change_priv;
  777. mutex_lock(&block->lock);
  778. chain0 = block->chain0.chain;
  779. if (chain0)
  780. tcf_chain_hold(chain0);
  781. else
  782. list_add(&item->list, &block->chain0.filter_chain_list);
  783. mutex_unlock(&block->lock);
  784. if (chain0) {
  785. struct tcf_proto *tp_head;
  786. mutex_lock(&chain0->filter_chain_lock);
  787. tp_head = tcf_chain_dereference(chain0->filter_chain, chain0);
  788. if (tp_head)
  789. tcf_chain_head_change_item(item, tp_head);
  790. mutex_lock(&block->lock);
  791. list_add(&item->list, &block->chain0.filter_chain_list);
  792. mutex_unlock(&block->lock);
  793. mutex_unlock(&chain0->filter_chain_lock);
  794. tcf_chain_put(chain0);
  795. }
  796. return 0;
  797. }
  798. static void
  799. tcf_chain0_head_change_cb_del(struct tcf_block *block,
  800. struct tcf_block_ext_info *ei)
  801. {
  802. struct tcf_filter_chain_list_item *item;
  803. mutex_lock(&block->lock);
  804. list_for_each_entry(item, &block->chain0.filter_chain_list, list) {
  805. if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
  806. (item->chain_head_change == ei->chain_head_change &&
  807. item->chain_head_change_priv == ei->chain_head_change_priv)) {
  808. if (block->chain0.chain)
  809. tcf_chain_head_change_item(item, NULL);
  810. list_del(&item->list);
  811. mutex_unlock(&block->lock);
  812. kfree(item);
  813. return;
  814. }
  815. }
  816. mutex_unlock(&block->lock);
  817. WARN_ON(1);
  818. }
  819. struct tcf_net {
  820. spinlock_t idr_lock; /* Protects idr */
  821. struct idr idr;
  822. };
  823. static unsigned int tcf_net_id;
  824. static int tcf_block_insert(struct tcf_block *block, struct net *net,
  825. struct netlink_ext_ack *extack)
  826. {
  827. struct tcf_net *tn = net_generic(net, tcf_net_id);
  828. int err;
  829. idr_preload(GFP_KERNEL);
  830. spin_lock(&tn->idr_lock);
  831. err = idr_alloc_u32(&tn->idr, block, &block->index, block->index,
  832. GFP_NOWAIT);
  833. spin_unlock(&tn->idr_lock);
  834. idr_preload_end();
  835. return err;
  836. }
  837. static void tcf_block_remove(struct tcf_block *block, struct net *net)
  838. {
  839. struct tcf_net *tn = net_generic(net, tcf_net_id);
  840. spin_lock(&tn->idr_lock);
  841. idr_remove(&tn->idr, block->index);
  842. spin_unlock(&tn->idr_lock);
  843. }
  844. static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
  845. u32 block_index,
  846. struct netlink_ext_ack *extack)
  847. {
  848. struct tcf_block *block;
  849. block = kzalloc(sizeof(*block), GFP_KERNEL);
  850. if (!block) {
  851. NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
  852. return ERR_PTR(-ENOMEM);
  853. }
  854. mutex_init(&block->lock);
  855. mutex_init(&block->proto_destroy_lock);
  856. init_rwsem(&block->cb_lock);
  857. flow_block_init(&block->flow_block);
  858. INIT_LIST_HEAD(&block->chain_list);
  859. INIT_LIST_HEAD(&block->owner_list);
  860. INIT_LIST_HEAD(&block->chain0.filter_chain_list);
  861. refcount_set(&block->refcnt, 1);
  862. block->net = net;
  863. block->index = block_index;
  864. xa_init(&block->ports);
  865. /* Don't store q pointer for blocks which are shared */
  866. if (!tcf_block_shared(block))
  867. block->q = q;
  868. return block;
  869. }
  870. struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
  871. {
  872. struct tcf_net *tn = net_generic(net, tcf_net_id);
  873. return idr_find(&tn->idr, block_index);
  874. }
  875. EXPORT_SYMBOL(tcf_block_lookup);
  876. static struct tcf_block *tcf_block_refcnt_get(struct net *net, u32 block_index)
  877. {
  878. struct tcf_block *block;
  879. rcu_read_lock();
  880. block = tcf_block_lookup(net, block_index);
  881. if (block && !refcount_inc_not_zero(&block->refcnt))
  882. block = NULL;
  883. rcu_read_unlock();
  884. return block;
  885. }
  886. static struct tcf_chain *
  887. __tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
  888. {
  889. mutex_lock(&block->lock);
  890. if (chain)
  891. chain = list_is_last(&chain->list, &block->chain_list) ?
  892. NULL : list_next_entry(chain, list);
  893. else
  894. chain = list_first_entry_or_null(&block->chain_list,
  895. struct tcf_chain, list);
  896. /* skip all action-only chains */
  897. while (chain && tcf_chain_held_by_acts_only(chain))
  898. chain = list_is_last(&chain->list, &block->chain_list) ?
  899. NULL : list_next_entry(chain, list);
  900. if (chain)
  901. tcf_chain_hold(chain);
  902. mutex_unlock(&block->lock);
  903. return chain;
  904. }
  905. /* Function to be used by all clients that want to iterate over all chains on
  906. * block. It properly obtains block->lock and takes reference to chain before
  907. * returning it. Users of this function must be tolerant to concurrent chain
  908. * insertion/deletion or ensure that no concurrent chain modification is
  909. * possible. Note that all netlink dump callbacks cannot guarantee to provide
  910. * consistent dump because rtnl lock is released each time skb is filled with
  911. * data and sent to user-space.
  912. */
  913. struct tcf_chain *
  914. tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
  915. {
  916. struct tcf_chain *chain_next = __tcf_get_next_chain(block, chain);
  917. if (chain)
  918. tcf_chain_put(chain);
  919. return chain_next;
  920. }
  921. EXPORT_SYMBOL(tcf_get_next_chain);
  922. static struct tcf_proto *
  923. __tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp)
  924. {
  925. u32 prio = 0;
  926. ASSERT_RTNL();
  927. mutex_lock(&chain->filter_chain_lock);
  928. if (!tp) {
  929. tp = tcf_chain_dereference(chain->filter_chain, chain);
  930. } else if (tcf_proto_is_deleting(tp)) {
  931. /* 'deleting' flag is set and chain->filter_chain_lock was
  932. * unlocked, which means next pointer could be invalid. Restart
  933. * search.
  934. */
  935. prio = tp->prio + 1;
  936. tp = tcf_chain_dereference(chain->filter_chain, chain);
  937. for (; tp; tp = tcf_chain_dereference(tp->next, chain))
  938. if (!tp->deleting && tp->prio >= prio)
  939. break;
  940. } else {
  941. tp = tcf_chain_dereference(tp->next, chain);
  942. }
  943. if (tp)
  944. tcf_proto_get(tp);
  945. mutex_unlock(&chain->filter_chain_lock);
  946. return tp;
  947. }
  948. /* Function to be used by all clients that want to iterate over all tp's on
  949. * chain. Users of this function must be tolerant to concurrent tp
  950. * insertion/deletion or ensure that no concurrent chain modification is
  951. * possible. Note that all netlink dump callbacks cannot guarantee to provide
  952. * consistent dump because rtnl lock is released each time skb is filled with
  953. * data and sent to user-space.
  954. */
  955. struct tcf_proto *
  956. tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp)
  957. {
  958. struct tcf_proto *tp_next = __tcf_get_next_proto(chain, tp);
  959. if (tp)
  960. tcf_proto_put(tp, true, NULL);
  961. return tp_next;
  962. }
  963. EXPORT_SYMBOL(tcf_get_next_proto);
  964. static void tcf_block_flush_all_chains(struct tcf_block *block, bool rtnl_held)
  965. {
  966. struct tcf_chain *chain;
  967. /* Last reference to block. At this point chains cannot be added or
  968. * removed concurrently.
  969. */
  970. for (chain = tcf_get_next_chain(block, NULL);
  971. chain;
  972. chain = tcf_get_next_chain(block, chain)) {
  973. tcf_chain_put_explicitly_created(chain);
  974. tcf_chain_flush(chain, rtnl_held);
  975. }
  976. }
  977. /* Lookup Qdisc and increments its reference counter.
  978. * Set parent, if necessary.
  979. */
  980. static int __tcf_qdisc_find(struct net *net, struct Qdisc **q,
  981. u32 *parent, int ifindex, bool rtnl_held,
  982. struct netlink_ext_ack *extack)
  983. {
  984. const struct Qdisc_class_ops *cops;
  985. struct net_device *dev;
  986. int err = 0;
  987. if (ifindex == TCM_IFINDEX_MAGIC_BLOCK)
  988. return 0;
  989. rcu_read_lock();
  990. /* Find link */
  991. dev = dev_get_by_index_rcu(net, ifindex);
  992. if (!dev) {
  993. rcu_read_unlock();
  994. return -ENODEV;
  995. }
  996. /* Find qdisc */
  997. if (!*parent) {
  998. *q = rcu_dereference(dev->qdisc);
  999. *parent = (*q)->handle;
  1000. } else {
  1001. *q = qdisc_lookup_rcu(dev, TC_H_MAJ(*parent));
  1002. if (!*q) {
  1003. NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
  1004. err = -EINVAL;
  1005. goto errout_rcu;
  1006. }
  1007. }
  1008. *q = qdisc_refcount_inc_nz(*q);
  1009. if (!*q) {
  1010. NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
  1011. err = -EINVAL;
  1012. goto errout_rcu;
  1013. }
  1014. /* Is it classful? */
  1015. cops = (*q)->ops->cl_ops;
  1016. if (!cops) {
  1017. NL_SET_ERR_MSG(extack, "Qdisc not classful");
  1018. err = -EINVAL;
  1019. goto errout_qdisc;
  1020. }
  1021. if (!cops->tcf_block) {
  1022. NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
  1023. err = -EOPNOTSUPP;
  1024. goto errout_qdisc;
  1025. }
  1026. errout_rcu:
  1027. /* At this point we know that qdisc is not noop_qdisc,
  1028. * which means that qdisc holds a reference to net_device
  1029. * and we hold a reference to qdisc, so it is safe to release
  1030. * rcu read lock.
  1031. */
  1032. rcu_read_unlock();
  1033. return err;
  1034. errout_qdisc:
  1035. rcu_read_unlock();
  1036. if (rtnl_held)
  1037. qdisc_put(*q);
  1038. else
  1039. qdisc_put_unlocked(*q);
  1040. *q = NULL;
  1041. return err;
  1042. }
  1043. static int __tcf_qdisc_cl_find(struct Qdisc *q, u32 parent, unsigned long *cl,
  1044. int ifindex, struct netlink_ext_ack *extack)
  1045. {
  1046. if (ifindex == TCM_IFINDEX_MAGIC_BLOCK)
  1047. return 0;
  1048. /* Do we search for filter, attached to class? */
  1049. if (TC_H_MIN(parent)) {
  1050. const struct Qdisc_class_ops *cops = q->ops->cl_ops;
  1051. *cl = cops->find(q, parent);
  1052. if (*cl == 0) {
  1053. NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
  1054. return -ENOENT;
  1055. }
  1056. }
  1057. return 0;
  1058. }
  1059. static struct tcf_block *__tcf_block_find(struct net *net, struct Qdisc *q,
  1060. unsigned long cl, int ifindex,
  1061. u32 block_index,
  1062. struct netlink_ext_ack *extack)
  1063. {
  1064. struct tcf_block *block;
  1065. if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
  1066. block = tcf_block_refcnt_get(net, block_index);
  1067. if (!block) {
  1068. NL_SET_ERR_MSG(extack, "Block of given index was not found");
  1069. return ERR_PTR(-EINVAL);
  1070. }
  1071. } else {
  1072. const struct Qdisc_class_ops *cops = q->ops->cl_ops;
  1073. block = cops->tcf_block(q, cl, extack);
  1074. if (!block)
  1075. return ERR_PTR(-EINVAL);
  1076. if (tcf_block_shared(block)) {
  1077. NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
  1078. return ERR_PTR(-EOPNOTSUPP);
  1079. }
  1080. /* Always take reference to block in order to support execution
  1081. * of rules update path of cls API without rtnl lock. Caller
  1082. * must release block when it is finished using it. 'if' block
  1083. * of this conditional obtain reference to block by calling
  1084. * tcf_block_refcnt_get().
  1085. */
  1086. refcount_inc(&block->refcnt);
  1087. }
  1088. return block;
  1089. }
  1090. static void __tcf_block_put(struct tcf_block *block, struct Qdisc *q,
  1091. struct tcf_block_ext_info *ei, bool rtnl_held)
  1092. {
  1093. if (refcount_dec_and_mutex_lock(&block->refcnt, &block->lock)) {
  1094. /* Flushing/putting all chains will cause the block to be
  1095. * deallocated when last chain is freed. However, if chain_list
  1096. * is empty, block has to be manually deallocated. After block
  1097. * reference counter reached 0, it is no longer possible to
  1098. * increment it or add new chains to block.
  1099. */
  1100. bool free_block = list_empty(&block->chain_list);
  1101. mutex_unlock(&block->lock);
  1102. if (tcf_block_shared(block))
  1103. tcf_block_remove(block, block->net);
  1104. if (q)
  1105. tcf_block_offload_unbind(block, q, ei);
  1106. if (free_block)
  1107. tcf_block_destroy(block);
  1108. else
  1109. tcf_block_flush_all_chains(block, rtnl_held);
  1110. } else if (q) {
  1111. tcf_block_offload_unbind(block, q, ei);
  1112. }
  1113. }
  1114. static void tcf_block_refcnt_put(struct tcf_block *block, bool rtnl_held)
  1115. {
  1116. __tcf_block_put(block, NULL, NULL, rtnl_held);
  1117. }
  1118. /* Find tcf block.
  1119. * Set q, parent, cl when appropriate.
  1120. */
  1121. static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
  1122. u32 *parent, unsigned long *cl,
  1123. int ifindex, u32 block_index,
  1124. struct netlink_ext_ack *extack)
  1125. {
  1126. struct tcf_block *block;
  1127. int err = 0;
  1128. ASSERT_RTNL();
  1129. err = __tcf_qdisc_find(net, q, parent, ifindex, true, extack);
  1130. if (err)
  1131. goto errout;
  1132. err = __tcf_qdisc_cl_find(*q, *parent, cl, ifindex, extack);
  1133. if (err)
  1134. goto errout_qdisc;
  1135. block = __tcf_block_find(net, *q, *cl, ifindex, block_index, extack);
  1136. if (IS_ERR(block)) {
  1137. err = PTR_ERR(block);
  1138. goto errout_qdisc;
  1139. }
  1140. return block;
  1141. errout_qdisc:
  1142. if (*q)
  1143. qdisc_put(*q);
  1144. errout:
  1145. *q = NULL;
  1146. return ERR_PTR(err);
  1147. }
  1148. static void tcf_block_release(struct Qdisc *q, struct tcf_block *block,
  1149. bool rtnl_held)
  1150. {
  1151. if (!IS_ERR_OR_NULL(block))
  1152. tcf_block_refcnt_put(block, rtnl_held);
  1153. if (q) {
  1154. if (rtnl_held)
  1155. qdisc_put(q);
  1156. else
  1157. qdisc_put_unlocked(q);
  1158. }
  1159. }
  1160. struct tcf_block_owner_item {
  1161. struct list_head list;
  1162. struct Qdisc *q;
  1163. enum flow_block_binder_type binder_type;
  1164. };
  1165. static void
  1166. tcf_block_owner_netif_keep_dst(struct tcf_block *block,
  1167. struct Qdisc *q,
  1168. enum flow_block_binder_type binder_type)
  1169. {
  1170. if (block->keep_dst &&
  1171. binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
  1172. binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
  1173. netif_keep_dst(qdisc_dev(q));
  1174. }
  1175. void tcf_block_netif_keep_dst(struct tcf_block *block)
  1176. {
  1177. struct tcf_block_owner_item *item;
  1178. block->keep_dst = true;
  1179. list_for_each_entry(item, &block->owner_list, list)
  1180. tcf_block_owner_netif_keep_dst(block, item->q,
  1181. item->binder_type);
  1182. }
  1183. EXPORT_SYMBOL(tcf_block_netif_keep_dst);
  1184. static int tcf_block_owner_add(struct tcf_block *block,
  1185. struct Qdisc *q,
  1186. enum flow_block_binder_type binder_type)
  1187. {
  1188. struct tcf_block_owner_item *item;
  1189. item = kmalloc(sizeof(*item), GFP_KERNEL);
  1190. if (!item)
  1191. return -ENOMEM;
  1192. item->q = q;
  1193. item->binder_type = binder_type;
  1194. list_add(&item->list, &block->owner_list);
  1195. return 0;
  1196. }
  1197. static void tcf_block_owner_del(struct tcf_block *block,
  1198. struct Qdisc *q,
  1199. enum flow_block_binder_type binder_type)
  1200. {
  1201. struct tcf_block_owner_item *item;
  1202. list_for_each_entry(item, &block->owner_list, list) {
  1203. if (item->q == q && item->binder_type == binder_type) {
  1204. list_del(&item->list);
  1205. kfree(item);
  1206. return;
  1207. }
  1208. }
  1209. WARN_ON(1);
  1210. }
  1211. static bool tcf_block_tracks_dev(struct tcf_block *block,
  1212. struct tcf_block_ext_info *ei)
  1213. {
  1214. return tcf_block_shared(block) &&
  1215. (ei->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS ||
  1216. ei->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS);
  1217. }
  1218. int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
  1219. struct tcf_block_ext_info *ei,
  1220. struct netlink_ext_ack *extack)
  1221. {
  1222. struct net_device *dev = qdisc_dev(q);
  1223. struct net *net = qdisc_net(q);
  1224. struct tcf_block *block = NULL;
  1225. int err;
  1226. if (ei->block_index)
  1227. /* block_index not 0 means the shared block is requested */
  1228. block = tcf_block_refcnt_get(net, ei->block_index);
  1229. if (!block) {
  1230. block = tcf_block_create(net, q, ei->block_index, extack);
  1231. if (IS_ERR(block))
  1232. return PTR_ERR(block);
  1233. if (tcf_block_shared(block)) {
  1234. err = tcf_block_insert(block, net, extack);
  1235. if (err)
  1236. goto err_block_insert;
  1237. }
  1238. }
  1239. err = tcf_block_owner_add(block, q, ei->binder_type);
  1240. if (err)
  1241. goto err_block_owner_add;
  1242. tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
  1243. err = tcf_chain0_head_change_cb_add(block, ei, extack);
  1244. if (err)
  1245. goto err_chain0_head_change_cb_add;
  1246. err = tcf_block_offload_bind(block, q, ei, extack);
  1247. if (err)
  1248. goto err_block_offload_bind;
  1249. if (tcf_block_tracks_dev(block, ei)) {
  1250. err = xa_insert(&block->ports, dev->ifindex, dev, GFP_KERNEL);
  1251. if (err) {
  1252. NL_SET_ERR_MSG(extack, "block dev insert failed");
  1253. goto err_dev_insert;
  1254. }
  1255. }
  1256. *p_block = block;
  1257. return 0;
  1258. err_dev_insert:
  1259. tcf_block_offload_unbind(block, q, ei);
  1260. err_block_offload_bind:
  1261. tcf_chain0_head_change_cb_del(block, ei);
  1262. err_chain0_head_change_cb_add:
  1263. tcf_block_owner_del(block, q, ei->binder_type);
  1264. err_block_owner_add:
  1265. err_block_insert:
  1266. tcf_block_refcnt_put(block, true);
  1267. return err;
  1268. }
  1269. EXPORT_SYMBOL(tcf_block_get_ext);
  1270. static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
  1271. {
  1272. struct tcf_proto __rcu **p_filter_chain = priv;
  1273. rcu_assign_pointer(*p_filter_chain, tp_head);
  1274. }
  1275. int tcf_block_get(struct tcf_block **p_block,
  1276. struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
  1277. struct netlink_ext_ack *extack)
  1278. {
  1279. struct tcf_block_ext_info ei = {
  1280. .chain_head_change = tcf_chain_head_change_dflt,
  1281. .chain_head_change_priv = p_filter_chain,
  1282. };
  1283. WARN_ON(!p_filter_chain);
  1284. return tcf_block_get_ext(p_block, q, &ei, extack);
  1285. }
  1286. EXPORT_SYMBOL(tcf_block_get);
  1287. /* XXX: Standalone actions are not allowed to jump to any chain, and bound
  1288. * actions should be all removed after flushing.
  1289. */
  1290. void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
  1291. struct tcf_block_ext_info *ei)
  1292. {
  1293. struct net_device *dev = qdisc_dev(q);
  1294. if (!block)
  1295. return;
  1296. if (tcf_block_tracks_dev(block, ei))
  1297. xa_erase(&block->ports, dev->ifindex);
  1298. tcf_chain0_head_change_cb_del(block, ei);
  1299. tcf_block_owner_del(block, q, ei->binder_type);
  1300. __tcf_block_put(block, q, ei, true);
  1301. }
  1302. EXPORT_SYMBOL(tcf_block_put_ext);
  1303. void tcf_block_put(struct tcf_block *block)
  1304. {
  1305. struct tcf_block_ext_info ei = {0, };
  1306. if (!block)
  1307. return;
  1308. tcf_block_put_ext(block, block->q, &ei);
  1309. }
  1310. EXPORT_SYMBOL(tcf_block_put);
  1311. static int
  1312. tcf_block_playback_offloads(struct tcf_block *block, flow_setup_cb_t *cb,
  1313. void *cb_priv, bool add, bool offload_in_use,
  1314. struct netlink_ext_ack *extack)
  1315. {
  1316. struct tcf_chain *chain, *chain_prev;
  1317. struct tcf_proto *tp, *tp_prev;
  1318. int err;
  1319. lockdep_assert_held(&block->cb_lock);
  1320. for (chain = __tcf_get_next_chain(block, NULL);
  1321. chain;
  1322. chain_prev = chain,
  1323. chain = __tcf_get_next_chain(block, chain),
  1324. tcf_chain_put(chain_prev)) {
  1325. if (chain->tmplt_ops && add)
  1326. chain->tmplt_ops->tmplt_reoffload(chain, true, cb,
  1327. cb_priv);
  1328. for (tp = __tcf_get_next_proto(chain, NULL); tp;
  1329. tp_prev = tp,
  1330. tp = __tcf_get_next_proto(chain, tp),
  1331. tcf_proto_put(tp_prev, true, NULL)) {
  1332. if (tp->ops->reoffload) {
  1333. err = tp->ops->reoffload(tp, add, cb, cb_priv,
  1334. extack);
  1335. if (err && add)
  1336. goto err_playback_remove;
  1337. } else if (add && offload_in_use) {
  1338. err = -EOPNOTSUPP;
  1339. NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
  1340. goto err_playback_remove;
  1341. }
  1342. }
  1343. if (chain->tmplt_ops && !add)
  1344. chain->tmplt_ops->tmplt_reoffload(chain, false, cb,
  1345. cb_priv);
  1346. }
  1347. return 0;
  1348. err_playback_remove:
  1349. tcf_proto_put(tp, true, NULL);
  1350. tcf_chain_put(chain);
  1351. tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
  1352. extack);
  1353. return err;
  1354. }
  1355. static int tcf_block_bind(struct tcf_block *block,
  1356. struct flow_block_offload *bo)
  1357. {
  1358. struct flow_block_cb *block_cb, *next;
  1359. int err, i = 0;
  1360. lockdep_assert_held(&block->cb_lock);
  1361. list_for_each_entry(block_cb, &bo->cb_list, list) {
  1362. err = tcf_block_playback_offloads(block, block_cb->cb,
  1363. block_cb->cb_priv, true,
  1364. tcf_block_offload_in_use(block),
  1365. bo->extack);
  1366. if (err)
  1367. goto err_unroll;
  1368. if (!bo->unlocked_driver_cb)
  1369. block->lockeddevcnt++;
  1370. i++;
  1371. }
  1372. list_splice(&bo->cb_list, &block->flow_block.cb_list);
  1373. return 0;
  1374. err_unroll:
  1375. list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) {
  1376. list_del(&block_cb->driver_list);
  1377. if (i-- > 0) {
  1378. list_del(&block_cb->list);
  1379. tcf_block_playback_offloads(block, block_cb->cb,
  1380. block_cb->cb_priv, false,
  1381. tcf_block_offload_in_use(block),
  1382. NULL);
  1383. if (!bo->unlocked_driver_cb)
  1384. block->lockeddevcnt--;
  1385. }
  1386. flow_block_cb_free(block_cb);
  1387. }
  1388. return err;
  1389. }
  1390. static void tcf_block_unbind(struct tcf_block *block,
  1391. struct flow_block_offload *bo)
  1392. {
  1393. struct flow_block_cb *block_cb, *next;
  1394. lockdep_assert_held(&block->cb_lock);
  1395. list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) {
  1396. tcf_block_playback_offloads(block, block_cb->cb,
  1397. block_cb->cb_priv, false,
  1398. tcf_block_offload_in_use(block),
  1399. NULL);
  1400. list_del(&block_cb->list);
  1401. flow_block_cb_free(block_cb);
  1402. if (!bo->unlocked_driver_cb)
  1403. block->lockeddevcnt--;
  1404. }
  1405. }
  1406. static int tcf_block_setup(struct tcf_block *block,
  1407. struct flow_block_offload *bo)
  1408. {
  1409. int err;
  1410. switch (bo->command) {
  1411. case FLOW_BLOCK_BIND:
  1412. err = tcf_block_bind(block, bo);
  1413. break;
  1414. case FLOW_BLOCK_UNBIND:
  1415. err = 0;
  1416. tcf_block_unbind(block, bo);
  1417. break;
  1418. default:
  1419. WARN_ON_ONCE(1);
  1420. err = -EOPNOTSUPP;
  1421. }
  1422. return err;
  1423. }
  1424. /* Main classifier routine: scans classifier chain attached
  1425. * to this qdisc, (optionally) tests for protocol and asks
  1426. * specific classifiers.
  1427. */
  1428. static inline int __tcf_classify(struct sk_buff *skb,
  1429. const struct tcf_proto *tp,
  1430. const struct tcf_proto *orig_tp,
  1431. struct tcf_result *res,
  1432. bool compat_mode,
  1433. struct tcf_exts_miss_cookie_node *n,
  1434. int act_index,
  1435. u32 *last_executed_chain)
  1436. {
  1437. #ifdef CONFIG_NET_CLS_ACT
  1438. const int max_reclassify_loop = 16;
  1439. const struct tcf_proto *first_tp;
  1440. int limit = 0;
  1441. reclassify:
  1442. #endif
  1443. for (; tp; tp = rcu_dereference_bh(tp->next)) {
  1444. __be16 protocol = skb_protocol(skb, false);
  1445. int err = 0;
  1446. if (n) {
  1447. struct tcf_exts *exts;
  1448. if (n->tp_prio != tp->prio)
  1449. continue;
  1450. /* We re-lookup the tp and chain based on index instead
  1451. * of having hard refs and locks to them, so do a sanity
  1452. * check if any of tp,chain,exts was replaced by the
  1453. * time we got here with a cookie from hardware.
  1454. */
  1455. if (unlikely(n->tp != tp || n->tp->chain != n->chain ||
  1456. !tp->ops->get_exts)) {
  1457. tcf_set_drop_reason(skb,
  1458. SKB_DROP_REASON_TC_COOKIE_ERROR);
  1459. return TC_ACT_SHOT;
  1460. }
  1461. exts = tp->ops->get_exts(tp, n->handle);
  1462. if (unlikely(!exts || n->exts != exts)) {
  1463. tcf_set_drop_reason(skb,
  1464. SKB_DROP_REASON_TC_COOKIE_ERROR);
  1465. return TC_ACT_SHOT;
  1466. }
  1467. n = NULL;
  1468. err = tcf_exts_exec_ex(skb, exts, act_index, res);
  1469. } else {
  1470. if (tp->protocol != protocol &&
  1471. tp->protocol != htons(ETH_P_ALL))
  1472. continue;
  1473. err = tc_classify(skb, tp, res);
  1474. }
  1475. #ifdef CONFIG_NET_CLS_ACT
  1476. if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
  1477. first_tp = orig_tp;
  1478. *last_executed_chain = first_tp->chain->index;
  1479. goto reset;
  1480. } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
  1481. first_tp = res->goto_tp;
  1482. *last_executed_chain = err & TC_ACT_EXT_VAL_MASK;
  1483. goto reset;
  1484. }
  1485. #endif
  1486. if (err >= 0)
  1487. return err;
  1488. }
  1489. if (unlikely(n)) {
  1490. tcf_set_drop_reason(skb,
  1491. SKB_DROP_REASON_TC_COOKIE_ERROR);
  1492. return TC_ACT_SHOT;
  1493. }
  1494. return TC_ACT_UNSPEC; /* signal: continue lookup */
  1495. #ifdef CONFIG_NET_CLS_ACT
  1496. reset:
  1497. if (unlikely(limit++ >= max_reclassify_loop)) {
  1498. net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
  1499. tp->chain->block->index,
  1500. tp->prio & 0xffff,
  1501. ntohs(tp->protocol));
  1502. tcf_set_drop_reason(skb,
  1503. SKB_DROP_REASON_TC_RECLASSIFY_LOOP);
  1504. return TC_ACT_SHOT;
  1505. }
  1506. tp = first_tp;
  1507. goto reclassify;
  1508. #endif
  1509. }
  1510. int tcf_classify(struct sk_buff *skb,
  1511. const struct tcf_block *block,
  1512. const struct tcf_proto *tp,
  1513. struct tcf_result *res, bool compat_mode)
  1514. {
  1515. #if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
  1516. u32 last_executed_chain = 0;
  1517. return __tcf_classify(skb, tp, tp, res, compat_mode, NULL, 0,
  1518. &last_executed_chain);
  1519. #else
  1520. u32 last_executed_chain = tp ? tp->chain->index : 0;
  1521. struct tcf_exts_miss_cookie_node *n = NULL;
  1522. const struct tcf_proto *orig_tp = tp;
  1523. struct tc_skb_ext *ext;
  1524. int act_index = 0;
  1525. int ret;
  1526. if (block) {
  1527. ext = skb_ext_find(skb, TC_SKB_EXT);
  1528. if (ext && (ext->chain || ext->act_miss)) {
  1529. struct tcf_chain *fchain;
  1530. u32 chain;
  1531. if (ext->act_miss) {
  1532. n = tcf_exts_miss_cookie_lookup(ext->act_miss_cookie,
  1533. &act_index);
  1534. if (!n) {
  1535. tcf_set_drop_reason(skb,
  1536. SKB_DROP_REASON_TC_COOKIE_ERROR);
  1537. return TC_ACT_SHOT;
  1538. }
  1539. chain = n->chain_index;
  1540. } else {
  1541. chain = ext->chain;
  1542. }
  1543. fchain = tcf_chain_lookup_rcu(block, chain);
  1544. if (!fchain) {
  1545. tcf_set_drop_reason(skb,
  1546. SKB_DROP_REASON_TC_CHAIN_NOTFOUND);
  1547. return TC_ACT_SHOT;
  1548. }
  1549. /* Consume, so cloned/redirect skbs won't inherit ext */
  1550. skb_ext_del(skb, TC_SKB_EXT);
  1551. tp = rcu_dereference_bh(fchain->filter_chain);
  1552. last_executed_chain = fchain->index;
  1553. }
  1554. }
  1555. ret = __tcf_classify(skb, tp, orig_tp, res, compat_mode, n, act_index,
  1556. &last_executed_chain);
  1557. if (tc_skb_ext_tc_enabled()) {
  1558. /* If we missed on some chain */
  1559. if (ret == TC_ACT_UNSPEC && last_executed_chain) {
  1560. struct tc_skb_cb *cb = tc_skb_cb(skb);
  1561. ext = tc_skb_ext_alloc(skb);
  1562. if (WARN_ON_ONCE(!ext)) {
  1563. tcf_set_drop_reason(skb, SKB_DROP_REASON_NOMEM);
  1564. return TC_ACT_SHOT;
  1565. }
  1566. ext->chain = last_executed_chain;
  1567. ext->mru = cb->mru;
  1568. ext->post_ct = cb->post_ct;
  1569. ext->post_ct_snat = cb->post_ct_snat;
  1570. ext->post_ct_dnat = cb->post_ct_dnat;
  1571. ext->zone = cb->zone;
  1572. }
  1573. }
  1574. return ret;
  1575. #endif
  1576. }
  1577. EXPORT_SYMBOL(tcf_classify);
  1578. struct tcf_chain_info {
  1579. struct tcf_proto __rcu **pprev;
  1580. struct tcf_proto __rcu *next;
  1581. };
  1582. static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain *chain,
  1583. struct tcf_chain_info *chain_info)
  1584. {
  1585. return tcf_chain_dereference(*chain_info->pprev, chain);
  1586. }
  1587. static int tcf_chain_tp_insert(struct tcf_chain *chain,
  1588. struct tcf_chain_info *chain_info,
  1589. struct tcf_proto *tp)
  1590. {
  1591. if (chain->flushing)
  1592. return -EAGAIN;
  1593. RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain, chain_info));
  1594. if (*chain_info->pprev == chain->filter_chain)
  1595. tcf_chain0_head_change(chain, tp);
  1596. tcf_proto_get(tp);
  1597. rcu_assign_pointer(*chain_info->pprev, tp);
  1598. return 0;
  1599. }
  1600. static void tcf_chain_tp_remove(struct tcf_chain *chain,
  1601. struct tcf_chain_info *chain_info,
  1602. struct tcf_proto *tp)
  1603. {
  1604. struct tcf_proto *next = tcf_chain_dereference(chain_info->next, chain);
  1605. tcf_proto_mark_delete(tp);
  1606. if (tp == chain->filter_chain)
  1607. tcf_chain0_head_change(chain, next);
  1608. RCU_INIT_POINTER(*chain_info->pprev, next);
  1609. }
  1610. static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
  1611. struct tcf_chain_info *chain_info,
  1612. u32 protocol, u32 prio,
  1613. bool prio_allocate);
  1614. /* Try to insert new proto.
  1615. * If proto with specified priority already exists, free new proto
  1616. * and return existing one.
  1617. */
  1618. static struct tcf_proto *tcf_chain_tp_insert_unique(struct tcf_chain *chain,
  1619. struct tcf_proto *tp_new,
  1620. u32 protocol, u32 prio,
  1621. bool rtnl_held)
  1622. {
  1623. struct tcf_chain_info chain_info;
  1624. struct tcf_proto *tp;
  1625. int err = 0;
  1626. mutex_lock(&chain->filter_chain_lock);
  1627. if (tcf_proto_exists_destroying(chain, tp_new)) {
  1628. mutex_unlock(&chain->filter_chain_lock);
  1629. tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
  1630. return ERR_PTR(-EAGAIN);
  1631. }
  1632. tp = tcf_chain_tp_find(chain, &chain_info,
  1633. protocol, prio, false);
  1634. if (!tp)
  1635. err = tcf_chain_tp_insert(chain, &chain_info, tp_new);
  1636. mutex_unlock(&chain->filter_chain_lock);
  1637. if (tp) {
  1638. tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
  1639. tp_new = tp;
  1640. } else if (err) {
  1641. tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
  1642. tp_new = ERR_PTR(err);
  1643. }
  1644. return tp_new;
  1645. }
  1646. static void tcf_chain_tp_delete_empty(struct tcf_chain *chain,
  1647. struct tcf_proto *tp, bool rtnl_held,
  1648. struct netlink_ext_ack *extack)
  1649. {
  1650. struct tcf_chain_info chain_info;
  1651. struct tcf_proto *tp_iter;
  1652. struct tcf_proto **pprev;
  1653. struct tcf_proto *next;
  1654. mutex_lock(&chain->filter_chain_lock);
  1655. /* Atomically find and remove tp from chain. */
  1656. for (pprev = &chain->filter_chain;
  1657. (tp_iter = tcf_chain_dereference(*pprev, chain));
  1658. pprev = &tp_iter->next) {
  1659. if (tp_iter == tp) {
  1660. chain_info.pprev = pprev;
  1661. chain_info.next = tp_iter->next;
  1662. WARN_ON(tp_iter->deleting);
  1663. break;
  1664. }
  1665. }
  1666. /* Verify that tp still exists and no new filters were inserted
  1667. * concurrently.
  1668. * Mark tp for deletion if it is empty.
  1669. */
  1670. if (!tp_iter || !tcf_proto_check_delete(tp)) {
  1671. mutex_unlock(&chain->filter_chain_lock);
  1672. return;
  1673. }
  1674. tcf_proto_signal_destroying(chain, tp);
  1675. next = tcf_chain_dereference(chain_info.next, chain);
  1676. if (tp == chain->filter_chain)
  1677. tcf_chain0_head_change(chain, next);
  1678. RCU_INIT_POINTER(*chain_info.pprev, next);
  1679. mutex_unlock(&chain->filter_chain_lock);
  1680. tcf_proto_put(tp, rtnl_held, extack);
  1681. }
  1682. static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
  1683. struct tcf_chain_info *chain_info,
  1684. u32 protocol, u32 prio,
  1685. bool prio_allocate)
  1686. {
  1687. struct tcf_proto **pprev;
  1688. struct tcf_proto *tp;
  1689. /* Check the chain for existence of proto-tcf with this priority */
  1690. for (pprev = &chain->filter_chain;
  1691. (tp = tcf_chain_dereference(*pprev, chain));
  1692. pprev = &tp->next) {
  1693. if (tp->prio >= prio) {
  1694. if (tp->prio == prio) {
  1695. if (prio_allocate ||
  1696. (tp->protocol != protocol && protocol))
  1697. return ERR_PTR(-EINVAL);
  1698. } else {
  1699. tp = NULL;
  1700. }
  1701. break;
  1702. }
  1703. }
  1704. chain_info->pprev = pprev;
  1705. if (tp) {
  1706. chain_info->next = tp->next;
  1707. tcf_proto_get(tp);
  1708. } else {
  1709. chain_info->next = NULL;
  1710. }
  1711. return tp;
  1712. }
  1713. static int tcf_fill_node(struct net *net, struct sk_buff *skb,
  1714. struct tcf_proto *tp, struct tcf_block *block,
  1715. struct Qdisc *q, u32 parent, void *fh,
  1716. u32 portid, u32 seq, u16 flags, int event,
  1717. bool terse_dump, bool rtnl_held,
  1718. struct netlink_ext_ack *extack)
  1719. {
  1720. struct tcmsg *tcm;
  1721. struct nlmsghdr *nlh;
  1722. unsigned char *b = skb_tail_pointer(skb);
  1723. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
  1724. if (!nlh)
  1725. goto out_nlmsg_trim;
  1726. tcm = nlmsg_data(nlh);
  1727. tcm->tcm_family = AF_UNSPEC;
  1728. tcm->tcm__pad1 = 0;
  1729. tcm->tcm__pad2 = 0;
  1730. if (q) {
  1731. tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
  1732. tcm->tcm_parent = parent;
  1733. } else {
  1734. tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
  1735. tcm->tcm_block_index = block->index;
  1736. }
  1737. tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
  1738. if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
  1739. goto nla_put_failure;
  1740. if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
  1741. goto nla_put_failure;
  1742. if (!fh) {
  1743. tcm->tcm_handle = 0;
  1744. } else if (terse_dump) {
  1745. if (tp->ops->terse_dump) {
  1746. if (tp->ops->terse_dump(net, tp, fh, skb, tcm,
  1747. rtnl_held) < 0)
  1748. goto nla_put_failure;
  1749. } else {
  1750. goto cls_op_not_supp;
  1751. }
  1752. } else {
  1753. if (tp->ops->dump &&
  1754. tp->ops->dump(net, tp, fh, skb, tcm, rtnl_held) < 0)
  1755. goto nla_put_failure;
  1756. }
  1757. if (extack && extack->_msg &&
  1758. nla_put_string(skb, TCA_EXT_WARN_MSG, extack->_msg))
  1759. goto nla_put_failure;
  1760. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  1761. return skb->len;
  1762. out_nlmsg_trim:
  1763. nla_put_failure:
  1764. cls_op_not_supp:
  1765. nlmsg_trim(skb, b);
  1766. return -1;
  1767. }
  1768. static int tfilter_notify(struct net *net, struct sk_buff *oskb,
  1769. struct nlmsghdr *n, struct tcf_proto *tp,
  1770. struct tcf_block *block, struct Qdisc *q,
  1771. u32 parent, void *fh, int event, bool unicast,
  1772. bool rtnl_held, struct netlink_ext_ack *extack)
  1773. {
  1774. struct sk_buff *skb;
  1775. u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
  1776. int err = 0;
  1777. if (!unicast && !rtnl_notify_needed(net, n->nlmsg_flags, RTNLGRP_TC))
  1778. return 0;
  1779. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  1780. if (!skb)
  1781. return -ENOBUFS;
  1782. if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
  1783. n->nlmsg_seq, n->nlmsg_flags, event,
  1784. false, rtnl_held, extack) <= 0) {
  1785. kfree_skb(skb);
  1786. return -EINVAL;
  1787. }
  1788. if (unicast)
  1789. err = rtnl_unicast(skb, net, portid);
  1790. else
  1791. err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  1792. n->nlmsg_flags & NLM_F_ECHO);
  1793. return err;
  1794. }
  1795. static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
  1796. struct nlmsghdr *n, struct tcf_proto *tp,
  1797. struct tcf_block *block, struct Qdisc *q,
  1798. u32 parent, void *fh, bool *last, bool rtnl_held,
  1799. struct netlink_ext_ack *extack)
  1800. {
  1801. struct sk_buff *skb;
  1802. u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
  1803. int err;
  1804. if (!rtnl_notify_needed(net, n->nlmsg_flags, RTNLGRP_TC))
  1805. return tp->ops->delete(tp, fh, last, rtnl_held, extack);
  1806. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  1807. if (!skb)
  1808. return -ENOBUFS;
  1809. if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
  1810. n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER,
  1811. false, rtnl_held, extack) <= 0) {
  1812. NL_SET_ERR_MSG(extack, "Failed to build del event notification");
  1813. kfree_skb(skb);
  1814. return -EINVAL;
  1815. }
  1816. err = tp->ops->delete(tp, fh, last, rtnl_held, extack);
  1817. if (err) {
  1818. kfree_skb(skb);
  1819. return err;
  1820. }
  1821. err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  1822. n->nlmsg_flags & NLM_F_ECHO);
  1823. if (err < 0)
  1824. NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
  1825. return err;
  1826. }
  1827. static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
  1828. struct tcf_block *block, struct Qdisc *q,
  1829. u32 parent, struct nlmsghdr *n,
  1830. struct tcf_chain *chain, int event,
  1831. struct netlink_ext_ack *extack)
  1832. {
  1833. struct tcf_proto *tp;
  1834. for (tp = tcf_get_next_proto(chain, NULL);
  1835. tp; tp = tcf_get_next_proto(chain, tp))
  1836. tfilter_notify(net, oskb, n, tp, block, q, parent, NULL,
  1837. event, false, true, extack);
  1838. }
  1839. static void tfilter_put(struct tcf_proto *tp, void *fh)
  1840. {
  1841. if (tp->ops->put && fh)
  1842. tp->ops->put(tp, fh);
  1843. }
  1844. static bool is_qdisc_ingress(__u32 classid)
  1845. {
  1846. return (TC_H_MIN(classid) == TC_H_MIN(TC_H_MIN_INGRESS));
  1847. }
  1848. static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
  1849. struct netlink_ext_ack *extack)
  1850. {
  1851. struct net *net = sock_net(skb->sk);
  1852. struct nlattr *tca[TCA_MAX + 1];
  1853. char name[IFNAMSIZ];
  1854. struct tcmsg *t;
  1855. u32 protocol;
  1856. u32 prio;
  1857. bool prio_allocate;
  1858. u32 parent;
  1859. u32 chain_index;
  1860. struct Qdisc *q;
  1861. struct tcf_chain_info chain_info;
  1862. struct tcf_chain *chain;
  1863. struct tcf_block *block;
  1864. struct tcf_proto *tp;
  1865. unsigned long cl;
  1866. void *fh;
  1867. int err;
  1868. int tp_created;
  1869. bool rtnl_held = false;
  1870. u32 flags;
  1871. replay:
  1872. tp_created = 0;
  1873. err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
  1874. rtm_tca_policy, extack);
  1875. if (err < 0)
  1876. return err;
  1877. t = nlmsg_data(n);
  1878. protocol = TC_H_MIN(t->tcm_info);
  1879. prio = TC_H_MAJ(t->tcm_info);
  1880. prio_allocate = false;
  1881. parent = t->tcm_parent;
  1882. tp = NULL;
  1883. cl = 0;
  1884. block = NULL;
  1885. q = NULL;
  1886. chain = NULL;
  1887. flags = 0;
  1888. if (prio == 0) {
  1889. /* If no priority is provided by the user,
  1890. * we allocate one.
  1891. */
  1892. if (n->nlmsg_flags & NLM_F_CREATE) {
  1893. prio = TC_H_MAKE(0x80000000U, 0U);
  1894. prio_allocate = true;
  1895. } else {
  1896. NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
  1897. return -ENOENT;
  1898. }
  1899. }
  1900. /* Find head of filter chain. */
  1901. err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
  1902. if (err)
  1903. return err;
  1904. if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
  1905. NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
  1906. err = -EINVAL;
  1907. goto errout;
  1908. }
  1909. /* Take rtnl mutex if rtnl_held was set to true on previous iteration,
  1910. * block is shared (no qdisc found), qdisc is not unlocked, classifier
  1911. * type is not specified, classifier is not unlocked.
  1912. */
  1913. if (rtnl_held ||
  1914. (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
  1915. !tcf_proto_is_unlocked(name)) {
  1916. rtnl_held = true;
  1917. rtnl_lock();
  1918. }
  1919. err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
  1920. if (err)
  1921. goto errout;
  1922. block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
  1923. extack);
  1924. if (IS_ERR(block)) {
  1925. err = PTR_ERR(block);
  1926. goto errout;
  1927. }
  1928. block->classid = parent;
  1929. chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
  1930. if (chain_index > TC_ACT_EXT_VAL_MASK) {
  1931. NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
  1932. err = -EINVAL;
  1933. goto errout;
  1934. }
  1935. chain = tcf_chain_get(block, chain_index, true);
  1936. if (!chain) {
  1937. NL_SET_ERR_MSG(extack, "Cannot create specified filter chain");
  1938. err = -ENOMEM;
  1939. goto errout;
  1940. }
  1941. mutex_lock(&chain->filter_chain_lock);
  1942. tp = tcf_chain_tp_find(chain, &chain_info, protocol,
  1943. prio, prio_allocate);
  1944. if (IS_ERR(tp)) {
  1945. NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
  1946. err = PTR_ERR(tp);
  1947. goto errout_locked;
  1948. }
  1949. if (tp == NULL) {
  1950. struct tcf_proto *tp_new = NULL;
  1951. if (chain->flushing) {
  1952. err = -EAGAIN;
  1953. goto errout_locked;
  1954. }
  1955. /* Proto-tcf does not exist, create new one */
  1956. if (tca[TCA_KIND] == NULL || !protocol) {
  1957. NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
  1958. err = -EINVAL;
  1959. goto errout_locked;
  1960. }
  1961. if (!(n->nlmsg_flags & NLM_F_CREATE)) {
  1962. NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
  1963. err = -ENOENT;
  1964. goto errout_locked;
  1965. }
  1966. if (prio_allocate)
  1967. prio = tcf_auto_prio(tcf_chain_tp_prev(chain,
  1968. &chain_info));
  1969. mutex_unlock(&chain->filter_chain_lock);
  1970. tp_new = tcf_proto_create(name, protocol, prio, chain,
  1971. rtnl_held, extack);
  1972. if (IS_ERR(tp_new)) {
  1973. err = PTR_ERR(tp_new);
  1974. goto errout_tp;
  1975. }
  1976. tp_created = 1;
  1977. tp = tcf_chain_tp_insert_unique(chain, tp_new, protocol, prio,
  1978. rtnl_held);
  1979. if (IS_ERR(tp)) {
  1980. err = PTR_ERR(tp);
  1981. goto errout_tp;
  1982. }
  1983. } else {
  1984. mutex_unlock(&chain->filter_chain_lock);
  1985. }
  1986. if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
  1987. NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
  1988. err = -EINVAL;
  1989. goto errout;
  1990. }
  1991. fh = tp->ops->get(tp, t->tcm_handle);
  1992. if (!fh) {
  1993. if (!(n->nlmsg_flags & NLM_F_CREATE)) {
  1994. NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
  1995. err = -ENOENT;
  1996. goto errout;
  1997. }
  1998. } else if (n->nlmsg_flags & NLM_F_EXCL) {
  1999. tfilter_put(tp, fh);
  2000. NL_SET_ERR_MSG(extack, "Filter already exists");
  2001. err = -EEXIST;
  2002. goto errout;
  2003. }
  2004. if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
  2005. tfilter_put(tp, fh);
  2006. NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
  2007. err = -EINVAL;
  2008. goto errout;
  2009. }
  2010. if (!(n->nlmsg_flags & NLM_F_CREATE))
  2011. flags |= TCA_ACT_FLAGS_REPLACE;
  2012. if (!rtnl_held)
  2013. flags |= TCA_ACT_FLAGS_NO_RTNL;
  2014. if (is_qdisc_ingress(parent))
  2015. flags |= TCA_ACT_FLAGS_AT_INGRESS;
  2016. err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
  2017. flags, extack);
  2018. if (err == 0) {
  2019. tfilter_notify(net, skb, n, tp, block, q, parent, fh,
  2020. RTM_NEWTFILTER, false, rtnl_held, extack);
  2021. tfilter_put(tp, fh);
  2022. tcf_proto_count_usesw(tp, true);
  2023. /* q pointer is NULL for shared blocks */
  2024. if (q)
  2025. q->flags &= ~TCQ_F_CAN_BYPASS;
  2026. }
  2027. errout:
  2028. if (err && tp_created)
  2029. tcf_chain_tp_delete_empty(chain, tp, rtnl_held, NULL);
  2030. errout_tp:
  2031. if (chain) {
  2032. if (tp && !IS_ERR(tp))
  2033. tcf_proto_put(tp, rtnl_held, NULL);
  2034. if (!tp_created)
  2035. tcf_chain_put(chain);
  2036. }
  2037. tcf_block_release(q, block, rtnl_held);
  2038. if (rtnl_held)
  2039. rtnl_unlock();
  2040. if (err == -EAGAIN) {
  2041. /* Take rtnl lock in case EAGAIN is caused by concurrent flush
  2042. * of target chain.
  2043. */
  2044. rtnl_held = true;
  2045. /* Replay the request. */
  2046. goto replay;
  2047. }
  2048. return err;
  2049. errout_locked:
  2050. mutex_unlock(&chain->filter_chain_lock);
  2051. goto errout;
  2052. }
  2053. static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
  2054. struct netlink_ext_ack *extack)
  2055. {
  2056. struct net *net = sock_net(skb->sk);
  2057. struct nlattr *tca[TCA_MAX + 1];
  2058. char name[IFNAMSIZ];
  2059. struct tcmsg *t;
  2060. u32 protocol;
  2061. u32 prio;
  2062. u32 parent;
  2063. u32 chain_index;
  2064. struct Qdisc *q = NULL;
  2065. struct tcf_chain_info chain_info;
  2066. struct tcf_chain *chain = NULL;
  2067. struct tcf_block *block = NULL;
  2068. struct tcf_proto *tp = NULL;
  2069. unsigned long cl = 0;
  2070. void *fh = NULL;
  2071. int err;
  2072. bool rtnl_held = false;
  2073. err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
  2074. rtm_tca_policy, extack);
  2075. if (err < 0)
  2076. return err;
  2077. t = nlmsg_data(n);
  2078. protocol = TC_H_MIN(t->tcm_info);
  2079. prio = TC_H_MAJ(t->tcm_info);
  2080. parent = t->tcm_parent;
  2081. if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
  2082. NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
  2083. return -ENOENT;
  2084. }
  2085. /* Find head of filter chain. */
  2086. err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
  2087. if (err)
  2088. return err;
  2089. if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
  2090. NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
  2091. err = -EINVAL;
  2092. goto errout;
  2093. }
  2094. /* Take rtnl mutex if flushing whole chain, block is shared (no qdisc
  2095. * found), qdisc is not unlocked, classifier type is not specified,
  2096. * classifier is not unlocked.
  2097. */
  2098. if (!prio ||
  2099. (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
  2100. !tcf_proto_is_unlocked(name)) {
  2101. rtnl_held = true;
  2102. rtnl_lock();
  2103. }
  2104. err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
  2105. if (err)
  2106. goto errout;
  2107. block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
  2108. extack);
  2109. if (IS_ERR(block)) {
  2110. err = PTR_ERR(block);
  2111. goto errout;
  2112. }
  2113. chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
  2114. if (chain_index > TC_ACT_EXT_VAL_MASK) {
  2115. NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
  2116. err = -EINVAL;
  2117. goto errout;
  2118. }
  2119. chain = tcf_chain_get(block, chain_index, false);
  2120. if (!chain) {
  2121. /* User requested flush on non-existent chain. Nothing to do,
  2122. * so just return success.
  2123. */
  2124. if (prio == 0) {
  2125. err = 0;
  2126. goto errout;
  2127. }
  2128. NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
  2129. err = -ENOENT;
  2130. goto errout;
  2131. }
  2132. if (prio == 0) {
  2133. tfilter_notify_chain(net, skb, block, q, parent, n,
  2134. chain, RTM_DELTFILTER, extack);
  2135. tcf_chain_flush(chain, rtnl_held);
  2136. err = 0;
  2137. goto errout;
  2138. }
  2139. mutex_lock(&chain->filter_chain_lock);
  2140. tp = tcf_chain_tp_find(chain, &chain_info, protocol,
  2141. prio, false);
  2142. if (!tp || IS_ERR(tp)) {
  2143. NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
  2144. err = tp ? PTR_ERR(tp) : -ENOENT;
  2145. goto errout_locked;
  2146. } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
  2147. NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
  2148. err = -EINVAL;
  2149. goto errout_locked;
  2150. } else if (t->tcm_handle == 0) {
  2151. tcf_proto_signal_destroying(chain, tp);
  2152. tcf_chain_tp_remove(chain, &chain_info, tp);
  2153. mutex_unlock(&chain->filter_chain_lock);
  2154. tcf_proto_put(tp, rtnl_held, NULL);
  2155. tfilter_notify(net, skb, n, tp, block, q, parent, fh,
  2156. RTM_DELTFILTER, false, rtnl_held, extack);
  2157. err = 0;
  2158. goto errout;
  2159. }
  2160. mutex_unlock(&chain->filter_chain_lock);
  2161. fh = tp->ops->get(tp, t->tcm_handle);
  2162. if (!fh) {
  2163. NL_SET_ERR_MSG(extack, "Specified filter handle not found");
  2164. err = -ENOENT;
  2165. } else {
  2166. bool last;
  2167. err = tfilter_del_notify(net, skb, n, tp, block, q, parent, fh,
  2168. &last, rtnl_held, extack);
  2169. if (err)
  2170. goto errout;
  2171. if (last)
  2172. tcf_chain_tp_delete_empty(chain, tp, rtnl_held, extack);
  2173. }
  2174. errout:
  2175. if (chain) {
  2176. if (tp && !IS_ERR(tp))
  2177. tcf_proto_put(tp, rtnl_held, NULL);
  2178. tcf_chain_put(chain);
  2179. }
  2180. tcf_block_release(q, block, rtnl_held);
  2181. if (rtnl_held)
  2182. rtnl_unlock();
  2183. return err;
  2184. errout_locked:
  2185. mutex_unlock(&chain->filter_chain_lock);
  2186. goto errout;
  2187. }
  2188. static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
  2189. struct netlink_ext_ack *extack)
  2190. {
  2191. struct net *net = sock_net(skb->sk);
  2192. struct nlattr *tca[TCA_MAX + 1];
  2193. char name[IFNAMSIZ];
  2194. struct tcmsg *t;
  2195. u32 protocol;
  2196. u32 prio;
  2197. u32 parent;
  2198. u32 chain_index;
  2199. struct Qdisc *q = NULL;
  2200. struct tcf_chain_info chain_info;
  2201. struct tcf_chain *chain = NULL;
  2202. struct tcf_block *block = NULL;
  2203. struct tcf_proto *tp = NULL;
  2204. unsigned long cl = 0;
  2205. void *fh = NULL;
  2206. int err;
  2207. bool rtnl_held = false;
  2208. err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
  2209. rtm_tca_policy, extack);
  2210. if (err < 0)
  2211. return err;
  2212. t = nlmsg_data(n);
  2213. protocol = TC_H_MIN(t->tcm_info);
  2214. prio = TC_H_MAJ(t->tcm_info);
  2215. parent = t->tcm_parent;
  2216. if (prio == 0) {
  2217. NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
  2218. return -ENOENT;
  2219. }
  2220. /* Find head of filter chain. */
  2221. err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
  2222. if (err)
  2223. return err;
  2224. if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
  2225. NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
  2226. err = -EINVAL;
  2227. goto errout;
  2228. }
  2229. /* Take rtnl mutex if block is shared (no qdisc found), qdisc is not
  2230. * unlocked, classifier type is not specified, classifier is not
  2231. * unlocked.
  2232. */
  2233. if ((q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
  2234. !tcf_proto_is_unlocked(name)) {
  2235. rtnl_held = true;
  2236. rtnl_lock();
  2237. }
  2238. err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
  2239. if (err)
  2240. goto errout;
  2241. block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
  2242. extack);
  2243. if (IS_ERR(block)) {
  2244. err = PTR_ERR(block);
  2245. goto errout;
  2246. }
  2247. chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
  2248. if (chain_index > TC_ACT_EXT_VAL_MASK) {
  2249. NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
  2250. err = -EINVAL;
  2251. goto errout;
  2252. }
  2253. chain = tcf_chain_get(block, chain_index, false);
  2254. if (!chain) {
  2255. NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
  2256. err = -EINVAL;
  2257. goto errout;
  2258. }
  2259. mutex_lock(&chain->filter_chain_lock);
  2260. tp = tcf_chain_tp_find(chain, &chain_info, protocol,
  2261. prio, false);
  2262. mutex_unlock(&chain->filter_chain_lock);
  2263. if (!tp || IS_ERR(tp)) {
  2264. NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
  2265. err = tp ? PTR_ERR(tp) : -ENOENT;
  2266. goto errout;
  2267. } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
  2268. NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
  2269. err = -EINVAL;
  2270. goto errout;
  2271. }
  2272. fh = tp->ops->get(tp, t->tcm_handle);
  2273. if (!fh) {
  2274. NL_SET_ERR_MSG(extack, "Specified filter handle not found");
  2275. err = -ENOENT;
  2276. } else {
  2277. err = tfilter_notify(net, skb, n, tp, block, q, parent,
  2278. fh, RTM_NEWTFILTER, true, rtnl_held, NULL);
  2279. if (err < 0)
  2280. NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
  2281. }
  2282. tfilter_put(tp, fh);
  2283. errout:
  2284. if (chain) {
  2285. if (tp && !IS_ERR(tp))
  2286. tcf_proto_put(tp, rtnl_held, NULL);
  2287. tcf_chain_put(chain);
  2288. }
  2289. tcf_block_release(q, block, rtnl_held);
  2290. if (rtnl_held)
  2291. rtnl_unlock();
  2292. return err;
  2293. }
  2294. struct tcf_dump_args {
  2295. struct tcf_walker w;
  2296. struct sk_buff *skb;
  2297. struct netlink_callback *cb;
  2298. struct tcf_block *block;
  2299. struct Qdisc *q;
  2300. u32 parent;
  2301. bool terse_dump;
  2302. };
  2303. static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
  2304. {
  2305. struct tcf_dump_args *a = (void *)arg;
  2306. struct net *net = sock_net(a->skb->sk);
  2307. return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
  2308. n, NETLINK_CB(a->cb->skb).portid,
  2309. a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
  2310. RTM_NEWTFILTER, a->terse_dump, true, NULL);
  2311. }
  2312. static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
  2313. struct sk_buff *skb, struct netlink_callback *cb,
  2314. long index_start, long *p_index, bool terse)
  2315. {
  2316. struct net *net = sock_net(skb->sk);
  2317. struct tcf_block *block = chain->block;
  2318. struct tcmsg *tcm = nlmsg_data(cb->nlh);
  2319. struct tcf_proto *tp, *tp_prev;
  2320. struct tcf_dump_args arg;
  2321. for (tp = __tcf_get_next_proto(chain, NULL);
  2322. tp;
  2323. tp_prev = tp,
  2324. tp = __tcf_get_next_proto(chain, tp),
  2325. tcf_proto_put(tp_prev, true, NULL),
  2326. (*p_index)++) {
  2327. if (*p_index < index_start)
  2328. continue;
  2329. if (TC_H_MAJ(tcm->tcm_info) &&
  2330. TC_H_MAJ(tcm->tcm_info) != tp->prio)
  2331. continue;
  2332. if (TC_H_MIN(tcm->tcm_info) &&
  2333. TC_H_MIN(tcm->tcm_info) != tp->protocol)
  2334. continue;
  2335. if (*p_index > index_start)
  2336. memset(&cb->args[1], 0,
  2337. sizeof(cb->args) - sizeof(cb->args[0]));
  2338. if (cb->args[1] == 0) {
  2339. if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
  2340. NETLINK_CB(cb->skb).portid,
  2341. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  2342. RTM_NEWTFILTER, false, true, NULL) <= 0)
  2343. goto errout;
  2344. cb->args[1] = 1;
  2345. }
  2346. if (!tp->ops->walk)
  2347. continue;
  2348. arg.w.fn = tcf_node_dump;
  2349. arg.skb = skb;
  2350. arg.cb = cb;
  2351. arg.block = block;
  2352. arg.q = q;
  2353. arg.parent = parent;
  2354. arg.w.stop = 0;
  2355. arg.w.skip = cb->args[1] - 1;
  2356. arg.w.count = 0;
  2357. arg.w.cookie = cb->args[2];
  2358. arg.terse_dump = terse;
  2359. tp->ops->walk(tp, &arg.w, true);
  2360. cb->args[2] = arg.w.cookie;
  2361. cb->args[1] = arg.w.count + 1;
  2362. if (arg.w.stop)
  2363. goto errout;
  2364. }
  2365. return true;
  2366. errout:
  2367. tcf_proto_put(tp, true, NULL);
  2368. return false;
  2369. }
  2370. static const struct nla_policy tcf_tfilter_dump_policy[TCA_MAX + 1] = {
  2371. [TCA_CHAIN] = { .type = NLA_U32 },
  2372. [TCA_DUMP_FLAGS] = NLA_POLICY_BITFIELD32(TCA_DUMP_FLAGS_TERSE),
  2373. };
  2374. /* called with RTNL */
  2375. static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
  2376. {
  2377. struct tcf_chain *chain, *chain_prev;
  2378. struct net *net = sock_net(skb->sk);
  2379. struct nlattr *tca[TCA_MAX + 1];
  2380. struct Qdisc *q = NULL;
  2381. struct tcf_block *block;
  2382. struct tcmsg *tcm = nlmsg_data(cb->nlh);
  2383. bool terse_dump = false;
  2384. long index_start;
  2385. long index;
  2386. u32 parent;
  2387. int err;
  2388. if (nlmsg_len(cb->nlh) < sizeof(*tcm))
  2389. return skb->len;
  2390. err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
  2391. tcf_tfilter_dump_policy, cb->extack);
  2392. if (err)
  2393. return err;
  2394. if (tca[TCA_DUMP_FLAGS]) {
  2395. struct nla_bitfield32 flags =
  2396. nla_get_bitfield32(tca[TCA_DUMP_FLAGS]);
  2397. terse_dump = flags.value & TCA_DUMP_FLAGS_TERSE;
  2398. }
  2399. if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
  2400. block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
  2401. if (!block)
  2402. goto out;
  2403. /* If we work with block index, q is NULL and parent value
  2404. * will never be used in the following code. The check
  2405. * in tcf_fill_node prevents it. However, compiler does not
  2406. * see that far, so set parent to zero to silence the warning
  2407. * about parent being uninitialized.
  2408. */
  2409. parent = 0;
  2410. } else {
  2411. const struct Qdisc_class_ops *cops;
  2412. struct net_device *dev;
  2413. unsigned long cl = 0;
  2414. dev = __dev_get_by_index(net, tcm->tcm_ifindex);
  2415. if (!dev)
  2416. return skb->len;
  2417. parent = tcm->tcm_parent;
  2418. if (!parent)
  2419. q = rtnl_dereference(dev->qdisc);
  2420. else
  2421. q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
  2422. if (!q)
  2423. goto out;
  2424. cops = q->ops->cl_ops;
  2425. if (!cops)
  2426. goto out;
  2427. if (!cops->tcf_block)
  2428. goto out;
  2429. if (TC_H_MIN(tcm->tcm_parent)) {
  2430. cl = cops->find(q, tcm->tcm_parent);
  2431. if (cl == 0)
  2432. goto out;
  2433. }
  2434. block = cops->tcf_block(q, cl, NULL);
  2435. if (!block)
  2436. goto out;
  2437. parent = block->classid;
  2438. if (tcf_block_shared(block))
  2439. q = NULL;
  2440. }
  2441. index_start = cb->args[0];
  2442. index = 0;
  2443. for (chain = __tcf_get_next_chain(block, NULL);
  2444. chain;
  2445. chain_prev = chain,
  2446. chain = __tcf_get_next_chain(block, chain),
  2447. tcf_chain_put(chain_prev)) {
  2448. if (tca[TCA_CHAIN] &&
  2449. nla_get_u32(tca[TCA_CHAIN]) != chain->index)
  2450. continue;
  2451. if (!tcf_chain_dump(chain, q, parent, skb, cb,
  2452. index_start, &index, terse_dump)) {
  2453. tcf_chain_put(chain);
  2454. err = -EMSGSIZE;
  2455. break;
  2456. }
  2457. }
  2458. if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
  2459. tcf_block_refcnt_put(block, true);
  2460. cb->args[0] = index;
  2461. out:
  2462. /* If we did no progress, the error (EMSGSIZE) is real */
  2463. if (skb->len == 0 && err)
  2464. return err;
  2465. return skb->len;
  2466. }
  2467. static int tc_chain_fill_node(const struct tcf_proto_ops *tmplt_ops,
  2468. void *tmplt_priv, u32 chain_index,
  2469. struct net *net, struct sk_buff *skb,
  2470. struct tcf_block *block,
  2471. u32 portid, u32 seq, u16 flags, int event,
  2472. struct netlink_ext_ack *extack)
  2473. {
  2474. unsigned char *b = skb_tail_pointer(skb);
  2475. const struct tcf_proto_ops *ops;
  2476. struct nlmsghdr *nlh;
  2477. struct tcmsg *tcm;
  2478. void *priv;
  2479. ops = tmplt_ops;
  2480. priv = tmplt_priv;
  2481. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
  2482. if (!nlh)
  2483. goto out_nlmsg_trim;
  2484. tcm = nlmsg_data(nlh);
  2485. tcm->tcm_family = AF_UNSPEC;
  2486. tcm->tcm__pad1 = 0;
  2487. tcm->tcm__pad2 = 0;
  2488. tcm->tcm_handle = 0;
  2489. if (block->q) {
  2490. tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
  2491. tcm->tcm_parent = block->q->handle;
  2492. } else {
  2493. tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
  2494. tcm->tcm_block_index = block->index;
  2495. }
  2496. if (nla_put_u32(skb, TCA_CHAIN, chain_index))
  2497. goto nla_put_failure;
  2498. if (ops) {
  2499. if (nla_put_string(skb, TCA_KIND, ops->kind))
  2500. goto nla_put_failure;
  2501. if (ops->tmplt_dump(skb, net, priv) < 0)
  2502. goto nla_put_failure;
  2503. }
  2504. if (extack && extack->_msg &&
  2505. nla_put_string(skb, TCA_EXT_WARN_MSG, extack->_msg))
  2506. goto out_nlmsg_trim;
  2507. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  2508. return skb->len;
  2509. out_nlmsg_trim:
  2510. nla_put_failure:
  2511. nlmsg_trim(skb, b);
  2512. return -EMSGSIZE;
  2513. }
  2514. static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
  2515. u32 seq, u16 flags, int event, bool unicast,
  2516. struct netlink_ext_ack *extack)
  2517. {
  2518. u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
  2519. struct tcf_block *block = chain->block;
  2520. struct net *net = block->net;
  2521. struct sk_buff *skb;
  2522. int err = 0;
  2523. if (!unicast && !rtnl_notify_needed(net, flags, RTNLGRP_TC))
  2524. return 0;
  2525. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  2526. if (!skb)
  2527. return -ENOBUFS;
  2528. if (tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
  2529. chain->index, net, skb, block, portid,
  2530. seq, flags, event, extack) <= 0) {
  2531. kfree_skb(skb);
  2532. return -EINVAL;
  2533. }
  2534. if (unicast)
  2535. err = rtnl_unicast(skb, net, portid);
  2536. else
  2537. err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  2538. flags & NLM_F_ECHO);
  2539. return err;
  2540. }
  2541. static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
  2542. void *tmplt_priv, u32 chain_index,
  2543. struct tcf_block *block, struct sk_buff *oskb,
  2544. u32 seq, u16 flags)
  2545. {
  2546. u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
  2547. struct net *net = block->net;
  2548. struct sk_buff *skb;
  2549. if (!rtnl_notify_needed(net, flags, RTNLGRP_TC))
  2550. return 0;
  2551. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  2552. if (!skb)
  2553. return -ENOBUFS;
  2554. if (tc_chain_fill_node(tmplt_ops, tmplt_priv, chain_index, net, skb,
  2555. block, portid, seq, flags, RTM_DELCHAIN, NULL) <= 0) {
  2556. kfree_skb(skb);
  2557. return -EINVAL;
  2558. }
  2559. return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
  2560. }
  2561. static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
  2562. struct nlattr **tca,
  2563. struct netlink_ext_ack *extack)
  2564. {
  2565. const struct tcf_proto_ops *ops;
  2566. char name[IFNAMSIZ];
  2567. void *tmplt_priv;
  2568. /* If kind is not set, user did not specify template. */
  2569. if (!tca[TCA_KIND])
  2570. return 0;
  2571. if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
  2572. NL_SET_ERR_MSG(extack, "Specified TC chain template name too long");
  2573. return -EINVAL;
  2574. }
  2575. ops = tcf_proto_lookup_ops(name, true, extack);
  2576. if (IS_ERR(ops))
  2577. return PTR_ERR(ops);
  2578. if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump ||
  2579. !ops->tmplt_reoffload) {
  2580. NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
  2581. module_put(ops->owner);
  2582. return -EOPNOTSUPP;
  2583. }
  2584. tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
  2585. if (IS_ERR(tmplt_priv)) {
  2586. module_put(ops->owner);
  2587. return PTR_ERR(tmplt_priv);
  2588. }
  2589. chain->tmplt_ops = ops;
  2590. chain->tmplt_priv = tmplt_priv;
  2591. return 0;
  2592. }
  2593. static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
  2594. void *tmplt_priv)
  2595. {
  2596. /* If template ops are set, no work to do for us. */
  2597. if (!tmplt_ops)
  2598. return;
  2599. tmplt_ops->tmplt_destroy(tmplt_priv);
  2600. module_put(tmplt_ops->owner);
  2601. }
  2602. /* Add/delete/get a chain */
  2603. static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
  2604. struct netlink_ext_ack *extack)
  2605. {
  2606. struct net *net = sock_net(skb->sk);
  2607. struct nlattr *tca[TCA_MAX + 1];
  2608. struct tcmsg *t;
  2609. u32 parent;
  2610. u32 chain_index;
  2611. struct Qdisc *q;
  2612. struct tcf_chain *chain;
  2613. struct tcf_block *block;
  2614. unsigned long cl;
  2615. int err;
  2616. replay:
  2617. q = NULL;
  2618. err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
  2619. rtm_tca_policy, extack);
  2620. if (err < 0)
  2621. return err;
  2622. t = nlmsg_data(n);
  2623. parent = t->tcm_parent;
  2624. cl = 0;
  2625. block = tcf_block_find(net, &q, &parent, &cl,
  2626. t->tcm_ifindex, t->tcm_block_index, extack);
  2627. if (IS_ERR(block))
  2628. return PTR_ERR(block);
  2629. chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
  2630. if (chain_index > TC_ACT_EXT_VAL_MASK) {
  2631. NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
  2632. err = -EINVAL;
  2633. goto errout_block;
  2634. }
  2635. mutex_lock(&block->lock);
  2636. chain = tcf_chain_lookup(block, chain_index);
  2637. if (n->nlmsg_type == RTM_NEWCHAIN) {
  2638. if (chain) {
  2639. if (tcf_chain_held_by_acts_only(chain)) {
  2640. /* The chain exists only because there is
  2641. * some action referencing it.
  2642. */
  2643. tcf_chain_hold(chain);
  2644. } else {
  2645. NL_SET_ERR_MSG(extack, "Filter chain already exists");
  2646. err = -EEXIST;
  2647. goto errout_block_locked;
  2648. }
  2649. } else {
  2650. if (!(n->nlmsg_flags & NLM_F_CREATE)) {
  2651. NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
  2652. err = -ENOENT;
  2653. goto errout_block_locked;
  2654. }
  2655. chain = tcf_chain_create(block, chain_index);
  2656. if (!chain) {
  2657. NL_SET_ERR_MSG(extack, "Failed to create filter chain");
  2658. err = -ENOMEM;
  2659. goto errout_block_locked;
  2660. }
  2661. }
  2662. } else {
  2663. if (!chain || tcf_chain_held_by_acts_only(chain)) {
  2664. NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
  2665. err = -EINVAL;
  2666. goto errout_block_locked;
  2667. }
  2668. tcf_chain_hold(chain);
  2669. }
  2670. if (n->nlmsg_type == RTM_NEWCHAIN) {
  2671. /* Modifying chain requires holding parent block lock. In case
  2672. * the chain was successfully added, take a reference to the
  2673. * chain. This ensures that an empty chain does not disappear at
  2674. * the end of this function.
  2675. */
  2676. tcf_chain_hold(chain);
  2677. chain->explicitly_created = true;
  2678. }
  2679. mutex_unlock(&block->lock);
  2680. switch (n->nlmsg_type) {
  2681. case RTM_NEWCHAIN:
  2682. err = tc_chain_tmplt_add(chain, net, tca, extack);
  2683. if (err) {
  2684. tcf_chain_put_explicitly_created(chain);
  2685. goto errout;
  2686. }
  2687. tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
  2688. RTM_NEWCHAIN, false, extack);
  2689. break;
  2690. case RTM_DELCHAIN:
  2691. tfilter_notify_chain(net, skb, block, q, parent, n,
  2692. chain, RTM_DELTFILTER, extack);
  2693. /* Flush the chain first as the user requested chain removal. */
  2694. tcf_chain_flush(chain, true);
  2695. /* In case the chain was successfully deleted, put a reference
  2696. * to the chain previously taken during addition.
  2697. */
  2698. tcf_chain_put_explicitly_created(chain);
  2699. break;
  2700. case RTM_GETCHAIN:
  2701. err = tc_chain_notify(chain, skb, n->nlmsg_seq,
  2702. n->nlmsg_flags, n->nlmsg_type, true, extack);
  2703. if (err < 0)
  2704. NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
  2705. break;
  2706. default:
  2707. err = -EOPNOTSUPP;
  2708. NL_SET_ERR_MSG(extack, "Unsupported message type");
  2709. goto errout;
  2710. }
  2711. errout:
  2712. tcf_chain_put(chain);
  2713. errout_block:
  2714. tcf_block_release(q, block, true);
  2715. if (err == -EAGAIN)
  2716. /* Replay the request. */
  2717. goto replay;
  2718. return err;
  2719. errout_block_locked:
  2720. mutex_unlock(&block->lock);
  2721. goto errout_block;
  2722. }
  2723. /* called with RTNL */
  2724. static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
  2725. {
  2726. struct net *net = sock_net(skb->sk);
  2727. struct nlattr *tca[TCA_MAX + 1];
  2728. struct Qdisc *q = NULL;
  2729. struct tcf_block *block;
  2730. struct tcmsg *tcm = nlmsg_data(cb->nlh);
  2731. struct tcf_chain *chain;
  2732. long index_start;
  2733. long index;
  2734. int err;
  2735. if (nlmsg_len(cb->nlh) < sizeof(*tcm))
  2736. return skb->len;
  2737. err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
  2738. rtm_tca_policy, cb->extack);
  2739. if (err)
  2740. return err;
  2741. if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
  2742. block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
  2743. if (!block)
  2744. goto out;
  2745. } else {
  2746. const struct Qdisc_class_ops *cops;
  2747. struct net_device *dev;
  2748. unsigned long cl = 0;
  2749. dev = __dev_get_by_index(net, tcm->tcm_ifindex);
  2750. if (!dev)
  2751. return skb->len;
  2752. if (!tcm->tcm_parent)
  2753. q = rtnl_dereference(dev->qdisc);
  2754. else
  2755. q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
  2756. if (!q)
  2757. goto out;
  2758. cops = q->ops->cl_ops;
  2759. if (!cops)
  2760. goto out;
  2761. if (!cops->tcf_block)
  2762. goto out;
  2763. if (TC_H_MIN(tcm->tcm_parent)) {
  2764. cl = cops->find(q, tcm->tcm_parent);
  2765. if (cl == 0)
  2766. goto out;
  2767. }
  2768. block = cops->tcf_block(q, cl, NULL);
  2769. if (!block)
  2770. goto out;
  2771. if (tcf_block_shared(block))
  2772. q = NULL;
  2773. }
  2774. index_start = cb->args[0];
  2775. index = 0;
  2776. mutex_lock(&block->lock);
  2777. list_for_each_entry(chain, &block->chain_list, list) {
  2778. if ((tca[TCA_CHAIN] &&
  2779. nla_get_u32(tca[TCA_CHAIN]) != chain->index))
  2780. continue;
  2781. if (index < index_start) {
  2782. index++;
  2783. continue;
  2784. }
  2785. if (tcf_chain_held_by_acts_only(chain))
  2786. continue;
  2787. err = tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
  2788. chain->index, net, skb, block,
  2789. NETLINK_CB(cb->skb).portid,
  2790. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  2791. RTM_NEWCHAIN, NULL);
  2792. if (err <= 0)
  2793. break;
  2794. index++;
  2795. }
  2796. mutex_unlock(&block->lock);
  2797. if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
  2798. tcf_block_refcnt_put(block, true);
  2799. cb->args[0] = index;
  2800. out:
  2801. /* If we did no progress, the error (EMSGSIZE) is real */
  2802. if (skb->len == 0 && err)
  2803. return err;
  2804. return skb->len;
  2805. }
  2806. int tcf_exts_init_ex(struct tcf_exts *exts, struct net *net, int action,
  2807. int police, struct tcf_proto *tp, u32 handle,
  2808. bool use_action_miss)
  2809. {
  2810. int err = 0;
  2811. #ifdef CONFIG_NET_CLS_ACT
  2812. exts->type = 0;
  2813. exts->nr_actions = 0;
  2814. exts->miss_cookie_node = NULL;
  2815. /* Note: we do not own yet a reference on net.
  2816. * This reference might be taken later from tcf_exts_get_net().
  2817. */
  2818. exts->net = net;
  2819. exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),
  2820. GFP_KERNEL);
  2821. if (!exts->actions)
  2822. return -ENOMEM;
  2823. #endif
  2824. exts->action = action;
  2825. exts->police = police;
  2826. if (!use_action_miss)
  2827. return 0;
  2828. err = tcf_exts_miss_cookie_base_alloc(exts, tp, handle);
  2829. if (err)
  2830. goto err_miss_alloc;
  2831. return 0;
  2832. err_miss_alloc:
  2833. tcf_exts_destroy(exts);
  2834. #ifdef CONFIG_NET_CLS_ACT
  2835. exts->actions = NULL;
  2836. #endif
  2837. return err;
  2838. }
  2839. EXPORT_SYMBOL(tcf_exts_init_ex);
  2840. void tcf_exts_destroy(struct tcf_exts *exts)
  2841. {
  2842. tcf_exts_miss_cookie_base_destroy(exts);
  2843. #ifdef CONFIG_NET_CLS_ACT
  2844. if (exts->actions) {
  2845. tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
  2846. kfree(exts->actions);
  2847. }
  2848. exts->nr_actions = 0;
  2849. #endif
  2850. }
  2851. EXPORT_SYMBOL(tcf_exts_destroy);
  2852. int tcf_exts_validate_ex(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
  2853. struct nlattr *rate_tlv, struct tcf_exts *exts,
  2854. u32 flags, u32 fl_flags, struct netlink_ext_ack *extack)
  2855. {
  2856. #ifdef CONFIG_NET_CLS_ACT
  2857. {
  2858. int init_res[TCA_ACT_MAX_PRIO] = {};
  2859. struct tc_action *act;
  2860. size_t attr_size = 0;
  2861. if (exts->police && tb[exts->police]) {
  2862. struct tc_action_ops *a_o;
  2863. flags |= TCA_ACT_FLAGS_POLICE | TCA_ACT_FLAGS_BIND;
  2864. a_o = tc_action_load_ops(tb[exts->police], flags,
  2865. extack);
  2866. if (IS_ERR(a_o))
  2867. return PTR_ERR(a_o);
  2868. act = tcf_action_init_1(net, tp, tb[exts->police],
  2869. rate_tlv, a_o, init_res, flags,
  2870. extack);
  2871. module_put(a_o->owner);
  2872. if (IS_ERR(act))
  2873. return PTR_ERR(act);
  2874. act->type = exts->type = TCA_OLD_COMPAT;
  2875. exts->actions[0] = act;
  2876. exts->nr_actions = 1;
  2877. tcf_idr_insert_many(exts->actions, init_res);
  2878. } else if (exts->action && tb[exts->action]) {
  2879. int err;
  2880. flags |= TCA_ACT_FLAGS_BIND;
  2881. err = tcf_action_init(net, tp, tb[exts->action],
  2882. rate_tlv, exts->actions, init_res,
  2883. &attr_size, flags, fl_flags,
  2884. extack);
  2885. if (err < 0)
  2886. return err;
  2887. exts->nr_actions = err;
  2888. }
  2889. }
  2890. #else
  2891. if ((exts->action && tb[exts->action]) ||
  2892. (exts->police && tb[exts->police])) {
  2893. NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
  2894. return -EOPNOTSUPP;
  2895. }
  2896. #endif
  2897. return 0;
  2898. }
  2899. EXPORT_SYMBOL(tcf_exts_validate_ex);
  2900. int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
  2901. struct nlattr *rate_tlv, struct tcf_exts *exts,
  2902. u32 flags, struct netlink_ext_ack *extack)
  2903. {
  2904. return tcf_exts_validate_ex(net, tp, tb, rate_tlv, exts,
  2905. flags, 0, extack);
  2906. }
  2907. EXPORT_SYMBOL(tcf_exts_validate);
  2908. void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
  2909. {
  2910. #ifdef CONFIG_NET_CLS_ACT
  2911. struct tcf_exts old = *dst;
  2912. *dst = *src;
  2913. tcf_exts_destroy(&old);
  2914. #endif
  2915. }
  2916. EXPORT_SYMBOL(tcf_exts_change);
  2917. #ifdef CONFIG_NET_CLS_ACT
  2918. static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
  2919. {
  2920. if (exts->nr_actions == 0)
  2921. return NULL;
  2922. else
  2923. return exts->actions[0];
  2924. }
  2925. #endif
  2926. int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
  2927. {
  2928. #ifdef CONFIG_NET_CLS_ACT
  2929. struct nlattr *nest;
  2930. if (exts->action && tcf_exts_has_actions(exts)) {
  2931. /*
  2932. * again for backward compatible mode - we want
  2933. * to work with both old and new modes of entering
  2934. * tc data even if iproute2 was newer - jhs
  2935. */
  2936. if (exts->type != TCA_OLD_COMPAT) {
  2937. nest = nla_nest_start_noflag(skb, exts->action);
  2938. if (nest == NULL)
  2939. goto nla_put_failure;
  2940. if (tcf_action_dump(skb, exts->actions, 0, 0, false)
  2941. < 0)
  2942. goto nla_put_failure;
  2943. nla_nest_end(skb, nest);
  2944. } else if (exts->police) {
  2945. struct tc_action *act = tcf_exts_first_act(exts);
  2946. nest = nla_nest_start_noflag(skb, exts->police);
  2947. if (nest == NULL || !act)
  2948. goto nla_put_failure;
  2949. if (tcf_action_dump_old(skb, act, 0, 0) < 0)
  2950. goto nla_put_failure;
  2951. nla_nest_end(skb, nest);
  2952. }
  2953. }
  2954. return 0;
  2955. nla_put_failure:
  2956. nla_nest_cancel(skb, nest);
  2957. return -1;
  2958. #else
  2959. return 0;
  2960. #endif
  2961. }
  2962. EXPORT_SYMBOL(tcf_exts_dump);
  2963. int tcf_exts_terse_dump(struct sk_buff *skb, struct tcf_exts *exts)
  2964. {
  2965. #ifdef CONFIG_NET_CLS_ACT
  2966. struct nlattr *nest;
  2967. if (!exts->action || !tcf_exts_has_actions(exts))
  2968. return 0;
  2969. nest = nla_nest_start_noflag(skb, exts->action);
  2970. if (!nest)
  2971. goto nla_put_failure;
  2972. if (tcf_action_dump(skb, exts->actions, 0, 0, true) < 0)
  2973. goto nla_put_failure;
  2974. nla_nest_end(skb, nest);
  2975. return 0;
  2976. nla_put_failure:
  2977. nla_nest_cancel(skb, nest);
  2978. return -1;
  2979. #else
  2980. return 0;
  2981. #endif
  2982. }
  2983. EXPORT_SYMBOL(tcf_exts_terse_dump);
  2984. int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
  2985. {
  2986. #ifdef CONFIG_NET_CLS_ACT
  2987. struct tc_action *a = tcf_exts_first_act(exts);
  2988. if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
  2989. return -1;
  2990. #endif
  2991. return 0;
  2992. }
  2993. EXPORT_SYMBOL(tcf_exts_dump_stats);
  2994. static void tcf_block_offload_inc(struct tcf_block *block, u32 *flags)
  2995. {
  2996. if (*flags & TCA_CLS_FLAGS_IN_HW)
  2997. return;
  2998. *flags |= TCA_CLS_FLAGS_IN_HW;
  2999. atomic_inc(&block->offloadcnt);
  3000. }
  3001. static void tcf_block_offload_dec(struct tcf_block *block, u32 *flags)
  3002. {
  3003. if (!(*flags & TCA_CLS_FLAGS_IN_HW))
  3004. return;
  3005. *flags &= ~TCA_CLS_FLAGS_IN_HW;
  3006. atomic_dec(&block->offloadcnt);
  3007. }
  3008. static void tc_cls_offload_cnt_update(struct tcf_block *block,
  3009. struct tcf_proto *tp, u32 *cnt,
  3010. u32 *flags, u32 diff, bool add)
  3011. {
  3012. lockdep_assert_held(&block->cb_lock);
  3013. spin_lock(&tp->lock);
  3014. if (add) {
  3015. if (!*cnt)
  3016. tcf_block_offload_inc(block, flags);
  3017. *cnt += diff;
  3018. } else {
  3019. *cnt -= diff;
  3020. if (!*cnt)
  3021. tcf_block_offload_dec(block, flags);
  3022. }
  3023. spin_unlock(&tp->lock);
  3024. }
  3025. static void
  3026. tc_cls_offload_cnt_reset(struct tcf_block *block, struct tcf_proto *tp,
  3027. u32 *cnt, u32 *flags)
  3028. {
  3029. lockdep_assert_held(&block->cb_lock);
  3030. spin_lock(&tp->lock);
  3031. tcf_block_offload_dec(block, flags);
  3032. *cnt = 0;
  3033. spin_unlock(&tp->lock);
  3034. }
  3035. static int
  3036. __tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
  3037. void *type_data, bool err_stop)
  3038. {
  3039. struct flow_block_cb *block_cb;
  3040. int ok_count = 0;
  3041. int err;
  3042. list_for_each_entry(block_cb, &block->flow_block.cb_list, list) {
  3043. err = block_cb->cb(type, type_data, block_cb->cb_priv);
  3044. if (err) {
  3045. if (err_stop)
  3046. return err;
  3047. } else {
  3048. ok_count++;
  3049. }
  3050. }
  3051. return ok_count;
  3052. }
  3053. int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
  3054. void *type_data, bool err_stop, bool rtnl_held)
  3055. {
  3056. bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
  3057. int ok_count;
  3058. retry:
  3059. if (take_rtnl)
  3060. rtnl_lock();
  3061. down_read(&block->cb_lock);
  3062. /* Need to obtain rtnl lock if block is bound to devs that require it.
  3063. * In block bind code cb_lock is obtained while holding rtnl, so we must
  3064. * obtain the locks in same order here.
  3065. */
  3066. if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
  3067. up_read(&block->cb_lock);
  3068. take_rtnl = true;
  3069. goto retry;
  3070. }
  3071. ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
  3072. up_read(&block->cb_lock);
  3073. if (take_rtnl)
  3074. rtnl_unlock();
  3075. return ok_count;
  3076. }
  3077. EXPORT_SYMBOL(tc_setup_cb_call);
  3078. /* Non-destructive filter add. If filter that wasn't already in hardware is
  3079. * successfully offloaded, increment block offloads counter. On failure,
  3080. * previously offloaded filter is considered to be intact and offloads counter
  3081. * is not decremented.
  3082. */
  3083. int tc_setup_cb_add(struct tcf_block *block, struct tcf_proto *tp,
  3084. enum tc_setup_type type, void *type_data, bool err_stop,
  3085. u32 *flags, unsigned int *in_hw_count, bool rtnl_held)
  3086. {
  3087. bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
  3088. int ok_count;
  3089. retry:
  3090. if (take_rtnl)
  3091. rtnl_lock();
  3092. down_read(&block->cb_lock);
  3093. /* Need to obtain rtnl lock if block is bound to devs that require it.
  3094. * In block bind code cb_lock is obtained while holding rtnl, so we must
  3095. * obtain the locks in same order here.
  3096. */
  3097. if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
  3098. up_read(&block->cb_lock);
  3099. take_rtnl = true;
  3100. goto retry;
  3101. }
  3102. /* Make sure all netdevs sharing this block are offload-capable. */
  3103. if (block->nooffloaddevcnt && err_stop) {
  3104. ok_count = -EOPNOTSUPP;
  3105. goto err_unlock;
  3106. }
  3107. ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
  3108. if (ok_count < 0)
  3109. goto err_unlock;
  3110. if (tp->ops->hw_add)
  3111. tp->ops->hw_add(tp, type_data);
  3112. if (ok_count > 0)
  3113. tc_cls_offload_cnt_update(block, tp, in_hw_count, flags,
  3114. ok_count, true);
  3115. err_unlock:
  3116. up_read(&block->cb_lock);
  3117. if (take_rtnl)
  3118. rtnl_unlock();
  3119. return min(ok_count, 0);
  3120. }
  3121. EXPORT_SYMBOL(tc_setup_cb_add);
  3122. /* Destructive filter replace. If filter that wasn't already in hardware is
  3123. * successfully offloaded, increment block offload counter. On failure,
  3124. * previously offloaded filter is considered to be destroyed and offload counter
  3125. * is decremented.
  3126. */
  3127. int tc_setup_cb_replace(struct tcf_block *block, struct tcf_proto *tp,
  3128. enum tc_setup_type type, void *type_data, bool err_stop,
  3129. u32 *old_flags, unsigned int *old_in_hw_count,
  3130. u32 *new_flags, unsigned int *new_in_hw_count,
  3131. bool rtnl_held)
  3132. {
  3133. bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
  3134. int ok_count;
  3135. retry:
  3136. if (take_rtnl)
  3137. rtnl_lock();
  3138. down_read(&block->cb_lock);
  3139. /* Need to obtain rtnl lock if block is bound to devs that require it.
  3140. * In block bind code cb_lock is obtained while holding rtnl, so we must
  3141. * obtain the locks in same order here.
  3142. */
  3143. if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
  3144. up_read(&block->cb_lock);
  3145. take_rtnl = true;
  3146. goto retry;
  3147. }
  3148. /* Make sure all netdevs sharing this block are offload-capable. */
  3149. if (block->nooffloaddevcnt && err_stop) {
  3150. ok_count = -EOPNOTSUPP;
  3151. goto err_unlock;
  3152. }
  3153. tc_cls_offload_cnt_reset(block, tp, old_in_hw_count, old_flags);
  3154. if (tp->ops->hw_del)
  3155. tp->ops->hw_del(tp, type_data);
  3156. ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
  3157. if (ok_count < 0)
  3158. goto err_unlock;
  3159. if (tp->ops->hw_add)
  3160. tp->ops->hw_add(tp, type_data);
  3161. if (ok_count > 0)
  3162. tc_cls_offload_cnt_update(block, tp, new_in_hw_count,
  3163. new_flags, ok_count, true);
  3164. err_unlock:
  3165. up_read(&block->cb_lock);
  3166. if (take_rtnl)
  3167. rtnl_unlock();
  3168. return min(ok_count, 0);
  3169. }
  3170. EXPORT_SYMBOL(tc_setup_cb_replace);
  3171. /* Destroy filter and decrement block offload counter, if filter was previously
  3172. * offloaded.
  3173. */
  3174. int tc_setup_cb_destroy(struct tcf_block *block, struct tcf_proto *tp,
  3175. enum tc_setup_type type, void *type_data, bool err_stop,
  3176. u32 *flags, unsigned int *in_hw_count, bool rtnl_held)
  3177. {
  3178. bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
  3179. int ok_count;
  3180. retry:
  3181. if (take_rtnl)
  3182. rtnl_lock();
  3183. down_read(&block->cb_lock);
  3184. /* Need to obtain rtnl lock if block is bound to devs that require it.
  3185. * In block bind code cb_lock is obtained while holding rtnl, so we must
  3186. * obtain the locks in same order here.
  3187. */
  3188. if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
  3189. up_read(&block->cb_lock);
  3190. take_rtnl = true;
  3191. goto retry;
  3192. }
  3193. ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
  3194. tc_cls_offload_cnt_reset(block, tp, in_hw_count, flags);
  3195. if (tp->ops->hw_del)
  3196. tp->ops->hw_del(tp, type_data);
  3197. up_read(&block->cb_lock);
  3198. if (take_rtnl)
  3199. rtnl_unlock();
  3200. return min(ok_count, 0);
  3201. }
  3202. EXPORT_SYMBOL(tc_setup_cb_destroy);
  3203. int tc_setup_cb_reoffload(struct tcf_block *block, struct tcf_proto *tp,
  3204. bool add, flow_setup_cb_t *cb,
  3205. enum tc_setup_type type, void *type_data,
  3206. void *cb_priv, u32 *flags, unsigned int *in_hw_count)
  3207. {
  3208. int err = cb(type, type_data, cb_priv);
  3209. if (err) {
  3210. if (add && tc_skip_sw(*flags))
  3211. return err;
  3212. } else {
  3213. tc_cls_offload_cnt_update(block, tp, in_hw_count, flags, 1,
  3214. add);
  3215. }
  3216. return 0;
  3217. }
  3218. EXPORT_SYMBOL(tc_setup_cb_reoffload);
  3219. static int tcf_act_get_user_cookie(struct flow_action_entry *entry,
  3220. const struct tc_action *act)
  3221. {
  3222. struct tc_cookie *user_cookie;
  3223. int err = 0;
  3224. rcu_read_lock();
  3225. user_cookie = rcu_dereference(act->user_cookie);
  3226. if (user_cookie) {
  3227. entry->user_cookie = flow_action_cookie_create(user_cookie->data,
  3228. user_cookie->len,
  3229. GFP_ATOMIC);
  3230. if (!entry->user_cookie)
  3231. err = -ENOMEM;
  3232. }
  3233. rcu_read_unlock();
  3234. return err;
  3235. }
  3236. static void tcf_act_put_user_cookie(struct flow_action_entry *entry)
  3237. {
  3238. flow_action_cookie_destroy(entry->user_cookie);
  3239. }
  3240. void tc_cleanup_offload_action(struct flow_action *flow_action)
  3241. {
  3242. struct flow_action_entry *entry;
  3243. int i;
  3244. flow_action_for_each(i, entry, flow_action) {
  3245. tcf_act_put_user_cookie(entry);
  3246. if (entry->destructor)
  3247. entry->destructor(entry->destructor_priv);
  3248. }
  3249. }
  3250. EXPORT_SYMBOL(tc_cleanup_offload_action);
  3251. static int tc_setup_offload_act(struct tc_action *act,
  3252. struct flow_action_entry *entry,
  3253. u32 *index_inc,
  3254. struct netlink_ext_ack *extack)
  3255. {
  3256. #ifdef CONFIG_NET_CLS_ACT
  3257. if (act->ops->offload_act_setup) {
  3258. return act->ops->offload_act_setup(act, entry, index_inc, true,
  3259. extack);
  3260. } else {
  3261. NL_SET_ERR_MSG(extack, "Action does not support offload");
  3262. return -EOPNOTSUPP;
  3263. }
  3264. #else
  3265. return 0;
  3266. #endif
  3267. }
  3268. int tc_setup_action(struct flow_action *flow_action,
  3269. struct tc_action *actions[],
  3270. u32 miss_cookie_base,
  3271. struct netlink_ext_ack *extack)
  3272. {
  3273. int i, j, k, index, err = 0;
  3274. struct tc_action *act;
  3275. BUILD_BUG_ON(TCA_ACT_HW_STATS_ANY != FLOW_ACTION_HW_STATS_ANY);
  3276. BUILD_BUG_ON(TCA_ACT_HW_STATS_IMMEDIATE != FLOW_ACTION_HW_STATS_IMMEDIATE);
  3277. BUILD_BUG_ON(TCA_ACT_HW_STATS_DELAYED != FLOW_ACTION_HW_STATS_DELAYED);
  3278. if (!actions)
  3279. return 0;
  3280. j = 0;
  3281. tcf_act_for_each_action(i, act, actions) {
  3282. struct flow_action_entry *entry;
  3283. entry = &flow_action->entries[j];
  3284. spin_lock_bh(&act->tcfa_lock);
  3285. err = tcf_act_get_user_cookie(entry, act);
  3286. if (err)
  3287. goto err_out_locked;
  3288. index = 0;
  3289. err = tc_setup_offload_act(act, entry, &index, extack);
  3290. if (err)
  3291. goto err_out_locked;
  3292. for (k = 0; k < index ; k++) {
  3293. entry[k].hw_stats = tc_act_hw_stats(act->hw_stats);
  3294. entry[k].hw_index = act->tcfa_index;
  3295. entry[k].cookie = (unsigned long)act;
  3296. entry[k].miss_cookie =
  3297. tcf_exts_miss_cookie_get(miss_cookie_base, i);
  3298. }
  3299. j += index;
  3300. spin_unlock_bh(&act->tcfa_lock);
  3301. }
  3302. err_out:
  3303. if (err)
  3304. tc_cleanup_offload_action(flow_action);
  3305. return err;
  3306. err_out_locked:
  3307. spin_unlock_bh(&act->tcfa_lock);
  3308. goto err_out;
  3309. }
  3310. int tc_setup_offload_action(struct flow_action *flow_action,
  3311. const struct tcf_exts *exts,
  3312. struct netlink_ext_ack *extack)
  3313. {
  3314. #ifdef CONFIG_NET_CLS_ACT
  3315. u32 miss_cookie_base;
  3316. if (!exts)
  3317. return 0;
  3318. miss_cookie_base = exts->miss_cookie_node ?
  3319. exts->miss_cookie_node->miss_cookie_base : 0;
  3320. return tc_setup_action(flow_action, exts->actions, miss_cookie_base,
  3321. extack);
  3322. #else
  3323. return 0;
  3324. #endif
  3325. }
  3326. EXPORT_SYMBOL(tc_setup_offload_action);
  3327. unsigned int tcf_exts_num_actions(struct tcf_exts *exts)
  3328. {
  3329. unsigned int num_acts = 0;
  3330. struct tc_action *act;
  3331. int i;
  3332. tcf_exts_for_each_action(i, act, exts) {
  3333. if (is_tcf_pedit(act))
  3334. num_acts += tcf_pedit_nkeys(act);
  3335. else
  3336. num_acts++;
  3337. }
  3338. return num_acts;
  3339. }
  3340. EXPORT_SYMBOL(tcf_exts_num_actions);
  3341. #ifdef CONFIG_NET_CLS_ACT
  3342. static int tcf_qevent_parse_block_index(struct nlattr *block_index_attr,
  3343. u32 *p_block_index,
  3344. struct netlink_ext_ack *extack)
  3345. {
  3346. *p_block_index = nla_get_u32(block_index_attr);
  3347. if (!*p_block_index) {
  3348. NL_SET_ERR_MSG(extack, "Block number may not be zero");
  3349. return -EINVAL;
  3350. }
  3351. return 0;
  3352. }
  3353. int tcf_qevent_init(struct tcf_qevent *qe, struct Qdisc *sch,
  3354. enum flow_block_binder_type binder_type,
  3355. struct nlattr *block_index_attr,
  3356. struct netlink_ext_ack *extack)
  3357. {
  3358. u32 block_index;
  3359. int err;
  3360. if (!block_index_attr)
  3361. return 0;
  3362. err = tcf_qevent_parse_block_index(block_index_attr, &block_index, extack);
  3363. if (err)
  3364. return err;
  3365. qe->info.binder_type = binder_type;
  3366. qe->info.chain_head_change = tcf_chain_head_change_dflt;
  3367. qe->info.chain_head_change_priv = &qe->filter_chain;
  3368. qe->info.block_index = block_index;
  3369. return tcf_block_get_ext(&qe->block, sch, &qe->info, extack);
  3370. }
  3371. EXPORT_SYMBOL(tcf_qevent_init);
  3372. void tcf_qevent_destroy(struct tcf_qevent *qe, struct Qdisc *sch)
  3373. {
  3374. if (qe->info.block_index)
  3375. tcf_block_put_ext(qe->block, sch, &qe->info);
  3376. }
  3377. EXPORT_SYMBOL(tcf_qevent_destroy);
  3378. int tcf_qevent_validate_change(struct tcf_qevent *qe, struct nlattr *block_index_attr,
  3379. struct netlink_ext_ack *extack)
  3380. {
  3381. u32 block_index;
  3382. int err;
  3383. if (!block_index_attr)
  3384. return 0;
  3385. err = tcf_qevent_parse_block_index(block_index_attr, &block_index, extack);
  3386. if (err)
  3387. return err;
  3388. /* Bounce newly-configured block or change in block. */
  3389. if (block_index != qe->info.block_index) {
  3390. NL_SET_ERR_MSG(extack, "Change of blocks is not supported");
  3391. return -EINVAL;
  3392. }
  3393. return 0;
  3394. }
  3395. EXPORT_SYMBOL(tcf_qevent_validate_change);
  3396. struct sk_buff *tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, struct sk_buff *skb,
  3397. struct sk_buff **to_free, int *ret)
  3398. {
  3399. struct tcf_result cl_res;
  3400. struct tcf_proto *fl;
  3401. if (!qe->info.block_index)
  3402. return skb;
  3403. fl = rcu_dereference_bh(qe->filter_chain);
  3404. switch (tcf_classify(skb, NULL, fl, &cl_res, false)) {
  3405. case TC_ACT_SHOT:
  3406. qdisc_qstats_drop(sch);
  3407. __qdisc_drop(skb, to_free);
  3408. *ret = __NET_XMIT_BYPASS;
  3409. return NULL;
  3410. case TC_ACT_STOLEN:
  3411. case TC_ACT_QUEUED:
  3412. case TC_ACT_TRAP:
  3413. __qdisc_drop(skb, to_free);
  3414. *ret = __NET_XMIT_STOLEN;
  3415. return NULL;
  3416. case TC_ACT_REDIRECT:
  3417. skb_do_redirect(skb);
  3418. *ret = __NET_XMIT_STOLEN;
  3419. return NULL;
  3420. }
  3421. return skb;
  3422. }
  3423. EXPORT_SYMBOL(tcf_qevent_handle);
  3424. int tcf_qevent_dump(struct sk_buff *skb, int attr_name, struct tcf_qevent *qe)
  3425. {
  3426. if (!qe->info.block_index)
  3427. return 0;
  3428. return nla_put_u32(skb, attr_name, qe->info.block_index);
  3429. }
  3430. EXPORT_SYMBOL(tcf_qevent_dump);
  3431. #endif
  3432. static __net_init int tcf_net_init(struct net *net)
  3433. {
  3434. struct tcf_net *tn = net_generic(net, tcf_net_id);
  3435. spin_lock_init(&tn->idr_lock);
  3436. idr_init(&tn->idr);
  3437. return 0;
  3438. }
  3439. static void __net_exit tcf_net_exit(struct net *net)
  3440. {
  3441. struct tcf_net *tn = net_generic(net, tcf_net_id);
  3442. idr_destroy(&tn->idr);
  3443. }
  3444. static struct pernet_operations tcf_net_ops = {
  3445. .init = tcf_net_init,
  3446. .exit = tcf_net_exit,
  3447. .id = &tcf_net_id,
  3448. .size = sizeof(struct tcf_net),
  3449. };
  3450. static int __init tc_filter_init(void)
  3451. {
  3452. int err;
  3453. tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
  3454. if (!tc_filter_wq)
  3455. return -ENOMEM;
  3456. err = register_pernet_subsys(&tcf_net_ops);
  3457. if (err)
  3458. goto err_register_pernet_subsys;
  3459. xa_init_flags(&tcf_exts_miss_cookies_xa, XA_FLAGS_ALLOC1);
  3460. rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL,
  3461. RTNL_FLAG_DOIT_UNLOCKED);
  3462. rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL,
  3463. RTNL_FLAG_DOIT_UNLOCKED);
  3464. rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
  3465. tc_dump_tfilter, RTNL_FLAG_DOIT_UNLOCKED);
  3466. rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
  3467. rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
  3468. rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
  3469. tc_dump_chain, 0);
  3470. return 0;
  3471. err_register_pernet_subsys:
  3472. destroy_workqueue(tc_filter_wq);
  3473. return err;
  3474. }
  3475. subsys_initcall(tc_filter_init);