act_api.c 23 KB

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