act_api.c 24 KB

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