act_api.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720
  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_put(struct tc_action *p)
  575. {
  576. return __tcf_action_put(p, false);
  577. }
  578. /* Put all actions in this array, skip those NULL's. */
  579. static void tcf_action_put_many(struct tc_action *actions[])
  580. {
  581. int i;
  582. for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
  583. struct tc_action *a = actions[i];
  584. const struct tc_action_ops *ops;
  585. if (!a)
  586. continue;
  587. ops = a->ops;
  588. if (tcf_action_put(a))
  589. module_put(ops->owner);
  590. }
  591. }
  592. int
  593. tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  594. {
  595. return a->ops->dump(skb, a, bind, ref);
  596. }
  597. int
  598. tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  599. {
  600. int err = -EINVAL;
  601. unsigned char *b = skb_tail_pointer(skb);
  602. struct nlattr *nest;
  603. struct tc_cookie *cookie;
  604. if (nla_put_string(skb, TCA_KIND, a->ops->kind))
  605. goto nla_put_failure;
  606. if (tcf_action_copy_stats(skb, a, 0))
  607. goto nla_put_failure;
  608. rcu_read_lock();
  609. cookie = rcu_dereference(a->act_cookie);
  610. if (cookie) {
  611. if (nla_put(skb, TCA_ACT_COOKIE, cookie->len, cookie->data)) {
  612. rcu_read_unlock();
  613. goto nla_put_failure;
  614. }
  615. }
  616. rcu_read_unlock();
  617. nest = nla_nest_start(skb, TCA_OPTIONS);
  618. if (nest == NULL)
  619. goto nla_put_failure;
  620. err = tcf_action_dump_old(skb, a, bind, ref);
  621. if (err > 0) {
  622. nla_nest_end(skb, nest);
  623. return err;
  624. }
  625. nla_put_failure:
  626. nlmsg_trim(skb, b);
  627. return -1;
  628. }
  629. EXPORT_SYMBOL(tcf_action_dump_1);
  630. int tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[],
  631. int bind, int ref)
  632. {
  633. struct tc_action *a;
  634. int err = -EINVAL, i;
  635. struct nlattr *nest;
  636. for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
  637. a = actions[i];
  638. nest = nla_nest_start(skb, a->order);
  639. if (nest == NULL)
  640. goto nla_put_failure;
  641. err = tcf_action_dump_1(skb, a, bind, ref);
  642. if (err < 0)
  643. goto errout;
  644. nla_nest_end(skb, nest);
  645. }
  646. return 0;
  647. nla_put_failure:
  648. err = -EINVAL;
  649. errout:
  650. nla_nest_cancel(skb, nest);
  651. return err;
  652. }
  653. static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
  654. {
  655. struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL);
  656. if (!c)
  657. return NULL;
  658. c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
  659. if (!c->data) {
  660. kfree(c);
  661. return NULL;
  662. }
  663. c->len = nla_len(tb[TCA_ACT_COOKIE]);
  664. return c;
  665. }
  666. static bool tcf_action_valid(int action)
  667. {
  668. int opcode = TC_ACT_EXT_OPCODE(action);
  669. if (!opcode)
  670. return action <= TC_ACT_VALUE_MAX;
  671. return opcode <= TC_ACT_EXT_OPCODE_MAX || action == TC_ACT_UNSPEC;
  672. }
  673. struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
  674. struct nlattr *nla, struct nlattr *est,
  675. char *name, int ovr, int bind,
  676. bool rtnl_held,
  677. struct netlink_ext_ack *extack)
  678. {
  679. struct tc_action *a;
  680. struct tc_action_ops *a_o;
  681. struct tc_cookie *cookie = NULL;
  682. char act_name[IFNAMSIZ];
  683. struct nlattr *tb[TCA_ACT_MAX + 1];
  684. struct nlattr *kind;
  685. int err;
  686. if (name == NULL) {
  687. err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack);
  688. if (err < 0)
  689. goto err_out;
  690. err = -EINVAL;
  691. kind = tb[TCA_ACT_KIND];
  692. if (!kind) {
  693. NL_SET_ERR_MSG(extack, "TC action kind must be specified");
  694. goto err_out;
  695. }
  696. if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ) {
  697. NL_SET_ERR_MSG(extack, "TC action name too long");
  698. goto err_out;
  699. }
  700. if (tb[TCA_ACT_COOKIE]) {
  701. int cklen = nla_len(tb[TCA_ACT_COOKIE]);
  702. if (cklen > TC_COOKIE_MAX_SIZE) {
  703. NL_SET_ERR_MSG(extack, "TC cookie size above the maximum");
  704. goto err_out;
  705. }
  706. cookie = nla_memdup_cookie(tb);
  707. if (!cookie) {
  708. NL_SET_ERR_MSG(extack, "No memory to generate TC cookie");
  709. err = -ENOMEM;
  710. goto err_out;
  711. }
  712. }
  713. } else {
  714. if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ) {
  715. NL_SET_ERR_MSG(extack, "TC action name too long");
  716. err = -EINVAL;
  717. goto err_out;
  718. }
  719. }
  720. a_o = tc_lookup_action_n(act_name);
  721. if (a_o == NULL) {
  722. #ifdef CONFIG_MODULES
  723. if (rtnl_held)
  724. rtnl_unlock();
  725. request_module("act_%s", act_name);
  726. if (rtnl_held)
  727. rtnl_lock();
  728. a_o = tc_lookup_action_n(act_name);
  729. /* We dropped the RTNL semaphore in order to
  730. * perform the module load. So, even if we
  731. * succeeded in loading the module we have to
  732. * tell the caller to replay the request. We
  733. * indicate this using -EAGAIN.
  734. */
  735. if (a_o != NULL) {
  736. err = -EAGAIN;
  737. goto err_mod;
  738. }
  739. #endif
  740. NL_SET_ERR_MSG(extack, "Failed to load TC action module");
  741. err = -ENOENT;
  742. goto err_out;
  743. }
  744. /* backward compatibility for policer */
  745. if (name == NULL)
  746. err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind,
  747. rtnl_held, extack);
  748. else
  749. err = a_o->init(net, nla, est, &a, ovr, bind, rtnl_held,
  750. extack);
  751. if (err < 0)
  752. goto err_mod;
  753. if (!name && tb[TCA_ACT_COOKIE])
  754. tcf_set_action_cookie(&a->act_cookie, cookie);
  755. /* module count goes up only when brand new policy is created
  756. * if it exists and is only bound to in a_o->init() then
  757. * ACT_P_CREATED is not returned (a zero is).
  758. */
  759. if (err != ACT_P_CREATED)
  760. module_put(a_o->owner);
  761. if (TC_ACT_EXT_CMP(a->tcfa_action, TC_ACT_GOTO_CHAIN)) {
  762. err = tcf_action_goto_chain_init(a, tp);
  763. if (err) {
  764. struct tc_action *actions[] = { a, NULL };
  765. tcf_action_destroy(actions, bind);
  766. NL_SET_ERR_MSG(extack, "Failed to init TC action chain");
  767. return ERR_PTR(err);
  768. }
  769. }
  770. if (!tcf_action_valid(a->tcfa_action)) {
  771. NL_SET_ERR_MSG(extack, "invalid action value, using TC_ACT_UNSPEC instead");
  772. a->tcfa_action = TC_ACT_UNSPEC;
  773. }
  774. return a;
  775. err_mod:
  776. module_put(a_o->owner);
  777. err_out:
  778. if (cookie) {
  779. kfree(cookie->data);
  780. kfree(cookie);
  781. }
  782. return ERR_PTR(err);
  783. }
  784. /* Returns numbers of initialized actions or negative error. */
  785. int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
  786. struct nlattr *est, char *name, int ovr, int bind,
  787. struct tc_action *actions[], size_t *attr_size,
  788. bool rtnl_held, struct netlink_ext_ack *extack)
  789. {
  790. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  791. struct tc_action *act;
  792. size_t sz = 0;
  793. int err;
  794. int i;
  795. err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, extack);
  796. if (err < 0)
  797. return err;
  798. for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
  799. act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind,
  800. rtnl_held, extack);
  801. if (IS_ERR(act)) {
  802. err = PTR_ERR(act);
  803. goto err;
  804. }
  805. act->order = i;
  806. sz += tcf_action_fill_size(act);
  807. /* Start from index 0 */
  808. actions[i - 1] = act;
  809. }
  810. *attr_size = tcf_action_full_attrs_size(sz);
  811. return i - 1;
  812. err:
  813. tcf_action_destroy(actions, bind);
  814. return err;
  815. }
  816. int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
  817. int compat_mode)
  818. {
  819. int err = 0;
  820. struct gnet_dump d;
  821. if (p == NULL)
  822. goto errout;
  823. /* compat_mode being true specifies a call that is supposed
  824. * to add additional backward compatibility statistic TLVs.
  825. */
  826. if (compat_mode) {
  827. if (p->type == TCA_OLD_COMPAT)
  828. err = gnet_stats_start_copy_compat(skb, 0,
  829. TCA_STATS,
  830. TCA_XSTATS,
  831. &p->tcfa_lock, &d,
  832. TCA_PAD);
  833. else
  834. return 0;
  835. } else
  836. err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
  837. &p->tcfa_lock, &d, TCA_ACT_PAD);
  838. if (err < 0)
  839. goto errout;
  840. if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
  841. gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
  842. gnet_stats_copy_queue(&d, p->cpu_qstats,
  843. &p->tcfa_qstats,
  844. p->tcfa_qstats.qlen) < 0)
  845. goto errout;
  846. if (gnet_stats_finish_copy(&d) < 0)
  847. goto errout;
  848. return 0;
  849. errout:
  850. return -1;
  851. }
  852. static int tca_get_fill(struct sk_buff *skb, struct tc_action *actions[],
  853. u32 portid, u32 seq, u16 flags, int event, int bind,
  854. int ref)
  855. {
  856. struct tcamsg *t;
  857. struct nlmsghdr *nlh;
  858. unsigned char *b = skb_tail_pointer(skb);
  859. struct nlattr *nest;
  860. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
  861. if (!nlh)
  862. goto out_nlmsg_trim;
  863. t = nlmsg_data(nlh);
  864. t->tca_family = AF_UNSPEC;
  865. t->tca__pad1 = 0;
  866. t->tca__pad2 = 0;
  867. nest = nla_nest_start(skb, TCA_ACT_TAB);
  868. if (!nest)
  869. goto out_nlmsg_trim;
  870. if (tcf_action_dump(skb, actions, bind, ref) < 0)
  871. goto out_nlmsg_trim;
  872. nla_nest_end(skb, nest);
  873. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  874. return skb->len;
  875. out_nlmsg_trim:
  876. nlmsg_trim(skb, b);
  877. return -1;
  878. }
  879. static int
  880. tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
  881. struct tc_action *actions[], int event,
  882. struct netlink_ext_ack *extack)
  883. {
  884. struct sk_buff *skb;
  885. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  886. if (!skb)
  887. return -ENOBUFS;
  888. if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
  889. 0, 1) <= 0) {
  890. NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
  891. kfree_skb(skb);
  892. return -EINVAL;
  893. }
  894. return rtnl_unicast(skb, net, portid);
  895. }
  896. static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
  897. struct nlmsghdr *n, u32 portid,
  898. struct netlink_ext_ack *extack)
  899. {
  900. struct nlattr *tb[TCA_ACT_MAX + 1];
  901. const struct tc_action_ops *ops;
  902. struct tc_action *a;
  903. int index;
  904. int err;
  905. err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack);
  906. if (err < 0)
  907. goto err_out;
  908. err = -EINVAL;
  909. if (tb[TCA_ACT_INDEX] == NULL ||
  910. nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) {
  911. NL_SET_ERR_MSG(extack, "Invalid TC action index value");
  912. goto err_out;
  913. }
  914. index = nla_get_u32(tb[TCA_ACT_INDEX]);
  915. err = -EINVAL;
  916. ops = tc_lookup_action(tb[TCA_ACT_KIND]);
  917. if (!ops) { /* could happen in batch of actions */
  918. NL_SET_ERR_MSG(extack, "Specified TC action not found");
  919. goto err_out;
  920. }
  921. err = -ENOENT;
  922. if (ops->lookup(net, &a, index, extack) == 0)
  923. goto err_mod;
  924. module_put(ops->owner);
  925. return a;
  926. err_mod:
  927. module_put(ops->owner);
  928. err_out:
  929. return ERR_PTR(err);
  930. }
  931. static int tca_action_flush(struct net *net, struct nlattr *nla,
  932. struct nlmsghdr *n, u32 portid,
  933. struct netlink_ext_ack *extack)
  934. {
  935. struct sk_buff *skb;
  936. unsigned char *b;
  937. struct nlmsghdr *nlh;
  938. struct tcamsg *t;
  939. struct netlink_callback dcb;
  940. struct nlattr *nest;
  941. struct nlattr *tb[TCA_ACT_MAX + 1];
  942. const struct tc_action_ops *ops;
  943. struct nlattr *kind;
  944. int err = -ENOMEM;
  945. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  946. if (!skb)
  947. return err;
  948. b = skb_tail_pointer(skb);
  949. err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack);
  950. if (err < 0)
  951. goto err_out;
  952. err = -EINVAL;
  953. kind = tb[TCA_ACT_KIND];
  954. ops = tc_lookup_action(kind);
  955. if (!ops) { /*some idjot trying to flush unknown action */
  956. NL_SET_ERR_MSG(extack, "Cannot flush unknown TC action");
  957. goto err_out;
  958. }
  959. nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
  960. sizeof(*t), 0);
  961. if (!nlh) {
  962. NL_SET_ERR_MSG(extack, "Failed to create TC action flush notification");
  963. goto out_module_put;
  964. }
  965. t = nlmsg_data(nlh);
  966. t->tca_family = AF_UNSPEC;
  967. t->tca__pad1 = 0;
  968. t->tca__pad2 = 0;
  969. nest = nla_nest_start(skb, TCA_ACT_TAB);
  970. if (!nest) {
  971. NL_SET_ERR_MSG(extack, "Failed to add new netlink message");
  972. goto out_module_put;
  973. }
  974. err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops, extack);
  975. if (err <= 0) {
  976. nla_nest_cancel(skb, nest);
  977. goto out_module_put;
  978. }
  979. nla_nest_end(skb, nest);
  980. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  981. nlh->nlmsg_flags |= NLM_F_ROOT;
  982. module_put(ops->owner);
  983. err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  984. n->nlmsg_flags & NLM_F_ECHO);
  985. if (err > 0)
  986. return 0;
  987. if (err < 0)
  988. NL_SET_ERR_MSG(extack, "Failed to send TC action flush notification");
  989. return err;
  990. out_module_put:
  991. module_put(ops->owner);
  992. err_out:
  993. kfree_skb(skb);
  994. return err;
  995. }
  996. static int tcf_action_delete(struct net *net, struct tc_action *actions[])
  997. {
  998. int i;
  999. for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) {
  1000. struct tc_action *a = actions[i];
  1001. const struct tc_action_ops *ops = a->ops;
  1002. /* Actions can be deleted concurrently so we must save their
  1003. * type and id to search again after reference is released.
  1004. */
  1005. struct tcf_idrinfo *idrinfo = a->idrinfo;
  1006. u32 act_index = a->tcfa_index;
  1007. if (tcf_action_put(a)) {
  1008. /* last reference, action was deleted concurrently */
  1009. module_put(ops->owner);
  1010. } else {
  1011. int ret;
  1012. /* now do the delete */
  1013. ret = tcf_idr_delete_index(idrinfo, act_index);
  1014. if (ret < 0)
  1015. return ret;
  1016. }
  1017. actions[i] = NULL;
  1018. }
  1019. return 0;
  1020. }
  1021. static int
  1022. tcf_del_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
  1023. u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
  1024. {
  1025. int ret;
  1026. struct sk_buff *skb;
  1027. skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
  1028. GFP_KERNEL);
  1029. if (!skb)
  1030. return -ENOBUFS;
  1031. if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
  1032. 0, 2) <= 0) {
  1033. NL_SET_ERR_MSG(extack, "Failed to fill netlink TC action attributes");
  1034. kfree_skb(skb);
  1035. return -EINVAL;
  1036. }
  1037. /* now do the delete */
  1038. ret = tcf_action_delete(net, actions);
  1039. if (ret < 0) {
  1040. NL_SET_ERR_MSG(extack, "Failed to delete TC action");
  1041. kfree_skb(skb);
  1042. return ret;
  1043. }
  1044. ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  1045. n->nlmsg_flags & NLM_F_ECHO);
  1046. if (ret > 0)
  1047. return 0;
  1048. return ret;
  1049. }
  1050. static int
  1051. tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
  1052. u32 portid, int event, struct netlink_ext_ack *extack)
  1053. {
  1054. int i, ret;
  1055. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  1056. struct tc_action *act;
  1057. size_t attr_size = 0;
  1058. struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
  1059. ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, extack);
  1060. if (ret < 0)
  1061. return ret;
  1062. if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
  1063. if (tb[1])
  1064. return tca_action_flush(net, tb[1], n, portid, extack);
  1065. NL_SET_ERR_MSG(extack, "Invalid netlink attributes while flushing TC action");
  1066. return -EINVAL;
  1067. }
  1068. for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
  1069. act = tcf_action_get_1(net, tb[i], n, portid, extack);
  1070. if (IS_ERR(act)) {
  1071. ret = PTR_ERR(act);
  1072. goto err;
  1073. }
  1074. act->order = i;
  1075. attr_size += tcf_action_fill_size(act);
  1076. actions[i - 1] = act;
  1077. }
  1078. attr_size = tcf_action_full_attrs_size(attr_size);
  1079. if (event == RTM_GETACTION)
  1080. ret = tcf_get_notify(net, portid, n, actions, event, extack);
  1081. else { /* delete */
  1082. ret = tcf_del_notify(net, n, actions, portid, attr_size, extack);
  1083. if (ret)
  1084. goto err;
  1085. return 0;
  1086. }
  1087. err:
  1088. tcf_action_put_many(actions);
  1089. return ret;
  1090. }
  1091. static int
  1092. tcf_add_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[],
  1093. u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
  1094. {
  1095. struct sk_buff *skb;
  1096. int err = 0;
  1097. skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
  1098. GFP_KERNEL);
  1099. if (!skb)
  1100. return -ENOBUFS;
  1101. if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
  1102. RTM_NEWACTION, 0, 0) <= 0) {
  1103. NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
  1104. kfree_skb(skb);
  1105. return -EINVAL;
  1106. }
  1107. err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  1108. n->nlmsg_flags & NLM_F_ECHO);
  1109. if (err > 0)
  1110. err = 0;
  1111. return err;
  1112. }
  1113. static int tcf_action_add(struct net *net, struct nlattr *nla,
  1114. struct nlmsghdr *n, u32 portid, int ovr,
  1115. struct netlink_ext_ack *extack)
  1116. {
  1117. size_t attr_size = 0;
  1118. int ret = 0;
  1119. struct tc_action *actions[TCA_ACT_MAX_PRIO] = {};
  1120. ret = tcf_action_init(net, NULL, nla, NULL, NULL, ovr, 0, actions,
  1121. &attr_size, true, extack);
  1122. if (ret < 0)
  1123. return ret;
  1124. ret = tcf_add_notify(net, n, actions, portid, attr_size, extack);
  1125. if (ovr)
  1126. tcf_action_put_many(actions);
  1127. return ret;
  1128. }
  1129. static u32 tcaa_root_flags_allowed = TCA_FLAG_LARGE_DUMP_ON;
  1130. static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = {
  1131. [TCA_ROOT_FLAGS] = { .type = NLA_BITFIELD32,
  1132. .validation_data = &tcaa_root_flags_allowed },
  1133. [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 },
  1134. };
  1135. static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
  1136. struct netlink_ext_ack *extack)
  1137. {
  1138. struct net *net = sock_net(skb->sk);
  1139. struct nlattr *tca[TCA_ROOT_MAX + 1];
  1140. u32 portid = skb ? NETLINK_CB(skb).portid : 0;
  1141. int ret = 0, ovr = 0;
  1142. if ((n->nlmsg_type != RTM_GETACTION) &&
  1143. !netlink_capable(skb, CAP_NET_ADMIN))
  1144. return -EPERM;
  1145. ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ROOT_MAX, NULL,
  1146. extack);
  1147. if (ret < 0)
  1148. return ret;
  1149. if (tca[TCA_ACT_TAB] == NULL) {
  1150. NL_SET_ERR_MSG(extack, "Netlink action attributes missing");
  1151. return -EINVAL;
  1152. }
  1153. /* n->nlmsg_flags & NLM_F_CREATE */
  1154. switch (n->nlmsg_type) {
  1155. case RTM_NEWACTION:
  1156. /* we are going to assume all other flags
  1157. * imply create only if it doesn't exist
  1158. * Note that CREATE | EXCL implies that
  1159. * but since we want avoid ambiguity (eg when flags
  1160. * is zero) then just set this
  1161. */
  1162. if (n->nlmsg_flags & NLM_F_REPLACE)
  1163. ovr = 1;
  1164. replay:
  1165. ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr,
  1166. extack);
  1167. if (ret == -EAGAIN)
  1168. goto replay;
  1169. break;
  1170. case RTM_DELACTION:
  1171. ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
  1172. portid, RTM_DELACTION, extack);
  1173. break;
  1174. case RTM_GETACTION:
  1175. ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
  1176. portid, RTM_GETACTION, extack);
  1177. break;
  1178. default:
  1179. BUG();
  1180. }
  1181. return ret;
  1182. }
  1183. static struct nlattr *find_dump_kind(struct nlattr **nla)
  1184. {
  1185. struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
  1186. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  1187. struct nlattr *kind;
  1188. tb1 = nla[TCA_ACT_TAB];
  1189. if (tb1 == NULL)
  1190. return NULL;
  1191. if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
  1192. NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0)
  1193. return NULL;
  1194. if (tb[1] == NULL)
  1195. return NULL;
  1196. if (nla_parse_nested(tb2, TCA_ACT_MAX, tb[1], NULL, NULL) < 0)
  1197. return NULL;
  1198. kind = tb2[TCA_ACT_KIND];
  1199. return kind;
  1200. }
  1201. static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
  1202. {
  1203. struct net *net = sock_net(skb->sk);
  1204. struct nlmsghdr *nlh;
  1205. unsigned char *b = skb_tail_pointer(skb);
  1206. struct nlattr *nest;
  1207. struct tc_action_ops *a_o;
  1208. int ret = 0;
  1209. struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
  1210. struct nlattr *tb[TCA_ROOT_MAX + 1];
  1211. struct nlattr *count_attr = NULL;
  1212. unsigned long jiffy_since = 0;
  1213. struct nlattr *kind = NULL;
  1214. struct nla_bitfield32 bf;
  1215. u32 msecs_since = 0;
  1216. u32 act_count = 0;
  1217. ret = nlmsg_parse(cb->nlh, sizeof(struct tcamsg), tb, TCA_ROOT_MAX,
  1218. tcaa_policy, NULL);
  1219. if (ret < 0)
  1220. return ret;
  1221. kind = find_dump_kind(tb);
  1222. if (kind == NULL) {
  1223. pr_info("tc_dump_action: action bad kind\n");
  1224. return 0;
  1225. }
  1226. a_o = tc_lookup_action(kind);
  1227. if (a_o == NULL)
  1228. return 0;
  1229. cb->args[2] = 0;
  1230. if (tb[TCA_ROOT_FLAGS]) {
  1231. bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]);
  1232. cb->args[2] = bf.value;
  1233. }
  1234. if (tb[TCA_ROOT_TIME_DELTA]) {
  1235. msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]);
  1236. }
  1237. nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  1238. cb->nlh->nlmsg_type, sizeof(*t), 0);
  1239. if (!nlh)
  1240. goto out_module_put;
  1241. if (msecs_since)
  1242. jiffy_since = jiffies - msecs_to_jiffies(msecs_since);
  1243. t = nlmsg_data(nlh);
  1244. t->tca_family = AF_UNSPEC;
  1245. t->tca__pad1 = 0;
  1246. t->tca__pad2 = 0;
  1247. cb->args[3] = jiffy_since;
  1248. count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32));
  1249. if (!count_attr)
  1250. goto out_module_put;
  1251. nest = nla_nest_start(skb, TCA_ACT_TAB);
  1252. if (nest == NULL)
  1253. goto out_module_put;
  1254. ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o, NULL);
  1255. if (ret < 0)
  1256. goto out_module_put;
  1257. if (ret > 0) {
  1258. nla_nest_end(skb, nest);
  1259. ret = skb->len;
  1260. act_count = cb->args[1];
  1261. memcpy(nla_data(count_attr), &act_count, sizeof(u32));
  1262. cb->args[1] = 0;
  1263. } else
  1264. nlmsg_trim(skb, b);
  1265. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  1266. if (NETLINK_CB(cb->skb).portid && ret)
  1267. nlh->nlmsg_flags |= NLM_F_MULTI;
  1268. module_put(a_o->owner);
  1269. return skb->len;
  1270. out_module_put:
  1271. module_put(a_o->owner);
  1272. nlmsg_trim(skb, b);
  1273. return skb->len;
  1274. }
  1275. struct tcf_action_net {
  1276. struct rhashtable egdev_ht;
  1277. };
  1278. static unsigned int tcf_action_net_id;
  1279. struct tcf_action_egdev_cb {
  1280. struct list_head list;
  1281. tc_setup_cb_t *cb;
  1282. void *cb_priv;
  1283. };
  1284. struct tcf_action_egdev {
  1285. struct rhash_head ht_node;
  1286. const struct net_device *dev;
  1287. unsigned int refcnt;
  1288. struct list_head cb_list;
  1289. };
  1290. static const struct rhashtable_params tcf_action_egdev_ht_params = {
  1291. .key_offset = offsetof(struct tcf_action_egdev, dev),
  1292. .head_offset = offsetof(struct tcf_action_egdev, ht_node),
  1293. .key_len = sizeof(const struct net_device *),
  1294. };
  1295. static struct tcf_action_egdev *
  1296. tcf_action_egdev_lookup(const struct net_device *dev)
  1297. {
  1298. struct net *net = dev_net(dev);
  1299. struct tcf_action_net *tan = net_generic(net, tcf_action_net_id);
  1300. return rhashtable_lookup_fast(&tan->egdev_ht, &dev,
  1301. tcf_action_egdev_ht_params);
  1302. }
  1303. static struct tcf_action_egdev *
  1304. tcf_action_egdev_get(const struct net_device *dev)
  1305. {
  1306. struct tcf_action_egdev *egdev;
  1307. struct tcf_action_net *tan;
  1308. egdev = tcf_action_egdev_lookup(dev);
  1309. if (egdev)
  1310. goto inc_ref;
  1311. egdev = kzalloc(sizeof(*egdev), GFP_KERNEL);
  1312. if (!egdev)
  1313. return NULL;
  1314. INIT_LIST_HEAD(&egdev->cb_list);
  1315. egdev->dev = dev;
  1316. tan = net_generic(dev_net(dev), tcf_action_net_id);
  1317. rhashtable_insert_fast(&tan->egdev_ht, &egdev->ht_node,
  1318. tcf_action_egdev_ht_params);
  1319. inc_ref:
  1320. egdev->refcnt++;
  1321. return egdev;
  1322. }
  1323. static void tcf_action_egdev_put(struct tcf_action_egdev *egdev)
  1324. {
  1325. struct tcf_action_net *tan;
  1326. if (--egdev->refcnt)
  1327. return;
  1328. tan = net_generic(dev_net(egdev->dev), tcf_action_net_id);
  1329. rhashtable_remove_fast(&tan->egdev_ht, &egdev->ht_node,
  1330. tcf_action_egdev_ht_params);
  1331. kfree(egdev);
  1332. }
  1333. static struct tcf_action_egdev_cb *
  1334. tcf_action_egdev_cb_lookup(struct tcf_action_egdev *egdev,
  1335. tc_setup_cb_t *cb, void *cb_priv)
  1336. {
  1337. struct tcf_action_egdev_cb *egdev_cb;
  1338. list_for_each_entry(egdev_cb, &egdev->cb_list, list)
  1339. if (egdev_cb->cb == cb && egdev_cb->cb_priv == cb_priv)
  1340. return egdev_cb;
  1341. return NULL;
  1342. }
  1343. static int tcf_action_egdev_cb_call(struct tcf_action_egdev *egdev,
  1344. enum tc_setup_type type,
  1345. void *type_data, bool err_stop)
  1346. {
  1347. struct tcf_action_egdev_cb *egdev_cb;
  1348. int ok_count = 0;
  1349. int err;
  1350. list_for_each_entry(egdev_cb, &egdev->cb_list, list) {
  1351. err = egdev_cb->cb(type, type_data, egdev_cb->cb_priv);
  1352. if (err) {
  1353. if (err_stop)
  1354. return err;
  1355. } else {
  1356. ok_count++;
  1357. }
  1358. }
  1359. return ok_count;
  1360. }
  1361. static int tcf_action_egdev_cb_add(struct tcf_action_egdev *egdev,
  1362. tc_setup_cb_t *cb, void *cb_priv)
  1363. {
  1364. struct tcf_action_egdev_cb *egdev_cb;
  1365. egdev_cb = tcf_action_egdev_cb_lookup(egdev, cb, cb_priv);
  1366. if (WARN_ON(egdev_cb))
  1367. return -EEXIST;
  1368. egdev_cb = kzalloc(sizeof(*egdev_cb), GFP_KERNEL);
  1369. if (!egdev_cb)
  1370. return -ENOMEM;
  1371. egdev_cb->cb = cb;
  1372. egdev_cb->cb_priv = cb_priv;
  1373. list_add(&egdev_cb->list, &egdev->cb_list);
  1374. return 0;
  1375. }
  1376. static void tcf_action_egdev_cb_del(struct tcf_action_egdev *egdev,
  1377. tc_setup_cb_t *cb, void *cb_priv)
  1378. {
  1379. struct tcf_action_egdev_cb *egdev_cb;
  1380. egdev_cb = tcf_action_egdev_cb_lookup(egdev, cb, cb_priv);
  1381. if (WARN_ON(!egdev_cb))
  1382. return;
  1383. list_del(&egdev_cb->list);
  1384. kfree(egdev_cb);
  1385. }
  1386. static int __tc_setup_cb_egdev_register(const struct net_device *dev,
  1387. tc_setup_cb_t *cb, void *cb_priv)
  1388. {
  1389. struct tcf_action_egdev *egdev = tcf_action_egdev_get(dev);
  1390. int err;
  1391. if (!egdev)
  1392. return -ENOMEM;
  1393. err = tcf_action_egdev_cb_add(egdev, cb, cb_priv);
  1394. if (err)
  1395. goto err_cb_add;
  1396. return 0;
  1397. err_cb_add:
  1398. tcf_action_egdev_put(egdev);
  1399. return err;
  1400. }
  1401. int tc_setup_cb_egdev_register(const struct net_device *dev,
  1402. tc_setup_cb_t *cb, void *cb_priv)
  1403. {
  1404. int err;
  1405. rtnl_lock();
  1406. err = __tc_setup_cb_egdev_register(dev, cb, cb_priv);
  1407. rtnl_unlock();
  1408. return err;
  1409. }
  1410. EXPORT_SYMBOL_GPL(tc_setup_cb_egdev_register);
  1411. static void __tc_setup_cb_egdev_unregister(const struct net_device *dev,
  1412. tc_setup_cb_t *cb, void *cb_priv)
  1413. {
  1414. struct tcf_action_egdev *egdev = tcf_action_egdev_lookup(dev);
  1415. if (WARN_ON(!egdev))
  1416. return;
  1417. tcf_action_egdev_cb_del(egdev, cb, cb_priv);
  1418. tcf_action_egdev_put(egdev);
  1419. }
  1420. void tc_setup_cb_egdev_unregister(const struct net_device *dev,
  1421. tc_setup_cb_t *cb, void *cb_priv)
  1422. {
  1423. rtnl_lock();
  1424. __tc_setup_cb_egdev_unregister(dev, cb, cb_priv);
  1425. rtnl_unlock();
  1426. }
  1427. EXPORT_SYMBOL_GPL(tc_setup_cb_egdev_unregister);
  1428. int tc_setup_cb_egdev_call(const struct net_device *dev,
  1429. enum tc_setup_type type, void *type_data,
  1430. bool err_stop)
  1431. {
  1432. struct tcf_action_egdev *egdev = tcf_action_egdev_lookup(dev);
  1433. if (!egdev)
  1434. return 0;
  1435. return tcf_action_egdev_cb_call(egdev, type, type_data, err_stop);
  1436. }
  1437. EXPORT_SYMBOL_GPL(tc_setup_cb_egdev_call);
  1438. static __net_init int tcf_action_net_init(struct net *net)
  1439. {
  1440. struct tcf_action_net *tan = net_generic(net, tcf_action_net_id);
  1441. return rhashtable_init(&tan->egdev_ht, &tcf_action_egdev_ht_params);
  1442. }
  1443. static void __net_exit tcf_action_net_exit(struct net *net)
  1444. {
  1445. struct tcf_action_net *tan = net_generic(net, tcf_action_net_id);
  1446. rhashtable_destroy(&tan->egdev_ht);
  1447. }
  1448. static struct pernet_operations tcf_action_net_ops = {
  1449. .init = tcf_action_net_init,
  1450. .exit = tcf_action_net_exit,
  1451. .id = &tcf_action_net_id,
  1452. .size = sizeof(struct tcf_action_net),
  1453. };
  1454. static int __init tc_action_init(void)
  1455. {
  1456. int err;
  1457. err = register_pernet_subsys(&tcf_action_net_ops);
  1458. if (err)
  1459. return err;
  1460. rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0);
  1461. rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0);
  1462. rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
  1463. 0);
  1464. return 0;
  1465. }
  1466. subsys_initcall(tc_action_init);