act_api.c 24 KB

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