act_api.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740
  1. /*
  2. * net/sched/act_api.c Packet action API.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Author: Jamal Hadi Salim
  10. *
  11. *
  12. */
  13. #include <linux/types.h>
  14. #include <linux/kernel.h>
  15. #include <linux/string.h>
  16. #include <linux/errno.h>
  17. #include <linux/slab.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/init.h>
  20. #include <linux/kmod.h>
  21. #include <linux/err.h>
  22. #include <linux/module.h>
  23. #include <linux/rhashtable.h>
  24. #include <linux/list.h>
  25. #include <net/net_namespace.h>
  26. #include <net/sock.h>
  27. #include <net/sch_generic.h>
  28. #include <net/pkt_cls.h>
  29. #include <net/act_api.h>
  30. #include <net/netlink.h>
  31. static int tcf_action_goto_chain_init(struct tc_action *a, struct tcf_proto *tp)
  32. {
  33. u32 chain_index = a->tcfa_action & TC_ACT_EXT_VAL_MASK;
  34. if (!tp)
  35. return -EINVAL;
  36. a->goto_chain = tcf_chain_get_by_act(tp->chain->block, chain_index);
  37. if (!a->goto_chain)
  38. return -ENOMEM;
  39. return 0;
  40. }
  41. static void tcf_action_goto_chain_fini(struct tc_action *a)
  42. {
  43. tcf_chain_put_by_act(a->goto_chain);
  44. }
  45. static void tcf_action_goto_chain_exec(const struct tc_action *a,
  46. struct tcf_result *res)
  47. {
  48. const struct tcf_chain *chain = a->goto_chain;
  49. res->goto_tp = rcu_dereference_bh(chain->filter_chain);
  50. }
  51. static void tcf_free_cookie_rcu(struct rcu_head *p)
  52. {
  53. struct tc_cookie *cookie = container_of(p, struct tc_cookie, rcu);
  54. kfree(cookie->data);
  55. kfree(cookie);
  56. }
  57. static void tcf_set_action_cookie(struct tc_cookie __rcu **old_cookie,
  58. struct tc_cookie *new_cookie)
  59. {
  60. struct tc_cookie *old;
  61. old = xchg((__force struct tc_cookie **)old_cookie, new_cookie);
  62. if (old)
  63. call_rcu(&old->rcu, tcf_free_cookie_rcu);
  64. }
  65. /* XXX: For standalone actions, we don't need a RCU grace period either, because
  66. * actions are always connected to filters and filters are already destroyed in
  67. * RCU callbacks, so after a RCU grace period actions are already disconnected
  68. * from filters. Readers later can not find us.
  69. */
  70. static void free_tcf(struct tc_action *p)
  71. {
  72. free_percpu(p->cpu_bstats);
  73. free_percpu(p->cpu_qstats);
  74. tcf_set_action_cookie(&p->act_cookie, NULL);
  75. if (p->goto_chain)
  76. tcf_action_goto_chain_fini(p);
  77. kfree(p);
  78. }
  79. static void tcf_action_cleanup(struct tc_action *p)
  80. {
  81. if (p->ops->cleanup)
  82. p->ops->cleanup(p);
  83. gen_kill_estimator(&p->tcfa_rate_est);
  84. free_tcf(p);
  85. }
  86. static int __tcf_action_put(struct tc_action *p, bool bind)
  87. {
  88. struct tcf_idrinfo *idrinfo = p->idrinfo;
  89. if (refcount_dec_and_lock(&p->tcfa_refcnt, &idrinfo->lock)) {
  90. if (bind)
  91. atomic_dec(&p->tcfa_bindcnt);
  92. idr_remove(&idrinfo->action_idr, p->tcfa_index);
  93. spin_unlock(&idrinfo->lock);
  94. tcf_action_cleanup(p);
  95. return 1;
  96. }
  97. if (bind)
  98. atomic_dec(&p->tcfa_bindcnt);
  99. return 0;
  100. }
  101. int __tcf_idr_release(struct tc_action *p, bool bind, bool strict)
  102. {
  103. int ret = 0;
  104. /* Release with strict==1 and bind==0 is only called through act API
  105. * interface (classifiers always bind). Only case when action with
  106. * positive reference count and zero bind count can exist is when it was
  107. * also created with act API (unbinding last classifier will destroy the
  108. * action if it was created by classifier). So only case when bind count
  109. * can be changed after initial check is when unbound action is
  110. * destroyed by act API while classifier binds to action with same id
  111. * concurrently. This result either creation of new action(same behavior
  112. * as before), or reusing existing action if concurrent process
  113. * increments reference count before action is deleted. Both scenarios
  114. * are acceptable.
  115. */
  116. if (p) {
  117. if (!bind && strict && atomic_read(&p->tcfa_bindcnt) > 0)
  118. return -EPERM;
  119. if (__tcf_action_put(p, bind))
  120. ret = ACT_P_DELETED;
  121. }
  122. return ret;
  123. }
  124. EXPORT_SYMBOL(__tcf_idr_release);
  125. static size_t tcf_action_shared_attrs_size(const struct tc_action *act)
  126. {
  127. struct tc_cookie *act_cookie;
  128. u32 cookie_len = 0;
  129. rcu_read_lock();
  130. act_cookie = rcu_dereference(act->act_cookie);
  131. if (act_cookie)
  132. cookie_len = nla_total_size(act_cookie->len);
  133. rcu_read_unlock();
  134. return nla_total_size(0) /* action number nested */
  135. + nla_total_size(IFNAMSIZ) /* TCA_ACT_KIND */
  136. + cookie_len /* TCA_ACT_COOKIE */
  137. + nla_total_size(0) /* TCA_ACT_STATS nested */
  138. /* TCA_STATS_BASIC */
  139. + nla_total_size_64bit(sizeof(struct gnet_stats_basic))
  140. /* TCA_STATS_QUEUE */
  141. + nla_total_size_64bit(sizeof(struct gnet_stats_queue))
  142. + nla_total_size(0) /* TCA_OPTIONS nested */
  143. + nla_total_size(sizeof(struct tcf_t)); /* TCA_GACT_TM */
  144. }
  145. static size_t tcf_action_full_attrs_size(size_t sz)
  146. {
  147. return NLMSG_HDRLEN /* struct nlmsghdr */
  148. + sizeof(struct tcamsg)
  149. + nla_total_size(0) /* TCA_ACT_TAB nested */
  150. + sz;
  151. }
  152. static size_t tcf_action_fill_size(const struct tc_action *act)
  153. {
  154. size_t sz = tcf_action_shared_attrs_size(act);
  155. if (act->ops->get_fill_size)
  156. return act->ops->get_fill_size(act) + sz;
  157. return sz;
  158. }
  159. static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
  160. struct netlink_callback *cb)
  161. {
  162. int err = 0, index = -1, s_i = 0, n_i = 0;
  163. u32 act_flags = cb->args[2];
  164. unsigned long jiffy_since = cb->args[3];
  165. struct nlattr *nest;
  166. struct idr *idr = &idrinfo->action_idr;
  167. struct tc_action *p;
  168. unsigned long id = 1;
  169. spin_lock(&idrinfo->lock);
  170. s_i = cb->args[0];
  171. idr_for_each_entry_ul(idr, p, id) {
  172. index++;
  173. if (index < s_i)
  174. continue;
  175. if (jiffy_since &&
  176. time_after(jiffy_since,
  177. (unsigned long)p->tcfa_tm.lastuse))
  178. continue;
  179. nest = nla_nest_start(skb, n_i);
  180. if (!nest) {
  181. index--;
  182. goto nla_put_failure;
  183. }
  184. err = tcf_action_dump_1(skb, p, 0, 0);
  185. if (err < 0) {
  186. index--;
  187. nlmsg_trim(skb, nest);
  188. goto done;
  189. }
  190. nla_nest_end(skb, nest);
  191. n_i++;
  192. if (!(act_flags & TCA_FLAG_LARGE_DUMP_ON) &&
  193. n_i >= TCA_ACT_MAX_PRIO)
  194. goto done;
  195. }
  196. done:
  197. if (index >= 0)
  198. cb->args[0] = index + 1;
  199. spin_unlock(&idrinfo->lock);
  200. if (n_i) {
  201. if (act_flags & TCA_FLAG_LARGE_DUMP_ON)
  202. cb->args[1] = n_i;
  203. }
  204. return n_i;
  205. nla_put_failure:
  206. nla_nest_cancel(skb, nest);
  207. goto done;
  208. }
  209. static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
  210. const struct tc_action_ops *ops)
  211. {
  212. struct nlattr *nest;
  213. int n_i = 0;
  214. int ret = -EINVAL;
  215. struct idr *idr = &idrinfo->action_idr;
  216. struct tc_action *p;
  217. unsigned long id = 1;
  218. nest = nla_nest_start(skb, 0);
  219. if (nest == NULL)
  220. goto nla_put_failure;
  221. if (nla_put_string(skb, TCA_KIND, ops->kind))
  222. goto nla_put_failure;
  223. idr_for_each_entry_ul(idr, p, id) {
  224. ret = __tcf_idr_release(p, false, true);
  225. if (ret == ACT_P_DELETED) {
  226. module_put(ops->owner);
  227. n_i++;
  228. } else if (ret < 0) {
  229. goto nla_put_failure;
  230. }
  231. }
  232. if (nla_put_u32(skb, TCA_FCNT, n_i))
  233. goto nla_put_failure;
  234. nla_nest_end(skb, nest);
  235. return n_i;
  236. nla_put_failure:
  237. nla_nest_cancel(skb, nest);
  238. return ret;
  239. }
  240. int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
  241. struct netlink_callback *cb, int type,
  242. const struct tc_action_ops *ops,
  243. struct netlink_ext_ack *extack)
  244. {
  245. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  246. if (type == RTM_DELACTION) {
  247. return tcf_del_walker(idrinfo, skb, ops);
  248. } else if (type == RTM_GETACTION) {
  249. return tcf_dump_walker(idrinfo, skb, cb);
  250. } else {
  251. WARN(1, "tcf_generic_walker: unknown command %d\n", type);
  252. NL_SET_ERR_MSG(extack, "tcf_generic_walker: unknown command");
  253. return -EINVAL;
  254. }
  255. }
  256. EXPORT_SYMBOL(tcf_generic_walker);
  257. static bool __tcf_idr_check(struct tc_action_net *tn, u32 index,
  258. struct tc_action **a, int bind)
  259. {
  260. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  261. struct tc_action *p;
  262. spin_lock(&idrinfo->lock);
  263. p = idr_find(&idrinfo->action_idr, index);
  264. if (IS_ERR(p)) {
  265. p = NULL;
  266. } else if (p) {
  267. refcount_inc(&p->tcfa_refcnt);
  268. if (bind)
  269. atomic_inc(&p->tcfa_bindcnt);
  270. }
  271. spin_unlock(&idrinfo->lock);
  272. if (p) {
  273. *a = p;
  274. return true;
  275. }
  276. return false;
  277. }
  278. int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
  279. {
  280. return __tcf_idr_check(tn, index, a, 0);
  281. }
  282. EXPORT_SYMBOL(tcf_idr_search);
  283. bool tcf_idr_check(struct tc_action_net *tn, u32 index, struct tc_action **a,
  284. int bind)
  285. {
  286. return __tcf_idr_check(tn, index, a, bind);
  287. }
  288. EXPORT_SYMBOL(tcf_idr_check);
  289. int tcf_idr_delete_index(struct tc_action_net *tn, u32 index)
  290. {
  291. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  292. struct tc_action *p;
  293. int ret = 0;
  294. spin_lock(&idrinfo->lock);
  295. p = idr_find(&idrinfo->action_idr, index);
  296. if (!p) {
  297. spin_unlock(&idrinfo->lock);
  298. return -ENOENT;
  299. }
  300. if (!atomic_read(&p->tcfa_bindcnt)) {
  301. if (refcount_dec_and_test(&p->tcfa_refcnt)) {
  302. struct module *owner = p->ops->owner;
  303. WARN_ON(p != idr_remove(&idrinfo->action_idr,
  304. p->tcfa_index));
  305. spin_unlock(&idrinfo->lock);
  306. tcf_action_cleanup(p);
  307. module_put(owner);
  308. return 0;
  309. }
  310. ret = 0;
  311. } else {
  312. ret = -EPERM;
  313. }
  314. spin_unlock(&idrinfo->lock);
  315. return ret;
  316. }
  317. EXPORT_SYMBOL(tcf_idr_delete_index);
  318. int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
  319. struct tc_action **a, const struct tc_action_ops *ops,
  320. int bind, bool cpustats)
  321. {
  322. struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
  323. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  324. int err = -ENOMEM;
  325. if (unlikely(!p))
  326. return -ENOMEM;
  327. refcount_set(&p->tcfa_refcnt, 1);
  328. if (bind)
  329. atomic_set(&p->tcfa_bindcnt, 1);
  330. if (cpustats) {
  331. p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
  332. if (!p->cpu_bstats)
  333. goto err1;
  334. p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
  335. if (!p->cpu_qstats)
  336. goto err2;
  337. }
  338. spin_lock_init(&p->tcfa_lock);
  339. p->tcfa_index = index;
  340. p->tcfa_tm.install = jiffies;
  341. p->tcfa_tm.lastuse = jiffies;
  342. p->tcfa_tm.firstuse = 0;
  343. if (est) {
  344. err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
  345. &p->tcfa_rate_est,
  346. &p->tcfa_lock, NULL, est);
  347. if (err)
  348. goto err3;
  349. }
  350. p->idrinfo = idrinfo;
  351. p->ops = ops;
  352. INIT_LIST_HEAD(&p->list);
  353. *a = p;
  354. return 0;
  355. err3:
  356. free_percpu(p->cpu_qstats);
  357. err2:
  358. free_percpu(p->cpu_bstats);
  359. err1:
  360. kfree(p);
  361. return err;
  362. }
  363. EXPORT_SYMBOL(tcf_idr_create);
  364. void tcf_idr_insert(struct tc_action_net *tn, struct tc_action *a)
  365. {
  366. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  367. spin_lock(&idrinfo->lock);
  368. /* Replace ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
  369. WARN_ON(!IS_ERR(idr_replace(&idrinfo->action_idr, a, a->tcfa_index)));
  370. spin_unlock(&idrinfo->lock);
  371. }
  372. EXPORT_SYMBOL(tcf_idr_insert);
  373. /* Cleanup idr index that was allocated but not initialized. */
  374. void tcf_idr_cleanup(struct tc_action_net *tn, u32 index)
  375. {
  376. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  377. spin_lock(&idrinfo->lock);
  378. /* Remove ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */
  379. WARN_ON(!IS_ERR(idr_remove(&idrinfo->action_idr, index)));
  380. spin_unlock(&idrinfo->lock);
  381. }
  382. EXPORT_SYMBOL(tcf_idr_cleanup);
  383. /* Check if action with specified index exists. If actions is found, increments
  384. * its reference and bind counters, and return 1. Otherwise insert temporary
  385. * error pointer (to prevent concurrent users from inserting actions with same
  386. * index) and return 0.
  387. */
  388. int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index,
  389. struct tc_action **a, int bind)
  390. {
  391. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  392. struct tc_action *p;
  393. int ret;
  394. again:
  395. spin_lock(&idrinfo->lock);
  396. if (*index) {
  397. p = idr_find(&idrinfo->action_idr, *index);
  398. if (IS_ERR(p)) {
  399. /* This means that another process allocated
  400. * index but did not assign the pointer yet.
  401. */
  402. spin_unlock(&idrinfo->lock);
  403. goto again;
  404. }
  405. if (p) {
  406. refcount_inc(&p->tcfa_refcnt);
  407. if (bind)
  408. atomic_inc(&p->tcfa_bindcnt);
  409. *a = p;
  410. ret = 1;
  411. } else {
  412. *a = NULL;
  413. ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
  414. *index, GFP_ATOMIC);
  415. if (!ret)
  416. idr_replace(&idrinfo->action_idr,
  417. ERR_PTR(-EBUSY), *index);
  418. }
  419. } else {
  420. *index = 1;
  421. *a = NULL;
  422. ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index,
  423. UINT_MAX, GFP_ATOMIC);
  424. if (!ret)
  425. idr_replace(&idrinfo->action_idr, ERR_PTR(-EBUSY),
  426. *index);
  427. }
  428. spin_unlock(&idrinfo->lock);
  429. return ret;
  430. }
  431. EXPORT_SYMBOL(tcf_idr_check_alloc);
  432. void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
  433. struct tcf_idrinfo *idrinfo)
  434. {
  435. struct idr *idr = &idrinfo->action_idr;
  436. struct tc_action *p;
  437. int ret;
  438. unsigned long id = 1;
  439. idr_for_each_entry_ul(idr, p, id) {
  440. ret = __tcf_idr_release(p, false, true);
  441. if (ret == ACT_P_DELETED)
  442. module_put(ops->owner);
  443. else if (ret < 0)
  444. return;
  445. }
  446. idr_destroy(&idrinfo->action_idr);
  447. }
  448. EXPORT_SYMBOL(tcf_idrinfo_destroy);
  449. static LIST_HEAD(act_base);
  450. static DEFINE_RWLOCK(act_mod_lock);
  451. int tcf_register_action(struct tc_action_ops *act,
  452. struct pernet_operations *ops)
  453. {
  454. struct tc_action_ops *a;
  455. int ret;
  456. if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
  457. return -EINVAL;
  458. /* We have to register pernet ops before making the action ops visible,
  459. * otherwise tcf_action_init_1() could get a partially initialized
  460. * netns.
  461. */
  462. ret = register_pernet_subsys(ops);
  463. if (ret)
  464. return ret;
  465. write_lock(&act_mod_lock);
  466. list_for_each_entry(a, &act_base, head) {
  467. if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
  468. write_unlock(&act_mod_lock);
  469. unregister_pernet_subsys(ops);
  470. return -EEXIST;
  471. }
  472. }
  473. list_add_tail(&act->head, &act_base);
  474. write_unlock(&act_mod_lock);
  475. return 0;
  476. }
  477. EXPORT_SYMBOL(tcf_register_action);
  478. int tcf_unregister_action(struct tc_action_ops *act,
  479. struct pernet_operations *ops)
  480. {
  481. struct tc_action_ops *a;
  482. int err = -ENOENT;
  483. write_lock(&act_mod_lock);
  484. list_for_each_entry(a, &act_base, head) {
  485. if (a == act) {
  486. list_del(&act->head);
  487. err = 0;
  488. break;
  489. }
  490. }
  491. write_unlock(&act_mod_lock);
  492. if (!err)
  493. unregister_pernet_subsys(ops);
  494. return err;
  495. }
  496. EXPORT_SYMBOL(tcf_unregister_action);
  497. /* lookup by name */
  498. static struct tc_action_ops *tc_lookup_action_n(char *kind)
  499. {
  500. struct tc_action_ops *a, *res = NULL;
  501. if (kind) {
  502. read_lock(&act_mod_lock);
  503. list_for_each_entry(a, &act_base, head) {
  504. if (strcmp(kind, a->kind) == 0) {
  505. if (try_module_get(a->owner))
  506. res = a;
  507. break;
  508. }
  509. }
  510. read_unlock(&act_mod_lock);
  511. }
  512. return res;
  513. }
  514. /* lookup by nlattr */
  515. static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
  516. {
  517. struct tc_action_ops *a, *res = NULL;
  518. if (kind) {
  519. read_lock(&act_mod_lock);
  520. list_for_each_entry(a, &act_base, head) {
  521. if (nla_strcmp(kind, a->kind) == 0) {
  522. if (try_module_get(a->owner))
  523. res = a;
  524. break;
  525. }
  526. }
  527. read_unlock(&act_mod_lock);
  528. }
  529. return res;
  530. }
  531. /*TCA_ACT_MAX_PRIO is 32, there count upto 32 */
  532. #define TCA_ACT_MAX_PRIO_MASK 0x1FF
  533. int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
  534. int nr_actions, struct tcf_result *res)
  535. {
  536. u32 jmp_prgcnt = 0;
  537. u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
  538. int i;
  539. int ret = TC_ACT_OK;
  540. if (skb_skip_tc_classify(skb))
  541. return TC_ACT_OK;
  542. restart_act_graph:
  543. for (i = 0; i < nr_actions; i++) {
  544. const struct tc_action *a = actions[i];
  545. if (jmp_prgcnt > 0) {
  546. jmp_prgcnt -= 1;
  547. continue;
  548. }
  549. repeat:
  550. ret = a->ops->act(skb, a, res);
  551. if (ret == TC_ACT_REPEAT)
  552. goto repeat; /* we need a ttl - JHS */
  553. if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
  554. jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
  555. if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
  556. /* faulty opcode, stop pipeline */
  557. return TC_ACT_OK;
  558. } else {
  559. jmp_ttl -= 1;
  560. if (jmp_ttl > 0)
  561. goto restart_act_graph;
  562. else /* faulty graph, stop pipeline */
  563. return TC_ACT_OK;
  564. }
  565. } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
  566. tcf_action_goto_chain_exec(a, res);
  567. }
  568. if (ret != TC_ACT_PIPE)
  569. break;
  570. }
  571. return ret;
  572. }
  573. EXPORT_SYMBOL(tcf_action_exec);
  574. int tcf_action_destroy(struct tc_action *actions[], int bind)
  575. {
  576. const struct tc_action_ops *ops;
  577. struct tc_action *a;
  578. int ret = 0, i;
  579. for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
  580. a = actions[i];
  581. actions[i] = NULL;
  582. ops = a->ops;
  583. ret = __tcf_idr_release(a, bind, true);
  584. if (ret == ACT_P_DELETED)
  585. module_put(ops->owner);
  586. else if (ret < 0)
  587. return ret;
  588. }
  589. return ret;
  590. }
  591. static int tcf_action_put(struct tc_action *p)
  592. {
  593. return __tcf_action_put(p, false);
  594. }
  595. static void tcf_action_put_many(struct tc_action *actions[])
  596. {
  597. int i;
  598. for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
  599. struct tc_action *a = actions[i];
  600. const struct tc_action_ops *ops = a->ops;
  601. if (tcf_action_put(a))
  602. module_put(ops->owner);
  603. }
  604. }
  605. int
  606. tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  607. {
  608. return a->ops->dump(skb, a, bind, ref);
  609. }
  610. int
  611. tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  612. {
  613. int err = -EINVAL;
  614. unsigned char *b = skb_tail_pointer(skb);
  615. struct nlattr *nest;
  616. struct tc_cookie *cookie;
  617. if (nla_put_string(skb, TCA_KIND, a->ops->kind))
  618. goto nla_put_failure;
  619. if (tcf_action_copy_stats(skb, a, 0))
  620. goto nla_put_failure;
  621. rcu_read_lock();
  622. cookie = rcu_dereference(a->act_cookie);
  623. if (cookie) {
  624. if (nla_put(skb, TCA_ACT_COOKIE, cookie->len, cookie->data)) {
  625. rcu_read_unlock();
  626. goto nla_put_failure;
  627. }
  628. }
  629. rcu_read_unlock();
  630. nest = nla_nest_start(skb, TCA_OPTIONS);
  631. if (nest == NULL)
  632. goto nla_put_failure;
  633. err = tcf_action_dump_old(skb, a, bind, ref);
  634. if (err > 0) {
  635. nla_nest_end(skb, nest);
  636. return err;
  637. }
  638. nla_put_failure:
  639. nlmsg_trim(skb, b);
  640. return -1;
  641. }
  642. EXPORT_SYMBOL(tcf_action_dump_1);
  643. int tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[],
  644. int bind, int ref)
  645. {
  646. struct tc_action *a;
  647. int err = -EINVAL, i;
  648. struct nlattr *nest;
  649. for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
  650. a = actions[i];
  651. nest = nla_nest_start(skb, a->order);
  652. if (nest == NULL)
  653. goto nla_put_failure;
  654. err = tcf_action_dump_1(skb, a, bind, ref);
  655. if (err < 0)
  656. goto errout;
  657. nla_nest_end(skb, nest);
  658. }
  659. return 0;
  660. nla_put_failure:
  661. err = -EINVAL;
  662. errout:
  663. nla_nest_cancel(skb, nest);
  664. return err;
  665. }
  666. static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
  667. {
  668. struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL);
  669. if (!c)
  670. return NULL;
  671. c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
  672. if (!c->data) {
  673. kfree(c);
  674. return NULL;
  675. }
  676. c->len = nla_len(tb[TCA_ACT_COOKIE]);
  677. return c;
  678. }
  679. static bool tcf_action_valid(int action)
  680. {
  681. int opcode = TC_ACT_EXT_OPCODE(action);
  682. if (!opcode)
  683. return action <= TC_ACT_VALUE_MAX;
  684. return opcode <= TC_ACT_EXT_OPCODE_MAX || action == TC_ACT_UNSPEC;
  685. }
  686. struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
  687. struct nlattr *nla, struct nlattr *est,
  688. char *name, int ovr, int bind,
  689. bool rtnl_held,
  690. struct netlink_ext_ack *extack)
  691. {
  692. struct tc_action *a;
  693. struct tc_action_ops *a_o;
  694. struct tc_cookie *cookie = NULL;
  695. char act_name[IFNAMSIZ];
  696. struct nlattr *tb[TCA_ACT_MAX + 1];
  697. struct nlattr *kind;
  698. int err;
  699. if (name == NULL) {
  700. err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack);
  701. if (err < 0)
  702. goto err_out;
  703. err = -EINVAL;
  704. kind = tb[TCA_ACT_KIND];
  705. if (!kind) {
  706. NL_SET_ERR_MSG(extack, "TC action kind must be specified");
  707. goto err_out;
  708. }
  709. if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ) {
  710. NL_SET_ERR_MSG(extack, "TC action name too long");
  711. goto err_out;
  712. }
  713. if (tb[TCA_ACT_COOKIE]) {
  714. int cklen = nla_len(tb[TCA_ACT_COOKIE]);
  715. if (cklen > TC_COOKIE_MAX_SIZE) {
  716. NL_SET_ERR_MSG(extack, "TC cookie size above the maximum");
  717. goto err_out;
  718. }
  719. cookie = nla_memdup_cookie(tb);
  720. if (!cookie) {
  721. NL_SET_ERR_MSG(extack, "No memory to generate TC cookie");
  722. err = -ENOMEM;
  723. goto err_out;
  724. }
  725. }
  726. } else {
  727. if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ) {
  728. NL_SET_ERR_MSG(extack, "TC action name too long");
  729. err = -EINVAL;
  730. goto err_out;
  731. }
  732. }
  733. a_o = tc_lookup_action_n(act_name);
  734. if (a_o == NULL) {
  735. #ifdef CONFIG_MODULES
  736. if (rtnl_held)
  737. rtnl_unlock();
  738. request_module("act_%s", act_name);
  739. if (rtnl_held)
  740. rtnl_lock();
  741. a_o = tc_lookup_action_n(act_name);
  742. /* We dropped the RTNL semaphore in order to
  743. * perform the module load. So, even if we
  744. * succeeded in loading the module we have to
  745. * tell the caller to replay the request. We
  746. * indicate this using -EAGAIN.
  747. */
  748. if (a_o != NULL) {
  749. err = -EAGAIN;
  750. goto err_mod;
  751. }
  752. #endif
  753. NL_SET_ERR_MSG(extack, "Failed to load TC action module");
  754. err = -ENOENT;
  755. goto err_out;
  756. }
  757. /* backward compatibility for policer */
  758. if (name == NULL)
  759. err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind,
  760. rtnl_held, extack);
  761. else
  762. err = a_o->init(net, nla, est, &a, ovr, bind, rtnl_held,
  763. extack);
  764. if (err < 0)
  765. goto err_mod;
  766. if (!name && tb[TCA_ACT_COOKIE])
  767. tcf_set_action_cookie(&a->act_cookie, cookie);
  768. /* module count goes up only when brand new policy is created
  769. * if it exists and is only bound to in a_o->init() then
  770. * ACT_P_CREATED is not returned (a zero is).
  771. */
  772. if (err != ACT_P_CREATED)
  773. module_put(a_o->owner);
  774. if (TC_ACT_EXT_CMP(a->tcfa_action, TC_ACT_GOTO_CHAIN)) {
  775. err = tcf_action_goto_chain_init(a, tp);
  776. if (err) {
  777. struct tc_action *actions[] = { a, NULL };
  778. tcf_action_destroy(actions, bind);
  779. NL_SET_ERR_MSG(extack, "Failed to init TC action chain");
  780. return ERR_PTR(err);
  781. }
  782. }
  783. if (!tcf_action_valid(a->tcfa_action)) {
  784. NL_SET_ERR_MSG(extack, "invalid action value, using TC_ACT_UNSPEC instead");
  785. a->tcfa_action = TC_ACT_UNSPEC;
  786. }
  787. return a;
  788. err_mod:
  789. module_put(a_o->owner);
  790. err_out:
  791. if (cookie) {
  792. kfree(cookie->data);
  793. kfree(cookie);
  794. }
  795. return ERR_PTR(err);
  796. }
  797. /* Returns numbers of initialized actions or negative error. */
  798. int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
  799. struct nlattr *est, char *name, int ovr, int bind,
  800. struct tc_action *actions[], size_t *attr_size,
  801. bool rtnl_held, struct netlink_ext_ack *extack)
  802. {
  803. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  804. struct tc_action *act;
  805. size_t sz = 0;
  806. int err;
  807. int i;
  808. err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, extack);
  809. if (err < 0)
  810. return err;
  811. for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
  812. act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind,
  813. rtnl_held, extack);
  814. if (IS_ERR(act)) {
  815. err = PTR_ERR(act);
  816. goto err;
  817. }
  818. act->order = i;
  819. sz += tcf_action_fill_size(act);
  820. /* Start from index 0 */
  821. actions[i - 1] = act;
  822. }
  823. *attr_size = tcf_action_full_attrs_size(sz);
  824. return i - 1;
  825. err:
  826. tcf_action_destroy(actions, bind);
  827. return err;
  828. }
  829. int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
  830. int compat_mode)
  831. {
  832. int err = 0;
  833. struct gnet_dump d;
  834. if (p == NULL)
  835. goto errout;
  836. /* compat_mode being true specifies a call that is supposed
  837. * to add additional backward compatibility statistic TLVs.
  838. */
  839. if (compat_mode) {
  840. if (p->type == TCA_OLD_COMPAT)
  841. err = gnet_stats_start_copy_compat(skb, 0,
  842. TCA_STATS,
  843. TCA_XSTATS,
  844. &p->tcfa_lock, &d,
  845. TCA_PAD);
  846. else
  847. return 0;
  848. } else
  849. err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
  850. &p->tcfa_lock, &d, TCA_ACT_PAD);
  851. if (err < 0)
  852. goto errout;
  853. if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
  854. gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
  855. gnet_stats_copy_queue(&d, p->cpu_qstats,
  856. &p->tcfa_qstats,
  857. p->tcfa_qstats.qlen) < 0)
  858. goto errout;
  859. if (gnet_stats_finish_copy(&d) < 0)
  860. goto errout;
  861. return 0;
  862. errout:
  863. return -1;
  864. }
  865. static int tca_get_fill(struct sk_buff *skb, struct tc_action *actions[],
  866. u32 portid, u32 seq, u16 flags, int event, int bind,
  867. int ref)
  868. {
  869. struct tcamsg *t;
  870. struct nlmsghdr *nlh;
  871. unsigned char *b = skb_tail_pointer(skb);
  872. struct nlattr *nest;
  873. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
  874. if (!nlh)
  875. goto out_nlmsg_trim;
  876. t = nlmsg_data(nlh);
  877. t->tca_family = AF_UNSPEC;
  878. t->tca__pad1 = 0;
  879. t->tca__pad2 = 0;
  880. nest = nla_nest_start(skb, TCA_ACT_TAB);
  881. if (!nest)
  882. goto out_nlmsg_trim;
  883. if (tcf_action_dump(skb, actions, bind, ref) < 0)
  884. goto out_nlmsg_trim;
  885. nla_nest_end(skb, nest);
  886. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  887. return skb->len;
  888. out_nlmsg_trim:
  889. nlmsg_trim(skb, b);
  890. return -1;
  891. }
  892. static int
  893. tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
  894. struct tc_action *actions[], int event,
  895. struct netlink_ext_ack *extack)
  896. {
  897. struct sk_buff *skb;
  898. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  899. if (!skb)
  900. return -ENOBUFS;
  901. if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
  902. 0, 1) <= 0) {
  903. NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
  904. kfree_skb(skb);
  905. return -EINVAL;
  906. }
  907. return rtnl_unicast(skb, net, portid);
  908. }
  909. static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
  910. struct nlmsghdr *n, u32 portid,
  911. struct netlink_ext_ack *extack)
  912. {
  913. struct nlattr *tb[TCA_ACT_MAX + 1];
  914. const struct tc_action_ops *ops;
  915. struct tc_action *a;
  916. int index;
  917. int err;
  918. err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack);
  919. if (err < 0)
  920. goto err_out;
  921. err = -EINVAL;
  922. if (tb[TCA_ACT_INDEX] == NULL ||
  923. nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) {
  924. NL_SET_ERR_MSG(extack, "Invalid TC action index value");
  925. goto err_out;
  926. }
  927. index = nla_get_u32(tb[TCA_ACT_INDEX]);
  928. err = -EINVAL;
  929. ops = tc_lookup_action(tb[TCA_ACT_KIND]);
  930. if (!ops) { /* could happen in batch of actions */
  931. NL_SET_ERR_MSG(extack, "Specified TC action not found");
  932. goto err_out;
  933. }
  934. err = -ENOENT;
  935. if (ops->lookup(net, &a, index, extack) == 0)
  936. goto err_mod;
  937. module_put(ops->owner);
  938. return a;
  939. err_mod:
  940. module_put(ops->owner);
  941. err_out:
  942. return ERR_PTR(err);
  943. }
  944. static int tca_action_flush(struct net *net, struct nlattr *nla,
  945. struct nlmsghdr *n, u32 portid,
  946. struct netlink_ext_ack *extack)
  947. {
  948. struct sk_buff *skb;
  949. unsigned char *b;
  950. struct nlmsghdr *nlh;
  951. struct tcamsg *t;
  952. struct netlink_callback dcb;
  953. struct nlattr *nest;
  954. struct nlattr *tb[TCA_ACT_MAX + 1];
  955. const struct tc_action_ops *ops;
  956. struct nlattr *kind;
  957. int err = -ENOMEM;
  958. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  959. if (!skb)
  960. return err;
  961. b = skb_tail_pointer(skb);
  962. err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack);
  963. if (err < 0)
  964. goto err_out;
  965. err = -EINVAL;
  966. kind = tb[TCA_ACT_KIND];
  967. ops = tc_lookup_action(kind);
  968. if (!ops) { /*some idjot trying to flush unknown action */
  969. NL_SET_ERR_MSG(extack, "Cannot flush unknown TC action");
  970. goto err_out;
  971. }
  972. nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
  973. sizeof(*t), 0);
  974. if (!nlh) {
  975. NL_SET_ERR_MSG(extack, "Failed to create TC action flush notification");
  976. goto out_module_put;
  977. }
  978. t = nlmsg_data(nlh);
  979. t->tca_family = AF_UNSPEC;
  980. t->tca__pad1 = 0;
  981. t->tca__pad2 = 0;
  982. nest = nla_nest_start(skb, TCA_ACT_TAB);
  983. if (!nest) {
  984. NL_SET_ERR_MSG(extack, "Failed to add new netlink message");
  985. goto out_module_put;
  986. }
  987. err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops, extack);
  988. if (err <= 0) {
  989. nla_nest_cancel(skb, nest);
  990. goto out_module_put;
  991. }
  992. nla_nest_end(skb, nest);
  993. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  994. nlh->nlmsg_flags |= NLM_F_ROOT;
  995. module_put(ops->owner);
  996. err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  997. n->nlmsg_flags & NLM_F_ECHO);
  998. if (err > 0)
  999. return 0;
  1000. if (err < 0)
  1001. NL_SET_ERR_MSG(extack, "Failed to send TC action flush notification");
  1002. return err;
  1003. out_module_put:
  1004. module_put(ops->owner);
  1005. err_out:
  1006. kfree_skb(skb);
  1007. return err;
  1008. }
  1009. static int tcf_action_delete(struct net *net, struct tc_action *actions[],
  1010. int *acts_deleted, struct netlink_ext_ack *extack)
  1011. {
  1012. u32 act_index;
  1013. int ret, i;
  1014. for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
  1015. struct tc_action *a = actions[i];
  1016. const struct tc_action_ops *ops = a->ops;
  1017. /* Actions can be deleted concurrently so we must save their
  1018. * type and id to search again after reference is released.
  1019. */
  1020. act_index = a->tcfa_index;
  1021. if (tcf_action_put(a)) {
  1022. /* last reference, action was deleted concurrently */
  1023. module_put(ops->owner);
  1024. } else {
  1025. /* now do the delete */
  1026. ret = ops->delete(net, act_index);
  1027. if (ret < 0) {
  1028. *acts_deleted = i + 1;
  1029. return ret;
  1030. }
  1031. }
  1032. }
  1033. *acts_deleted = i;
  1034. return 0;
  1035. }
  1036. static int
  1037. tcf_del_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
  1038. int *acts_deleted, u32 portid, size_t attr_size,
  1039. struct netlink_ext_ack *extack)
  1040. {
  1041. int ret;
  1042. struct sk_buff *skb;
  1043. skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
  1044. GFP_KERNEL);
  1045. if (!skb)
  1046. return -ENOBUFS;
  1047. if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
  1048. 0, 2) <= 0) {
  1049. NL_SET_ERR_MSG(extack, "Failed to fill netlink TC action attributes");
  1050. kfree_skb(skb);
  1051. return -EINVAL;
  1052. }
  1053. /* now do the delete */
  1054. ret = tcf_action_delete(net, actions, acts_deleted, extack);
  1055. if (ret < 0) {
  1056. NL_SET_ERR_MSG(extack, "Failed to delete TC action");
  1057. kfree_skb(skb);
  1058. return ret;
  1059. }
  1060. ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  1061. n->nlmsg_flags & NLM_F_ECHO);
  1062. if (ret > 0)
  1063. return 0;
  1064. return ret;
  1065. }
  1066. static int
  1067. tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
  1068. u32 portid, int event, struct netlink_ext_ack *extack)
  1069. {
  1070. int i, ret;
  1071. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  1072. struct tc_action *act;
  1073. size_t attr_size = 0;
  1074. struct tc_action *actions[TCA_ACT_MAX_PRIO + 1] = {};
  1075. int acts_deleted = 0;
  1076. ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, extack);
  1077. if (ret < 0)
  1078. return ret;
  1079. if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
  1080. if (tb[1])
  1081. return tca_action_flush(net, tb[1], n, portid, extack);
  1082. NL_SET_ERR_MSG(extack, "Invalid netlink attributes while flushing TC action");
  1083. return -EINVAL;
  1084. }
  1085. for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
  1086. act = tcf_action_get_1(net, tb[i], n, portid, extack);
  1087. if (IS_ERR(act)) {
  1088. ret = PTR_ERR(act);
  1089. goto err;
  1090. }
  1091. act->order = i;
  1092. attr_size += tcf_action_fill_size(act);
  1093. actions[i - 1] = act;
  1094. }
  1095. attr_size = tcf_action_full_attrs_size(attr_size);
  1096. if (event == RTM_GETACTION)
  1097. ret = tcf_get_notify(net, portid, n, actions, event, extack);
  1098. else { /* delete */
  1099. ret = tcf_del_notify(net, n, actions, &acts_deleted, portid,
  1100. attr_size, extack);
  1101. if (ret)
  1102. goto err;
  1103. return ret;
  1104. }
  1105. err:
  1106. tcf_action_put_many(&actions[acts_deleted]);
  1107. return ret;
  1108. }
  1109. static int
  1110. tcf_add_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
  1111. u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
  1112. {
  1113. struct sk_buff *skb;
  1114. int err = 0;
  1115. skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
  1116. GFP_KERNEL);
  1117. if (!skb)
  1118. return -ENOBUFS;
  1119. if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
  1120. RTM_NEWACTION, 0, 0) <= 0) {
  1121. NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
  1122. kfree_skb(skb);
  1123. return -EINVAL;
  1124. }
  1125. err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  1126. n->nlmsg_flags & NLM_F_ECHO);
  1127. if (err > 0)
  1128. err = 0;
  1129. return err;
  1130. }
  1131. static int tcf_action_add(struct net *net, struct nlattr *nla,
  1132. struct nlmsghdr *n, u32 portid, int ovr,
  1133. struct netlink_ext_ack *extack)
  1134. {
  1135. size_t attr_size = 0;
  1136. int ret = 0;
  1137. struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
  1138. ret = tcf_action_init(net, NULL, nla, NULL, NULL, ovr, 0, actions,
  1139. &attr_size, true, extack);
  1140. if (ret < 0)
  1141. return ret;
  1142. ret = tcf_add_notify(net, n, actions, portid, attr_size, extack);
  1143. if (ovr)
  1144. tcf_action_put_many(actions);
  1145. return ret;
  1146. }
  1147. static u32 tcaa_root_flags_allowed = TCA_FLAG_LARGE_DUMP_ON;
  1148. static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = {
  1149. [TCA_ROOT_FLAGS] = { .type = NLA_BITFIELD32,
  1150. .validation_data = &tcaa_root_flags_allowed },
  1151. [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 },
  1152. };
  1153. static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
  1154. struct netlink_ext_ack *extack)
  1155. {
  1156. struct net *net = sock_net(skb->sk);
  1157. struct nlattr *tca[TCA_ROOT_MAX + 1];
  1158. u32 portid = skb ? NETLINK_CB(skb).portid : 0;
  1159. int ret = 0, ovr = 0;
  1160. if ((n->nlmsg_type != RTM_GETACTION) &&
  1161. !netlink_capable(skb, CAP_NET_ADMIN))
  1162. return -EPERM;
  1163. ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ROOT_MAX, NULL,
  1164. extack);
  1165. if (ret < 0)
  1166. return ret;
  1167. if (tca[TCA_ACT_TAB] == NULL) {
  1168. NL_SET_ERR_MSG(extack, "Netlink action attributes missing");
  1169. return -EINVAL;
  1170. }
  1171. /* n->nlmsg_flags & NLM_F_CREATE */
  1172. switch (n->nlmsg_type) {
  1173. case RTM_NEWACTION:
  1174. /* we are going to assume all other flags
  1175. * imply create only if it doesn't exist
  1176. * Note that CREATE | EXCL implies that
  1177. * but since we want avoid ambiguity (eg when flags
  1178. * is zero) then just set this
  1179. */
  1180. if (n->nlmsg_flags & NLM_F_REPLACE)
  1181. ovr = 1;
  1182. replay:
  1183. ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr,
  1184. extack);
  1185. if (ret == -EAGAIN)
  1186. goto replay;
  1187. break;
  1188. case RTM_DELACTION:
  1189. ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
  1190. portid, RTM_DELACTION, extack);
  1191. break;
  1192. case RTM_GETACTION:
  1193. ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
  1194. portid, RTM_GETACTION, extack);
  1195. break;
  1196. default:
  1197. BUG();
  1198. }
  1199. return ret;
  1200. }
  1201. static struct nlattr *find_dump_kind(struct nlattr **nla)
  1202. {
  1203. struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
  1204. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  1205. struct nlattr *kind;
  1206. tb1 = nla[TCA_ACT_TAB];
  1207. if (tb1 == NULL)
  1208. return NULL;
  1209. if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
  1210. NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0)
  1211. return NULL;
  1212. if (tb[1] == NULL)
  1213. return NULL;
  1214. if (nla_parse_nested(tb2, TCA_ACT_MAX, tb[1], NULL, NULL) < 0)
  1215. return NULL;
  1216. kind = tb2[TCA_ACT_KIND];
  1217. return kind;
  1218. }
  1219. static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
  1220. {
  1221. struct net *net = sock_net(skb->sk);
  1222. struct nlmsghdr *nlh;
  1223. unsigned char *b = skb_tail_pointer(skb);
  1224. struct nlattr *nest;
  1225. struct tc_action_ops *a_o;
  1226. int ret = 0;
  1227. struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
  1228. struct nlattr *tb[TCA_ROOT_MAX + 1];
  1229. struct nlattr *count_attr = NULL;
  1230. unsigned long jiffy_since = 0;
  1231. struct nlattr *kind = NULL;
  1232. struct nla_bitfield32 bf;
  1233. u32 msecs_since = 0;
  1234. u32 act_count = 0;
  1235. ret = nlmsg_parse(cb->nlh, sizeof(struct tcamsg), tb, TCA_ROOT_MAX,
  1236. tcaa_policy, NULL);
  1237. if (ret < 0)
  1238. return ret;
  1239. kind = find_dump_kind(tb);
  1240. if (kind == NULL) {
  1241. pr_info("tc_dump_action: action bad kind\n");
  1242. return 0;
  1243. }
  1244. a_o = tc_lookup_action(kind);
  1245. if (a_o == NULL)
  1246. return 0;
  1247. cb->args[2] = 0;
  1248. if (tb[TCA_ROOT_FLAGS]) {
  1249. bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]);
  1250. cb->args[2] = bf.value;
  1251. }
  1252. if (tb[TCA_ROOT_TIME_DELTA]) {
  1253. msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]);
  1254. }
  1255. nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  1256. cb->nlh->nlmsg_type, sizeof(*t), 0);
  1257. if (!nlh)
  1258. goto out_module_put;
  1259. if (msecs_since)
  1260. jiffy_since = jiffies - msecs_to_jiffies(msecs_since);
  1261. t = nlmsg_data(nlh);
  1262. t->tca_family = AF_UNSPEC;
  1263. t->tca__pad1 = 0;
  1264. t->tca__pad2 = 0;
  1265. cb->args[3] = jiffy_since;
  1266. count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32));
  1267. if (!count_attr)
  1268. goto out_module_put;
  1269. nest = nla_nest_start(skb, TCA_ACT_TAB);
  1270. if (nest == NULL)
  1271. goto out_module_put;
  1272. ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o, NULL);
  1273. if (ret < 0)
  1274. goto out_module_put;
  1275. if (ret > 0) {
  1276. nla_nest_end(skb, nest);
  1277. ret = skb->len;
  1278. act_count = cb->args[1];
  1279. memcpy(nla_data(count_attr), &act_count, sizeof(u32));
  1280. cb->args[1] = 0;
  1281. } else
  1282. nlmsg_trim(skb, b);
  1283. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  1284. if (NETLINK_CB(cb->skb).portid && ret)
  1285. nlh->nlmsg_flags |= NLM_F_MULTI;
  1286. module_put(a_o->owner);
  1287. return skb->len;
  1288. out_module_put:
  1289. module_put(a_o->owner);
  1290. nlmsg_trim(skb, b);
  1291. return skb->len;
  1292. }
  1293. struct tcf_action_net {
  1294. struct rhashtable egdev_ht;
  1295. };
  1296. static unsigned int tcf_action_net_id;
  1297. struct tcf_action_egdev_cb {
  1298. struct list_head list;
  1299. tc_setup_cb_t *cb;
  1300. void *cb_priv;
  1301. };
  1302. struct tcf_action_egdev {
  1303. struct rhash_head ht_node;
  1304. const struct net_device *dev;
  1305. unsigned int refcnt;
  1306. struct list_head cb_list;
  1307. };
  1308. static const struct rhashtable_params tcf_action_egdev_ht_params = {
  1309. .key_offset = offsetof(struct tcf_action_egdev, dev),
  1310. .head_offset = offsetof(struct tcf_action_egdev, ht_node),
  1311. .key_len = sizeof(const struct net_device *),
  1312. };
  1313. static struct tcf_action_egdev *
  1314. tcf_action_egdev_lookup(const struct net_device *dev)
  1315. {
  1316. struct net *net = dev_net(dev);
  1317. struct tcf_action_net *tan = net_generic(net, tcf_action_net_id);
  1318. return rhashtable_lookup_fast(&tan->egdev_ht, &dev,
  1319. tcf_action_egdev_ht_params);
  1320. }
  1321. static struct tcf_action_egdev *
  1322. tcf_action_egdev_get(const struct net_device *dev)
  1323. {
  1324. struct tcf_action_egdev *egdev;
  1325. struct tcf_action_net *tan;
  1326. egdev = tcf_action_egdev_lookup(dev);
  1327. if (egdev)
  1328. goto inc_ref;
  1329. egdev = kzalloc(sizeof(*egdev), GFP_KERNEL);
  1330. if (!egdev)
  1331. return NULL;
  1332. INIT_LIST_HEAD(&egdev->cb_list);
  1333. egdev->dev = dev;
  1334. tan = net_generic(dev_net(dev), tcf_action_net_id);
  1335. rhashtable_insert_fast(&tan->egdev_ht, &egdev->ht_node,
  1336. tcf_action_egdev_ht_params);
  1337. inc_ref:
  1338. egdev->refcnt++;
  1339. return egdev;
  1340. }
  1341. static void tcf_action_egdev_put(struct tcf_action_egdev *egdev)
  1342. {
  1343. struct tcf_action_net *tan;
  1344. if (--egdev->refcnt)
  1345. return;
  1346. tan = net_generic(dev_net(egdev->dev), tcf_action_net_id);
  1347. rhashtable_remove_fast(&tan->egdev_ht, &egdev->ht_node,
  1348. tcf_action_egdev_ht_params);
  1349. kfree(egdev);
  1350. }
  1351. static struct tcf_action_egdev_cb *
  1352. tcf_action_egdev_cb_lookup(struct tcf_action_egdev *egdev,
  1353. tc_setup_cb_t *cb, void *cb_priv)
  1354. {
  1355. struct tcf_action_egdev_cb *egdev_cb;
  1356. list_for_each_entry(egdev_cb, &egdev->cb_list, list)
  1357. if (egdev_cb->cb == cb && egdev_cb->cb_priv == cb_priv)
  1358. return egdev_cb;
  1359. return NULL;
  1360. }
  1361. static int tcf_action_egdev_cb_call(struct tcf_action_egdev *egdev,
  1362. enum tc_setup_type type,
  1363. void *type_data, bool err_stop)
  1364. {
  1365. struct tcf_action_egdev_cb *egdev_cb;
  1366. int ok_count = 0;
  1367. int err;
  1368. list_for_each_entry(egdev_cb, &egdev->cb_list, list) {
  1369. err = egdev_cb->cb(type, type_data, egdev_cb->cb_priv);
  1370. if (err) {
  1371. if (err_stop)
  1372. return err;
  1373. } else {
  1374. ok_count++;
  1375. }
  1376. }
  1377. return ok_count;
  1378. }
  1379. static int tcf_action_egdev_cb_add(struct tcf_action_egdev *egdev,
  1380. tc_setup_cb_t *cb, void *cb_priv)
  1381. {
  1382. struct tcf_action_egdev_cb *egdev_cb;
  1383. egdev_cb = tcf_action_egdev_cb_lookup(egdev, cb, cb_priv);
  1384. if (WARN_ON(egdev_cb))
  1385. return -EEXIST;
  1386. egdev_cb = kzalloc(sizeof(*egdev_cb), GFP_KERNEL);
  1387. if (!egdev_cb)
  1388. return -ENOMEM;
  1389. egdev_cb->cb = cb;
  1390. egdev_cb->cb_priv = cb_priv;
  1391. list_add(&egdev_cb->list, &egdev->cb_list);
  1392. return 0;
  1393. }
  1394. static void tcf_action_egdev_cb_del(struct tcf_action_egdev *egdev,
  1395. tc_setup_cb_t *cb, void *cb_priv)
  1396. {
  1397. struct tcf_action_egdev_cb *egdev_cb;
  1398. egdev_cb = tcf_action_egdev_cb_lookup(egdev, cb, cb_priv);
  1399. if (WARN_ON(!egdev_cb))
  1400. return;
  1401. list_del(&egdev_cb->list);
  1402. kfree(egdev_cb);
  1403. }
  1404. static int __tc_setup_cb_egdev_register(const struct net_device *dev,
  1405. tc_setup_cb_t *cb, void *cb_priv)
  1406. {
  1407. struct tcf_action_egdev *egdev = tcf_action_egdev_get(dev);
  1408. int err;
  1409. if (!egdev)
  1410. return -ENOMEM;
  1411. err = tcf_action_egdev_cb_add(egdev, cb, cb_priv);
  1412. if (err)
  1413. goto err_cb_add;
  1414. return 0;
  1415. err_cb_add:
  1416. tcf_action_egdev_put(egdev);
  1417. return err;
  1418. }
  1419. int tc_setup_cb_egdev_register(const struct net_device *dev,
  1420. tc_setup_cb_t *cb, void *cb_priv)
  1421. {
  1422. int err;
  1423. rtnl_lock();
  1424. err = __tc_setup_cb_egdev_register(dev, cb, cb_priv);
  1425. rtnl_unlock();
  1426. return err;
  1427. }
  1428. EXPORT_SYMBOL_GPL(tc_setup_cb_egdev_register);
  1429. static void __tc_setup_cb_egdev_unregister(const struct net_device *dev,
  1430. tc_setup_cb_t *cb, void *cb_priv)
  1431. {
  1432. struct tcf_action_egdev *egdev = tcf_action_egdev_lookup(dev);
  1433. if (WARN_ON(!egdev))
  1434. return;
  1435. tcf_action_egdev_cb_del(egdev, cb, cb_priv);
  1436. tcf_action_egdev_put(egdev);
  1437. }
  1438. void tc_setup_cb_egdev_unregister(const struct net_device *dev,
  1439. tc_setup_cb_t *cb, void *cb_priv)
  1440. {
  1441. rtnl_lock();
  1442. __tc_setup_cb_egdev_unregister(dev, cb, cb_priv);
  1443. rtnl_unlock();
  1444. }
  1445. EXPORT_SYMBOL_GPL(tc_setup_cb_egdev_unregister);
  1446. int tc_setup_cb_egdev_call(const struct net_device *dev,
  1447. enum tc_setup_type type, void *type_data,
  1448. bool err_stop)
  1449. {
  1450. struct tcf_action_egdev *egdev = tcf_action_egdev_lookup(dev);
  1451. if (!egdev)
  1452. return 0;
  1453. return tcf_action_egdev_cb_call(egdev, type, type_data, err_stop);
  1454. }
  1455. EXPORT_SYMBOL_GPL(tc_setup_cb_egdev_call);
  1456. static __net_init int tcf_action_net_init(struct net *net)
  1457. {
  1458. struct tcf_action_net *tan = net_generic(net, tcf_action_net_id);
  1459. return rhashtable_init(&tan->egdev_ht, &tcf_action_egdev_ht_params);
  1460. }
  1461. static void __net_exit tcf_action_net_exit(struct net *net)
  1462. {
  1463. struct tcf_action_net *tan = net_generic(net, tcf_action_net_id);
  1464. rhashtable_destroy(&tan->egdev_ht);
  1465. }
  1466. static struct pernet_operations tcf_action_net_ops = {
  1467. .init = tcf_action_net_init,
  1468. .exit = tcf_action_net_exit,
  1469. .id = &tcf_action_net_id,
  1470. .size = sizeof(struct tcf_action_net),
  1471. };
  1472. static int __init tc_action_init(void)
  1473. {
  1474. int err;
  1475. err = register_pernet_subsys(&tcf_action_net_ops);
  1476. if (err)
  1477. return err;
  1478. rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0);
  1479. rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0);
  1480. rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
  1481. 0);
  1482. return 0;
  1483. }
  1484. subsys_initcall(tc_action_init);