act_api.c 23 KB

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