act_api.c 28 KB

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