cls_api.c 92 KB

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