act_api.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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 <net/net_namespace.h>
  24. #include <net/sock.h>
  25. #include <net/sch_generic.h>
  26. #include <net/pkt_cls.h>
  27. #include <net/act_api.h>
  28. #include <net/netlink.h>
  29. static int tcf_action_goto_chain_init(struct tc_action *a, struct tcf_proto *tp)
  30. {
  31. u32 chain_index = a->tcfa_action & TC_ACT_EXT_VAL_MASK;
  32. if (!tp)
  33. return -EINVAL;
  34. a->goto_chain = tcf_chain_get(tp->chain->block, chain_index, true);
  35. if (!a->goto_chain)
  36. return -ENOMEM;
  37. return 0;
  38. }
  39. static void tcf_action_goto_chain_fini(struct tc_action *a)
  40. {
  41. tcf_chain_put(a->goto_chain);
  42. }
  43. static void tcf_action_goto_chain_exec(const struct tc_action *a,
  44. struct tcf_result *res)
  45. {
  46. const struct tcf_chain *chain = a->goto_chain;
  47. res->goto_tp = rcu_dereference_bh(chain->filter_chain);
  48. }
  49. static void free_tcf(struct rcu_head *head)
  50. {
  51. struct tc_action *p = container_of(head, struct tc_action, tcfa_rcu);
  52. free_percpu(p->cpu_bstats);
  53. free_percpu(p->cpu_qstats);
  54. if (p->act_cookie) {
  55. kfree(p->act_cookie->data);
  56. kfree(p->act_cookie);
  57. }
  58. if (p->goto_chain)
  59. tcf_action_goto_chain_fini(p);
  60. kfree(p);
  61. }
  62. static void tcf_hash_destroy(struct tcf_hashinfo *hinfo, struct tc_action *p)
  63. {
  64. spin_lock_bh(&hinfo->lock);
  65. hlist_del(&p->tcfa_head);
  66. spin_unlock_bh(&hinfo->lock);
  67. gen_kill_estimator(&p->tcfa_rate_est);
  68. /*
  69. * gen_estimator est_timer() might access p->tcfa_lock
  70. * or bstats, wait a RCU grace period before freeing p
  71. */
  72. call_rcu(&p->tcfa_rcu, free_tcf);
  73. }
  74. int __tcf_hash_release(struct tc_action *p, bool bind, bool strict)
  75. {
  76. int ret = 0;
  77. if (p) {
  78. if (bind)
  79. p->tcfa_bindcnt--;
  80. else if (strict && p->tcfa_bindcnt > 0)
  81. return -EPERM;
  82. p->tcfa_refcnt--;
  83. if (p->tcfa_bindcnt <= 0 && p->tcfa_refcnt <= 0) {
  84. if (p->ops->cleanup)
  85. p->ops->cleanup(p, bind);
  86. tcf_hash_destroy(p->hinfo, p);
  87. ret = ACT_P_DELETED;
  88. }
  89. }
  90. return ret;
  91. }
  92. EXPORT_SYMBOL(__tcf_hash_release);
  93. static int tcf_dump_walker(struct tcf_hashinfo *hinfo, struct sk_buff *skb,
  94. struct netlink_callback *cb)
  95. {
  96. int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
  97. struct nlattr *nest;
  98. spin_lock_bh(&hinfo->lock);
  99. s_i = cb->args[0];
  100. for (i = 0; i < (hinfo->hmask + 1); i++) {
  101. struct hlist_head *head;
  102. struct tc_action *p;
  103. head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
  104. hlist_for_each_entry_rcu(p, head, tcfa_head) {
  105. index++;
  106. if (index < s_i)
  107. continue;
  108. nest = nla_nest_start(skb, n_i);
  109. if (nest == NULL)
  110. goto nla_put_failure;
  111. err = tcf_action_dump_1(skb, p, 0, 0);
  112. if (err < 0) {
  113. index--;
  114. nlmsg_trim(skb, nest);
  115. goto done;
  116. }
  117. nla_nest_end(skb, nest);
  118. n_i++;
  119. if (n_i >= TCA_ACT_MAX_PRIO)
  120. goto done;
  121. }
  122. }
  123. done:
  124. spin_unlock_bh(&hinfo->lock);
  125. if (n_i)
  126. cb->args[0] += n_i;
  127. return n_i;
  128. nla_put_failure:
  129. nla_nest_cancel(skb, nest);
  130. goto done;
  131. }
  132. static int tcf_del_walker(struct tcf_hashinfo *hinfo, struct sk_buff *skb,
  133. const struct tc_action_ops *ops)
  134. {
  135. struct nlattr *nest;
  136. int i = 0, n_i = 0;
  137. int ret = -EINVAL;
  138. nest = nla_nest_start(skb, 0);
  139. if (nest == NULL)
  140. goto nla_put_failure;
  141. if (nla_put_string(skb, TCA_KIND, ops->kind))
  142. goto nla_put_failure;
  143. for (i = 0; i < (hinfo->hmask + 1); i++) {
  144. struct hlist_head *head;
  145. struct hlist_node *n;
  146. struct tc_action *p;
  147. head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
  148. hlist_for_each_entry_safe(p, n, head, tcfa_head) {
  149. ret = __tcf_hash_release(p, false, true);
  150. if (ret == ACT_P_DELETED) {
  151. module_put(p->ops->owner);
  152. n_i++;
  153. } else if (ret < 0)
  154. goto nla_put_failure;
  155. }
  156. }
  157. if (nla_put_u32(skb, TCA_FCNT, n_i))
  158. goto nla_put_failure;
  159. nla_nest_end(skb, nest);
  160. return n_i;
  161. nla_put_failure:
  162. nla_nest_cancel(skb, nest);
  163. return ret;
  164. }
  165. int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
  166. struct netlink_callback *cb, int type,
  167. const struct tc_action_ops *ops)
  168. {
  169. struct tcf_hashinfo *hinfo = tn->hinfo;
  170. if (type == RTM_DELACTION) {
  171. return tcf_del_walker(hinfo, skb, ops);
  172. } else if (type == RTM_GETACTION) {
  173. return tcf_dump_walker(hinfo, skb, cb);
  174. } else {
  175. WARN(1, "tcf_generic_walker: unknown action %d\n", type);
  176. return -EINVAL;
  177. }
  178. }
  179. EXPORT_SYMBOL(tcf_generic_walker);
  180. static struct tc_action *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
  181. {
  182. struct tc_action *p = NULL;
  183. struct hlist_head *head;
  184. spin_lock_bh(&hinfo->lock);
  185. head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
  186. hlist_for_each_entry_rcu(p, head, tcfa_head)
  187. if (p->tcfa_index == index)
  188. break;
  189. spin_unlock_bh(&hinfo->lock);
  190. return p;
  191. }
  192. u32 tcf_hash_new_index(struct tc_action_net *tn)
  193. {
  194. struct tcf_hashinfo *hinfo = tn->hinfo;
  195. u32 val = hinfo->index;
  196. do {
  197. if (++val == 0)
  198. val = 1;
  199. } while (tcf_hash_lookup(val, hinfo));
  200. hinfo->index = val;
  201. return val;
  202. }
  203. EXPORT_SYMBOL(tcf_hash_new_index);
  204. int tcf_hash_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
  205. {
  206. struct tcf_hashinfo *hinfo = tn->hinfo;
  207. struct tc_action *p = tcf_hash_lookup(index, hinfo);
  208. if (p) {
  209. *a = p;
  210. return 1;
  211. }
  212. return 0;
  213. }
  214. EXPORT_SYMBOL(tcf_hash_search);
  215. bool tcf_hash_check(struct tc_action_net *tn, u32 index, struct tc_action **a,
  216. int bind)
  217. {
  218. struct tcf_hashinfo *hinfo = tn->hinfo;
  219. struct tc_action *p = NULL;
  220. if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
  221. if (bind)
  222. p->tcfa_bindcnt++;
  223. p->tcfa_refcnt++;
  224. *a = p;
  225. return true;
  226. }
  227. return false;
  228. }
  229. EXPORT_SYMBOL(tcf_hash_check);
  230. void tcf_hash_cleanup(struct tc_action *a, struct nlattr *est)
  231. {
  232. if (est)
  233. gen_kill_estimator(&a->tcfa_rate_est);
  234. call_rcu(&a->tcfa_rcu, free_tcf);
  235. }
  236. EXPORT_SYMBOL(tcf_hash_cleanup);
  237. int tcf_hash_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
  238. struct tc_action **a, const struct tc_action_ops *ops,
  239. int bind, bool cpustats)
  240. {
  241. struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
  242. struct tcf_hashinfo *hinfo = tn->hinfo;
  243. int err = -ENOMEM;
  244. if (unlikely(!p))
  245. return -ENOMEM;
  246. p->tcfa_refcnt = 1;
  247. if (bind)
  248. p->tcfa_bindcnt = 1;
  249. if (cpustats) {
  250. p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
  251. if (!p->cpu_bstats) {
  252. err1:
  253. kfree(p);
  254. return err;
  255. }
  256. p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
  257. if (!p->cpu_qstats) {
  258. err2:
  259. free_percpu(p->cpu_bstats);
  260. goto err1;
  261. }
  262. }
  263. spin_lock_init(&p->tcfa_lock);
  264. INIT_HLIST_NODE(&p->tcfa_head);
  265. p->tcfa_index = index ? index : tcf_hash_new_index(tn);
  266. p->tcfa_tm.install = jiffies;
  267. p->tcfa_tm.lastuse = jiffies;
  268. p->tcfa_tm.firstuse = 0;
  269. if (est) {
  270. err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
  271. &p->tcfa_rate_est,
  272. &p->tcfa_lock, NULL, est);
  273. if (err) {
  274. free_percpu(p->cpu_qstats);
  275. goto err2;
  276. }
  277. }
  278. p->hinfo = hinfo;
  279. p->ops = ops;
  280. INIT_LIST_HEAD(&p->list);
  281. *a = p;
  282. return 0;
  283. }
  284. EXPORT_SYMBOL(tcf_hash_create);
  285. void tcf_hash_insert(struct tc_action_net *tn, struct tc_action *a)
  286. {
  287. struct tcf_hashinfo *hinfo = tn->hinfo;
  288. unsigned int h = tcf_hash(a->tcfa_index, hinfo->hmask);
  289. spin_lock_bh(&hinfo->lock);
  290. hlist_add_head(&a->tcfa_head, &hinfo->htab[h]);
  291. spin_unlock_bh(&hinfo->lock);
  292. }
  293. EXPORT_SYMBOL(tcf_hash_insert);
  294. void tcf_hashinfo_destroy(const struct tc_action_ops *ops,
  295. struct tcf_hashinfo *hinfo)
  296. {
  297. int i;
  298. for (i = 0; i < hinfo->hmask + 1; i++) {
  299. struct tc_action *p;
  300. struct hlist_node *n;
  301. hlist_for_each_entry_safe(p, n, &hinfo->htab[i], tcfa_head) {
  302. int ret;
  303. ret = __tcf_hash_release(p, false, true);
  304. if (ret == ACT_P_DELETED)
  305. module_put(ops->owner);
  306. else if (ret < 0)
  307. return;
  308. }
  309. }
  310. kfree(hinfo->htab);
  311. }
  312. EXPORT_SYMBOL(tcf_hashinfo_destroy);
  313. static LIST_HEAD(act_base);
  314. static DEFINE_RWLOCK(act_mod_lock);
  315. int tcf_register_action(struct tc_action_ops *act,
  316. struct pernet_operations *ops)
  317. {
  318. struct tc_action_ops *a;
  319. int ret;
  320. if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
  321. return -EINVAL;
  322. /* We have to register pernet ops before making the action ops visible,
  323. * otherwise tcf_action_init_1() could get a partially initialized
  324. * netns.
  325. */
  326. ret = register_pernet_subsys(ops);
  327. if (ret)
  328. return ret;
  329. write_lock(&act_mod_lock);
  330. list_for_each_entry(a, &act_base, head) {
  331. if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
  332. write_unlock(&act_mod_lock);
  333. unregister_pernet_subsys(ops);
  334. return -EEXIST;
  335. }
  336. }
  337. list_add_tail(&act->head, &act_base);
  338. write_unlock(&act_mod_lock);
  339. return 0;
  340. }
  341. EXPORT_SYMBOL(tcf_register_action);
  342. int tcf_unregister_action(struct tc_action_ops *act,
  343. struct pernet_operations *ops)
  344. {
  345. struct tc_action_ops *a;
  346. int err = -ENOENT;
  347. write_lock(&act_mod_lock);
  348. list_for_each_entry(a, &act_base, head) {
  349. if (a == act) {
  350. list_del(&act->head);
  351. err = 0;
  352. break;
  353. }
  354. }
  355. write_unlock(&act_mod_lock);
  356. if (!err)
  357. unregister_pernet_subsys(ops);
  358. return err;
  359. }
  360. EXPORT_SYMBOL(tcf_unregister_action);
  361. /* lookup by name */
  362. static struct tc_action_ops *tc_lookup_action_n(char *kind)
  363. {
  364. struct tc_action_ops *a, *res = NULL;
  365. if (kind) {
  366. read_lock(&act_mod_lock);
  367. list_for_each_entry(a, &act_base, head) {
  368. if (strcmp(kind, a->kind) == 0) {
  369. if (try_module_get(a->owner))
  370. res = a;
  371. break;
  372. }
  373. }
  374. read_unlock(&act_mod_lock);
  375. }
  376. return res;
  377. }
  378. /* lookup by nlattr */
  379. static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
  380. {
  381. struct tc_action_ops *a, *res = NULL;
  382. if (kind) {
  383. read_lock(&act_mod_lock);
  384. list_for_each_entry(a, &act_base, head) {
  385. if (nla_strcmp(kind, a->kind) == 0) {
  386. if (try_module_get(a->owner))
  387. res = a;
  388. break;
  389. }
  390. }
  391. read_unlock(&act_mod_lock);
  392. }
  393. return res;
  394. }
  395. /*TCA_ACT_MAX_PRIO is 32, there count upto 32 */
  396. #define TCA_ACT_MAX_PRIO_MASK 0x1FF
  397. int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
  398. int nr_actions, struct tcf_result *res)
  399. {
  400. int ret = -1, i;
  401. u32 jmp_prgcnt = 0;
  402. u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
  403. if (skb_skip_tc_classify(skb))
  404. return TC_ACT_OK;
  405. restart_act_graph:
  406. for (i = 0; i < nr_actions; i++) {
  407. const struct tc_action *a = actions[i];
  408. if (jmp_prgcnt > 0) {
  409. jmp_prgcnt -= 1;
  410. continue;
  411. }
  412. repeat:
  413. ret = a->ops->act(skb, a, res);
  414. if (ret == TC_ACT_REPEAT)
  415. goto repeat; /* we need a ttl - JHS */
  416. if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
  417. jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
  418. if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
  419. /* faulty opcode, stop pipeline */
  420. return TC_ACT_OK;
  421. } else {
  422. jmp_ttl -= 1;
  423. if (jmp_ttl > 0)
  424. goto restart_act_graph;
  425. else /* faulty graph, stop pipeline */
  426. return TC_ACT_OK;
  427. }
  428. } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
  429. tcf_action_goto_chain_exec(a, res);
  430. }
  431. if (ret != TC_ACT_PIPE)
  432. break;
  433. }
  434. return ret;
  435. }
  436. EXPORT_SYMBOL(tcf_action_exec);
  437. int tcf_action_destroy(struct list_head *actions, int bind)
  438. {
  439. struct tc_action *a, *tmp;
  440. int ret = 0;
  441. list_for_each_entry_safe(a, tmp, actions, list) {
  442. ret = __tcf_hash_release(a, bind, true);
  443. if (ret == ACT_P_DELETED)
  444. module_put(a->ops->owner);
  445. else if (ret < 0)
  446. return ret;
  447. }
  448. return ret;
  449. }
  450. int
  451. tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  452. {
  453. return a->ops->dump(skb, a, bind, ref);
  454. }
  455. int
  456. tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  457. {
  458. int err = -EINVAL;
  459. unsigned char *b = skb_tail_pointer(skb);
  460. struct nlattr *nest;
  461. if (nla_put_string(skb, TCA_KIND, a->ops->kind))
  462. goto nla_put_failure;
  463. if (tcf_action_copy_stats(skb, a, 0))
  464. goto nla_put_failure;
  465. if (a->act_cookie) {
  466. if (nla_put(skb, TCA_ACT_COOKIE, a->act_cookie->len,
  467. a->act_cookie->data))
  468. goto nla_put_failure;
  469. }
  470. nest = nla_nest_start(skb, TCA_OPTIONS);
  471. if (nest == NULL)
  472. goto nla_put_failure;
  473. err = tcf_action_dump_old(skb, a, bind, ref);
  474. if (err > 0) {
  475. nla_nest_end(skb, nest);
  476. return err;
  477. }
  478. nla_put_failure:
  479. nlmsg_trim(skb, b);
  480. return -1;
  481. }
  482. EXPORT_SYMBOL(tcf_action_dump_1);
  483. int tcf_action_dump(struct sk_buff *skb, struct list_head *actions,
  484. int bind, int ref)
  485. {
  486. struct tc_action *a;
  487. int err = -EINVAL;
  488. struct nlattr *nest;
  489. list_for_each_entry(a, actions, list) {
  490. nest = nla_nest_start(skb, a->order);
  491. if (nest == NULL)
  492. goto nla_put_failure;
  493. err = tcf_action_dump_1(skb, a, bind, ref);
  494. if (err < 0)
  495. goto errout;
  496. nla_nest_end(skb, nest);
  497. }
  498. return 0;
  499. nla_put_failure:
  500. err = -EINVAL;
  501. errout:
  502. nla_nest_cancel(skb, nest);
  503. return err;
  504. }
  505. static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
  506. {
  507. struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL);
  508. if (!c)
  509. return NULL;
  510. c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
  511. if (!c->data) {
  512. kfree(c);
  513. return NULL;
  514. }
  515. c->len = nla_len(tb[TCA_ACT_COOKIE]);
  516. return c;
  517. }
  518. struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
  519. struct nlattr *nla, struct nlattr *est,
  520. char *name, int ovr, int bind)
  521. {
  522. struct tc_action *a;
  523. struct tc_action_ops *a_o;
  524. struct tc_cookie *cookie = NULL;
  525. char act_name[IFNAMSIZ];
  526. struct nlattr *tb[TCA_ACT_MAX + 1];
  527. struct nlattr *kind;
  528. int err;
  529. if (name == NULL) {
  530. err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL);
  531. if (err < 0)
  532. goto err_out;
  533. err = -EINVAL;
  534. kind = tb[TCA_ACT_KIND];
  535. if (kind == NULL)
  536. goto err_out;
  537. if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
  538. goto err_out;
  539. if (tb[TCA_ACT_COOKIE]) {
  540. int cklen = nla_len(tb[TCA_ACT_COOKIE]);
  541. if (cklen > TC_COOKIE_MAX_SIZE)
  542. goto err_out;
  543. cookie = nla_memdup_cookie(tb);
  544. if (!cookie) {
  545. err = -ENOMEM;
  546. goto err_out;
  547. }
  548. }
  549. } else {
  550. err = -EINVAL;
  551. if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
  552. goto err_out;
  553. }
  554. a_o = tc_lookup_action_n(act_name);
  555. if (a_o == NULL) {
  556. #ifdef CONFIG_MODULES
  557. rtnl_unlock();
  558. request_module("act_%s", act_name);
  559. rtnl_lock();
  560. a_o = tc_lookup_action_n(act_name);
  561. /* We dropped the RTNL semaphore in order to
  562. * perform the module load. So, even if we
  563. * succeeded in loading the module we have to
  564. * tell the caller to replay the request. We
  565. * indicate this using -EAGAIN.
  566. */
  567. if (a_o != NULL) {
  568. err = -EAGAIN;
  569. goto err_mod;
  570. }
  571. #endif
  572. err = -ENOENT;
  573. goto err_out;
  574. }
  575. /* backward compatibility for policer */
  576. if (name == NULL)
  577. err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind);
  578. else
  579. err = a_o->init(net, nla, est, &a, ovr, bind);
  580. if (err < 0)
  581. goto err_mod;
  582. if (name == NULL && tb[TCA_ACT_COOKIE]) {
  583. if (a->act_cookie) {
  584. kfree(a->act_cookie->data);
  585. kfree(a->act_cookie);
  586. }
  587. a->act_cookie = cookie;
  588. }
  589. /* module count goes up only when brand new policy is created
  590. * if it exists and is only bound to in a_o->init() then
  591. * ACT_P_CREATED is not returned (a zero is).
  592. */
  593. if (err != ACT_P_CREATED)
  594. module_put(a_o->owner);
  595. if (TC_ACT_EXT_CMP(a->tcfa_action, TC_ACT_GOTO_CHAIN)) {
  596. err = tcf_action_goto_chain_init(a, tp);
  597. if (err) {
  598. LIST_HEAD(actions);
  599. list_add_tail(&a->list, &actions);
  600. tcf_action_destroy(&actions, bind);
  601. return ERR_PTR(err);
  602. }
  603. }
  604. return a;
  605. err_mod:
  606. module_put(a_o->owner);
  607. err_out:
  608. if (cookie) {
  609. kfree(cookie->data);
  610. kfree(cookie);
  611. }
  612. return ERR_PTR(err);
  613. }
  614. static void cleanup_a(struct list_head *actions, int ovr)
  615. {
  616. struct tc_action *a;
  617. if (!ovr)
  618. return;
  619. list_for_each_entry(a, actions, list)
  620. a->tcfa_refcnt--;
  621. }
  622. int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
  623. struct nlattr *est, char *name, int ovr, int bind,
  624. struct list_head *actions)
  625. {
  626. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  627. struct tc_action *act;
  628. int err;
  629. int i;
  630. err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, NULL);
  631. if (err < 0)
  632. return err;
  633. for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
  634. act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind);
  635. if (IS_ERR(act)) {
  636. err = PTR_ERR(act);
  637. goto err;
  638. }
  639. act->order = i;
  640. if (ovr)
  641. act->tcfa_refcnt++;
  642. list_add_tail(&act->list, actions);
  643. }
  644. /* Remove the temp refcnt which was necessary to protect against
  645. * destroying an existing action which was being replaced
  646. */
  647. cleanup_a(actions, ovr);
  648. return 0;
  649. err:
  650. tcf_action_destroy(actions, bind);
  651. return err;
  652. }
  653. int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
  654. int compat_mode)
  655. {
  656. int err = 0;
  657. struct gnet_dump d;
  658. if (p == NULL)
  659. goto errout;
  660. /* compat_mode being true specifies a call that is supposed
  661. * to add additional backward compatibility statistic TLVs.
  662. */
  663. if (compat_mode) {
  664. if (p->type == TCA_OLD_COMPAT)
  665. err = gnet_stats_start_copy_compat(skb, 0,
  666. TCA_STATS,
  667. TCA_XSTATS,
  668. &p->tcfa_lock, &d,
  669. TCA_PAD);
  670. else
  671. return 0;
  672. } else
  673. err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
  674. &p->tcfa_lock, &d, TCA_ACT_PAD);
  675. if (err < 0)
  676. goto errout;
  677. if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
  678. gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
  679. gnet_stats_copy_queue(&d, p->cpu_qstats,
  680. &p->tcfa_qstats,
  681. p->tcfa_qstats.qlen) < 0)
  682. goto errout;
  683. if (gnet_stats_finish_copy(&d) < 0)
  684. goto errout;
  685. return 0;
  686. errout:
  687. return -1;
  688. }
  689. static int tca_get_fill(struct sk_buff *skb, struct list_head *actions,
  690. u32 portid, u32 seq, u16 flags, int event, int bind,
  691. int ref)
  692. {
  693. struct tcamsg *t;
  694. struct nlmsghdr *nlh;
  695. unsigned char *b = skb_tail_pointer(skb);
  696. struct nlattr *nest;
  697. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
  698. if (!nlh)
  699. goto out_nlmsg_trim;
  700. t = nlmsg_data(nlh);
  701. t->tca_family = AF_UNSPEC;
  702. t->tca__pad1 = 0;
  703. t->tca__pad2 = 0;
  704. nest = nla_nest_start(skb, TCA_ACT_TAB);
  705. if (nest == NULL)
  706. goto out_nlmsg_trim;
  707. if (tcf_action_dump(skb, actions, bind, ref) < 0)
  708. goto out_nlmsg_trim;
  709. nla_nest_end(skb, nest);
  710. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  711. return skb->len;
  712. out_nlmsg_trim:
  713. nlmsg_trim(skb, b);
  714. return -1;
  715. }
  716. static int
  717. tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
  718. struct list_head *actions, int event)
  719. {
  720. struct sk_buff *skb;
  721. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  722. if (!skb)
  723. return -ENOBUFS;
  724. if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
  725. 0, 0) <= 0) {
  726. kfree_skb(skb);
  727. return -EINVAL;
  728. }
  729. return rtnl_unicast(skb, net, portid);
  730. }
  731. static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
  732. struct nlmsghdr *n, u32 portid)
  733. {
  734. struct nlattr *tb[TCA_ACT_MAX + 1];
  735. const struct tc_action_ops *ops;
  736. struct tc_action *a;
  737. int index;
  738. int err;
  739. err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL);
  740. if (err < 0)
  741. goto err_out;
  742. err = -EINVAL;
  743. if (tb[TCA_ACT_INDEX] == NULL ||
  744. nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
  745. goto err_out;
  746. index = nla_get_u32(tb[TCA_ACT_INDEX]);
  747. err = -EINVAL;
  748. ops = tc_lookup_action(tb[TCA_ACT_KIND]);
  749. if (!ops) /* could happen in batch of actions */
  750. goto err_out;
  751. err = -ENOENT;
  752. if (ops->lookup(net, &a, index) == 0)
  753. goto err_mod;
  754. module_put(ops->owner);
  755. return a;
  756. err_mod:
  757. module_put(ops->owner);
  758. err_out:
  759. return ERR_PTR(err);
  760. }
  761. static int tca_action_flush(struct net *net, struct nlattr *nla,
  762. struct nlmsghdr *n, u32 portid)
  763. {
  764. struct sk_buff *skb;
  765. unsigned char *b;
  766. struct nlmsghdr *nlh;
  767. struct tcamsg *t;
  768. struct netlink_callback dcb;
  769. struct nlattr *nest;
  770. struct nlattr *tb[TCA_ACT_MAX + 1];
  771. const struct tc_action_ops *ops;
  772. struct nlattr *kind;
  773. int err = -ENOMEM;
  774. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  775. if (!skb) {
  776. pr_debug("tca_action_flush: failed skb alloc\n");
  777. return err;
  778. }
  779. b = skb_tail_pointer(skb);
  780. err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL);
  781. if (err < 0)
  782. goto err_out;
  783. err = -EINVAL;
  784. kind = tb[TCA_ACT_KIND];
  785. ops = tc_lookup_action(kind);
  786. if (!ops) /*some idjot trying to flush unknown action */
  787. goto err_out;
  788. nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
  789. sizeof(*t), 0);
  790. if (!nlh)
  791. goto out_module_put;
  792. t = nlmsg_data(nlh);
  793. t->tca_family = AF_UNSPEC;
  794. t->tca__pad1 = 0;
  795. t->tca__pad2 = 0;
  796. nest = nla_nest_start(skb, TCA_ACT_TAB);
  797. if (nest == NULL)
  798. goto out_module_put;
  799. err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops);
  800. if (err <= 0)
  801. goto out_module_put;
  802. nla_nest_end(skb, nest);
  803. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  804. nlh->nlmsg_flags |= NLM_F_ROOT;
  805. module_put(ops->owner);
  806. err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  807. n->nlmsg_flags & NLM_F_ECHO);
  808. if (err > 0)
  809. return 0;
  810. return err;
  811. out_module_put:
  812. module_put(ops->owner);
  813. err_out:
  814. kfree_skb(skb);
  815. return err;
  816. }
  817. static int
  818. tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
  819. u32 portid)
  820. {
  821. int ret;
  822. struct sk_buff *skb;
  823. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  824. if (!skb)
  825. return -ENOBUFS;
  826. if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
  827. 0, 1) <= 0) {
  828. kfree_skb(skb);
  829. return -EINVAL;
  830. }
  831. /* now do the delete */
  832. ret = tcf_action_destroy(actions, 0);
  833. if (ret < 0) {
  834. kfree_skb(skb);
  835. return ret;
  836. }
  837. ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  838. n->nlmsg_flags & NLM_F_ECHO);
  839. if (ret > 0)
  840. return 0;
  841. return ret;
  842. }
  843. static int
  844. tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
  845. u32 portid, int event)
  846. {
  847. int i, ret;
  848. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  849. struct tc_action *act;
  850. LIST_HEAD(actions);
  851. ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, NULL);
  852. if (ret < 0)
  853. return ret;
  854. if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
  855. if (tb[1] != NULL)
  856. return tca_action_flush(net, tb[1], n, portid);
  857. else
  858. return -EINVAL;
  859. }
  860. for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
  861. act = tcf_action_get_1(net, tb[i], n, portid);
  862. if (IS_ERR(act)) {
  863. ret = PTR_ERR(act);
  864. goto err;
  865. }
  866. act->order = i;
  867. list_add_tail(&act->list, &actions);
  868. }
  869. if (event == RTM_GETACTION)
  870. ret = tcf_get_notify(net, portid, n, &actions, event);
  871. else { /* delete */
  872. ret = tcf_del_notify(net, n, &actions, portid);
  873. if (ret)
  874. goto err;
  875. return ret;
  876. }
  877. err:
  878. if (event != RTM_GETACTION)
  879. tcf_action_destroy(&actions, 0);
  880. return ret;
  881. }
  882. static int
  883. tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
  884. u32 portid)
  885. {
  886. struct sk_buff *skb;
  887. int err = 0;
  888. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  889. if (!skb)
  890. return -ENOBUFS;
  891. if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
  892. RTM_NEWACTION, 0, 0) <= 0) {
  893. kfree_skb(skb);
  894. return -EINVAL;
  895. }
  896. err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  897. n->nlmsg_flags & NLM_F_ECHO);
  898. if (err > 0)
  899. err = 0;
  900. return err;
  901. }
  902. static int tcf_action_add(struct net *net, struct nlattr *nla,
  903. struct nlmsghdr *n, u32 portid, int ovr)
  904. {
  905. int ret = 0;
  906. LIST_HEAD(actions);
  907. ret = tcf_action_init(net, NULL, nla, NULL, NULL, ovr, 0, &actions);
  908. if (ret)
  909. return ret;
  910. return tcf_add_notify(net, n, &actions, portid);
  911. }
  912. static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
  913. struct netlink_ext_ack *extack)
  914. {
  915. struct net *net = sock_net(skb->sk);
  916. struct nlattr *tca[TCA_ACT_MAX + 1];
  917. u32 portid = skb ? NETLINK_CB(skb).portid : 0;
  918. int ret = 0, ovr = 0;
  919. if ((n->nlmsg_type != RTM_GETACTION) &&
  920. !netlink_capable(skb, CAP_NET_ADMIN))
  921. return -EPERM;
  922. ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL,
  923. extack);
  924. if (ret < 0)
  925. return ret;
  926. if (tca[TCA_ACT_TAB] == NULL) {
  927. pr_notice("tc_ctl_action: received NO action attribs\n");
  928. return -EINVAL;
  929. }
  930. /* n->nlmsg_flags & NLM_F_CREATE */
  931. switch (n->nlmsg_type) {
  932. case RTM_NEWACTION:
  933. /* we are going to assume all other flags
  934. * imply create only if it doesn't exist
  935. * Note that CREATE | EXCL implies that
  936. * but since we want avoid ambiguity (eg when flags
  937. * is zero) then just set this
  938. */
  939. if (n->nlmsg_flags & NLM_F_REPLACE)
  940. ovr = 1;
  941. replay:
  942. ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
  943. if (ret == -EAGAIN)
  944. goto replay;
  945. break;
  946. case RTM_DELACTION:
  947. ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
  948. portid, RTM_DELACTION);
  949. break;
  950. case RTM_GETACTION:
  951. ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
  952. portid, RTM_GETACTION);
  953. break;
  954. default:
  955. BUG();
  956. }
  957. return ret;
  958. }
  959. static struct nlattr *find_dump_kind(const struct nlmsghdr *n)
  960. {
  961. struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
  962. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  963. struct nlattr *nla[TCAA_MAX + 1];
  964. struct nlattr *kind;
  965. if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX,
  966. NULL, NULL) < 0)
  967. return NULL;
  968. tb1 = nla[TCA_ACT_TAB];
  969. if (tb1 == NULL)
  970. return NULL;
  971. if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
  972. NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0)
  973. return NULL;
  974. if (tb[1] == NULL)
  975. return NULL;
  976. if (nla_parse_nested(tb2, TCA_ACT_MAX, tb[1], NULL, NULL) < 0)
  977. return NULL;
  978. kind = tb2[TCA_ACT_KIND];
  979. return kind;
  980. }
  981. static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
  982. {
  983. struct net *net = sock_net(skb->sk);
  984. struct nlmsghdr *nlh;
  985. unsigned char *b = skb_tail_pointer(skb);
  986. struct nlattr *nest;
  987. struct tc_action_ops *a_o;
  988. int ret = 0;
  989. struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
  990. struct nlattr *kind = find_dump_kind(cb->nlh);
  991. if (kind == NULL) {
  992. pr_info("tc_dump_action: action bad kind\n");
  993. return 0;
  994. }
  995. a_o = tc_lookup_action(kind);
  996. if (a_o == NULL)
  997. return 0;
  998. nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  999. cb->nlh->nlmsg_type, sizeof(*t), 0);
  1000. if (!nlh)
  1001. goto out_module_put;
  1002. t = nlmsg_data(nlh);
  1003. t->tca_family = AF_UNSPEC;
  1004. t->tca__pad1 = 0;
  1005. t->tca__pad2 = 0;
  1006. nest = nla_nest_start(skb, TCA_ACT_TAB);
  1007. if (nest == NULL)
  1008. goto out_module_put;
  1009. ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o);
  1010. if (ret < 0)
  1011. goto out_module_put;
  1012. if (ret > 0) {
  1013. nla_nest_end(skb, nest);
  1014. ret = skb->len;
  1015. } else
  1016. nlmsg_trim(skb, b);
  1017. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  1018. if (NETLINK_CB(cb->skb).portid && ret)
  1019. nlh->nlmsg_flags |= NLM_F_MULTI;
  1020. module_put(a_o->owner);
  1021. return skb->len;
  1022. out_module_put:
  1023. module_put(a_o->owner);
  1024. nlmsg_trim(skb, b);
  1025. return skb->len;
  1026. }
  1027. static int __init tc_action_init(void)
  1028. {
  1029. rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
  1030. rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
  1031. rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
  1032. NULL);
  1033. return 0;
  1034. }
  1035. subsys_initcall(tc_action_init);