cls_api.c 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104
  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 < 0)
  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. int ret = -EMSGSIZE;
  1724. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
  1725. if (!nlh)
  1726. goto out_nlmsg_trim;
  1727. tcm = nlmsg_data(nlh);
  1728. tcm->tcm_family = AF_UNSPEC;
  1729. tcm->tcm__pad1 = 0;
  1730. tcm->tcm__pad2 = 0;
  1731. if (q) {
  1732. tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
  1733. tcm->tcm_parent = parent;
  1734. } else {
  1735. tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
  1736. tcm->tcm_block_index = block->index;
  1737. }
  1738. tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
  1739. if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
  1740. goto nla_put_failure;
  1741. if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
  1742. goto nla_put_failure;
  1743. if (!fh) {
  1744. tcm->tcm_handle = 0;
  1745. } else if (terse_dump) {
  1746. if (tp->ops->terse_dump) {
  1747. if (tp->ops->terse_dump(net, tp, fh, skb, tcm,
  1748. rtnl_held) < 0)
  1749. goto nla_put_failure;
  1750. } else {
  1751. goto cls_op_not_supp;
  1752. }
  1753. } else {
  1754. if (tp->ops->dump &&
  1755. tp->ops->dump(net, tp, fh, skb, tcm, rtnl_held) < 0)
  1756. goto nla_put_failure;
  1757. }
  1758. if (extack && extack->_msg &&
  1759. nla_put_string(skb, TCA_EXT_WARN_MSG, extack->_msg))
  1760. goto nla_put_failure;
  1761. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  1762. return skb->len;
  1763. cls_op_not_supp:
  1764. ret = -EOPNOTSUPP;
  1765. out_nlmsg_trim:
  1766. nla_put_failure:
  1767. nlmsg_trim(skb, b);
  1768. return ret;
  1769. }
  1770. static struct sk_buff *tfilter_notify_prep(struct net *net,
  1771. struct sk_buff *oskb,
  1772. struct nlmsghdr *n,
  1773. struct tcf_proto *tp,
  1774. struct tcf_block *block,
  1775. struct Qdisc *q, u32 parent,
  1776. void *fh, int event,
  1777. u32 portid, bool rtnl_held,
  1778. struct netlink_ext_ack *extack)
  1779. {
  1780. unsigned int size = oskb ? max(NLMSG_GOODSIZE, oskb->len) : NLMSG_GOODSIZE;
  1781. struct sk_buff *skb;
  1782. int ret;
  1783. retry:
  1784. skb = alloc_skb(size, GFP_KERNEL);
  1785. if (!skb)
  1786. return ERR_PTR(-ENOBUFS);
  1787. ret = tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
  1788. n->nlmsg_seq, n->nlmsg_flags, event, false,
  1789. rtnl_held, extack);
  1790. if (ret <= 0) {
  1791. kfree_skb(skb);
  1792. if (ret == -EMSGSIZE) {
  1793. size += NLMSG_GOODSIZE;
  1794. goto retry;
  1795. }
  1796. return ERR_PTR(-EINVAL);
  1797. }
  1798. return skb;
  1799. }
  1800. static int tfilter_notify(struct net *net, struct sk_buff *oskb,
  1801. struct nlmsghdr *n, struct tcf_proto *tp,
  1802. struct tcf_block *block, struct Qdisc *q,
  1803. u32 parent, void *fh, int event, bool unicast,
  1804. bool rtnl_held, struct netlink_ext_ack *extack)
  1805. {
  1806. struct sk_buff *skb;
  1807. u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
  1808. int err = 0;
  1809. if (!unicast && !rtnl_notify_needed(net, n->nlmsg_flags, RTNLGRP_TC))
  1810. return 0;
  1811. skb = tfilter_notify_prep(net, oskb, n, tp, block, q, parent, fh, event,
  1812. portid, rtnl_held, extack);
  1813. if (IS_ERR(skb))
  1814. return PTR_ERR(skb);
  1815. if (unicast)
  1816. err = rtnl_unicast(skb, net, portid);
  1817. else
  1818. err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  1819. n->nlmsg_flags & NLM_F_ECHO);
  1820. return err;
  1821. }
  1822. static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
  1823. struct nlmsghdr *n, struct tcf_proto *tp,
  1824. struct tcf_block *block, struct Qdisc *q,
  1825. u32 parent, void *fh, bool *last, bool rtnl_held,
  1826. struct netlink_ext_ack *extack)
  1827. {
  1828. struct sk_buff *skb;
  1829. u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
  1830. int err;
  1831. if (!rtnl_notify_needed(net, n->nlmsg_flags, RTNLGRP_TC))
  1832. return tp->ops->delete(tp, fh, last, rtnl_held, extack);
  1833. skb = tfilter_notify_prep(net, oskb, n, tp, block, q, parent, fh,
  1834. RTM_DELTFILTER, portid, rtnl_held, extack);
  1835. if (IS_ERR(skb)) {
  1836. NL_SET_ERR_MSG(extack, "Failed to build del event notification");
  1837. return PTR_ERR(skb);
  1838. }
  1839. err = tp->ops->delete(tp, fh, last, rtnl_held, extack);
  1840. if (err) {
  1841. kfree_skb(skb);
  1842. return err;
  1843. }
  1844. err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  1845. n->nlmsg_flags & NLM_F_ECHO);
  1846. if (err < 0)
  1847. NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
  1848. return err;
  1849. }
  1850. static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
  1851. struct tcf_block *block, struct Qdisc *q,
  1852. u32 parent, struct nlmsghdr *n,
  1853. struct tcf_chain *chain, int event,
  1854. struct netlink_ext_ack *extack)
  1855. {
  1856. struct tcf_proto *tp;
  1857. for (tp = tcf_get_next_proto(chain, NULL);
  1858. tp; tp = tcf_get_next_proto(chain, tp))
  1859. tfilter_notify(net, oskb, n, tp, block, q, parent, NULL,
  1860. event, false, true, extack);
  1861. }
  1862. static void tfilter_put(struct tcf_proto *tp, void *fh)
  1863. {
  1864. if (tp->ops->put && fh)
  1865. tp->ops->put(tp, fh);
  1866. }
  1867. static bool is_qdisc_ingress(__u32 classid)
  1868. {
  1869. return (TC_H_MIN(classid) == TC_H_MIN(TC_H_MIN_INGRESS));
  1870. }
  1871. static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
  1872. struct netlink_ext_ack *extack)
  1873. {
  1874. struct net *net = sock_net(skb->sk);
  1875. struct nlattr *tca[TCA_MAX + 1];
  1876. char name[IFNAMSIZ];
  1877. struct tcmsg *t;
  1878. u32 protocol;
  1879. u32 prio;
  1880. bool prio_allocate;
  1881. u32 parent;
  1882. u32 chain_index;
  1883. struct Qdisc *q;
  1884. struct tcf_chain_info chain_info;
  1885. struct tcf_chain *chain;
  1886. struct tcf_block *block;
  1887. struct tcf_proto *tp;
  1888. unsigned long cl;
  1889. void *fh;
  1890. int err;
  1891. int tp_created;
  1892. bool rtnl_held = false;
  1893. u32 flags;
  1894. replay:
  1895. tp_created = 0;
  1896. err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
  1897. rtm_tca_policy, extack);
  1898. if (err < 0)
  1899. return err;
  1900. t = nlmsg_data(n);
  1901. protocol = TC_H_MIN(t->tcm_info);
  1902. prio = TC_H_MAJ(t->tcm_info);
  1903. prio_allocate = false;
  1904. parent = t->tcm_parent;
  1905. tp = NULL;
  1906. cl = 0;
  1907. block = NULL;
  1908. q = NULL;
  1909. chain = NULL;
  1910. flags = 0;
  1911. if (prio == 0) {
  1912. /* If no priority is provided by the user,
  1913. * we allocate one.
  1914. */
  1915. if (n->nlmsg_flags & NLM_F_CREATE) {
  1916. prio = TC_H_MAKE(0x80000000U, 0U);
  1917. prio_allocate = true;
  1918. } else {
  1919. NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
  1920. return -ENOENT;
  1921. }
  1922. }
  1923. /* Find head of filter chain. */
  1924. err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
  1925. if (err)
  1926. return err;
  1927. if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
  1928. NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
  1929. err = -EINVAL;
  1930. goto errout;
  1931. }
  1932. /* Take rtnl mutex if rtnl_held was set to true on previous iteration,
  1933. * block is shared (no qdisc found), qdisc is not unlocked, classifier
  1934. * type is not specified, classifier is not unlocked.
  1935. */
  1936. if (rtnl_held ||
  1937. (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
  1938. !tcf_proto_is_unlocked(name)) {
  1939. rtnl_held = true;
  1940. rtnl_lock();
  1941. }
  1942. err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
  1943. if (err)
  1944. goto errout;
  1945. block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
  1946. extack);
  1947. if (IS_ERR(block)) {
  1948. err = PTR_ERR(block);
  1949. goto errout;
  1950. }
  1951. block->classid = parent;
  1952. chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
  1953. if (chain_index > TC_ACT_EXT_VAL_MASK) {
  1954. NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
  1955. err = -EINVAL;
  1956. goto errout;
  1957. }
  1958. chain = tcf_chain_get(block, chain_index, true);
  1959. if (!chain) {
  1960. NL_SET_ERR_MSG(extack, "Cannot create specified filter chain");
  1961. err = -ENOMEM;
  1962. goto errout;
  1963. }
  1964. mutex_lock(&chain->filter_chain_lock);
  1965. tp = tcf_chain_tp_find(chain, &chain_info, protocol,
  1966. prio, prio_allocate);
  1967. if (IS_ERR(tp)) {
  1968. NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
  1969. err = PTR_ERR(tp);
  1970. goto errout_locked;
  1971. }
  1972. if (tp == NULL) {
  1973. struct tcf_proto *tp_new = NULL;
  1974. if (chain->flushing) {
  1975. err = -EAGAIN;
  1976. goto errout_locked;
  1977. }
  1978. /* Proto-tcf does not exist, create new one */
  1979. if (tca[TCA_KIND] == NULL || !protocol) {
  1980. NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
  1981. err = -EINVAL;
  1982. goto errout_locked;
  1983. }
  1984. if (!(n->nlmsg_flags & NLM_F_CREATE)) {
  1985. NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
  1986. err = -ENOENT;
  1987. goto errout_locked;
  1988. }
  1989. if (prio_allocate)
  1990. prio = tcf_auto_prio(tcf_chain_tp_prev(chain,
  1991. &chain_info));
  1992. mutex_unlock(&chain->filter_chain_lock);
  1993. tp_new = tcf_proto_create(name, protocol, prio, chain,
  1994. rtnl_held, extack);
  1995. if (IS_ERR(tp_new)) {
  1996. err = PTR_ERR(tp_new);
  1997. goto errout_tp;
  1998. }
  1999. tp_created = 1;
  2000. tp = tcf_chain_tp_insert_unique(chain, tp_new, protocol, prio,
  2001. rtnl_held);
  2002. if (IS_ERR(tp)) {
  2003. err = PTR_ERR(tp);
  2004. goto errout_tp;
  2005. }
  2006. } else {
  2007. mutex_unlock(&chain->filter_chain_lock);
  2008. }
  2009. if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
  2010. NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
  2011. err = -EINVAL;
  2012. goto errout;
  2013. }
  2014. fh = tp->ops->get(tp, t->tcm_handle);
  2015. if (!fh) {
  2016. if (!(n->nlmsg_flags & NLM_F_CREATE)) {
  2017. NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
  2018. err = -ENOENT;
  2019. goto errout;
  2020. }
  2021. } else if (n->nlmsg_flags & NLM_F_EXCL) {
  2022. tfilter_put(tp, fh);
  2023. NL_SET_ERR_MSG(extack, "Filter already exists");
  2024. err = -EEXIST;
  2025. goto errout;
  2026. }
  2027. if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
  2028. tfilter_put(tp, fh);
  2029. NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
  2030. err = -EINVAL;
  2031. goto errout;
  2032. }
  2033. if (!(n->nlmsg_flags & NLM_F_CREATE))
  2034. flags |= TCA_ACT_FLAGS_REPLACE;
  2035. if (!rtnl_held)
  2036. flags |= TCA_ACT_FLAGS_NO_RTNL;
  2037. if (is_qdisc_ingress(parent))
  2038. flags |= TCA_ACT_FLAGS_AT_INGRESS;
  2039. err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
  2040. flags, extack);
  2041. if (err == 0) {
  2042. tfilter_notify(net, skb, n, tp, block, q, parent, fh,
  2043. RTM_NEWTFILTER, false, rtnl_held, extack);
  2044. tfilter_put(tp, fh);
  2045. tcf_proto_count_usesw(tp, true);
  2046. /* q pointer is NULL for shared blocks */
  2047. if (q)
  2048. q->flags &= ~TCQ_F_CAN_BYPASS;
  2049. }
  2050. errout:
  2051. if (err && tp_created)
  2052. tcf_chain_tp_delete_empty(chain, tp, rtnl_held, NULL);
  2053. errout_tp:
  2054. if (chain) {
  2055. if (tp && !IS_ERR(tp))
  2056. tcf_proto_put(tp, rtnl_held, NULL);
  2057. if (!tp_created)
  2058. tcf_chain_put(chain);
  2059. }
  2060. tcf_block_release(q, block, rtnl_held);
  2061. if (rtnl_held)
  2062. rtnl_unlock();
  2063. if (err == -EAGAIN) {
  2064. /* Take rtnl lock in case EAGAIN is caused by concurrent flush
  2065. * of target chain.
  2066. */
  2067. rtnl_held = true;
  2068. /* Replay the request. */
  2069. goto replay;
  2070. }
  2071. return err;
  2072. errout_locked:
  2073. mutex_unlock(&chain->filter_chain_lock);
  2074. goto errout;
  2075. }
  2076. static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
  2077. struct netlink_ext_ack *extack)
  2078. {
  2079. struct net *net = sock_net(skb->sk);
  2080. struct nlattr *tca[TCA_MAX + 1];
  2081. char name[IFNAMSIZ];
  2082. struct tcmsg *t;
  2083. u32 protocol;
  2084. u32 prio;
  2085. u32 parent;
  2086. u32 chain_index;
  2087. struct Qdisc *q = NULL;
  2088. struct tcf_chain_info chain_info;
  2089. struct tcf_chain *chain = NULL;
  2090. struct tcf_block *block = NULL;
  2091. struct tcf_proto *tp = NULL;
  2092. unsigned long cl = 0;
  2093. void *fh = NULL;
  2094. int err;
  2095. bool rtnl_held = false;
  2096. err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
  2097. rtm_tca_policy, extack);
  2098. if (err < 0)
  2099. return err;
  2100. t = nlmsg_data(n);
  2101. protocol = TC_H_MIN(t->tcm_info);
  2102. prio = TC_H_MAJ(t->tcm_info);
  2103. parent = t->tcm_parent;
  2104. if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
  2105. NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
  2106. return -ENOENT;
  2107. }
  2108. /* Find head of filter chain. */
  2109. err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
  2110. if (err)
  2111. return err;
  2112. if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
  2113. NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
  2114. err = -EINVAL;
  2115. goto errout;
  2116. }
  2117. /* Take rtnl mutex if flushing whole chain, block is shared (no qdisc
  2118. * found), qdisc is not unlocked, classifier type is not specified,
  2119. * classifier is not unlocked.
  2120. */
  2121. if (!prio ||
  2122. (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
  2123. !tcf_proto_is_unlocked(name)) {
  2124. rtnl_held = true;
  2125. rtnl_lock();
  2126. }
  2127. err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
  2128. if (err)
  2129. goto errout;
  2130. block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
  2131. extack);
  2132. if (IS_ERR(block)) {
  2133. err = PTR_ERR(block);
  2134. goto errout;
  2135. }
  2136. chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
  2137. if (chain_index > TC_ACT_EXT_VAL_MASK) {
  2138. NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
  2139. err = -EINVAL;
  2140. goto errout;
  2141. }
  2142. chain = tcf_chain_get(block, chain_index, false);
  2143. if (!chain) {
  2144. /* User requested flush on non-existent chain. Nothing to do,
  2145. * so just return success.
  2146. */
  2147. if (prio == 0) {
  2148. err = 0;
  2149. goto errout;
  2150. }
  2151. NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
  2152. err = -ENOENT;
  2153. goto errout;
  2154. }
  2155. if (prio == 0) {
  2156. tfilter_notify_chain(net, skb, block, q, parent, n,
  2157. chain, RTM_DELTFILTER, extack);
  2158. tcf_chain_flush(chain, rtnl_held);
  2159. err = 0;
  2160. goto errout;
  2161. }
  2162. mutex_lock(&chain->filter_chain_lock);
  2163. tp = tcf_chain_tp_find(chain, &chain_info, protocol,
  2164. prio, false);
  2165. if (!tp || IS_ERR(tp)) {
  2166. NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
  2167. err = tp ? PTR_ERR(tp) : -ENOENT;
  2168. goto errout_locked;
  2169. } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
  2170. NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
  2171. err = -EINVAL;
  2172. goto errout_locked;
  2173. } else if (t->tcm_handle == 0) {
  2174. tcf_proto_signal_destroying(chain, tp);
  2175. tcf_chain_tp_remove(chain, &chain_info, tp);
  2176. mutex_unlock(&chain->filter_chain_lock);
  2177. tcf_proto_put(tp, rtnl_held, NULL);
  2178. tfilter_notify(net, skb, n, tp, block, q, parent, fh,
  2179. RTM_DELTFILTER, false, rtnl_held, extack);
  2180. err = 0;
  2181. goto errout;
  2182. }
  2183. mutex_unlock(&chain->filter_chain_lock);
  2184. fh = tp->ops->get(tp, t->tcm_handle);
  2185. if (!fh) {
  2186. NL_SET_ERR_MSG(extack, "Specified filter handle not found");
  2187. err = -ENOENT;
  2188. } else {
  2189. bool last;
  2190. err = tfilter_del_notify(net, skb, n, tp, block, q, parent, fh,
  2191. &last, rtnl_held, extack);
  2192. if (err)
  2193. goto errout;
  2194. if (last)
  2195. tcf_chain_tp_delete_empty(chain, tp, rtnl_held, extack);
  2196. }
  2197. errout:
  2198. if (chain) {
  2199. if (tp && !IS_ERR(tp))
  2200. tcf_proto_put(tp, rtnl_held, NULL);
  2201. tcf_chain_put(chain);
  2202. }
  2203. tcf_block_release(q, block, rtnl_held);
  2204. if (rtnl_held)
  2205. rtnl_unlock();
  2206. return err;
  2207. errout_locked:
  2208. mutex_unlock(&chain->filter_chain_lock);
  2209. goto errout;
  2210. }
  2211. static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
  2212. struct netlink_ext_ack *extack)
  2213. {
  2214. struct net *net = sock_net(skb->sk);
  2215. struct nlattr *tca[TCA_MAX + 1];
  2216. char name[IFNAMSIZ];
  2217. struct tcmsg *t;
  2218. u32 protocol;
  2219. u32 prio;
  2220. u32 parent;
  2221. u32 chain_index;
  2222. struct Qdisc *q = NULL;
  2223. struct tcf_chain_info chain_info;
  2224. struct tcf_chain *chain = NULL;
  2225. struct tcf_block *block = NULL;
  2226. struct tcf_proto *tp = NULL;
  2227. unsigned long cl = 0;
  2228. void *fh = NULL;
  2229. int err;
  2230. bool rtnl_held = false;
  2231. err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
  2232. rtm_tca_policy, extack);
  2233. if (err < 0)
  2234. return err;
  2235. t = nlmsg_data(n);
  2236. protocol = TC_H_MIN(t->tcm_info);
  2237. prio = TC_H_MAJ(t->tcm_info);
  2238. parent = t->tcm_parent;
  2239. if (prio == 0) {
  2240. NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
  2241. return -ENOENT;
  2242. }
  2243. /* Find head of filter chain. */
  2244. err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
  2245. if (err)
  2246. return err;
  2247. if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
  2248. NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
  2249. err = -EINVAL;
  2250. goto errout;
  2251. }
  2252. /* Take rtnl mutex if block is shared (no qdisc found), qdisc is not
  2253. * unlocked, classifier type is not specified, classifier is not
  2254. * unlocked.
  2255. */
  2256. if ((q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
  2257. !tcf_proto_is_unlocked(name)) {
  2258. rtnl_held = true;
  2259. rtnl_lock();
  2260. }
  2261. err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
  2262. if (err)
  2263. goto errout;
  2264. block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
  2265. extack);
  2266. if (IS_ERR(block)) {
  2267. err = PTR_ERR(block);
  2268. goto errout;
  2269. }
  2270. chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
  2271. if (chain_index > TC_ACT_EXT_VAL_MASK) {
  2272. NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
  2273. err = -EINVAL;
  2274. goto errout;
  2275. }
  2276. chain = tcf_chain_get(block, chain_index, false);
  2277. if (!chain) {
  2278. NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
  2279. err = -EINVAL;
  2280. goto errout;
  2281. }
  2282. mutex_lock(&chain->filter_chain_lock);
  2283. tp = tcf_chain_tp_find(chain, &chain_info, protocol,
  2284. prio, false);
  2285. mutex_unlock(&chain->filter_chain_lock);
  2286. if (!tp || IS_ERR(tp)) {
  2287. NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
  2288. err = tp ? PTR_ERR(tp) : -ENOENT;
  2289. goto errout;
  2290. } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
  2291. NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
  2292. err = -EINVAL;
  2293. goto errout;
  2294. }
  2295. fh = tp->ops->get(tp, t->tcm_handle);
  2296. if (!fh) {
  2297. NL_SET_ERR_MSG(extack, "Specified filter handle not found");
  2298. err = -ENOENT;
  2299. } else {
  2300. err = tfilter_notify(net, skb, n, tp, block, q, parent,
  2301. fh, RTM_NEWTFILTER, true, rtnl_held, NULL);
  2302. if (err < 0)
  2303. NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
  2304. }
  2305. tfilter_put(tp, fh);
  2306. errout:
  2307. if (chain) {
  2308. if (tp && !IS_ERR(tp))
  2309. tcf_proto_put(tp, rtnl_held, NULL);
  2310. tcf_chain_put(chain);
  2311. }
  2312. tcf_block_release(q, block, rtnl_held);
  2313. if (rtnl_held)
  2314. rtnl_unlock();
  2315. return err;
  2316. }
  2317. struct tcf_dump_args {
  2318. struct tcf_walker w;
  2319. struct sk_buff *skb;
  2320. struct netlink_callback *cb;
  2321. struct tcf_block *block;
  2322. struct Qdisc *q;
  2323. u32 parent;
  2324. bool terse_dump;
  2325. };
  2326. static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
  2327. {
  2328. struct tcf_dump_args *a = (void *)arg;
  2329. struct net *net = sock_net(a->skb->sk);
  2330. return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
  2331. n, NETLINK_CB(a->cb->skb).portid,
  2332. a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
  2333. RTM_NEWTFILTER, a->terse_dump, true, NULL);
  2334. }
  2335. static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
  2336. struct sk_buff *skb, struct netlink_callback *cb,
  2337. long index_start, long *p_index, bool terse)
  2338. {
  2339. struct net *net = sock_net(skb->sk);
  2340. struct tcf_block *block = chain->block;
  2341. struct tcmsg *tcm = nlmsg_data(cb->nlh);
  2342. struct tcf_proto *tp, *tp_prev;
  2343. struct tcf_dump_args arg;
  2344. for (tp = __tcf_get_next_proto(chain, NULL);
  2345. tp;
  2346. tp_prev = tp,
  2347. tp = __tcf_get_next_proto(chain, tp),
  2348. tcf_proto_put(tp_prev, true, NULL),
  2349. (*p_index)++) {
  2350. if (*p_index < index_start)
  2351. continue;
  2352. if (TC_H_MAJ(tcm->tcm_info) &&
  2353. TC_H_MAJ(tcm->tcm_info) != tp->prio)
  2354. continue;
  2355. if (TC_H_MIN(tcm->tcm_info) &&
  2356. TC_H_MIN(tcm->tcm_info) != tp->protocol)
  2357. continue;
  2358. if (*p_index > index_start)
  2359. memset(&cb->args[1], 0,
  2360. sizeof(cb->args) - sizeof(cb->args[0]));
  2361. if (cb->args[1] == 0) {
  2362. if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
  2363. NETLINK_CB(cb->skb).portid,
  2364. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  2365. RTM_NEWTFILTER, false, true, NULL) <= 0)
  2366. goto errout;
  2367. cb->args[1] = 1;
  2368. }
  2369. if (!tp->ops->walk)
  2370. continue;
  2371. arg.w.fn = tcf_node_dump;
  2372. arg.skb = skb;
  2373. arg.cb = cb;
  2374. arg.block = block;
  2375. arg.q = q;
  2376. arg.parent = parent;
  2377. arg.w.stop = 0;
  2378. arg.w.skip = cb->args[1] - 1;
  2379. arg.w.count = 0;
  2380. arg.w.cookie = cb->args[2];
  2381. arg.terse_dump = terse;
  2382. tp->ops->walk(tp, &arg.w, true);
  2383. cb->args[2] = arg.w.cookie;
  2384. cb->args[1] = arg.w.count + 1;
  2385. if (arg.w.stop)
  2386. goto errout;
  2387. }
  2388. return true;
  2389. errout:
  2390. tcf_proto_put(tp, true, NULL);
  2391. return false;
  2392. }
  2393. static const struct nla_policy tcf_tfilter_dump_policy[TCA_MAX + 1] = {
  2394. [TCA_CHAIN] = { .type = NLA_U32 },
  2395. [TCA_DUMP_FLAGS] = NLA_POLICY_BITFIELD32(TCA_DUMP_FLAGS_TERSE),
  2396. };
  2397. /* called with RTNL */
  2398. static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
  2399. {
  2400. struct tcf_chain *chain, *chain_prev;
  2401. struct net *net = sock_net(skb->sk);
  2402. struct nlattr *tca[TCA_MAX + 1];
  2403. struct Qdisc *q = NULL;
  2404. struct tcf_block *block;
  2405. struct tcmsg *tcm = nlmsg_data(cb->nlh);
  2406. bool terse_dump = false;
  2407. long index_start;
  2408. long index;
  2409. u32 parent;
  2410. int err;
  2411. if (nlmsg_len(cb->nlh) < sizeof(*tcm))
  2412. return skb->len;
  2413. err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
  2414. tcf_tfilter_dump_policy, cb->extack);
  2415. if (err)
  2416. return err;
  2417. if (tca[TCA_DUMP_FLAGS]) {
  2418. struct nla_bitfield32 flags =
  2419. nla_get_bitfield32(tca[TCA_DUMP_FLAGS]);
  2420. terse_dump = flags.value & TCA_DUMP_FLAGS_TERSE;
  2421. }
  2422. if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
  2423. block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
  2424. if (!block)
  2425. goto out;
  2426. /* If we work with block index, q is NULL and parent value
  2427. * will never be used in the following code. The check
  2428. * in tcf_fill_node prevents it. However, compiler does not
  2429. * see that far, so set parent to zero to silence the warning
  2430. * about parent being uninitialized.
  2431. */
  2432. parent = 0;
  2433. } else {
  2434. const struct Qdisc_class_ops *cops;
  2435. struct net_device *dev;
  2436. unsigned long cl = 0;
  2437. dev = __dev_get_by_index(net, tcm->tcm_ifindex);
  2438. if (!dev)
  2439. return skb->len;
  2440. parent = tcm->tcm_parent;
  2441. if (!parent)
  2442. q = rtnl_dereference(dev->qdisc);
  2443. else
  2444. q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
  2445. if (!q)
  2446. goto out;
  2447. cops = q->ops->cl_ops;
  2448. if (!cops)
  2449. goto out;
  2450. if (!cops->tcf_block)
  2451. goto out;
  2452. if (TC_H_MIN(tcm->tcm_parent)) {
  2453. cl = cops->find(q, tcm->tcm_parent);
  2454. if (cl == 0)
  2455. goto out;
  2456. }
  2457. block = cops->tcf_block(q, cl, NULL);
  2458. if (!block)
  2459. goto out;
  2460. parent = block->classid;
  2461. if (tcf_block_shared(block))
  2462. q = NULL;
  2463. }
  2464. index_start = cb->args[0];
  2465. index = 0;
  2466. for (chain = __tcf_get_next_chain(block, NULL);
  2467. chain;
  2468. chain_prev = chain,
  2469. chain = __tcf_get_next_chain(block, chain),
  2470. tcf_chain_put(chain_prev)) {
  2471. if (tca[TCA_CHAIN] &&
  2472. nla_get_u32(tca[TCA_CHAIN]) != chain->index)
  2473. continue;
  2474. if (!tcf_chain_dump(chain, q, parent, skb, cb,
  2475. index_start, &index, terse_dump)) {
  2476. tcf_chain_put(chain);
  2477. err = -EMSGSIZE;
  2478. break;
  2479. }
  2480. }
  2481. if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
  2482. tcf_block_refcnt_put(block, true);
  2483. cb->args[0] = index;
  2484. out:
  2485. /* If we did no progress, the error (EMSGSIZE) is real */
  2486. if (skb->len == 0 && err)
  2487. return err;
  2488. return skb->len;
  2489. }
  2490. static int tc_chain_fill_node(const struct tcf_proto_ops *tmplt_ops,
  2491. void *tmplt_priv, u32 chain_index,
  2492. struct net *net, struct sk_buff *skb,
  2493. struct tcf_block *block,
  2494. u32 portid, u32 seq, u16 flags, int event,
  2495. struct netlink_ext_ack *extack)
  2496. {
  2497. unsigned char *b = skb_tail_pointer(skb);
  2498. const struct tcf_proto_ops *ops;
  2499. struct nlmsghdr *nlh;
  2500. struct tcmsg *tcm;
  2501. void *priv;
  2502. ops = tmplt_ops;
  2503. priv = tmplt_priv;
  2504. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
  2505. if (!nlh)
  2506. goto out_nlmsg_trim;
  2507. tcm = nlmsg_data(nlh);
  2508. tcm->tcm_family = AF_UNSPEC;
  2509. tcm->tcm__pad1 = 0;
  2510. tcm->tcm__pad2 = 0;
  2511. tcm->tcm_handle = 0;
  2512. if (block->q) {
  2513. tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
  2514. tcm->tcm_parent = block->q->handle;
  2515. } else {
  2516. tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
  2517. tcm->tcm_block_index = block->index;
  2518. }
  2519. if (nla_put_u32(skb, TCA_CHAIN, chain_index))
  2520. goto nla_put_failure;
  2521. if (ops) {
  2522. if (nla_put_string(skb, TCA_KIND, ops->kind))
  2523. goto nla_put_failure;
  2524. if (ops->tmplt_dump(skb, net, priv) < 0)
  2525. goto nla_put_failure;
  2526. }
  2527. if (extack && extack->_msg &&
  2528. nla_put_string(skb, TCA_EXT_WARN_MSG, extack->_msg))
  2529. goto out_nlmsg_trim;
  2530. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  2531. return skb->len;
  2532. out_nlmsg_trim:
  2533. nla_put_failure:
  2534. nlmsg_trim(skb, b);
  2535. return -EMSGSIZE;
  2536. }
  2537. static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
  2538. u32 seq, u16 flags, int event, bool unicast,
  2539. struct netlink_ext_ack *extack)
  2540. {
  2541. u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
  2542. struct tcf_block *block = chain->block;
  2543. struct net *net = block->net;
  2544. struct sk_buff *skb;
  2545. int err = 0;
  2546. if (!unicast && !rtnl_notify_needed(net, flags, RTNLGRP_TC))
  2547. return 0;
  2548. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  2549. if (!skb)
  2550. return -ENOBUFS;
  2551. if (tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
  2552. chain->index, net, skb, block, portid,
  2553. seq, flags, event, extack) <= 0) {
  2554. kfree_skb(skb);
  2555. return -EINVAL;
  2556. }
  2557. if (unicast)
  2558. err = rtnl_unicast(skb, net, portid);
  2559. else
  2560. err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  2561. flags & NLM_F_ECHO);
  2562. return err;
  2563. }
  2564. static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
  2565. void *tmplt_priv, u32 chain_index,
  2566. struct tcf_block *block, struct sk_buff *oskb,
  2567. u32 seq, u16 flags)
  2568. {
  2569. u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
  2570. struct net *net = block->net;
  2571. struct sk_buff *skb;
  2572. if (!rtnl_notify_needed(net, flags, RTNLGRP_TC))
  2573. return 0;
  2574. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  2575. if (!skb)
  2576. return -ENOBUFS;
  2577. if (tc_chain_fill_node(tmplt_ops, tmplt_priv, chain_index, net, skb,
  2578. block, portid, seq, flags, RTM_DELCHAIN, NULL) <= 0) {
  2579. kfree_skb(skb);
  2580. return -EINVAL;
  2581. }
  2582. return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
  2583. }
  2584. static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
  2585. struct nlattr **tca,
  2586. struct netlink_ext_ack *extack)
  2587. {
  2588. const struct tcf_proto_ops *ops;
  2589. char name[IFNAMSIZ];
  2590. void *tmplt_priv;
  2591. /* If kind is not set, user did not specify template. */
  2592. if (!tca[TCA_KIND])
  2593. return 0;
  2594. if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
  2595. NL_SET_ERR_MSG(extack, "Specified TC chain template name too long");
  2596. return -EINVAL;
  2597. }
  2598. ops = tcf_proto_lookup_ops(name, true, extack);
  2599. if (IS_ERR(ops))
  2600. return PTR_ERR(ops);
  2601. if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump ||
  2602. !ops->tmplt_reoffload) {
  2603. NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
  2604. module_put(ops->owner);
  2605. return -EOPNOTSUPP;
  2606. }
  2607. tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
  2608. if (IS_ERR(tmplt_priv)) {
  2609. module_put(ops->owner);
  2610. return PTR_ERR(tmplt_priv);
  2611. }
  2612. chain->tmplt_ops = ops;
  2613. chain->tmplt_priv = tmplt_priv;
  2614. return 0;
  2615. }
  2616. static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
  2617. void *tmplt_priv)
  2618. {
  2619. /* If template ops are set, no work to do for us. */
  2620. if (!tmplt_ops)
  2621. return;
  2622. tmplt_ops->tmplt_destroy(tmplt_priv);
  2623. module_put(tmplt_ops->owner);
  2624. }
  2625. /* Add/delete/get a chain */
  2626. static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
  2627. struct netlink_ext_ack *extack)
  2628. {
  2629. struct net *net = sock_net(skb->sk);
  2630. struct nlattr *tca[TCA_MAX + 1];
  2631. struct tcmsg *t;
  2632. u32 parent;
  2633. u32 chain_index;
  2634. struct Qdisc *q;
  2635. struct tcf_chain *chain;
  2636. struct tcf_block *block;
  2637. unsigned long cl;
  2638. int err;
  2639. replay:
  2640. q = NULL;
  2641. err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
  2642. rtm_tca_policy, extack);
  2643. if (err < 0)
  2644. return err;
  2645. t = nlmsg_data(n);
  2646. parent = t->tcm_parent;
  2647. cl = 0;
  2648. block = tcf_block_find(net, &q, &parent, &cl,
  2649. t->tcm_ifindex, t->tcm_block_index, extack);
  2650. if (IS_ERR(block))
  2651. return PTR_ERR(block);
  2652. chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
  2653. if (chain_index > TC_ACT_EXT_VAL_MASK) {
  2654. NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
  2655. err = -EINVAL;
  2656. goto errout_block;
  2657. }
  2658. mutex_lock(&block->lock);
  2659. chain = tcf_chain_lookup(block, chain_index);
  2660. if (n->nlmsg_type == RTM_NEWCHAIN) {
  2661. if (chain) {
  2662. if (tcf_chain_held_by_acts_only(chain)) {
  2663. /* The chain exists only because there is
  2664. * some action referencing it.
  2665. */
  2666. tcf_chain_hold(chain);
  2667. } else {
  2668. NL_SET_ERR_MSG(extack, "Filter chain already exists");
  2669. err = -EEXIST;
  2670. goto errout_block_locked;
  2671. }
  2672. } else {
  2673. if (!(n->nlmsg_flags & NLM_F_CREATE)) {
  2674. NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
  2675. err = -ENOENT;
  2676. goto errout_block_locked;
  2677. }
  2678. chain = tcf_chain_create(block, chain_index);
  2679. if (!chain) {
  2680. NL_SET_ERR_MSG(extack, "Failed to create filter chain");
  2681. err = -ENOMEM;
  2682. goto errout_block_locked;
  2683. }
  2684. }
  2685. } else {
  2686. if (!chain || tcf_chain_held_by_acts_only(chain)) {
  2687. NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
  2688. err = -EINVAL;
  2689. goto errout_block_locked;
  2690. }
  2691. tcf_chain_hold(chain);
  2692. }
  2693. if (n->nlmsg_type == RTM_NEWCHAIN) {
  2694. /* Modifying chain requires holding parent block lock. In case
  2695. * the chain was successfully added, take a reference to the
  2696. * chain. This ensures that an empty chain does not disappear at
  2697. * the end of this function.
  2698. */
  2699. tcf_chain_hold(chain);
  2700. chain->explicitly_created = true;
  2701. }
  2702. mutex_unlock(&block->lock);
  2703. switch (n->nlmsg_type) {
  2704. case RTM_NEWCHAIN:
  2705. err = tc_chain_tmplt_add(chain, net, tca, extack);
  2706. if (err) {
  2707. tcf_chain_put_explicitly_created(chain);
  2708. goto errout;
  2709. }
  2710. tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
  2711. RTM_NEWCHAIN, false, extack);
  2712. break;
  2713. case RTM_DELCHAIN:
  2714. tfilter_notify_chain(net, skb, block, q, parent, n,
  2715. chain, RTM_DELTFILTER, extack);
  2716. /* Flush the chain first as the user requested chain removal. */
  2717. tcf_chain_flush(chain, true);
  2718. /* In case the chain was successfully deleted, put a reference
  2719. * to the chain previously taken during addition.
  2720. */
  2721. tcf_chain_put_explicitly_created(chain);
  2722. break;
  2723. case RTM_GETCHAIN:
  2724. err = tc_chain_notify(chain, skb, n->nlmsg_seq,
  2725. n->nlmsg_flags, n->nlmsg_type, true, extack);
  2726. if (err < 0)
  2727. NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
  2728. break;
  2729. default:
  2730. err = -EOPNOTSUPP;
  2731. NL_SET_ERR_MSG(extack, "Unsupported message type");
  2732. goto errout;
  2733. }
  2734. errout:
  2735. tcf_chain_put(chain);
  2736. errout_block:
  2737. tcf_block_release(q, block, true);
  2738. if (err == -EAGAIN)
  2739. /* Replay the request. */
  2740. goto replay;
  2741. return err;
  2742. errout_block_locked:
  2743. mutex_unlock(&block->lock);
  2744. goto errout_block;
  2745. }
  2746. /* called with RTNL */
  2747. static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
  2748. {
  2749. struct net *net = sock_net(skb->sk);
  2750. struct nlattr *tca[TCA_MAX + 1];
  2751. struct Qdisc *q = NULL;
  2752. struct tcf_block *block;
  2753. struct tcmsg *tcm = nlmsg_data(cb->nlh);
  2754. struct tcf_chain *chain;
  2755. long index_start;
  2756. long index;
  2757. int err;
  2758. if (nlmsg_len(cb->nlh) < sizeof(*tcm))
  2759. return skb->len;
  2760. err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
  2761. rtm_tca_policy, cb->extack);
  2762. if (err)
  2763. return err;
  2764. if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
  2765. block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
  2766. if (!block)
  2767. goto out;
  2768. } else {
  2769. const struct Qdisc_class_ops *cops;
  2770. struct net_device *dev;
  2771. unsigned long cl = 0;
  2772. dev = __dev_get_by_index(net, tcm->tcm_ifindex);
  2773. if (!dev)
  2774. return skb->len;
  2775. if (!tcm->tcm_parent)
  2776. q = rtnl_dereference(dev->qdisc);
  2777. else
  2778. q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
  2779. if (!q)
  2780. goto out;
  2781. cops = q->ops->cl_ops;
  2782. if (!cops)
  2783. goto out;
  2784. if (!cops->tcf_block)
  2785. goto out;
  2786. if (TC_H_MIN(tcm->tcm_parent)) {
  2787. cl = cops->find(q, tcm->tcm_parent);
  2788. if (cl == 0)
  2789. goto out;
  2790. }
  2791. block = cops->tcf_block(q, cl, NULL);
  2792. if (!block)
  2793. goto out;
  2794. if (tcf_block_shared(block))
  2795. q = NULL;
  2796. }
  2797. index_start = cb->args[0];
  2798. index = 0;
  2799. mutex_lock(&block->lock);
  2800. list_for_each_entry(chain, &block->chain_list, list) {
  2801. if ((tca[TCA_CHAIN] &&
  2802. nla_get_u32(tca[TCA_CHAIN]) != chain->index))
  2803. continue;
  2804. if (index < index_start) {
  2805. index++;
  2806. continue;
  2807. }
  2808. if (tcf_chain_held_by_acts_only(chain))
  2809. continue;
  2810. err = tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
  2811. chain->index, net, skb, block,
  2812. NETLINK_CB(cb->skb).portid,
  2813. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  2814. RTM_NEWCHAIN, NULL);
  2815. if (err <= 0)
  2816. break;
  2817. index++;
  2818. }
  2819. mutex_unlock(&block->lock);
  2820. if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
  2821. tcf_block_refcnt_put(block, true);
  2822. cb->args[0] = index;
  2823. out:
  2824. /* If we did no progress, the error (EMSGSIZE) is real */
  2825. if (skb->len == 0 && err)
  2826. return err;
  2827. return skb->len;
  2828. }
  2829. int tcf_exts_init_ex(struct tcf_exts *exts, struct net *net, int action,
  2830. int police, struct tcf_proto *tp, u32 handle,
  2831. bool use_action_miss)
  2832. {
  2833. int err = 0;
  2834. #ifdef CONFIG_NET_CLS_ACT
  2835. exts->type = 0;
  2836. exts->nr_actions = 0;
  2837. exts->miss_cookie_node = NULL;
  2838. /* Note: we do not own yet a reference on net.
  2839. * This reference might be taken later from tcf_exts_get_net().
  2840. */
  2841. exts->net = net;
  2842. exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),
  2843. GFP_KERNEL);
  2844. if (!exts->actions)
  2845. return -ENOMEM;
  2846. #endif
  2847. exts->action = action;
  2848. exts->police = police;
  2849. if (!use_action_miss)
  2850. return 0;
  2851. err = tcf_exts_miss_cookie_base_alloc(exts, tp, handle);
  2852. if (err)
  2853. goto err_miss_alloc;
  2854. return 0;
  2855. err_miss_alloc:
  2856. tcf_exts_destroy(exts);
  2857. #ifdef CONFIG_NET_CLS_ACT
  2858. exts->actions = NULL;
  2859. #endif
  2860. return err;
  2861. }
  2862. EXPORT_SYMBOL(tcf_exts_init_ex);
  2863. void tcf_exts_destroy(struct tcf_exts *exts)
  2864. {
  2865. tcf_exts_miss_cookie_base_destroy(exts);
  2866. #ifdef CONFIG_NET_CLS_ACT
  2867. if (exts->actions) {
  2868. tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
  2869. kfree(exts->actions);
  2870. }
  2871. exts->nr_actions = 0;
  2872. #endif
  2873. }
  2874. EXPORT_SYMBOL(tcf_exts_destroy);
  2875. int tcf_exts_validate_ex(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
  2876. struct nlattr *rate_tlv, struct tcf_exts *exts,
  2877. u32 flags, u32 fl_flags, struct netlink_ext_ack *extack)
  2878. {
  2879. #ifdef CONFIG_NET_CLS_ACT
  2880. {
  2881. int init_res[TCA_ACT_MAX_PRIO] = {};
  2882. struct tc_action *act;
  2883. size_t attr_size = 0;
  2884. if (exts->police && tb[exts->police]) {
  2885. struct tc_action_ops *a_o;
  2886. flags |= TCA_ACT_FLAGS_POLICE | TCA_ACT_FLAGS_BIND;
  2887. a_o = tc_action_load_ops(tb[exts->police], flags,
  2888. extack);
  2889. if (IS_ERR(a_o))
  2890. return PTR_ERR(a_o);
  2891. act = tcf_action_init_1(net, tp, tb[exts->police],
  2892. rate_tlv, a_o, init_res, flags,
  2893. extack);
  2894. module_put(a_o->owner);
  2895. if (IS_ERR(act))
  2896. return PTR_ERR(act);
  2897. act->type = exts->type = TCA_OLD_COMPAT;
  2898. exts->actions[0] = act;
  2899. exts->nr_actions = 1;
  2900. tcf_idr_insert_many(exts->actions, init_res);
  2901. } else if (exts->action && tb[exts->action]) {
  2902. int err;
  2903. flags |= TCA_ACT_FLAGS_BIND;
  2904. err = tcf_action_init(net, tp, tb[exts->action],
  2905. rate_tlv, exts->actions, init_res,
  2906. &attr_size, flags, fl_flags,
  2907. extack);
  2908. if (err < 0)
  2909. return err;
  2910. exts->nr_actions = err;
  2911. }
  2912. }
  2913. #else
  2914. if ((exts->action && tb[exts->action]) ||
  2915. (exts->police && tb[exts->police])) {
  2916. NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
  2917. return -EOPNOTSUPP;
  2918. }
  2919. #endif
  2920. return 0;
  2921. }
  2922. EXPORT_SYMBOL(tcf_exts_validate_ex);
  2923. int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
  2924. struct nlattr *rate_tlv, struct tcf_exts *exts,
  2925. u32 flags, struct netlink_ext_ack *extack)
  2926. {
  2927. return tcf_exts_validate_ex(net, tp, tb, rate_tlv, exts,
  2928. flags, 0, extack);
  2929. }
  2930. EXPORT_SYMBOL(tcf_exts_validate);
  2931. void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
  2932. {
  2933. #ifdef CONFIG_NET_CLS_ACT
  2934. struct tcf_exts old = *dst;
  2935. *dst = *src;
  2936. tcf_exts_destroy(&old);
  2937. #endif
  2938. }
  2939. EXPORT_SYMBOL(tcf_exts_change);
  2940. #ifdef CONFIG_NET_CLS_ACT
  2941. static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
  2942. {
  2943. if (exts->nr_actions == 0)
  2944. return NULL;
  2945. else
  2946. return exts->actions[0];
  2947. }
  2948. #endif
  2949. int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
  2950. {
  2951. #ifdef CONFIG_NET_CLS_ACT
  2952. struct nlattr *nest;
  2953. if (exts->action && tcf_exts_has_actions(exts)) {
  2954. /*
  2955. * again for backward compatible mode - we want
  2956. * to work with both old and new modes of entering
  2957. * tc data even if iproute2 was newer - jhs
  2958. */
  2959. if (exts->type != TCA_OLD_COMPAT) {
  2960. nest = nla_nest_start_noflag(skb, exts->action);
  2961. if (nest == NULL)
  2962. goto nla_put_failure;
  2963. if (tcf_action_dump(skb, exts->actions, 0, 0, false)
  2964. < 0)
  2965. goto nla_put_failure;
  2966. nla_nest_end(skb, nest);
  2967. } else if (exts->police) {
  2968. struct tc_action *act = tcf_exts_first_act(exts);
  2969. nest = nla_nest_start_noflag(skb, exts->police);
  2970. if (nest == NULL || !act)
  2971. goto nla_put_failure;
  2972. if (tcf_action_dump_old(skb, act, 0, 0) < 0)
  2973. goto nla_put_failure;
  2974. nla_nest_end(skb, nest);
  2975. }
  2976. }
  2977. return 0;
  2978. nla_put_failure:
  2979. nla_nest_cancel(skb, nest);
  2980. return -1;
  2981. #else
  2982. return 0;
  2983. #endif
  2984. }
  2985. EXPORT_SYMBOL(tcf_exts_dump);
  2986. int tcf_exts_terse_dump(struct sk_buff *skb, struct tcf_exts *exts)
  2987. {
  2988. #ifdef CONFIG_NET_CLS_ACT
  2989. struct nlattr *nest;
  2990. if (!exts->action || !tcf_exts_has_actions(exts))
  2991. return 0;
  2992. nest = nla_nest_start_noflag(skb, exts->action);
  2993. if (!nest)
  2994. goto nla_put_failure;
  2995. if (tcf_action_dump(skb, exts->actions, 0, 0, true) < 0)
  2996. goto nla_put_failure;
  2997. nla_nest_end(skb, nest);
  2998. return 0;
  2999. nla_put_failure:
  3000. nla_nest_cancel(skb, nest);
  3001. return -1;
  3002. #else
  3003. return 0;
  3004. #endif
  3005. }
  3006. EXPORT_SYMBOL(tcf_exts_terse_dump);
  3007. int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
  3008. {
  3009. #ifdef CONFIG_NET_CLS_ACT
  3010. struct tc_action *a = tcf_exts_first_act(exts);
  3011. if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
  3012. return -1;
  3013. #endif
  3014. return 0;
  3015. }
  3016. EXPORT_SYMBOL(tcf_exts_dump_stats);
  3017. static void tcf_block_offload_inc(struct tcf_block *block, u32 *flags)
  3018. {
  3019. if (*flags & TCA_CLS_FLAGS_IN_HW)
  3020. return;
  3021. *flags |= TCA_CLS_FLAGS_IN_HW;
  3022. atomic_inc(&block->offloadcnt);
  3023. }
  3024. static void tcf_block_offload_dec(struct tcf_block *block, u32 *flags)
  3025. {
  3026. if (!(*flags & TCA_CLS_FLAGS_IN_HW))
  3027. return;
  3028. *flags &= ~TCA_CLS_FLAGS_IN_HW;
  3029. atomic_dec(&block->offloadcnt);
  3030. }
  3031. static void tc_cls_offload_cnt_update(struct tcf_block *block,
  3032. struct tcf_proto *tp, u32 *cnt,
  3033. u32 *flags, u32 diff, bool add)
  3034. {
  3035. lockdep_assert_held(&block->cb_lock);
  3036. spin_lock(&tp->lock);
  3037. if (add) {
  3038. if (!*cnt)
  3039. tcf_block_offload_inc(block, flags);
  3040. *cnt += diff;
  3041. } else {
  3042. *cnt -= diff;
  3043. if (!*cnt)
  3044. tcf_block_offload_dec(block, flags);
  3045. }
  3046. spin_unlock(&tp->lock);
  3047. }
  3048. static void
  3049. tc_cls_offload_cnt_reset(struct tcf_block *block, struct tcf_proto *tp,
  3050. u32 *cnt, u32 *flags)
  3051. {
  3052. lockdep_assert_held(&block->cb_lock);
  3053. spin_lock(&tp->lock);
  3054. tcf_block_offload_dec(block, flags);
  3055. *cnt = 0;
  3056. spin_unlock(&tp->lock);
  3057. }
  3058. static int
  3059. __tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
  3060. void *type_data, bool err_stop)
  3061. {
  3062. struct flow_block_cb *block_cb;
  3063. int ok_count = 0;
  3064. int err;
  3065. list_for_each_entry(block_cb, &block->flow_block.cb_list, list) {
  3066. err = block_cb->cb(type, type_data, block_cb->cb_priv);
  3067. if (err) {
  3068. if (err_stop)
  3069. return err;
  3070. } else {
  3071. ok_count++;
  3072. }
  3073. }
  3074. return ok_count;
  3075. }
  3076. int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
  3077. void *type_data, bool err_stop, bool rtnl_held)
  3078. {
  3079. bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
  3080. int ok_count;
  3081. retry:
  3082. if (take_rtnl)
  3083. rtnl_lock();
  3084. down_read(&block->cb_lock);
  3085. /* Need to obtain rtnl lock if block is bound to devs that require it.
  3086. * In block bind code cb_lock is obtained while holding rtnl, so we must
  3087. * obtain the locks in same order here.
  3088. */
  3089. if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
  3090. up_read(&block->cb_lock);
  3091. take_rtnl = true;
  3092. goto retry;
  3093. }
  3094. ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
  3095. up_read(&block->cb_lock);
  3096. if (take_rtnl)
  3097. rtnl_unlock();
  3098. return ok_count;
  3099. }
  3100. EXPORT_SYMBOL(tc_setup_cb_call);
  3101. /* Non-destructive filter add. If filter that wasn't already in hardware is
  3102. * successfully offloaded, increment block offloads counter. On failure,
  3103. * previously offloaded filter is considered to be intact and offloads counter
  3104. * is not decremented.
  3105. */
  3106. int tc_setup_cb_add(struct tcf_block *block, struct tcf_proto *tp,
  3107. enum tc_setup_type type, void *type_data, bool err_stop,
  3108. u32 *flags, unsigned int *in_hw_count, bool rtnl_held)
  3109. {
  3110. bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
  3111. int ok_count;
  3112. retry:
  3113. if (take_rtnl)
  3114. rtnl_lock();
  3115. down_read(&block->cb_lock);
  3116. /* Need to obtain rtnl lock if block is bound to devs that require it.
  3117. * In block bind code cb_lock is obtained while holding rtnl, so we must
  3118. * obtain the locks in same order here.
  3119. */
  3120. if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
  3121. up_read(&block->cb_lock);
  3122. take_rtnl = true;
  3123. goto retry;
  3124. }
  3125. /* Make sure all netdevs sharing this block are offload-capable. */
  3126. if (block->nooffloaddevcnt && err_stop) {
  3127. ok_count = -EOPNOTSUPP;
  3128. goto err_unlock;
  3129. }
  3130. ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
  3131. if (ok_count < 0)
  3132. goto err_unlock;
  3133. if (tp->ops->hw_add)
  3134. tp->ops->hw_add(tp, type_data);
  3135. if (ok_count > 0)
  3136. tc_cls_offload_cnt_update(block, tp, in_hw_count, flags,
  3137. ok_count, true);
  3138. err_unlock:
  3139. up_read(&block->cb_lock);
  3140. if (take_rtnl)
  3141. rtnl_unlock();
  3142. return min(ok_count, 0);
  3143. }
  3144. EXPORT_SYMBOL(tc_setup_cb_add);
  3145. /* Destructive filter replace. If filter that wasn't already in hardware is
  3146. * successfully offloaded, increment block offload counter. On failure,
  3147. * previously offloaded filter is considered to be destroyed and offload counter
  3148. * is decremented.
  3149. */
  3150. int tc_setup_cb_replace(struct tcf_block *block, struct tcf_proto *tp,
  3151. enum tc_setup_type type, void *type_data, bool err_stop,
  3152. u32 *old_flags, unsigned int *old_in_hw_count,
  3153. u32 *new_flags, unsigned int *new_in_hw_count,
  3154. bool rtnl_held)
  3155. {
  3156. bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
  3157. int ok_count;
  3158. retry:
  3159. if (take_rtnl)
  3160. rtnl_lock();
  3161. down_read(&block->cb_lock);
  3162. /* Need to obtain rtnl lock if block is bound to devs that require it.
  3163. * In block bind code cb_lock is obtained while holding rtnl, so we must
  3164. * obtain the locks in same order here.
  3165. */
  3166. if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
  3167. up_read(&block->cb_lock);
  3168. take_rtnl = true;
  3169. goto retry;
  3170. }
  3171. /* Make sure all netdevs sharing this block are offload-capable. */
  3172. if (block->nooffloaddevcnt && err_stop) {
  3173. ok_count = -EOPNOTSUPP;
  3174. goto err_unlock;
  3175. }
  3176. tc_cls_offload_cnt_reset(block, tp, old_in_hw_count, old_flags);
  3177. if (tp->ops->hw_del)
  3178. tp->ops->hw_del(tp, type_data);
  3179. ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
  3180. if (ok_count < 0)
  3181. goto err_unlock;
  3182. if (tp->ops->hw_add)
  3183. tp->ops->hw_add(tp, type_data);
  3184. if (ok_count > 0)
  3185. tc_cls_offload_cnt_update(block, tp, new_in_hw_count,
  3186. new_flags, ok_count, true);
  3187. err_unlock:
  3188. up_read(&block->cb_lock);
  3189. if (take_rtnl)
  3190. rtnl_unlock();
  3191. return min(ok_count, 0);
  3192. }
  3193. EXPORT_SYMBOL(tc_setup_cb_replace);
  3194. /* Destroy filter and decrement block offload counter, if filter was previously
  3195. * offloaded.
  3196. */
  3197. int tc_setup_cb_destroy(struct tcf_block *block, struct tcf_proto *tp,
  3198. enum tc_setup_type type, void *type_data, bool err_stop,
  3199. u32 *flags, unsigned int *in_hw_count, bool rtnl_held)
  3200. {
  3201. bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
  3202. int ok_count;
  3203. retry:
  3204. if (take_rtnl)
  3205. rtnl_lock();
  3206. down_read(&block->cb_lock);
  3207. /* Need to obtain rtnl lock if block is bound to devs that require it.
  3208. * In block bind code cb_lock is obtained while holding rtnl, so we must
  3209. * obtain the locks in same order here.
  3210. */
  3211. if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
  3212. up_read(&block->cb_lock);
  3213. take_rtnl = true;
  3214. goto retry;
  3215. }
  3216. ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
  3217. tc_cls_offload_cnt_reset(block, tp, in_hw_count, flags);
  3218. if (tp->ops->hw_del)
  3219. tp->ops->hw_del(tp, type_data);
  3220. up_read(&block->cb_lock);
  3221. if (take_rtnl)
  3222. rtnl_unlock();
  3223. return min(ok_count, 0);
  3224. }
  3225. EXPORT_SYMBOL(tc_setup_cb_destroy);
  3226. int tc_setup_cb_reoffload(struct tcf_block *block, struct tcf_proto *tp,
  3227. bool add, flow_setup_cb_t *cb,
  3228. enum tc_setup_type type, void *type_data,
  3229. void *cb_priv, u32 *flags, unsigned int *in_hw_count)
  3230. {
  3231. int err = cb(type, type_data, cb_priv);
  3232. if (err) {
  3233. if (add && tc_skip_sw(*flags))
  3234. return err;
  3235. } else {
  3236. tc_cls_offload_cnt_update(block, tp, in_hw_count, flags, 1,
  3237. add);
  3238. }
  3239. return 0;
  3240. }
  3241. EXPORT_SYMBOL(tc_setup_cb_reoffload);
  3242. static int tcf_act_get_user_cookie(struct flow_action_entry *entry,
  3243. const struct tc_action *act)
  3244. {
  3245. struct tc_cookie *user_cookie;
  3246. int err = 0;
  3247. rcu_read_lock();
  3248. user_cookie = rcu_dereference(act->user_cookie);
  3249. if (user_cookie) {
  3250. entry->user_cookie = flow_action_cookie_create(user_cookie->data,
  3251. user_cookie->len,
  3252. GFP_ATOMIC);
  3253. if (!entry->user_cookie)
  3254. err = -ENOMEM;
  3255. }
  3256. rcu_read_unlock();
  3257. return err;
  3258. }
  3259. static void tcf_act_put_user_cookie(struct flow_action_entry *entry)
  3260. {
  3261. flow_action_cookie_destroy(entry->user_cookie);
  3262. }
  3263. void tc_cleanup_offload_action(struct flow_action *flow_action)
  3264. {
  3265. struct flow_action_entry *entry;
  3266. int i;
  3267. flow_action_for_each(i, entry, flow_action) {
  3268. tcf_act_put_user_cookie(entry);
  3269. if (entry->destructor)
  3270. entry->destructor(entry->destructor_priv);
  3271. }
  3272. }
  3273. EXPORT_SYMBOL(tc_cleanup_offload_action);
  3274. static int tc_setup_offload_act(struct tc_action *act,
  3275. struct flow_action_entry *entry,
  3276. u32 *index_inc,
  3277. struct netlink_ext_ack *extack)
  3278. {
  3279. #ifdef CONFIG_NET_CLS_ACT
  3280. if (act->ops->offload_act_setup) {
  3281. return act->ops->offload_act_setup(act, entry, index_inc, true,
  3282. extack);
  3283. } else {
  3284. NL_SET_ERR_MSG(extack, "Action does not support offload");
  3285. return -EOPNOTSUPP;
  3286. }
  3287. #else
  3288. return 0;
  3289. #endif
  3290. }
  3291. int tc_setup_action(struct flow_action *flow_action,
  3292. struct tc_action *actions[],
  3293. u32 miss_cookie_base,
  3294. struct netlink_ext_ack *extack)
  3295. {
  3296. int i, j, k, index, err = 0;
  3297. struct tc_action *act;
  3298. BUILD_BUG_ON(TCA_ACT_HW_STATS_ANY != FLOW_ACTION_HW_STATS_ANY);
  3299. BUILD_BUG_ON(TCA_ACT_HW_STATS_IMMEDIATE != FLOW_ACTION_HW_STATS_IMMEDIATE);
  3300. BUILD_BUG_ON(TCA_ACT_HW_STATS_DELAYED != FLOW_ACTION_HW_STATS_DELAYED);
  3301. if (!actions)
  3302. return 0;
  3303. j = 0;
  3304. tcf_act_for_each_action(i, act, actions) {
  3305. struct flow_action_entry *entry;
  3306. entry = &flow_action->entries[j];
  3307. spin_lock_bh(&act->tcfa_lock);
  3308. err = tcf_act_get_user_cookie(entry, act);
  3309. if (err)
  3310. goto err_out_locked;
  3311. index = 0;
  3312. err = tc_setup_offload_act(act, entry, &index, extack);
  3313. if (err)
  3314. goto err_out_locked;
  3315. for (k = 0; k < index ; k++) {
  3316. entry[k].hw_stats = tc_act_hw_stats(act->hw_stats);
  3317. entry[k].hw_index = act->tcfa_index;
  3318. entry[k].cookie = (unsigned long)act;
  3319. entry[k].miss_cookie =
  3320. tcf_exts_miss_cookie_get(miss_cookie_base, i);
  3321. }
  3322. j += index;
  3323. spin_unlock_bh(&act->tcfa_lock);
  3324. }
  3325. err_out:
  3326. if (err)
  3327. tc_cleanup_offload_action(flow_action);
  3328. return err;
  3329. err_out_locked:
  3330. spin_unlock_bh(&act->tcfa_lock);
  3331. goto err_out;
  3332. }
  3333. int tc_setup_offload_action(struct flow_action *flow_action,
  3334. const struct tcf_exts *exts,
  3335. struct netlink_ext_ack *extack)
  3336. {
  3337. #ifdef CONFIG_NET_CLS_ACT
  3338. u32 miss_cookie_base;
  3339. if (!exts)
  3340. return 0;
  3341. miss_cookie_base = exts->miss_cookie_node ?
  3342. exts->miss_cookie_node->miss_cookie_base : 0;
  3343. return tc_setup_action(flow_action, exts->actions, miss_cookie_base,
  3344. extack);
  3345. #else
  3346. return 0;
  3347. #endif
  3348. }
  3349. EXPORT_SYMBOL(tc_setup_offload_action);
  3350. unsigned int tcf_exts_num_actions(struct tcf_exts *exts)
  3351. {
  3352. unsigned int num_acts = 0;
  3353. struct tc_action *act;
  3354. int i;
  3355. tcf_exts_for_each_action(i, act, exts) {
  3356. if (is_tcf_pedit(act))
  3357. num_acts += tcf_pedit_nkeys(act);
  3358. else
  3359. num_acts++;
  3360. }
  3361. return num_acts;
  3362. }
  3363. EXPORT_SYMBOL(tcf_exts_num_actions);
  3364. #ifdef CONFIG_NET_CLS_ACT
  3365. static int tcf_qevent_parse_block_index(struct nlattr *block_index_attr,
  3366. u32 *p_block_index,
  3367. struct netlink_ext_ack *extack)
  3368. {
  3369. *p_block_index = nla_get_u32(block_index_attr);
  3370. if (!*p_block_index) {
  3371. NL_SET_ERR_MSG(extack, "Block number may not be zero");
  3372. return -EINVAL;
  3373. }
  3374. return 0;
  3375. }
  3376. int tcf_qevent_init(struct tcf_qevent *qe, struct Qdisc *sch,
  3377. enum flow_block_binder_type binder_type,
  3378. 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. qe->info.binder_type = binder_type;
  3389. qe->info.chain_head_change = tcf_chain_head_change_dflt;
  3390. qe->info.chain_head_change_priv = &qe->filter_chain;
  3391. qe->info.block_index = block_index;
  3392. return tcf_block_get_ext(&qe->block, sch, &qe->info, extack);
  3393. }
  3394. EXPORT_SYMBOL(tcf_qevent_init);
  3395. void tcf_qevent_destroy(struct tcf_qevent *qe, struct Qdisc *sch)
  3396. {
  3397. if (qe->info.block_index)
  3398. tcf_block_put_ext(qe->block, sch, &qe->info);
  3399. }
  3400. EXPORT_SYMBOL(tcf_qevent_destroy);
  3401. int tcf_qevent_validate_change(struct tcf_qevent *qe, struct nlattr *block_index_attr,
  3402. struct netlink_ext_ack *extack)
  3403. {
  3404. u32 block_index;
  3405. int err;
  3406. if (!block_index_attr)
  3407. return 0;
  3408. err = tcf_qevent_parse_block_index(block_index_attr, &block_index, extack);
  3409. if (err)
  3410. return err;
  3411. /* Bounce newly-configured block or change in block. */
  3412. if (block_index != qe->info.block_index) {
  3413. NL_SET_ERR_MSG(extack, "Change of blocks is not supported");
  3414. return -EINVAL;
  3415. }
  3416. return 0;
  3417. }
  3418. EXPORT_SYMBOL(tcf_qevent_validate_change);
  3419. struct sk_buff *tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, struct sk_buff *skb,
  3420. struct sk_buff **to_free, int *ret)
  3421. {
  3422. struct tcf_result cl_res;
  3423. struct tcf_proto *fl;
  3424. if (!qe->info.block_index)
  3425. return skb;
  3426. fl = rcu_dereference_bh(qe->filter_chain);
  3427. switch (tcf_classify(skb, NULL, fl, &cl_res, false)) {
  3428. case TC_ACT_SHOT:
  3429. qdisc_qstats_drop(sch);
  3430. __qdisc_drop(skb, to_free);
  3431. *ret = __NET_XMIT_BYPASS;
  3432. return NULL;
  3433. case TC_ACT_STOLEN:
  3434. case TC_ACT_QUEUED:
  3435. case TC_ACT_TRAP:
  3436. __qdisc_drop(skb, to_free);
  3437. *ret = __NET_XMIT_STOLEN;
  3438. return NULL;
  3439. case TC_ACT_REDIRECT:
  3440. skb_do_redirect(skb);
  3441. *ret = __NET_XMIT_STOLEN;
  3442. return NULL;
  3443. }
  3444. return skb;
  3445. }
  3446. EXPORT_SYMBOL(tcf_qevent_handle);
  3447. int tcf_qevent_dump(struct sk_buff *skb, int attr_name, struct tcf_qevent *qe)
  3448. {
  3449. if (!qe->info.block_index)
  3450. return 0;
  3451. return nla_put_u32(skb, attr_name, qe->info.block_index);
  3452. }
  3453. EXPORT_SYMBOL(tcf_qevent_dump);
  3454. #endif
  3455. static __net_init int tcf_net_init(struct net *net)
  3456. {
  3457. struct tcf_net *tn = net_generic(net, tcf_net_id);
  3458. spin_lock_init(&tn->idr_lock);
  3459. idr_init(&tn->idr);
  3460. return 0;
  3461. }
  3462. static void __net_exit tcf_net_exit(struct net *net)
  3463. {
  3464. struct tcf_net *tn = net_generic(net, tcf_net_id);
  3465. idr_destroy(&tn->idr);
  3466. }
  3467. static struct pernet_operations tcf_net_ops = {
  3468. .init = tcf_net_init,
  3469. .exit = tcf_net_exit,
  3470. .id = &tcf_net_id,
  3471. .size = sizeof(struct tcf_net),
  3472. };
  3473. static int __init tc_filter_init(void)
  3474. {
  3475. int err;
  3476. tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
  3477. if (!tc_filter_wq)
  3478. return -ENOMEM;
  3479. err = register_pernet_subsys(&tcf_net_ops);
  3480. if (err)
  3481. goto err_register_pernet_subsys;
  3482. xa_init_flags(&tcf_exts_miss_cookies_xa, XA_FLAGS_ALLOC1);
  3483. rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL,
  3484. RTNL_FLAG_DOIT_UNLOCKED);
  3485. rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL,
  3486. RTNL_FLAG_DOIT_UNLOCKED);
  3487. rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
  3488. tc_dump_tfilter, RTNL_FLAG_DOIT_UNLOCKED);
  3489. rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
  3490. rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
  3491. rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
  3492. tc_dump_chain, 0);
  3493. return 0;
  3494. err_register_pernet_subsys:
  3495. destroy_workqueue(tc_filter_wq);
  3496. return err;
  3497. }
  3498. subsys_initcall(tc_filter_init);