act_api.c 36 KB

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