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 rtattr_failure;
  229. r->rta_len = skb->tail - (u8*)r;
  230. }
  231. return 0;
  232. rtattr_failure:
  233. skb_trim(skb, b - skb->data);
  234. return -err;
  235. }
  236. struct tc_action *tcf_action_init_1(struct rtattr *rta, struct rtattr *est,
  237. char *name, int ovr, int bind, int *err)
  238. {
  239. struct tc_action *a;
  240. struct tc_action_ops *a_o;
  241. char act_name[IFNAMSIZ];
  242. struct rtattr *tb[TCA_ACT_MAX+1];
  243. struct rtattr *kind;
  244. *err = -EINVAL;
  245. if (name == NULL) {
  246. if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
  247. goto err_out;
  248. kind = tb[TCA_ACT_KIND-1];
  249. if (kind == NULL)
  250. goto err_out;
  251. if (rtattr_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
  252. goto err_out;
  253. } else {
  254. if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
  255. goto err_out;
  256. }
  257. a_o = tc_lookup_action_n(act_name);
  258. if (a_o == NULL) {
  259. #ifdef CONFIG_KMOD
  260. rtnl_unlock();
  261. request_module("act_%s", act_name);
  262. rtnl_lock();
  263. a_o = tc_lookup_action_n(act_name);
  264. /* We dropped the RTNL semaphore in order to
  265. * perform the module load. So, even if we
  266. * succeeded in loading the module we have to
  267. * tell the caller to replay the request. We
  268. * indicate this using -EAGAIN.
  269. */
  270. if (a_o != NULL) {
  271. *err = -EAGAIN;
  272. goto err_mod;
  273. }
  274. #endif
  275. goto err_out;
  276. }
  277. *err = -ENOMEM;
  278. a = kmalloc(sizeof(*a), GFP_KERNEL);
  279. if (a == NULL)
  280. goto err_mod;
  281. memset(a, 0, sizeof(*a));
  282. /* backward compatibility for policer */
  283. if (name == NULL)
  284. *err = a_o->init(tb[TCA_ACT_OPTIONS-1], est, a, ovr, bind);
  285. else
  286. *err = a_o->init(rta, est, a, ovr, bind);
  287. if (*err < 0)
  288. goto err_free;
  289. /* module count goes up only when brand new policy is created
  290. if it exists and is only bound to in a_o->init() then
  291. ACT_P_CREATED is not returned (a zero is).
  292. */
  293. if (*err != ACT_P_CREATED)
  294. module_put(a_o->owner);
  295. a->ops = a_o;
  296. DPRINTK("tcf_action_init_1: successfull %s\n", act_name);
  297. *err = 0;
  298. return a;
  299. err_free:
  300. kfree(a);
  301. err_mod:
  302. module_put(a_o->owner);
  303. err_out:
  304. return NULL;
  305. }
  306. struct tc_action *tcf_action_init(struct rtattr *rta, struct rtattr *est,
  307. char *name, int ovr, int bind, int *err)
  308. {
  309. struct rtattr *tb[TCA_ACT_MAX_PRIO+1];
  310. struct tc_action *head = NULL, *act, *act_prev = NULL;
  311. int i;
  312. if (rtattr_parse_nested(tb, TCA_ACT_MAX_PRIO, rta) < 0) {
  313. *err = -EINVAL;
  314. return head;
  315. }
  316. for (i=0; i < TCA_ACT_MAX_PRIO && tb[i]; i++) {
  317. act = tcf_action_init_1(tb[i], est, name, ovr, bind, err);
  318. if (act == NULL)
  319. goto err;
  320. act->order = i+1;
  321. if (head == NULL)
  322. head = act;
  323. else
  324. act_prev->next = act;
  325. act_prev = act;
  326. }
  327. return head;
  328. err:
  329. if (head != NULL)
  330. tcf_action_destroy(head, bind);
  331. return NULL;
  332. }
  333. int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
  334. int compat_mode)
  335. {
  336. int err = 0;
  337. struct gnet_dump d;
  338. struct tcf_act_hdr *h = a->priv;
  339. if (h == NULL)
  340. goto errout;
  341. /* compat_mode being true specifies a call that is supposed
  342. * to add additional backward compatiblity statistic TLVs.
  343. */
  344. if (compat_mode) {
  345. if (a->type == TCA_OLD_COMPAT)
  346. err = gnet_stats_start_copy_compat(skb, 0,
  347. TCA_STATS, TCA_XSTATS, h->stats_lock, &d);
  348. else
  349. return 0;
  350. } else
  351. err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
  352. h->stats_lock, &d);
  353. if (err < 0)
  354. goto errout;
  355. if (a->ops != NULL && a->ops->get_stats != NULL)
  356. if (a->ops->get_stats(skb, a) < 0)
  357. goto errout;
  358. if (gnet_stats_copy_basic(&d, &h->bstats) < 0 ||
  359. #ifdef CONFIG_NET_ESTIMATOR
  360. gnet_stats_copy_rate_est(&d, &h->rate_est) < 0 ||
  361. #endif
  362. gnet_stats_copy_queue(&d, &h->qstats) < 0)
  363. goto errout;
  364. if (gnet_stats_finish_copy(&d) < 0)
  365. goto errout;
  366. return 0;
  367. errout:
  368. return -1;
  369. }
  370. static int
  371. tca_get_fill(struct sk_buff *skb, struct tc_action *a, u32 pid, u32 seq,
  372. u16 flags, int event, int bind, int ref)
  373. {
  374. struct tcamsg *t;
  375. struct nlmsghdr *nlh;
  376. unsigned char *b = skb->tail;
  377. struct rtattr *x;
  378. nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
  379. t = NLMSG_DATA(nlh);
  380. t->tca_family = AF_UNSPEC;
  381. t->tca__pad1 = 0;
  382. t->tca__pad2 = 0;
  383. x = (struct rtattr*) skb->tail;
  384. RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
  385. if (tcf_action_dump(skb, a, bind, ref) < 0)
  386. goto rtattr_failure;
  387. x->rta_len = skb->tail - (u8*)x;
  388. nlh->nlmsg_len = skb->tail - b;
  389. return skb->len;
  390. rtattr_failure:
  391. nlmsg_failure:
  392. skb_trim(skb, b - skb->data);
  393. return -1;
  394. }
  395. static int
  396. act_get_notify(u32 pid, struct nlmsghdr *n, struct tc_action *a, int event)
  397. {
  398. struct sk_buff *skb;
  399. int err = 0;
  400. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  401. if (!skb)
  402. return -ENOBUFS;
  403. if (tca_get_fill(skb, a, pid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
  404. kfree_skb(skb);
  405. return -EINVAL;
  406. }
  407. err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
  408. if (err > 0)
  409. err = 0;
  410. return err;
  411. }
  412. static struct tc_action *
  413. tcf_action_get_1(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int *err)
  414. {
  415. struct rtattr *tb[TCA_ACT_MAX+1];
  416. struct tc_action *a;
  417. int index;
  418. *err = -EINVAL;
  419. if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
  420. return NULL;
  421. if (tb[TCA_ACT_INDEX - 1] == NULL ||
  422. RTA_PAYLOAD(tb[TCA_ACT_INDEX - 1]) < sizeof(index))
  423. return NULL;
  424. index = *(int *)RTA_DATA(tb[TCA_ACT_INDEX - 1]);
  425. *err = -ENOMEM;
  426. a = kmalloc(sizeof(struct tc_action), GFP_KERNEL);
  427. if (a == NULL)
  428. return NULL;
  429. memset(a, 0, sizeof(struct tc_action));
  430. *err = -EINVAL;
  431. a->ops = tc_lookup_action(tb[TCA_ACT_KIND - 1]);
  432. if (a->ops == NULL)
  433. goto err_free;
  434. if (a->ops->lookup == NULL)
  435. goto err_mod;
  436. *err = -ENOENT;
  437. if (a->ops->lookup(a, index) == 0)
  438. goto err_mod;
  439. module_put(a->ops->owner);
  440. *err = 0;
  441. return a;
  442. err_mod:
  443. module_put(a->ops->owner);
  444. err_free:
  445. kfree(a);
  446. return NULL;
  447. }
  448. static void cleanup_a(struct tc_action *act)
  449. {
  450. struct tc_action *a;
  451. for (a = act; a; a = act) {
  452. act = a->next;
  453. kfree(a);
  454. }
  455. }
  456. static struct tc_action *create_a(int i)
  457. {
  458. struct tc_action *act;
  459. act = kmalloc(sizeof(*act), GFP_KERNEL);
  460. if (act == NULL) {
  461. printk("create_a: failed to alloc!\n");
  462. return NULL;
  463. }
  464. memset(act, 0, sizeof(*act));
  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. module_put(a->ops->owner);
  518. nlmsg_failure:
  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 char *
  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 (char *) RTA_DATA(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. char *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_n(kind);
  708. if (a_o == NULL) {
  709. printk("failed to find %s\n", kind);
  710. return 0;
  711. }
  712. memset(&a, 0, sizeof(struct tc_action));
  713. a.ops = a_o;
  714. if (a_o->walk == NULL) {
  715. printk("tc_dump_action: %s !capable of dumping table\n", kind);
  716. goto rtattr_failure;
  717. }
  718. nlh = NLMSG_PUT(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
  719. cb->nlh->nlmsg_type, sizeof(*t));
  720. t = NLMSG_DATA(nlh);
  721. t->tca_family = AF_UNSPEC;
  722. t->tca__pad1 = 0;
  723. t->tca__pad2 = 0;
  724. x = (struct rtattr *) skb->tail;
  725. RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
  726. ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
  727. if (ret < 0)
  728. goto rtattr_failure;
  729. if (ret > 0) {
  730. x->rta_len = skb->tail - (u8 *) x;
  731. ret = skb->len;
  732. } else
  733. skb_trim(skb, (u8*)x - skb->data);
  734. nlh->nlmsg_len = skb->tail - b;
  735. if (NETLINK_CB(cb->skb).pid && ret)
  736. nlh->nlmsg_flags |= NLM_F_MULTI;
  737. module_put(a_o->owner);
  738. return skb->len;
  739. rtattr_failure:
  740. nlmsg_failure:
  741. module_put(a_o->owner);
  742. skb_trim(skb, b - skb->data);
  743. return skb->len;
  744. }
  745. static int __init tc_action_init(void)
  746. {
  747. struct rtnetlink_link *link_p = rtnetlink_links[PF_UNSPEC];
  748. if (link_p) {
  749. link_p[RTM_NEWACTION-RTM_BASE].doit = tc_ctl_action;
  750. link_p[RTM_DELACTION-RTM_BASE].doit = tc_ctl_action;
  751. link_p[RTM_GETACTION-RTM_BASE].doit = tc_ctl_action;
  752. link_p[RTM_GETACTION-RTM_BASE].dumpit = tc_dump_action;
  753. }
  754. printk("TC classifier action (bugs to netdev@vger.kernel.org cc "
  755. "hadi@cyberus.ca)\n");
  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);