act_api.c 24 KB

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