act_api.c 28 KB

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