act_api.c 23 KB

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