act_api.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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 1 /* 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);
  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. if (skb->tc_classid > 0) {
  168. res->classid = skb->tc_classid;
  169. res->class = 0;
  170. skb->tc_classid = 0;
  171. }
  172. return ret;
  173. }
  174. void tcf_action_destroy(struct tc_action *act, int bind)
  175. {
  176. struct tc_action *a;
  177. for (a = act; a; a = act) {
  178. if (a->ops && a->ops->cleanup) {
  179. DPRINTK("tcf_action_destroy destroying %p next %p\n",
  180. a, a->next);
  181. if (a->ops->cleanup(a, bind) == ACT_P_DELETED)
  182. module_put(a->ops->owner);
  183. act = act->next;
  184. kfree(a);
  185. } else { /*FIXME: Remove later - catch insertion bugs*/
  186. printk("tcf_action_destroy: BUG? destroying NULL ops\n");
  187. act = act->next;
  188. kfree(a);
  189. }
  190. }
  191. }
  192. int
  193. tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  194. {
  195. int err = -EINVAL;
  196. if (a->ops == NULL || a->ops->dump == NULL)
  197. return err;
  198. return a->ops->dump(skb, a, bind, ref);
  199. }
  200. int
  201. tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  202. {
  203. int err = -EINVAL;
  204. unsigned char *b = skb->tail;
  205. struct rtattr *r;
  206. if (a->ops == NULL || a->ops->dump == NULL)
  207. return err;
  208. RTA_PUT(skb, TCA_KIND, IFNAMSIZ, a->ops->kind);
  209. if (tcf_action_copy_stats(skb, a, 0))
  210. goto rtattr_failure;
  211. r = (struct rtattr*) skb->tail;
  212. RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
  213. if ((err = tcf_action_dump_old(skb, a, bind, ref)) > 0) {
  214. r->rta_len = skb->tail - (u8*)r;
  215. return err;
  216. }
  217. rtattr_failure:
  218. skb_trim(skb, b - skb->data);
  219. return -1;
  220. }
  221. int
  222. tcf_action_dump(struct sk_buff *skb, struct tc_action *act, int bind, int ref)
  223. {
  224. struct tc_action *a;
  225. int err = -EINVAL;
  226. unsigned char *b = skb->tail;
  227. struct rtattr *r ;
  228. while ((a = act) != NULL) {
  229. r = (struct rtattr*) skb->tail;
  230. act = a->next;
  231. RTA_PUT(skb, a->order, 0, NULL);
  232. err = tcf_action_dump_1(skb, a, bind, ref);
  233. if (err < 0)
  234. goto rtattr_failure;
  235. r->rta_len = skb->tail - (u8*)r;
  236. }
  237. return 0;
  238. rtattr_failure:
  239. skb_trim(skb, b - skb->data);
  240. return -err;
  241. }
  242. struct tc_action *tcf_action_init_1(struct rtattr *rta, struct rtattr *est,
  243. char *name, int ovr, int bind, int *err)
  244. {
  245. struct tc_action *a;
  246. struct tc_action_ops *a_o;
  247. char act_name[IFNAMSIZ];
  248. struct rtattr *tb[TCA_ACT_MAX+1];
  249. struct rtattr *kind;
  250. *err = -EINVAL;
  251. if (name == NULL) {
  252. if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
  253. goto err_out;
  254. kind = tb[TCA_ACT_KIND-1];
  255. if (kind == NULL)
  256. goto err_out;
  257. if (rtattr_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
  258. goto err_out;
  259. } else {
  260. if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
  261. goto err_out;
  262. }
  263. a_o = tc_lookup_action_n(act_name);
  264. if (a_o == NULL) {
  265. #ifdef CONFIG_KMOD
  266. rtnl_unlock();
  267. request_module(act_name);
  268. rtnl_lock();
  269. a_o = tc_lookup_action_n(act_name);
  270. /* We dropped the RTNL semaphore in order to
  271. * perform the module load. So, even if we
  272. * succeeded in loading the module we have to
  273. * tell the caller to replay the request. We
  274. * indicate this using -EAGAIN.
  275. */
  276. if (a_o != NULL) {
  277. *err = -EAGAIN;
  278. goto err_mod;
  279. }
  280. #endif
  281. goto err_out;
  282. }
  283. *err = -ENOMEM;
  284. a = kmalloc(sizeof(*a), GFP_KERNEL);
  285. if (a == NULL)
  286. goto err_mod;
  287. memset(a, 0, sizeof(*a));
  288. /* backward compatibility for policer */
  289. if (name == NULL)
  290. *err = a_o->init(tb[TCA_ACT_OPTIONS-1], est, a, ovr, bind);
  291. else
  292. *err = a_o->init(rta, est, a, ovr, bind);
  293. if (*err < 0)
  294. goto err_free;
  295. /* module count goes up only when brand new policy is created
  296. if it exists and is only bound to in a_o->init() then
  297. ACT_P_CREATED is not returned (a zero is).
  298. */
  299. if (*err != ACT_P_CREATED)
  300. module_put(a_o->owner);
  301. a->ops = a_o;
  302. DPRINTK("tcf_action_init_1: successfull %s\n", act_name);
  303. *err = 0;
  304. return a;
  305. err_free:
  306. kfree(a);
  307. err_mod:
  308. module_put(a_o->owner);
  309. err_out:
  310. return NULL;
  311. }
  312. struct tc_action *tcf_action_init(struct rtattr *rta, struct rtattr *est,
  313. char *name, int ovr, int bind, int *err)
  314. {
  315. struct rtattr *tb[TCA_ACT_MAX_PRIO+1];
  316. struct tc_action *head = NULL, *act, *act_prev = NULL;
  317. int i;
  318. if (rtattr_parse_nested(tb, TCA_ACT_MAX_PRIO, rta) < 0) {
  319. *err = -EINVAL;
  320. return head;
  321. }
  322. for (i=0; i < TCA_ACT_MAX_PRIO && tb[i]; i++) {
  323. act = tcf_action_init_1(tb[i], est, name, ovr, bind, err);
  324. if (act == NULL)
  325. goto err;
  326. act->order = i+1;
  327. if (head == NULL)
  328. head = act;
  329. else
  330. act_prev->next = act;
  331. act_prev = act;
  332. }
  333. return head;
  334. err:
  335. if (head != NULL)
  336. tcf_action_destroy(head, bind);
  337. return NULL;
  338. }
  339. int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
  340. int compat_mode)
  341. {
  342. int err = 0;
  343. struct gnet_dump d;
  344. struct tcf_act_hdr *h = a->priv;
  345. if (h == NULL)
  346. goto errout;
  347. /* compat_mode being true specifies a call that is supposed
  348. * to add additional backward compatiblity statistic TLVs.
  349. */
  350. if (compat_mode) {
  351. if (a->type == TCA_OLD_COMPAT)
  352. err = gnet_stats_start_copy_compat(skb, 0,
  353. TCA_STATS, TCA_XSTATS, h->stats_lock, &d);
  354. else
  355. return 0;
  356. } else
  357. err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
  358. h->stats_lock, &d);
  359. if (err < 0)
  360. goto errout;
  361. if (a->ops != NULL && a->ops->get_stats != NULL)
  362. if (a->ops->get_stats(skb, a) < 0)
  363. goto errout;
  364. if (gnet_stats_copy_basic(&d, &h->bstats) < 0 ||
  365. #ifdef CONFIG_NET_ESTIMATOR
  366. gnet_stats_copy_rate_est(&d, &h->rate_est) < 0 ||
  367. #endif
  368. gnet_stats_copy_queue(&d, &h->qstats) < 0)
  369. goto errout;
  370. if (gnet_stats_finish_copy(&d) < 0)
  371. goto errout;
  372. return 0;
  373. errout:
  374. return -1;
  375. }
  376. static int
  377. tca_get_fill(struct sk_buff *skb, struct tc_action *a, u32 pid, u32 seq,
  378. u16 flags, int event, int bind, int ref)
  379. {
  380. struct tcamsg *t;
  381. struct nlmsghdr *nlh;
  382. unsigned char *b = skb->tail;
  383. struct rtattr *x;
  384. nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
  385. t = NLMSG_DATA(nlh);
  386. t->tca_family = AF_UNSPEC;
  387. x = (struct rtattr*) skb->tail;
  388. RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
  389. if (tcf_action_dump(skb, a, bind, ref) < 0)
  390. goto rtattr_failure;
  391. x->rta_len = skb->tail - (u8*)x;
  392. nlh->nlmsg_len = skb->tail - b;
  393. return skb->len;
  394. rtattr_failure:
  395. nlmsg_failure:
  396. skb_trim(skb, b - skb->data);
  397. return -1;
  398. }
  399. static int
  400. act_get_notify(u32 pid, struct nlmsghdr *n, struct tc_action *a, int event)
  401. {
  402. struct sk_buff *skb;
  403. int err = 0;
  404. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  405. if (!skb)
  406. return -ENOBUFS;
  407. if (tca_get_fill(skb, a, pid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
  408. kfree_skb(skb);
  409. return -EINVAL;
  410. }
  411. err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
  412. if (err > 0)
  413. err = 0;
  414. return err;
  415. }
  416. static struct tc_action *
  417. tcf_action_get_1(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int *err)
  418. {
  419. struct rtattr *tb[TCA_ACT_MAX+1];
  420. struct tc_action *a;
  421. int index;
  422. *err = -EINVAL;
  423. if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
  424. return NULL;
  425. if (tb[TCA_ACT_INDEX - 1] == NULL ||
  426. RTA_PAYLOAD(tb[TCA_ACT_INDEX - 1]) < sizeof(index))
  427. return NULL;
  428. index = *(int *)RTA_DATA(tb[TCA_ACT_INDEX - 1]);
  429. *err = -ENOMEM;
  430. a = kmalloc(sizeof(struct tc_action), GFP_KERNEL);
  431. if (a == NULL)
  432. return NULL;
  433. memset(a, 0, sizeof(struct tc_action));
  434. *err = -EINVAL;
  435. a->ops = tc_lookup_action(tb[TCA_ACT_KIND - 1]);
  436. if (a->ops == NULL)
  437. goto err_free;
  438. if (a->ops->lookup == NULL)
  439. goto err_mod;
  440. *err = -ENOENT;
  441. if (a->ops->lookup(a, index) == 0)
  442. goto err_mod;
  443. module_put(a->ops->owner);
  444. *err = 0;
  445. return a;
  446. err_mod:
  447. module_put(a->ops->owner);
  448. err_free:
  449. kfree(a);
  450. return NULL;
  451. }
  452. static void cleanup_a(struct tc_action *act)
  453. {
  454. struct tc_action *a;
  455. for (a = act; a; a = act) {
  456. act = a->next;
  457. kfree(a);
  458. }
  459. }
  460. static struct tc_action *create_a(int i)
  461. {
  462. struct tc_action *act;
  463. act = kmalloc(sizeof(*act), GFP_KERNEL);
  464. if (act == NULL) {
  465. printk("create_a: failed to alloc!\n");
  466. return NULL;
  467. }
  468. memset(act, 0, sizeof(*act));
  469. act->order = i;
  470. return act;
  471. }
  472. static int tca_action_flush(struct rtattr *rta, struct nlmsghdr *n, u32 pid)
  473. {
  474. struct sk_buff *skb;
  475. unsigned char *b;
  476. struct nlmsghdr *nlh;
  477. struct tcamsg *t;
  478. struct netlink_callback dcb;
  479. struct rtattr *x;
  480. struct rtattr *tb[TCA_ACT_MAX+1];
  481. struct rtattr *kind;
  482. struct tc_action *a = create_a(0);
  483. int err = -EINVAL;
  484. if (a == NULL) {
  485. printk("tca_action_flush: couldnt create tc_action\n");
  486. return err;
  487. }
  488. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  489. if (!skb) {
  490. printk("tca_action_flush: failed skb alloc\n");
  491. kfree(a);
  492. return -ENOBUFS;
  493. }
  494. b = (unsigned char *)skb->tail;
  495. if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
  496. goto err_out;
  497. kind = tb[TCA_ACT_KIND-1];
  498. a->ops = tc_lookup_action(kind);
  499. if (a->ops == NULL)
  500. goto err_out;
  501. nlh = NLMSG_PUT(skb, pid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t));
  502. t = NLMSG_DATA(nlh);
  503. t->tca_family = AF_UNSPEC;
  504. x = (struct rtattr *) skb->tail;
  505. RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
  506. err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
  507. if (err < 0)
  508. goto rtattr_failure;
  509. x->rta_len = skb->tail - (u8 *) x;
  510. nlh->nlmsg_len = skb->tail - b;
  511. nlh->nlmsg_flags |= NLM_F_ROOT;
  512. module_put(a->ops->owner);
  513. kfree(a);
  514. err = rtnetlink_send(skb, pid, RTMGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
  515. if (err > 0)
  516. return 0;
  517. return err;
  518. rtattr_failure:
  519. module_put(a->ops->owner);
  520. nlmsg_failure:
  521. err_out:
  522. kfree_skb(skb);
  523. kfree(a);
  524. return err;
  525. }
  526. static int
  527. tca_action_gd(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int event)
  528. {
  529. int i, ret = 0;
  530. struct rtattr *tb[TCA_ACT_MAX_PRIO+1];
  531. struct tc_action *head = NULL, *act, *act_prev = NULL;
  532. if (rtattr_parse_nested(tb, TCA_ACT_MAX_PRIO, rta) < 0)
  533. return -EINVAL;
  534. if (event == RTM_DELACTION && n->nlmsg_flags&NLM_F_ROOT) {
  535. if (tb[0] != NULL && tb[1] == NULL)
  536. return tca_action_flush(tb[0], n, pid);
  537. }
  538. for (i=0; i < TCA_ACT_MAX_PRIO && tb[i]; i++) {
  539. act = tcf_action_get_1(tb[i], n, pid, &ret);
  540. if (act == NULL)
  541. goto err;
  542. act->order = i+1;
  543. if (head == NULL)
  544. head = act;
  545. else
  546. act_prev->next = act;
  547. act_prev = act;
  548. }
  549. if (event == RTM_GETACTION)
  550. ret = act_get_notify(pid, n, head, event);
  551. else { /* delete */
  552. struct sk_buff *skb;
  553. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  554. if (!skb) {
  555. ret = -ENOBUFS;
  556. goto err;
  557. }
  558. if (tca_get_fill(skb, head, pid, n->nlmsg_seq, 0, event,
  559. 0, 1) <= 0) {
  560. kfree_skb(skb);
  561. ret = -EINVAL;
  562. goto err;
  563. }
  564. /* now do the delete */
  565. tcf_action_destroy(head, 0);
  566. ret = rtnetlink_send(skb, pid, RTMGRP_TC,
  567. n->nlmsg_flags&NLM_F_ECHO);
  568. if (ret > 0)
  569. return 0;
  570. return ret;
  571. }
  572. err:
  573. cleanup_a(head);
  574. return ret;
  575. }
  576. static int tcf_add_notify(struct tc_action *a, u32 pid, u32 seq, int event,
  577. u16 flags)
  578. {
  579. struct tcamsg *t;
  580. struct nlmsghdr *nlh;
  581. struct sk_buff *skb;
  582. struct rtattr *x;
  583. unsigned char *b;
  584. int err = 0;
  585. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  586. if (!skb)
  587. return -ENOBUFS;
  588. b = (unsigned char *)skb->tail;
  589. nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
  590. t = NLMSG_DATA(nlh);
  591. t->tca_family = AF_UNSPEC;
  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_groups = RTMGRP_TC;
  599. err = rtnetlink_send(skb, pid, RTMGRP_TC, flags&NLM_F_ECHO);
  600. if (err > 0)
  601. err = 0;
  602. return err;
  603. rtattr_failure:
  604. nlmsg_failure:
  605. skb_trim(skb, b - skb->data);
  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. x = (struct rtattr *) skb->tail;
  723. RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
  724. ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
  725. if (ret < 0)
  726. goto rtattr_failure;
  727. if (ret > 0) {
  728. x->rta_len = skb->tail - (u8 *) x;
  729. ret = skb->len;
  730. } else
  731. skb_trim(skb, (u8*)x - skb->data);
  732. nlh->nlmsg_len = skb->tail - b;
  733. if (NETLINK_CB(cb->skb).pid && ret)
  734. nlh->nlmsg_flags |= NLM_F_MULTI;
  735. module_put(a_o->owner);
  736. return skb->len;
  737. rtattr_failure:
  738. nlmsg_failure:
  739. module_put(a_o->owner);
  740. skb_trim(skb, b - skb->data);
  741. return skb->len;
  742. }
  743. static int __init tc_action_init(void)
  744. {
  745. struct rtnetlink_link *link_p = rtnetlink_links[PF_UNSPEC];
  746. if (link_p) {
  747. link_p[RTM_NEWACTION-RTM_BASE].doit = tc_ctl_action;
  748. link_p[RTM_DELACTION-RTM_BASE].doit = tc_ctl_action;
  749. link_p[RTM_GETACTION-RTM_BASE].doit = tc_ctl_action;
  750. link_p[RTM_GETACTION-RTM_BASE].dumpit = tc_dump_action;
  751. }
  752. printk("TC classifier action (bugs to netdev@vger.kernel.org cc "
  753. "hadi@cyberus.ca)\n");
  754. return 0;
  755. }
  756. subsys_initcall(tc_action_init);
  757. EXPORT_SYMBOL(tcf_register_action);
  758. EXPORT_SYMBOL(tcf_unregister_action);
  759. EXPORT_SYMBOL(tcf_action_exec);
  760. EXPORT_SYMBOL(tcf_action_dump_1);