act_api.c 23 KB

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