act_api.c 24 KB

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