act_api.c 23 KB

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