act_api.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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. t->tca__pad1 = 0;
  388. t->tca__pad2 = 0;
  389. x = (struct rtattr*) skb->tail;
  390. RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
  391. if (tcf_action_dump(skb, a, bind, ref) < 0)
  392. goto rtattr_failure;
  393. x->rta_len = skb->tail - (u8*)x;
  394. nlh->nlmsg_len = skb->tail - b;
  395. return skb->len;
  396. rtattr_failure:
  397. nlmsg_failure:
  398. skb_trim(skb, b - skb->data);
  399. return -1;
  400. }
  401. static int
  402. act_get_notify(u32 pid, struct nlmsghdr *n, struct tc_action *a, int event)
  403. {
  404. struct sk_buff *skb;
  405. int err = 0;
  406. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  407. if (!skb)
  408. return -ENOBUFS;
  409. if (tca_get_fill(skb, a, pid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
  410. kfree_skb(skb);
  411. return -EINVAL;
  412. }
  413. err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
  414. if (err > 0)
  415. err = 0;
  416. return err;
  417. }
  418. static struct tc_action *
  419. tcf_action_get_1(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int *err)
  420. {
  421. struct rtattr *tb[TCA_ACT_MAX+1];
  422. struct tc_action *a;
  423. int index;
  424. *err = -EINVAL;
  425. if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
  426. return NULL;
  427. if (tb[TCA_ACT_INDEX - 1] == NULL ||
  428. RTA_PAYLOAD(tb[TCA_ACT_INDEX - 1]) < sizeof(index))
  429. return NULL;
  430. index = *(int *)RTA_DATA(tb[TCA_ACT_INDEX - 1]);
  431. *err = -ENOMEM;
  432. a = kmalloc(sizeof(struct tc_action), GFP_KERNEL);
  433. if (a == NULL)
  434. return NULL;
  435. memset(a, 0, sizeof(struct tc_action));
  436. *err = -EINVAL;
  437. a->ops = tc_lookup_action(tb[TCA_ACT_KIND - 1]);
  438. if (a->ops == NULL)
  439. goto err_free;
  440. if (a->ops->lookup == NULL)
  441. goto err_mod;
  442. *err = -ENOENT;
  443. if (a->ops->lookup(a, index) == 0)
  444. goto err_mod;
  445. module_put(a->ops->owner);
  446. *err = 0;
  447. return a;
  448. err_mod:
  449. module_put(a->ops->owner);
  450. err_free:
  451. kfree(a);
  452. return NULL;
  453. }
  454. static void cleanup_a(struct tc_action *act)
  455. {
  456. struct tc_action *a;
  457. for (a = act; a; a = act) {
  458. act = a->next;
  459. kfree(a);
  460. }
  461. }
  462. static struct tc_action *create_a(int i)
  463. {
  464. struct tc_action *act;
  465. act = kmalloc(sizeof(*act), GFP_KERNEL);
  466. if (act == NULL) {
  467. printk("create_a: failed to alloc!\n");
  468. return NULL;
  469. }
  470. memset(act, 0, sizeof(*act));
  471. act->order = i;
  472. return act;
  473. }
  474. static int tca_action_flush(struct rtattr *rta, struct nlmsghdr *n, u32 pid)
  475. {
  476. struct sk_buff *skb;
  477. unsigned char *b;
  478. struct nlmsghdr *nlh;
  479. struct tcamsg *t;
  480. struct netlink_callback dcb;
  481. struct rtattr *x;
  482. struct rtattr *tb[TCA_ACT_MAX+1];
  483. struct rtattr *kind;
  484. struct tc_action *a = create_a(0);
  485. int err = -EINVAL;
  486. if (a == NULL) {
  487. printk("tca_action_flush: couldnt create tc_action\n");
  488. return err;
  489. }
  490. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  491. if (!skb) {
  492. printk("tca_action_flush: failed skb alloc\n");
  493. kfree(a);
  494. return -ENOBUFS;
  495. }
  496. b = (unsigned char *)skb->tail;
  497. if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
  498. goto err_out;
  499. kind = tb[TCA_ACT_KIND-1];
  500. a->ops = tc_lookup_action(kind);
  501. if (a->ops == NULL)
  502. goto err_out;
  503. nlh = NLMSG_PUT(skb, pid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t));
  504. t = NLMSG_DATA(nlh);
  505. t->tca_family = AF_UNSPEC;
  506. t->tca__pad1 = 0;
  507. t->tca__pad2 = 0;
  508. x = (struct rtattr *) skb->tail;
  509. RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
  510. err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
  511. if (err < 0)
  512. goto rtattr_failure;
  513. x->rta_len = skb->tail - (u8 *) x;
  514. nlh->nlmsg_len = skb->tail - b;
  515. nlh->nlmsg_flags |= NLM_F_ROOT;
  516. module_put(a->ops->owner);
  517. kfree(a);
  518. err = rtnetlink_send(skb, pid, RTMGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
  519. if (err > 0)
  520. return 0;
  521. return err;
  522. rtattr_failure:
  523. module_put(a->ops->owner);
  524. nlmsg_failure:
  525. err_out:
  526. kfree_skb(skb);
  527. kfree(a);
  528. return err;
  529. }
  530. static int
  531. tca_action_gd(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int event)
  532. {
  533. int i, ret = 0;
  534. struct rtattr *tb[TCA_ACT_MAX_PRIO+1];
  535. struct tc_action *head = NULL, *act, *act_prev = NULL;
  536. if (rtattr_parse_nested(tb, TCA_ACT_MAX_PRIO, rta) < 0)
  537. return -EINVAL;
  538. if (event == RTM_DELACTION && n->nlmsg_flags&NLM_F_ROOT) {
  539. if (tb[0] != NULL && tb[1] == NULL)
  540. return tca_action_flush(tb[0], n, pid);
  541. }
  542. for (i=0; i < TCA_ACT_MAX_PRIO && tb[i]; i++) {
  543. act = tcf_action_get_1(tb[i], n, pid, &ret);
  544. if (act == NULL)
  545. goto err;
  546. act->order = i+1;
  547. if (head == NULL)
  548. head = act;
  549. else
  550. act_prev->next = act;
  551. act_prev = act;
  552. }
  553. if (event == RTM_GETACTION)
  554. ret = act_get_notify(pid, n, head, event);
  555. else { /* delete */
  556. struct sk_buff *skb;
  557. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  558. if (!skb) {
  559. ret = -ENOBUFS;
  560. goto err;
  561. }
  562. if (tca_get_fill(skb, head, pid, n->nlmsg_seq, 0, event,
  563. 0, 1) <= 0) {
  564. kfree_skb(skb);
  565. ret = -EINVAL;
  566. goto err;
  567. }
  568. /* now do the delete */
  569. tcf_action_destroy(head, 0);
  570. ret = rtnetlink_send(skb, pid, RTMGRP_TC,
  571. n->nlmsg_flags&NLM_F_ECHO);
  572. if (ret > 0)
  573. return 0;
  574. return ret;
  575. }
  576. err:
  577. cleanup_a(head);
  578. return ret;
  579. }
  580. static int tcf_add_notify(struct tc_action *a, u32 pid, u32 seq, int event,
  581. u16 flags)
  582. {
  583. struct tcamsg *t;
  584. struct nlmsghdr *nlh;
  585. struct sk_buff *skb;
  586. struct rtattr *x;
  587. unsigned char *b;
  588. int err = 0;
  589. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  590. if (!skb)
  591. return -ENOBUFS;
  592. b = (unsigned char *)skb->tail;
  593. nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
  594. t = NLMSG_DATA(nlh);
  595. t->tca_family = AF_UNSPEC;
  596. t->tca__pad1 = 0;
  597. t->tca__pad2 = 0;
  598. x = (struct rtattr*) skb->tail;
  599. RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
  600. if (tcf_action_dump(skb, a, 0, 0) < 0)
  601. goto rtattr_failure;
  602. x->rta_len = skb->tail - (u8*)x;
  603. nlh->nlmsg_len = skb->tail - b;
  604. NETLINK_CB(skb).dst_groups = RTMGRP_TC;
  605. err = rtnetlink_send(skb, pid, RTMGRP_TC, flags&NLM_F_ECHO);
  606. if (err > 0)
  607. err = 0;
  608. return err;
  609. rtattr_failure:
  610. nlmsg_failure:
  611. skb_trim(skb, b - skb->data);
  612. return -1;
  613. }
  614. static int
  615. tcf_action_add(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int ovr)
  616. {
  617. int ret = 0;
  618. struct tc_action *act;
  619. struct tc_action *a;
  620. u32 seq = n->nlmsg_seq;
  621. act = tcf_action_init(rta, NULL, NULL, ovr, 0, &ret);
  622. if (act == NULL)
  623. goto done;
  624. /* dump then free all the actions after update; inserted policy
  625. * stays intact
  626. * */
  627. ret = tcf_add_notify(act, pid, seq, RTM_NEWACTION, n->nlmsg_flags);
  628. for (a = act; a; a = act) {
  629. act = a->next;
  630. kfree(a);
  631. }
  632. done:
  633. return ret;
  634. }
  635. static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
  636. {
  637. struct rtattr **tca = arg;
  638. u32 pid = skb ? NETLINK_CB(skb).pid : 0;
  639. int ret = 0, ovr = 0;
  640. if (tca[TCA_ACT_TAB-1] == NULL) {
  641. printk("tc_ctl_action: received NO action attribs\n");
  642. return -EINVAL;
  643. }
  644. /* n->nlmsg_flags&NLM_F_CREATE
  645. * */
  646. switch (n->nlmsg_type) {
  647. case RTM_NEWACTION:
  648. /* we are going to assume all other flags
  649. * imply create only if it doesnt exist
  650. * Note that CREATE | EXCL implies that
  651. * but since we want avoid ambiguity (eg when flags
  652. * is zero) then just set this
  653. */
  654. if (n->nlmsg_flags&NLM_F_REPLACE)
  655. ovr = 1;
  656. replay:
  657. ret = tcf_action_add(tca[TCA_ACT_TAB-1], n, pid, ovr);
  658. if (ret == -EAGAIN)
  659. goto replay;
  660. break;
  661. case RTM_DELACTION:
  662. ret = tca_action_gd(tca[TCA_ACT_TAB-1], n, pid, RTM_DELACTION);
  663. break;
  664. case RTM_GETACTION:
  665. ret = tca_action_gd(tca[TCA_ACT_TAB-1], n, pid, RTM_GETACTION);
  666. break;
  667. default:
  668. BUG();
  669. }
  670. return ret;
  671. }
  672. static char *
  673. find_dump_kind(struct nlmsghdr *n)
  674. {
  675. struct rtattr *tb1, *tb2[TCA_ACT_MAX+1];
  676. struct rtattr *tb[TCA_ACT_MAX_PRIO + 1];
  677. struct rtattr *rta[TCAA_MAX + 1];
  678. struct rtattr *kind;
  679. int min_len = NLMSG_LENGTH(sizeof(struct tcamsg));
  680. int attrlen = n->nlmsg_len - NLMSG_ALIGN(min_len);
  681. struct rtattr *attr = (void *) n + NLMSG_ALIGN(min_len);
  682. if (rtattr_parse(rta, TCAA_MAX, attr, attrlen) < 0)
  683. return NULL;
  684. tb1 = rta[TCA_ACT_TAB - 1];
  685. if (tb1 == NULL)
  686. return NULL;
  687. if (rtattr_parse(tb, TCA_ACT_MAX_PRIO, RTA_DATA(tb1),
  688. NLMSG_ALIGN(RTA_PAYLOAD(tb1))) < 0)
  689. return NULL;
  690. if (tb[0] == NULL)
  691. return NULL;
  692. if (rtattr_parse(tb2, TCA_ACT_MAX, RTA_DATA(tb[0]),
  693. RTA_PAYLOAD(tb[0])) < 0)
  694. return NULL;
  695. kind = tb2[TCA_ACT_KIND-1];
  696. return (char *) RTA_DATA(kind);
  697. }
  698. static int
  699. tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
  700. {
  701. struct nlmsghdr *nlh;
  702. unsigned char *b = skb->tail;
  703. struct rtattr *x;
  704. struct tc_action_ops *a_o;
  705. struct tc_action a;
  706. int ret = 0;
  707. struct tcamsg *t = (struct tcamsg *) NLMSG_DATA(cb->nlh);
  708. char *kind = find_dump_kind(cb->nlh);
  709. if (kind == NULL) {
  710. printk("tc_dump_action: action bad kind\n");
  711. return 0;
  712. }
  713. a_o = tc_lookup_action_n(kind);
  714. if (a_o == NULL) {
  715. printk("failed to find %s\n", kind);
  716. return 0;
  717. }
  718. memset(&a, 0, sizeof(struct tc_action));
  719. a.ops = a_o;
  720. if (a_o->walk == NULL) {
  721. printk("tc_dump_action: %s !capable of dumping table\n", kind);
  722. goto rtattr_failure;
  723. }
  724. nlh = NLMSG_PUT(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
  725. cb->nlh->nlmsg_type, sizeof(*t));
  726. t = NLMSG_DATA(nlh);
  727. t->tca_family = AF_UNSPEC;
  728. t->tca__pad1 = 0;
  729. t->tca__pad2 = 0;
  730. x = (struct rtattr *) skb->tail;
  731. RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
  732. ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
  733. if (ret < 0)
  734. goto rtattr_failure;
  735. if (ret > 0) {
  736. x->rta_len = skb->tail - (u8 *) x;
  737. ret = skb->len;
  738. } else
  739. skb_trim(skb, (u8*)x - skb->data);
  740. nlh->nlmsg_len = skb->tail - b;
  741. if (NETLINK_CB(cb->skb).pid && ret)
  742. nlh->nlmsg_flags |= NLM_F_MULTI;
  743. module_put(a_o->owner);
  744. return skb->len;
  745. rtattr_failure:
  746. nlmsg_failure:
  747. module_put(a_o->owner);
  748. skb_trim(skb, b - skb->data);
  749. return skb->len;
  750. }
  751. static int __init tc_action_init(void)
  752. {
  753. struct rtnetlink_link *link_p = rtnetlink_links[PF_UNSPEC];
  754. if (link_p) {
  755. link_p[RTM_NEWACTION-RTM_BASE].doit = tc_ctl_action;
  756. link_p[RTM_DELACTION-RTM_BASE].doit = tc_ctl_action;
  757. link_p[RTM_GETACTION-RTM_BASE].doit = tc_ctl_action;
  758. link_p[RTM_GETACTION-RTM_BASE].dumpit = tc_dump_action;
  759. }
  760. printk("TC classifier action (bugs to netdev@vger.kernel.org cc "
  761. "hadi@cyberus.ca)\n");
  762. return 0;
  763. }
  764. subsys_initcall(tc_action_init);
  765. EXPORT_SYMBOL(tcf_register_action);
  766. EXPORT_SYMBOL(tcf_unregister_action);
  767. EXPORT_SYMBOL(tcf_action_exec);
  768. EXPORT_SYMBOL(tcf_action_dump_1);