act_api.c 24 KB

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