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