act_api.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  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. int ret = -1, i;
  412. u32 jmp_prgcnt = 0;
  413. u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
  414. if (skb_skip_tc_classify(skb))
  415. return TC_ACT_OK;
  416. restart_act_graph:
  417. for (i = 0; i < nr_actions; i++) {
  418. const struct tc_action *a = actions[i];
  419. if (jmp_prgcnt > 0) {
  420. jmp_prgcnt -= 1;
  421. continue;
  422. }
  423. repeat:
  424. ret = a->ops->act(skb, a, res);
  425. if (ret == TC_ACT_REPEAT)
  426. goto repeat; /* we need a ttl - JHS */
  427. if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
  428. jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
  429. if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
  430. /* faulty opcode, stop pipeline */
  431. return TC_ACT_OK;
  432. } else {
  433. jmp_ttl -= 1;
  434. if (jmp_ttl > 0)
  435. goto restart_act_graph;
  436. else /* faulty graph, stop pipeline */
  437. return TC_ACT_OK;
  438. }
  439. } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
  440. tcf_action_goto_chain_exec(a, res);
  441. }
  442. if (ret != TC_ACT_PIPE)
  443. break;
  444. }
  445. return ret;
  446. }
  447. EXPORT_SYMBOL(tcf_action_exec);
  448. int tcf_action_destroy(struct list_head *actions, int bind)
  449. {
  450. struct tc_action *a, *tmp;
  451. int ret = 0;
  452. list_for_each_entry_safe(a, tmp, actions, list) {
  453. ret = __tcf_hash_release(a, bind, true);
  454. if (ret == ACT_P_DELETED)
  455. module_put(a->ops->owner);
  456. else if (ret < 0)
  457. return ret;
  458. }
  459. return ret;
  460. }
  461. int
  462. tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  463. {
  464. return a->ops->dump(skb, a, bind, ref);
  465. }
  466. int
  467. tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  468. {
  469. int err = -EINVAL;
  470. unsigned char *b = skb_tail_pointer(skb);
  471. struct nlattr *nest;
  472. if (nla_put_string(skb, TCA_KIND, a->ops->kind))
  473. goto nla_put_failure;
  474. if (tcf_action_copy_stats(skb, a, 0))
  475. goto nla_put_failure;
  476. if (a->act_cookie) {
  477. if (nla_put(skb, TCA_ACT_COOKIE, a->act_cookie->len,
  478. a->act_cookie->data))
  479. goto nla_put_failure;
  480. }
  481. nest = nla_nest_start(skb, TCA_OPTIONS);
  482. if (nest == NULL)
  483. goto nla_put_failure;
  484. err = tcf_action_dump_old(skb, a, bind, ref);
  485. if (err > 0) {
  486. nla_nest_end(skb, nest);
  487. return err;
  488. }
  489. nla_put_failure:
  490. nlmsg_trim(skb, b);
  491. return -1;
  492. }
  493. EXPORT_SYMBOL(tcf_action_dump_1);
  494. int tcf_action_dump(struct sk_buff *skb, struct list_head *actions,
  495. int bind, int ref)
  496. {
  497. struct tc_action *a;
  498. int err = -EINVAL;
  499. struct nlattr *nest;
  500. list_for_each_entry(a, actions, list) {
  501. nest = nla_nest_start(skb, a->order);
  502. if (nest == NULL)
  503. goto nla_put_failure;
  504. err = tcf_action_dump_1(skb, a, bind, ref);
  505. if (err < 0)
  506. goto errout;
  507. nla_nest_end(skb, nest);
  508. }
  509. return 0;
  510. nla_put_failure:
  511. err = -EINVAL;
  512. errout:
  513. nla_nest_cancel(skb, nest);
  514. return err;
  515. }
  516. static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
  517. {
  518. struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL);
  519. if (!c)
  520. return NULL;
  521. c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
  522. if (!c->data) {
  523. kfree(c);
  524. return NULL;
  525. }
  526. c->len = nla_len(tb[TCA_ACT_COOKIE]);
  527. return c;
  528. }
  529. struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
  530. struct nlattr *nla, struct nlattr *est,
  531. char *name, int ovr, int bind)
  532. {
  533. struct tc_action *a;
  534. struct tc_action_ops *a_o;
  535. struct tc_cookie *cookie = NULL;
  536. char act_name[IFNAMSIZ];
  537. struct nlattr *tb[TCA_ACT_MAX + 1];
  538. struct nlattr *kind;
  539. int err;
  540. if (name == NULL) {
  541. err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL);
  542. if (err < 0)
  543. goto err_out;
  544. err = -EINVAL;
  545. kind = tb[TCA_ACT_KIND];
  546. if (kind == NULL)
  547. goto err_out;
  548. if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
  549. goto err_out;
  550. if (tb[TCA_ACT_COOKIE]) {
  551. int cklen = nla_len(tb[TCA_ACT_COOKIE]);
  552. if (cklen > TC_COOKIE_MAX_SIZE)
  553. goto err_out;
  554. cookie = nla_memdup_cookie(tb);
  555. if (!cookie) {
  556. err = -ENOMEM;
  557. goto err_out;
  558. }
  559. }
  560. } else {
  561. err = -EINVAL;
  562. if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
  563. goto err_out;
  564. }
  565. a_o = tc_lookup_action_n(act_name);
  566. if (a_o == NULL) {
  567. #ifdef CONFIG_MODULES
  568. rtnl_unlock();
  569. request_module("act_%s", act_name);
  570. rtnl_lock();
  571. a_o = tc_lookup_action_n(act_name);
  572. /* We dropped the RTNL semaphore in order to
  573. * perform the module load. So, even if we
  574. * succeeded in loading the module we have to
  575. * tell the caller to replay the request. We
  576. * indicate this using -EAGAIN.
  577. */
  578. if (a_o != NULL) {
  579. err = -EAGAIN;
  580. goto err_mod;
  581. }
  582. #endif
  583. err = -ENOENT;
  584. goto err_out;
  585. }
  586. /* backward compatibility for policer */
  587. if (name == NULL)
  588. err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind);
  589. else
  590. err = a_o->init(net, nla, est, &a, ovr, bind);
  591. if (err < 0)
  592. goto err_mod;
  593. if (name == NULL && tb[TCA_ACT_COOKIE]) {
  594. if (a->act_cookie) {
  595. kfree(a->act_cookie->data);
  596. kfree(a->act_cookie);
  597. }
  598. a->act_cookie = cookie;
  599. }
  600. /* module count goes up only when brand new policy is created
  601. * if it exists and is only bound to in a_o->init() then
  602. * ACT_P_CREATED is not returned (a zero is).
  603. */
  604. if (err != ACT_P_CREATED)
  605. module_put(a_o->owner);
  606. if (TC_ACT_EXT_CMP(a->tcfa_action, TC_ACT_GOTO_CHAIN)) {
  607. err = tcf_action_goto_chain_init(a, tp);
  608. if (err) {
  609. LIST_HEAD(actions);
  610. list_add_tail(&a->list, &actions);
  611. tcf_action_destroy(&actions, bind);
  612. return ERR_PTR(err);
  613. }
  614. }
  615. return a;
  616. err_mod:
  617. module_put(a_o->owner);
  618. err_out:
  619. if (cookie) {
  620. kfree(cookie->data);
  621. kfree(cookie);
  622. }
  623. return ERR_PTR(err);
  624. }
  625. static void cleanup_a(struct list_head *actions, int ovr)
  626. {
  627. struct tc_action *a;
  628. if (!ovr)
  629. return;
  630. list_for_each_entry(a, actions, list)
  631. a->tcfa_refcnt--;
  632. }
  633. int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
  634. struct nlattr *est, char *name, int ovr, int bind,
  635. struct list_head *actions)
  636. {
  637. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  638. struct tc_action *act;
  639. int err;
  640. int i;
  641. err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, NULL);
  642. if (err < 0)
  643. return err;
  644. for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
  645. act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind);
  646. if (IS_ERR(act)) {
  647. err = PTR_ERR(act);
  648. goto err;
  649. }
  650. act->order = i;
  651. if (ovr)
  652. act->tcfa_refcnt++;
  653. list_add_tail(&act->list, actions);
  654. }
  655. /* Remove the temp refcnt which was necessary to protect against
  656. * destroying an existing action which was being replaced
  657. */
  658. cleanup_a(actions, ovr);
  659. return 0;
  660. err:
  661. tcf_action_destroy(actions, bind);
  662. return err;
  663. }
  664. int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
  665. int compat_mode)
  666. {
  667. int err = 0;
  668. struct gnet_dump d;
  669. if (p == NULL)
  670. goto errout;
  671. /* compat_mode being true specifies a call that is supposed
  672. * to add additional backward compatibility statistic TLVs.
  673. */
  674. if (compat_mode) {
  675. if (p->type == TCA_OLD_COMPAT)
  676. err = gnet_stats_start_copy_compat(skb, 0,
  677. TCA_STATS,
  678. TCA_XSTATS,
  679. &p->tcfa_lock, &d,
  680. TCA_PAD);
  681. else
  682. return 0;
  683. } else
  684. err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
  685. &p->tcfa_lock, &d, TCA_ACT_PAD);
  686. if (err < 0)
  687. goto errout;
  688. if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
  689. gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
  690. gnet_stats_copy_queue(&d, p->cpu_qstats,
  691. &p->tcfa_qstats,
  692. p->tcfa_qstats.qlen) < 0)
  693. goto errout;
  694. if (gnet_stats_finish_copy(&d) < 0)
  695. goto errout;
  696. return 0;
  697. errout:
  698. return -1;
  699. }
  700. static int tca_get_fill(struct sk_buff *skb, struct list_head *actions,
  701. u32 portid, u32 seq, u16 flags, int event, int bind,
  702. int ref)
  703. {
  704. struct tcamsg *t;
  705. struct nlmsghdr *nlh;
  706. unsigned char *b = skb_tail_pointer(skb);
  707. struct nlattr *nest;
  708. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
  709. if (!nlh)
  710. goto out_nlmsg_trim;
  711. t = nlmsg_data(nlh);
  712. t->tca_family = AF_UNSPEC;
  713. t->tca__pad1 = 0;
  714. t->tca__pad2 = 0;
  715. nest = nla_nest_start(skb, TCA_ACT_TAB);
  716. if (nest == NULL)
  717. goto out_nlmsg_trim;
  718. if (tcf_action_dump(skb, actions, bind, ref) < 0)
  719. goto out_nlmsg_trim;
  720. nla_nest_end(skb, nest);
  721. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  722. return skb->len;
  723. out_nlmsg_trim:
  724. nlmsg_trim(skb, b);
  725. return -1;
  726. }
  727. static int
  728. tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
  729. struct list_head *actions, int event)
  730. {
  731. struct sk_buff *skb;
  732. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  733. if (!skb)
  734. return -ENOBUFS;
  735. if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
  736. 0, 0) <= 0) {
  737. kfree_skb(skb);
  738. return -EINVAL;
  739. }
  740. return rtnl_unicast(skb, net, portid);
  741. }
  742. static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
  743. struct nlmsghdr *n, u32 portid)
  744. {
  745. struct nlattr *tb[TCA_ACT_MAX + 1];
  746. const struct tc_action_ops *ops;
  747. struct tc_action *a;
  748. int index;
  749. int err;
  750. err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL);
  751. if (err < 0)
  752. goto err_out;
  753. err = -EINVAL;
  754. if (tb[TCA_ACT_INDEX] == NULL ||
  755. nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
  756. goto err_out;
  757. index = nla_get_u32(tb[TCA_ACT_INDEX]);
  758. err = -EINVAL;
  759. ops = tc_lookup_action(tb[TCA_ACT_KIND]);
  760. if (!ops) /* could happen in batch of actions */
  761. goto err_out;
  762. err = -ENOENT;
  763. if (ops->lookup(net, &a, index) == 0)
  764. goto err_mod;
  765. module_put(ops->owner);
  766. return a;
  767. err_mod:
  768. module_put(ops->owner);
  769. err_out:
  770. return ERR_PTR(err);
  771. }
  772. static int tca_action_flush(struct net *net, struct nlattr *nla,
  773. struct nlmsghdr *n, u32 portid)
  774. {
  775. struct sk_buff *skb;
  776. unsigned char *b;
  777. struct nlmsghdr *nlh;
  778. struct tcamsg *t;
  779. struct netlink_callback dcb;
  780. struct nlattr *nest;
  781. struct nlattr *tb[TCA_ACT_MAX + 1];
  782. const struct tc_action_ops *ops;
  783. struct nlattr *kind;
  784. int err = -ENOMEM;
  785. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  786. if (!skb) {
  787. pr_debug("tca_action_flush: failed skb alloc\n");
  788. return err;
  789. }
  790. b = skb_tail_pointer(skb);
  791. err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL);
  792. if (err < 0)
  793. goto err_out;
  794. err = -EINVAL;
  795. kind = tb[TCA_ACT_KIND];
  796. ops = tc_lookup_action(kind);
  797. if (!ops) /*some idjot trying to flush unknown action */
  798. goto err_out;
  799. nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
  800. sizeof(*t), 0);
  801. if (!nlh)
  802. goto out_module_put;
  803. t = nlmsg_data(nlh);
  804. t->tca_family = AF_UNSPEC;
  805. t->tca__pad1 = 0;
  806. t->tca__pad2 = 0;
  807. nest = nla_nest_start(skb, TCA_ACT_TAB);
  808. if (nest == NULL)
  809. goto out_module_put;
  810. err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops);
  811. if (err <= 0)
  812. goto out_module_put;
  813. nla_nest_end(skb, nest);
  814. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  815. nlh->nlmsg_flags |= NLM_F_ROOT;
  816. module_put(ops->owner);
  817. err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  818. n->nlmsg_flags & NLM_F_ECHO);
  819. if (err > 0)
  820. return 0;
  821. return err;
  822. out_module_put:
  823. module_put(ops->owner);
  824. err_out:
  825. kfree_skb(skb);
  826. return err;
  827. }
  828. static int
  829. tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
  830. u32 portid)
  831. {
  832. int ret;
  833. struct sk_buff *skb;
  834. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  835. if (!skb)
  836. return -ENOBUFS;
  837. if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
  838. 0, 1) <= 0) {
  839. kfree_skb(skb);
  840. return -EINVAL;
  841. }
  842. /* now do the delete */
  843. ret = tcf_action_destroy(actions, 0);
  844. if (ret < 0) {
  845. kfree_skb(skb);
  846. return ret;
  847. }
  848. ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  849. n->nlmsg_flags & NLM_F_ECHO);
  850. if (ret > 0)
  851. return 0;
  852. return ret;
  853. }
  854. static int
  855. tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
  856. u32 portid, int event)
  857. {
  858. int i, ret;
  859. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  860. struct tc_action *act;
  861. LIST_HEAD(actions);
  862. ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, NULL);
  863. if (ret < 0)
  864. return ret;
  865. if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
  866. if (tb[1] != NULL)
  867. return tca_action_flush(net, tb[1], n, portid);
  868. else
  869. return -EINVAL;
  870. }
  871. for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
  872. act = tcf_action_get_1(net, tb[i], n, portid);
  873. if (IS_ERR(act)) {
  874. ret = PTR_ERR(act);
  875. goto err;
  876. }
  877. act->order = i;
  878. list_add_tail(&act->list, &actions);
  879. }
  880. if (event == RTM_GETACTION)
  881. ret = tcf_get_notify(net, portid, n, &actions, event);
  882. else { /* delete */
  883. ret = tcf_del_notify(net, n, &actions, portid);
  884. if (ret)
  885. goto err;
  886. return ret;
  887. }
  888. err:
  889. if (event != RTM_GETACTION)
  890. tcf_action_destroy(&actions, 0);
  891. return ret;
  892. }
  893. static int
  894. tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
  895. u32 portid)
  896. {
  897. struct sk_buff *skb;
  898. int err = 0;
  899. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  900. if (!skb)
  901. return -ENOBUFS;
  902. if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
  903. RTM_NEWACTION, 0, 0) <= 0) {
  904. kfree_skb(skb);
  905. return -EINVAL;
  906. }
  907. err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  908. n->nlmsg_flags & NLM_F_ECHO);
  909. if (err > 0)
  910. err = 0;
  911. return err;
  912. }
  913. static int tcf_action_add(struct net *net, struct nlattr *nla,
  914. struct nlmsghdr *n, u32 portid, int ovr)
  915. {
  916. int ret = 0;
  917. LIST_HEAD(actions);
  918. ret = tcf_action_init(net, NULL, nla, NULL, NULL, ovr, 0, &actions);
  919. if (ret)
  920. return ret;
  921. return tcf_add_notify(net, n, &actions, portid);
  922. }
  923. static u32 tcaa_root_flags_allowed = TCA_FLAG_LARGE_DUMP_ON;
  924. static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = {
  925. [TCA_ROOT_FLAGS] = { .type = NLA_BITFIELD32,
  926. .validation_data = &tcaa_root_flags_allowed },
  927. [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 },
  928. };
  929. static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
  930. struct netlink_ext_ack *extack)
  931. {
  932. struct net *net = sock_net(skb->sk);
  933. struct nlattr *tca[TCA_ROOT_MAX + 1];
  934. u32 portid = skb ? NETLINK_CB(skb).portid : 0;
  935. int ret = 0, ovr = 0;
  936. if ((n->nlmsg_type != RTM_GETACTION) &&
  937. !netlink_capable(skb, CAP_NET_ADMIN))
  938. return -EPERM;
  939. ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ROOT_MAX, NULL,
  940. extack);
  941. if (ret < 0)
  942. return ret;
  943. if (tca[TCA_ACT_TAB] == NULL) {
  944. pr_notice("tc_ctl_action: received NO action attribs\n");
  945. return -EINVAL;
  946. }
  947. /* n->nlmsg_flags & NLM_F_CREATE */
  948. switch (n->nlmsg_type) {
  949. case RTM_NEWACTION:
  950. /* we are going to assume all other flags
  951. * imply create only if it doesn't exist
  952. * Note that CREATE | EXCL implies that
  953. * but since we want avoid ambiguity (eg when flags
  954. * is zero) then just set this
  955. */
  956. if (n->nlmsg_flags & NLM_F_REPLACE)
  957. ovr = 1;
  958. replay:
  959. ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
  960. if (ret == -EAGAIN)
  961. goto replay;
  962. break;
  963. case RTM_DELACTION:
  964. ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
  965. portid, RTM_DELACTION);
  966. break;
  967. case RTM_GETACTION:
  968. ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
  969. portid, RTM_GETACTION);
  970. break;
  971. default:
  972. BUG();
  973. }
  974. return ret;
  975. }
  976. static struct nlattr *find_dump_kind(struct nlattr **nla)
  977. {
  978. struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
  979. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  980. struct nlattr *kind;
  981. tb1 = nla[TCA_ACT_TAB];
  982. if (tb1 == NULL)
  983. return NULL;
  984. if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
  985. NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0)
  986. return NULL;
  987. if (tb[1] == NULL)
  988. return NULL;
  989. if (nla_parse_nested(tb2, TCA_ACT_MAX, tb[1], NULL, NULL) < 0)
  990. return NULL;
  991. kind = tb2[TCA_ACT_KIND];
  992. return kind;
  993. }
  994. static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
  995. {
  996. struct net *net = sock_net(skb->sk);
  997. struct nlmsghdr *nlh;
  998. unsigned char *b = skb_tail_pointer(skb);
  999. struct nlattr *nest;
  1000. struct tc_action_ops *a_o;
  1001. int ret = 0;
  1002. struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
  1003. struct nlattr *tb[TCA_ROOT_MAX + 1];
  1004. struct nlattr *count_attr = NULL;
  1005. unsigned long jiffy_since = 0;
  1006. struct nlattr *kind = NULL;
  1007. struct nla_bitfield32 bf;
  1008. u32 msecs_since = 0;
  1009. u32 act_count = 0;
  1010. ret = nlmsg_parse(cb->nlh, sizeof(struct tcamsg), tb, TCA_ROOT_MAX,
  1011. tcaa_policy, NULL);
  1012. if (ret < 0)
  1013. return ret;
  1014. kind = find_dump_kind(tb);
  1015. if (kind == NULL) {
  1016. pr_info("tc_dump_action: action bad kind\n");
  1017. return 0;
  1018. }
  1019. a_o = tc_lookup_action(kind);
  1020. if (a_o == NULL)
  1021. return 0;
  1022. cb->args[2] = 0;
  1023. if (tb[TCA_ROOT_FLAGS]) {
  1024. bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]);
  1025. cb->args[2] = bf.value;
  1026. }
  1027. if (tb[TCA_ROOT_TIME_DELTA]) {
  1028. msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]);
  1029. }
  1030. nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  1031. cb->nlh->nlmsg_type, sizeof(*t), 0);
  1032. if (!nlh)
  1033. goto out_module_put;
  1034. if (msecs_since)
  1035. jiffy_since = jiffies - msecs_to_jiffies(msecs_since);
  1036. t = nlmsg_data(nlh);
  1037. t->tca_family = AF_UNSPEC;
  1038. t->tca__pad1 = 0;
  1039. t->tca__pad2 = 0;
  1040. cb->args[3] = jiffy_since;
  1041. count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32));
  1042. if (!count_attr)
  1043. goto out_module_put;
  1044. nest = nla_nest_start(skb, TCA_ACT_TAB);
  1045. if (nest == NULL)
  1046. goto out_module_put;
  1047. ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o);
  1048. if (ret < 0)
  1049. goto out_module_put;
  1050. if (ret > 0) {
  1051. nla_nest_end(skb, nest);
  1052. ret = skb->len;
  1053. act_count = cb->args[1];
  1054. memcpy(nla_data(count_attr), &act_count, sizeof(u32));
  1055. cb->args[1] = 0;
  1056. } else
  1057. nlmsg_trim(skb, b);
  1058. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  1059. if (NETLINK_CB(cb->skb).portid && ret)
  1060. nlh->nlmsg_flags |= NLM_F_MULTI;
  1061. module_put(a_o->owner);
  1062. return skb->len;
  1063. out_module_put:
  1064. module_put(a_o->owner);
  1065. nlmsg_trim(skb, b);
  1066. return skb->len;
  1067. }
  1068. static int __init tc_action_init(void)
  1069. {
  1070. rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
  1071. rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
  1072. rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
  1073. NULL);
  1074. return 0;
  1075. }
  1076. subsys_initcall(tc_action_init);