act_api.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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_KMOD
  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 = -EINVAL;
  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 -ENOBUFS;
  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. nla_nest_end(skb, nest);
  681. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  682. nlh->nlmsg_flags |= NLM_F_ROOT;
  683. module_put(a->ops->owner);
  684. kfree(a);
  685. err = rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
  686. if (err > 0)
  687. return 0;
  688. return err;
  689. nla_put_failure:
  690. nlmsg_failure:
  691. module_put(a->ops->owner);
  692. err_out:
  693. kfree_skb(skb);
  694. kfree(a);
  695. return err;
  696. }
  697. static int
  698. tca_action_gd(struct nlattr *nla, struct nlmsghdr *n, u32 pid, int event)
  699. {
  700. int i, ret;
  701. struct nlattr *tb[TCA_ACT_MAX_PRIO+1];
  702. struct tc_action *head = NULL, *act, *act_prev = NULL;
  703. ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
  704. if (ret < 0)
  705. return ret;
  706. if (event == RTM_DELACTION && n->nlmsg_flags&NLM_F_ROOT) {
  707. if (tb[0] != NULL && tb[1] == NULL)
  708. return tca_action_flush(tb[0], n, pid);
  709. }
  710. for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
  711. act = tcf_action_get_1(tb[i], n, pid);
  712. if (IS_ERR(act)) {
  713. ret = PTR_ERR(act);
  714. goto err;
  715. }
  716. act->order = i;
  717. if (head == NULL)
  718. head = act;
  719. else
  720. act_prev->next = act;
  721. act_prev = act;
  722. }
  723. if (event == RTM_GETACTION)
  724. ret = act_get_notify(pid, n, head, event);
  725. else { /* delete */
  726. struct sk_buff *skb;
  727. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  728. if (!skb) {
  729. ret = -ENOBUFS;
  730. goto err;
  731. }
  732. if (tca_get_fill(skb, head, pid, n->nlmsg_seq, 0, event,
  733. 0, 1) <= 0) {
  734. kfree_skb(skb);
  735. ret = -EINVAL;
  736. goto err;
  737. }
  738. /* now do the delete */
  739. tcf_action_destroy(head, 0);
  740. ret = rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC,
  741. n->nlmsg_flags&NLM_F_ECHO);
  742. if (ret > 0)
  743. return 0;
  744. return ret;
  745. }
  746. err:
  747. cleanup_a(head);
  748. return ret;
  749. }
  750. static int tcf_add_notify(struct tc_action *a, u32 pid, u32 seq, int event,
  751. u16 flags)
  752. {
  753. struct tcamsg *t;
  754. struct nlmsghdr *nlh;
  755. struct sk_buff *skb;
  756. struct nlattr *nest;
  757. unsigned char *b;
  758. int err = 0;
  759. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  760. if (!skb)
  761. return -ENOBUFS;
  762. b = skb_tail_pointer(skb);
  763. nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
  764. t = NLMSG_DATA(nlh);
  765. t->tca_family = AF_UNSPEC;
  766. t->tca__pad1 = 0;
  767. t->tca__pad2 = 0;
  768. nest = nla_nest_start(skb, TCA_ACT_TAB);
  769. if (nest == NULL)
  770. goto nla_put_failure;
  771. if (tcf_action_dump(skb, a, 0, 0) < 0)
  772. goto nla_put_failure;
  773. nla_nest_end(skb, nest);
  774. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  775. NETLINK_CB(skb).dst_group = RTNLGRP_TC;
  776. err = rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC, flags&NLM_F_ECHO);
  777. if (err > 0)
  778. err = 0;
  779. return err;
  780. nla_put_failure:
  781. nlmsg_failure:
  782. kfree_skb(skb);
  783. return -1;
  784. }
  785. static int
  786. tcf_action_add(struct nlattr *nla, struct nlmsghdr *n, u32 pid, int ovr)
  787. {
  788. int ret = 0;
  789. struct tc_action *act;
  790. struct tc_action *a;
  791. u32 seq = n->nlmsg_seq;
  792. act = tcf_action_init(nla, NULL, NULL, ovr, 0);
  793. if (act == NULL)
  794. goto done;
  795. if (IS_ERR(act)) {
  796. ret = PTR_ERR(act);
  797. goto done;
  798. }
  799. /* dump then free all the actions after update; inserted policy
  800. * stays intact
  801. * */
  802. ret = tcf_add_notify(act, pid, seq, RTM_NEWACTION, n->nlmsg_flags);
  803. for (a = act; a; a = act) {
  804. act = a->next;
  805. kfree(a);
  806. }
  807. done:
  808. return ret;
  809. }
  810. static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
  811. {
  812. struct net *net = sock_net(skb->sk);
  813. struct nlattr *tca[TCA_ACT_MAX + 1];
  814. u32 pid = skb ? NETLINK_CB(skb).pid : 0;
  815. int ret = 0, ovr = 0;
  816. if (net != &init_net)
  817. return -EINVAL;
  818. ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
  819. if (ret < 0)
  820. return ret;
  821. if (tca[TCA_ACT_TAB] == NULL) {
  822. printk("tc_ctl_action: received NO action attribs\n");
  823. return -EINVAL;
  824. }
  825. /* n->nlmsg_flags&NLM_F_CREATE
  826. * */
  827. switch (n->nlmsg_type) {
  828. case RTM_NEWACTION:
  829. /* we are going to assume all other flags
  830. * imply create only if it doesnt exist
  831. * Note that CREATE | EXCL implies that
  832. * but since we want avoid ambiguity (eg when flags
  833. * is zero) then just set this
  834. */
  835. if (n->nlmsg_flags&NLM_F_REPLACE)
  836. ovr = 1;
  837. replay:
  838. ret = tcf_action_add(tca[TCA_ACT_TAB], n, pid, ovr);
  839. if (ret == -EAGAIN)
  840. goto replay;
  841. break;
  842. case RTM_DELACTION:
  843. ret = tca_action_gd(tca[TCA_ACT_TAB], n, pid, RTM_DELACTION);
  844. break;
  845. case RTM_GETACTION:
  846. ret = tca_action_gd(tca[TCA_ACT_TAB], n, pid, RTM_GETACTION);
  847. break;
  848. default:
  849. BUG();
  850. }
  851. return ret;
  852. }
  853. static struct nlattr *
  854. find_dump_kind(struct nlmsghdr *n)
  855. {
  856. struct nlattr *tb1, *tb2[TCA_ACT_MAX+1];
  857. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  858. struct nlattr *nla[TCAA_MAX + 1];
  859. struct nlattr *kind;
  860. if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
  861. return NULL;
  862. tb1 = nla[TCA_ACT_TAB];
  863. if (tb1 == NULL)
  864. return NULL;
  865. if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
  866. NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
  867. return NULL;
  868. if (tb[1] == NULL)
  869. return NULL;
  870. if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
  871. nla_len(tb[1]), NULL) < 0)
  872. return NULL;
  873. kind = tb2[TCA_ACT_KIND];
  874. return kind;
  875. }
  876. static int
  877. tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
  878. {
  879. struct net *net = sock_net(skb->sk);
  880. struct nlmsghdr *nlh;
  881. unsigned char *b = skb_tail_pointer(skb);
  882. struct nlattr *nest;
  883. struct tc_action_ops *a_o;
  884. struct tc_action a;
  885. int ret = 0;
  886. struct tcamsg *t = (struct tcamsg *) NLMSG_DATA(cb->nlh);
  887. struct nlattr *kind = find_dump_kind(cb->nlh);
  888. if (net != &init_net)
  889. return 0;
  890. if (kind == NULL) {
  891. printk("tc_dump_action: action bad kind\n");
  892. return 0;
  893. }
  894. a_o = tc_lookup_action(kind);
  895. if (a_o == NULL) {
  896. return 0;
  897. }
  898. memset(&a, 0, sizeof(struct tc_action));
  899. a.ops = a_o;
  900. if (a_o->walk == NULL) {
  901. printk("tc_dump_action: %s !capable of dumping table\n", a_o->kind);
  902. goto nla_put_failure;
  903. }
  904. nlh = NLMSG_PUT(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
  905. cb->nlh->nlmsg_type, sizeof(*t));
  906. t = NLMSG_DATA(nlh);
  907. t->tca_family = AF_UNSPEC;
  908. t->tca__pad1 = 0;
  909. t->tca__pad2 = 0;
  910. nest = nla_nest_start(skb, TCA_ACT_TAB);
  911. if (nest == NULL)
  912. goto nla_put_failure;
  913. ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
  914. if (ret < 0)
  915. goto nla_put_failure;
  916. if (ret > 0) {
  917. nla_nest_end(skb, nest);
  918. ret = skb->len;
  919. } else
  920. nla_nest_cancel(skb, nest);
  921. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  922. if (NETLINK_CB(cb->skb).pid && ret)
  923. nlh->nlmsg_flags |= NLM_F_MULTI;
  924. module_put(a_o->owner);
  925. return skb->len;
  926. nla_put_failure:
  927. nlmsg_failure:
  928. module_put(a_o->owner);
  929. nlmsg_trim(skb, b);
  930. return skb->len;
  931. }
  932. static int __init tc_action_init(void)
  933. {
  934. rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL);
  935. rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL);
  936. rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action);
  937. return 0;
  938. }
  939. subsys_initcall(tc_action_init);