act_api.c 23 KB

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