act_api.c 23 KB

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