act_api.c 23 KB

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