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