act_api.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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 <asm/uaccess.h>
  14. #include <asm/system.h>
  15. #include <linux/bitops.h>
  16. #include <linux/types.h>
  17. #include <linux/kernel.h>
  18. #include <linux/sched.h>
  19. #include <linux/string.h>
  20. #include <linux/mm.h>
  21. #include <linux/socket.h>
  22. #include <linux/sockios.h>
  23. #include <linux/in.h>
  24. #include <linux/errno.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/rtnetlink.h>
  29. #include <linux/init.h>
  30. #include <linux/kmod.h>
  31. #include <net/sock.h>
  32. #include <net/sch_generic.h>
  33. #include <net/act_api.h>
  34. #if 0 /* control */
  35. #define DPRINTK(format, args...) printk(KERN_DEBUG format, ##args)
  36. #else
  37. #define DPRINTK(format, args...)
  38. #endif
  39. #if 0 /* data */
  40. #define D2PRINTK(format, args...) printk(KERN_DEBUG format, ##args)
  41. #else
  42. #define D2PRINTK(format, args...)
  43. #endif
  44. static struct tc_action_ops *act_base = NULL;
  45. static DEFINE_RWLOCK(act_mod_lock);
  46. int tcf_register_action(struct tc_action_ops *act)
  47. {
  48. struct tc_action_ops *a, **ap;
  49. write_lock(&act_mod_lock);
  50. for (ap = &act_base; (a = *ap) != NULL; ap = &a->next) {
  51. if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
  52. write_unlock(&act_mod_lock);
  53. return -EEXIST;
  54. }
  55. }
  56. act->next = NULL;
  57. *ap = act;
  58. write_unlock(&act_mod_lock);
  59. return 0;
  60. }
  61. int tcf_unregister_action(struct tc_action_ops *act)
  62. {
  63. struct tc_action_ops *a, **ap;
  64. int err = -ENOENT;
  65. write_lock(&act_mod_lock);
  66. for (ap = &act_base; (a = *ap) != NULL; ap = &a->next)
  67. if (a == act)
  68. break;
  69. if (a) {
  70. *ap = a->next;
  71. a->next = NULL;
  72. err = 0;
  73. }
  74. write_unlock(&act_mod_lock);
  75. return err;
  76. }
  77. /* lookup by name */
  78. static struct tc_action_ops *tc_lookup_action_n(char *kind)
  79. {
  80. struct tc_action_ops *a = NULL;
  81. if (kind) {
  82. read_lock(&act_mod_lock);
  83. for (a = act_base; a; a = a->next) {
  84. if (strcmp(kind, a->kind) == 0) {
  85. if (!try_module_get(a->owner)) {
  86. read_unlock(&act_mod_lock);
  87. return NULL;
  88. }
  89. break;
  90. }
  91. }
  92. read_unlock(&act_mod_lock);
  93. }
  94. return a;
  95. }
  96. /* lookup by rtattr */
  97. static struct tc_action_ops *tc_lookup_action(struct rtattr *kind)
  98. {
  99. struct tc_action_ops *a = NULL;
  100. if (kind) {
  101. read_lock(&act_mod_lock);
  102. for (a = act_base; a; a = a->next) {
  103. if (rtattr_strcmp(kind, a->kind) == 0) {
  104. if (!try_module_get(a->owner)) {
  105. read_unlock(&act_mod_lock);
  106. return NULL;
  107. }
  108. break;
  109. }
  110. }
  111. read_unlock(&act_mod_lock);
  112. }
  113. return a;
  114. }
  115. #if 0
  116. /* lookup by id */
  117. static struct tc_action_ops *tc_lookup_action_id(u32 type)
  118. {
  119. struct tc_action_ops *a = NULL;
  120. if (type) {
  121. read_lock(&act_mod_lock);
  122. for (a = act_base; a; a = a->next) {
  123. if (a->type == type) {
  124. if (!try_module_get(a->owner)) {
  125. read_unlock(&act_mod_lock);
  126. return NULL;
  127. }
  128. break;
  129. }
  130. }
  131. read_unlock(&act_mod_lock);
  132. }
  133. return a;
  134. }
  135. #endif
  136. int tcf_action_exec(struct sk_buff *skb, struct tc_action *act,
  137. struct tcf_result *res)
  138. {
  139. struct tc_action *a;
  140. int ret = -1;
  141. if (skb->tc_verd & TC_NCLS) {
  142. skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
  143. D2PRINTK("(%p)tcf_action_exec: cleared TC_NCLS in %s out %s\n",
  144. skb, skb->input_dev ? skb->input_dev->name : "xxx",
  145. skb->dev->name);
  146. ret = TC_ACT_OK;
  147. goto exec_done;
  148. }
  149. while ((a = act) != NULL) {
  150. repeat:
  151. if (a->ops && a->ops->act) {
  152. ret = a->ops->act(skb, a, res);
  153. if (TC_MUNGED & skb->tc_verd) {
  154. /* copied already, allow trampling */
  155. skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
  156. skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
  157. }
  158. if (ret == TC_ACT_REPEAT)
  159. goto repeat; /* we need a ttl - JHS */
  160. if (ret != TC_ACT_PIPE)
  161. goto exec_done;
  162. }
  163. act = a->next;
  164. }
  165. exec_done:
  166. return ret;
  167. }
  168. void tcf_action_destroy(struct tc_action *act, int bind)
  169. {
  170. struct tc_action *a;
  171. for (a = act; a; a = act) {
  172. if (a->ops && a->ops->cleanup) {
  173. DPRINTK("tcf_action_destroy destroying %p next %p\n",
  174. a, a->next);
  175. if (a->ops->cleanup(a, bind) == ACT_P_DELETED)
  176. module_put(a->ops->owner);
  177. act = act->next;
  178. kfree(a);
  179. } else { /*FIXME: Remove later - catch insertion bugs*/
  180. printk("tcf_action_destroy: BUG? destroying NULL ops\n");
  181. act = act->next;
  182. kfree(a);
  183. }
  184. }
  185. }
  186. int
  187. tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  188. {
  189. int err = -EINVAL;
  190. if (a->ops == NULL || a->ops->dump == NULL)
  191. return err;
  192. return a->ops->dump(skb, a, bind, ref);
  193. }
  194. int
  195. tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  196. {
  197. int err = -EINVAL;
  198. unsigned char *b = skb->tail;
  199. struct rtattr *r;
  200. if (a->ops == NULL || a->ops->dump == NULL)
  201. return err;
  202. RTA_PUT(skb, TCA_KIND, IFNAMSIZ, a->ops->kind);
  203. if (tcf_action_copy_stats(skb, a, 0))
  204. goto rtattr_failure;
  205. r = (struct rtattr*) skb->tail;
  206. RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
  207. if ((err = tcf_action_dump_old(skb, a, bind, ref)) > 0) {
  208. r->rta_len = skb->tail - (u8*)r;
  209. return err;
  210. }
  211. rtattr_failure:
  212. skb_trim(skb, b - skb->data);
  213. return -1;
  214. }
  215. int
  216. tcf_action_dump(struct sk_buff *skb, struct tc_action *act, int bind, int ref)
  217. {
  218. struct tc_action *a;
  219. int err = -EINVAL;
  220. unsigned char *b = skb->tail;
  221. struct rtattr *r ;
  222. while ((a = act) != NULL) {
  223. r = (struct rtattr*) skb->tail;
  224. act = a->next;
  225. RTA_PUT(skb, a->order, 0, NULL);
  226. err = tcf_action_dump_1(skb, a, bind, ref);
  227. if (err < 0)
  228. goto errout;
  229. r->rta_len = skb->tail - (u8*)r;
  230. }
  231. return 0;
  232. rtattr_failure:
  233. err = -EINVAL;
  234. errout:
  235. skb_trim(skb, b - skb->data);
  236. return err;
  237. }
  238. struct tc_action *tcf_action_init_1(struct rtattr *rta, struct rtattr *est,
  239. char *name, int ovr, int bind, int *err)
  240. {
  241. struct tc_action *a;
  242. struct tc_action_ops *a_o;
  243. char act_name[IFNAMSIZ];
  244. struct rtattr *tb[TCA_ACT_MAX+1];
  245. struct rtattr *kind;
  246. *err = -EINVAL;
  247. if (name == NULL) {
  248. if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
  249. goto err_out;
  250. kind = tb[TCA_ACT_KIND-1];
  251. if (kind == NULL)
  252. goto err_out;
  253. if (rtattr_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
  254. goto err_out;
  255. } else {
  256. if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
  257. goto err_out;
  258. }
  259. a_o = tc_lookup_action_n(act_name);
  260. if (a_o == NULL) {
  261. #ifdef CONFIG_KMOD
  262. rtnl_unlock();
  263. request_module("act_%s", act_name);
  264. rtnl_lock();
  265. a_o = tc_lookup_action_n(act_name);
  266. /* We dropped the RTNL semaphore in order to
  267. * perform the module load. So, even if we
  268. * succeeded in loading the module we have to
  269. * tell the caller to replay the request. We
  270. * indicate this using -EAGAIN.
  271. */
  272. if (a_o != NULL) {
  273. *err = -EAGAIN;
  274. goto err_mod;
  275. }
  276. #endif
  277. *err = -ENOENT;
  278. goto err_out;
  279. }
  280. *err = -ENOMEM;
  281. a = kzalloc(sizeof(*a), GFP_KERNEL);
  282. if (a == NULL)
  283. goto err_mod;
  284. /* backward compatibility for policer */
  285. if (name == NULL)
  286. *err = a_o->init(tb[TCA_ACT_OPTIONS-1], est, a, ovr, bind);
  287. else
  288. *err = a_o->init(rta, est, a, ovr, bind);
  289. if (*err < 0)
  290. goto err_free;
  291. /* module count goes up only when brand new policy is created
  292. if it exists and is only bound to in a_o->init() then
  293. ACT_P_CREATED is not returned (a zero is).
  294. */
  295. if (*err != ACT_P_CREATED)
  296. module_put(a_o->owner);
  297. a->ops = a_o;
  298. DPRINTK("tcf_action_init_1: successfull %s\n", act_name);
  299. *err = 0;
  300. return a;
  301. err_free:
  302. kfree(a);
  303. err_mod:
  304. module_put(a_o->owner);
  305. err_out:
  306. return NULL;
  307. }
  308. struct tc_action *tcf_action_init(struct rtattr *rta, struct rtattr *est,
  309. char *name, int ovr, int bind, int *err)
  310. {
  311. struct rtattr *tb[TCA_ACT_MAX_PRIO+1];
  312. struct tc_action *head = NULL, *act, *act_prev = NULL;
  313. int i;
  314. if (rtattr_parse_nested(tb, TCA_ACT_MAX_PRIO, rta) < 0) {
  315. *err = -EINVAL;
  316. return head;
  317. }
  318. for (i=0; i < TCA_ACT_MAX_PRIO && tb[i]; i++) {
  319. act = tcf_action_init_1(tb[i], est, name, ovr, bind, err);
  320. if (act == NULL)
  321. goto err;
  322. act->order = i+1;
  323. if (head == NULL)
  324. head = act;
  325. else
  326. act_prev->next = act;
  327. act_prev = act;
  328. }
  329. return head;
  330. err:
  331. if (head != NULL)
  332. tcf_action_destroy(head, bind);
  333. return NULL;
  334. }
  335. int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
  336. int compat_mode)
  337. {
  338. int err = 0;
  339. struct gnet_dump d;
  340. struct tcf_act_hdr *h = a->priv;
  341. if (h == NULL)
  342. goto errout;
  343. /* compat_mode being true specifies a call that is supposed
  344. * to add additional backward compatiblity statistic TLVs.
  345. */
  346. if (compat_mode) {
  347. if (a->type == TCA_OLD_COMPAT)
  348. err = gnet_stats_start_copy_compat(skb, 0,
  349. TCA_STATS, TCA_XSTATS, h->stats_lock, &d);
  350. else
  351. return 0;
  352. } else
  353. err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
  354. h->stats_lock, &d);
  355. if (err < 0)
  356. goto errout;
  357. if (a->ops != NULL && a->ops->get_stats != NULL)
  358. if (a->ops->get_stats(skb, a) < 0)
  359. goto errout;
  360. if (gnet_stats_copy_basic(&d, &h->bstats) < 0 ||
  361. #ifdef CONFIG_NET_ESTIMATOR
  362. gnet_stats_copy_rate_est(&d, &h->rate_est) < 0 ||
  363. #endif
  364. gnet_stats_copy_queue(&d, &h->qstats) < 0)
  365. goto errout;
  366. if (gnet_stats_finish_copy(&d) < 0)
  367. goto errout;
  368. return 0;
  369. errout:
  370. return -1;
  371. }
  372. static int
  373. tca_get_fill(struct sk_buff *skb, struct tc_action *a, u32 pid, u32 seq,
  374. u16 flags, int event, int bind, int ref)
  375. {
  376. struct tcamsg *t;
  377. struct nlmsghdr *nlh;
  378. unsigned char *b = skb->tail;
  379. struct rtattr *x;
  380. nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
  381. t = NLMSG_DATA(nlh);
  382. t->tca_family = AF_UNSPEC;
  383. t->tca__pad1 = 0;
  384. t->tca__pad2 = 0;
  385. x = (struct rtattr*) skb->tail;
  386. RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
  387. if (tcf_action_dump(skb, a, bind, ref) < 0)
  388. goto rtattr_failure;
  389. x->rta_len = skb->tail - (u8*)x;
  390. nlh->nlmsg_len = skb->tail - b;
  391. return skb->len;
  392. rtattr_failure:
  393. nlmsg_failure:
  394. skb_trim(skb, b - skb->data);
  395. return -1;
  396. }
  397. static int
  398. act_get_notify(u32 pid, struct nlmsghdr *n, struct tc_action *a, int event)
  399. {
  400. struct sk_buff *skb;
  401. int err = 0;
  402. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  403. if (!skb)
  404. return -ENOBUFS;
  405. if (tca_get_fill(skb, a, pid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
  406. kfree_skb(skb);
  407. return -EINVAL;
  408. }
  409. err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
  410. if (err > 0)
  411. err = 0;
  412. return err;
  413. }
  414. static struct tc_action *
  415. tcf_action_get_1(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int *err)
  416. {
  417. struct rtattr *tb[TCA_ACT_MAX+1];
  418. struct tc_action *a;
  419. int index;
  420. *err = -EINVAL;
  421. if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
  422. return NULL;
  423. if (tb[TCA_ACT_INDEX - 1] == NULL ||
  424. RTA_PAYLOAD(tb[TCA_ACT_INDEX - 1]) < sizeof(index))
  425. return NULL;
  426. index = *(int *)RTA_DATA(tb[TCA_ACT_INDEX - 1]);
  427. *err = -ENOMEM;
  428. a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
  429. if (a == NULL)
  430. return NULL;
  431. *err = -EINVAL;
  432. a->ops = tc_lookup_action(tb[TCA_ACT_KIND - 1]);
  433. if (a->ops == NULL)
  434. goto err_free;
  435. if (a->ops->lookup == NULL)
  436. goto err_mod;
  437. *err = -ENOENT;
  438. if (a->ops->lookup(a, index) == 0)
  439. goto err_mod;
  440. module_put(a->ops->owner);
  441. *err = 0;
  442. return a;
  443. err_mod:
  444. module_put(a->ops->owner);
  445. err_free:
  446. kfree(a);
  447. return NULL;
  448. }
  449. static void cleanup_a(struct tc_action *act)
  450. {
  451. struct tc_action *a;
  452. for (a = act; a; a = act) {
  453. act = a->next;
  454. kfree(a);
  455. }
  456. }
  457. static struct tc_action *create_a(int i)
  458. {
  459. struct tc_action *act;
  460. act = kzalloc(sizeof(*act), GFP_KERNEL);
  461. if (act == NULL) {
  462. printk("create_a: failed to alloc!\n");
  463. return NULL;
  464. }
  465. act->order = i;
  466. return act;
  467. }
  468. static int tca_action_flush(struct rtattr *rta, struct nlmsghdr *n, u32 pid)
  469. {
  470. struct sk_buff *skb;
  471. unsigned char *b;
  472. struct nlmsghdr *nlh;
  473. struct tcamsg *t;
  474. struct netlink_callback dcb;
  475. struct rtattr *x;
  476. struct rtattr *tb[TCA_ACT_MAX+1];
  477. struct rtattr *kind;
  478. struct tc_action *a = create_a(0);
  479. int err = -EINVAL;
  480. if (a == NULL) {
  481. printk("tca_action_flush: couldnt create tc_action\n");
  482. return err;
  483. }
  484. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  485. if (!skb) {
  486. printk("tca_action_flush: failed skb alloc\n");
  487. kfree(a);
  488. return -ENOBUFS;
  489. }
  490. b = (unsigned char *)skb->tail;
  491. if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
  492. goto err_out;
  493. kind = tb[TCA_ACT_KIND-1];
  494. a->ops = tc_lookup_action(kind);
  495. if (a->ops == NULL)
  496. goto err_out;
  497. nlh = NLMSG_PUT(skb, pid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t));
  498. t = NLMSG_DATA(nlh);
  499. t->tca_family = AF_UNSPEC;
  500. t->tca__pad1 = 0;
  501. t->tca__pad2 = 0;
  502. x = (struct rtattr *) skb->tail;
  503. RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
  504. err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
  505. if (err < 0)
  506. goto rtattr_failure;
  507. x->rta_len = skb->tail - (u8 *) x;
  508. nlh->nlmsg_len = skb->tail - b;
  509. nlh->nlmsg_flags |= NLM_F_ROOT;
  510. module_put(a->ops->owner);
  511. kfree(a);
  512. err = rtnetlink_send(skb, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
  513. if (err > 0)
  514. return 0;
  515. return err;
  516. rtattr_failure:
  517. nlmsg_failure:
  518. module_put(a->ops->owner);
  519. err_out:
  520. kfree_skb(skb);
  521. kfree(a);
  522. return err;
  523. }
  524. static int
  525. tca_action_gd(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int event)
  526. {
  527. int i, ret = 0;
  528. struct rtattr *tb[TCA_ACT_MAX_PRIO+1];
  529. struct tc_action *head = NULL, *act, *act_prev = NULL;
  530. if (rtattr_parse_nested(tb, TCA_ACT_MAX_PRIO, rta) < 0)
  531. return -EINVAL;
  532. if (event == RTM_DELACTION && n->nlmsg_flags&NLM_F_ROOT) {
  533. if (tb[0] != NULL && tb[1] == NULL)
  534. return tca_action_flush(tb[0], n, pid);
  535. }
  536. for (i=0; i < TCA_ACT_MAX_PRIO && tb[i]; i++) {
  537. act = tcf_action_get_1(tb[i], n, pid, &ret);
  538. if (act == NULL)
  539. goto err;
  540. act->order = i+1;
  541. if (head == NULL)
  542. head = act;
  543. else
  544. act_prev->next = act;
  545. act_prev = act;
  546. }
  547. if (event == RTM_GETACTION)
  548. ret = act_get_notify(pid, n, head, event);
  549. else { /* delete */
  550. struct sk_buff *skb;
  551. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  552. if (!skb) {
  553. ret = -ENOBUFS;
  554. goto err;
  555. }
  556. if (tca_get_fill(skb, head, pid, n->nlmsg_seq, 0, event,
  557. 0, 1) <= 0) {
  558. kfree_skb(skb);
  559. ret = -EINVAL;
  560. goto err;
  561. }
  562. /* now do the delete */
  563. tcf_action_destroy(head, 0);
  564. ret = rtnetlink_send(skb, pid, RTNLGRP_TC,
  565. n->nlmsg_flags&NLM_F_ECHO);
  566. if (ret > 0)
  567. return 0;
  568. return ret;
  569. }
  570. err:
  571. cleanup_a(head);
  572. return ret;
  573. }
  574. static int tcf_add_notify(struct tc_action *a, u32 pid, u32 seq, int event,
  575. u16 flags)
  576. {
  577. struct tcamsg *t;
  578. struct nlmsghdr *nlh;
  579. struct sk_buff *skb;
  580. struct rtattr *x;
  581. unsigned char *b;
  582. int err = 0;
  583. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  584. if (!skb)
  585. return -ENOBUFS;
  586. b = (unsigned char *)skb->tail;
  587. nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
  588. t = NLMSG_DATA(nlh);
  589. t->tca_family = AF_UNSPEC;
  590. t->tca__pad1 = 0;
  591. t->tca__pad2 = 0;
  592. x = (struct rtattr*) skb->tail;
  593. RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
  594. if (tcf_action_dump(skb, a, 0, 0) < 0)
  595. goto rtattr_failure;
  596. x->rta_len = skb->tail - (u8*)x;
  597. nlh->nlmsg_len = skb->tail - b;
  598. NETLINK_CB(skb).dst_group = RTNLGRP_TC;
  599. err = rtnetlink_send(skb, pid, RTNLGRP_TC, flags&NLM_F_ECHO);
  600. if (err > 0)
  601. err = 0;
  602. return err;
  603. rtattr_failure:
  604. nlmsg_failure:
  605. kfree_skb(skb);
  606. return -1;
  607. }
  608. static int
  609. tcf_action_add(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int ovr)
  610. {
  611. int ret = 0;
  612. struct tc_action *act;
  613. struct tc_action *a;
  614. u32 seq = n->nlmsg_seq;
  615. act = tcf_action_init(rta, NULL, NULL, ovr, 0, &ret);
  616. if (act == NULL)
  617. goto done;
  618. /* dump then free all the actions after update; inserted policy
  619. * stays intact
  620. * */
  621. ret = tcf_add_notify(act, pid, seq, RTM_NEWACTION, n->nlmsg_flags);
  622. for (a = act; a; a = act) {
  623. act = a->next;
  624. kfree(a);
  625. }
  626. done:
  627. return ret;
  628. }
  629. static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
  630. {
  631. struct rtattr **tca = arg;
  632. u32 pid = skb ? NETLINK_CB(skb).pid : 0;
  633. int ret = 0, ovr = 0;
  634. if (tca[TCA_ACT_TAB-1] == NULL) {
  635. printk("tc_ctl_action: received NO action attribs\n");
  636. return -EINVAL;
  637. }
  638. /* n->nlmsg_flags&NLM_F_CREATE
  639. * */
  640. switch (n->nlmsg_type) {
  641. case RTM_NEWACTION:
  642. /* we are going to assume all other flags
  643. * imply create only if it doesnt exist
  644. * Note that CREATE | EXCL implies that
  645. * but since we want avoid ambiguity (eg when flags
  646. * is zero) then just set this
  647. */
  648. if (n->nlmsg_flags&NLM_F_REPLACE)
  649. ovr = 1;
  650. replay:
  651. ret = tcf_action_add(tca[TCA_ACT_TAB-1], n, pid, ovr);
  652. if (ret == -EAGAIN)
  653. goto replay;
  654. break;
  655. case RTM_DELACTION:
  656. ret = tca_action_gd(tca[TCA_ACT_TAB-1], n, pid, RTM_DELACTION);
  657. break;
  658. case RTM_GETACTION:
  659. ret = tca_action_gd(tca[TCA_ACT_TAB-1], n, pid, RTM_GETACTION);
  660. break;
  661. default:
  662. BUG();
  663. }
  664. return ret;
  665. }
  666. static struct rtattr *
  667. find_dump_kind(struct nlmsghdr *n)
  668. {
  669. struct rtattr *tb1, *tb2[TCA_ACT_MAX+1];
  670. struct rtattr *tb[TCA_ACT_MAX_PRIO + 1];
  671. struct rtattr *rta[TCAA_MAX + 1];
  672. struct rtattr *kind;
  673. int min_len = NLMSG_LENGTH(sizeof(struct tcamsg));
  674. int attrlen = n->nlmsg_len - NLMSG_ALIGN(min_len);
  675. struct rtattr *attr = (void *) n + NLMSG_ALIGN(min_len);
  676. if (rtattr_parse(rta, TCAA_MAX, attr, attrlen) < 0)
  677. return NULL;
  678. tb1 = rta[TCA_ACT_TAB - 1];
  679. if (tb1 == NULL)
  680. return NULL;
  681. if (rtattr_parse(tb, TCA_ACT_MAX_PRIO, RTA_DATA(tb1),
  682. NLMSG_ALIGN(RTA_PAYLOAD(tb1))) < 0)
  683. return NULL;
  684. if (tb[0] == NULL)
  685. return NULL;
  686. if (rtattr_parse(tb2, TCA_ACT_MAX, RTA_DATA(tb[0]),
  687. RTA_PAYLOAD(tb[0])) < 0)
  688. return NULL;
  689. kind = tb2[TCA_ACT_KIND-1];
  690. return kind;
  691. }
  692. static int
  693. tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
  694. {
  695. struct nlmsghdr *nlh;
  696. unsigned char *b = skb->tail;
  697. struct rtattr *x;
  698. struct tc_action_ops *a_o;
  699. struct tc_action a;
  700. int ret = 0;
  701. struct tcamsg *t = (struct tcamsg *) NLMSG_DATA(cb->nlh);
  702. struct rtattr *kind = find_dump_kind(cb->nlh);
  703. if (kind == NULL) {
  704. printk("tc_dump_action: action bad kind\n");
  705. return 0;
  706. }
  707. a_o = tc_lookup_action(kind);
  708. if (a_o == NULL) {
  709. return 0;
  710. }
  711. memset(&a, 0, sizeof(struct tc_action));
  712. a.ops = a_o;
  713. if (a_o->walk == NULL) {
  714. printk("tc_dump_action: %s !capable of dumping table\n", a_o->kind);
  715. goto rtattr_failure;
  716. }
  717. nlh = NLMSG_PUT(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
  718. cb->nlh->nlmsg_type, sizeof(*t));
  719. t = NLMSG_DATA(nlh);
  720. t->tca_family = AF_UNSPEC;
  721. t->tca__pad1 = 0;
  722. t->tca__pad2 = 0;
  723. x = (struct rtattr *) skb->tail;
  724. RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
  725. ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
  726. if (ret < 0)
  727. goto rtattr_failure;
  728. if (ret > 0) {
  729. x->rta_len = skb->tail - (u8 *) x;
  730. ret = skb->len;
  731. } else
  732. skb_trim(skb, (u8*)x - skb->data);
  733. nlh->nlmsg_len = skb->tail - b;
  734. if (NETLINK_CB(cb->skb).pid && ret)
  735. nlh->nlmsg_flags |= NLM_F_MULTI;
  736. module_put(a_o->owner);
  737. return skb->len;
  738. rtattr_failure:
  739. nlmsg_failure:
  740. module_put(a_o->owner);
  741. skb_trim(skb, b - skb->data);
  742. return skb->len;
  743. }
  744. static int __init tc_action_init(void)
  745. {
  746. struct rtnetlink_link *link_p = rtnetlink_links[PF_UNSPEC];
  747. if (link_p) {
  748. link_p[RTM_NEWACTION-RTM_BASE].doit = tc_ctl_action;
  749. link_p[RTM_DELACTION-RTM_BASE].doit = tc_ctl_action;
  750. link_p[RTM_GETACTION-RTM_BASE].doit = tc_ctl_action;
  751. link_p[RTM_GETACTION-RTM_BASE].dumpit = tc_dump_action;
  752. }
  753. return 0;
  754. }
  755. subsys_initcall(tc_action_init);
  756. EXPORT_SYMBOL(tcf_register_action);
  757. EXPORT_SYMBOL(tcf_unregister_action);
  758. EXPORT_SYMBOL(tcf_action_exec);
  759. EXPORT_SYMBOL(tcf_action_dump_1);