act_api.c 51 KB

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