act_api.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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 = kmalloc(sizeof(*a), GFP_KERNEL);
  282. if (a == NULL)
  283. goto err_mod;
  284. memset(a, 0, sizeof(*a));
  285. /* backward compatibility for policer */
  286. if (name == NULL)
  287. *err = a_o->init(tb[TCA_ACT_OPTIONS-1], est, a, ovr, bind);
  288. else
  289. *err = a_o->init(rta, est, a, ovr, bind);
  290. if (*err < 0)
  291. goto err_free;
  292. /* module count goes up only when brand new policy is created
  293. if it exists and is only bound to in a_o->init() then
  294. ACT_P_CREATED is not returned (a zero is).
  295. */
  296. if (*err != ACT_P_CREATED)
  297. module_put(a_o->owner);
  298. a->ops = a_o;
  299. DPRINTK("tcf_action_init_1: successfull %s\n", act_name);
  300. *err = 0;
  301. return a;
  302. err_free:
  303. kfree(a);
  304. err_mod:
  305. module_put(a_o->owner);
  306. err_out:
  307. return NULL;
  308. }
  309. struct tc_action *tcf_action_init(struct rtattr *rta, struct rtattr *est,
  310. char *name, int ovr, int bind, int *err)
  311. {
  312. struct rtattr *tb[TCA_ACT_MAX_PRIO+1];
  313. struct tc_action *head = NULL, *act, *act_prev = NULL;
  314. int i;
  315. if (rtattr_parse_nested(tb, TCA_ACT_MAX_PRIO, rta) < 0) {
  316. *err = -EINVAL;
  317. return head;
  318. }
  319. for (i=0; i < TCA_ACT_MAX_PRIO && tb[i]; i++) {
  320. act = tcf_action_init_1(tb[i], est, name, ovr, bind, err);
  321. if (act == NULL)
  322. goto err;
  323. act->order = i+1;
  324. if (head == NULL)
  325. head = act;
  326. else
  327. act_prev->next = act;
  328. act_prev = act;
  329. }
  330. return head;
  331. err:
  332. if (head != NULL)
  333. tcf_action_destroy(head, bind);
  334. return NULL;
  335. }
  336. int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
  337. int compat_mode)
  338. {
  339. int err = 0;
  340. struct gnet_dump d;
  341. struct tcf_act_hdr *h = a->priv;
  342. if (h == NULL)
  343. goto errout;
  344. /* compat_mode being true specifies a call that is supposed
  345. * to add additional backward compatiblity statistic TLVs.
  346. */
  347. if (compat_mode) {
  348. if (a->type == TCA_OLD_COMPAT)
  349. err = gnet_stats_start_copy_compat(skb, 0,
  350. TCA_STATS, TCA_XSTATS, h->stats_lock, &d);
  351. else
  352. return 0;
  353. } else
  354. err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
  355. h->stats_lock, &d);
  356. if (err < 0)
  357. goto errout;
  358. if (a->ops != NULL && a->ops->get_stats != NULL)
  359. if (a->ops->get_stats(skb, a) < 0)
  360. goto errout;
  361. if (gnet_stats_copy_basic(&d, &h->bstats) < 0 ||
  362. #ifdef CONFIG_NET_ESTIMATOR
  363. gnet_stats_copy_rate_est(&d, &h->rate_est) < 0 ||
  364. #endif
  365. gnet_stats_copy_queue(&d, &h->qstats) < 0)
  366. goto errout;
  367. if (gnet_stats_finish_copy(&d) < 0)
  368. goto errout;
  369. return 0;
  370. errout:
  371. return -1;
  372. }
  373. static int
  374. tca_get_fill(struct sk_buff *skb, struct tc_action *a, u32 pid, u32 seq,
  375. u16 flags, int event, int bind, int ref)
  376. {
  377. struct tcamsg *t;
  378. struct nlmsghdr *nlh;
  379. unsigned char *b = skb->tail;
  380. struct rtattr *x;
  381. nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
  382. t = NLMSG_DATA(nlh);
  383. t->tca_family = AF_UNSPEC;
  384. t->tca__pad1 = 0;
  385. t->tca__pad2 = 0;
  386. x = (struct rtattr*) skb->tail;
  387. RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
  388. if (tcf_action_dump(skb, a, bind, ref) < 0)
  389. goto rtattr_failure;
  390. x->rta_len = skb->tail - (u8*)x;
  391. nlh->nlmsg_len = skb->tail - b;
  392. return skb->len;
  393. rtattr_failure:
  394. nlmsg_failure:
  395. skb_trim(skb, b - skb->data);
  396. return -1;
  397. }
  398. static int
  399. act_get_notify(u32 pid, struct nlmsghdr *n, struct tc_action *a, int event)
  400. {
  401. struct sk_buff *skb;
  402. int err = 0;
  403. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  404. if (!skb)
  405. return -ENOBUFS;
  406. if (tca_get_fill(skb, a, pid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
  407. kfree_skb(skb);
  408. return -EINVAL;
  409. }
  410. err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
  411. if (err > 0)
  412. err = 0;
  413. return err;
  414. }
  415. static struct tc_action *
  416. tcf_action_get_1(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int *err)
  417. {
  418. struct rtattr *tb[TCA_ACT_MAX+1];
  419. struct tc_action *a;
  420. int index;
  421. *err = -EINVAL;
  422. if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
  423. return NULL;
  424. if (tb[TCA_ACT_INDEX - 1] == NULL ||
  425. RTA_PAYLOAD(tb[TCA_ACT_INDEX - 1]) < sizeof(index))
  426. return NULL;
  427. index = *(int *)RTA_DATA(tb[TCA_ACT_INDEX - 1]);
  428. *err = -ENOMEM;
  429. a = kmalloc(sizeof(struct tc_action), GFP_KERNEL);
  430. if (a == NULL)
  431. return NULL;
  432. memset(a, 0, sizeof(struct tc_action));
  433. *err = -EINVAL;
  434. a->ops = tc_lookup_action(tb[TCA_ACT_KIND - 1]);
  435. if (a->ops == NULL)
  436. goto err_free;
  437. if (a->ops->lookup == NULL)
  438. goto err_mod;
  439. *err = -ENOENT;
  440. if (a->ops->lookup(a, index) == 0)
  441. goto err_mod;
  442. module_put(a->ops->owner);
  443. *err = 0;
  444. return a;
  445. err_mod:
  446. module_put(a->ops->owner);
  447. err_free:
  448. kfree(a);
  449. return NULL;
  450. }
  451. static void cleanup_a(struct tc_action *act)
  452. {
  453. struct tc_action *a;
  454. for (a = act; a; a = act) {
  455. act = a->next;
  456. kfree(a);
  457. }
  458. }
  459. static struct tc_action *create_a(int i)
  460. {
  461. struct tc_action *act;
  462. act = kmalloc(sizeof(*act), GFP_KERNEL);
  463. if (act == NULL) {
  464. printk("create_a: failed to alloc!\n");
  465. return NULL;
  466. }
  467. memset(act, 0, sizeof(*act));
  468. act->order = i;
  469. return act;
  470. }
  471. static int tca_action_flush(struct rtattr *rta, struct nlmsghdr *n, u32 pid)
  472. {
  473. struct sk_buff *skb;
  474. unsigned char *b;
  475. struct nlmsghdr *nlh;
  476. struct tcamsg *t;
  477. struct netlink_callback dcb;
  478. struct rtattr *x;
  479. struct rtattr *tb[TCA_ACT_MAX+1];
  480. struct rtattr *kind;
  481. struct tc_action *a = create_a(0);
  482. int err = -EINVAL;
  483. if (a == NULL) {
  484. printk("tca_action_flush: couldnt create tc_action\n");
  485. return err;
  486. }
  487. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  488. if (!skb) {
  489. printk("tca_action_flush: failed skb alloc\n");
  490. kfree(a);
  491. return -ENOBUFS;
  492. }
  493. b = (unsigned char *)skb->tail;
  494. if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
  495. goto err_out;
  496. kind = tb[TCA_ACT_KIND-1];
  497. a->ops = tc_lookup_action(kind);
  498. if (a->ops == NULL)
  499. goto err_out;
  500. nlh = NLMSG_PUT(skb, pid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t));
  501. t = NLMSG_DATA(nlh);
  502. t->tca_family = AF_UNSPEC;
  503. t->tca__pad1 = 0;
  504. t->tca__pad2 = 0;
  505. x = (struct rtattr *) skb->tail;
  506. RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
  507. err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
  508. if (err < 0)
  509. goto rtattr_failure;
  510. x->rta_len = skb->tail - (u8 *) x;
  511. nlh->nlmsg_len = skb->tail - b;
  512. nlh->nlmsg_flags |= NLM_F_ROOT;
  513. module_put(a->ops->owner);
  514. kfree(a);
  515. err = rtnetlink_send(skb, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
  516. if (err > 0)
  517. return 0;
  518. return err;
  519. rtattr_failure:
  520. nlmsg_failure:
  521. module_put(a->ops->owner);
  522. err_out:
  523. kfree_skb(skb);
  524. kfree(a);
  525. return err;
  526. }
  527. static int
  528. tca_action_gd(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int event)
  529. {
  530. int i, ret = 0;
  531. struct rtattr *tb[TCA_ACT_MAX_PRIO+1];
  532. struct tc_action *head = NULL, *act, *act_prev = NULL;
  533. if (rtattr_parse_nested(tb, TCA_ACT_MAX_PRIO, rta) < 0)
  534. return -EINVAL;
  535. if (event == RTM_DELACTION && n->nlmsg_flags&NLM_F_ROOT) {
  536. if (tb[0] != NULL && tb[1] == NULL)
  537. return tca_action_flush(tb[0], n, pid);
  538. }
  539. for (i=0; i < TCA_ACT_MAX_PRIO && tb[i]; i++) {
  540. act = tcf_action_get_1(tb[i], n, pid, &ret);
  541. if (act == NULL)
  542. goto err;
  543. act->order = i+1;
  544. if (head == NULL)
  545. head = act;
  546. else
  547. act_prev->next = act;
  548. act_prev = act;
  549. }
  550. if (event == RTM_GETACTION)
  551. ret = act_get_notify(pid, n, head, event);
  552. else { /* delete */
  553. struct sk_buff *skb;
  554. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  555. if (!skb) {
  556. ret = -ENOBUFS;
  557. goto err;
  558. }
  559. if (tca_get_fill(skb, head, pid, n->nlmsg_seq, 0, event,
  560. 0, 1) <= 0) {
  561. kfree_skb(skb);
  562. ret = -EINVAL;
  563. goto err;
  564. }
  565. /* now do the delete */
  566. tcf_action_destroy(head, 0);
  567. ret = rtnetlink_send(skb, pid, RTNLGRP_TC,
  568. n->nlmsg_flags&NLM_F_ECHO);
  569. if (ret > 0)
  570. return 0;
  571. return ret;
  572. }
  573. err:
  574. cleanup_a(head);
  575. return ret;
  576. }
  577. static int tcf_add_notify(struct tc_action *a, u32 pid, u32 seq, int event,
  578. u16 flags)
  579. {
  580. struct tcamsg *t;
  581. struct nlmsghdr *nlh;
  582. struct sk_buff *skb;
  583. struct rtattr *x;
  584. unsigned char *b;
  585. int err = 0;
  586. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  587. if (!skb)
  588. return -ENOBUFS;
  589. b = (unsigned char *)skb->tail;
  590. nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
  591. t = NLMSG_DATA(nlh);
  592. t->tca_family = AF_UNSPEC;
  593. t->tca__pad1 = 0;
  594. t->tca__pad2 = 0;
  595. x = (struct rtattr*) skb->tail;
  596. RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
  597. if (tcf_action_dump(skb, a, 0, 0) < 0)
  598. goto rtattr_failure;
  599. x->rta_len = skb->tail - (u8*)x;
  600. nlh->nlmsg_len = skb->tail - b;
  601. NETLINK_CB(skb).dst_group = RTNLGRP_TC;
  602. err = rtnetlink_send(skb, pid, RTNLGRP_TC, flags&NLM_F_ECHO);
  603. if (err > 0)
  604. err = 0;
  605. return err;
  606. rtattr_failure:
  607. nlmsg_failure:
  608. kfree_skb(skb);
  609. return -1;
  610. }
  611. static int
  612. tcf_action_add(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int ovr)
  613. {
  614. int ret = 0;
  615. struct tc_action *act;
  616. struct tc_action *a;
  617. u32 seq = n->nlmsg_seq;
  618. act = tcf_action_init(rta, NULL, NULL, ovr, 0, &ret);
  619. if (act == NULL)
  620. goto done;
  621. /* dump then free all the actions after update; inserted policy
  622. * stays intact
  623. * */
  624. ret = tcf_add_notify(act, pid, seq, RTM_NEWACTION, n->nlmsg_flags);
  625. for (a = act; a; a = act) {
  626. act = a->next;
  627. kfree(a);
  628. }
  629. done:
  630. return ret;
  631. }
  632. static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
  633. {
  634. struct rtattr **tca = arg;
  635. u32 pid = skb ? NETLINK_CB(skb).pid : 0;
  636. int ret = 0, ovr = 0;
  637. if (tca[TCA_ACT_TAB-1] == NULL) {
  638. printk("tc_ctl_action: received NO action attribs\n");
  639. return -EINVAL;
  640. }
  641. /* n->nlmsg_flags&NLM_F_CREATE
  642. * */
  643. switch (n->nlmsg_type) {
  644. case RTM_NEWACTION:
  645. /* we are going to assume all other flags
  646. * imply create only if it doesnt exist
  647. * Note that CREATE | EXCL implies that
  648. * but since we want avoid ambiguity (eg when flags
  649. * is zero) then just set this
  650. */
  651. if (n->nlmsg_flags&NLM_F_REPLACE)
  652. ovr = 1;
  653. replay:
  654. ret = tcf_action_add(tca[TCA_ACT_TAB-1], n, pid, ovr);
  655. if (ret == -EAGAIN)
  656. goto replay;
  657. break;
  658. case RTM_DELACTION:
  659. ret = tca_action_gd(tca[TCA_ACT_TAB-1], n, pid, RTM_DELACTION);
  660. break;
  661. case RTM_GETACTION:
  662. ret = tca_action_gd(tca[TCA_ACT_TAB-1], n, pid, RTM_GETACTION);
  663. break;
  664. default:
  665. BUG();
  666. }
  667. return ret;
  668. }
  669. static struct rtattr *
  670. find_dump_kind(struct nlmsghdr *n)
  671. {
  672. struct rtattr *tb1, *tb2[TCA_ACT_MAX+1];
  673. struct rtattr *tb[TCA_ACT_MAX_PRIO + 1];
  674. struct rtattr *rta[TCAA_MAX + 1];
  675. struct rtattr *kind;
  676. int min_len = NLMSG_LENGTH(sizeof(struct tcamsg));
  677. int attrlen = n->nlmsg_len - NLMSG_ALIGN(min_len);
  678. struct rtattr *attr = (void *) n + NLMSG_ALIGN(min_len);
  679. if (rtattr_parse(rta, TCAA_MAX, attr, attrlen) < 0)
  680. return NULL;
  681. tb1 = rta[TCA_ACT_TAB - 1];
  682. if (tb1 == NULL)
  683. return NULL;
  684. if (rtattr_parse(tb, TCA_ACT_MAX_PRIO, RTA_DATA(tb1),
  685. NLMSG_ALIGN(RTA_PAYLOAD(tb1))) < 0)
  686. return NULL;
  687. if (tb[0] == NULL)
  688. return NULL;
  689. if (rtattr_parse(tb2, TCA_ACT_MAX, RTA_DATA(tb[0]),
  690. RTA_PAYLOAD(tb[0])) < 0)
  691. return NULL;
  692. kind = tb2[TCA_ACT_KIND-1];
  693. return kind;
  694. }
  695. static int
  696. tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
  697. {
  698. struct nlmsghdr *nlh;
  699. unsigned char *b = skb->tail;
  700. struct rtattr *x;
  701. struct tc_action_ops *a_o;
  702. struct tc_action a;
  703. int ret = 0;
  704. struct tcamsg *t = (struct tcamsg *) NLMSG_DATA(cb->nlh);
  705. struct rtattr *kind = find_dump_kind(cb->nlh);
  706. if (kind == NULL) {
  707. printk("tc_dump_action: action bad kind\n");
  708. return 0;
  709. }
  710. a_o = tc_lookup_action(kind);
  711. if (a_o == NULL) {
  712. return 0;
  713. }
  714. memset(&a, 0, sizeof(struct tc_action));
  715. a.ops = a_o;
  716. if (a_o->walk == NULL) {
  717. printk("tc_dump_action: %s !capable of dumping table\n", a_o->kind);
  718. goto rtattr_failure;
  719. }
  720. nlh = NLMSG_PUT(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
  721. cb->nlh->nlmsg_type, sizeof(*t));
  722. t = NLMSG_DATA(nlh);
  723. t->tca_family = AF_UNSPEC;
  724. t->tca__pad1 = 0;
  725. t->tca__pad2 = 0;
  726. x = (struct rtattr *) skb->tail;
  727. RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
  728. ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
  729. if (ret < 0)
  730. goto rtattr_failure;
  731. if (ret > 0) {
  732. x->rta_len = skb->tail - (u8 *) x;
  733. ret = skb->len;
  734. } else
  735. skb_trim(skb, (u8*)x - skb->data);
  736. nlh->nlmsg_len = skb->tail - b;
  737. if (NETLINK_CB(cb->skb).pid && ret)
  738. nlh->nlmsg_flags |= NLM_F_MULTI;
  739. module_put(a_o->owner);
  740. return skb->len;
  741. rtattr_failure:
  742. nlmsg_failure:
  743. module_put(a_o->owner);
  744. skb_trim(skb, b - skb->data);
  745. return skb->len;
  746. }
  747. static int __init tc_action_init(void)
  748. {
  749. struct rtnetlink_link *link_p = rtnetlink_links[PF_UNSPEC];
  750. if (link_p) {
  751. link_p[RTM_NEWACTION-RTM_BASE].doit = tc_ctl_action;
  752. link_p[RTM_DELACTION-RTM_BASE].doit = tc_ctl_action;
  753. link_p[RTM_GETACTION-RTM_BASE].doit = tc_ctl_action;
  754. link_p[RTM_GETACTION-RTM_BASE].dumpit = tc_dump_action;
  755. }
  756. return 0;
  757. }
  758. subsys_initcall(tc_action_init);
  759. EXPORT_SYMBOL(tcf_register_action);
  760. EXPORT_SYMBOL(tcf_unregister_action);
  761. EXPORT_SYMBOL(tcf_action_exec);
  762. EXPORT_SYMBOL(tcf_action_dump_1);