act_api.c 23 KB

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