act_api.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304
  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
  420. tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  421. {
  422. unsigned char *b = skb_tail_pointer(skb);
  423. struct nlattr *nest;
  424. int err = -EINVAL;
  425. u32 flags;
  426. if (tcf_action_dump_terse(skb, a, false))
  427. goto nla_put_failure;
  428. if (a->hw_stats != TCA_ACT_HW_STATS_ANY &&
  429. nla_put_bitfield32(skb, TCA_ACT_HW_STATS,
  430. a->hw_stats, TCA_ACT_HW_STATS_ANY))
  431. goto nla_put_failure;
  432. if (a->used_hw_stats_valid &&
  433. nla_put_bitfield32(skb, TCA_ACT_USED_HW_STATS,
  434. a->used_hw_stats, TCA_ACT_HW_STATS_ANY))
  435. goto nla_put_failure;
  436. flags = a->tcfa_flags & TCA_ACT_FLAGS_USER_MASK;
  437. if (flags &&
  438. nla_put_bitfield32(skb, TCA_ACT_FLAGS,
  439. flags, flags))
  440. goto nla_put_failure;
  441. if (nla_put_u32(skb, TCA_ACT_IN_HW_COUNT, a->in_hw_count))
  442. goto nla_put_failure;
  443. nest = nla_nest_start_noflag(skb, TCA_ACT_OPTIONS);
  444. if (nest == NULL)
  445. goto nla_put_failure;
  446. err = tcf_action_dump_old(skb, a, bind, ref);
  447. if (err > 0) {
  448. nla_nest_end(skb, nest);
  449. return err;
  450. }
  451. nla_put_failure:
  452. nlmsg_trim(skb, b);
  453. return -1;
  454. }
  455. static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
  456. struct netlink_callback *cb)
  457. {
  458. int err = 0, index = -1, s_i = 0, n_i = 0;
  459. u32 act_flags = cb->args[2];
  460. unsigned long jiffy_since = cb->args[3];
  461. struct nlattr *nest;
  462. struct idr *idr = &idrinfo->action_idr;
  463. struct tc_action *p;
  464. unsigned long id = 1;
  465. unsigned long tmp;
  466. mutex_lock(&idrinfo->lock);
  467. s_i = cb->args[0];
  468. idr_for_each_entry_ul(idr, p, tmp, id) {
  469. index++;
  470. if (index < s_i)
  471. continue;
  472. if (IS_ERR(p))
  473. continue;
  474. if (jiffy_since &&
  475. time_after(jiffy_since,
  476. (unsigned long)p->tcfa_tm.lastuse))
  477. continue;
  478. tcf_action_update_hw_stats(p);
  479. nest = nla_nest_start_noflag(skb, n_i);
  480. if (!nest) {
  481. index--;
  482. goto nla_put_failure;
  483. }
  484. err = (act_flags & TCA_ACT_FLAG_TERSE_DUMP) ?
  485. tcf_action_dump_terse(skb, p, true) :
  486. tcf_action_dump_1(skb, p, 0, 0);
  487. if (err < 0) {
  488. index--;
  489. nlmsg_trim(skb, nest);
  490. goto done;
  491. }
  492. nla_nest_end(skb, nest);
  493. n_i++;
  494. if (!(act_flags & TCA_ACT_FLAG_LARGE_DUMP_ON) &&
  495. n_i >= TCA_ACT_MAX_PRIO)
  496. goto done;
  497. }
  498. done:
  499. if (index >= 0)
  500. cb->args[0] = index + 1;
  501. mutex_unlock(&idrinfo->lock);
  502. if (n_i) {
  503. if (act_flags & TCA_ACT_FLAG_LARGE_DUMP_ON)
  504. cb->args[1] = n_i;
  505. }
  506. return n_i;
  507. nla_put_failure:
  508. nla_nest_cancel(skb, nest);
  509. goto done;
  510. }
  511. static int tcf_idr_release_unsafe(struct tc_action *p)
  512. {
  513. if (atomic_read(&p->tcfa_bindcnt) > 0)
  514. return -EPERM;
  515. if (refcount_dec_and_test(&p->tcfa_refcnt)) {
  516. idr_remove(&p->idrinfo->action_idr, p->tcfa_index);
  517. tcf_action_cleanup(p);
  518. return ACT_P_DELETED;
  519. }
  520. return 0;
  521. }
  522. static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
  523. const struct tc_action_ops *ops,
  524. struct netlink_ext_ack *extack)
  525. {
  526. struct nlattr *nest;
  527. int n_i = 0;
  528. int ret = -EINVAL;
  529. struct idr *idr = &idrinfo->action_idr;
  530. struct tc_action *p;
  531. unsigned long id = 1;
  532. unsigned long tmp;
  533. nest = nla_nest_start_noflag(skb, 0);
  534. if (nest == NULL)
  535. goto nla_put_failure;
  536. if (nla_put_string(skb, TCA_ACT_KIND, ops->kind))
  537. goto nla_put_failure;
  538. ret = 0;
  539. mutex_lock(&idrinfo->lock);
  540. idr_for_each_entry_ul(idr, p, tmp, id) {
  541. if (IS_ERR(p))
  542. continue;
  543. ret = tcf_idr_release_unsafe(p);
  544. if (ret == ACT_P_DELETED)
  545. module_put(ops->owner);
  546. else if (ret < 0)
  547. break;
  548. n_i++;
  549. }
  550. mutex_unlock(&idrinfo->lock);
  551. if (ret < 0) {
  552. if (n_i)
  553. NL_SET_ERR_MSG(extack, "Unable to flush all TC actions");
  554. else
  555. goto nla_put_failure;
  556. }
  557. ret = nla_put_u32(skb, TCA_FCNT, n_i);
  558. if (ret)
  559. goto nla_put_failure;
  560. nla_nest_end(skb, nest);
  561. return n_i;
  562. nla_put_failure:
  563. nla_nest_cancel(skb, nest);
  564. return ret;
  565. }
  566. int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
  567. struct netlink_callback *cb, int type,
  568. const struct tc_action_ops *ops,
  569. struct netlink_ext_ack *extack)
  570. {
  571. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  572. if (type == RTM_DELACTION) {
  573. return tcf_del_walker(idrinfo, skb, ops, extack);
  574. } else if (type == RTM_GETACTION) {
  575. return tcf_dump_walker(idrinfo, skb, cb);
  576. } else {
  577. WARN(1, "tcf_generic_walker: unknown command %d\n", type);
  578. NL_SET_ERR_MSG(extack, "tcf_generic_walker: unknown command");
  579. return -EINVAL;
  580. }
  581. }
  582. EXPORT_SYMBOL(tcf_generic_walker);
  583. int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
  584. {
  585. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  586. struct tc_action *p;
  587. mutex_lock(&idrinfo->lock);
  588. p = idr_find(&idrinfo->action_idr, index);
  589. if (IS_ERR(p))
  590. p = NULL;
  591. else if (p)
  592. refcount_inc(&p->tcfa_refcnt);
  593. mutex_unlock(&idrinfo->lock);
  594. if (p) {
  595. *a = p;
  596. return true;
  597. }
  598. return false;
  599. }
  600. EXPORT_SYMBOL(tcf_idr_search);
  601. static int __tcf_generic_walker(struct net *net, struct sk_buff *skb,
  602. struct netlink_callback *cb, int type,
  603. const struct tc_action_ops *ops,
  604. struct netlink_ext_ack *extack)
  605. {
  606. struct tc_action_net *tn = net_generic(net, ops->net_id);
  607. if (unlikely(ops->walk))
  608. return ops->walk(net, skb, cb, type, ops, extack);
  609. return tcf_generic_walker(tn, skb, cb, type, ops, extack);
  610. }
  611. static int __tcf_idr_search(struct net *net,
  612. const struct tc_action_ops *ops,
  613. struct tc_action **a, u32 index)
  614. {
  615. struct tc_action_net *tn = net_generic(net, ops->net_id);
  616. if (unlikely(ops->lookup))
  617. return ops->lookup(net, a, index);
  618. return tcf_idr_search(tn, a, index);
  619. }
  620. static int tcf_idr_delete_index(struct tcf_idrinfo *idrinfo, u32 index)
  621. {
  622. struct tc_action *p;
  623. int ret = 0;
  624. mutex_lock(&idrinfo->lock);
  625. p = idr_find(&idrinfo->action_idr, index);
  626. if (!p) {
  627. mutex_unlock(&idrinfo->lock);
  628. return -ENOENT;
  629. }
  630. if (!atomic_read(&p->tcfa_bindcnt)) {
  631. if (refcount_dec_and_test(&p->tcfa_refcnt)) {
  632. struct module *owner = p->ops->owner;
  633. WARN_ON(p != idr_remove(&idrinfo->action_idr,
  634. p->tcfa_index));
  635. mutex_unlock(&idrinfo->lock);
  636. tcf_action_cleanup(p);
  637. module_put(owner);
  638. return 0;
  639. }
  640. ret = 0;
  641. } else {
  642. ret = -EPERM;
  643. }
  644. mutex_unlock(&idrinfo->lock);
  645. return ret;
  646. }
  647. int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
  648. struct tc_action **a, const struct tc_action_ops *ops,
  649. int bind, bool cpustats, u32 flags)
  650. {
  651. struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
  652. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  653. int err = -ENOMEM;
  654. if (unlikely(!p))
  655. return -ENOMEM;
  656. refcount_set(&p->tcfa_refcnt, 1);
  657. if (bind)
  658. atomic_set(&p->tcfa_bindcnt, 1);
  659. if (cpustats) {
  660. p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_sync);
  661. if (!p->cpu_bstats)
  662. goto err1;
  663. p->cpu_bstats_hw = netdev_alloc_pcpu_stats(struct gnet_stats_basic_sync);
  664. if (!p->cpu_bstats_hw)
  665. goto err2;
  666. p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
  667. if (!p->cpu_qstats)
  668. goto err3;
  669. }
  670. gnet_stats_basic_sync_init(&p->tcfa_bstats);
  671. gnet_stats_basic_sync_init(&p->tcfa_bstats_hw);
  672. spin_lock_init(&p->tcfa_lock);
  673. p->tcfa_index = index;
  674. p->tcfa_tm.install = jiffies;
  675. p->tcfa_tm.lastuse = jiffies;
  676. p->tcfa_tm.firstuse = 0;
  677. p->tcfa_flags = flags;
  678. if (est) {
  679. err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
  680. &p->tcfa_rate_est,
  681. &p->tcfa_lock, false, est);
  682. if (err)
  683. goto err4;
  684. }
  685. p->idrinfo = idrinfo;
  686. __module_get(ops->owner);
  687. p->ops = ops;
  688. *a = p;
  689. return 0;
  690. err4:
  691. free_percpu(p->cpu_qstats);
  692. err3:
  693. free_percpu(p->cpu_bstats_hw);
  694. err2:
  695. free_percpu(p->cpu_bstats);
  696. err1:
  697. kfree(p);
  698. return err;
  699. }
  700. EXPORT_SYMBOL(tcf_idr_create);
  701. int tcf_idr_create_from_flags(struct tc_action_net *tn, u32 index,
  702. struct nlattr *est, struct tc_action **a,
  703. const struct tc_action_ops *ops, int bind,
  704. u32 flags)
  705. {
  706. /* Set cpustats according to actions flags. */
  707. return tcf_idr_create(tn, index, est, a, ops, bind,
  708. !(flags & TCA_ACT_FLAGS_NO_PERCPU_STATS), flags);
  709. }
  710. EXPORT_SYMBOL(tcf_idr_create_from_flags);
  711. /* Cleanup idr index that was allocated but not initialized. */
  712. void tcf_idr_cleanup(struct tc_action_net *tn, u32 index)
  713. {
  714. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  715. mutex_lock(&idrinfo->lock);
  716. /* Remove ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
  717. WARN_ON(!IS_ERR(idr_remove(&idrinfo->action_idr, index)));
  718. mutex_unlock(&idrinfo->lock);
  719. }
  720. EXPORT_SYMBOL(tcf_idr_cleanup);
  721. /* Check if action with specified index exists. If actions is found, increments
  722. * its reference and bind counters, and return 1. Otherwise insert temporary
  723. * error pointer (to prevent concurrent users from inserting actions with same
  724. * index) and return 0.
  725. *
  726. * May return -EAGAIN for binding actions in case of a parallel add/delete on
  727. * the requested index.
  728. */
  729. int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index,
  730. struct tc_action **a, int bind)
  731. {
  732. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  733. struct tc_action *p;
  734. int ret;
  735. u32 max;
  736. if (*index) {
  737. rcu_read_lock();
  738. p = idr_find(&idrinfo->action_idr, *index);
  739. if (IS_ERR(p)) {
  740. /* This means that another process allocated
  741. * index but did not assign the pointer yet.
  742. */
  743. rcu_read_unlock();
  744. return -EAGAIN;
  745. }
  746. if (!p) {
  747. /* Empty slot, try to allocate it */
  748. max = *index;
  749. rcu_read_unlock();
  750. goto new;
  751. }
  752. if (!refcount_inc_not_zero(&p->tcfa_refcnt)) {
  753. /* Action was deleted in parallel */
  754. rcu_read_unlock();
  755. return -EAGAIN;
  756. }
  757. if (bind)
  758. atomic_inc(&p->tcfa_bindcnt);
  759. *a = p;
  760. rcu_read_unlock();
  761. return 1;
  762. } else {
  763. /* Find a slot */
  764. *index = 1;
  765. max = UINT_MAX;
  766. }
  767. new:
  768. *a = NULL;
  769. mutex_lock(&idrinfo->lock);
  770. ret = idr_alloc_u32(&idrinfo->action_idr, ERR_PTR(-EBUSY), index, max,
  771. GFP_KERNEL);
  772. mutex_unlock(&idrinfo->lock);
  773. /* N binds raced for action allocation,
  774. * retry for all the ones that failed.
  775. */
  776. if (ret == -ENOSPC && *index == max)
  777. ret = -EAGAIN;
  778. return ret;
  779. }
  780. EXPORT_SYMBOL(tcf_idr_check_alloc);
  781. void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
  782. struct tcf_idrinfo *idrinfo)
  783. {
  784. struct idr *idr = &idrinfo->action_idr;
  785. bool mutex_taken = false;
  786. struct tc_action *p;
  787. unsigned long id = 1;
  788. unsigned long tmp;
  789. int ret;
  790. idr_for_each_entry_ul(idr, p, tmp, id) {
  791. if (IS_ERR(p))
  792. continue;
  793. if (tc_act_in_hw(p) && !mutex_taken) {
  794. rtnl_lock();
  795. mutex_taken = true;
  796. }
  797. ret = __tcf_idr_release(p, false, true);
  798. if (ret == ACT_P_DELETED)
  799. module_put(ops->owner);
  800. else if (ret < 0)
  801. return;
  802. }
  803. if (mutex_taken)
  804. rtnl_unlock();
  805. idr_destroy(&idrinfo->action_idr);
  806. }
  807. EXPORT_SYMBOL(tcf_idrinfo_destroy);
  808. static LIST_HEAD(act_base);
  809. static DEFINE_RWLOCK(act_mod_lock);
  810. /* since act ops id is stored in pernet subsystem list,
  811. * then there is no way to walk through only all the action
  812. * subsystem, so we keep tc action pernet ops id for
  813. * reoffload to walk through.
  814. */
  815. static LIST_HEAD(act_pernet_id_list);
  816. static DEFINE_MUTEX(act_id_mutex);
  817. struct tc_act_pernet_id {
  818. struct list_head list;
  819. unsigned int id;
  820. };
  821. static int tcf_pernet_add_id_list(unsigned int id)
  822. {
  823. struct tc_act_pernet_id *id_ptr;
  824. int ret = 0;
  825. mutex_lock(&act_id_mutex);
  826. list_for_each_entry(id_ptr, &act_pernet_id_list, list) {
  827. if (id_ptr->id == id) {
  828. ret = -EEXIST;
  829. goto err_out;
  830. }
  831. }
  832. id_ptr = kzalloc_obj(*id_ptr);
  833. if (!id_ptr) {
  834. ret = -ENOMEM;
  835. goto err_out;
  836. }
  837. id_ptr->id = id;
  838. list_add_tail(&id_ptr->list, &act_pernet_id_list);
  839. err_out:
  840. mutex_unlock(&act_id_mutex);
  841. return ret;
  842. }
  843. static void tcf_pernet_del_id_list(unsigned int id)
  844. {
  845. struct tc_act_pernet_id *id_ptr;
  846. mutex_lock(&act_id_mutex);
  847. list_for_each_entry(id_ptr, &act_pernet_id_list, list) {
  848. if (id_ptr->id == id) {
  849. list_del(&id_ptr->list);
  850. kfree(id_ptr);
  851. break;
  852. }
  853. }
  854. mutex_unlock(&act_id_mutex);
  855. }
  856. int tcf_register_action(struct tc_action_ops *act,
  857. struct pernet_operations *ops)
  858. {
  859. struct tc_action_ops *a;
  860. int ret;
  861. if (!act->act || !act->dump || !act->init)
  862. return -EINVAL;
  863. /* We have to register pernet ops before making the action ops visible,
  864. * otherwise tcf_action_init_1() could get a partially initialized
  865. * netns.
  866. */
  867. ret = register_pernet_subsys(ops);
  868. if (ret)
  869. return ret;
  870. if (ops->id) {
  871. ret = tcf_pernet_add_id_list(*ops->id);
  872. if (ret)
  873. goto err_id;
  874. }
  875. write_lock(&act_mod_lock);
  876. list_for_each_entry(a, &act_base, head) {
  877. if (act->id == a->id || (strcmp(act->kind, a->kind) == 0)) {
  878. ret = -EEXIST;
  879. goto err_out;
  880. }
  881. }
  882. list_add_tail(&act->head, &act_base);
  883. write_unlock(&act_mod_lock);
  884. return 0;
  885. err_out:
  886. write_unlock(&act_mod_lock);
  887. if (ops->id)
  888. tcf_pernet_del_id_list(*ops->id);
  889. err_id:
  890. unregister_pernet_subsys(ops);
  891. return ret;
  892. }
  893. EXPORT_SYMBOL(tcf_register_action);
  894. int tcf_unregister_action(struct tc_action_ops *act,
  895. struct pernet_operations *ops)
  896. {
  897. struct tc_action_ops *a;
  898. int err = -ENOENT;
  899. write_lock(&act_mod_lock);
  900. list_for_each_entry(a, &act_base, head) {
  901. if (a == act) {
  902. list_del(&act->head);
  903. err = 0;
  904. break;
  905. }
  906. }
  907. write_unlock(&act_mod_lock);
  908. if (!err) {
  909. unregister_pernet_subsys(ops);
  910. if (ops->id)
  911. tcf_pernet_del_id_list(*ops->id);
  912. }
  913. return err;
  914. }
  915. EXPORT_SYMBOL(tcf_unregister_action);
  916. /* lookup by name */
  917. static struct tc_action_ops *tc_lookup_action_n(char *kind)
  918. {
  919. struct tc_action_ops *a, *res = NULL;
  920. if (kind) {
  921. read_lock(&act_mod_lock);
  922. list_for_each_entry(a, &act_base, head) {
  923. if (strcmp(kind, a->kind) == 0) {
  924. if (try_module_get(a->owner))
  925. res = a;
  926. break;
  927. }
  928. }
  929. read_unlock(&act_mod_lock);
  930. }
  931. return res;
  932. }
  933. /* lookup by nlattr */
  934. static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
  935. {
  936. struct tc_action_ops *a, *res = NULL;
  937. if (kind) {
  938. read_lock(&act_mod_lock);
  939. list_for_each_entry(a, &act_base, head) {
  940. if (nla_strcmp(kind, a->kind) == 0) {
  941. if (try_module_get(a->owner))
  942. res = a;
  943. break;
  944. }
  945. }
  946. read_unlock(&act_mod_lock);
  947. }
  948. return res;
  949. }
  950. /*TCA_ACT_MAX_PRIO is 32, there count up to 32 */
  951. #define TCA_ACT_MAX_PRIO_MASK 0x1FF
  952. int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
  953. int nr_actions, struct tcf_result *res)
  954. {
  955. u32 jmp_prgcnt = 0;
  956. u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
  957. int i;
  958. int ret = TC_ACT_OK;
  959. if (skb_skip_tc_classify(skb))
  960. return TC_ACT_OK;
  961. restart_act_graph:
  962. for (i = 0; i < nr_actions; i++) {
  963. const struct tc_action *a = actions[i];
  964. int repeat_ttl;
  965. if (jmp_prgcnt > 0) {
  966. jmp_prgcnt -= 1;
  967. continue;
  968. }
  969. if (tc_act_skip_sw(a->tcfa_flags))
  970. continue;
  971. repeat_ttl = 32;
  972. repeat:
  973. ret = tc_act(skb, a, res);
  974. if (unlikely(ret == TC_ACT_REPEAT)) {
  975. if (--repeat_ttl != 0)
  976. goto repeat;
  977. /* suspicious opcode, stop pipeline */
  978. net_warn_ratelimited("TC_ACT_REPEAT abuse ?\n");
  979. return TC_ACT_OK;
  980. }
  981. if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
  982. jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
  983. if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
  984. /* faulty opcode, stop pipeline */
  985. return TC_ACT_OK;
  986. } else {
  987. jmp_ttl -= 1;
  988. if (jmp_ttl > 0)
  989. goto restart_act_graph;
  990. else /* faulty graph, stop pipeline */
  991. return TC_ACT_OK;
  992. }
  993. } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
  994. if (unlikely(!rcu_access_pointer(a->goto_chain))) {
  995. tcf_set_drop_reason(skb,
  996. SKB_DROP_REASON_TC_CHAIN_NOTFOUND);
  997. return TC_ACT_SHOT;
  998. }
  999. tcf_action_goto_chain_exec(a, res);
  1000. }
  1001. if (ret != TC_ACT_PIPE)
  1002. break;
  1003. }
  1004. return ret;
  1005. }
  1006. EXPORT_SYMBOL(tcf_action_exec);
  1007. int tcf_action_destroy(struct tc_action *actions[], int bind)
  1008. {
  1009. const struct tc_action_ops *ops;
  1010. struct tc_action *a;
  1011. int ret = 0, i;
  1012. tcf_act_for_each_action(i, a, actions) {
  1013. actions[i] = NULL;
  1014. ops = a->ops;
  1015. ret = __tcf_idr_release(a, bind, true);
  1016. if (ret == ACT_P_DELETED)
  1017. module_put(ops->owner);
  1018. else if (ret < 0)
  1019. return ret;
  1020. }
  1021. return ret;
  1022. }
  1023. static int tcf_action_put(struct tc_action *p)
  1024. {
  1025. return __tcf_action_put(p, false);
  1026. }
  1027. static void tcf_action_put_many(struct tc_action *actions[])
  1028. {
  1029. struct tc_action *a;
  1030. int i;
  1031. tcf_act_for_each_action(i, a, actions) {
  1032. const struct tc_action_ops *ops = a->ops;
  1033. if (tcf_action_put(a))
  1034. module_put(ops->owner);
  1035. }
  1036. }
  1037. static void tca_put_bound_many(struct tc_action *actions[], int init_res[])
  1038. {
  1039. struct tc_action *a;
  1040. int i;
  1041. tcf_act_for_each_action(i, a, actions) {
  1042. const struct tc_action_ops *ops = a->ops;
  1043. if (init_res[i] == ACT_P_CREATED)
  1044. continue;
  1045. if (tcf_action_put(a))
  1046. module_put(ops->owner);
  1047. }
  1048. }
  1049. int
  1050. tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  1051. {
  1052. return a->ops->dump(skb, a, bind, ref);
  1053. }
  1054. int tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[],
  1055. int bind, int ref, bool terse)
  1056. {
  1057. struct tc_action *a;
  1058. int err = -EINVAL, i;
  1059. struct nlattr *nest;
  1060. tcf_act_for_each_action(i, a, actions) {
  1061. nest = nla_nest_start_noflag(skb, i + 1);
  1062. if (nest == NULL)
  1063. goto nla_put_failure;
  1064. err = terse ? tcf_action_dump_terse(skb, a, false) :
  1065. tcf_action_dump_1(skb, a, bind, ref);
  1066. if (err < 0)
  1067. goto errout;
  1068. nla_nest_end(skb, nest);
  1069. }
  1070. return 0;
  1071. nla_put_failure:
  1072. err = -EINVAL;
  1073. errout:
  1074. nla_nest_cancel(skb, nest);
  1075. return err;
  1076. }
  1077. static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
  1078. {
  1079. struct tc_cookie *c = kzalloc_obj(*c);
  1080. if (!c)
  1081. return NULL;
  1082. c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
  1083. if (!c->data) {
  1084. kfree(c);
  1085. return NULL;
  1086. }
  1087. c->len = nla_len(tb[TCA_ACT_COOKIE]);
  1088. return c;
  1089. }
  1090. static u8 tcf_action_hw_stats_get(struct nlattr *hw_stats_attr)
  1091. {
  1092. struct nla_bitfield32 hw_stats_bf;
  1093. /* If the user did not pass the attr, that means he does
  1094. * not care about the type. Return "any" in that case
  1095. * which is setting on all supported types.
  1096. */
  1097. if (!hw_stats_attr)
  1098. return TCA_ACT_HW_STATS_ANY;
  1099. hw_stats_bf = nla_get_bitfield32(hw_stats_attr);
  1100. return hw_stats_bf.value;
  1101. }
  1102. static const struct nla_policy tcf_action_policy[TCA_ACT_MAX + 1] = {
  1103. [TCA_ACT_KIND] = { .type = NLA_STRING },
  1104. [TCA_ACT_INDEX] = { .type = NLA_U32 },
  1105. [TCA_ACT_COOKIE] = { .type = NLA_BINARY,
  1106. .len = TC_COOKIE_MAX_SIZE },
  1107. [TCA_ACT_OPTIONS] = { .type = NLA_NESTED },
  1108. [TCA_ACT_FLAGS] = NLA_POLICY_BITFIELD32(TCA_ACT_FLAGS_NO_PERCPU_STATS |
  1109. TCA_ACT_FLAGS_SKIP_HW |
  1110. TCA_ACT_FLAGS_SKIP_SW),
  1111. [TCA_ACT_HW_STATS] = NLA_POLICY_BITFIELD32(TCA_ACT_HW_STATS_ANY),
  1112. };
  1113. void tcf_idr_insert_many(struct tc_action *actions[], int init_res[])
  1114. {
  1115. struct tc_action *a;
  1116. int i;
  1117. tcf_act_for_each_action(i, a, actions) {
  1118. struct tcf_idrinfo *idrinfo;
  1119. if (init_res[i] == ACT_P_BOUND)
  1120. continue;
  1121. idrinfo = a->idrinfo;
  1122. mutex_lock(&idrinfo->lock);
  1123. /* Replace ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
  1124. idr_replace(&idrinfo->action_idr, a, a->tcfa_index);
  1125. mutex_unlock(&idrinfo->lock);
  1126. }
  1127. }
  1128. struct tc_action_ops *tc_action_load_ops(struct nlattr *nla, u32 flags,
  1129. struct netlink_ext_ack *extack)
  1130. {
  1131. bool police = flags & TCA_ACT_FLAGS_POLICE;
  1132. struct nlattr *tb[TCA_ACT_MAX + 1];
  1133. struct tc_action_ops *a_o;
  1134. char act_name[IFNAMSIZ];
  1135. struct nlattr *kind;
  1136. int err;
  1137. if (!police) {
  1138. err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
  1139. tcf_action_policy, extack);
  1140. if (err < 0)
  1141. return ERR_PTR(err);
  1142. err = -EINVAL;
  1143. kind = tb[TCA_ACT_KIND];
  1144. if (!kind) {
  1145. NL_SET_ERR_MSG(extack, "TC action kind must be specified");
  1146. return ERR_PTR(err);
  1147. }
  1148. if (nla_strscpy(act_name, kind, IFNAMSIZ) < 0) {
  1149. NL_SET_ERR_MSG(extack, "TC action name too long");
  1150. return ERR_PTR(err);
  1151. }
  1152. } else {
  1153. if (strscpy(act_name, "police", IFNAMSIZ) < 0) {
  1154. NL_SET_ERR_MSG(extack, "TC action name too long");
  1155. return ERR_PTR(-EINVAL);
  1156. }
  1157. }
  1158. a_o = tc_lookup_action_n(act_name);
  1159. if (a_o == NULL) {
  1160. #ifdef CONFIG_MODULES
  1161. bool rtnl_held = !(flags & TCA_ACT_FLAGS_NO_RTNL);
  1162. if (rtnl_held)
  1163. rtnl_unlock();
  1164. request_module(NET_ACT_ALIAS_PREFIX "%s", act_name);
  1165. if (rtnl_held)
  1166. rtnl_lock();
  1167. a_o = tc_lookup_action_n(act_name);
  1168. /* We dropped the RTNL semaphore in order to
  1169. * perform the module load. So, even if we
  1170. * succeeded in loading the module we have to
  1171. * tell the caller to replay the request. We
  1172. * indicate this using -EAGAIN.
  1173. */
  1174. if (a_o != NULL) {
  1175. module_put(a_o->owner);
  1176. return ERR_PTR(-EAGAIN);
  1177. }
  1178. #endif
  1179. NL_SET_ERR_MSG(extack, "Failed to load TC action module");
  1180. return ERR_PTR(-ENOENT);
  1181. }
  1182. return a_o;
  1183. }
  1184. struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
  1185. struct nlattr *nla, struct nlattr *est,
  1186. struct tc_action_ops *a_o, int *init_res,
  1187. u32 flags, struct netlink_ext_ack *extack)
  1188. {
  1189. bool police = flags & TCA_ACT_FLAGS_POLICE;
  1190. struct nla_bitfield32 userflags = { 0, 0 };
  1191. struct tc_cookie *user_cookie = NULL;
  1192. u8 hw_stats = TCA_ACT_HW_STATS_ANY;
  1193. struct nlattr *tb[TCA_ACT_MAX + 1];
  1194. struct tc_action *a;
  1195. int err;
  1196. /* backward compatibility for policer */
  1197. if (!police) {
  1198. err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
  1199. tcf_action_policy, extack);
  1200. if (err < 0)
  1201. return ERR_PTR(err);
  1202. if (tb[TCA_ACT_COOKIE]) {
  1203. user_cookie = nla_memdup_cookie(tb);
  1204. if (!user_cookie) {
  1205. NL_SET_ERR_MSG(extack, "No memory to generate TC cookie");
  1206. err = -ENOMEM;
  1207. goto err_out;
  1208. }
  1209. }
  1210. hw_stats = tcf_action_hw_stats_get(tb[TCA_ACT_HW_STATS]);
  1211. if (tb[TCA_ACT_FLAGS]) {
  1212. userflags = nla_get_bitfield32(tb[TCA_ACT_FLAGS]);
  1213. if (!tc_act_flags_valid(userflags.value)) {
  1214. err = -EINVAL;
  1215. goto err_out;
  1216. }
  1217. }
  1218. err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, tp,
  1219. userflags.value | flags, extack);
  1220. } else {
  1221. err = a_o->init(net, nla, est, &a, tp, userflags.value | flags,
  1222. extack);
  1223. }
  1224. if (err < 0)
  1225. goto err_out;
  1226. *init_res = err;
  1227. if (!police && tb[TCA_ACT_COOKIE])
  1228. tcf_set_action_cookie(&a->user_cookie, user_cookie);
  1229. if (!police)
  1230. a->hw_stats = hw_stats;
  1231. return a;
  1232. err_out:
  1233. if (user_cookie) {
  1234. kfree(user_cookie->data);
  1235. kfree(user_cookie);
  1236. }
  1237. return ERR_PTR(err);
  1238. }
  1239. static bool tc_act_bind(u32 flags)
  1240. {
  1241. return !!(flags & TCA_ACT_FLAGS_BIND);
  1242. }
  1243. /* Returns numbers of initialized actions or negative error. */
  1244. int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
  1245. struct nlattr *est, struct tc_action *actions[],
  1246. int init_res[], size_t *attr_size,
  1247. u32 flags, u32 fl_flags,
  1248. struct netlink_ext_ack *extack)
  1249. {
  1250. struct tc_action_ops *ops[TCA_ACT_MAX_PRIO] = {};
  1251. struct nlattr *tb[TCA_ACT_MAX_PRIO + 2];
  1252. struct tc_action *act;
  1253. size_t sz = 0;
  1254. int err;
  1255. int i;
  1256. err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO + 1, nla, NULL,
  1257. extack);
  1258. if (err < 0)
  1259. return err;
  1260. /* The nested attributes are parsed as types, but they are really an
  1261. * array of actions. So we parse one more than we can handle, and return
  1262. * an error if the last one is set (as that indicates that the request
  1263. * contained more than the maximum number of actions).
  1264. */
  1265. if (tb[TCA_ACT_MAX_PRIO + 1]) {
  1266. NL_SET_ERR_MSG_FMT(extack,
  1267. "Only %d actions supported per filter",
  1268. TCA_ACT_MAX_PRIO);
  1269. return -EINVAL;
  1270. }
  1271. for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
  1272. struct tc_action_ops *a_o;
  1273. a_o = tc_action_load_ops(tb[i], flags, extack);
  1274. if (IS_ERR(a_o)) {
  1275. err = PTR_ERR(a_o);
  1276. goto err_mod;
  1277. }
  1278. ops[i - 1] = a_o;
  1279. }
  1280. for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
  1281. act = tcf_action_init_1(net, tp, tb[i], est, ops[i - 1],
  1282. &init_res[i - 1], flags, extack);
  1283. if (IS_ERR(act)) {
  1284. err = PTR_ERR(act);
  1285. goto err;
  1286. }
  1287. sz += tcf_action_fill_size(act);
  1288. /* Start from index 0 */
  1289. actions[i - 1] = act;
  1290. if (tc_act_bind(flags)) {
  1291. bool skip_sw = tc_skip_sw(fl_flags);
  1292. bool skip_hw = tc_skip_hw(fl_flags);
  1293. if (tc_act_bind(act->tcfa_flags)) {
  1294. /* Action is created by classifier and is not
  1295. * standalone. Check that the user did not set
  1296. * any action flags different than the
  1297. * classifier flags, and inherit the flags from
  1298. * the classifier for the compatibility case
  1299. * where no flags were specified at all.
  1300. */
  1301. if ((tc_act_skip_sw(act->tcfa_flags) && !skip_sw) ||
  1302. (tc_act_skip_hw(act->tcfa_flags) && !skip_hw)) {
  1303. NL_SET_ERR_MSG(extack,
  1304. "Mismatch between action and filter offload flags");
  1305. err = -EINVAL;
  1306. goto err;
  1307. }
  1308. if (skip_sw)
  1309. act->tcfa_flags |= TCA_ACT_FLAGS_SKIP_SW;
  1310. if (skip_hw)
  1311. act->tcfa_flags |= TCA_ACT_FLAGS_SKIP_HW;
  1312. continue;
  1313. }
  1314. /* Action is standalone */
  1315. if (skip_sw != tc_act_skip_sw(act->tcfa_flags) ||
  1316. skip_hw != tc_act_skip_hw(act->tcfa_flags)) {
  1317. NL_SET_ERR_MSG(extack,
  1318. "Mismatch between action and filter offload flags");
  1319. err = -EINVAL;
  1320. goto err;
  1321. }
  1322. } else {
  1323. err = tcf_action_offload_add(act, extack);
  1324. if (tc_act_skip_sw(act->tcfa_flags) && err)
  1325. goto err;
  1326. }
  1327. }
  1328. /* We have to commit them all together, because if any error happened in
  1329. * between, we could not handle the failure gracefully.
  1330. */
  1331. tcf_idr_insert_many(actions, init_res);
  1332. *attr_size = tcf_action_full_attrs_size(sz);
  1333. err = i - 1;
  1334. goto err_mod;
  1335. err:
  1336. tcf_action_destroy(actions, flags & TCA_ACT_FLAGS_BIND);
  1337. err_mod:
  1338. for (i = 0; i < TCA_ACT_MAX_PRIO && ops[i]; i++)
  1339. module_put(ops[i]->owner);
  1340. return err;
  1341. }
  1342. void tcf_action_update_stats(struct tc_action *a, u64 bytes, u64 packets,
  1343. u64 drops, bool hw)
  1344. {
  1345. if (a->cpu_bstats) {
  1346. _bstats_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
  1347. this_cpu_ptr(a->cpu_qstats)->drops += drops;
  1348. if (hw)
  1349. _bstats_update(this_cpu_ptr(a->cpu_bstats_hw),
  1350. bytes, packets);
  1351. return;
  1352. }
  1353. _bstats_update(&a->tcfa_bstats, bytes, packets);
  1354. atomic_add(drops, &a->tcfa_drops);
  1355. if (hw)
  1356. _bstats_update(&a->tcfa_bstats_hw, bytes, packets);
  1357. }
  1358. EXPORT_SYMBOL(tcf_action_update_stats);
  1359. int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
  1360. int compat_mode)
  1361. {
  1362. struct gnet_stats_queue qstats = {0};
  1363. struct gnet_dump d;
  1364. int err = 0;
  1365. if (p == NULL)
  1366. goto errout;
  1367. /* compat_mode being true specifies a call that is supposed
  1368. * to add additional backward compatibility statistic TLVs.
  1369. */
  1370. if (compat_mode) {
  1371. if (p->type == TCA_OLD_COMPAT)
  1372. err = gnet_stats_start_copy_compat(skb, 0,
  1373. TCA_STATS,
  1374. TCA_XSTATS,
  1375. &p->tcfa_lock, &d,
  1376. TCA_PAD);
  1377. else
  1378. return 0;
  1379. } else
  1380. err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
  1381. &p->tcfa_lock, &d, TCA_ACT_PAD);
  1382. if (err < 0)
  1383. goto errout;
  1384. qstats.drops = atomic_read(&p->tcfa_drops);
  1385. qstats.overlimits = atomic_read(&p->tcfa_overlimits);
  1386. if (gnet_stats_copy_basic(&d, p->cpu_bstats,
  1387. &p->tcfa_bstats, false) < 0 ||
  1388. gnet_stats_copy_basic_hw(&d, p->cpu_bstats_hw,
  1389. &p->tcfa_bstats_hw, false) < 0 ||
  1390. gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
  1391. gnet_stats_copy_queue(&d, p->cpu_qstats,
  1392. &qstats,
  1393. qstats.qlen) < 0)
  1394. goto errout;
  1395. if (gnet_stats_finish_copy(&d) < 0)
  1396. goto errout;
  1397. return 0;
  1398. errout:
  1399. return -1;
  1400. }
  1401. static int tca_get_fill(struct sk_buff *skb, struct tc_action *actions[],
  1402. u32 portid, u32 seq, u16 flags, int event, int bind,
  1403. int ref, struct netlink_ext_ack *extack)
  1404. {
  1405. struct tcamsg *t;
  1406. struct nlmsghdr *nlh;
  1407. unsigned char *b = skb_tail_pointer(skb);
  1408. struct nlattr *nest;
  1409. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
  1410. if (!nlh)
  1411. goto out_nlmsg_trim;
  1412. t = nlmsg_data(nlh);
  1413. t->tca_family = AF_UNSPEC;
  1414. t->tca__pad1 = 0;
  1415. t->tca__pad2 = 0;
  1416. if (extack && extack->_msg &&
  1417. nla_put_string(skb, TCA_ROOT_EXT_WARN_MSG, extack->_msg))
  1418. goto out_nlmsg_trim;
  1419. nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
  1420. if (!nest)
  1421. goto out_nlmsg_trim;
  1422. if (tcf_action_dump(skb, actions, bind, ref, false) < 0)
  1423. goto out_nlmsg_trim;
  1424. nla_nest_end(skb, nest);
  1425. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  1426. return skb->len;
  1427. out_nlmsg_trim:
  1428. nlmsg_trim(skb, b);
  1429. return -1;
  1430. }
  1431. static int
  1432. tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
  1433. struct tc_action *actions[], int event,
  1434. struct netlink_ext_ack *extack)
  1435. {
  1436. struct sk_buff *skb;
  1437. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  1438. if (!skb)
  1439. return -ENOBUFS;
  1440. if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
  1441. 0, 1, NULL) <= 0) {
  1442. NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
  1443. kfree_skb(skb);
  1444. return -EINVAL;
  1445. }
  1446. return rtnl_unicast(skb, net, portid);
  1447. }
  1448. static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
  1449. struct nlmsghdr *n, u32 portid,
  1450. struct netlink_ext_ack *extack)
  1451. {
  1452. struct nlattr *tb[TCA_ACT_MAX + 1];
  1453. const struct tc_action_ops *ops;
  1454. struct tc_action *a;
  1455. int index;
  1456. int err;
  1457. err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
  1458. tcf_action_policy, extack);
  1459. if (err < 0)
  1460. goto err_out;
  1461. err = -EINVAL;
  1462. if (tb[TCA_ACT_INDEX] == NULL ||
  1463. nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) {
  1464. NL_SET_ERR_MSG(extack, "Invalid TC action index value");
  1465. goto err_out;
  1466. }
  1467. index = nla_get_u32(tb[TCA_ACT_INDEX]);
  1468. err = -EINVAL;
  1469. ops = tc_lookup_action(tb[TCA_ACT_KIND]);
  1470. if (!ops) { /* could happen in batch of actions */
  1471. NL_SET_ERR_MSG(extack, "Specified TC action kind not found");
  1472. goto err_out;
  1473. }
  1474. err = -ENOENT;
  1475. if (__tcf_idr_search(net, ops, &a, index) == 0) {
  1476. NL_SET_ERR_MSG(extack, "TC action with specified index not found");
  1477. goto err_mod;
  1478. }
  1479. module_put(ops->owner);
  1480. return a;
  1481. err_mod:
  1482. module_put(ops->owner);
  1483. err_out:
  1484. return ERR_PTR(err);
  1485. }
  1486. static int tca_action_flush(struct net *net, struct nlattr *nla,
  1487. struct nlmsghdr *n, u32 portid,
  1488. struct netlink_ext_ack *extack)
  1489. {
  1490. struct sk_buff *skb;
  1491. unsigned char *b;
  1492. struct nlmsghdr *nlh;
  1493. struct tcamsg *t;
  1494. struct netlink_callback dcb;
  1495. struct nlattr *nest;
  1496. struct nlattr *tb[TCA_ACT_MAX + 1];
  1497. const struct tc_action_ops *ops;
  1498. struct nlattr *kind;
  1499. int err = -ENOMEM;
  1500. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  1501. if (!skb)
  1502. return err;
  1503. b = skb_tail_pointer(skb);
  1504. err = nla_parse_nested_deprecated(tb, TCA_ACT_MAX, nla,
  1505. tcf_action_policy, extack);
  1506. if (err < 0)
  1507. goto err_out;
  1508. err = -EINVAL;
  1509. kind = tb[TCA_ACT_KIND];
  1510. ops = tc_lookup_action(kind);
  1511. if (!ops) { /*some idjot trying to flush unknown action */
  1512. NL_SET_ERR_MSG(extack, "Cannot flush unknown TC action");
  1513. goto err_out;
  1514. }
  1515. nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
  1516. sizeof(*t), 0);
  1517. if (!nlh) {
  1518. NL_SET_ERR_MSG(extack, "Failed to create TC action flush notification");
  1519. goto out_module_put;
  1520. }
  1521. t = nlmsg_data(nlh);
  1522. t->tca_family = AF_UNSPEC;
  1523. t->tca__pad1 = 0;
  1524. t->tca__pad2 = 0;
  1525. nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
  1526. if (!nest) {
  1527. NL_SET_ERR_MSG(extack, "Failed to add new netlink message");
  1528. goto out_module_put;
  1529. }
  1530. err = __tcf_generic_walker(net, skb, &dcb, RTM_DELACTION, ops, extack);
  1531. if (err <= 0) {
  1532. nla_nest_cancel(skb, nest);
  1533. goto out_module_put;
  1534. }
  1535. nla_nest_end(skb, nest);
  1536. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  1537. nlh->nlmsg_flags |= NLM_F_ROOT;
  1538. module_put(ops->owner);
  1539. err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  1540. n->nlmsg_flags & NLM_F_ECHO);
  1541. if (err < 0)
  1542. NL_SET_ERR_MSG(extack, "Failed to send TC action flush notification");
  1543. return err;
  1544. out_module_put:
  1545. module_put(ops->owner);
  1546. err_out:
  1547. kfree_skb(skb);
  1548. return err;
  1549. }
  1550. static int tcf_action_delete(struct net *net, struct tc_action *actions[])
  1551. {
  1552. struct tc_action *a;
  1553. int i;
  1554. tcf_act_for_each_action(i, a, actions) {
  1555. const struct tc_action_ops *ops = a->ops;
  1556. /* Actions can be deleted concurrently so we must save their
  1557. * type and id to search again after reference is released.
  1558. */
  1559. struct tcf_idrinfo *idrinfo = a->idrinfo;
  1560. u32 act_index = a->tcfa_index;
  1561. actions[i] = NULL;
  1562. if (tcf_action_put(a)) {
  1563. /* last reference, action was deleted concurrently */
  1564. module_put(ops->owner);
  1565. } else {
  1566. int ret;
  1567. /* now do the delete */
  1568. ret = tcf_idr_delete_index(idrinfo, act_index);
  1569. if (ret < 0)
  1570. return ret;
  1571. }
  1572. }
  1573. return 0;
  1574. }
  1575. static struct sk_buff *tcf_reoffload_del_notify_msg(struct net *net,
  1576. struct tc_action *action)
  1577. {
  1578. size_t attr_size = tcf_action_fill_size(action);
  1579. struct tc_action *actions[TCA_ACT_MAX_PRIO] = {
  1580. [0] = action,
  1581. };
  1582. struct sk_buff *skb;
  1583. skb = alloc_skb(max(attr_size, NLMSG_GOODSIZE), GFP_KERNEL);
  1584. if (!skb)
  1585. return ERR_PTR(-ENOBUFS);
  1586. if (tca_get_fill(skb, actions, 0, 0, 0, RTM_DELACTION, 0, 1, NULL) <= 0) {
  1587. kfree_skb(skb);
  1588. return ERR_PTR(-EINVAL);
  1589. }
  1590. return skb;
  1591. }
  1592. static int tcf_reoffload_del_notify(struct net *net, struct tc_action *action)
  1593. {
  1594. const struct tc_action_ops *ops = action->ops;
  1595. struct sk_buff *skb;
  1596. int ret;
  1597. if (!rtnl_notify_needed(net, 0, RTNLGRP_TC)) {
  1598. skb = NULL;
  1599. } else {
  1600. skb = tcf_reoffload_del_notify_msg(net, action);
  1601. if (IS_ERR(skb))
  1602. return PTR_ERR(skb);
  1603. }
  1604. ret = tcf_idr_release_unsafe(action);
  1605. if (ret == ACT_P_DELETED) {
  1606. module_put(ops->owner);
  1607. ret = rtnetlink_maybe_send(skb, net, 0, RTNLGRP_TC, 0);
  1608. } else {
  1609. kfree_skb(skb);
  1610. }
  1611. return ret;
  1612. }
  1613. int tcf_action_reoffload_cb(flow_indr_block_bind_cb_t *cb,
  1614. void *cb_priv, bool add)
  1615. {
  1616. struct tc_act_pernet_id *id_ptr;
  1617. struct tcf_idrinfo *idrinfo;
  1618. struct tc_action_net *tn;
  1619. struct tc_action *p;
  1620. unsigned int act_id;
  1621. unsigned long tmp;
  1622. unsigned long id;
  1623. struct idr *idr;
  1624. struct net *net;
  1625. int ret;
  1626. if (!cb)
  1627. return -EINVAL;
  1628. down_read(&net_rwsem);
  1629. mutex_lock(&act_id_mutex);
  1630. for_each_net(net) {
  1631. list_for_each_entry(id_ptr, &act_pernet_id_list, list) {
  1632. act_id = id_ptr->id;
  1633. tn = net_generic(net, act_id);
  1634. if (!tn)
  1635. continue;
  1636. idrinfo = tn->idrinfo;
  1637. if (!idrinfo)
  1638. continue;
  1639. mutex_lock(&idrinfo->lock);
  1640. idr = &idrinfo->action_idr;
  1641. idr_for_each_entry_ul(idr, p, tmp, id) {
  1642. if (IS_ERR(p) || tc_act_bind(p->tcfa_flags))
  1643. continue;
  1644. if (add) {
  1645. tcf_action_offload_add_ex(p, NULL, cb,
  1646. cb_priv);
  1647. continue;
  1648. }
  1649. /* cb unregister to update hw count */
  1650. ret = tcf_action_offload_del_ex(p, cb, cb_priv);
  1651. if (ret < 0)
  1652. continue;
  1653. if (tc_act_skip_sw(p->tcfa_flags) &&
  1654. !tc_act_in_hw(p))
  1655. tcf_reoffload_del_notify(net, p);
  1656. }
  1657. mutex_unlock(&idrinfo->lock);
  1658. }
  1659. }
  1660. mutex_unlock(&act_id_mutex);
  1661. up_read(&net_rwsem);
  1662. return 0;
  1663. }
  1664. static struct sk_buff *tcf_del_notify_msg(struct net *net, struct nlmsghdr *n,
  1665. struct tc_action *actions[],
  1666. u32 portid, size_t attr_size,
  1667. struct netlink_ext_ack *extack)
  1668. {
  1669. struct sk_buff *skb;
  1670. skb = alloc_skb(max(attr_size, NLMSG_GOODSIZE), GFP_KERNEL);
  1671. if (!skb)
  1672. return ERR_PTR(-ENOBUFS);
  1673. if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
  1674. 0, 2, extack) <= 0) {
  1675. NL_SET_ERR_MSG(extack, "Failed to fill netlink TC action attributes");
  1676. kfree_skb(skb);
  1677. return ERR_PTR(-EINVAL);
  1678. }
  1679. return skb;
  1680. }
  1681. static int tcf_del_notify(struct net *net, struct nlmsghdr *n,
  1682. struct tc_action *actions[], u32 portid,
  1683. size_t attr_size, struct netlink_ext_ack *extack)
  1684. {
  1685. struct sk_buff *skb;
  1686. int ret;
  1687. if (!rtnl_notify_needed(net, n->nlmsg_flags, RTNLGRP_TC)) {
  1688. skb = NULL;
  1689. } else {
  1690. skb = tcf_del_notify_msg(net, n, actions, portid, attr_size,
  1691. extack);
  1692. if (IS_ERR(skb))
  1693. return PTR_ERR(skb);
  1694. }
  1695. /* now do the delete */
  1696. ret = tcf_action_delete(net, actions);
  1697. if (ret < 0) {
  1698. NL_SET_ERR_MSG(extack, "Failed to delete TC action");
  1699. kfree_skb(skb);
  1700. return ret;
  1701. }
  1702. return rtnetlink_maybe_send(skb, net, portid, RTNLGRP_TC,
  1703. n->nlmsg_flags & NLM_F_ECHO);
  1704. }
  1705. static int
  1706. tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
  1707. u32 portid, int event, struct netlink_ext_ack *extack)
  1708. {
  1709. int i, ret;
  1710. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  1711. struct tc_action *act;
  1712. size_t attr_size = 0;
  1713. struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
  1714. ret = nla_parse_nested_deprecated(tb, TCA_ACT_MAX_PRIO, nla, NULL,
  1715. extack);
  1716. if (ret < 0)
  1717. return ret;
  1718. if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
  1719. if (tb[1])
  1720. return tca_action_flush(net, tb[1], n, portid, extack);
  1721. NL_SET_ERR_MSG(extack, "Invalid netlink attributes while flushing TC action");
  1722. return -EINVAL;
  1723. }
  1724. for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
  1725. act = tcf_action_get_1(net, tb[i], n, portid, extack);
  1726. if (IS_ERR(act)) {
  1727. ret = PTR_ERR(act);
  1728. goto err;
  1729. }
  1730. attr_size += tcf_action_fill_size(act);
  1731. actions[i - 1] = act;
  1732. }
  1733. attr_size = tcf_action_full_attrs_size(attr_size);
  1734. if (event == RTM_GETACTION)
  1735. ret = tcf_get_notify(net, portid, n, actions, event, extack);
  1736. else { /* delete */
  1737. ret = tcf_del_notify(net, n, actions, portid, attr_size, extack);
  1738. if (ret)
  1739. goto err;
  1740. return 0;
  1741. }
  1742. err:
  1743. tcf_action_put_many(actions);
  1744. return ret;
  1745. }
  1746. static struct sk_buff *tcf_add_notify_msg(struct net *net, struct nlmsghdr *n,
  1747. struct tc_action *actions[],
  1748. u32 portid, size_t attr_size,
  1749. struct netlink_ext_ack *extack)
  1750. {
  1751. struct sk_buff *skb;
  1752. skb = alloc_skb(max(attr_size, NLMSG_GOODSIZE), GFP_KERNEL);
  1753. if (!skb)
  1754. return ERR_PTR(-ENOBUFS);
  1755. if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
  1756. RTM_NEWACTION, 0, 0, extack) <= 0) {
  1757. NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
  1758. kfree_skb(skb);
  1759. return ERR_PTR(-EINVAL);
  1760. }
  1761. return skb;
  1762. }
  1763. static int tcf_add_notify(struct net *net, struct nlmsghdr *n,
  1764. struct tc_action *actions[], u32 portid,
  1765. size_t attr_size, struct netlink_ext_ack *extack)
  1766. {
  1767. struct sk_buff *skb;
  1768. if (!rtnl_notify_needed(net, n->nlmsg_flags, RTNLGRP_TC)) {
  1769. skb = NULL;
  1770. } else {
  1771. skb = tcf_add_notify_msg(net, n, actions, portid, attr_size,
  1772. extack);
  1773. if (IS_ERR(skb))
  1774. return PTR_ERR(skb);
  1775. }
  1776. return rtnetlink_maybe_send(skb, net, portid, RTNLGRP_TC,
  1777. n->nlmsg_flags & NLM_F_ECHO);
  1778. }
  1779. static int tcf_action_add(struct net *net, struct nlattr *nla,
  1780. struct nlmsghdr *n, u32 portid, u32 flags,
  1781. struct netlink_ext_ack *extack)
  1782. {
  1783. size_t attr_size = 0;
  1784. int loop, ret;
  1785. struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
  1786. int init_res[TCA_ACT_MAX_PRIO] = {};
  1787. for (loop = 0; loop < 10; loop++) {
  1788. ret = tcf_action_init(net, NULL, nla, NULL, actions, init_res,
  1789. &attr_size, flags, 0, extack);
  1790. if (ret != -EAGAIN)
  1791. break;
  1792. }
  1793. if (ret < 0)
  1794. return ret;
  1795. ret = tcf_add_notify(net, n, actions, portid, attr_size, extack);
  1796. /* only put bound actions */
  1797. tca_put_bound_many(actions, init_res);
  1798. return ret;
  1799. }
  1800. static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = {
  1801. [TCA_ROOT_FLAGS] = NLA_POLICY_BITFIELD32(TCA_ACT_FLAG_LARGE_DUMP_ON |
  1802. TCA_ACT_FLAG_TERSE_DUMP),
  1803. [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 },
  1804. };
  1805. static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
  1806. struct netlink_ext_ack *extack)
  1807. {
  1808. struct net *net = sock_net(skb->sk);
  1809. struct nlattr *tca[TCA_ROOT_MAX + 1];
  1810. u32 portid = NETLINK_CB(skb).portid;
  1811. u32 flags = 0;
  1812. int ret = 0;
  1813. if ((n->nlmsg_type != RTM_GETACTION) &&
  1814. !netlink_capable(skb, CAP_NET_ADMIN))
  1815. return -EPERM;
  1816. ret = nlmsg_parse_deprecated(n, sizeof(struct tcamsg), tca,
  1817. TCA_ROOT_MAX, NULL, extack);
  1818. if (ret < 0)
  1819. return ret;
  1820. if (tca[TCA_ACT_TAB] == NULL) {
  1821. NL_SET_ERR_MSG(extack, "Netlink action attributes missing");
  1822. return -EINVAL;
  1823. }
  1824. /* n->nlmsg_flags & NLM_F_CREATE */
  1825. switch (n->nlmsg_type) {
  1826. case RTM_NEWACTION:
  1827. /* we are going to assume all other flags
  1828. * imply create only if it doesn't exist
  1829. * Note that CREATE | EXCL implies that
  1830. * but since we want avoid ambiguity (eg when flags
  1831. * is zero) then just set this
  1832. */
  1833. if (n->nlmsg_flags & NLM_F_REPLACE)
  1834. flags = TCA_ACT_FLAGS_REPLACE;
  1835. ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, flags,
  1836. extack);
  1837. break;
  1838. case RTM_DELACTION:
  1839. ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
  1840. portid, RTM_DELACTION, extack);
  1841. break;
  1842. case RTM_GETACTION:
  1843. ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
  1844. portid, RTM_GETACTION, extack);
  1845. break;
  1846. default:
  1847. BUG();
  1848. }
  1849. return ret;
  1850. }
  1851. static struct nlattr *find_dump_kind(struct nlattr **nla)
  1852. {
  1853. struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
  1854. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  1855. struct nlattr *kind;
  1856. tb1 = nla[TCA_ACT_TAB];
  1857. if (tb1 == NULL)
  1858. return NULL;
  1859. if (nla_parse_deprecated(tb, TCA_ACT_MAX_PRIO, nla_data(tb1), NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0)
  1860. return NULL;
  1861. if (tb[1] == NULL)
  1862. return NULL;
  1863. if (nla_parse_nested_deprecated(tb2, TCA_ACT_MAX, tb[1], tcf_action_policy, NULL) < 0)
  1864. return NULL;
  1865. kind = tb2[TCA_ACT_KIND];
  1866. return kind;
  1867. }
  1868. static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
  1869. {
  1870. struct net *net = sock_net(skb->sk);
  1871. struct nlmsghdr *nlh;
  1872. unsigned char *b = skb_tail_pointer(skb);
  1873. struct nlattr *nest;
  1874. struct tc_action_ops *a_o;
  1875. int ret = 0;
  1876. struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
  1877. struct nlattr *tb[TCA_ROOT_MAX + 1];
  1878. struct nlattr *count_attr = NULL;
  1879. unsigned long jiffy_since = 0;
  1880. struct nlattr *kind = NULL;
  1881. struct nla_bitfield32 bf;
  1882. u32 msecs_since = 0;
  1883. u32 act_count = 0;
  1884. ret = nlmsg_parse_deprecated(cb->nlh, sizeof(struct tcamsg), tb,
  1885. TCA_ROOT_MAX, tcaa_policy, cb->extack);
  1886. if (ret < 0)
  1887. return ret;
  1888. kind = find_dump_kind(tb);
  1889. if (kind == NULL) {
  1890. pr_info("tc_dump_action: action bad kind\n");
  1891. return 0;
  1892. }
  1893. a_o = tc_lookup_action(kind);
  1894. if (a_o == NULL)
  1895. return 0;
  1896. cb->args[2] = 0;
  1897. if (tb[TCA_ROOT_FLAGS]) {
  1898. bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]);
  1899. cb->args[2] = bf.value;
  1900. }
  1901. if (tb[TCA_ROOT_TIME_DELTA]) {
  1902. msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]);
  1903. }
  1904. nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  1905. cb->nlh->nlmsg_type, sizeof(*t), 0);
  1906. if (!nlh)
  1907. goto out_module_put;
  1908. if (msecs_since)
  1909. jiffy_since = jiffies - msecs_to_jiffies(msecs_since);
  1910. t = nlmsg_data(nlh);
  1911. t->tca_family = AF_UNSPEC;
  1912. t->tca__pad1 = 0;
  1913. t->tca__pad2 = 0;
  1914. cb->args[3] = jiffy_since;
  1915. count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32));
  1916. if (!count_attr)
  1917. goto out_module_put;
  1918. nest = nla_nest_start_noflag(skb, TCA_ACT_TAB);
  1919. if (nest == NULL)
  1920. goto out_module_put;
  1921. ret = __tcf_generic_walker(net, skb, cb, RTM_GETACTION, a_o, NULL);
  1922. if (ret < 0)
  1923. goto out_module_put;
  1924. if (ret > 0) {
  1925. nla_nest_end(skb, nest);
  1926. ret = skb->len;
  1927. act_count = cb->args[1];
  1928. memcpy(nla_data(count_attr), &act_count, sizeof(u32));
  1929. cb->args[1] = 0;
  1930. } else
  1931. nlmsg_trim(skb, b);
  1932. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  1933. if (NETLINK_CB(cb->skb).portid && ret)
  1934. nlh->nlmsg_flags |= NLM_F_MULTI;
  1935. module_put(a_o->owner);
  1936. return skb->len;
  1937. out_module_put:
  1938. module_put(a_o->owner);
  1939. nlmsg_trim(skb, b);
  1940. return skb->len;
  1941. }
  1942. static const struct rtnl_msg_handler tc_action_rtnl_msg_handlers[] __initconst = {
  1943. {.msgtype = RTM_NEWACTION, .doit = tc_ctl_action},
  1944. {.msgtype = RTM_DELACTION, .doit = tc_ctl_action},
  1945. {.msgtype = RTM_GETACTION, .doit = tc_ctl_action,
  1946. .dumpit = tc_dump_action},
  1947. };
  1948. static int __init tc_action_init(void)
  1949. {
  1950. rtnl_register_many(tc_action_rtnl_msg_handlers);
  1951. return 0;
  1952. }
  1953. subsys_initcall(tc_action_init);