act_api.c 40 KB

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