act_api.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563
  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 <linux/rhashtable.h>
  24. #include <linux/list.h>
  25. #include <net/net_namespace.h>
  26. #include <net/sock.h>
  27. #include <net/sch_generic.h>
  28. #include <net/pkt_cls.h>
  29. #include <net/act_api.h>
  30. #include <net/netlink.h>
  31. static int tcf_action_goto_chain_init(struct tc_action *a, struct tcf_proto *tp)
  32. {
  33. u32 chain_index = a->tcfa_action & TC_ACT_EXT_VAL_MASK;
  34. if (!tp)
  35. return -EINVAL;
  36. a->goto_chain = tcf_chain_get(tp->chain->block, chain_index, true);
  37. if (!a->goto_chain)
  38. return -ENOMEM;
  39. return 0;
  40. }
  41. static void tcf_action_goto_chain_fini(struct tc_action *a)
  42. {
  43. tcf_chain_put(a->goto_chain);
  44. }
  45. static void tcf_action_goto_chain_exec(const struct tc_action *a,
  46. struct tcf_result *res)
  47. {
  48. const struct tcf_chain *chain = a->goto_chain;
  49. res->goto_tp = rcu_dereference_bh(chain->filter_chain);
  50. }
  51. /* XXX: For standalone actions, we don't need a RCU grace period either, because
  52. * actions are always connected to filters and filters are already destroyed in
  53. * RCU callbacks, so after a RCU grace period actions are already disconnected
  54. * from filters. Readers later can not find us.
  55. */
  56. static void free_tcf(struct tc_action *p)
  57. {
  58. free_percpu(p->cpu_bstats);
  59. free_percpu(p->cpu_qstats);
  60. if (p->act_cookie) {
  61. kfree(p->act_cookie->data);
  62. kfree(p->act_cookie);
  63. }
  64. if (p->goto_chain)
  65. tcf_action_goto_chain_fini(p);
  66. kfree(p);
  67. }
  68. static void tcf_idr_remove(struct tcf_idrinfo *idrinfo, struct tc_action *p)
  69. {
  70. spin_lock_bh(&idrinfo->lock);
  71. idr_remove(&idrinfo->action_idr, p->tcfa_index);
  72. spin_unlock_bh(&idrinfo->lock);
  73. gen_kill_estimator(&p->tcfa_rate_est);
  74. free_tcf(p);
  75. }
  76. int __tcf_idr_release(struct tc_action *p, bool bind, bool strict)
  77. {
  78. int ret = 0;
  79. ASSERT_RTNL();
  80. if (p) {
  81. if (bind)
  82. p->tcfa_bindcnt--;
  83. else if (strict && p->tcfa_bindcnt > 0)
  84. return -EPERM;
  85. p->tcfa_refcnt--;
  86. if (p->tcfa_bindcnt <= 0 && p->tcfa_refcnt <= 0) {
  87. if (p->ops->cleanup)
  88. p->ops->cleanup(p);
  89. tcf_idr_remove(p->idrinfo, p);
  90. ret = ACT_P_DELETED;
  91. }
  92. }
  93. return ret;
  94. }
  95. EXPORT_SYMBOL(__tcf_idr_release);
  96. static size_t tcf_action_shared_attrs_size(const struct tc_action *act)
  97. {
  98. u32 cookie_len = 0;
  99. if (act->act_cookie)
  100. cookie_len = nla_total_size(act->act_cookie->len);
  101. return nla_total_size(0) /* action number nested */
  102. + nla_total_size(IFNAMSIZ) /* TCA_ACT_KIND */
  103. + cookie_len /* TCA_ACT_COOKIE */
  104. + nla_total_size(0) /* TCA_ACT_STATS nested */
  105. /* TCA_STATS_BASIC */
  106. + nla_total_size_64bit(sizeof(struct gnet_stats_basic))
  107. /* TCA_STATS_QUEUE */
  108. + nla_total_size_64bit(sizeof(struct gnet_stats_queue))
  109. + nla_total_size(0) /* TCA_OPTIONS nested */
  110. + nla_total_size(sizeof(struct tcf_t)); /* TCA_GACT_TM */
  111. }
  112. static size_t tcf_action_full_attrs_size(size_t sz)
  113. {
  114. return NLMSG_HDRLEN /* struct nlmsghdr */
  115. + sizeof(struct tcamsg)
  116. + nla_total_size(0) /* TCA_ACT_TAB nested */
  117. + sz;
  118. }
  119. static size_t tcf_action_fill_size(const struct tc_action *act)
  120. {
  121. size_t sz = tcf_action_shared_attrs_size(act);
  122. if (act->ops->get_fill_size)
  123. return act->ops->get_fill_size(act) + sz;
  124. return sz;
  125. }
  126. static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
  127. struct netlink_callback *cb)
  128. {
  129. int err = 0, index = -1, s_i = 0, n_i = 0;
  130. u32 act_flags = cb->args[2];
  131. unsigned long jiffy_since = cb->args[3];
  132. struct nlattr *nest;
  133. struct idr *idr = &idrinfo->action_idr;
  134. struct tc_action *p;
  135. unsigned long id = 1;
  136. spin_lock_bh(&idrinfo->lock);
  137. s_i = cb->args[0];
  138. idr_for_each_entry_ul(idr, p, id) {
  139. index++;
  140. if (index < s_i)
  141. continue;
  142. if (jiffy_since &&
  143. time_after(jiffy_since,
  144. (unsigned long)p->tcfa_tm.lastuse))
  145. continue;
  146. nest = nla_nest_start(skb, n_i);
  147. if (!nest)
  148. goto nla_put_failure;
  149. err = tcf_action_dump_1(skb, p, 0, 0);
  150. if (err < 0) {
  151. index--;
  152. nlmsg_trim(skb, nest);
  153. goto done;
  154. }
  155. nla_nest_end(skb, nest);
  156. n_i++;
  157. if (!(act_flags & TCA_FLAG_LARGE_DUMP_ON) &&
  158. n_i >= TCA_ACT_MAX_PRIO)
  159. goto done;
  160. }
  161. done:
  162. if (index >= 0)
  163. cb->args[0] = index + 1;
  164. spin_unlock_bh(&idrinfo->lock);
  165. if (n_i) {
  166. if (act_flags & TCA_FLAG_LARGE_DUMP_ON)
  167. cb->args[1] = n_i;
  168. }
  169. return n_i;
  170. nla_put_failure:
  171. nla_nest_cancel(skb, nest);
  172. goto done;
  173. }
  174. static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
  175. const struct tc_action_ops *ops)
  176. {
  177. struct nlattr *nest;
  178. int n_i = 0;
  179. int ret = -EINVAL;
  180. struct idr *idr = &idrinfo->action_idr;
  181. struct tc_action *p;
  182. unsigned long id = 1;
  183. nest = nla_nest_start(skb, 0);
  184. if (nest == NULL)
  185. goto nla_put_failure;
  186. if (nla_put_string(skb, TCA_KIND, ops->kind))
  187. goto nla_put_failure;
  188. idr_for_each_entry_ul(idr, p, id) {
  189. ret = __tcf_idr_release(p, false, true);
  190. if (ret == ACT_P_DELETED) {
  191. module_put(ops->owner);
  192. n_i++;
  193. } else if (ret < 0) {
  194. goto nla_put_failure;
  195. }
  196. }
  197. if (nla_put_u32(skb, TCA_FCNT, n_i))
  198. goto nla_put_failure;
  199. nla_nest_end(skb, nest);
  200. return n_i;
  201. nla_put_failure:
  202. nla_nest_cancel(skb, nest);
  203. return ret;
  204. }
  205. int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
  206. struct netlink_callback *cb, int type,
  207. const struct tc_action_ops *ops,
  208. struct netlink_ext_ack *extack)
  209. {
  210. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  211. if (type == RTM_DELACTION) {
  212. return tcf_del_walker(idrinfo, skb, ops);
  213. } else if (type == RTM_GETACTION) {
  214. return tcf_dump_walker(idrinfo, skb, cb);
  215. } else {
  216. WARN(1, "tcf_generic_walker: unknown command %d\n", type);
  217. NL_SET_ERR_MSG(extack, "tcf_generic_walker: unknown command");
  218. return -EINVAL;
  219. }
  220. }
  221. EXPORT_SYMBOL(tcf_generic_walker);
  222. static struct tc_action *tcf_idr_lookup(u32 index, struct tcf_idrinfo *idrinfo)
  223. {
  224. struct tc_action *p = NULL;
  225. spin_lock_bh(&idrinfo->lock);
  226. p = idr_find(&idrinfo->action_idr, index);
  227. spin_unlock_bh(&idrinfo->lock);
  228. return p;
  229. }
  230. int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
  231. {
  232. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  233. struct tc_action *p = tcf_idr_lookup(index, idrinfo);
  234. if (p) {
  235. *a = p;
  236. return 1;
  237. }
  238. return 0;
  239. }
  240. EXPORT_SYMBOL(tcf_idr_search);
  241. bool tcf_idr_check(struct tc_action_net *tn, u32 index, struct tc_action **a,
  242. int bind)
  243. {
  244. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  245. struct tc_action *p = tcf_idr_lookup(index, idrinfo);
  246. if (index && p) {
  247. if (bind)
  248. p->tcfa_bindcnt++;
  249. p->tcfa_refcnt++;
  250. *a = p;
  251. return true;
  252. }
  253. return false;
  254. }
  255. EXPORT_SYMBOL(tcf_idr_check);
  256. void tcf_idr_cleanup(struct tc_action *a, struct nlattr *est)
  257. {
  258. if (est)
  259. gen_kill_estimator(&a->tcfa_rate_est);
  260. free_tcf(a);
  261. }
  262. EXPORT_SYMBOL(tcf_idr_cleanup);
  263. int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
  264. struct tc_action **a, const struct tc_action_ops *ops,
  265. int bind, bool cpustats)
  266. {
  267. struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
  268. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  269. struct idr *idr = &idrinfo->action_idr;
  270. int err = -ENOMEM;
  271. if (unlikely(!p))
  272. return -ENOMEM;
  273. p->tcfa_refcnt = 1;
  274. if (bind)
  275. p->tcfa_bindcnt = 1;
  276. if (cpustats) {
  277. p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
  278. if (!p->cpu_bstats)
  279. goto err1;
  280. p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
  281. if (!p->cpu_qstats)
  282. goto err2;
  283. }
  284. spin_lock_init(&p->tcfa_lock);
  285. idr_preload(GFP_KERNEL);
  286. spin_lock_bh(&idrinfo->lock);
  287. /* user doesn't specify an index */
  288. if (!index) {
  289. index = 1;
  290. err = idr_alloc_u32(idr, NULL, &index, UINT_MAX, GFP_ATOMIC);
  291. } else {
  292. err = idr_alloc_u32(idr, NULL, &index, index, GFP_ATOMIC);
  293. }
  294. spin_unlock_bh(&idrinfo->lock);
  295. idr_preload_end();
  296. if (err)
  297. goto err3;
  298. p->tcfa_index = index;
  299. p->tcfa_tm.install = jiffies;
  300. p->tcfa_tm.lastuse = jiffies;
  301. p->tcfa_tm.firstuse = 0;
  302. if (est) {
  303. err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
  304. &p->tcfa_rate_est,
  305. &p->tcfa_lock, NULL, est);
  306. if (err)
  307. goto err4;
  308. }
  309. p->idrinfo = idrinfo;
  310. p->ops = ops;
  311. INIT_LIST_HEAD(&p->list);
  312. *a = p;
  313. return 0;
  314. err4:
  315. idr_remove(idr, index);
  316. err3:
  317. free_percpu(p->cpu_qstats);
  318. err2:
  319. free_percpu(p->cpu_bstats);
  320. err1:
  321. kfree(p);
  322. return err;
  323. }
  324. EXPORT_SYMBOL(tcf_idr_create);
  325. void tcf_idr_insert(struct tc_action_net *tn, struct tc_action *a)
  326. {
  327. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  328. spin_lock_bh(&idrinfo->lock);
  329. idr_replace(&idrinfo->action_idr, a, a->tcfa_index);
  330. spin_unlock_bh(&idrinfo->lock);
  331. }
  332. EXPORT_SYMBOL(tcf_idr_insert);
  333. void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
  334. struct tcf_idrinfo *idrinfo)
  335. {
  336. struct idr *idr = &idrinfo->action_idr;
  337. struct tc_action *p;
  338. int ret;
  339. unsigned long id = 1;
  340. idr_for_each_entry_ul(idr, p, id) {
  341. ret = __tcf_idr_release(p, false, true);
  342. if (ret == ACT_P_DELETED)
  343. module_put(ops->owner);
  344. else if (ret < 0)
  345. return;
  346. }
  347. idr_destroy(&idrinfo->action_idr);
  348. }
  349. EXPORT_SYMBOL(tcf_idrinfo_destroy);
  350. static LIST_HEAD(act_base);
  351. static DEFINE_RWLOCK(act_mod_lock);
  352. int tcf_register_action(struct tc_action_ops *act,
  353. struct pernet_operations *ops)
  354. {
  355. struct tc_action_ops *a;
  356. int ret;
  357. if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
  358. return -EINVAL;
  359. /* We have to register pernet ops before making the action ops visible,
  360. * otherwise tcf_action_init_1() could get a partially initialized
  361. * netns.
  362. */
  363. ret = register_pernet_subsys(ops);
  364. if (ret)
  365. return ret;
  366. write_lock(&act_mod_lock);
  367. list_for_each_entry(a, &act_base, head) {
  368. if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
  369. write_unlock(&act_mod_lock);
  370. unregister_pernet_subsys(ops);
  371. return -EEXIST;
  372. }
  373. }
  374. list_add_tail(&act->head, &act_base);
  375. write_unlock(&act_mod_lock);
  376. return 0;
  377. }
  378. EXPORT_SYMBOL(tcf_register_action);
  379. int tcf_unregister_action(struct tc_action_ops *act,
  380. struct pernet_operations *ops)
  381. {
  382. struct tc_action_ops *a;
  383. int err = -ENOENT;
  384. write_lock(&act_mod_lock);
  385. list_for_each_entry(a, &act_base, head) {
  386. if (a == act) {
  387. list_del(&act->head);
  388. err = 0;
  389. break;
  390. }
  391. }
  392. write_unlock(&act_mod_lock);
  393. if (!err)
  394. unregister_pernet_subsys(ops);
  395. return err;
  396. }
  397. EXPORT_SYMBOL(tcf_unregister_action);
  398. /* lookup by name */
  399. static struct tc_action_ops *tc_lookup_action_n(char *kind)
  400. {
  401. struct tc_action_ops *a, *res = NULL;
  402. if (kind) {
  403. read_lock(&act_mod_lock);
  404. list_for_each_entry(a, &act_base, head) {
  405. if (strcmp(kind, a->kind) == 0) {
  406. if (try_module_get(a->owner))
  407. res = a;
  408. break;
  409. }
  410. }
  411. read_unlock(&act_mod_lock);
  412. }
  413. return res;
  414. }
  415. /* lookup by nlattr */
  416. static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
  417. {
  418. struct tc_action_ops *a, *res = NULL;
  419. if (kind) {
  420. read_lock(&act_mod_lock);
  421. list_for_each_entry(a, &act_base, head) {
  422. if (nla_strcmp(kind, a->kind) == 0) {
  423. if (try_module_get(a->owner))
  424. res = a;
  425. break;
  426. }
  427. }
  428. read_unlock(&act_mod_lock);
  429. }
  430. return res;
  431. }
  432. /*TCA_ACT_MAX_PRIO is 32, there count upto 32 */
  433. #define TCA_ACT_MAX_PRIO_MASK 0x1FF
  434. int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
  435. int nr_actions, struct tcf_result *res)
  436. {
  437. u32 jmp_prgcnt = 0;
  438. u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
  439. int i;
  440. int ret = TC_ACT_OK;
  441. if (skb_skip_tc_classify(skb))
  442. return TC_ACT_OK;
  443. restart_act_graph:
  444. for (i = 0; i < nr_actions; i++) {
  445. const struct tc_action *a = actions[i];
  446. if (jmp_prgcnt > 0) {
  447. jmp_prgcnt -= 1;
  448. continue;
  449. }
  450. repeat:
  451. ret = a->ops->act(skb, a, res);
  452. if (ret == TC_ACT_REPEAT)
  453. goto repeat; /* we need a ttl - JHS */
  454. if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
  455. jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
  456. if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
  457. /* faulty opcode, stop pipeline */
  458. return TC_ACT_OK;
  459. } else {
  460. jmp_ttl -= 1;
  461. if (jmp_ttl > 0)
  462. goto restart_act_graph;
  463. else /* faulty graph, stop pipeline */
  464. return TC_ACT_OK;
  465. }
  466. } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
  467. tcf_action_goto_chain_exec(a, res);
  468. }
  469. if (ret != TC_ACT_PIPE)
  470. break;
  471. }
  472. return ret;
  473. }
  474. EXPORT_SYMBOL(tcf_action_exec);
  475. int tcf_action_destroy(struct list_head *actions, int bind)
  476. {
  477. const struct tc_action_ops *ops;
  478. struct tc_action *a, *tmp;
  479. int ret = 0;
  480. list_for_each_entry_safe(a, tmp, actions, list) {
  481. ops = a->ops;
  482. ret = __tcf_idr_release(a, bind, true);
  483. if (ret == ACT_P_DELETED)
  484. module_put(ops->owner);
  485. else if (ret < 0)
  486. return ret;
  487. }
  488. return ret;
  489. }
  490. int
  491. tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  492. {
  493. return a->ops->dump(skb, a, bind, ref);
  494. }
  495. int
  496. tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  497. {
  498. int err = -EINVAL;
  499. unsigned char *b = skb_tail_pointer(skb);
  500. struct nlattr *nest;
  501. if (nla_put_string(skb, TCA_KIND, a->ops->kind))
  502. goto nla_put_failure;
  503. if (tcf_action_copy_stats(skb, a, 0))
  504. goto nla_put_failure;
  505. if (a->act_cookie) {
  506. if (nla_put(skb, TCA_ACT_COOKIE, a->act_cookie->len,
  507. a->act_cookie->data))
  508. goto nla_put_failure;
  509. }
  510. nest = nla_nest_start(skb, TCA_OPTIONS);
  511. if (nest == NULL)
  512. goto nla_put_failure;
  513. err = tcf_action_dump_old(skb, a, bind, ref);
  514. if (err > 0) {
  515. nla_nest_end(skb, nest);
  516. return err;
  517. }
  518. nla_put_failure:
  519. nlmsg_trim(skb, b);
  520. return -1;
  521. }
  522. EXPORT_SYMBOL(tcf_action_dump_1);
  523. int tcf_action_dump(struct sk_buff *skb, struct list_head *actions,
  524. int bind, int ref)
  525. {
  526. struct tc_action *a;
  527. int err = -EINVAL;
  528. struct nlattr *nest;
  529. list_for_each_entry(a, actions, list) {
  530. nest = nla_nest_start(skb, a->order);
  531. if (nest == NULL)
  532. goto nla_put_failure;
  533. err = tcf_action_dump_1(skb, a, bind, ref);
  534. if (err < 0)
  535. goto errout;
  536. nla_nest_end(skb, nest);
  537. }
  538. return 0;
  539. nla_put_failure:
  540. err = -EINVAL;
  541. errout:
  542. nla_nest_cancel(skb, nest);
  543. return err;
  544. }
  545. static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
  546. {
  547. struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL);
  548. if (!c)
  549. return NULL;
  550. c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
  551. if (!c->data) {
  552. kfree(c);
  553. return NULL;
  554. }
  555. c->len = nla_len(tb[TCA_ACT_COOKIE]);
  556. return c;
  557. }
  558. struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
  559. struct nlattr *nla, struct nlattr *est,
  560. char *name, int ovr, int bind,
  561. struct netlink_ext_ack *extack)
  562. {
  563. struct tc_action *a;
  564. struct tc_action_ops *a_o;
  565. struct tc_cookie *cookie = NULL;
  566. char act_name[IFNAMSIZ];
  567. struct nlattr *tb[TCA_ACT_MAX + 1];
  568. struct nlattr *kind;
  569. int err;
  570. if (name == NULL) {
  571. err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack);
  572. if (err < 0)
  573. goto err_out;
  574. err = -EINVAL;
  575. kind = tb[TCA_ACT_KIND];
  576. if (!kind) {
  577. NL_SET_ERR_MSG(extack, "TC action kind must be specified");
  578. goto err_out;
  579. }
  580. if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ) {
  581. NL_SET_ERR_MSG(extack, "TC action name too long");
  582. goto err_out;
  583. }
  584. if (tb[TCA_ACT_COOKIE]) {
  585. int cklen = nla_len(tb[TCA_ACT_COOKIE]);
  586. if (cklen > TC_COOKIE_MAX_SIZE) {
  587. NL_SET_ERR_MSG(extack, "TC cookie size above the maximum");
  588. goto err_out;
  589. }
  590. cookie = nla_memdup_cookie(tb);
  591. if (!cookie) {
  592. NL_SET_ERR_MSG(extack, "No memory to generate TC cookie");
  593. err = -ENOMEM;
  594. goto err_out;
  595. }
  596. }
  597. } else {
  598. if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ) {
  599. NL_SET_ERR_MSG(extack, "TC action name too long");
  600. err = -EINVAL;
  601. goto err_out;
  602. }
  603. }
  604. a_o = tc_lookup_action_n(act_name);
  605. if (a_o == NULL) {
  606. #ifdef CONFIG_MODULES
  607. rtnl_unlock();
  608. request_module("act_%s", act_name);
  609. rtnl_lock();
  610. a_o = tc_lookup_action_n(act_name);
  611. /* We dropped the RTNL semaphore in order to
  612. * perform the module load. So, even if we
  613. * succeeded in loading the module we have to
  614. * tell the caller to replay the request. We
  615. * indicate this using -EAGAIN.
  616. */
  617. if (a_o != NULL) {
  618. err = -EAGAIN;
  619. goto err_mod;
  620. }
  621. #endif
  622. NL_SET_ERR_MSG(extack, "Failed to load TC action module");
  623. err = -ENOENT;
  624. goto err_out;
  625. }
  626. /* backward compatibility for policer */
  627. if (name == NULL)
  628. err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind,
  629. extack);
  630. else
  631. err = a_o->init(net, nla, est, &a, ovr, bind, extack);
  632. if (err < 0)
  633. goto err_mod;
  634. if (name == NULL && tb[TCA_ACT_COOKIE]) {
  635. if (a->act_cookie) {
  636. kfree(a->act_cookie->data);
  637. kfree(a->act_cookie);
  638. }
  639. a->act_cookie = cookie;
  640. }
  641. /* module count goes up only when brand new policy is created
  642. * if it exists and is only bound to in a_o->init() then
  643. * ACT_P_CREATED is not returned (a zero is).
  644. */
  645. if (err != ACT_P_CREATED)
  646. module_put(a_o->owner);
  647. if (TC_ACT_EXT_CMP(a->tcfa_action, TC_ACT_GOTO_CHAIN)) {
  648. err = tcf_action_goto_chain_init(a, tp);
  649. if (err) {
  650. LIST_HEAD(actions);
  651. list_add_tail(&a->list, &actions);
  652. tcf_action_destroy(&actions, bind);
  653. NL_SET_ERR_MSG(extack, "Failed to init TC action chain");
  654. return ERR_PTR(err);
  655. }
  656. }
  657. return a;
  658. err_mod:
  659. module_put(a_o->owner);
  660. err_out:
  661. if (cookie) {
  662. kfree(cookie->data);
  663. kfree(cookie);
  664. }
  665. return ERR_PTR(err);
  666. }
  667. static void cleanup_a(struct list_head *actions, int ovr)
  668. {
  669. struct tc_action *a;
  670. if (!ovr)
  671. return;
  672. list_for_each_entry(a, actions, list)
  673. a->tcfa_refcnt--;
  674. }
  675. int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
  676. struct nlattr *est, char *name, int ovr, int bind,
  677. struct list_head *actions, size_t *attr_size,
  678. struct netlink_ext_ack *extack)
  679. {
  680. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  681. struct tc_action *act;
  682. size_t sz = 0;
  683. int err;
  684. int i;
  685. err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, extack);
  686. if (err < 0)
  687. return err;
  688. for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
  689. act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind,
  690. extack);
  691. if (IS_ERR(act)) {
  692. err = PTR_ERR(act);
  693. goto err;
  694. }
  695. act->order = i;
  696. sz += tcf_action_fill_size(act);
  697. if (ovr)
  698. act->tcfa_refcnt++;
  699. list_add_tail(&act->list, actions);
  700. }
  701. *attr_size = tcf_action_full_attrs_size(sz);
  702. /* Remove the temp refcnt which was necessary to protect against
  703. * destroying an existing action which was being replaced
  704. */
  705. cleanup_a(actions, ovr);
  706. return 0;
  707. err:
  708. tcf_action_destroy(actions, bind);
  709. return err;
  710. }
  711. int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
  712. int compat_mode)
  713. {
  714. int err = 0;
  715. struct gnet_dump d;
  716. if (p == NULL)
  717. goto errout;
  718. /* compat_mode being true specifies a call that is supposed
  719. * to add additional backward compatibility statistic TLVs.
  720. */
  721. if (compat_mode) {
  722. if (p->type == TCA_OLD_COMPAT)
  723. err = gnet_stats_start_copy_compat(skb, 0,
  724. TCA_STATS,
  725. TCA_XSTATS,
  726. &p->tcfa_lock, &d,
  727. TCA_PAD);
  728. else
  729. return 0;
  730. } else
  731. err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
  732. &p->tcfa_lock, &d, TCA_ACT_PAD);
  733. if (err < 0)
  734. goto errout;
  735. if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
  736. gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
  737. gnet_stats_copy_queue(&d, p->cpu_qstats,
  738. &p->tcfa_qstats,
  739. p->tcfa_qstats.qlen) < 0)
  740. goto errout;
  741. if (gnet_stats_finish_copy(&d) < 0)
  742. goto errout;
  743. return 0;
  744. errout:
  745. return -1;
  746. }
  747. static int tca_get_fill(struct sk_buff *skb, struct list_head *actions,
  748. u32 portid, u32 seq, u16 flags, int event, int bind,
  749. int ref)
  750. {
  751. struct tcamsg *t;
  752. struct nlmsghdr *nlh;
  753. unsigned char *b = skb_tail_pointer(skb);
  754. struct nlattr *nest;
  755. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
  756. if (!nlh)
  757. goto out_nlmsg_trim;
  758. t = nlmsg_data(nlh);
  759. t->tca_family = AF_UNSPEC;
  760. t->tca__pad1 = 0;
  761. t->tca__pad2 = 0;
  762. nest = nla_nest_start(skb, TCA_ACT_TAB);
  763. if (!nest)
  764. goto out_nlmsg_trim;
  765. if (tcf_action_dump(skb, actions, bind, ref) < 0)
  766. goto out_nlmsg_trim;
  767. nla_nest_end(skb, nest);
  768. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  769. return skb->len;
  770. out_nlmsg_trim:
  771. nlmsg_trim(skb, b);
  772. return -1;
  773. }
  774. static int
  775. tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
  776. struct list_head *actions, int event,
  777. struct netlink_ext_ack *extack)
  778. {
  779. struct sk_buff *skb;
  780. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  781. if (!skb)
  782. return -ENOBUFS;
  783. if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
  784. 0, 0) <= 0) {
  785. NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
  786. kfree_skb(skb);
  787. return -EINVAL;
  788. }
  789. return rtnl_unicast(skb, net, portid);
  790. }
  791. static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
  792. struct nlmsghdr *n, u32 portid,
  793. struct netlink_ext_ack *extack)
  794. {
  795. struct nlattr *tb[TCA_ACT_MAX + 1];
  796. const struct tc_action_ops *ops;
  797. struct tc_action *a;
  798. int index;
  799. int err;
  800. err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack);
  801. if (err < 0)
  802. goto err_out;
  803. err = -EINVAL;
  804. if (tb[TCA_ACT_INDEX] == NULL ||
  805. nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) {
  806. NL_SET_ERR_MSG(extack, "Invalid TC action index value");
  807. goto err_out;
  808. }
  809. index = nla_get_u32(tb[TCA_ACT_INDEX]);
  810. err = -EINVAL;
  811. ops = tc_lookup_action(tb[TCA_ACT_KIND]);
  812. if (!ops) { /* could happen in batch of actions */
  813. NL_SET_ERR_MSG(extack, "Specified TC action not found");
  814. goto err_out;
  815. }
  816. err = -ENOENT;
  817. if (ops->lookup(net, &a, index, extack) == 0)
  818. goto err_mod;
  819. module_put(ops->owner);
  820. return a;
  821. err_mod:
  822. module_put(ops->owner);
  823. err_out:
  824. return ERR_PTR(err);
  825. }
  826. static int tca_action_flush(struct net *net, struct nlattr *nla,
  827. struct nlmsghdr *n, u32 portid,
  828. struct netlink_ext_ack *extack)
  829. {
  830. struct sk_buff *skb;
  831. unsigned char *b;
  832. struct nlmsghdr *nlh;
  833. struct tcamsg *t;
  834. struct netlink_callback dcb;
  835. struct nlattr *nest;
  836. struct nlattr *tb[TCA_ACT_MAX + 1];
  837. const struct tc_action_ops *ops;
  838. struct nlattr *kind;
  839. int err = -ENOMEM;
  840. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  841. if (!skb)
  842. return err;
  843. b = skb_tail_pointer(skb);
  844. err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack);
  845. if (err < 0)
  846. goto err_out;
  847. err = -EINVAL;
  848. kind = tb[TCA_ACT_KIND];
  849. ops = tc_lookup_action(kind);
  850. if (!ops) { /*some idjot trying to flush unknown action */
  851. NL_SET_ERR_MSG(extack, "Cannot flush unknown TC action");
  852. goto err_out;
  853. }
  854. nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
  855. sizeof(*t), 0);
  856. if (!nlh) {
  857. NL_SET_ERR_MSG(extack, "Failed to create TC action flush notification");
  858. goto out_module_put;
  859. }
  860. t = nlmsg_data(nlh);
  861. t->tca_family = AF_UNSPEC;
  862. t->tca__pad1 = 0;
  863. t->tca__pad2 = 0;
  864. nest = nla_nest_start(skb, TCA_ACT_TAB);
  865. if (!nest) {
  866. NL_SET_ERR_MSG(extack, "Failed to add new netlink message");
  867. goto out_module_put;
  868. }
  869. err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops, extack);
  870. if (err <= 0) {
  871. nla_nest_cancel(skb, nest);
  872. goto out_module_put;
  873. }
  874. nla_nest_end(skb, nest);
  875. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  876. nlh->nlmsg_flags |= NLM_F_ROOT;
  877. module_put(ops->owner);
  878. err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  879. n->nlmsg_flags & NLM_F_ECHO);
  880. if (err > 0)
  881. return 0;
  882. if (err < 0)
  883. NL_SET_ERR_MSG(extack, "Failed to send TC action flush notification");
  884. return err;
  885. out_module_put:
  886. module_put(ops->owner);
  887. err_out:
  888. kfree_skb(skb);
  889. return err;
  890. }
  891. static int
  892. tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
  893. u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
  894. {
  895. int ret;
  896. struct sk_buff *skb;
  897. skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
  898. GFP_KERNEL);
  899. if (!skb)
  900. return -ENOBUFS;
  901. if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
  902. 0, 1) <= 0) {
  903. NL_SET_ERR_MSG(extack, "Failed to fill netlink TC action attributes");
  904. kfree_skb(skb);
  905. return -EINVAL;
  906. }
  907. /* now do the delete */
  908. ret = tcf_action_destroy(actions, 0);
  909. if (ret < 0) {
  910. NL_SET_ERR_MSG(extack, "Failed to delete TC action");
  911. kfree_skb(skb);
  912. return ret;
  913. }
  914. ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  915. n->nlmsg_flags & NLM_F_ECHO);
  916. if (ret > 0)
  917. return 0;
  918. return ret;
  919. }
  920. static int
  921. tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
  922. u32 portid, int event, struct netlink_ext_ack *extack)
  923. {
  924. int i, ret;
  925. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  926. struct tc_action *act;
  927. size_t attr_size = 0;
  928. LIST_HEAD(actions);
  929. ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, extack);
  930. if (ret < 0)
  931. return ret;
  932. if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
  933. if (tb[1])
  934. return tca_action_flush(net, tb[1], n, portid, extack);
  935. NL_SET_ERR_MSG(extack, "Invalid netlink attributes while flushing TC action");
  936. return -EINVAL;
  937. }
  938. for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
  939. act = tcf_action_get_1(net, tb[i], n, portid, extack);
  940. if (IS_ERR(act)) {
  941. ret = PTR_ERR(act);
  942. goto err;
  943. }
  944. act->order = i;
  945. attr_size += tcf_action_fill_size(act);
  946. list_add_tail(&act->list, &actions);
  947. }
  948. attr_size = tcf_action_full_attrs_size(attr_size);
  949. if (event == RTM_GETACTION)
  950. ret = tcf_get_notify(net, portid, n, &actions, event, extack);
  951. else { /* delete */
  952. ret = tcf_del_notify(net, n, &actions, portid, attr_size, extack);
  953. if (ret)
  954. goto err;
  955. return ret;
  956. }
  957. err:
  958. if (event != RTM_GETACTION)
  959. tcf_action_destroy(&actions, 0);
  960. return ret;
  961. }
  962. static int
  963. tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
  964. u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
  965. {
  966. struct sk_buff *skb;
  967. int err = 0;
  968. skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
  969. GFP_KERNEL);
  970. if (!skb)
  971. return -ENOBUFS;
  972. if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
  973. RTM_NEWACTION, 0, 0) <= 0) {
  974. NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
  975. kfree_skb(skb);
  976. return -EINVAL;
  977. }
  978. err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  979. n->nlmsg_flags & NLM_F_ECHO);
  980. if (err > 0)
  981. err = 0;
  982. return err;
  983. }
  984. static int tcf_action_add(struct net *net, struct nlattr *nla,
  985. struct nlmsghdr *n, u32 portid, int ovr,
  986. struct netlink_ext_ack *extack)
  987. {
  988. size_t attr_size = 0;
  989. int ret = 0;
  990. LIST_HEAD(actions);
  991. ret = tcf_action_init(net, NULL, nla, NULL, NULL, ovr, 0, &actions,
  992. &attr_size, extack);
  993. if (ret)
  994. return ret;
  995. return tcf_add_notify(net, n, &actions, portid, attr_size, extack);
  996. }
  997. static u32 tcaa_root_flags_allowed = TCA_FLAG_LARGE_DUMP_ON;
  998. static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = {
  999. [TCA_ROOT_FLAGS] = { .type = NLA_BITFIELD32,
  1000. .validation_data = &tcaa_root_flags_allowed },
  1001. [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 },
  1002. };
  1003. static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
  1004. struct netlink_ext_ack *extack)
  1005. {
  1006. struct net *net = sock_net(skb->sk);
  1007. struct nlattr *tca[TCA_ROOT_MAX + 1];
  1008. u32 portid = skb ? NETLINK_CB(skb).portid : 0;
  1009. int ret = 0, ovr = 0;
  1010. if ((n->nlmsg_type != RTM_GETACTION) &&
  1011. !netlink_capable(skb, CAP_NET_ADMIN))
  1012. return -EPERM;
  1013. ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ROOT_MAX, NULL,
  1014. extack);
  1015. if (ret < 0)
  1016. return ret;
  1017. if (tca[TCA_ACT_TAB] == NULL) {
  1018. NL_SET_ERR_MSG(extack, "Netlink action attributes missing");
  1019. return -EINVAL;
  1020. }
  1021. /* n->nlmsg_flags & NLM_F_CREATE */
  1022. switch (n->nlmsg_type) {
  1023. case RTM_NEWACTION:
  1024. /* we are going to assume all other flags
  1025. * imply create only if it doesn't exist
  1026. * Note that CREATE | EXCL implies that
  1027. * but since we want avoid ambiguity (eg when flags
  1028. * is zero) then just set this
  1029. */
  1030. if (n->nlmsg_flags & NLM_F_REPLACE)
  1031. ovr = 1;
  1032. replay:
  1033. ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr,
  1034. extack);
  1035. if (ret == -EAGAIN)
  1036. goto replay;
  1037. break;
  1038. case RTM_DELACTION:
  1039. ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
  1040. portid, RTM_DELACTION, extack);
  1041. break;
  1042. case RTM_GETACTION:
  1043. ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
  1044. portid, RTM_GETACTION, extack);
  1045. break;
  1046. default:
  1047. BUG();
  1048. }
  1049. return ret;
  1050. }
  1051. static struct nlattr *find_dump_kind(struct nlattr **nla)
  1052. {
  1053. struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
  1054. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  1055. struct nlattr *kind;
  1056. tb1 = nla[TCA_ACT_TAB];
  1057. if (tb1 == NULL)
  1058. return NULL;
  1059. if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
  1060. NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0)
  1061. return NULL;
  1062. if (tb[1] == NULL)
  1063. return NULL;
  1064. if (nla_parse_nested(tb2, TCA_ACT_MAX, tb[1], NULL, NULL) < 0)
  1065. return NULL;
  1066. kind = tb2[TCA_ACT_KIND];
  1067. return kind;
  1068. }
  1069. static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
  1070. {
  1071. struct net *net = sock_net(skb->sk);
  1072. struct nlmsghdr *nlh;
  1073. unsigned char *b = skb_tail_pointer(skb);
  1074. struct nlattr *nest;
  1075. struct tc_action_ops *a_o;
  1076. int ret = 0;
  1077. struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
  1078. struct nlattr *tb[TCA_ROOT_MAX + 1];
  1079. struct nlattr *count_attr = NULL;
  1080. unsigned long jiffy_since = 0;
  1081. struct nlattr *kind = NULL;
  1082. struct nla_bitfield32 bf;
  1083. u32 msecs_since = 0;
  1084. u32 act_count = 0;
  1085. ret = nlmsg_parse(cb->nlh, sizeof(struct tcamsg), tb, TCA_ROOT_MAX,
  1086. tcaa_policy, NULL);
  1087. if (ret < 0)
  1088. return ret;
  1089. kind = find_dump_kind(tb);
  1090. if (kind == NULL) {
  1091. pr_info("tc_dump_action: action bad kind\n");
  1092. return 0;
  1093. }
  1094. a_o = tc_lookup_action(kind);
  1095. if (a_o == NULL)
  1096. return 0;
  1097. cb->args[2] = 0;
  1098. if (tb[TCA_ROOT_FLAGS]) {
  1099. bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]);
  1100. cb->args[2] = bf.value;
  1101. }
  1102. if (tb[TCA_ROOT_TIME_DELTA]) {
  1103. msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]);
  1104. }
  1105. nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  1106. cb->nlh->nlmsg_type, sizeof(*t), 0);
  1107. if (!nlh)
  1108. goto out_module_put;
  1109. if (msecs_since)
  1110. jiffy_since = jiffies - msecs_to_jiffies(msecs_since);
  1111. t = nlmsg_data(nlh);
  1112. t->tca_family = AF_UNSPEC;
  1113. t->tca__pad1 = 0;
  1114. t->tca__pad2 = 0;
  1115. cb->args[3] = jiffy_since;
  1116. count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32));
  1117. if (!count_attr)
  1118. goto out_module_put;
  1119. nest = nla_nest_start(skb, TCA_ACT_TAB);
  1120. if (nest == NULL)
  1121. goto out_module_put;
  1122. ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o, NULL);
  1123. if (ret < 0)
  1124. goto out_module_put;
  1125. if (ret > 0) {
  1126. nla_nest_end(skb, nest);
  1127. ret = skb->len;
  1128. act_count = cb->args[1];
  1129. memcpy(nla_data(count_attr), &act_count, sizeof(u32));
  1130. cb->args[1] = 0;
  1131. } else
  1132. nlmsg_trim(skb, b);
  1133. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  1134. if (NETLINK_CB(cb->skb).portid && ret)
  1135. nlh->nlmsg_flags |= NLM_F_MULTI;
  1136. module_put(a_o->owner);
  1137. return skb->len;
  1138. out_module_put:
  1139. module_put(a_o->owner);
  1140. nlmsg_trim(skb, b);
  1141. return skb->len;
  1142. }
  1143. struct tcf_action_net {
  1144. struct rhashtable egdev_ht;
  1145. };
  1146. static unsigned int tcf_action_net_id;
  1147. struct tcf_action_egdev_cb {
  1148. struct list_head list;
  1149. tc_setup_cb_t *cb;
  1150. void *cb_priv;
  1151. };
  1152. struct tcf_action_egdev {
  1153. struct rhash_head ht_node;
  1154. const struct net_device *dev;
  1155. unsigned int refcnt;
  1156. struct list_head cb_list;
  1157. };
  1158. static const struct rhashtable_params tcf_action_egdev_ht_params = {
  1159. .key_offset = offsetof(struct tcf_action_egdev, dev),
  1160. .head_offset = offsetof(struct tcf_action_egdev, ht_node),
  1161. .key_len = sizeof(const struct net_device *),
  1162. };
  1163. static struct tcf_action_egdev *
  1164. tcf_action_egdev_lookup(const struct net_device *dev)
  1165. {
  1166. struct net *net = dev_net(dev);
  1167. struct tcf_action_net *tan = net_generic(net, tcf_action_net_id);
  1168. return rhashtable_lookup_fast(&tan->egdev_ht, &dev,
  1169. tcf_action_egdev_ht_params);
  1170. }
  1171. static struct tcf_action_egdev *
  1172. tcf_action_egdev_get(const struct net_device *dev)
  1173. {
  1174. struct tcf_action_egdev *egdev;
  1175. struct tcf_action_net *tan;
  1176. egdev = tcf_action_egdev_lookup(dev);
  1177. if (egdev)
  1178. goto inc_ref;
  1179. egdev = kzalloc(sizeof(*egdev), GFP_KERNEL);
  1180. if (!egdev)
  1181. return NULL;
  1182. INIT_LIST_HEAD(&egdev->cb_list);
  1183. egdev->dev = dev;
  1184. tan = net_generic(dev_net(dev), tcf_action_net_id);
  1185. rhashtable_insert_fast(&tan->egdev_ht, &egdev->ht_node,
  1186. tcf_action_egdev_ht_params);
  1187. inc_ref:
  1188. egdev->refcnt++;
  1189. return egdev;
  1190. }
  1191. static void tcf_action_egdev_put(struct tcf_action_egdev *egdev)
  1192. {
  1193. struct tcf_action_net *tan;
  1194. if (--egdev->refcnt)
  1195. return;
  1196. tan = net_generic(dev_net(egdev->dev), tcf_action_net_id);
  1197. rhashtable_remove_fast(&tan->egdev_ht, &egdev->ht_node,
  1198. tcf_action_egdev_ht_params);
  1199. kfree(egdev);
  1200. }
  1201. static struct tcf_action_egdev_cb *
  1202. tcf_action_egdev_cb_lookup(struct tcf_action_egdev *egdev,
  1203. tc_setup_cb_t *cb, void *cb_priv)
  1204. {
  1205. struct tcf_action_egdev_cb *egdev_cb;
  1206. list_for_each_entry(egdev_cb, &egdev->cb_list, list)
  1207. if (egdev_cb->cb == cb && egdev_cb->cb_priv == cb_priv)
  1208. return egdev_cb;
  1209. return NULL;
  1210. }
  1211. static int tcf_action_egdev_cb_call(struct tcf_action_egdev *egdev,
  1212. enum tc_setup_type type,
  1213. void *type_data, bool err_stop)
  1214. {
  1215. struct tcf_action_egdev_cb *egdev_cb;
  1216. int ok_count = 0;
  1217. int err;
  1218. list_for_each_entry(egdev_cb, &egdev->cb_list, list) {
  1219. err = egdev_cb->cb(type, type_data, egdev_cb->cb_priv);
  1220. if (err) {
  1221. if (err_stop)
  1222. return err;
  1223. } else {
  1224. ok_count++;
  1225. }
  1226. }
  1227. return ok_count;
  1228. }
  1229. static int tcf_action_egdev_cb_add(struct tcf_action_egdev *egdev,
  1230. tc_setup_cb_t *cb, void *cb_priv)
  1231. {
  1232. struct tcf_action_egdev_cb *egdev_cb;
  1233. egdev_cb = tcf_action_egdev_cb_lookup(egdev, cb, cb_priv);
  1234. if (WARN_ON(egdev_cb))
  1235. return -EEXIST;
  1236. egdev_cb = kzalloc(sizeof(*egdev_cb), GFP_KERNEL);
  1237. if (!egdev_cb)
  1238. return -ENOMEM;
  1239. egdev_cb->cb = cb;
  1240. egdev_cb->cb_priv = cb_priv;
  1241. list_add(&egdev_cb->list, &egdev->cb_list);
  1242. return 0;
  1243. }
  1244. static void tcf_action_egdev_cb_del(struct tcf_action_egdev *egdev,
  1245. tc_setup_cb_t *cb, void *cb_priv)
  1246. {
  1247. struct tcf_action_egdev_cb *egdev_cb;
  1248. egdev_cb = tcf_action_egdev_cb_lookup(egdev, cb, cb_priv);
  1249. if (WARN_ON(!egdev_cb))
  1250. return;
  1251. list_del(&egdev_cb->list);
  1252. kfree(egdev_cb);
  1253. }
  1254. static int __tc_setup_cb_egdev_register(const struct net_device *dev,
  1255. tc_setup_cb_t *cb, void *cb_priv)
  1256. {
  1257. struct tcf_action_egdev *egdev = tcf_action_egdev_get(dev);
  1258. int err;
  1259. if (!egdev)
  1260. return -ENOMEM;
  1261. err = tcf_action_egdev_cb_add(egdev, cb, cb_priv);
  1262. if (err)
  1263. goto err_cb_add;
  1264. return 0;
  1265. err_cb_add:
  1266. tcf_action_egdev_put(egdev);
  1267. return err;
  1268. }
  1269. int tc_setup_cb_egdev_register(const struct net_device *dev,
  1270. tc_setup_cb_t *cb, void *cb_priv)
  1271. {
  1272. int err;
  1273. rtnl_lock();
  1274. err = __tc_setup_cb_egdev_register(dev, cb, cb_priv);
  1275. rtnl_unlock();
  1276. return err;
  1277. }
  1278. EXPORT_SYMBOL_GPL(tc_setup_cb_egdev_register);
  1279. static void __tc_setup_cb_egdev_unregister(const struct net_device *dev,
  1280. tc_setup_cb_t *cb, void *cb_priv)
  1281. {
  1282. struct tcf_action_egdev *egdev = tcf_action_egdev_lookup(dev);
  1283. if (WARN_ON(!egdev))
  1284. return;
  1285. tcf_action_egdev_cb_del(egdev, cb, cb_priv);
  1286. tcf_action_egdev_put(egdev);
  1287. }
  1288. void tc_setup_cb_egdev_unregister(const struct net_device *dev,
  1289. tc_setup_cb_t *cb, void *cb_priv)
  1290. {
  1291. rtnl_lock();
  1292. __tc_setup_cb_egdev_unregister(dev, cb, cb_priv);
  1293. rtnl_unlock();
  1294. }
  1295. EXPORT_SYMBOL_GPL(tc_setup_cb_egdev_unregister);
  1296. int tc_setup_cb_egdev_call(const struct net_device *dev,
  1297. enum tc_setup_type type, void *type_data,
  1298. bool err_stop)
  1299. {
  1300. struct tcf_action_egdev *egdev = tcf_action_egdev_lookup(dev);
  1301. if (!egdev)
  1302. return 0;
  1303. return tcf_action_egdev_cb_call(egdev, type, type_data, err_stop);
  1304. }
  1305. EXPORT_SYMBOL_GPL(tc_setup_cb_egdev_call);
  1306. static __net_init int tcf_action_net_init(struct net *net)
  1307. {
  1308. struct tcf_action_net *tan = net_generic(net, tcf_action_net_id);
  1309. return rhashtable_init(&tan->egdev_ht, &tcf_action_egdev_ht_params);
  1310. }
  1311. static void __net_exit tcf_action_net_exit(struct net *net)
  1312. {
  1313. struct tcf_action_net *tan = net_generic(net, tcf_action_net_id);
  1314. rhashtable_destroy(&tan->egdev_ht);
  1315. }
  1316. static struct pernet_operations tcf_action_net_ops = {
  1317. .init = tcf_action_net_init,
  1318. .exit = tcf_action_net_exit,
  1319. .id = &tcf_action_net_id,
  1320. .size = sizeof(struct tcf_action_net),
  1321. .async = true,
  1322. };
  1323. static int __init tc_action_init(void)
  1324. {
  1325. int err;
  1326. err = register_pernet_subsys(&tcf_action_net_ops);
  1327. if (err)
  1328. return err;
  1329. rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0);
  1330. rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0);
  1331. rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
  1332. 0);
  1333. return 0;
  1334. }
  1335. subsys_initcall(tc_action_init);