act_api.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/sched/act_api.c Packet action API.
  4. *
  5. * Author: Jamal Hadi Salim
  6. */
  7. #include <linux/types.h>
  8. #include <linux/kernel.h>
  9. #include <linux/string.h>
  10. #include <linux/errno.h>
  11. #include <linux/slab.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/init.h>
  14. #include <linux/kmod.h>
  15. #include <linux/err.h>
  16. #include <linux/module.h>
  17. #include <net/net_namespace.h>
  18. #include <net/sock.h>
  19. #include <net/sch_generic.h>
  20. #include <net/pkt_cls.h>
  21. #include <net/tc_act/tc_pedit.h>
  22. #include <net/act_api.h>
  23. #include <net/netlink.h>
  24. #include <net/flow_offload.h>
  25. #include <net/tc_wrapper.h>
  26. #ifdef CONFIG_INET
  27. DEFINE_STATIC_KEY_FALSE(tcf_frag_xmit_count);
  28. EXPORT_SYMBOL_GPL(tcf_frag_xmit_count);
  29. #endif
  30. int tcf_dev_queue_xmit(struct sk_buff *skb, int (*xmit)(struct sk_buff *skb))
  31. {
  32. #ifdef CONFIG_INET
  33. if (static_branch_unlikely(&tcf_frag_xmit_count))
  34. return sch_frag_xmit_hook(skb, xmit);
  35. #endif
  36. return xmit(skb);
  37. }
  38. EXPORT_SYMBOL_GPL(tcf_dev_queue_xmit);
  39. static void tcf_action_goto_chain_exec(const struct tc_action *a,
  40. struct tcf_result *res)
  41. {
  42. const struct tcf_chain *chain = rcu_dereference_bh(a->goto_chain);
  43. res->goto_tp = rcu_dereference_bh(chain->filter_chain);
  44. }
  45. static void tcf_free_cookie_rcu(struct rcu_head *p)
  46. {
  47. struct tc_cookie *cookie = container_of(p, struct tc_cookie, rcu);
  48. kfree(cookie->data);
  49. kfree(cookie);
  50. }
  51. static void tcf_set_action_cookie(struct tc_cookie __rcu **old_cookie,
  52. struct tc_cookie *new_cookie)
  53. {
  54. struct tc_cookie *old;
  55. old = unrcu_pointer(xchg(old_cookie, RCU_INITIALIZER(new_cookie)));
  56. if (old)
  57. call_rcu(&old->rcu, tcf_free_cookie_rcu);
  58. }
  59. int tcf_action_check_ctrlact(int action, struct tcf_proto *tp,
  60. struct tcf_chain **newchain,
  61. struct netlink_ext_ack *extack)
  62. {
  63. int opcode = TC_ACT_EXT_OPCODE(action), ret = -EINVAL;
  64. u32 chain_index;
  65. if (!opcode)
  66. ret = action > TC_ACT_VALUE_MAX ? -EINVAL : 0;
  67. else if (opcode <= TC_ACT_EXT_OPCODE_MAX || action == TC_ACT_UNSPEC)
  68. ret = 0;
  69. if (ret) {
  70. NL_SET_ERR_MSG(extack, "invalid control action");
  71. goto end;
  72. }
  73. if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN)) {
  74. chain_index = action & TC_ACT_EXT_VAL_MASK;
  75. if (!tp || !newchain) {
  76. ret = -EINVAL;
  77. NL_SET_ERR_MSG(extack,
  78. "can't goto NULL proto/chain");
  79. goto end;
  80. }
  81. *newchain = tcf_chain_get_by_act(tp->chain->block, chain_index);
  82. if (!*newchain) {
  83. ret = -ENOMEM;
  84. NL_SET_ERR_MSG(extack,
  85. "can't allocate goto_chain");
  86. }
  87. }
  88. end:
  89. return ret;
  90. }
  91. EXPORT_SYMBOL(tcf_action_check_ctrlact);
  92. struct tcf_chain *tcf_action_set_ctrlact(struct tc_action *a, int action,
  93. struct tcf_chain *goto_chain)
  94. {
  95. a->tcfa_action = action;
  96. goto_chain = rcu_replace_pointer(a->goto_chain, goto_chain, 1);
  97. return goto_chain;
  98. }
  99. EXPORT_SYMBOL(tcf_action_set_ctrlact);
  100. /* XXX: For standalone actions, we don't need a RCU grace period either, because
  101. * actions are always connected to filters and filters are already destroyed in
  102. * RCU callbacks, so after a RCU grace period actions are already disconnected
  103. * from filters. Readers later can not find us.
  104. */
  105. static void free_tcf(struct tc_action *p)
  106. {
  107. struct tcf_chain *chain = rcu_dereference_protected(p->goto_chain, 1);
  108. free_percpu(p->cpu_bstats);
  109. free_percpu(p->cpu_bstats_hw);
  110. free_percpu(p->cpu_qstats);
  111. tcf_set_action_cookie(&p->user_cookie, NULL);
  112. if (chain)
  113. tcf_chain_put_by_act(chain);
  114. kfree(p);
  115. }
  116. static void offload_action_hw_count_set(struct tc_action *act,
  117. u32 hw_count)
  118. {
  119. act->in_hw_count = hw_count;
  120. }
  121. static void offload_action_hw_count_inc(struct tc_action *act,
  122. u32 hw_count)
  123. {
  124. act->in_hw_count += hw_count;
  125. }
  126. static void offload_action_hw_count_dec(struct tc_action *act,
  127. u32 hw_count)
  128. {
  129. act->in_hw_count = act->in_hw_count > hw_count ?
  130. act->in_hw_count - hw_count : 0;
  131. }
  132. static unsigned int tcf_offload_act_num_actions_single(struct tc_action *act)
  133. {
  134. if (is_tcf_pedit(act))
  135. return tcf_pedit_nkeys(act);
  136. else
  137. return 1;
  138. }
  139. static bool tc_act_skip_hw(u32 flags)
  140. {
  141. return (flags & TCA_ACT_FLAGS_SKIP_HW) ? true : false;
  142. }
  143. static bool tc_act_skip_sw(u32 flags)
  144. {
  145. return (flags & TCA_ACT_FLAGS_SKIP_SW) ? true : false;
  146. }
  147. /* SKIP_HW and SKIP_SW are mutually exclusive flags. */
  148. static bool tc_act_flags_valid(u32 flags)
  149. {
  150. flags &= TCA_ACT_FLAGS_SKIP_HW | TCA_ACT_FLAGS_SKIP_SW;
  151. return flags ^ (TCA_ACT_FLAGS_SKIP_HW | TCA_ACT_FLAGS_SKIP_SW);
  152. }
  153. static int offload_action_init(struct flow_offload_action *fl_action,
  154. struct tc_action *act,
  155. enum offload_act_command cmd,
  156. struct netlink_ext_ack *extack)
  157. {
  158. int err;
  159. fl_action->extack = extack;
  160. fl_action->command = cmd;
  161. fl_action->index = act->tcfa_index;
  162. fl_action->cookie = (unsigned long)act;
  163. if (act->ops->offload_act_setup) {
  164. spin_lock_bh(&act->tcfa_lock);
  165. err = act->ops->offload_act_setup(act, fl_action, NULL,
  166. false, extack);
  167. spin_unlock_bh(&act->tcfa_lock);
  168. return err;
  169. }
  170. return -EOPNOTSUPP;
  171. }
  172. static int tcf_action_offload_cmd_ex(struct flow_offload_action *fl_act,
  173. u32 *hw_count)
  174. {
  175. int err;
  176. err = flow_indr_dev_setup_offload(NULL, NULL, TC_SETUP_ACT,
  177. fl_act, NULL, NULL);
  178. if (err < 0)
  179. return err;
  180. if (hw_count)
  181. *hw_count = err;
  182. return 0;
  183. }
  184. static int tcf_action_offload_cmd_cb_ex(struct flow_offload_action *fl_act,
  185. u32 *hw_count,
  186. flow_indr_block_bind_cb_t *cb,
  187. void *cb_priv)
  188. {
  189. int err;
  190. err = cb(NULL, NULL, cb_priv, TC_SETUP_ACT, NULL, fl_act, NULL);
  191. if (err < 0)
  192. return err;
  193. if (hw_count)
  194. *hw_count = 1;
  195. return 0;
  196. }
  197. static int tcf_action_offload_cmd(struct flow_offload_action *fl_act,
  198. u32 *hw_count,
  199. flow_indr_block_bind_cb_t *cb,
  200. void *cb_priv)
  201. {
  202. return cb ? tcf_action_offload_cmd_cb_ex(fl_act, hw_count,
  203. cb, cb_priv) :
  204. tcf_action_offload_cmd_ex(fl_act, hw_count);
  205. }
  206. static int tcf_action_offload_add_ex(struct tc_action *action,
  207. struct netlink_ext_ack *extack,
  208. flow_indr_block_bind_cb_t *cb,
  209. void *cb_priv)
  210. {
  211. bool skip_sw = tc_act_skip_sw(action->tcfa_flags);
  212. struct tc_action *actions[TCA_ACT_MAX_PRIO] = {
  213. [0] = action,
  214. };
  215. struct flow_offload_action *fl_action;
  216. u32 in_hw_count = 0;
  217. int num, err = 0;
  218. if (tc_act_skip_hw(action->tcfa_flags))
  219. return 0;
  220. num = tcf_offload_act_num_actions_single(action);
  221. fl_action = offload_action_alloc(num);
  222. if (!fl_action)
  223. return -ENOMEM;
  224. err = offload_action_init(fl_action, action, FLOW_ACT_REPLACE, extack);
  225. if (err)
  226. goto fl_err;
  227. err = tc_setup_action(&fl_action->action, actions, 0, extack);
  228. if (err) {
  229. NL_SET_ERR_MSG_MOD(extack,
  230. "Failed to setup tc actions for offload");
  231. goto fl_err;
  232. }
  233. err = tcf_action_offload_cmd(fl_action, &in_hw_count, cb, cb_priv);
  234. if (!err)
  235. cb ? offload_action_hw_count_inc(action, in_hw_count) :
  236. offload_action_hw_count_set(action, in_hw_count);
  237. if (skip_sw && !tc_act_in_hw(action))
  238. err = -EINVAL;
  239. tc_cleanup_offload_action(&fl_action->action);
  240. fl_err:
  241. kfree(fl_action);
  242. return err;
  243. }
  244. /* offload the tc action after it is inserted */
  245. static int tcf_action_offload_add(struct tc_action *action,
  246. struct netlink_ext_ack *extack)
  247. {
  248. return tcf_action_offload_add_ex(action, extack, NULL, NULL);
  249. }
  250. int tcf_action_update_hw_stats(struct tc_action *action)
  251. {
  252. struct flow_offload_action fl_act = {};
  253. int err;
  254. err = offload_action_init(&fl_act, action, FLOW_ACT_STATS, NULL);
  255. if (err)
  256. return err;
  257. err = tcf_action_offload_cmd(&fl_act, NULL, NULL, NULL);
  258. if (!err) {
  259. preempt_disable();
  260. tcf_action_stats_update(action, fl_act.stats.bytes,
  261. fl_act.stats.pkts,
  262. fl_act.stats.drops,
  263. fl_act.stats.lastused,
  264. true);
  265. preempt_enable();
  266. action->used_hw_stats = fl_act.stats.used_hw_stats;
  267. action->used_hw_stats_valid = true;
  268. } else {
  269. return -EOPNOTSUPP;
  270. }
  271. return 0;
  272. }
  273. EXPORT_SYMBOL(tcf_action_update_hw_stats);
  274. static int tcf_action_offload_del_ex(struct tc_action *action,
  275. flow_indr_block_bind_cb_t *cb,
  276. void *cb_priv)
  277. {
  278. struct flow_offload_action fl_act = {};
  279. u32 in_hw_count = 0;
  280. int err = 0;
  281. if (!tc_act_in_hw(action))
  282. return 0;
  283. err = offload_action_init(&fl_act, action, FLOW_ACT_DESTROY, NULL);
  284. if (err)
  285. return err;
  286. err = tcf_action_offload_cmd(&fl_act, &in_hw_count, cb, cb_priv);
  287. if (err < 0)
  288. return err;
  289. if (!cb && action->in_hw_count != in_hw_count)
  290. return -EINVAL;
  291. /* do not need to update hw state when deleting action */
  292. if (cb && in_hw_count)
  293. offload_action_hw_count_dec(action, in_hw_count);
  294. return 0;
  295. }
  296. static int tcf_action_offload_del(struct tc_action *action)
  297. {
  298. return tcf_action_offload_del_ex(action, NULL, NULL);
  299. }
  300. static void tcf_action_cleanup(struct tc_action *p)
  301. {
  302. tcf_action_offload_del(p);
  303. if (p->ops->cleanup)
  304. p->ops->cleanup(p);
  305. gen_kill_estimator(&p->tcfa_rate_est);
  306. free_tcf(p);
  307. }
  308. static int __tcf_action_put(struct tc_action *p, bool bind)
  309. {
  310. struct tcf_idrinfo *idrinfo = p->idrinfo;
  311. if (refcount_dec_and_mutex_lock(&p->tcfa_refcnt, &idrinfo->lock)) {
  312. if (bind)
  313. atomic_dec(&p->tcfa_bindcnt);
  314. idr_remove(&idrinfo->action_idr, p->tcfa_index);
  315. mutex_unlock(&idrinfo->lock);
  316. tcf_action_cleanup(p);
  317. return 1;
  318. }
  319. if (bind)
  320. atomic_dec(&p->tcfa_bindcnt);
  321. return 0;
  322. }
  323. static int __tcf_idr_release(struct tc_action *p, bool bind, bool strict)
  324. {
  325. int ret = 0;
  326. /* Release with strict==1 and bind==0 is only called through act API
  327. * interface (classifiers always bind). Only case when action with
  328. * positive reference count and zero bind count can exist is when it was
  329. * also created with act API (unbinding last classifier will destroy the
  330. * action if it was created by classifier). So only case when bind count
  331. * can be changed after initial check is when unbound action is
  332. * destroyed by act API while classifier binds to action with same id
  333. * concurrently. This result either creation of new action(same behavior
  334. * as before), or reusing existing action if concurrent process
  335. * increments reference count before action is deleted. Both scenarios
  336. * are acceptable.
  337. */
  338. if (p) {
  339. if (!bind && strict && atomic_read(&p->tcfa_bindcnt) > 0)
  340. return -EPERM;
  341. if (__tcf_action_put(p, bind))
  342. ret = ACT_P_DELETED;
  343. }
  344. return ret;
  345. }
  346. int tcf_idr_release(struct tc_action *a, bool bind)
  347. {
  348. const struct tc_action_ops *ops = a->ops;
  349. int ret;
  350. ret = __tcf_idr_release(a, bind, false);
  351. if (ret == ACT_P_DELETED)
  352. module_put(ops->owner);
  353. return ret;
  354. }
  355. EXPORT_SYMBOL(tcf_idr_release);
  356. static size_t tcf_action_shared_attrs_size(const struct tc_action *act)
  357. {
  358. struct tc_cookie *user_cookie;
  359. u32 cookie_len = 0;
  360. rcu_read_lock();
  361. user_cookie = rcu_dereference(act->user_cookie);
  362. if (user_cookie)
  363. cookie_len = nla_total_size(user_cookie->len);
  364. rcu_read_unlock();
  365. return nla_total_size(0) /* action number nested */
  366. + nla_total_size(IFNAMSIZ) /* TCA_ACT_KIND */
  367. + cookie_len /* TCA_ACT_COOKIE */
  368. + nla_total_size(sizeof(struct nla_bitfield32)) /* TCA_ACT_HW_STATS */
  369. + nla_total_size(0) /* TCA_ACT_STATS nested */
  370. + nla_total_size(sizeof(struct nla_bitfield32)) /* TCA_ACT_FLAGS */
  371. /* TCA_STATS_BASIC */
  372. + nla_total_size_64bit(sizeof(struct gnet_stats_basic))
  373. /* TCA_STATS_PKT64 */
  374. + nla_total_size_64bit(sizeof(u64))
  375. /* TCA_STATS_QUEUE */
  376. + nla_total_size_64bit(sizeof(struct gnet_stats_queue))
  377. + nla_total_size(0) /* TCA_ACT_OPTIONS nested */
  378. + nla_total_size(sizeof(struct tcf_t)); /* TCA_GACT_TM */
  379. }
  380. static size_t tcf_action_full_attrs_size(size_t sz)
  381. {
  382. return NLMSG_HDRLEN /* struct nlmsghdr */
  383. + sizeof(struct tcamsg)
  384. + nla_total_size(0) /* TCA_ACT_TAB nested */
  385. + sz;
  386. }
  387. static size_t tcf_action_fill_size(const struct tc_action *act)
  388. {
  389. size_t sz = tcf_action_shared_attrs_size(act);
  390. if (act->ops->get_fill_size)
  391. return act->ops->get_fill_size(act) + sz;
  392. return sz;
  393. }
  394. static int
  395. tcf_action_dump_terse(struct sk_buff *skb, struct tc_action *a, bool from_act)
  396. {
  397. unsigned char *b = skb_tail_pointer(skb);
  398. struct tc_cookie *cookie;
  399. if (nla_put_string(skb, TCA_ACT_KIND, a->ops->kind))
  400. goto nla_put_failure;
  401. if (tcf_action_copy_stats(skb, a, 0))
  402. goto nla_put_failure;
  403. if (from_act && nla_put_u32(skb, TCA_ACT_INDEX, a->tcfa_index))
  404. goto nla_put_failure;
  405. rcu_read_lock();
  406. cookie = rcu_dereference(a->user_cookie);
  407. if (cookie) {
  408. if (nla_put(skb, TCA_ACT_COOKIE, cookie->len, cookie->data)) {
  409. rcu_read_unlock();
  410. goto nla_put_failure;
  411. }
  412. }
  413. rcu_read_unlock();
  414. return 0;
  415. nla_put_failure:
  416. nlmsg_trim(skb, b);
  417. return -1;
  418. }
  419. static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
  420. struct netlink_callback *cb)
  421. {
  422. int err = 0, index = -1, s_i = 0, n_i = 0;
  423. u32 act_flags = cb->args[2];
  424. unsigned long jiffy_since = cb->args[3];
  425. struct nlattr *nest;
  426. struct idr *idr = &idrinfo->action_idr;
  427. struct tc_action *p;
  428. unsigned long id = 1;
  429. unsigned long tmp;
  430. mutex_lock(&idrinfo->lock);
  431. s_i = cb->args[0];
  432. idr_for_each_entry_ul(idr, p, tmp, id) {
  433. index++;
  434. if (index < s_i)
  435. continue;
  436. if (IS_ERR(p))
  437. continue;
  438. if (jiffy_since &&
  439. time_after(jiffy_since,
  440. (unsigned long)p->tcfa_tm.lastuse))
  441. continue;
  442. tcf_action_update_hw_stats(p);
  443. nest = nla_nest_start_noflag(skb, n_i);
  444. if (!nest) {
  445. index--;
  446. goto nla_put_failure;
  447. }
  448. err = (act_flags & TCA_ACT_FLAG_TERSE_DUMP) ?
  449. tcf_action_dump_terse(skb, p, true) :
  450. tcf_action_dump_1(skb, p, 0, 0);
  451. if (err < 0) {
  452. index--;
  453. nlmsg_trim(skb, nest);
  454. goto done;
  455. }
  456. nla_nest_end(skb, nest);
  457. n_i++;
  458. if (!(act_flags & TCA_ACT_FLAG_LARGE_DUMP_ON) &&
  459. n_i >= TCA_ACT_MAX_PRIO)
  460. goto done;
  461. }
  462. done:
  463. if (index >= 0)
  464. cb->args[0] = index + 1;
  465. mutex_unlock(&idrinfo->lock);
  466. if (n_i) {
  467. if (act_flags & TCA_ACT_FLAG_LARGE_DUMP_ON)
  468. cb->args[1] = n_i;
  469. }
  470. return n_i;
  471. nla_put_failure:
  472. nla_nest_cancel(skb, nest);
  473. goto done;
  474. }
  475. static int tcf_idr_release_unsafe(struct tc_action *p)
  476. {
  477. if (atomic_read(&p->tcfa_bindcnt) > 0)
  478. return -EPERM;
  479. if (refcount_dec_and_test(&p->tcfa_refcnt)) {
  480. idr_remove(&p->idrinfo->action_idr, p->tcfa_index);
  481. tcf_action_cleanup(p);
  482. return ACT_P_DELETED;
  483. }
  484. return 0;
  485. }
  486. static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
  487. const struct tc_action_ops *ops,
  488. struct netlink_ext_ack *extack)
  489. {
  490. struct nlattr *nest;
  491. int n_i = 0;
  492. int ret = -EINVAL;
  493. struct idr *idr = &idrinfo->action_idr;
  494. struct tc_action *p;
  495. unsigned long id = 1;
  496. unsigned long tmp;
  497. nest = nla_nest_start_noflag(skb, 0);
  498. if (nest == NULL)
  499. goto nla_put_failure;
  500. if (nla_put_string(skb, TCA_ACT_KIND, ops->kind))
  501. goto nla_put_failure;
  502. ret = 0;
  503. mutex_lock(&idrinfo->lock);
  504. idr_for_each_entry_ul(idr, p, tmp, id) {
  505. if (IS_ERR(p))
  506. continue;
  507. ret = tcf_idr_release_unsafe(p);
  508. if (ret == ACT_P_DELETED)
  509. module_put(ops->owner);
  510. else if (ret < 0)
  511. break;
  512. n_i++;
  513. }
  514. mutex_unlock(&idrinfo->lock);
  515. if (ret < 0) {
  516. if (n_i)
  517. NL_SET_ERR_MSG(extack, "Unable to flush all TC actions");
  518. else
  519. goto nla_put_failure;
  520. }
  521. ret = nla_put_u32(skb, TCA_FCNT, n_i);
  522. if (ret)
  523. goto nla_put_failure;
  524. nla_nest_end(skb, nest);
  525. return n_i;
  526. nla_put_failure:
  527. nla_nest_cancel(skb, nest);
  528. return ret;
  529. }
  530. int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
  531. struct netlink_callback *cb, int type,
  532. const struct tc_action_ops *ops,
  533. struct netlink_ext_ack *extack)
  534. {
  535. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  536. if (type == RTM_DELACTION) {
  537. return tcf_del_walker(idrinfo, skb, ops, extack);
  538. } else if (type == RTM_GETACTION) {
  539. return tcf_dump_walker(idrinfo, skb, cb);
  540. } else {
  541. WARN(1, "tcf_generic_walker: unknown command %d\n", type);
  542. NL_SET_ERR_MSG(extack, "tcf_generic_walker: unknown command");
  543. return -EINVAL;
  544. }
  545. }
  546. EXPORT_SYMBOL(tcf_generic_walker);
  547. int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
  548. {
  549. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  550. struct tc_action *p;
  551. mutex_lock(&idrinfo->lock);
  552. p = idr_find(&idrinfo->action_idr, index);
  553. if (IS_ERR(p))
  554. p = NULL;
  555. else if (p)
  556. refcount_inc(&p->tcfa_refcnt);
  557. mutex_unlock(&idrinfo->lock);
  558. if (p) {
  559. *a = p;
  560. return true;
  561. }
  562. return false;
  563. }
  564. EXPORT_SYMBOL(tcf_idr_search);
  565. static int __tcf_generic_walker(struct net *net, struct sk_buff *skb,
  566. struct netlink_callback *cb, int type,
  567. const struct tc_action_ops *ops,
  568. struct netlink_ext_ack *extack)
  569. {
  570. struct tc_action_net *tn = net_generic(net, ops->net_id);
  571. if (unlikely(ops->walk))
  572. return ops->walk(net, skb, cb, type, ops, extack);
  573. return tcf_generic_walker(tn, skb, cb, type, ops, extack);
  574. }
  575. static int __tcf_idr_search(struct net *net,
  576. const struct tc_action_ops *ops,
  577. struct tc_action **a, u32 index)
  578. {
  579. struct tc_action_net *tn = net_generic(net, ops->net_id);
  580. if (unlikely(ops->lookup))
  581. return ops->lookup(net, a, index);
  582. return tcf_idr_search(tn, a, index);
  583. }
  584. static int tcf_idr_delete_index(struct tcf_idrinfo *idrinfo, u32 index)
  585. {
  586. struct tc_action *p;
  587. int ret = 0;
  588. mutex_lock(&idrinfo->lock);
  589. p = idr_find(&idrinfo->action_idr, index);
  590. if (!p) {
  591. mutex_unlock(&idrinfo->lock);
  592. return -ENOENT;
  593. }
  594. if (!atomic_read(&p->tcfa_bindcnt)) {
  595. if (refcount_dec_and_test(&p->tcfa_refcnt)) {
  596. struct module *owner = p->ops->owner;
  597. WARN_ON(p != idr_remove(&idrinfo->action_idr,
  598. p->tcfa_index));
  599. mutex_unlock(&idrinfo->lock);
  600. tcf_action_cleanup(p);
  601. module_put(owner);
  602. return 0;
  603. }
  604. ret = 0;
  605. } else {
  606. ret = -EPERM;
  607. }
  608. mutex_unlock(&idrinfo->lock);
  609. return ret;
  610. }
  611. int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
  612. struct tc_action **a, const struct tc_action_ops *ops,
  613. int bind, bool cpustats, u32 flags)
  614. {
  615. struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
  616. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  617. int err = -ENOMEM;
  618. if (unlikely(!p))
  619. return -ENOMEM;
  620. refcount_set(&p->tcfa_refcnt, 1);
  621. if (bind)
  622. atomic_set(&p->tcfa_bindcnt, 1);
  623. if (cpustats) {
  624. p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_sync);
  625. if (!p->cpu_bstats)
  626. goto err1;
  627. p->cpu_bstats_hw = netdev_alloc_pcpu_stats(struct gnet_stats_basic_sync);
  628. if (!p->cpu_bstats_hw)
  629. goto err2;
  630. p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
  631. if (!p->cpu_qstats)
  632. goto err3;
  633. }
  634. gnet_stats_basic_sync_init(&p->tcfa_bstats);
  635. gnet_stats_basic_sync_init(&p->tcfa_bstats_hw);
  636. spin_lock_init(&p->tcfa_lock);
  637. p->tcfa_index = index;
  638. p->tcfa_tm.install = jiffies;
  639. p->tcfa_tm.lastuse = jiffies;
  640. p->tcfa_tm.firstuse = 0;
  641. p->tcfa_flags = flags;
  642. if (est) {
  643. err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
  644. &p->tcfa_rate_est,
  645. &p->tcfa_lock, false, est);
  646. if (err)
  647. goto err4;
  648. }
  649. p->idrinfo = idrinfo;
  650. __module_get(ops->owner);
  651. p->ops = ops;
  652. *a = p;
  653. return 0;
  654. err4:
  655. free_percpu(p->cpu_qstats);
  656. err3:
  657. free_percpu(p->cpu_bstats_hw);
  658. err2:
  659. free_percpu(p->cpu_bstats);
  660. err1:
  661. kfree(p);
  662. return err;
  663. }
  664. EXPORT_SYMBOL(tcf_idr_create);
  665. int tcf_idr_create_from_flags(struct tc_action_net *tn, u32 index,
  666. struct nlattr *est, struct tc_action **a,
  667. const struct tc_action_ops *ops, int bind,
  668. u32 flags)
  669. {
  670. /* Set cpustats according to actions flags. */
  671. return tcf_idr_create(tn, index, est, a, ops, bind,
  672. !(flags & TCA_ACT_FLAGS_NO_PERCPU_STATS), flags);
  673. }
  674. EXPORT_SYMBOL(tcf_idr_create_from_flags);
  675. /* Cleanup idr index that was allocated but not initialized. */
  676. void tcf_idr_cleanup(struct tc_action_net *tn, u32 index)
  677. {
  678. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  679. mutex_lock(&idrinfo->lock);
  680. /* Remove ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
  681. WARN_ON(!IS_ERR(idr_remove(&idrinfo->action_idr, index)));
  682. mutex_unlock(&idrinfo->lock);
  683. }
  684. EXPORT_SYMBOL(tcf_idr_cleanup);
  685. /* Check if action with specified index exists. If actions is found, increments
  686. * its reference and bind counters, and return 1. Otherwise insert temporary
  687. * error pointer (to prevent concurrent users from inserting actions with same
  688. * index) and return 0.
  689. *
  690. * May return -EAGAIN for binding actions in case of a parallel add/delete on
  691. * the requested index.
  692. */
  693. int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index,
  694. struct tc_action **a, int bind)
  695. {
  696. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  697. struct tc_action *p;
  698. int ret;
  699. u32 max;
  700. if (*index) {
  701. rcu_read_lock();
  702. p = idr_find(&idrinfo->action_idr, *index);
  703. if (IS_ERR(p)) {
  704. /* This means that another process allocated
  705. * index but did not assign the pointer yet.
  706. */
  707. rcu_read_unlock();
  708. return -EAGAIN;
  709. }
  710. if (!p) {
  711. /* Empty slot, try to allocate it */
  712. max = *index;
  713. rcu_read_unlock();
  714. goto new;
  715. }
  716. if (!refcount_inc_not_zero(&p->tcfa_refcnt)) {
  717. /* Action was deleted in parallel */
  718. rcu_read_unlock();
  719. return -EAGAIN;
  720. }
  721. if (bind)
  722. atomic_inc(&p->tcfa_bindcnt);
  723. *a = p;
  724. rcu_read_unlock();
  725. return 1;
  726. } else {
  727. /* Find a slot */
  728. *index = 1;
  729. max = UINT_MAX;
  730. }
  731. new:
  732. *a = NULL;
  733. mutex_lock(&idrinfo->lock);
  734. ret = idr_alloc_u32(&idrinfo->action_idr, ERR_PTR(-EBUSY), index, max,
  735. GFP_KERNEL);
  736. mutex_unlock(&idrinfo->lock);
  737. /* N binds raced for action allocation,
  738. * retry for all the ones that failed.
  739. */
  740. if (ret == -ENOSPC && *index == max)
  741. ret = -EAGAIN;
  742. return ret;
  743. }
  744. EXPORT_SYMBOL(tcf_idr_check_alloc);
  745. void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
  746. struct tcf_idrinfo *idrinfo)
  747. {
  748. struct idr *idr = &idrinfo->action_idr;
  749. struct tc_action *p;
  750. int ret;
  751. unsigned long id = 1;
  752. unsigned long tmp;
  753. idr_for_each_entry_ul(idr, p, tmp, id) {
  754. ret = __tcf_idr_release(p, false, true);
  755. if (ret == ACT_P_DELETED)
  756. module_put(ops->owner);
  757. else if (ret < 0)
  758. return;
  759. }
  760. idr_destroy(&idrinfo->action_idr);
  761. }
  762. EXPORT_SYMBOL(tcf_idrinfo_destroy);
  763. static LIST_HEAD(act_base);
  764. static DEFINE_RWLOCK(act_mod_lock);
  765. /* since act ops id is stored in pernet subsystem list,
  766. * then there is no way to walk through only all the action
  767. * subsystem, so we keep tc action pernet ops id for
  768. * reoffload to walk through.
  769. */
  770. static LIST_HEAD(act_pernet_id_list);
  771. static DEFINE_MUTEX(act_id_mutex);
  772. struct tc_act_pernet_id {
  773. struct list_head list;
  774. unsigned int id;
  775. };
  776. static int tcf_pernet_add_id_list(unsigned int id)
  777. {
  778. struct tc_act_pernet_id *id_ptr;
  779. int ret = 0;
  780. mutex_lock(&act_id_mutex);
  781. list_for_each_entry(id_ptr, &act_pernet_id_list, list) {
  782. if (id_ptr->id == id) {
  783. ret = -EEXIST;
  784. goto err_out;
  785. }
  786. }
  787. id_ptr = kzalloc(sizeof(*id_ptr), GFP_KERNEL);
  788. if (!id_ptr) {
  789. ret = -ENOMEM;
  790. goto err_out;
  791. }
  792. id_ptr->id = id;
  793. list_add_tail(&id_ptr->list, &act_pernet_id_list);
  794. err_out:
  795. mutex_unlock(&act_id_mutex);
  796. return ret;
  797. }
  798. static void tcf_pernet_del_id_list(unsigned int id)
  799. {
  800. struct tc_act_pernet_id *id_ptr;
  801. mutex_lock(&act_id_mutex);
  802. list_for_each_entry(id_ptr, &act_pernet_id_list, list) {
  803. if (id_ptr->id == id) {
  804. list_del(&id_ptr->list);
  805. kfree(id_ptr);
  806. break;
  807. }
  808. }
  809. mutex_unlock(&act_id_mutex);
  810. }
  811. int tcf_register_action(struct tc_action_ops *act,
  812. struct pernet_operations *ops)
  813. {
  814. struct tc_action_ops *a;
  815. int ret;
  816. if (!act->act || !act->dump || !act->init)
  817. return -EINVAL;
  818. /* We have to register pernet ops before making the action ops visible,
  819. * otherwise tcf_action_init_1() could get a partially initialized
  820. * netns.
  821. */
  822. ret = register_pernet_subsys(ops);
  823. if (ret)
  824. return ret;
  825. if (ops->id) {
  826. ret = tcf_pernet_add_id_list(*ops->id);
  827. if (ret)
  828. goto err_id;
  829. }
  830. write_lock(&act_mod_lock);
  831. list_for_each_entry(a, &act_base, head) {
  832. if (act->id == a->id || (strcmp(act->kind, a->kind) == 0)) {
  833. ret = -EEXIST;
  834. goto err_out;
  835. }
  836. }
  837. list_add_tail(&act->head, &act_base);
  838. write_unlock(&act_mod_lock);
  839. return 0;
  840. err_out:
  841. write_unlock(&act_mod_lock);
  842. if (ops->id)
  843. tcf_pernet_del_id_list(*ops->id);
  844. err_id:
  845. unregister_pernet_subsys(ops);
  846. return ret;
  847. }
  848. EXPORT_SYMBOL(tcf_register_action);
  849. int tcf_unregister_action(struct tc_action_ops *act,
  850. struct pernet_operations *ops)
  851. {
  852. struct tc_action_ops *a;
  853. int err = -ENOENT;
  854. write_lock(&act_mod_lock);
  855. list_for_each_entry(a, &act_base, head) {
  856. if (a == act) {
  857. list_del(&act->head);
  858. err = 0;
  859. break;
  860. }
  861. }
  862. write_unlock(&act_mod_lock);
  863. if (!err) {
  864. unregister_pernet_subsys(ops);
  865. if (ops->id)
  866. tcf_pernet_del_id_list(*ops->id);
  867. }
  868. return err;
  869. }
  870. EXPORT_SYMBOL(tcf_unregister_action);
  871. /* lookup by name */
  872. static struct tc_action_ops *tc_lookup_action_n(char *kind)
  873. {
  874. struct tc_action_ops *a, *res = NULL;
  875. if (kind) {
  876. read_lock(&act_mod_lock);
  877. list_for_each_entry(a, &act_base, head) {
  878. if (strcmp(kind, a->kind) == 0) {
  879. if (try_module_get(a->owner))
  880. res = a;
  881. break;
  882. }
  883. }
  884. read_unlock(&act_mod_lock);
  885. }
  886. return res;
  887. }
  888. /* lookup by nlattr */
  889. static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
  890. {
  891. struct tc_action_ops *a, *res = NULL;
  892. if (kind) {
  893. read_lock(&act_mod_lock);
  894. list_for_each_entry(a, &act_base, head) {
  895. if (nla_strcmp(kind, a->kind) == 0) {
  896. if (try_module_get(a->owner))
  897. res = a;
  898. break;
  899. }
  900. }
  901. read_unlock(&act_mod_lock);
  902. }
  903. return res;
  904. }
  905. /*TCA_ACT_MAX_PRIO is 32, there count up to 32 */
  906. #define TCA_ACT_MAX_PRIO_MASK 0x1FF
  907. int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
  908. int nr_actions, struct tcf_result *res)
  909. {
  910. u32 jmp_prgcnt = 0;
  911. u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
  912. int i;
  913. int ret = TC_ACT_OK;
  914. if (skb_skip_tc_classify(skb))
  915. return TC_ACT_OK;
  916. restart_act_graph:
  917. for (i = 0; i < nr_actions; i++) {
  918. const struct tc_action *a = actions[i];
  919. int repeat_ttl;
  920. if (jmp_prgcnt > 0) {
  921. jmp_prgcnt -= 1;
  922. continue;
  923. }
  924. if (tc_act_skip_sw(a->tcfa_flags))
  925. continue;
  926. repeat_ttl = 32;
  927. repeat:
  928. ret = tc_act(skb, a, res);
  929. if (unlikely(ret == TC_ACT_REPEAT)) {
  930. if (--repeat_ttl != 0)
  931. goto repeat;
  932. /* suspicious opcode, stop pipeline */
  933. net_warn_ratelimited("TC_ACT_REPEAT abuse ?\n");
  934. return TC_ACT_OK;
  935. }
  936. if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
  937. jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
  938. if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
  939. /* faulty opcode, stop pipeline */
  940. return TC_ACT_OK;
  941. } else {
  942. jmp_ttl -= 1;
  943. if (jmp_ttl > 0)
  944. goto restart_act_graph;
  945. else /* faulty graph, stop pipeline */
  946. return TC_ACT_OK;
  947. }
  948. } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
  949. if (unlikely(!rcu_access_pointer(a->goto_chain))) {
  950. tcf_set_drop_reason(skb,
  951. SKB_DROP_REASON_TC_CHAIN_NOTFOUND);
  952. return TC_ACT_SHOT;
  953. }
  954. tcf_action_goto_chain_exec(a, res);
  955. }
  956. if (ret != TC_ACT_PIPE)
  957. break;
  958. }
  959. return ret;
  960. }
  961. EXPORT_SYMBOL(tcf_action_exec);
  962. int tcf_action_destroy(struct tc_action *actions[], int bind)
  963. {
  964. const struct tc_action_ops *ops;
  965. struct tc_action *a;
  966. int ret = 0, i;
  967. tcf_act_for_each_action(i, a, actions) {
  968. actions[i] = NULL;
  969. ops = a->ops;
  970. ret = __tcf_idr_release(a, bind, true);
  971. if (ret == ACT_P_DELETED)
  972. module_put(ops->owner);
  973. else if (ret < 0)
  974. return ret;
  975. }
  976. return ret;
  977. }
  978. static int tcf_action_put(struct tc_action *p)
  979. {
  980. return __tcf_action_put(p, false);
  981. }
  982. static void tcf_action_put_many(struct tc_action *actions[])
  983. {
  984. struct tc_action *a;
  985. int i;
  986. tcf_act_for_each_action(i, a, actions) {
  987. const struct tc_action_ops *ops = a->ops;
  988. if (tcf_action_put(a))
  989. module_put(ops->owner);
  990. }
  991. }
  992. static void tca_put_bound_many(struct tc_action *actions[], int init_res[])
  993. {
  994. struct tc_action *a;
  995. int i;
  996. tcf_act_for_each_action(i, a, actions) {
  997. const struct tc_action_ops *ops = a->ops;
  998. if (init_res[i] == ACT_P_CREATED)
  999. continue;
  1000. if (tcf_action_put(a))
  1001. module_put(ops->owner);
  1002. }
  1003. }
  1004. int
  1005. tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  1006. {
  1007. return a->ops->dump(skb, a, bind, ref);
  1008. }
  1009. int
  1010. tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  1011. {
  1012. int err = -EINVAL;
  1013. unsigned char *b = skb_tail_pointer(skb);
  1014. struct nlattr *nest;
  1015. u32 flags;
  1016. if (tcf_action_dump_terse(skb, a, false))
  1017. goto nla_put_failure;
  1018. if (a->hw_stats != TCA_ACT_HW_STATS_ANY &&
  1019. nla_put_bitfield32(skb, TCA_ACT_HW_STATS,
  1020. a->hw_stats, TCA_ACT_HW_STATS_ANY))
  1021. goto nla_put_failure;
  1022. if (a->used_hw_stats_valid &&
  1023. nla_put_bitfield32(skb, TCA_ACT_USED_HW_STATS,
  1024. a->used_hw_stats, TCA_ACT_HW_STATS_ANY))
  1025. goto nla_put_failure;
  1026. flags = a->tcfa_flags & TCA_ACT_FLAGS_USER_MASK;
  1027. if (flags &&
  1028. nla_put_bitfield32(skb, TCA_ACT_FLAGS,
  1029. flags, flags))
  1030. goto nla_put_failure;
  1031. if (nla_put_u32(skb, TCA_ACT_IN_HW_COUNT, a->in_hw_count))
  1032. goto nla_put_failure;
  1033. nest = nla_nest_start_noflag(skb, TCA_ACT_OPTIONS);
  1034. if (nest == NULL)
  1035. goto nla_put_failure;
  1036. err = tcf_action_dump_old(skb, a, bind, ref);
  1037. if (err > 0) {
  1038. nla_nest_end(skb, nest);
  1039. return err;
  1040. }
  1041. nla_put_failure:
  1042. nlmsg_trim(skb, b);
  1043. return -1;
  1044. }
  1045. EXPORT_SYMBOL(tcf_action_dump_1);
  1046. int tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[],
  1047. int bind, int ref, bool terse)
  1048. {
  1049. struct tc_action *a;
  1050. int err = -EINVAL, i;
  1051. struct nlattr *nest;
  1052. tcf_act_for_each_action(i, a, actions) {
  1053. nest = nla_nest_start_noflag(skb, i + 1);
  1054. if (nest == NULL)
  1055. goto nla_put_failure;
  1056. err = terse ? tcf_action_dump_terse(skb, a, false) :
  1057. tcf_action_dump_1(skb, a, bind, ref);
  1058. if (err < 0)
  1059. goto errout;
  1060. nla_nest_end(skb, nest);
  1061. }
  1062. return 0;
  1063. nla_put_failure:
  1064. err = -EINVAL;
  1065. errout:
  1066. nla_nest_cancel(skb, nest);
  1067. return err;
  1068. }
  1069. static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
  1070. {
  1071. struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL);
  1072. if (!c)
  1073. return NULL;
  1074. c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
  1075. if (!c->data) {
  1076. kfree(c);
  1077. return NULL;
  1078. }
  1079. c->len = nla_len(tb[TCA_ACT_COOKIE]);
  1080. return c;
  1081. }
  1082. static u8 tcf_action_hw_stats_get(struct nlattr *hw_stats_attr)
  1083. {
  1084. struct nla_bitfield32 hw_stats_bf;
  1085. /* If the user did not pass the attr, that means he does
  1086. * not care about the type. Return "any" in that case
  1087. * which is setting on all supported types.
  1088. */
  1089. if (!hw_stats_attr)
  1090. return TCA_ACT_HW_STATS_ANY;
  1091. hw_stats_bf = nla_get_bitfield32(hw_stats_attr);
  1092. return hw_stats_bf.value;
  1093. }
  1094. static const struct nla_policy tcf_action_policy[TCA_ACT_MAX + 1] = {
  1095. [TCA_ACT_KIND] = { .type = NLA_STRING },
  1096. [TCA_ACT_INDEX] = { .type = NLA_U32 },
  1097. [TCA_ACT_COOKIE] = { .type = NLA_BINARY,
  1098. .len = TC_COOKIE_MAX_SIZE },
  1099. [TCA_ACT_OPTIONS] = { .type = NLA_NESTED },
  1100. [TCA_ACT_FLAGS] = NLA_POLICY_BITFIELD32(TCA_ACT_FLAGS_NO_PERCPU_STATS |
  1101. TCA_ACT_FLAGS_SKIP_HW |
  1102. TCA_ACT_FLAGS_SKIP_SW),
  1103. [TCA_ACT_HW_STATS] = NLA_POLICY_BITFIELD32(TCA_ACT_HW_STATS_ANY),
  1104. };
  1105. void tcf_idr_insert_many(struct tc_action *actions[], int init_res[])
  1106. {
  1107. struct tc_action *a;
  1108. int i;
  1109. tcf_act_for_each_action(i, a, actions) {
  1110. struct tcf_idrinfo *idrinfo;
  1111. if (init_res[i] == ACT_P_BOUND)
  1112. continue;
  1113. idrinfo = a->idrinfo;
  1114. mutex_lock(&idrinfo->lock);
  1115. /* Replace ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
  1116. idr_replace(&idrinfo->action_idr, a, a->tcfa_index);
  1117. mutex_unlock(&idrinfo->lock);
  1118. }
  1119. }
  1120. struct tc_action_ops *tc_action_load_ops(struct nlattr *nla, u32 flags,
  1121. struct netlink_ext_ack *extack)
  1122. {
  1123. bool police = flags & TCA_ACT_FLAGS_POLICE;
  1124. struct nlattr *tb[TCA_ACT_MAX + 1];
  1125. struct tc_action_ops *a_o;
  1126. char act_name[IFNAMSIZ];
  1127. struct nlattr *kind;
  1128. int err;
  1129. if (!police) {
  1130. err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
  1131. tcf_action_policy, extack);
  1132. if (err < 0)
  1133. return ERR_PTR(err);
  1134. err = -EINVAL;
  1135. kind = tb[TCA_ACT_KIND];
  1136. if (!kind) {
  1137. NL_SET_ERR_MSG(extack, "TC action kind must be specified");
  1138. return ERR_PTR(err);
  1139. }
  1140. if (nla_strscpy(act_name, kind, IFNAMSIZ) < 0) {
  1141. NL_SET_ERR_MSG(extack, "TC action name too long");
  1142. return ERR_PTR(err);
  1143. }
  1144. } else {
  1145. if (strscpy(act_name, "police", IFNAMSIZ) < 0) {
  1146. NL_SET_ERR_MSG(extack, "TC action name too long");
  1147. return ERR_PTR(-EINVAL);
  1148. }
  1149. }
  1150. a_o = tc_lookup_action_n(act_name);
  1151. if (a_o == NULL) {
  1152. #ifdef CONFIG_MODULES
  1153. bool rtnl_held = !(flags & TCA_ACT_FLAGS_NO_RTNL);
  1154. if (rtnl_held)
  1155. rtnl_unlock();
  1156. request_module(NET_ACT_ALIAS_PREFIX "%s", act_name);
  1157. if (rtnl_held)
  1158. rtnl_lock();
  1159. a_o = tc_lookup_action_n(act_name);
  1160. /* We dropped the RTNL semaphore in order to
  1161. * perform the module load. So, even if we
  1162. * succeeded in loading the module we have to
  1163. * tell the caller to replay the request. We
  1164. * indicate this using -EAGAIN.
  1165. */
  1166. if (a_o != NULL) {
  1167. module_put(a_o->owner);
  1168. return ERR_PTR(-EAGAIN);
  1169. }
  1170. #endif
  1171. NL_SET_ERR_MSG(extack, "Failed to load TC action module");
  1172. return ERR_PTR(-ENOENT);
  1173. }
  1174. return a_o;
  1175. }
  1176. struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
  1177. struct nlattr *nla, struct nlattr *est,
  1178. struct tc_action_ops *a_o, int *init_res,
  1179. u32 flags, struct netlink_ext_ack *extack)
  1180. {
  1181. bool police = flags & TCA_ACT_FLAGS_POLICE;
  1182. struct nla_bitfield32 userflags = { 0, 0 };
  1183. struct tc_cookie *user_cookie = NULL;
  1184. u8 hw_stats = TCA_ACT_HW_STATS_ANY;
  1185. struct nlattr *tb[TCA_ACT_MAX + 1];
  1186. struct tc_action *a;
  1187. int err;
  1188. /* backward compatibility for policer */
  1189. if (!police) {
  1190. err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
  1191. tcf_action_policy, extack);
  1192. if (err < 0)
  1193. return ERR_PTR(err);
  1194. if (tb[TCA_ACT_COOKIE]) {
  1195. user_cookie = nla_memdup_cookie(tb);
  1196. if (!user_cookie) {
  1197. NL_SET_ERR_MSG(extack, "No memory to generate TC cookie");
  1198. err = -ENOMEM;
  1199. goto err_out;
  1200. }
  1201. }
  1202. hw_stats = tcf_action_hw_stats_get(tb[TCA_ACT_HW_STATS]);
  1203. if (tb[TCA_ACT_FLAGS]) {
  1204. userflags = nla_get_bitfield32(tb[TCA_ACT_FLAGS]);
  1205. if (!tc_act_flags_valid(userflags.value)) {
  1206. err = -EINVAL;
  1207. goto err_out;
  1208. }
  1209. }
  1210. err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, tp,
  1211. userflags.value | flags, extack);
  1212. } else {
  1213. err = a_o->init(net, nla, est, &a, tp, userflags.value | flags,
  1214. extack);
  1215. }
  1216. if (err < 0)
  1217. goto err_out;
  1218. *init_res = err;
  1219. if (!police && tb[TCA_ACT_COOKIE])
  1220. tcf_set_action_cookie(&a->user_cookie, user_cookie);
  1221. if (!police)
  1222. a->hw_stats = hw_stats;
  1223. return a;
  1224. err_out:
  1225. if (user_cookie) {
  1226. kfree(user_cookie->data);
  1227. kfree(user_cookie);
  1228. }
  1229. return ERR_PTR(err);
  1230. }
  1231. static bool tc_act_bind(u32 flags)
  1232. {
  1233. return !!(flags & TCA_ACT_FLAGS_BIND);
  1234. }
  1235. /* Returns numbers of initialized actions or negative error. */
  1236. int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
  1237. struct nlattr *est, struct tc_action *actions[],
  1238. int init_res[], size_t *attr_size,
  1239. u32 flags, u32 fl_flags,
  1240. struct netlink_ext_ack *extack)
  1241. {
  1242. struct tc_action_ops *ops[TCA_ACT_MAX_PRIO] = {};
  1243. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  1244. struct tc_action *act;
  1245. size_t sz = 0;
  1246. int err;
  1247. int i;
  1248. err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO, nla, NULL,
  1249. extack);
  1250. if (err < 0)
  1251. return err;
  1252. for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
  1253. struct tc_action_ops *a_o;
  1254. a_o = tc_action_load_ops(tb[i], flags, extack);
  1255. if (IS_ERR(a_o)) {
  1256. err = PTR_ERR(a_o);
  1257. goto err_mod;
  1258. }
  1259. ops[i - 1] = a_o;
  1260. }
  1261. for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
  1262. act = tcf_action_init_1(net, tp, tb[i], est, ops[i - 1],
  1263. &init_res[i - 1], flags, extack);
  1264. if (IS_ERR(act)) {
  1265. err = PTR_ERR(act);
  1266. goto err;
  1267. }
  1268. sz += tcf_action_fill_size(act);
  1269. /* Start from index 0 */
  1270. actions[i - 1] = act;
  1271. if (tc_act_bind(flags)) {
  1272. bool skip_sw = tc_skip_sw(fl_flags);
  1273. bool skip_hw = tc_skip_hw(fl_flags);
  1274. if (tc_act_bind(act->tcfa_flags)) {
  1275. /* Action is created by classifier and is not
  1276. * standalone. Check that the user did not set
  1277. * any action flags different than the
  1278. * classifier flags, and inherit the flags from
  1279. * the classifier for the compatibility case
  1280. * where no flags were specified at all.
  1281. */
  1282. if ((tc_act_skip_sw(act->tcfa_flags) && !skip_sw) ||
  1283. (tc_act_skip_hw(act->tcfa_flags) && !skip_hw)) {
  1284. NL_SET_ERR_MSG(extack,
  1285. "Mismatch between action and filter offload flags");
  1286. err = -EINVAL;
  1287. goto err;
  1288. }
  1289. if (skip_sw)
  1290. act->tcfa_flags |= TCA_ACT_FLAGS_SKIP_SW;
  1291. if (skip_hw)
  1292. act->tcfa_flags |= TCA_ACT_FLAGS_SKIP_HW;
  1293. continue;
  1294. }
  1295. /* Action is standalone */
  1296. if (skip_sw != tc_act_skip_sw(act->tcfa_flags) ||
  1297. skip_hw != tc_act_skip_hw(act->tcfa_flags)) {
  1298. NL_SET_ERR_MSG(extack,
  1299. "Mismatch between action and filter offload flags");
  1300. err = -EINVAL;
  1301. goto err;
  1302. }
  1303. } else {
  1304. err = tcf_action_offload_add(act, extack);
  1305. if (tc_act_skip_sw(act->tcfa_flags) && err)
  1306. goto err;
  1307. }
  1308. }
  1309. /* We have to commit them all together, because if any error happened in
  1310. * between, we could not handle the failure gracefully.
  1311. */
  1312. tcf_idr_insert_many(actions, init_res);
  1313. *attr_size = tcf_action_full_attrs_size(sz);
  1314. err = i - 1;
  1315. goto err_mod;
  1316. err:
  1317. tcf_action_destroy(actions, flags & TCA_ACT_FLAGS_BIND);
  1318. err_mod:
  1319. for (i = 0; i < TCA_ACT_MAX_PRIO && ops[i]; i++)
  1320. module_put(ops[i]->owner);
  1321. return err;
  1322. }
  1323. void tcf_action_update_stats(struct tc_action *a, u64 bytes, u64 packets,
  1324. u64 drops, bool hw)
  1325. {
  1326. if (a->cpu_bstats) {
  1327. _bstats_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
  1328. this_cpu_ptr(a->cpu_qstats)->drops += drops;
  1329. if (hw)
  1330. _bstats_update(this_cpu_ptr(a->cpu_bstats_hw),
  1331. bytes, packets);
  1332. return;
  1333. }
  1334. _bstats_update(&a->tcfa_bstats, bytes, packets);
  1335. a->tcfa_qstats.drops += drops;
  1336. if (hw)
  1337. _bstats_update(&a->tcfa_bstats_hw, bytes, packets);
  1338. }
  1339. EXPORT_SYMBOL(tcf_action_update_stats);
  1340. int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
  1341. int compat_mode)
  1342. {
  1343. int err = 0;
  1344. struct gnet_dump d;
  1345. if (p == NULL)
  1346. goto errout;
  1347. /* compat_mode being true specifies a call that is supposed
  1348. * to add additional backward compatibility statistic TLVs.
  1349. */
  1350. if (compat_mode) {
  1351. if (p->type == TCA_OLD_COMPAT)
  1352. err = gnet_stats_start_copy_compat(skb, 0,
  1353. TCA_STATS,
  1354. TCA_XSTATS,
  1355. &p->tcfa_lock, &d,
  1356. TCA_PAD);
  1357. else
  1358. return 0;
  1359. } else
  1360. err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
  1361. &p->tcfa_lock, &d, TCA_ACT_PAD);
  1362. if (err < 0)
  1363. goto errout;
  1364. if (gnet_stats_copy_basic(&d, p->cpu_bstats,
  1365. &p->tcfa_bstats, false) < 0 ||
  1366. gnet_stats_copy_basic_hw(&d, p->cpu_bstats_hw,
  1367. &p->tcfa_bstats_hw, false) < 0 ||
  1368. gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
  1369. gnet_stats_copy_queue(&d, p->cpu_qstats,
  1370. &p->tcfa_qstats,
  1371. p->tcfa_qstats.qlen) < 0)
  1372. goto errout;
  1373. if (gnet_stats_finish_copy(&d) < 0)
  1374. goto errout;
  1375. return 0;
  1376. errout:
  1377. return -1;
  1378. }
  1379. static int tca_get_fill(struct sk_buff *skb, struct tc_action *actions[],
  1380. u32 portid, u32 seq, u16 flags, int event, int bind,
  1381. int ref, struct netlink_ext_ack *extack)
  1382. {
  1383. struct tcamsg *t;
  1384. struct nlmsghdr *nlh;
  1385. unsigned char *b = skb_tail_pointer(skb);
  1386. struct nlattr *nest;
  1387. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
  1388. if (!nlh)
  1389. goto out_nlmsg_trim;
  1390. t = nlmsg_data(nlh);
  1391. t->tca_family = AF_UNSPEC;
  1392. t->tca__pad1 = 0;
  1393. t->tca__pad2 = 0;
  1394. if (extack && extack->_msg &&
  1395. nla_put_string(skb, TCA_ROOT_EXT_WARN_MSG, extack->_msg))
  1396. goto out_nlmsg_trim;
  1397. nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
  1398. if (!nest)
  1399. goto out_nlmsg_trim;
  1400. if (tcf_action_dump(skb, actions, bind, ref, false) < 0)
  1401. goto out_nlmsg_trim;
  1402. nla_nest_end(skb, nest);
  1403. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  1404. return skb->len;
  1405. out_nlmsg_trim:
  1406. nlmsg_trim(skb, b);
  1407. return -1;
  1408. }
  1409. static int
  1410. tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
  1411. struct tc_action *actions[], int event,
  1412. struct netlink_ext_ack *extack)
  1413. {
  1414. struct sk_buff *skb;
  1415. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  1416. if (!skb)
  1417. return -ENOBUFS;
  1418. if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
  1419. 0, 1, NULL) <= 0) {
  1420. NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
  1421. kfree_skb(skb);
  1422. return -EINVAL;
  1423. }
  1424. return rtnl_unicast(skb, net, portid);
  1425. }
  1426. static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
  1427. struct nlmsghdr *n, u32 portid,
  1428. struct netlink_ext_ack *extack)
  1429. {
  1430. struct nlattr *tb[TCA_ACT_MAX + 1];
  1431. const struct tc_action_ops *ops;
  1432. struct tc_action *a;
  1433. int index;
  1434. int err;
  1435. err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
  1436. tcf_action_policy, extack);
  1437. if (err < 0)
  1438. goto err_out;
  1439. err = -EINVAL;
  1440. if (tb[TCA_ACT_INDEX] == NULL ||
  1441. nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) {
  1442. NL_SET_ERR_MSG(extack, "Invalid TC action index value");
  1443. goto err_out;
  1444. }
  1445. index = nla_get_u32(tb[TCA_ACT_INDEX]);
  1446. err = -EINVAL;
  1447. ops = tc_lookup_action(tb[TCA_ACT_KIND]);
  1448. if (!ops) { /* could happen in batch of actions */
  1449. NL_SET_ERR_MSG(extack, "Specified TC action kind not found");
  1450. goto err_out;
  1451. }
  1452. err = -ENOENT;
  1453. if (__tcf_idr_search(net, ops, &a, index) == 0) {
  1454. NL_SET_ERR_MSG(extack, "TC action with specified index not found");
  1455. goto err_mod;
  1456. }
  1457. module_put(ops->owner);
  1458. return a;
  1459. err_mod:
  1460. module_put(ops->owner);
  1461. err_out:
  1462. return ERR_PTR(err);
  1463. }
  1464. static int tca_action_flush(struct net *net, struct nlattr *nla,
  1465. struct nlmsghdr *n, u32 portid,
  1466. struct netlink_ext_ack *extack)
  1467. {
  1468. struct sk_buff *skb;
  1469. unsigned char *b;
  1470. struct nlmsghdr *nlh;
  1471. struct tcamsg *t;
  1472. struct netlink_callback dcb;
  1473. struct nlattr *nest;
  1474. struct nlattr *tb[TCA_ACT_MAX + 1];
  1475. const struct tc_action_ops *ops;
  1476. struct nlattr *kind;
  1477. int err = -ENOMEM;
  1478. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  1479. if (!skb)
  1480. return err;
  1481. b = skb_tail_pointer(skb);
  1482. err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
  1483. tcf_action_policy, extack);
  1484. if (err < 0)
  1485. goto err_out;
  1486. err = -EINVAL;
  1487. kind = tb[TCA_ACT_KIND];
  1488. ops = tc_lookup_action(kind);
  1489. if (!ops) { /*some idjot trying to flush unknown action */
  1490. NL_SET_ERR_MSG(extack, "Cannot flush unknown TC action");
  1491. goto err_out;
  1492. }
  1493. nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
  1494. sizeof(*t), 0);
  1495. if (!nlh) {
  1496. NL_SET_ERR_MSG(extack, "Failed to create TC action flush notification");
  1497. goto out_module_put;
  1498. }
  1499. t = nlmsg_data(nlh);
  1500. t->tca_family = AF_UNSPEC;
  1501. t->tca__pad1 = 0;
  1502. t->tca__pad2 = 0;
  1503. nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
  1504. if (!nest) {
  1505. NL_SET_ERR_MSG(extack, "Failed to add new netlink message");
  1506. goto out_module_put;
  1507. }
  1508. err = __tcf_generic_walker(net, skb, &dcb, RTM_DELACTION, ops, extack);
  1509. if (err <= 0) {
  1510. nla_nest_cancel(skb, nest);
  1511. goto out_module_put;
  1512. }
  1513. nla_nest_end(skb, nest);
  1514. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  1515. nlh->nlmsg_flags |= NLM_F_ROOT;
  1516. module_put(ops->owner);
  1517. err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  1518. n->nlmsg_flags & NLM_F_ECHO);
  1519. if (err < 0)
  1520. NL_SET_ERR_MSG(extack, "Failed to send TC action flush notification");
  1521. return err;
  1522. out_module_put:
  1523. module_put(ops->owner);
  1524. err_out:
  1525. kfree_skb(skb);
  1526. return err;
  1527. }
  1528. static int tcf_action_delete(struct net *net, struct tc_action *actions[])
  1529. {
  1530. struct tc_action *a;
  1531. int i;
  1532. tcf_act_for_each_action(i, a, actions) {
  1533. const struct tc_action_ops *ops = a->ops;
  1534. /* Actions can be deleted concurrently so we must save their
  1535. * type and id to search again after reference is released.
  1536. */
  1537. struct tcf_idrinfo *idrinfo = a->idrinfo;
  1538. u32 act_index = a->tcfa_index;
  1539. actions[i] = NULL;
  1540. if (tcf_action_put(a)) {
  1541. /* last reference, action was deleted concurrently */
  1542. module_put(ops->owner);
  1543. } else {
  1544. int ret;
  1545. /* now do the delete */
  1546. ret = tcf_idr_delete_index(idrinfo, act_index);
  1547. if (ret < 0)
  1548. return ret;
  1549. }
  1550. }
  1551. return 0;
  1552. }
  1553. static struct sk_buff *tcf_reoffload_del_notify_msg(struct net *net,
  1554. struct tc_action *action)
  1555. {
  1556. size_t attr_size = tcf_action_fill_size(action);
  1557. struct tc_action *actions[TCA_ACT_MAX_PRIO] = {
  1558. [0] = action,
  1559. };
  1560. struct sk_buff *skb;
  1561. skb = alloc_skb(max(attr_size, NLMSG_GOODSIZE), GFP_KERNEL);
  1562. if (!skb)
  1563. return ERR_PTR(-ENOBUFS);
  1564. if (tca_get_fill(skb, actions, 0, 0, 0, RTM_DELACTION, 0, 1, NULL) <= 0) {
  1565. kfree_skb(skb);
  1566. return ERR_PTR(-EINVAL);
  1567. }
  1568. return skb;
  1569. }
  1570. static int tcf_reoffload_del_notify(struct net *net, struct tc_action *action)
  1571. {
  1572. const struct tc_action_ops *ops = action->ops;
  1573. struct sk_buff *skb;
  1574. int ret;
  1575. if (!rtnl_notify_needed(net, 0, RTNLGRP_TC)) {
  1576. skb = NULL;
  1577. } else {
  1578. skb = tcf_reoffload_del_notify_msg(net, action);
  1579. if (IS_ERR(skb))
  1580. return PTR_ERR(skb);
  1581. }
  1582. ret = tcf_idr_release_unsafe(action);
  1583. if (ret == ACT_P_DELETED) {
  1584. module_put(ops->owner);
  1585. ret = rtnetlink_maybe_send(skb, net, 0, RTNLGRP_TC, 0);
  1586. } else {
  1587. kfree_skb(skb);
  1588. }
  1589. return ret;
  1590. }
  1591. int tcf_action_reoffload_cb(flow_indr_block_bind_cb_t *cb,
  1592. void *cb_priv, bool add)
  1593. {
  1594. struct tc_act_pernet_id *id_ptr;
  1595. struct tcf_idrinfo *idrinfo;
  1596. struct tc_action_net *tn;
  1597. struct tc_action *p;
  1598. unsigned int act_id;
  1599. unsigned long tmp;
  1600. unsigned long id;
  1601. struct idr *idr;
  1602. struct net *net;
  1603. int ret;
  1604. if (!cb)
  1605. return -EINVAL;
  1606. down_read(&net_rwsem);
  1607. mutex_lock(&act_id_mutex);
  1608. for_each_net(net) {
  1609. list_for_each_entry(id_ptr, &act_pernet_id_list, list) {
  1610. act_id = id_ptr->id;
  1611. tn = net_generic(net, act_id);
  1612. if (!tn)
  1613. continue;
  1614. idrinfo = tn->idrinfo;
  1615. if (!idrinfo)
  1616. continue;
  1617. mutex_lock(&idrinfo->lock);
  1618. idr = &idrinfo->action_idr;
  1619. idr_for_each_entry_ul(idr, p, tmp, id) {
  1620. if (IS_ERR(p) || tc_act_bind(p->tcfa_flags))
  1621. continue;
  1622. if (add) {
  1623. tcf_action_offload_add_ex(p, NULL, cb,
  1624. cb_priv);
  1625. continue;
  1626. }
  1627. /* cb unregister to update hw count */
  1628. ret = tcf_action_offload_del_ex(p, cb, cb_priv);
  1629. if (ret < 0)
  1630. continue;
  1631. if (tc_act_skip_sw(p->tcfa_flags) &&
  1632. !tc_act_in_hw(p))
  1633. tcf_reoffload_del_notify(net, p);
  1634. }
  1635. mutex_unlock(&idrinfo->lock);
  1636. }
  1637. }
  1638. mutex_unlock(&act_id_mutex);
  1639. up_read(&net_rwsem);
  1640. return 0;
  1641. }
  1642. static struct sk_buff *tcf_del_notify_msg(struct net *net, struct nlmsghdr *n,
  1643. struct tc_action *actions[],
  1644. u32 portid, size_t attr_size,
  1645. struct netlink_ext_ack *extack)
  1646. {
  1647. struct sk_buff *skb;
  1648. skb = alloc_skb(max(attr_size, NLMSG_GOODSIZE), GFP_KERNEL);
  1649. if (!skb)
  1650. return ERR_PTR(-ENOBUFS);
  1651. if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
  1652. 0, 2, extack) <= 0) {
  1653. NL_SET_ERR_MSG(extack, "Failed to fill netlink TC action attributes");
  1654. kfree_skb(skb);
  1655. return ERR_PTR(-EINVAL);
  1656. }
  1657. return skb;
  1658. }
  1659. static int tcf_del_notify(struct net *net, struct nlmsghdr *n,
  1660. struct tc_action *actions[], u32 portid,
  1661. size_t attr_size, struct netlink_ext_ack *extack)
  1662. {
  1663. struct sk_buff *skb;
  1664. int ret;
  1665. if (!rtnl_notify_needed(net, n->nlmsg_flags, RTNLGRP_TC)) {
  1666. skb = NULL;
  1667. } else {
  1668. skb = tcf_del_notify_msg(net, n, actions, portid, attr_size,
  1669. extack);
  1670. if (IS_ERR(skb))
  1671. return PTR_ERR(skb);
  1672. }
  1673. /* now do the delete */
  1674. ret = tcf_action_delete(net, actions);
  1675. if (ret < 0) {
  1676. NL_SET_ERR_MSG(extack, "Failed to delete TC action");
  1677. kfree_skb(skb);
  1678. return ret;
  1679. }
  1680. return rtnetlink_maybe_send(skb, net, portid, RTNLGRP_TC,
  1681. n->nlmsg_flags & NLM_F_ECHO);
  1682. }
  1683. static int
  1684. tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
  1685. u32 portid, int event, struct netlink_ext_ack *extack)
  1686. {
  1687. int i, ret;
  1688. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  1689. struct tc_action *act;
  1690. size_t attr_size = 0;
  1691. struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
  1692. ret = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO, nla, NULL,
  1693. extack);
  1694. if (ret < 0)
  1695. return ret;
  1696. if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
  1697. if (tb[1])
  1698. return tca_action_flush(net, tb[1], n, portid, extack);
  1699. NL_SET_ERR_MSG(extack, "Invalid netlink attributes while flushing TC action");
  1700. return -EINVAL;
  1701. }
  1702. for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
  1703. act = tcf_action_get_1(net, tb[i], n, portid, extack);
  1704. if (IS_ERR(act)) {
  1705. ret = PTR_ERR(act);
  1706. goto err;
  1707. }
  1708. attr_size += tcf_action_fill_size(act);
  1709. actions[i - 1] = act;
  1710. }
  1711. attr_size = tcf_action_full_attrs_size(attr_size);
  1712. if (event == RTM_GETACTION)
  1713. ret = tcf_get_notify(net, portid, n, actions, event, extack);
  1714. else { /* delete */
  1715. ret = tcf_del_notify(net, n, actions, portid, attr_size, extack);
  1716. if (ret)
  1717. goto err;
  1718. return 0;
  1719. }
  1720. err:
  1721. tcf_action_put_many(actions);
  1722. return ret;
  1723. }
  1724. static struct sk_buff *tcf_add_notify_msg(struct net *net, struct nlmsghdr *n,
  1725. struct tc_action *actions[],
  1726. u32 portid, size_t attr_size,
  1727. struct netlink_ext_ack *extack)
  1728. {
  1729. struct sk_buff *skb;
  1730. skb = alloc_skb(max(attr_size, NLMSG_GOODSIZE), GFP_KERNEL);
  1731. if (!skb)
  1732. return ERR_PTR(-ENOBUFS);
  1733. if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
  1734. RTM_NEWACTION, 0, 0, extack) <= 0) {
  1735. NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
  1736. kfree_skb(skb);
  1737. return ERR_PTR(-EINVAL);
  1738. }
  1739. return skb;
  1740. }
  1741. static int tcf_add_notify(struct net *net, struct nlmsghdr *n,
  1742. struct tc_action *actions[], u32 portid,
  1743. size_t attr_size, struct netlink_ext_ack *extack)
  1744. {
  1745. struct sk_buff *skb;
  1746. if (!rtnl_notify_needed(net, n->nlmsg_flags, RTNLGRP_TC)) {
  1747. skb = NULL;
  1748. } else {
  1749. skb = tcf_add_notify_msg(net, n, actions, portid, attr_size,
  1750. extack);
  1751. if (IS_ERR(skb))
  1752. return PTR_ERR(skb);
  1753. }
  1754. return rtnetlink_maybe_send(skb, net, portid, RTNLGRP_TC,
  1755. n->nlmsg_flags & NLM_F_ECHO);
  1756. }
  1757. static int tcf_action_add(struct net *net, struct nlattr *nla,
  1758. struct nlmsghdr *n, u32 portid, u32 flags,
  1759. struct netlink_ext_ack *extack)
  1760. {
  1761. size_t attr_size = 0;
  1762. int loop, ret;
  1763. struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
  1764. int init_res[TCA_ACT_MAX_PRIO] = {};
  1765. for (loop = 0; loop < 10; loop++) {
  1766. ret = tcf_action_init(net, NULL, nla, NULL, actions, init_res,
  1767. &attr_size, flags, 0, extack);
  1768. if (ret != -EAGAIN)
  1769. break;
  1770. }
  1771. if (ret < 0)
  1772. return ret;
  1773. ret = tcf_add_notify(net, n, actions, portid, attr_size, extack);
  1774. /* only put bound actions */
  1775. tca_put_bound_many(actions, init_res);
  1776. return ret;
  1777. }
  1778. static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = {
  1779. [TCA_ROOT_FLAGS] = NLA_POLICY_BITFIELD32(TCA_ACT_FLAG_LARGE_DUMP_ON |
  1780. TCA_ACT_FLAG_TERSE_DUMP),
  1781. [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 },
  1782. };
  1783. static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
  1784. struct netlink_ext_ack *extack)
  1785. {
  1786. struct net *net = sock_net(skb->sk);
  1787. struct nlattr *tca[TCA_ROOT_MAX + 1];
  1788. u32 portid = NETLINK_CB(skb).portid;
  1789. u32 flags = 0;
  1790. int ret = 0;
  1791. if ((n->nlmsg_type != RTM_GETACTION) &&
  1792. !netlink_capable(skb, CAP_NET_ADMIN))
  1793. return -EPERM;
  1794. ret = nlmsg_parse_deprecated(n, sizeof(struct tcamsg), tca,
  1795. TCA_ROOT_MAX, NULL, extack);
  1796. if (ret < 0)
  1797. return ret;
  1798. if (tca[TCA_ACT_TAB] == NULL) {
  1799. NL_SET_ERR_MSG(extack, "Netlink action attributes missing");
  1800. return -EINVAL;
  1801. }
  1802. /* n->nlmsg_flags & NLM_F_CREATE */
  1803. switch (n->nlmsg_type) {
  1804. case RTM_NEWACTION:
  1805. /* we are going to assume all other flags
  1806. * imply create only if it doesn't exist
  1807. * Note that CREATE | EXCL implies that
  1808. * but since we want avoid ambiguity (eg when flags
  1809. * is zero) then just set this
  1810. */
  1811. if (n->nlmsg_flags & NLM_F_REPLACE)
  1812. flags = TCA_ACT_FLAGS_REPLACE;
  1813. ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, flags,
  1814. extack);
  1815. break;
  1816. case RTM_DELACTION:
  1817. ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
  1818. portid, RTM_DELACTION, extack);
  1819. break;
  1820. case RTM_GETACTION:
  1821. ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
  1822. portid, RTM_GETACTION, extack);
  1823. break;
  1824. default:
  1825. BUG();
  1826. }
  1827. return ret;
  1828. }
  1829. static struct nlattr *find_dump_kind(struct nlattr **nla)
  1830. {
  1831. struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
  1832. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  1833. struct nlattr *kind;
  1834. tb1 = nla[TCA_ACT_TAB];
  1835. if (tb1 == NULL)
  1836. return NULL;
  1837. if (nla_parse_deprecated(tb, TCA_ACT_MAX_PRIO, nla_data(tb1), NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0)
  1838. return NULL;
  1839. if (tb[1] == NULL)
  1840. return NULL;
  1841. if (nla_parse_nested_deprecated(tb2, TCA_ACT_MAX, tb[1], tcf_action_policy, NULL) < 0)
  1842. return NULL;
  1843. kind = tb2[TCA_ACT_KIND];
  1844. return kind;
  1845. }
  1846. static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
  1847. {
  1848. struct net *net = sock_net(skb->sk);
  1849. struct nlmsghdr *nlh;
  1850. unsigned char *b = skb_tail_pointer(skb);
  1851. struct nlattr *nest;
  1852. struct tc_action_ops *a_o;
  1853. int ret = 0;
  1854. struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
  1855. struct nlattr *tb[TCA_ROOT_MAX + 1];
  1856. struct nlattr *count_attr = NULL;
  1857. unsigned long jiffy_since = 0;
  1858. struct nlattr *kind = NULL;
  1859. struct nla_bitfield32 bf;
  1860. u32 msecs_since = 0;
  1861. u32 act_count = 0;
  1862. ret = nlmsg_parse_deprecated(cb->nlh, sizeof(struct tcamsg), tb,
  1863. TCA_ROOT_MAX, tcaa_policy, cb->extack);
  1864. if (ret < 0)
  1865. return ret;
  1866. kind = find_dump_kind(tb);
  1867. if (kind == NULL) {
  1868. pr_info("tc_dump_action: action bad kind\n");
  1869. return 0;
  1870. }
  1871. a_o = tc_lookup_action(kind);
  1872. if (a_o == NULL)
  1873. return 0;
  1874. cb->args[2] = 0;
  1875. if (tb[TCA_ROOT_FLAGS]) {
  1876. bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]);
  1877. cb->args[2] = bf.value;
  1878. }
  1879. if (tb[TCA_ROOT_TIME_DELTA]) {
  1880. msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]);
  1881. }
  1882. nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  1883. cb->nlh->nlmsg_type, sizeof(*t), 0);
  1884. if (!nlh)
  1885. goto out_module_put;
  1886. if (msecs_since)
  1887. jiffy_since = jiffies - msecs_to_jiffies(msecs_since);
  1888. t = nlmsg_data(nlh);
  1889. t->tca_family = AF_UNSPEC;
  1890. t->tca__pad1 = 0;
  1891. t->tca__pad2 = 0;
  1892. cb->args[3] = jiffy_since;
  1893. count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32));
  1894. if (!count_attr)
  1895. goto out_module_put;
  1896. nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
  1897. if (nest == NULL)
  1898. goto out_module_put;
  1899. ret = __tcf_generic_walker(net, skb, cb, RTM_GETACTION, a_o, NULL);
  1900. if (ret < 0)
  1901. goto out_module_put;
  1902. if (ret > 0) {
  1903. nla_nest_end(skb, nest);
  1904. ret = skb->len;
  1905. act_count = cb->args[1];
  1906. memcpy(nla_data(count_attr), &act_count, sizeof(u32));
  1907. cb->args[1] = 0;
  1908. } else
  1909. nlmsg_trim(skb, b);
  1910. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  1911. if (NETLINK_CB(cb->skb).portid && ret)
  1912. nlh->nlmsg_flags |= NLM_F_MULTI;
  1913. module_put(a_o->owner);
  1914. return skb->len;
  1915. out_module_put:
  1916. module_put(a_o->owner);
  1917. nlmsg_trim(skb, b);
  1918. return skb->len;
  1919. }
  1920. static int __init tc_action_init(void)
  1921. {
  1922. rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0);
  1923. rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0);
  1924. rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
  1925. 0);
  1926. return 0;
  1927. }
  1928. subsys_initcall(tc_action_init);