act_api.c 23 KB

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