act_api.c 19 KB

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