act_api.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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 <linux/types.h>
  14. #include <linux/kernel.h>
  15. #include <linux/string.h>
  16. #include <linux/errno.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/init.h>
  19. #include <linux/kmod.h>
  20. #include <net/sch_generic.h>
  21. #include <net/act_api.h>
  22. #include <net/netlink.h>
  23. void tcf_hash_destroy(struct tcf_common *p, struct tcf_hashinfo *hinfo)
  24. {
  25. unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
  26. struct tcf_common **p1p;
  27. for (p1p = &hinfo->htab[h]; *p1p; p1p = &(*p1p)->tcfc_next) {
  28. if (*p1p == p) {
  29. write_lock_bh(hinfo->lock);
  30. *p1p = p->tcfc_next;
  31. write_unlock_bh(hinfo->lock);
  32. gen_kill_estimator(&p->tcfc_bstats,
  33. &p->tcfc_rate_est);
  34. kfree(p);
  35. return;
  36. }
  37. }
  38. BUG_TRAP(0);
  39. }
  40. EXPORT_SYMBOL(tcf_hash_destroy);
  41. int tcf_hash_release(struct tcf_common *p, int bind,
  42. struct tcf_hashinfo *hinfo)
  43. {
  44. int ret = 0;
  45. if (p) {
  46. if (bind)
  47. p->tcfc_bindcnt--;
  48. p->tcfc_refcnt--;
  49. if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) {
  50. tcf_hash_destroy(p, hinfo);
  51. ret = 1;
  52. }
  53. }
  54. return ret;
  55. }
  56. EXPORT_SYMBOL(tcf_hash_release);
  57. static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
  58. struct tc_action *a, struct tcf_hashinfo *hinfo)
  59. {
  60. struct tcf_common *p;
  61. int err = 0, index = -1,i = 0, s_i = 0, n_i = 0;
  62. struct rtattr *r ;
  63. read_lock_bh(hinfo->lock);
  64. s_i = cb->args[0];
  65. for (i = 0; i < (hinfo->hmask + 1); i++) {
  66. p = hinfo->htab[tcf_hash(i, hinfo->hmask)];
  67. for (; p; p = p->tcfc_next) {
  68. index++;
  69. if (index < s_i)
  70. continue;
  71. a->priv = p;
  72. a->order = n_i;
  73. r = (struct rtattr *)skb_tail_pointer(skb);
  74. RTA_PUT(skb, a->order, 0, NULL);
  75. err = tcf_action_dump_1(skb, a, 0, 0);
  76. if (err < 0) {
  77. index--;
  78. nlmsg_trim(skb, r);
  79. goto done;
  80. }
  81. r->rta_len = skb_tail_pointer(skb) - (u8 *)r;
  82. n_i++;
  83. if (n_i >= TCA_ACT_MAX_PRIO)
  84. goto done;
  85. }
  86. }
  87. done:
  88. read_unlock_bh(hinfo->lock);
  89. if (n_i)
  90. cb->args[0] += n_i;
  91. return n_i;
  92. rtattr_failure:
  93. nlmsg_trim(skb, r);
  94. goto done;
  95. }
  96. static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a,
  97. struct tcf_hashinfo *hinfo)
  98. {
  99. struct tcf_common *p, *s_p;
  100. struct rtattr *r ;
  101. int i= 0, n_i = 0;
  102. r = (struct rtattr *)skb_tail_pointer(skb);
  103. RTA_PUT(skb, a->order, 0, NULL);
  104. RTA_PUT(skb, TCA_KIND, IFNAMSIZ, a->ops->kind);
  105. for (i = 0; i < (hinfo->hmask + 1); i++) {
  106. p = hinfo->htab[tcf_hash(i, hinfo->hmask)];
  107. while (p != NULL) {
  108. s_p = p->tcfc_next;
  109. if (ACT_P_DELETED == tcf_hash_release(p, 0, hinfo))
  110. module_put(a->ops->owner);
  111. n_i++;
  112. p = s_p;
  113. }
  114. }
  115. RTA_PUT(skb, TCA_FCNT, 4, &n_i);
  116. r->rta_len = skb_tail_pointer(skb) - (u8 *)r;
  117. return n_i;
  118. rtattr_failure:
  119. nlmsg_trim(skb, r);
  120. return -EINVAL;
  121. }
  122. int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb,
  123. int type, struct tc_action *a)
  124. {
  125. struct tcf_hashinfo *hinfo = a->ops->hinfo;
  126. if (type == RTM_DELACTION) {
  127. return tcf_del_walker(skb, a, hinfo);
  128. } else if (type == RTM_GETACTION) {
  129. return tcf_dump_walker(skb, cb, a, hinfo);
  130. } else {
  131. printk("tcf_generic_walker: unknown action %d\n", type);
  132. return -EINVAL;
  133. }
  134. }
  135. EXPORT_SYMBOL(tcf_generic_walker);
  136. struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
  137. {
  138. struct tcf_common *p;
  139. read_lock_bh(hinfo->lock);
  140. for (p = hinfo->htab[tcf_hash(index, hinfo->hmask)]; p;
  141. p = p->tcfc_next) {
  142. if (p->tcfc_index == index)
  143. break;
  144. }
  145. read_unlock_bh(hinfo->lock);
  146. return p;
  147. }
  148. EXPORT_SYMBOL(tcf_hash_lookup);
  149. u32 tcf_hash_new_index(u32 *idx_gen, struct tcf_hashinfo *hinfo)
  150. {
  151. u32 val = *idx_gen;
  152. do {
  153. if (++val == 0)
  154. val = 1;
  155. } while (tcf_hash_lookup(val, hinfo));
  156. return (*idx_gen = val);
  157. }
  158. EXPORT_SYMBOL(tcf_hash_new_index);
  159. int tcf_hash_search(struct tc_action *a, u32 index)
  160. {
  161. struct tcf_hashinfo *hinfo = a->ops->hinfo;
  162. struct tcf_common *p = tcf_hash_lookup(index, hinfo);
  163. if (p) {
  164. a->priv = p;
  165. return 1;
  166. }
  167. return 0;
  168. }
  169. EXPORT_SYMBOL(tcf_hash_search);
  170. struct tcf_common *tcf_hash_check(u32 index, struct tc_action *a, int bind,
  171. struct tcf_hashinfo *hinfo)
  172. {
  173. struct tcf_common *p = NULL;
  174. if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
  175. if (bind) {
  176. p->tcfc_bindcnt++;
  177. p->tcfc_refcnt++;
  178. }
  179. a->priv = p;
  180. }
  181. return p;
  182. }
  183. EXPORT_SYMBOL(tcf_hash_check);
  184. struct tcf_common *tcf_hash_create(u32 index, struct rtattr *est, struct tc_action *a, int size, int bind, u32 *idx_gen, struct tcf_hashinfo *hinfo)
  185. {
  186. struct tcf_common *p = kzalloc(size, GFP_KERNEL);
  187. if (unlikely(!p))
  188. return p;
  189. p->tcfc_refcnt = 1;
  190. if (bind)
  191. p->tcfc_bindcnt = 1;
  192. spin_lock_init(&p->tcfc_lock);
  193. p->tcfc_index = index ? index : tcf_hash_new_index(idx_gen, hinfo);
  194. p->tcfc_tm.install = jiffies;
  195. p->tcfc_tm.lastuse = jiffies;
  196. if (est)
  197. gen_new_estimator(&p->tcfc_bstats, &p->tcfc_rate_est,
  198. &p->tcfc_lock, est);
  199. a->priv = (void *) p;
  200. return p;
  201. }
  202. EXPORT_SYMBOL(tcf_hash_create);
  203. void tcf_hash_insert(struct tcf_common *p, struct tcf_hashinfo *hinfo)
  204. {
  205. unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
  206. write_lock_bh(hinfo->lock);
  207. p->tcfc_next = hinfo->htab[h];
  208. hinfo->htab[h] = p;
  209. write_unlock_bh(hinfo->lock);
  210. }
  211. EXPORT_SYMBOL(tcf_hash_insert);
  212. static struct tc_action_ops *act_base = NULL;
  213. static DEFINE_RWLOCK(act_mod_lock);
  214. int tcf_register_action(struct tc_action_ops *act)
  215. {
  216. struct tc_action_ops *a, **ap;
  217. write_lock(&act_mod_lock);
  218. for (ap = &act_base; (a = *ap) != NULL; ap = &a->next) {
  219. if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
  220. write_unlock(&act_mod_lock);
  221. return -EEXIST;
  222. }
  223. }
  224. act->next = NULL;
  225. *ap = act;
  226. write_unlock(&act_mod_lock);
  227. return 0;
  228. }
  229. int tcf_unregister_action(struct tc_action_ops *act)
  230. {
  231. struct tc_action_ops *a, **ap;
  232. int err = -ENOENT;
  233. write_lock(&act_mod_lock);
  234. for (ap = &act_base; (a = *ap) != NULL; ap = &a->next)
  235. if (a == act)
  236. break;
  237. if (a) {
  238. *ap = a->next;
  239. a->next = NULL;
  240. err = 0;
  241. }
  242. write_unlock(&act_mod_lock);
  243. return err;
  244. }
  245. /* lookup by name */
  246. static struct tc_action_ops *tc_lookup_action_n(char *kind)
  247. {
  248. struct tc_action_ops *a = NULL;
  249. if (kind) {
  250. read_lock(&act_mod_lock);
  251. for (a = act_base; a; a = a->next) {
  252. if (strcmp(kind, a->kind) == 0) {
  253. if (!try_module_get(a->owner)) {
  254. read_unlock(&act_mod_lock);
  255. return NULL;
  256. }
  257. break;
  258. }
  259. }
  260. read_unlock(&act_mod_lock);
  261. }
  262. return a;
  263. }
  264. /* lookup by rtattr */
  265. static struct tc_action_ops *tc_lookup_action(struct rtattr *kind)
  266. {
  267. struct tc_action_ops *a = NULL;
  268. if (kind) {
  269. read_lock(&act_mod_lock);
  270. for (a = act_base; a; a = a->next) {
  271. if (rtattr_strcmp(kind, a->kind) == 0) {
  272. if (!try_module_get(a->owner)) {
  273. read_unlock(&act_mod_lock);
  274. return NULL;
  275. }
  276. break;
  277. }
  278. }
  279. read_unlock(&act_mod_lock);
  280. }
  281. return a;
  282. }
  283. #if 0
  284. /* lookup by id */
  285. static struct tc_action_ops *tc_lookup_action_id(u32 type)
  286. {
  287. struct tc_action_ops *a = NULL;
  288. if (type) {
  289. read_lock(&act_mod_lock);
  290. for (a = act_base; a; a = a->next) {
  291. if (a->type == type) {
  292. if (!try_module_get(a->owner)) {
  293. read_unlock(&act_mod_lock);
  294. return NULL;
  295. }
  296. break;
  297. }
  298. }
  299. read_unlock(&act_mod_lock);
  300. }
  301. return a;
  302. }
  303. #endif
  304. int tcf_action_exec(struct sk_buff *skb, struct tc_action *act,
  305. struct tcf_result *res)
  306. {
  307. struct tc_action *a;
  308. int ret = -1;
  309. if (skb->tc_verd & TC_NCLS) {
  310. skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
  311. ret = TC_ACT_OK;
  312. goto exec_done;
  313. }
  314. while ((a = act) != NULL) {
  315. repeat:
  316. if (a->ops && a->ops->act) {
  317. ret = a->ops->act(skb, a, res);
  318. if (TC_MUNGED & skb->tc_verd) {
  319. /* copied already, allow trampling */
  320. skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
  321. skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
  322. }
  323. if (ret == TC_ACT_REPEAT)
  324. goto repeat; /* we need a ttl - JHS */
  325. if (ret != TC_ACT_PIPE)
  326. goto exec_done;
  327. }
  328. act = a->next;
  329. }
  330. exec_done:
  331. return ret;
  332. }
  333. void tcf_action_destroy(struct tc_action *act, int bind)
  334. {
  335. struct tc_action *a;
  336. for (a = act; a; a = act) {
  337. if (a->ops && a->ops->cleanup) {
  338. if (a->ops->cleanup(a, bind) == ACT_P_DELETED)
  339. module_put(a->ops->owner);
  340. act = act->next;
  341. kfree(a);
  342. } else { /*FIXME: Remove later - catch insertion bugs*/
  343. printk("tcf_action_destroy: BUG? destroying NULL ops\n");
  344. act = act->next;
  345. kfree(a);
  346. }
  347. }
  348. }
  349. int
  350. tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  351. {
  352. int err = -EINVAL;
  353. if (a->ops == NULL || a->ops->dump == NULL)
  354. return err;
  355. return a->ops->dump(skb, a, bind, ref);
  356. }
  357. int
  358. tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  359. {
  360. int err = -EINVAL;
  361. unsigned char *b = skb_tail_pointer(skb);
  362. struct rtattr *r;
  363. if (a->ops == NULL || a->ops->dump == NULL)
  364. return err;
  365. RTA_PUT(skb, TCA_KIND, IFNAMSIZ, a->ops->kind);
  366. if (tcf_action_copy_stats(skb, a, 0))
  367. goto rtattr_failure;
  368. r = (struct rtattr *)skb_tail_pointer(skb);
  369. RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
  370. if ((err = tcf_action_dump_old(skb, a, bind, ref)) > 0) {
  371. r->rta_len = skb_tail_pointer(skb) - (u8 *)r;
  372. return err;
  373. }
  374. rtattr_failure:
  375. nlmsg_trim(skb, b);
  376. return -1;
  377. }
  378. int
  379. tcf_action_dump(struct sk_buff *skb, struct tc_action *act, int bind, int ref)
  380. {
  381. struct tc_action *a;
  382. int err = -EINVAL;
  383. unsigned char *b = skb_tail_pointer(skb);
  384. struct rtattr *r ;
  385. while ((a = act) != NULL) {
  386. r = (struct rtattr *)skb_tail_pointer(skb);
  387. act = a->next;
  388. RTA_PUT(skb, a->order, 0, NULL);
  389. err = tcf_action_dump_1(skb, a, bind, ref);
  390. if (err < 0)
  391. goto errout;
  392. r->rta_len = skb_tail_pointer(skb) - (u8 *)r;
  393. }
  394. return 0;
  395. rtattr_failure:
  396. err = -EINVAL;
  397. errout:
  398. nlmsg_trim(skb, b);
  399. return err;
  400. }
  401. struct tc_action *tcf_action_init_1(struct rtattr *rta, struct rtattr *est,
  402. char *name, int ovr, int bind, int *err)
  403. {
  404. struct tc_action *a;
  405. struct tc_action_ops *a_o;
  406. char act_name[IFNAMSIZ];
  407. struct rtattr *tb[TCA_ACT_MAX+1];
  408. struct rtattr *kind;
  409. *err = -EINVAL;
  410. if (name == NULL) {
  411. if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
  412. goto err_out;
  413. kind = tb[TCA_ACT_KIND-1];
  414. if (kind == NULL)
  415. goto err_out;
  416. if (rtattr_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
  417. goto err_out;
  418. } else {
  419. if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
  420. goto err_out;
  421. }
  422. a_o = tc_lookup_action_n(act_name);
  423. if (a_o == NULL) {
  424. #ifdef CONFIG_KMOD
  425. rtnl_unlock();
  426. request_module("act_%s", act_name);
  427. rtnl_lock();
  428. a_o = tc_lookup_action_n(act_name);
  429. /* We dropped the RTNL semaphore in order to
  430. * perform the module load. So, even if we
  431. * succeeded in loading the module we have to
  432. * tell the caller to replay the request. We
  433. * indicate this using -EAGAIN.
  434. */
  435. if (a_o != NULL) {
  436. *err = -EAGAIN;
  437. goto err_mod;
  438. }
  439. #endif
  440. *err = -ENOENT;
  441. goto err_out;
  442. }
  443. *err = -ENOMEM;
  444. a = kzalloc(sizeof(*a), GFP_KERNEL);
  445. if (a == NULL)
  446. goto err_mod;
  447. /* backward compatibility for policer */
  448. if (name == NULL)
  449. *err = a_o->init(tb[TCA_ACT_OPTIONS-1], est, a, ovr, bind);
  450. else
  451. *err = a_o->init(rta, est, a, ovr, bind);
  452. if (*err < 0)
  453. goto err_free;
  454. /* module count goes up only when brand new policy is created
  455. if it exists and is only bound to in a_o->init() then
  456. ACT_P_CREATED is not returned (a zero is).
  457. */
  458. if (*err != ACT_P_CREATED)
  459. module_put(a_o->owner);
  460. a->ops = a_o;
  461. *err = 0;
  462. return a;
  463. err_free:
  464. kfree(a);
  465. err_mod:
  466. module_put(a_o->owner);
  467. err_out:
  468. return NULL;
  469. }
  470. struct tc_action *tcf_action_init(struct rtattr *rta, struct rtattr *est,
  471. char *name, int ovr, int bind, int *err)
  472. {
  473. struct rtattr *tb[TCA_ACT_MAX_PRIO+1];
  474. struct tc_action *head = NULL, *act, *act_prev = NULL;
  475. int i;
  476. if (rtattr_parse_nested(tb, TCA_ACT_MAX_PRIO, rta) < 0) {
  477. *err = -EINVAL;
  478. return head;
  479. }
  480. for (i=0; i < TCA_ACT_MAX_PRIO && tb[i]; i++) {
  481. act = tcf_action_init_1(tb[i], est, name, ovr, bind, err);
  482. if (act == NULL)
  483. goto err;
  484. act->order = i+1;
  485. if (head == NULL)
  486. head = act;
  487. else
  488. act_prev->next = act;
  489. act_prev = act;
  490. }
  491. return head;
  492. err:
  493. if (head != NULL)
  494. tcf_action_destroy(head, bind);
  495. return NULL;
  496. }
  497. int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
  498. int compat_mode)
  499. {
  500. int err = 0;
  501. struct gnet_dump d;
  502. struct tcf_act_hdr *h = a->priv;
  503. if (h == NULL)
  504. goto errout;
  505. /* compat_mode being true specifies a call that is supposed
  506. * to add additional backward compatiblity statistic TLVs.
  507. */
  508. if (compat_mode) {
  509. if (a->type == TCA_OLD_COMPAT)
  510. err = gnet_stats_start_copy_compat(skb, 0,
  511. TCA_STATS, TCA_XSTATS, &h->tcf_lock, &d);
  512. else
  513. return 0;
  514. } else
  515. err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
  516. &h->tcf_lock, &d);
  517. if (err < 0)
  518. goto errout;
  519. if (a->ops != NULL && a->ops->get_stats != NULL)
  520. if (a->ops->get_stats(skb, a) < 0)
  521. goto errout;
  522. if (gnet_stats_copy_basic(&d, &h->tcf_bstats) < 0 ||
  523. gnet_stats_copy_rate_est(&d, &h->tcf_rate_est) < 0 ||
  524. gnet_stats_copy_queue(&d, &h->tcf_qstats) < 0)
  525. goto errout;
  526. if (gnet_stats_finish_copy(&d) < 0)
  527. goto errout;
  528. return 0;
  529. errout:
  530. return -1;
  531. }
  532. static int
  533. tca_get_fill(struct sk_buff *skb, struct tc_action *a, u32 pid, u32 seq,
  534. u16 flags, int event, int bind, int ref)
  535. {
  536. struct tcamsg *t;
  537. struct nlmsghdr *nlh;
  538. unsigned char *b = skb_tail_pointer(skb);
  539. struct rtattr *x;
  540. nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
  541. t = NLMSG_DATA(nlh);
  542. t->tca_family = AF_UNSPEC;
  543. t->tca__pad1 = 0;
  544. t->tca__pad2 = 0;
  545. x = (struct rtattr *)skb_tail_pointer(skb);
  546. RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
  547. if (tcf_action_dump(skb, a, bind, ref) < 0)
  548. goto rtattr_failure;
  549. x->rta_len = skb_tail_pointer(skb) - (u8 *)x;
  550. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  551. return skb->len;
  552. rtattr_failure:
  553. nlmsg_failure:
  554. nlmsg_trim(skb, b);
  555. return -1;
  556. }
  557. static int
  558. act_get_notify(u32 pid, struct nlmsghdr *n, struct tc_action *a, int event)
  559. {
  560. struct sk_buff *skb;
  561. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  562. if (!skb)
  563. return -ENOBUFS;
  564. if (tca_get_fill(skb, a, pid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
  565. kfree_skb(skb);
  566. return -EINVAL;
  567. }
  568. return rtnl_unicast(skb, pid);
  569. }
  570. static struct tc_action *
  571. tcf_action_get_1(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int *err)
  572. {
  573. struct rtattr *tb[TCA_ACT_MAX+1];
  574. struct tc_action *a;
  575. int index;
  576. *err = -EINVAL;
  577. if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
  578. return NULL;
  579. if (tb[TCA_ACT_INDEX - 1] == NULL ||
  580. RTA_PAYLOAD(tb[TCA_ACT_INDEX - 1]) < sizeof(index))
  581. return NULL;
  582. index = *(int *)RTA_DATA(tb[TCA_ACT_INDEX - 1]);
  583. *err = -ENOMEM;
  584. a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
  585. if (a == NULL)
  586. return NULL;
  587. *err = -EINVAL;
  588. a->ops = tc_lookup_action(tb[TCA_ACT_KIND - 1]);
  589. if (a->ops == NULL)
  590. goto err_free;
  591. if (a->ops->lookup == NULL)
  592. goto err_mod;
  593. *err = -ENOENT;
  594. if (a->ops->lookup(a, index) == 0)
  595. goto err_mod;
  596. module_put(a->ops->owner);
  597. *err = 0;
  598. return a;
  599. err_mod:
  600. module_put(a->ops->owner);
  601. err_free:
  602. kfree(a);
  603. return NULL;
  604. }
  605. static void cleanup_a(struct tc_action *act)
  606. {
  607. struct tc_action *a;
  608. for (a = act; a; a = act) {
  609. act = a->next;
  610. kfree(a);
  611. }
  612. }
  613. static struct tc_action *create_a(int i)
  614. {
  615. struct tc_action *act;
  616. act = kzalloc(sizeof(*act), GFP_KERNEL);
  617. if (act == NULL) {
  618. printk("create_a: failed to alloc!\n");
  619. return NULL;
  620. }
  621. act->order = i;
  622. return act;
  623. }
  624. static int tca_action_flush(struct rtattr *rta, struct nlmsghdr *n, u32 pid)
  625. {
  626. struct sk_buff *skb;
  627. unsigned char *b;
  628. struct nlmsghdr *nlh;
  629. struct tcamsg *t;
  630. struct netlink_callback dcb;
  631. struct rtattr *x;
  632. struct rtattr *tb[TCA_ACT_MAX+1];
  633. struct rtattr *kind;
  634. struct tc_action *a = create_a(0);
  635. int err = -EINVAL;
  636. if (a == NULL) {
  637. printk("tca_action_flush: couldnt create tc_action\n");
  638. return err;
  639. }
  640. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  641. if (!skb) {
  642. printk("tca_action_flush: failed skb alloc\n");
  643. kfree(a);
  644. return -ENOBUFS;
  645. }
  646. b = skb_tail_pointer(skb);
  647. if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0)
  648. goto err_out;
  649. kind = tb[TCA_ACT_KIND-1];
  650. a->ops = tc_lookup_action(kind);
  651. if (a->ops == NULL)
  652. goto err_out;
  653. nlh = NLMSG_PUT(skb, pid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t));
  654. t = NLMSG_DATA(nlh);
  655. t->tca_family = AF_UNSPEC;
  656. t->tca__pad1 = 0;
  657. t->tca__pad2 = 0;
  658. x = (struct rtattr *)skb_tail_pointer(skb);
  659. RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
  660. err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
  661. if (err < 0)
  662. goto rtattr_failure;
  663. x->rta_len = skb_tail_pointer(skb) - (u8 *)x;
  664. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  665. nlh->nlmsg_flags |= NLM_F_ROOT;
  666. module_put(a->ops->owner);
  667. kfree(a);
  668. err = rtnetlink_send(skb, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
  669. if (err > 0)
  670. return 0;
  671. return err;
  672. rtattr_failure:
  673. nlmsg_failure:
  674. module_put(a->ops->owner);
  675. err_out:
  676. kfree_skb(skb);
  677. kfree(a);
  678. return err;
  679. }
  680. static int
  681. tca_action_gd(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int event)
  682. {
  683. int i, ret = 0;
  684. struct rtattr *tb[TCA_ACT_MAX_PRIO+1];
  685. struct tc_action *head = NULL, *act, *act_prev = NULL;
  686. if (rtattr_parse_nested(tb, TCA_ACT_MAX_PRIO, rta) < 0)
  687. return -EINVAL;
  688. if (event == RTM_DELACTION && n->nlmsg_flags&NLM_F_ROOT) {
  689. if (tb[0] != NULL && tb[1] == NULL)
  690. return tca_action_flush(tb[0], n, pid);
  691. }
  692. for (i=0; i < TCA_ACT_MAX_PRIO && tb[i]; i++) {
  693. act = tcf_action_get_1(tb[i], n, pid, &ret);
  694. if (act == NULL)
  695. goto err;
  696. act->order = i+1;
  697. if (head == NULL)
  698. head = act;
  699. else
  700. act_prev->next = act;
  701. act_prev = act;
  702. }
  703. if (event == RTM_GETACTION)
  704. ret = act_get_notify(pid, n, head, event);
  705. else { /* delete */
  706. struct sk_buff *skb;
  707. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  708. if (!skb) {
  709. ret = -ENOBUFS;
  710. goto err;
  711. }
  712. if (tca_get_fill(skb, head, pid, n->nlmsg_seq, 0, event,
  713. 0, 1) <= 0) {
  714. kfree_skb(skb);
  715. ret = -EINVAL;
  716. goto err;
  717. }
  718. /* now do the delete */
  719. tcf_action_destroy(head, 0);
  720. ret = rtnetlink_send(skb, pid, RTNLGRP_TC,
  721. n->nlmsg_flags&NLM_F_ECHO);
  722. if (ret > 0)
  723. return 0;
  724. return ret;
  725. }
  726. err:
  727. cleanup_a(head);
  728. return ret;
  729. }
  730. static int tcf_add_notify(struct tc_action *a, u32 pid, u32 seq, int event,
  731. u16 flags)
  732. {
  733. struct tcamsg *t;
  734. struct nlmsghdr *nlh;
  735. struct sk_buff *skb;
  736. struct rtattr *x;
  737. unsigned char *b;
  738. int err = 0;
  739. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  740. if (!skb)
  741. return -ENOBUFS;
  742. b = skb_tail_pointer(skb);
  743. nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
  744. t = NLMSG_DATA(nlh);
  745. t->tca_family = AF_UNSPEC;
  746. t->tca__pad1 = 0;
  747. t->tca__pad2 = 0;
  748. x = (struct rtattr *)skb_tail_pointer(skb);
  749. RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
  750. if (tcf_action_dump(skb, a, 0, 0) < 0)
  751. goto rtattr_failure;
  752. x->rta_len = skb_tail_pointer(skb) - (u8 *)x;
  753. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  754. NETLINK_CB(skb).dst_group = RTNLGRP_TC;
  755. err = rtnetlink_send(skb, pid, RTNLGRP_TC, flags&NLM_F_ECHO);
  756. if (err > 0)
  757. err = 0;
  758. return err;
  759. rtattr_failure:
  760. nlmsg_failure:
  761. kfree_skb(skb);
  762. return -1;
  763. }
  764. static int
  765. tcf_action_add(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int ovr)
  766. {
  767. int ret = 0;
  768. struct tc_action *act;
  769. struct tc_action *a;
  770. u32 seq = n->nlmsg_seq;
  771. act = tcf_action_init(rta, NULL, NULL, ovr, 0, &ret);
  772. if (act == NULL)
  773. goto done;
  774. /* dump then free all the actions after update; inserted policy
  775. * stays intact
  776. * */
  777. ret = tcf_add_notify(act, pid, seq, RTM_NEWACTION, n->nlmsg_flags);
  778. for (a = act; a; a = act) {
  779. act = a->next;
  780. kfree(a);
  781. }
  782. done:
  783. return ret;
  784. }
  785. static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
  786. {
  787. struct rtattr **tca = arg;
  788. u32 pid = skb ? NETLINK_CB(skb).pid : 0;
  789. int ret = 0, ovr = 0;
  790. if (tca[TCA_ACT_TAB-1] == NULL) {
  791. printk("tc_ctl_action: received NO action attribs\n");
  792. return -EINVAL;
  793. }
  794. /* n->nlmsg_flags&NLM_F_CREATE
  795. * */
  796. switch (n->nlmsg_type) {
  797. case RTM_NEWACTION:
  798. /* we are going to assume all other flags
  799. * imply create only if it doesnt exist
  800. * Note that CREATE | EXCL implies that
  801. * but since we want avoid ambiguity (eg when flags
  802. * is zero) then just set this
  803. */
  804. if (n->nlmsg_flags&NLM_F_REPLACE)
  805. ovr = 1;
  806. replay:
  807. ret = tcf_action_add(tca[TCA_ACT_TAB-1], n, pid, ovr);
  808. if (ret == -EAGAIN)
  809. goto replay;
  810. break;
  811. case RTM_DELACTION:
  812. ret = tca_action_gd(tca[TCA_ACT_TAB-1], n, pid, RTM_DELACTION);
  813. break;
  814. case RTM_GETACTION:
  815. ret = tca_action_gd(tca[TCA_ACT_TAB-1], n, pid, RTM_GETACTION);
  816. break;
  817. default:
  818. BUG();
  819. }
  820. return ret;
  821. }
  822. static struct rtattr *
  823. find_dump_kind(struct nlmsghdr *n)
  824. {
  825. struct rtattr *tb1, *tb2[TCA_ACT_MAX+1];
  826. struct rtattr *tb[TCA_ACT_MAX_PRIO + 1];
  827. struct rtattr *rta[TCAA_MAX + 1];
  828. struct rtattr *kind;
  829. int min_len = NLMSG_LENGTH(sizeof(struct tcamsg));
  830. int attrlen = n->nlmsg_len - NLMSG_ALIGN(min_len);
  831. struct rtattr *attr = (void *) n + NLMSG_ALIGN(min_len);
  832. if (rtattr_parse(rta, TCAA_MAX, attr, attrlen) < 0)
  833. return NULL;
  834. tb1 = rta[TCA_ACT_TAB - 1];
  835. if (tb1 == NULL)
  836. return NULL;
  837. if (rtattr_parse(tb, TCA_ACT_MAX_PRIO, RTA_DATA(tb1),
  838. NLMSG_ALIGN(RTA_PAYLOAD(tb1))) < 0)
  839. return NULL;
  840. if (tb[0] == NULL)
  841. return NULL;
  842. if (rtattr_parse(tb2, TCA_ACT_MAX, RTA_DATA(tb[0]),
  843. RTA_PAYLOAD(tb[0])) < 0)
  844. return NULL;
  845. kind = tb2[TCA_ACT_KIND-1];
  846. return kind;
  847. }
  848. static int
  849. tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
  850. {
  851. struct nlmsghdr *nlh;
  852. unsigned char *b = skb_tail_pointer(skb);
  853. struct rtattr *x;
  854. struct tc_action_ops *a_o;
  855. struct tc_action a;
  856. int ret = 0;
  857. struct tcamsg *t = (struct tcamsg *) NLMSG_DATA(cb->nlh);
  858. struct rtattr *kind = find_dump_kind(cb->nlh);
  859. if (kind == NULL) {
  860. printk("tc_dump_action: action bad kind\n");
  861. return 0;
  862. }
  863. a_o = tc_lookup_action(kind);
  864. if (a_o == NULL) {
  865. return 0;
  866. }
  867. memset(&a, 0, sizeof(struct tc_action));
  868. a.ops = a_o;
  869. if (a_o->walk == NULL) {
  870. printk("tc_dump_action: %s !capable of dumping table\n", a_o->kind);
  871. goto rtattr_failure;
  872. }
  873. nlh = NLMSG_PUT(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
  874. cb->nlh->nlmsg_type, sizeof(*t));
  875. t = NLMSG_DATA(nlh);
  876. t->tca_family = AF_UNSPEC;
  877. t->tca__pad1 = 0;
  878. t->tca__pad2 = 0;
  879. x = (struct rtattr *)skb_tail_pointer(skb);
  880. RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
  881. ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
  882. if (ret < 0)
  883. goto rtattr_failure;
  884. if (ret > 0) {
  885. x->rta_len = skb_tail_pointer(skb) - (u8 *)x;
  886. ret = skb->len;
  887. } else
  888. nlmsg_trim(skb, x);
  889. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  890. if (NETLINK_CB(cb->skb).pid && ret)
  891. nlh->nlmsg_flags |= NLM_F_MULTI;
  892. module_put(a_o->owner);
  893. return skb->len;
  894. rtattr_failure:
  895. nlmsg_failure:
  896. module_put(a_o->owner);
  897. nlmsg_trim(skb, b);
  898. return skb->len;
  899. }
  900. static int __init tc_action_init(void)
  901. {
  902. rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL);
  903. rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL);
  904. rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action);
  905. return 0;
  906. }
  907. subsys_initcall(tc_action_init);
  908. EXPORT_SYMBOL(tcf_register_action);
  909. EXPORT_SYMBOL(tcf_unregister_action);
  910. EXPORT_SYMBOL(tcf_action_exec);
  911. EXPORT_SYMBOL(tcf_action_dump_1);